Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions command/v7/shared/manifest_diff_displayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ func redactEnvVarsInRawManifest(rawManifest []byte) ([]byte, error) {
for appListIndex, application := range manifestDataStructure.Applications {
for appDataIndex, appDataKV := range application {
if appDataKV.Key == "env" {
envMapSlice := appDataKV.Value.(yaml.MapSlice)
envMapSlice, ok := appDataKV.Value.(yaml.MapSlice)
if !ok {
// env is present but not a map (e.g. a scalar). Redact the
// value as-is rather than asserting it to be a MapSlice,
// which would panic.
appDataKV.Value = redacted
application[appDataIndex] = appDataKV
manifestDataStructure.Applications[appListIndex] = application
continue
}
for envKeyIndex := range envMapSlice {
envMapSlice[envKeyIndex].Value = redacted
}
Expand Down Expand Up @@ -168,11 +177,21 @@ func redactDiff(diff resources.Diff) resources.Diff {
diff.Was = redacted
}
if isEnvMap {
cleanMap := diff.Value.(map[string]interface{})
for index := range cleanMap {
cleanMap[index] = redacted
if cleanMap, ok := diff.Value.(map[string]interface{}); ok {
for index := range cleanMap {
cleanMap[index] = redacted
}
diff.Value = cleanMap
} else {
// The diff path ends in "env" but the value is not a map.
// CAPI 1.253.0 (cloud_controller_ng PR #5712) redacts sensitive
// manifest values server-side and sends the whole env map as the
// string "[PRIVATE DATA HIDDEN]". Redact the value directly rather
// than asserting it to be a map, which would panic.
// https://github.com/cloudfoundry/cloud_controller_ng/pull/5712
diff.Value = redacted
diff.Was = redacted
}
diff.Value = cleanMap
} else if envMap, containsEnvInMap := fullMap["env"].(map[string]interface{}); containsMap && containsEnvInMap {
for index := range envMap {
envMap[index] = redacted
Expand Down
54 changes: 54 additions & 0 deletions command/v7/shared/manifest_diff_displayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,60 @@ applications:
})
})

When("the raw manifest has env set to a non-map value", func() {
// Regression test for a panic in redactEnvVarsInRawManifest:
// it asserted the raw manifest's env value to be a yaml.MapSlice
// unconditionally, panicking when env was a scalar.
BeforeEach(func() {
rawManifest = []byte(`---
applications:
- name: dora
env: somestring`)
diff = resources.ManifestDiff{
Diffs: []resources.Diff{
{Op: resources.AddOperation, Path: "/applications/0/name", Value: "dora"},
},
}
})

It("does not panic and redacts the env value", func() {
Expect(err).NotTo(HaveOccurred())
Expect(testUI.Out).To(Say(`env: <redacted>`))
})
})

When("the diff server redacts env to a scalar string", func() {
// Regression test for a panic in redactDiff: isEnvMap is computed
// via strings.HasSuffix(diff.Path, "env"), so any diff whose path
// ends in "env" takes the branch that unconditionally asserts
// diff.Value.(map[string]interface{}). CAPI 1.253.0
// (cloud_controller_ng PR #5712, which redacts sensitive manifest
// values server-side) sends the whole env map as the string
// "[PRIVATE DATA HIDDEN]", so that assertion panics with
// "interface conversion: interface {} is string, not map[string]interface {}".
// See https://github.com/cloudfoundry/cloud_controller_ng/pull/5712
BeforeEach(func() {
rawManifest = []byte(`---
applications:
- name: dora
env:
a: b`)
diff = resources.ManifestDiff{
Diffs: []resources.Diff{
{Op: resources.AddOperation, Path: "/applications/0/env", Value: "[PRIVATE DATA HIDDEN]"},
},
}
})

It("does not panic and redacts the value", func() {
Expect(err).NotTo(HaveOccurred())
Expect(testUI.Out).To(Say(` ---
applications:
- name: dora
\+ env: <redacted>`))
})
})

When("remove", func() {
BeforeEach(func() {
rawManifest = []byte(`---
Expand Down
Loading