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

Add: the ability to redact values from Stringer and Error interface. #592

Merged
merged 8 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
34 changes: 31 additions & 3 deletions gen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,9 +694,10 @@ func (f fieldGroupGenerator) String(g Generator) error {
<$fields := newVar "fields">
<$i := newVar "i">

var <$fields> [<len .Fields>]string
<$sanitizedFields := scrubPII .Fields >
var <$fields> [<len $sanitizedFields>]string
<$i> := 0
<range .Fields>
<range $sanitizedFields >
<- $fname := goName . ->
<- $f := printf "%s.%s" $v $fname ->

Expand All @@ -717,7 +718,9 @@ func (f fieldGroupGenerator) String(g Generator) error {

return <$fmt>.Sprintf("<.Name>{%v}", <$strings>.Join(<$fields>[:<$i>], ", "))
}
`, f)
`, f,
TemplateFunc("scrubPII", scrubPII),
)
}

func (f fieldGroupGenerator) ErrorName(g Generator) error {
Expand Down Expand Up @@ -875,3 +878,28 @@ func verifyUniqueFieldLabels(fs compile.FieldGroup) error {
}
return nil
}

// PIILabel provides a mechanism to excludes certain struct fields from
// Zap logs, and from the outputs of String() and Error() methods.
//
// struct Contact {
// 1: required string name
// 2: required string email (go.pii) // will be omitted from Zap logs, Stringer(), and Error() methods.
// }
const PIILabel = "go.pii"
moisesvega marked this conversation as resolved.
Show resolved Hide resolved

func hasPIIAnnotation(spec *compile.FieldSpec) bool {
_, ok := spec.Annotations[PIILabel]
return ok
}

func scrubPII(fields compile.FieldGroup) compile.FieldGroup {
out := make(compile.FieldGroup, 0)
moisesvega marked this conversation as resolved.
Show resolved Hide resolved
for _, field := range fields {
if hasPIIAnnotation(field) {
continue
}
out = append(out, field)
}
return out
}
111 changes: 109 additions & 2 deletions gen/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import (
"strings"
"testing"

"go.uber.org/thriftrw/compile"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/thriftrw/compile"
"go.uber.org/thriftrw/gen/internal/tests/exceptions"
ts "go.uber.org/thriftrw/gen/internal/tests/structs"
"go.uber.org/zap/zapcore"
)

func TestFieldLabelConflict(t *testing.T) {
Expand Down Expand Up @@ -137,3 +139,108 @@ func TestCompileJSONTag(t *testing.T) {
})
}
}

func TestScrubPII(t *testing.T) {
foo := &compile.FieldSpec{
Name: "foo",
}
pii := &compile.FieldSpec{
Name: "pii",
Annotations: compile.Annotations{PIILabel: ""},
}
tests := []struct {
name string
spec compile.FieldGroup
want compile.FieldGroup
}{
{
name: "without pii annotation",
spec: compile.FieldGroup{
foo,
foo,
},
want: compile.FieldGroup{
foo,
foo,
},
},
{
name: "with pii annotation",
spec: compile.FieldGroup{
foo,
pii,
foo,
},
want: compile.FieldGroup{
foo,
foo,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, scrubPII(tt.spec), "scrubPII(%v)", tt.spec)
})
}
}

func TestHasPIIAnnotation(t *testing.T) {
foo := &compile.FieldSpec{
Name: "foo",
}
pii := &compile.FieldSpec{
Name: "pii",
Annotations: compile.Annotations{PIILabel: ""},
}
tests := []struct {
name string
spec *compile.FieldSpec
want bool
}{
{name: "pii annotation", spec: pii, want: true},
{name: "no pii annotation", spec: foo, want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, hasPIIAnnotation(tt.spec), "hasPIIAnnotation(%v)", tt.spec)
})
}
}

func TestSanitizePII(t *testing.T) {
age := int32(21)
pi := ts.PersonalInfo{
Age: toPtr(age),
Race: toPtr("kryptonian"),
}
piiException := exceptions.DoesNotExistException{
Key: "s",
UserName: toPtr("superman"),
}
piEncoder := zapcore.NewMapObjectEncoder()
require.NoError(t, pi.MarshalLogObject(piEncoder))

piiExceptionEncoder := zapcore.NewMapObjectEncoder()
require.NoError(t, piiException.MarshalLogObject(piiExceptionEncoder))

tests := []struct {
name string
got any
want any
}{
{name: "struct/zap", got: piEncoder.Fields, want: map[string]interface{}{"age": age}},
{name: "struct/string", got: pi.String(), want: "PersonalInfo{Age: 21}"},
{name: "exception/zap", got: piiExceptionEncoder.Fields, want: map[string]interface{}{"key": "s"}},
{name: "exception/string", got: piiException.String(), want: "DoesNotExistException{Key: s}"},
{name: "exception/error", got: piiException.Error(), want: "DoesNotExistException{Key: s}"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, tt.got)
})
}
}

func toPtr[T any](input T) *T {
return &input
}
68 changes: 63 additions & 5 deletions gen/internal/tests/exceptions/exceptions.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading