Skip to content

Commit

Permalink
Obfuscate sensitive data in complexType struct
Browse files Browse the repository at this point in the history
  • Loading branch information
mativm02 committed Jul 22, 2024
1 parent c440378 commit 51dd1f4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
6 changes: 4 additions & 2 deletions internal/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ type complexType struct {
} `json:"data"`
Metadata map[string]struct {
ID int `json:"id,omitempty"`
Value string `json:"value,omitempty"`
Value string `json:"value,omitempty" structviewer:"obfuscate"`
} `json:"metadata,omitempty"`
Random map[int]string `json:"random,omitempty"`
Test string `json:"test" structviewer:"obfuscate"`
}

var complexStruct = complexType{
Expand All @@ -29,14 +30,15 @@ var complexStruct = complexType{
},
Metadata: map[string]struct {
ID int `json:"id,omitempty"`
Value string `json:"value,omitempty"`
Value string `json:"value,omitempty" structviewer:"obfuscate"`
}{
"key_99": {ID: 99, Value: "key99"},
},
Random: map[int]string{
1: "one",
2: "two",
},
Test: "test",
}

func main() {
Expand Down
52 changes: 50 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ func parseEnvs(config interface{}, prefix, configField string) []*EnvVar {

return envs
}

func obfuscateTags(config interface{}) (interface{}, error) {
val := reflect.ValueOf(config)
if val.Kind() == reflect.Ptr {
Expand Down Expand Up @@ -328,10 +327,59 @@ func obfuscateTags(config interface{}) (interface{}, error) {

fieldValue.Set(reflect.ValueOf(newStruct).Elem())
}
} else {
} else if fieldValue.Kind() == reflect.Map {
if strings.EqualFold(svTag, "obfuscate") {
zeroValue := reflect.Zero(fieldValue.Type())
fieldValue.Set(zeroValue)
} else {
newMap := reflect.MakeMap(fieldValue.Type())
keys := fieldValue.MapKeys()

for _, key := range keys {
mapValue := fieldValue.MapIndex(key)
if !mapValue.IsValid() {
continue
}

var newValue reflect.Value

switch mapValue.Kind() {
case reflect.Ptr, reflect.Interface:
elemValue := mapValue.Elem()
if elemValue.Kind() == reflect.Struct {
newStruct, err := obfuscateTags(elemValue.Addr().Interface())
if err != nil {
return nil, err
}
newValue = reflect.ValueOf(newStruct).Elem()
} else {
newValue = mapValue
}
case reflect.Struct:
ptrToStruct := reflect.New(mapValue.Type())
ptrToStruct.Elem().Set(mapValue)
newStruct, err := obfuscateTags(ptrToStruct.Interface())
if err != nil {
return nil, err
}
newValue = reflect.ValueOf(newStruct).Elem()
default:
newValue = mapValue
}

newMap.SetMapIndex(key, newValue)
}

fieldValue.Set(newMap)
}
} else {
if strings.EqualFold(svTag, "obfuscate") {
if fieldValue.Kind() == reflect.String {
fieldValue.SetString("*REDACTED*")
} else {
zeroValue := reflect.Zero(fieldValue.Type())
fieldValue.Set(zeroValue)
}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func TestObfuscateTags(t *testing.T) {
Exported string `json:"exported" structviewer:"obfuscate"`
Test int `json:"test"`
}{
Exported: "",
Exported: "*REDACTED*",
Test: 10,
},
wantErr: false,
Expand Down Expand Up @@ -395,7 +395,7 @@ func TestObfuscateTags(t *testing.T) {
InnerField string `json:"inner_field" structviewer:"obfuscate"`
NotObf string `json:"not_obf"`
}{
InnerField: "",
InnerField: "*REDACTED*",
NotObf: "not obfuscated",
},
},
Expand Down Expand Up @@ -427,12 +427,12 @@ func TestObfuscateTags(t *testing.T) {
NotObf string `json:"not_obf"`
} `json:"inner"`
}{
Exported: "",
Exported: "*REDACTED*",
Inner: struct {
InnerField string `json:"inner_field" structviewer:"obfuscate"`
NotObf string `json:"not_obf"`
}{
InnerField: "",
InnerField: "*REDACTED*",
NotObf: "not obfuscated",
},
},
Expand Down

0 comments on commit 51dd1f4

Please sign in to comment.