From a64631a8dcf2d85fc6f47aadcd162cf64ee2b9e4 Mon Sep 17 00:00:00 2001 From: Can Sentay Date: Sat, 28 Nov 2026 20:38:44 +0300 Subject: [PATCH] fix: keep secrets redacted when --find-renames compares them With --find-renames, contentSearch redacted (or decoded) each removed and added Secret in place to score it as a rename candidate. doDiff then redacted the reported pair a second time, and a redacted secret does not parse as a secret any more: its data values are no longer base64. A renamed Secret, and a removed or added one that was scored as a candidate, was printed as Error parsing old secret: error unmarshaling JSON: ... illegal base64 data at input byte 0 instead of the redacted manifest. Score the candidates on copies, so that doDiff transforms each secret once. --- diff/diff.go | 9 +++-- diff/diff_test.go | 90 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 3 deletions(-) diff --git a/diff/diff.go b/diff/diff.go index 124c290c..4cb48bd9 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -285,14 +285,17 @@ func contentSearch(report *Report, possiblyRemoved []string, oldIndex map[string } } + // Compare copies: doDiff redacts or decodes the secrets it reports itself, + // and a secret that is already redacted does not parse a second time. + oldCandidate, newCandidate := *oldContent, *newContent switch { case options.ShowSecretsDecoded: - decodeSecrets(oldContent, newContent) + decodeSecrets(&oldCandidate, &newCandidate) case !options.ShowSecrets: - redactSecrets(oldContent, newContent) + redactSecrets(&oldCandidate, &newCandidate) } - diff := diffMappingResults(oldContent, newContent, options.StripTrailingCR) + diff := diffMappingResults(&oldCandidate, &newCandidate, options.StripTrailingCR) delta := actualChanges(diff) if delta == 0 || len(diff) == 0 { continue diff --git a/diff/diff_test.go b/diff/diff_test.go index e4ed3c15..024a1a53 100644 --- a/diff/diff_test.go +++ b/diff/diff_test.go @@ -1796,3 +1796,93 @@ spec: require.Contains(t, buf.String(), "has been added") }) } + +func TestRenameDetectionWithRedactedSecrets(t *testing.T) { + ansi.DisableColors(true) + + specs := func() (map[string]*manifest.MappingResult, map[string]*manifest.MappingResult) { + oldSpec := map[string]*manifest.MappingResult{ + "default, foo, Secret (v1)": { + Name: "default, foo, Secret (v1)", + Kind: "Secret", + Content: ` +apiVersion: v1 +kind: Secret +metadata: + name: foo +type: Opaque +data: + key1: dmFsdWUx + key2: dmFsdWUy +`, + }, + } + newSpec := map[string]*manifest.MappingResult{ + "default, bar, Secret (v1)": { + Name: "default, bar, Secret (v1)", + Kind: "Secret", + Content: ` +apiVersion: v1 +kind: Secret +metadata: + name: bar +type: Opaque +data: + key1: dmFsdWUxY2hhbmdlZA== + key2: dmFsdWUy +`, + }, + } + return oldSpec, newSpec + } + + t.Run("rename detected", func(t *testing.T) { + var buf bytes.Buffer + opts := &Options{OutputFormat: "diff", OutputContext: 10, FindRenames: 0.5} + + oldSpec, newSpec := specs() + require.True(t, Manifests(oldSpec, newSpec, opts, &buf)) + require.Equal(t, `default, foo, Secret (v1) has changed: + apiVersion: v1 + kind: Secret + metadata: +- name: foo ++ name: bar + data: +- key1: '-------- # (6 bytes)' ++ key1: '++++++++ # (13 bytes)' + key2: 'REDACTED # (6 bytes)' + type: Opaque + +`, buf.String()) + }) + + t.Run("rename rejected", func(t *testing.T) { + var buf bytes.Buffer + opts := &Options{OutputFormat: "diff", OutputContext: 10, FindRenames: 0.1} + + oldSpec, newSpec := specs() + require.True(t, Manifests(oldSpec, newSpec, opts, &buf)) + require.Equal(t, `default, foo, Secret (v1) has been removed: +- apiVersion: v1 +- kind: Secret +- metadata: +- name: foo +- data: +- key1: '-------- # (6 bytes)' +- key2: '-------- # (6 bytes)' +- type: Opaque + +default, bar, Secret (v1) has been added: ++ apiVersion: v1 ++ kind: Secret ++ metadata: ++ name: bar ++ data: ++ key1: '++++++++ # (13 bytes)' ++ key2: '++++++++ # (6 bytes)' ++ type: Opaque + +`, buf.String()) + }) +}