Skip to content

Commit

Permalink
Mask sensitive values in cli printing, improve ENV variable handling …
Browse files Browse the repository at this point in the history
…to set variable as blank if exists
  • Loading branch information
NHAS committed Nov 9, 2024
1 parent 593197f commit 538d96a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
5 changes: 4 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ func (cp *ciParser[T]) apply(result *T) (err error) {
type association struct {
v reflect.Value
path []string
tag reflect.StructTag
}

flagAssociation := map[string]association{}
Expand Down Expand Up @@ -326,7 +327,7 @@ func (cp *ciParser[T]) apply(result *T) (err error) {
}

cp.o.logger.Info("adding flag", "flag", "-"+flagName, "type", field.value.Kind())
flagAssociation[flagName] = association{v: field.value, path: field.path}
flagAssociation[flagName] = association{v: field.value, path: field.path, tag: field.tag}

switch field.value.Kind() {
case reflect.String:
Expand Down Expand Up @@ -405,6 +406,8 @@ func (cp *ciParser[T]) apply(result *T) (err error) {
v, _ := getField(result, association.path)

v.Set(association.v)

cp.o.logger.Info("CLI FLAG", "-"+f.Name, maskSensitive(f.Value.String(), association.tag))
})

if help {
Expand Down
24 changes: 4 additions & 20 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,11 @@ func (ep *envParser[T]) apply(result *T) (err error) {
ep.o.logger.Info("using transform func on env variable", "before_func", strings.Join(resolvePath(result, field.path), ep.o.env.delimiter), "after_func", envVariable)
}

envVarValue := os.Getenv(envVariable)
value, wasSet := os.LookupEnv(envVariable)
ep.o.logger.Info("ENV", "was_set", wasSet, envVariable, maskSensitive(value, field.tag))

printedValue := envVarValue

isSensitive := false
value, ok := field.tag.Lookup(confyTag)
if ok {
parts := strings.Split(value, ";")
if len(parts) > 1 {
isSensitive = strings.TrimSpace(parts[1]) == "sensitive"
}
}

if isSensitive && envVarValue != "" {
printedValue = "**********"
}

ep.o.logger.Info("ENV", envVariable, printedValue)

if envVarValue != "" {
ep.setBasicFieldFromString(result, field.path, envVarValue)
if wasSet {
ep.setBasicFieldFromString(result, field.path, value)
}
}

Expand Down
21 changes: 21 additions & 0 deletions reflection_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package confy

import (
"reflect"
"strings"
)

type fieldsData struct {
Expand Down Expand Up @@ -122,3 +123,23 @@ func equalStringSlices(a, b []string) bool {
}
return true
}

func maskSensitive(value string, tag reflect.StructTag) string {

printedValue := value

isSensitive := false
value, ok := tag.Lookup(confyTag)
if ok {
parts := strings.Split(value, ";")
if len(parts) > 1 {
isSensitive = strings.TrimSpace(parts[1]) == "sensitive"
}
}

if isSensitive && value != "" {
printedValue = "**********"
}

return printedValue
}

0 comments on commit 538d96a

Please sign in to comment.