Skip to content

Commit

Permalink
Set Value Case
Browse files Browse the repository at this point in the history
  • Loading branch information
gdanichev authored and gdanichev committed Dec 7, 2023
1 parent 3fbfef2 commit f2ca8aa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
20 changes: 12 additions & 8 deletions redact.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@ import (
"google.golang.org/protobuf/types/descriptorpb"
)

var StdRedactor = Redactor{
RedactingHandler: func(parent protoreflect.Value, fd protoreflect.FieldDescriptor) error {
var (
clearFunc = func(parent protoreflect.Value, fd protoreflect.FieldDescriptor) error {
parent.Message().Clear(fd)
return nil
},
}
}
)

type Redactor struct {
RedactingHandler func(parent protoreflect.Value, field protoreflect.FieldDescriptor) error
SensitiveFieldAnnotation *protoimpl.ExtensionInfo
RedactingHandler func(parent protoreflect.Value, field protoreflect.FieldDescriptor) error
}

func (r Redactor) Redact(msg proto.Message, sensitiveFieldAnnotation *protoimpl.ExtensionInfo) error {
func (r Redactor) Redact(msg proto.Message) error {
if r.SensitiveFieldAnnotation == nil || r.RedactingHandler == nil {
return nil
}
return protorange.Range(msg.ProtoReflect(), func(p protopath.Values) error {
fd := p.Path.Index(-1).FieldDescriptor()
if isFieldSensetive(fd, p.Index(-1).Value, sensitiveFieldAnnotation) {
if isFieldSensetive(fd, p.Index(-1).Value, r.SensitiveFieldAnnotation) {
parent := p.Index(-2)
if parent.Value.IsValid() {
err := r.RedactingHandler(parent.Value, fd)
Expand All @@ -38,7 +42,7 @@ func (r Redactor) Redact(msg proto.Message, sensitiveFieldAnnotation *protoimpl.
}

func Redact(msg proto.Message, sensitiveFieldAnnotation *protoimpl.ExtensionInfo) error {
return StdRedactor.Redact(msg, sensitiveFieldAnnotation)
return Redactor{RedactingHandler: clearFunc, SensitiveFieldAnnotation: sensitiveFieldAnnotation}.Redact(msg)
}

func isFieldSensetive(fieldDescriptor protoreflect.FieldDescriptor, value protoreflect.Value, sensitiveFieldAnnotation *protoimpl.ExtensionInfo) bool {
Expand Down
34 changes: 20 additions & 14 deletions redact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import (

func TestRedactProto_Clear(t *testing.T) {
t.Parallel()
redactor := Redactor{RedactingHandler: func(parent protoreflect.Value, fd protoreflect.FieldDescriptor) error {
parent.Message().Clear(fd)
return nil
}}
redactor := Redactor{
RedactingHandler: func(parent protoreflect.Value, fd protoreflect.FieldDescriptor) error {
parent.Message().Clear(fd)
return nil
},
SensitiveFieldAnnotation: testproto.E_SensitiveData,
}

type args struct {
message proto.Message
Expand Down Expand Up @@ -130,7 +133,7 @@ func TestRedactProto_Clear(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := redactor.Redact(tt.args.message, testproto.E_SensitiveData)
err := redactor.Redact(tt.args.message)
if (err != nil) != tt.wantErr {
t.Errorf("SensetiveFields() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -142,14 +145,17 @@ func TestRedactProto_Clear(t *testing.T) {
}
func TestRedactProto_SetStringClearOther(t *testing.T) {
t.Parallel()
redactor := Redactor{RedactingHandler: func(parent protoreflect.Value, field protoreflect.FieldDescriptor) error {
if parent.IsValid() && field.Kind() == protoreflect.StringKind {
parent.Message().Set(field, protoreflect.ValueOfString("REDACTED"))
} else {
parent.Message().Clear(field)
}
return nil
}}
redactor := Redactor{
RedactingHandler: func(parent protoreflect.Value, field protoreflect.FieldDescriptor) error {
if parent.IsValid() && field.Kind() == protoreflect.StringKind {
parent.Message().Set(field, protoreflect.ValueOfString("REDACTED"))
} else {
parent.Message().Clear(field)
}
return nil
},
SensitiveFieldAnnotation: testproto.E_SensitiveData,
}
type args struct {
message proto.Message
}
Expand Down Expand Up @@ -226,7 +232,7 @@ func TestRedactProto_SetStringClearOther(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := redactor.Redact(tt.args.message, testproto.E_SensitiveData)
err := redactor.Redact(tt.args.message)
if (err != nil) != tt.wantErr {
t.Errorf("SensetiveFields() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down

0 comments on commit f2ca8aa

Please sign in to comment.