Skip to content

Commit 028a574

Browse files
committed
read secret variables in Read but keep non-hashed value in state
1 parent 7ab5706 commit 028a574

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

internal/services/container/helpers_container.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
"time"
88

9+
"github.com/alexedwards/argon2id"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1011
container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1"
1112
"github.com/scaleway/scaleway-sdk-go/scw"
@@ -330,10 +331,33 @@ func retryCreateContainerDomain(ctx context.Context, containerAPI *container.API
330331
}
331332
}
332333

333-
func flattenContainerSecretEnvironmentVariables(secrets []*container.SecretHashedValue) map[string]any {
334+
func flattenContainerSecretEnvironmentVariables(secrets []*container.Secret) map[string]any {
334335
m := make(map[string]any, len(secrets))
335336
for _, s := range secrets {
336-
m[s.Key] = s.HashedValue
337+
m[s.Key] = s.Value
338+
}
339+
340+
return m
341+
}
342+
343+
// flattenContainerHashedSecretEnvironmentVariables convert secret hashed values to a state ready map.
344+
// It needs stateSecrets which is the old map stored in state. It will
345+
func flattenContainerHashedSecretEnvironmentVariables(secrets []*container.SecretHashedValue, stateSecrets any) map[string]any {
346+
stateSecretsMap := stateSecrets.(map[string]any)
347+
_ = stateSecretsMap
348+
349+
m := make(map[string]any, len(secrets))
350+
for _, s := range secrets {
351+
secret := s.HashedValue
352+
353+
if oldValue, hasOldValue := stateSecretsMap[s.Key]; hasOldValue {
354+
match, _ := argon2id.ComparePasswordAndHash(oldValue.(string), secret)
355+
if match {
356+
secret = oldValue.(string) // Keep state value if hashed value is the same
357+
}
358+
}
359+
360+
m[s.Key] = secret
337361
}
338362

339363
return m

internal/services/container/namespace.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func ResourceContainerNamespaceRead(ctx context.Context, d *schema.ResourceData,
178178
_ = d.Set("description", types.FlattenStringPtr(ns.Description))
179179
_ = d.Set("tags", types.FlattenSliceString(ns.Tags))
180180
_ = d.Set("environment_variables", ns.EnvironmentVariables)
181-
_ = d.Set("secret_environment_variables", flattenContainerSecretEnvironmentVariables(ns.SecretEnvironmentVariables))
181+
_ = d.Set("secret_environment_variables", flattenContainerHashedSecretEnvironmentVariables(ns.SecretEnvironmentVariables, d.Get("secret_environment_variables")))
182182
_ = d.Set("name", ns.Name)
183183
_ = d.Set("organization_id", ns.OrganizationID)
184184
_ = d.Set("project_id", ns.ProjectID)

0 commit comments

Comments
 (0)