@@ -28,6 +28,11 @@ var supportedContextType = []string{
28
28
contextSecretYaml ,
29
29
}
30
30
31
+ var encryptedContextTypes = []string {
32
+ contextSecret ,
33
+ contextSecretYaml ,
34
+ }
35
+
31
36
func getConflictingContexts (context string ) []string {
32
37
var conflictingTypes []string
33
38
normalizedContext := schemautil .MustNormalizeFieldName (context )
@@ -57,6 +62,12 @@ func resourceContext() *schema.Resource {
57
62
Required : true ,
58
63
ForceNew : true ,
59
64
},
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
+ },
60
71
"spec" : {
61
72
Description : "The context's specs." ,
62
73
Type : schema .TypeList ,
@@ -174,12 +185,18 @@ func resourceContextRead(d *schema.ResourceData, meta interface{}) error {
174
185
175
186
contextName := d .Id ()
176
187
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
+
177
193
if contextName == "" {
178
194
d .SetId ("" )
179
195
return nil
180
196
}
181
197
182
- context , err := client .GetContext (contextName )
198
+ context , err := client .GetContext (contextName , setExplicitDecrypt )
199
+
183
200
if err != nil {
184
201
log .Printf ("[DEBUG] Error while getting context. Error = %v" , contextName )
185
202
return err
@@ -225,14 +242,22 @@ func resourceContextDelete(d *schema.ResourceData, meta interface{}) error {
225
242
func mapContextToResource (context cfclient.Context , d * schema.ResourceData ) error {
226
243
227
244
err := d .Set ("name" , context .Metadata .Name )
245
+
228
246
if err != nil {
229
247
return err
230
248
}
231
249
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
+ }
236
261
}
237
262
238
263
return nil
@@ -253,7 +278,6 @@ func flattenContextSpec(spec cfclient.ContextSpec) []interface{} {
253
278
case contextAzureStorage :
254
279
m [schemautil .MustNormalizeFieldName (currentContextType )] = storageContext .FlattenAzureStorageContextConfig (spec )
255
280
default :
256
- log .Printf ("[DEBUG] Invalid context type = %v" , currentContextType )
257
281
return nil
258
282
}
259
283
@@ -319,3 +343,23 @@ func mapResourceToContext(d *schema.ResourceData) *cfclient.Context {
319
343
},
320
344
}
321
345
}
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
+ }
0 commit comments