Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CBG-4140: Cheaply clone additionalFields when producing audit event - avoids un… #7041

Merged
merged 2 commits into from
Aug 6, 2024
Merged
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
5 changes: 3 additions & 2 deletions base/logger_audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package base
import (
"context"
"fmt"
"maps"
"net"
"strings"
"time"
Expand All @@ -28,7 +29,7 @@ const (
func expandFields(id AuditID, ctx context.Context, globalFields AuditFields, additionalData AuditFields) AuditFields {
var fields AuditFields
if additionalData != nil {
fields = additionalData
fields = maps.Clone(additionalData)
} else {
fields = make(AuditFields)
}
Expand Down Expand Up @@ -103,7 +104,7 @@ func (f *AuditFields) merge(ctx context.Context, overwrites AuditFields) {
for k, v := range overwrites {
_, ok := (*f)[k]
if ok {
duplicateFields = append(duplicateFields, fmt.Sprintf("%q=%q", k, v))
duplicateFields = append(duplicateFields, fmt.Sprintf("%q='%v'", k, v))
continue
}
(*f)[k] = v
Expand Down
11 changes: 11 additions & 0 deletions base/logger_audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,14 @@ func TestAuditFieldsMerge(t *testing.T) {
}

}

func Test_expandFieldsAdditionalDataReadOnly(t *testing.T) {
additionalData := AuditFields{"foo": "bar"}
for i := 0; i < 5; i++ {
fields := expandFields(AuditIDDocumentRead, TestCtx(t), AuditFields{"global": true}, additionalData)
// id, name, description, timestamp, global, foo
assert.Len(t, fields, 6)
}
// additionalData should not be modified
assert.Len(t, additionalData, 1)
}
Loading