-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient_interceptor.go
98 lines (84 loc) · 3.03 KB
/
client_interceptor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package validation
import (
"context"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// UnaryClientInterceptor returns a grpc.UnaryClientInterceptor that validates
// inbound and outbound messages. If a service request is invalid, a
// codes.InvalidArgument is returned. If a service response is invalid, a
// codes.Internal is returned.
func UnaryClientInterceptor() grpc.UnaryClientInterceptor {
log := logrus.StandardLogger().WithField("type", "protobuf/validation/interceptor")
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
// Validate request
if v, ok := req.(Validator); ok {
if err := v.Validate(); err != nil {
// Log warn since the caller is at fault.
log.WithError(err).Warn("dropping invalid request")
return status.Errorf(codes.InvalidArgument, err.Error())
}
}
// Do service call
if err := invoker(ctx, method, req, reply, cc, opts...); err != nil {
return err
}
// Validate service response
if v, ok := reply.(Validator); ok {
if err := v.Validate(); err != nil {
// Just log debug here since the outbound service is mis-behaving.
log.WithError(err).Debug("dropping invalid response")
return status.Errorf(codes.Internal, err.Error())
}
}
return nil
}
}
// StreamClientInterceptor returns a grpc.StreamClientInterceptor that validates
// inbound and outbound messages. If any streamed service request is invalid, a
// codes.InvalidArgument is returned. If any streamed service response is invalid, a
// codes.Internal is returned.
func StreamClientInterceptor() grpc.StreamClientInterceptor {
log := logrus.StandardLogger().WithField("type", "protobuf/validation/interceptor")
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
clientStream, err := streamer(ctx, desc, cc, method, opts...)
if err != nil {
return nil, err
}
return &clientStreamWrapper{
log: log,
ClientStream: clientStream,
}, nil
}
}
type clientStreamWrapper struct {
log *logrus.Entry
grpc.ClientStream
}
func (c *clientStreamWrapper) SendMsg(req interface{}) error {
// Validate request
if v, ok := req.(Validator); ok {
if err := v.Validate(); err != nil {
// Log warn since the caller is at fault.
c.log.WithError(err).Warn("dropping invalid request")
return status.Errorf(codes.InvalidArgument, err.Error())
}
}
return c.ClientStream.SendMsg(req)
}
func (c *clientStreamWrapper) RecvMsg(res interface{}) error {
if err := c.ClientStream.RecvMsg(res); err != nil {
return err
}
// Validate service response
if v, ok := res.(Validator); ok {
if err := v.Validate(); err != nil {
// Just log debug here since the outbound service is mis-behaving.
c.log.WithError(err).Debug("dropping invalid response")
return status.Errorf(codes.Internal, err.Error())
}
}
return nil
}