@@ -28,6 +28,11 @@ var supportedContextType = []string{
2828 contextSecretYaml ,
2929}
3030
31+ var encryptedContextTypes = []string {
32+ contextSecret ,
33+ contextSecretYaml ,
34+ }
35+
3136func 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 {
225242func 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+ }
0 commit comments