Skip to content

Commit

Permalink
Fix redacting sensitive values that uses backslashes hashicorp#737
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulo Machado committed May 15, 2021
1 parent a24e405 commit edb3100
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
25 changes: 24 additions & 1 deletion helm/resource_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,20 +875,43 @@ func cloakSetValues(config map[string]interface{}, d resourceGetter) {

const sensitiveContentValue = "(sensitive value)"

func tokenizeCloakElements(valuePath string) []string {
var res []string
beginSubstr, endSubstr := 0, 0

for idx := range valuePath {
if (valuePath[idx] == '.') && (idx > 0) {
if valuePath[idx-1] != '\\' {
res = append(res, valuePath[beginSubstr:endSubstr])
beginSubstr = endSubstr + 1
}
}
endSubstr++
}

if endSubstr-beginSubstr > 0 {
res = append(res, valuePath[beginSubstr:])
}

return res
}

func cloakSetValue(values map[string]interface{}, valuePath string) {
pathKeys := strings.Split(valuePath, ".")
pathKeys := tokenizeCloakElements(valuePath)
sensitiveKey := pathKeys[len(pathKeys)-1]
parentPathKeys := pathKeys[:len(pathKeys)-1]

m := values
for _, key := range parentPathKeys {
key = strings.ReplaceAll(key, "\\", "")
v, ok := m[key].(map[string]interface{})
if !ok {
return
}
m = v
}

sensitiveKey = strings.ReplaceAll(sensitiveKey, "\\", "")
m[sensitiveKey] = sensitiveContentValue
}

Expand Down
8 changes: 4 additions & 4 deletions helm/resource_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,14 +750,14 @@ func TestCloakSetValues(t *testing.T) {
func TestCloakSetValuesNested(t *testing.T) {
d := resourceRelease().Data(nil)
err := d.Set("set_sensitive", []interface{}{
map[string]interface{}{"name": "foo.qux.bar", "value": "42"},
map[string]interface{}{"name": "foo.qux.bar\\.nested", "value": "42"},
})
if err != nil {
t.Fatalf("error setting values: %v", err)
}

qux := map[string]interface{}{
"bar": "bar",
"bar.nested": "bar",
}

values := map[string]interface{}{
Expand All @@ -767,8 +767,8 @@ func TestCloakSetValuesNested(t *testing.T) {
}

cloakSetValues(values, d)
if qux["bar"] != sensitiveContentValue {
t.Fatalf("error cloak values, expected %q, got %s", sensitiveContentValue, qux["bar"])
if qux["bar.nested"] != sensitiveContentValue {
t.Fatalf("error cloak values, expected %q, got %s", sensitiveContentValue, qux["bar.nested"])
}
}

Expand Down

0 comments on commit edb3100

Please sign in to comment.