Skip to content

Commit dcb29a4

Browse files
Redact secrets in slices (#11271) (#11389)
Redact secrets in config and component files found in the diagnostics archive that occur within slices. (cherry picked from commit 42e78ec) Co-authored-by: Michel Laterman <[email protected]>
1 parent a4b96b6 commit dcb29a4

File tree

3 files changed

+102
-3
lines changed

3 files changed

+102
-3
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: security
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Redact secrets in slices
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
19+
description: Redact secrets in conifg and component files found in the diagnostics archive that occur within slices.
20+
21+
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
22+
component: elastic-agent
23+
24+
# PR URL; optional; the PR number that added the changeset.
25+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
26+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
27+
# Please provide it if you are adding a fragment for a different PR.
28+
pr: https://github.com/elastic/elastic-agent/pull/11271
29+
30+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
31+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
32+
#issue: https://github.com/owner/repo/1234

internal/pkg/diagnostics/diagnostics.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,11 @@ func redactMap[K comparable](errOut io.Writer, inputMap map[K]interface{}, slice
369369
if rootValue != nil {
370370
switch cast := rootValue.(type) {
371371
case map[string]interface{}:
372-
rootValue = redactMap(errOut, cast, sliceElem)
372+
rootValue = redactMap(errOut, cast, false)
373373
case map[interface{}]interface{}:
374-
rootValue = redactMap(errOut, cast, sliceElem)
374+
rootValue = redactMap(errOut, cast, false)
375375
case map[int]interface{}:
376-
rootValue = redactMap(errOut, cast, sliceElem)
376+
rootValue = redactMap(errOut, cast, false)
377377
case []interface{}:
378378
// Recursively process each element in the slice so that we also walk
379379
// through lists (e.g. inputs[4].streams[0]). This is required to

internal/pkg/diagnostics/diagnostics_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,3 +791,70 @@ func TestAddRedactionMarkers(t *testing.T) {
791791
})
792792
}
793793
}
794+
795+
func TestRedactSSLKeyInInputs(t *testing.T) {
796+
inputYaml := []byte(`inputs:
797+
- ssl:
798+
certificate: cert1
799+
key: key1
800+
nested:
801+
ssl:
802+
certificate: cert2
803+
key: key2
804+
slice:
805+
- ssl:
806+
certificate: cert3
807+
key: key3`)
808+
809+
var unmarshalled map[string]any
810+
err := yaml.Unmarshal(inputYaml, &unmarshalled)
811+
require.NoError(t, err)
812+
813+
var errOut bytes.Buffer
814+
redacted := Redact(unmarshalled, &errOut)
815+
assert.Equalf(t, 0, errOut.Len(), "Unexpected errors written when redacting secrets: %s", errOut.String())
816+
require.NotNil(t, redacted)
817+
818+
require.Contains(t, redacted, "inputs")
819+
inputs, ok := redacted["inputs"].([]any)
820+
require.Truef(t, ok, "expected inputs to be slice, detected: %T", redacted["inputs"])
821+
require.Len(t, inputs, 1)
822+
input, ok := inputs[0].(map[string]any)
823+
require.True(t, ok, "expected input to be object, detected: %T", inputs[0])
824+
825+
// check top level ssl
826+
require.Contains(t, input, "ssl")
827+
top, ok := input["ssl"].(map[string]any)
828+
require.True(t, ok, "expected type to be object, detected: %T", input["ssl"])
829+
require.Contains(t, top, "certificate")
830+
assert.Equal(t, REDACTED, top["certificate"])
831+
require.Contains(t, top, "key")
832+
assert.Equal(t, REDACTED, top["key"])
833+
834+
// check nested object
835+
require.Contains(t, input, "nested")
836+
nested, ok := input["nested"].(map[string]any)
837+
require.True(t, ok, "expected type to be object, detected: %T", input["nested"])
838+
require.Contains(t, nested, "ssl")
839+
nestedSSL, ok := nested["ssl"].(map[string]any)
840+
require.True(t, ok, "expected type to be object, detected: %T", nested["ssl"])
841+
require.Contains(t, nestedSSL, "certificate")
842+
assert.Equal(t, REDACTED, nestedSSL["certificate"])
843+
require.Contains(t, nestedSSL, "key")
844+
assert.Equal(t, REDACTED, nestedSSL["key"])
845+
846+
// check nested slice
847+
require.Contains(t, input, "slice")
848+
slice, ok := input["slice"].([]any)
849+
require.True(t, ok, "expected type to be slice, detected: %T", input["slice"])
850+
require.Len(t, slice, 1)
851+
elem, ok := slice[0].(map[string]any)
852+
require.True(t, ok, "expected type to be object, detected: %T", slice[0])
853+
require.Contains(t, elem, "ssl")
854+
sliceSSL, ok := elem["ssl"].(map[string]any)
855+
require.True(t, ok, "expected type to be object, detected: %T", elem["ssl"])
856+
require.Contains(t, sliceSSL, "certificate")
857+
assert.Equal(t, REDACTED, sliceSSL["certificate"])
858+
require.Contains(t, sliceSSL, "key")
859+
assert.Equal(t, REDACTED, sliceSSL["key"])
860+
}

0 commit comments

Comments
 (0)