Skip to content

Commit d2b4723

Browse files
author
immutablet
committed
Move the common logic of checking for kms-plugin's version into gRPC client interceptor.
1 parent 46b33af commit d2b4723

File tree

3 files changed

+65
-26
lines changed

3 files changed

+65
-26
lines changed

staging/src/k8s.io/apiserver/pkg/storage/value/encrypt/envelope/grpc_service.go

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,31 @@ func NewGRPCService(endpoint string, callTimeout time.Duration) (Service, error)
6161
return nil, err
6262
}
6363

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+
}))
7682

7783
if err != nil {
7884
return nil, fmt.Errorf("failed to create connection to %s, error: %v", endpoint, err)
7985
}
8086

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
8789
}
8890

8991
// Parse the endpoint to extract schema, host or path.
@@ -139,10 +141,6 @@ func (g *gRPCService) Decrypt(cipher []byte) ([]byte, error) {
139141
ctx, cancel := context.WithTimeout(context.Background(), g.callTimeout)
140142
defer cancel()
141143

142-
if err := g.checkAPIVersion(ctx); err != nil {
143-
return nil, err
144-
}
145-
146144
request := &kmsapi.DecryptRequest{Cipher: cipher, Version: kmsapiVersion}
147145
response, err := g.kmsClient.Decrypt(ctx, request)
148146
if err != nil {
@@ -155,9 +153,6 @@ func (g *gRPCService) Decrypt(cipher []byte) ([]byte, error) {
155153
func (g *gRPCService) Encrypt(plain []byte) ([]byte, error) {
156154
ctx, cancel := context.WithTimeout(context.Background(), g.callTimeout)
157155
defer cancel()
158-
if err := g.checkAPIVersion(ctx); err != nil {
159-
return nil, err
160-
}
161156

162157
request := &kmsapi.EncryptRequest{Plain: plain, Version: kmsapiVersion}
163158
response, err := g.kmsClient.Encrypt(ctx, request)
@@ -166,3 +161,21 @@ func (g *gRPCService) Encrypt(plain []byte) ([]byte, error) {
166161
}
167162
return response.Cipher, nil
168163
}
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+
}

staging/src/k8s.io/apiserver/pkg/storage/value/encrypt/envelope/v1beta1/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
22

33
go_library(
44
name = "go_default_library",
5-
srcs = ["service.pb.go"],
5+
srcs = [
6+
"service.pb.go",
7+
"v1beta1.go",
8+
],
69
importmap = "k8s.io/kubernetes/vendor/k8s.io/apiserver/pkg/storage/value/encrypt/envelope/v1beta1",
710
importpath = "k8s.io/apiserver/pkg/storage/value/encrypt/envelope/v1beta1",
811
visibility = ["//visibility:public"],
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package v1beta1 contains definition of kms-plugin's gRPC service.
18+
package v1beta1
19+
20+
// IsVersionCheckMethod determines whether the supplied method is a version check against kms-plugin.
21+
func IsVersionCheckMethod(method string) bool {
22+
return method == "/v1beta1.KeyManagementService/Version"
23+
}

0 commit comments

Comments
 (0)