Skip to content

Commit 348be64

Browse files
committed
exclude masking of numbers / booleans
1 parent 5bb1195 commit 348be64

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

internal/data-patcher/secret_patcher.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,17 @@ func updateSecretData(secret *corev1.Secret, secretKey string, valueToPatch *str
151151
}
152152
}
153153

154-
// replaceSensitiveValues replaces occurrences of a sensitive value in the HTTP response body
155-
// and headers with a placeholder.
154+
// replaceSensitiveValues replaces occurrences of sensitive values in the HTTP response body
155+
// and headers with a placeholder, iff the value is a json string surrounded by double quotes.
156156
func replaceSensitiveValues(data *httpClient.HttpResponse, secret *corev1.Secret, secretKey string, valueToPatch *string) {
157157
if valueToPatch == nil || *valueToPatch == "" {
158158
return
159159
}
160160

161161
placeholder := fmt.Sprintf("{{%s:%s:%s}}", secret.Name, secret.Namespace, secretKey)
162-
data.Body = strings.ReplaceAll(data.Body, *valueToPatch, placeholder)
162+
quotedValue := fmt.Sprintf("\"%s\"", *valueToPatch)
163+
quotedPlaceholder := fmt.Sprintf("\"%s\"", placeholder)
164+
data.Body = strings.ReplaceAll(data.Body, quotedValue, quotedPlaceholder)
163165

164166
for _, headersList := range data.Headers {
165167
for i, header := range headersList {

internal/data-patcher/secret_patcher_test.go

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func TestReplaceSensitiveValues(t *testing.T) {
242242
"ShouldHandleEmptyHeadersGracefully": {
243243
args: args{
244244
data: &httpClient.HttpResponse{
245-
Body: "Sensitive value in the body.",
245+
Body: `{"message": "Sensitive value in the body", "token": "value"}`,
246246
Headers: map[string][]string{
247247
"Authorization": {},
248248
},
@@ -257,7 +257,7 @@ func TestReplaceSensitiveValues(t *testing.T) {
257257
valueToPatch: ptr.To("value"),
258258
},
259259
want: want{
260-
body: "Sensitive {{my-secret:default:sensitiveKey}} in the body.",
260+
body: `{"message": "Sensitive value in the body", "token": "{{my-secret:default:sensitiveKey}}"}`,
261261
headers: map[string][]string{
262262
"Authorization": {},
263263
},
@@ -282,10 +282,10 @@ func TestReplaceSensitiveValues(t *testing.T) {
282282

283283
func TestUpdateSecretData(t *testing.T) {
284284
type args struct {
285-
secret *corev1.Secret
286-
secretKey string
287-
valueToPatch *string
288-
missingStrategy common.MissingFieldStrategy
285+
secret *corev1.Secret
286+
secretKey string
287+
valueToPatch *string
288+
missingFieldStrategy common.MissingFieldStrategy
289289
}
290290

291291
type want struct {
@@ -341,12 +341,50 @@ func TestUpdateSecretData(t *testing.T) {
341341
},
342342
},
343343
},
344+
"ShouldSetEmptyMissingFieldWhenFieldMissing": {
345+
// Secret already contains key "key1" but the response did not return a value;
346+
// missing field strategy "setEmpty" should override it to empty string.
347+
args: args{
348+
secret: &corev1.Secret{
349+
Data: map[string][]byte{
350+
"key1": []byte("existingValue"),
351+
},
352+
},
353+
secretKey: "key1",
354+
valueToPatch: nil,
355+
missingFieldStrategy: common.SetEmptyMissingField,
356+
},
357+
want: want{
358+
data: map[string][]byte{
359+
"key1": []byte(""),
360+
},
361+
},
362+
},
363+
"ShouldPreserveExistingValueWhenFieldMissing": {
364+
// Secret already contains key "key1" but the response did not return a value;
365+
// missing field strategy "preserve" should leave the value unchanged.
366+
args: args{
367+
secret: &corev1.Secret{
368+
Data: map[string][]byte{
369+
"key1": []byte("existingValue"),
370+
},
371+
},
372+
secretKey: "key1",
373+
valueToPatch: nil,
374+
missingFieldStrategy: common.PreserveMissingField,
375+
},
376+
want: want{
377+
data: map[string][]byte{
378+
"key1": []byte("existingValue"),
379+
},
380+
},
381+
},
344382
}
345383

346384
for name, tc := range cases {
385+
tc := tc // Create local copies of loop variables
347386
t.Run(name, func(t *testing.T) {
348-
updateSecretData(tc.args.secret, tc.args.secretKey, tc.args.valueToPatch, tc.args.missingStrategy)
349-
387+
updateSecretData(tc.args.secret, tc.args.secretKey, tc.args.valueToPatch, tc.args.missingFieldStrategy)
350388
if diff := cmp.Diff(tc.want.data, tc.args.secret.Data); diff != "" {
351389
t.Errorf("updateSecretData(...): -want data, +got data: %s", diff)
352390
}

0 commit comments

Comments
 (0)