Skip to content

Commit 698e20c

Browse files
initial version of fix
1 parent f41f07f commit 698e20c

File tree

4 files changed

+62
-13
lines changed

4 files changed

+62
-13
lines changed

codefresh/cfclient/context.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ func (context *Context) GetID() string {
3131
return context.Metadata.Name
3232
}
3333

34-
func (client *Client) GetContext(name string) (*Context, error) {
35-
fullPath := fmt.Sprintf("/contexts/%s?decrypt=true", url.PathEscape(name))
34+
func (client *Client) GetContext(name string, decrypt bool) (*Context, error) {
35+
fullPath := fmt.Sprintf("/contexts/%s", url.PathEscape(name))
36+
37+
if decrypt {
38+
fullPath += "?decrypt=true"
39+
}
40+
3641
opts := RequestOptions{
3742
Path: fullPath,
3843
Method: "GET",

codefresh/data_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func dataSourceContextRead(d *schema.ResourceData, meta interface{}) error {
3636
var err error
3737

3838
if name, nameOk := d.GetOk("name"); nameOk {
39-
context, err = client.GetContext(name.(string))
39+
context, err = client.GetContext(name.(string), true)
4040
} else {
4141
return fmt.Errorf("data.codefresh_context - must specify name")
4242
}

codefresh/resource_context.go

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ var supportedContextType = []string{
2828
contextSecretYaml,
2929
}
3030

31+
var encryptedContextTypes = []string{
32+
contextSecret,
33+
contextSecretYaml,
34+
}
35+
3136
func getConflictingContexts(context string) []string {
3237
var conflictingTypes []string
3338
normalizedContext := schemautil.MustNormalizeFieldName(context)
@@ -57,6 +62,12 @@ func resourceContext() *schema.Resource {
5762
Required: true,
5863
ForceNew: true,
5964
},
65+
"decrypt_spec": {
66+
Type: schema.TypeBool,
67+
Default: true,
68+
Optional: true,
69+
Description: "Whether to allow decryption of context spec for encrypted contexts on read. If set to false context content diff will not be calculated against the API. Must be set to false if `forbidDecrypt` feature flag on Codefresh platfrom is enabled",
70+
},
6071
"spec": {
6172
Description: "The context's specs.",
6273
Type: schema.TypeList,
@@ -174,12 +185,18 @@ func resourceContextRead(d *schema.ResourceData, meta interface{}) error {
174185

175186
contextName := d.Id()
176187

188+
currentContextType := getContextTypeFromResource(d)
189+
190+
// Explicitly set decypt flag to true only if context type is encrypted and decrypt_spec is set to true
191+
setExplicitDecrypt := contains(encryptedContextTypes, currentContextType) && d.Get("decrypt_spec").(bool)
192+
177193
if contextName == "" {
178194
d.SetId("")
179195
return nil
180196
}
181197

182-
context, err := client.GetContext(contextName)
198+
context, err := client.GetContext(contextName, setExplicitDecrypt)
199+
183200
if err != nil {
184201
log.Printf("[DEBUG] Error while getting context. Error = %v", contextName)
185202
return err
@@ -225,14 +242,22 @@ func resourceContextDelete(d *schema.ResourceData, meta interface{}) error {
225242
func mapContextToResource(context cfclient.Context, d *schema.ResourceData) error {
226243

227244
err := d.Set("name", context.Metadata.Name)
245+
228246
if err != nil {
229247
return err
230248
}
231249

232-
err = d.Set("spec", flattenContextSpec(context.Spec))
233-
if err != nil {
234-
log.Printf("[DEBUG] Failed to flatten Context spec = %v", context.Spec)
235-
return err
250+
currentContextType := getContextTypeFromResource(d)
251+
252+
// Read spec from API if context is not encrypted or decrypt_spec is set to true explicitly
253+
if d.Get("decrypt_spec").(bool) || !contains(encryptedContextTypes, currentContextType) {
254+
255+
err = d.Set("spec", flattenContextSpec(context.Spec))
256+
257+
if err != nil {
258+
log.Printf("[DEBUG] Failed to flatten Context spec = %v", context.Spec)
259+
return err
260+
}
236261
}
237262

238263
return nil
@@ -253,7 +278,6 @@ func flattenContextSpec(spec cfclient.ContextSpec) []interface{} {
253278
case contextAzureStorage:
254279
m[schemautil.MustNormalizeFieldName(currentContextType)] = storageContext.FlattenAzureStorageContextConfig(spec)
255280
default:
256-
log.Printf("[DEBUG] Invalid context type = %v", currentContextType)
257281
return nil
258282
}
259283

@@ -319,3 +343,23 @@ func mapResourceToContext(d *schema.ResourceData) *cfclient.Context {
319343
},
320344
}
321345
}
346+
347+
func getContextTypeFromResource(d *schema.ResourceData) string {
348+
if _, ok := d.GetOk("spec.0." + schemautil.MustNormalizeFieldName(contextConfig) + ".0.data"); ok {
349+
return contextConfig
350+
} else if _, ok := d.GetOk("spec.0." + schemautil.MustNormalizeFieldName(contextSecret) + ".0.data"); ok {
351+
return contextSecret
352+
} else if _, ok := d.GetOk("spec.0." + schemautil.MustNormalizeFieldName(contextYaml) + ".0.data"); ok {
353+
return contextYaml
354+
} else if _, ok := d.GetOk("spec.0." + schemautil.MustNormalizeFieldName(contextSecretYaml) + ".0.data"); ok {
355+
return contextSecretYaml
356+
} else if _, ok := d.GetOk("spec.0." + schemautil.MustNormalizeFieldName(contextGoogleStorage) + ".0.data"); ok {
357+
return contextGoogleStorage
358+
} else if _, ok := d.GetOk("spec.0." + schemautil.MustNormalizeFieldName(contextS3Storage) + ".0.data"); ok {
359+
return contextS3Storage
360+
} else if _, ok := d.GetOk("spec.0." + schemautil.MustNormalizeFieldName(contextAzureStorage) + ".0.data"); ok {
361+
return contextAzureStorage
362+
}
363+
364+
return ""
365+
}

codefresh/resource_context_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func testAccCheckCodefreshContextExists(resource string) resource.TestCheckFunc
159159
contextID := rs.Primary.ID
160160

161161
apiClient := testAccProvider.Meta().(*cfclient.Client)
162-
_, err := apiClient.GetContext(contextID)
162+
_, err := apiClient.GetContext(contextID, true)
163163

164164
if err != nil {
165165
return fmt.Errorf("error fetching context with ID %s. %s", contextID, err)
@@ -177,7 +177,7 @@ func testAccCheckCodefreshContextDestroy(s *terraform.State) error {
177177
continue
178178
}
179179

180-
_, err := apiClient.GetContext(rs.Primary.ID)
180+
_, err := apiClient.GetContext(rs.Primary.ID, true)
181181

182182
if err == nil {
183183
return fmt.Errorf("Alert still exists")
@@ -204,7 +204,7 @@ resource "codefresh_context" "test" {
204204
205205
spec {
206206
config {
207-
data = {
207+
data = {
208208
%q = %q
209209
%q = %q
210210
}
@@ -223,7 +223,7 @@ resource "codefresh_context" "test" {
223223
224224
spec {
225225
secret {
226-
data = {
226+
data = {
227227
%q = %q
228228
%q = %q
229229
}

0 commit comments

Comments
 (0)