Skip to content

Commit d155872

Browse files
committed
Update protobuf validation interceptors to log invalid requests and responses
1 parent c73cada commit d155872

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

pkg/grpc/protobuf/validation/client_interceptor.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,26 @@ import (
1616
func UnaryClientInterceptor() grpc.UnaryClientInterceptor {
1717
log := logrus.StandardLogger().WithField("type", "protobuf/validation/interceptor")
1818

19-
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
19+
return func(ctx context.Context, method string, req, resp interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
2020
// Validate request
2121
if v, ok := req.(Validator); ok {
2222
if err := v.Validate(); err != nil {
2323
// Log warn since the caller is at fault.
24-
log.WithError(err).Warn("dropping invalid request")
24+
log.WithError(err).WithField("req", req).Warn("dropping invalid request")
2525
return status.Errorf(codes.InvalidArgument, err.Error())
2626
}
2727
}
2828

2929
// Do service call
30-
if err := invoker(ctx, method, req, reply, cc, opts...); err != nil {
30+
if err := invoker(ctx, method, req, resp, cc, opts...); err != nil {
3131
return err
3232
}
3333

3434
// Validate service response
35-
if v, ok := reply.(Validator); ok {
35+
if v, ok := resp.(Validator); ok {
3636
if err := v.Validate(); err != nil {
37-
// Just log debug here since the outbound service is mis-behaving.
38-
log.WithError(err).Debug("dropping invalid response")
37+
// Just log info here since the outbound service is mis-behaving.
38+
log.WithError(err).WithField("resp", resp).Info("dropping invalid response")
3939
return status.Errorf(codes.Internal, err.Error())
4040
}
4141
}
@@ -73,24 +73,24 @@ func (c *clientStreamWrapper) SendMsg(req interface{}) error {
7373
if v, ok := req.(Validator); ok {
7474
if err := v.Validate(); err != nil {
7575
// Log warn since the caller is at fault.
76-
c.log.WithError(err).Warn("dropping invalid request")
76+
c.log.WithError(err).WithField("req", req).Warn("dropping invalid request")
7777
return status.Errorf(codes.InvalidArgument, err.Error())
7878
}
7979
}
8080

8181
return c.ClientStream.SendMsg(req)
8282
}
8383

84-
func (c *clientStreamWrapper) RecvMsg(res interface{}) error {
85-
if err := c.ClientStream.RecvMsg(res); err != nil {
84+
func (c *clientStreamWrapper) RecvMsg(resp interface{}) error {
85+
if err := c.ClientStream.RecvMsg(resp); err != nil {
8686
return err
8787
}
8888

8989
// Validate service response
90-
if v, ok := res.(Validator); ok {
90+
if v, ok := resp.(Validator); ok {
9191
if err := v.Validate(); err != nil {
92-
// Just log debug here since the outbound service is mis-behaving.
93-
c.log.WithError(err).Debug("dropping invalid response")
92+
// Just log info here since the outbound service is mis-behaving.
93+
c.log.WithError(err).WithField("resp", resp).Info("dropping invalid response")
9494
return status.Errorf(codes.Internal, err.Error())
9595
}
9696
}

pkg/grpc/protobuf/validation/server_interceptor.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func UnaryServerInterceptor() grpc.UnaryServerInterceptor {
2323
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
2424
if v, ok := req.(Validator); ok {
2525
if err := v.Validate(); err != nil {
26-
// We use a debug level here because it is outside of 'our' control.
27-
log.WithError(err).Debug("dropping invalid request")
26+
// We use an info level here because it is outside of 'our' control.
27+
log.WithError(err).WithField("req", req).Info("dropping invalid request")
2828
return nil, status.Errorf(codes.InvalidArgument, err.Error())
2929
}
3030
}
@@ -37,7 +37,7 @@ func UnaryServerInterceptor() grpc.UnaryServerInterceptor {
3737
if v, ok := resp.(Validator); ok {
3838
if err := v.Validate(); err != nil {
3939
// We warn here because this indicates a problem with 'our' service.
40-
log.WithError(err).Warn("dropping invalid response")
40+
log.WithError(err).WithField("resp", req).Warn("dropping invalid response")
4141
return nil, status.Errorf(codes.Internal, err.Error())
4242
}
4343
}
@@ -69,21 +69,21 @@ func (s *serverStreamWrapper) RecvMsg(req interface{}) error {
6969

7070
if v, ok := req.(Validator); ok {
7171
if err := v.Validate(); err != nil {
72-
s.log.WithError(err).Debug("dropping invalid request")
72+
s.log.WithError(err).WithField("req", req).Info("dropping invalid request")
7373
return status.Errorf(codes.InvalidArgument, err.Error())
7474
}
7575
}
7676

7777
return nil
7878
}
7979

80-
func (s *serverStreamWrapper) SendMsg(res interface{}) error {
81-
if v, ok := res.(Validator); ok {
80+
func (s *serverStreamWrapper) SendMsg(resp interface{}) error {
81+
if v, ok := resp.(Validator); ok {
8282
if err := v.Validate(); err != nil {
83-
s.log.WithError(err).Warn("dropping invalid response")
83+
s.log.WithError(err).WithField("resp", resp).Warn("dropping invalid response")
8484
return status.Errorf(codes.Internal, err.Error())
8585
}
8686
}
8787

88-
return s.ServerStream.SendMsg(res)
88+
return s.ServerStream.SendMsg(resp)
8989
}

0 commit comments

Comments
 (0)