Skip to content

Commit

Permalink
fix(secrets): Fix secrets environment to content mapping
Browse files Browse the repository at this point in the history
Signed-off-by: Suleiman Dibirov <[email protected]>
  • Loading branch information
idsulik authored and glours committed Sep 9, 2024
1 parent aa1db26 commit 5769840
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion loader/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func resolveSecretsEnvironment(dict map[string]any, environment types.Mapping) {
continue
}
if found, ok := environment[env]; ok {
secret["content"] = found
secret[types.SecretConfigXValue] = found
}
secrets[name] = secret
}
Expand Down
21 changes: 20 additions & 1 deletion loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,9 @@ func Transform(source interface{}, target interface{}) error {
DecodeHook: mapstructure.ComposeDecodeHookFunc(
nameServices,
decoderHook,
cast),
cast,
secretConfigDecoderHook,
),
Result: target,
TagName: "yaml",
Metadata: &data,
Expand All @@ -764,6 +766,23 @@ func nameServices(from reflect.Value, to reflect.Value) (interface{}, error) {
return from.Interface(), nil
}

func secretConfigDecoderHook(from, to reflect.Type, data interface{}) (interface{}, error) {
// Check if the input is a map and we're decoding into a SecretConfig
if from.Kind() == reflect.Map && to == reflect.TypeOf(types.SecretConfig{}) {
if v, ok := data.(map[string]interface{}); ok {
if ext, ok := v["#extensions"].(map[string]interface{}); ok {
if val, ok := ext[types.SecretConfigXValue].(string); ok {
// Return a map with the Content field populated
v["Content"] = val
}
}
}
}

// Return the original data so the rest is handled by default mapstructure logic
return data, nil
}

// keys need to be converted to strings for jsonschema
func convertToStringKeysRecursive(value interface{}, keyPrefix string) (interface{}, error) {
if mapping, ok := value.(map[string]interface{}); ok {
Expand Down
4 changes: 4 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ const (
NetworkModeContainerPrefix = ContainerPrefix
)

const (
SecretConfigXValue = "x-#value"
)

// GetDependencies retrieves all services this service depends on
func (s ServiceConfig) GetDependencies() []string {
var dependencies []string
Expand Down

0 comments on commit 5769840

Please sign in to comment.