Skip to content

Commit

Permalink
fix(cloudslog): don't over-redact fields
Browse files Browse the repository at this point in the history
Due to a bug, messages with redacted fields were having all string
fields redacted.
  • Loading branch information
odsod committed Oct 21, 2024
1 parent 9dfb23e commit b7449a7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
1 change: 0 additions & 1 deletion cloudslog/redact.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func redact(input proto.Message) {
values.Index(-2).Value.Message().Set(last.Step.FieldDescriptor(), protoreflect.ValueOfString("<redacted>"))
return nil
}
values.Index(-2).Value.Message().Set(last.Step.FieldDescriptor(), protoreflect.ValueOfString("<redacted>"))
return nil
})
}
23 changes: 18 additions & 5 deletions cloudslog/redact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,23 @@ import (
)

func TestHandler_redact(t *testing.T) {
var b strings.Builder
logger := slog.New(newHandler(&b, LoggerConfig{}))
logger.Info("test", "example", &examplev1.ExampleMessage{
DebugRedactedField: "foobar",
t.Run("redacted field", func(t *testing.T) {
var b strings.Builder
logger := slog.New(newHandler(&b, LoggerConfig{}))
logger.Info("test", "example", &examplev1.ExampleMessage{
DebugRedactedField: "foobar",
})
assert.Assert(t, strings.Contains(b.String(), `"debugRedactedField":"<redacted>"`), b.String())
})

t.Run("redacted and non-redacted field", func(t *testing.T) {
var b strings.Builder
logger := slog.New(newHandler(&b, LoggerConfig{}))
logger.Info("test", "example", &examplev1.ExampleMessage{
DebugRedactedField: "foobar",
NonSensitiveField: "baz",
})
assert.Assert(t, strings.Contains(b.String(), `"debugRedactedField":"<redacted>"`), b.String())
assert.Assert(t, strings.Contains(b.String(), `"nonSensitiveField":"baz"`), b.String())
})
assert.Assert(t, strings.Contains(b.String(), `"debugRedactedField":"<redacted>"`), b.String())
}

0 comments on commit b7449a7

Please sign in to comment.