-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver_interceptor.go
89 lines (73 loc) · 2.62 KB
/
server_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
package validation
import (
"context"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type Validator interface {
Validate() error
}
// UnaryServerInterceptor returns a grpc.UnaryServerInterceptor that validates
// inbound and outbound messages. If an inbound message is invalid, a
// codes.InvalidArgument is returned. If an outbound message is invalid, a
// codes.Internal is returned.
func UnaryServerInterceptor() grpc.UnaryServerInterceptor {
log := logrus.StandardLogger().WithField("type", "protobuf/validation/interceptor")
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
if v, ok := req.(Validator); ok {
if err := v.Validate(); err != nil {
// We use a debug level here because it is outside of 'our' control.
log.WithError(err).Debug("dropping invalid request")
return nil, status.Errorf(codes.InvalidArgument, err.Error())
}
}
resp, err := handler(ctx, req)
if err != nil {
return nil, err
}
if v, ok := resp.(Validator); ok {
if err := v.Validate(); err != nil {
// We warn here because this indicates a problem with 'our' service.
log.WithError(err).Warn("dropping invalid response")
return nil, status.Errorf(codes.Internal, err.Error())
}
}
return resp, err
}
}
// StreamServerInterceptor returns a grpc.StreamServerInterceptor that validates
// inbound and outbound messages. If an inbound message is invalid, a
// codes.InvalidArgument is returned. If an outbound message is invalid, a
// codes.Internal is returned.
func StreamServerInterceptor() grpc.StreamServerInterceptor {
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
return handler(srv, &serverStreamWrapper{logrus.StandardLogger().WithField("type", "protobuf/validation/interceptor"), ss})
}
}
type serverStreamWrapper struct {
log *logrus.Entry
grpc.ServerStream
}
func (s *serverStreamWrapper) RecvMsg(req interface{}) error {
if err := s.ServerStream.RecvMsg(req); err != nil {
return err
}
if v, ok := req.(Validator); ok {
if err := v.Validate(); err != nil {
s.log.WithError(err).Debug("dropping invalid request")
return status.Errorf(codes.InvalidArgument, err.Error())
}
}
return nil
}
func (s *serverStreamWrapper) SendMsg(res interface{}) error {
if v, ok := res.(Validator); ok {
if err := v.Validate(); err != nil {
s.log.WithError(err).Warn("dropping invalid response")
return status.Errorf(codes.Internal, err.Error())
}
}
return s.ServerStream.SendMsg(res)
}