@@ -61,29 +61,31 @@ func NewGRPCService(endpoint string, callTimeout time.Duration) (Service, error)
61
61
return nil , err
62
62
}
63
63
64
- connection , err := grpc .Dial (addr , grpc .WithInsecure (), grpc .WithDefaultCallOptions (grpc .WaitForReady (true )), grpc .WithContextDialer (
65
- func (context.Context , string ) (net.Conn , error ) {
66
- // Ignoring addr and timeout arguments:
67
- // addr - comes from the closure
68
- c , err := net .DialUnix (unixProtocol , nil , & net.UnixAddr {Name : addr })
69
- if err != nil {
70
- klog .Errorf ("failed to create connection to unix socket: %s, error: %v" , addr , err )
71
- } else {
72
- klog .V (4 ).Infof ("Successfully dialed Unix socket %v" , addr )
73
- }
74
- return c , err
75
- }))
64
+ s := & gRPCService {callTimeout : callTimeout }
65
+ s .connection , err = grpc .Dial (
66
+ addr ,
67
+ grpc .WithInsecure (),
68
+ grpc .WithUnaryInterceptor (s .interceptor ),
69
+ grpc .WithDefaultCallOptions (grpc .WaitForReady (true )),
70
+ grpc .WithContextDialer (
71
+ func (context.Context , string ) (net.Conn , error ) {
72
+ // Ignoring addr and timeout arguments:
73
+ // addr - comes from the closure
74
+ c , err := net .DialUnix (unixProtocol , nil , & net.UnixAddr {Name : addr })
75
+ if err != nil {
76
+ klog .Errorf ("failed to create connection to unix socket: %s, error: %v" , addr , err )
77
+ } else {
78
+ klog .V (4 ).Infof ("Successfully dialed Unix socket %v" , addr )
79
+ }
80
+ return c , err
81
+ }))
76
82
77
83
if err != nil {
78
84
return nil , fmt .Errorf ("failed to create connection to %s, error: %v" , endpoint , err )
79
85
}
80
86
81
- kmsClient := kmsapi .NewKeyManagementServiceClient (connection )
82
- return & gRPCService {
83
- kmsClient : kmsClient ,
84
- connection : connection ,
85
- callTimeout : callTimeout ,
86
- }, nil
87
+ s .kmsClient = kmsapi .NewKeyManagementServiceClient (s .connection )
88
+ return s , nil
87
89
}
88
90
89
91
// Parse the endpoint to extract schema, host or path.
@@ -139,10 +141,6 @@ func (g *gRPCService) Decrypt(cipher []byte) ([]byte, error) {
139
141
ctx , cancel := context .WithTimeout (context .Background (), g .callTimeout )
140
142
defer cancel ()
141
143
142
- if err := g .checkAPIVersion (ctx ); err != nil {
143
- return nil , err
144
- }
145
-
146
144
request := & kmsapi.DecryptRequest {Cipher : cipher , Version : kmsapiVersion }
147
145
response , err := g .kmsClient .Decrypt (ctx , request )
148
146
if err != nil {
@@ -155,9 +153,6 @@ func (g *gRPCService) Decrypt(cipher []byte) ([]byte, error) {
155
153
func (g * gRPCService ) Encrypt (plain []byte ) ([]byte , error ) {
156
154
ctx , cancel := context .WithTimeout (context .Background (), g .callTimeout )
157
155
defer cancel ()
158
- if err := g .checkAPIVersion (ctx ); err != nil {
159
- return nil , err
160
- }
161
156
162
157
request := & kmsapi.EncryptRequest {Plain : plain , Version : kmsapiVersion }
163
158
response , err := g .kmsClient .Encrypt (ctx , request )
@@ -166,3 +161,21 @@ func (g *gRPCService) Encrypt(plain []byte) ([]byte, error) {
166
161
}
167
162
return response .Cipher , nil
168
163
}
164
+
165
+ func (g * gRPCService ) interceptor (
166
+ ctx context.Context ,
167
+ method string ,
168
+ req interface {},
169
+ reply interface {},
170
+ cc * grpc.ClientConn ,
171
+ invoker grpc.UnaryInvoker ,
172
+ opts ... grpc.CallOption ,
173
+ ) error {
174
+ if ! kmsapi .IsVersionCheckMethod (method ) {
175
+ if err := g .checkAPIVersion (ctx ); err != nil {
176
+ return err
177
+ }
178
+ }
179
+
180
+ return invoker (ctx , method , req , reply , cc , opts ... )
181
+ }
0 commit comments