Debian Codegraph @

pkg/storage/value/encrypt/envelope/testing/v2/kms_plugin_mock.go

raw
1 //go:build !windows
2 // +build !windows
3
4 /*
5 Copyright 2022 The Kubernetes Authors.
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 */
19
20 package v2
21
22 import (
23 "context"
24 "encoding/base64"
25 "fmt"
26 "net"
27 "os"
28 "sync"
29 "testing"
30 "time"
31
32 "google.golang.org/grpc"
33 "google.golang.org/grpc/codes"
34 "google.golang.org/grpc/metadata"
35 "google.golang.org/grpc/status"
36
37 "k8s.io/apimachinery/pkg/util/wait"
38 "k8s.io/klog/v2"
39 kmsapi "k8s.io/kms/apis/v2"
40 )
41
42 const (
43 // Now only supported unix domain socket.
44 unixProtocol = "unix"
45
46 // Current version for the protocol interface definition.
47 kmsapiVersion = "v2beta1"
48 )
49
50 // Base64Plugin gRPC sever for a mock KMS provider.
51 // Uses base64 to simulate encrypt and decrypt.
52 type Base64Plugin struct {
53 grpcServer *grpc.Server
54 listener net.Listener
55 mu *sync.Mutex
56 lastEncryptRequest *kmsapi.EncryptRequest
57 inFailedState bool
58 ver string
59 socketPath string
60 keyID string
61 }
62
63 // NewBase64Plugin is a constructor for Base64Plugin.
64 func NewBase64Plugin(t testing.TB, socketPath string) *Base64Plugin {
65 server := grpc.NewServer(
66 grpc.UnaryInterceptor(
67 func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
68 if val := metadata.ValueFromIncomingContext(ctx, ":authority"); len(val) != 1 || val[0] != "localhost" {
69 t.Errorf("wanted localhost authority, got: %v", val)
70 }
71 return handler(ctx, req)
72 },
73 ),
74 )
75 result := &Base64Plugin{
76 grpcServer: server,
77 mu: &sync.Mutex{},
78 ver: kmsapiVersion,
79 socketPath: socketPath,
80 keyID: "1",
81 }
82
83 kmsapi.RegisterKeyManagementServiceServer(server, result)
84
85 if err := result.start(); err != nil {
86 t.Fatalf("failed to start KMS plugin, err: %v", err)
87 }
88 t.Cleanup(result.CleanUp)
89 if err := waitForBase64PluginToBeUp(result); err != nil {
90 t.Fatalf("failed to start KMS plugin: err: %v", err)
91 }
92 return result
93 }
94
95 // waitForBase64PluginToBeUp waits until the plugin is ready to serve requests.
96 func waitForBase64PluginToBeUp(plugin *Base64Plugin) error {
97 var gRPCErr error
98 var resp *kmsapi.StatusResponse
99 pollErr := wait.PollImmediate(1*time.Second, wait.ForeverTestTimeout, func() (bool, error) {
100 resp, gRPCErr = plugin.Status(context.Background(), &kmsapi.StatusRequest{})
101 return gRPCErr == nil && resp.Healthz == "ok", nil
102 })
103
104 if pollErr != nil {
105 return fmt.Errorf("failed to start kms-plugin, gRPC error: %v, poll error: %v", gRPCErr, pollErr)
106 }
107
108 return nil
109 }
110
111 // waitForBase64PluginToBeUpdated waits until the plugin updates keyID.
112 func WaitForBase64PluginToBeUpdated(plugin *Base64Plugin) error {
113 var gRPCErr error
114 var resp *kmsapi.StatusResponse
115
116 updatePollErr := wait.PollImmediate(1*time.Second, wait.ForeverTestTimeout, func() (bool, error) {
117 resp, gRPCErr = plugin.Status(context.Background(), &kmsapi.StatusRequest{})
118 klog.InfoS("WaitForBase64PluginToBeUpdated", "keyID", resp.KeyId)
119 return gRPCErr == nil && resp.Healthz == "ok" && resp.KeyId == "2", nil
120 })
121
122 if updatePollErr != nil {
123 return fmt.Errorf("failed to update keyID for kmsv2-plugin, gRPC error: %w, updatePoll error: %w", gRPCErr, updatePollErr)
124 }
125
126 return nil
127 }
128
129 // LastEncryptRequest returns the last EncryptRequest.Plain sent to the plugin.
130 func (s *Base64Plugin) LastEncryptRequest() []byte {
131 return s.lastEncryptRequest.Plaintext
132 }
133
134 // SetVersion sets the version of kms-plugin.
135 func (s *Base64Plugin) SetVersion(ver string) {
136 s.ver = ver
137 }
138
139 // start starts plugin's gRPC service.
140 func (s *Base64Plugin) start() error {
141 var err error
142 s.listener, err = net.Listen(unixProtocol, s.socketPath)
143 if err != nil {
144 return fmt.Errorf("failed to listen on the unix socket, error: %v", err)
145 }
146 klog.InfoS("Starting KMS Plugin", "socketPath", s.socketPath)
147
148 go s.grpcServer.Serve(s.listener)
149 return nil
150 }
151
152 // CleanUp stops gRPC server and the underlying listener.
153 func (s *Base64Plugin) CleanUp() {
154 s.grpcServer.Stop()
155 _ = s.listener.Close()
156 _ = os.Remove(s.socketPath)
157 }
158
159 // EnterFailedState places the plugin into failed state.
160 func (s *Base64Plugin) EnterFailedState() {
161 s.mu.Lock()
162 defer s.mu.Unlock()
163 s.inFailedState = true
164 }
165
166 // ExitFailedState removes the plugin from the failed state.
167 func (s *Base64Plugin) ExitFailedState() {
168 s.mu.Lock()
169 defer s.mu.Unlock()
170 s.inFailedState = false
171 }
172
173 // Update keyID for the plugin.
174 func (s *Base64Plugin) UpdateKeyID() {
175 s.mu.Lock()
176 defer s.mu.Unlock()
177 s.keyID = "2"
178 }
179
180 // Status returns the status of the kms-plugin.
181 func (s *Base64Plugin) Status(ctx context.Context, request *kmsapi.StatusRequest) (*kmsapi.StatusResponse, error) {
182 klog.V(3).InfoS("Received request for Status", "request", request)
183 s.mu.Lock()
184 defer s.mu.Unlock()
185
186 if s.inFailedState {
187 return nil, status.Error(codes.FailedPrecondition, "failed precondition - key disabled")
188 }
189
190 return &kmsapi.StatusResponse{Version: s.ver, Healthz: "ok", KeyId: s.keyID}, nil
191 }
192
193 // Decrypt performs base64 decoding of the payload of kms.DecryptRequest.
194 func (s *Base64Plugin) Decrypt(ctx context.Context, request *kmsapi.DecryptRequest) (*kmsapi.DecryptResponse, error) {
195 klog.V(3).InfoS("Received Decrypt Request", "ciphertext", string(request.Ciphertext))
196
197 s.mu.Lock()
198 defer s.mu.Unlock()
199 if s.inFailedState {
200 return nil, status.Error(codes.FailedPrecondition, "failed precondition - key disabled")
201 }
202 if len(request.Uid) == 0 {
203 return nil, status.Error(codes.InvalidArgument, "uid is required")
204 }
205
206 buf := make([]byte, base64.StdEncoding.DecodedLen(len(request.Ciphertext)))
207 n, err := base64.StdEncoding.Decode(buf, request.Ciphertext)
208 if err != nil {
209 return nil, err
210 }
211
212 return &kmsapi.DecryptResponse{Plaintext: buf[:n]}, nil
213 }
214
215 // Encrypt performs base64 encoding of the payload of kms.EncryptRequest.
216 func (s *Base64Plugin) Encrypt(ctx context.Context, request *kmsapi.EncryptRequest) (*kmsapi.EncryptResponse, error) {
217 klog.V(3).InfoS("Received Encrypt Request", "plaintext", string(request.Plaintext))
218 s.mu.Lock()
219 defer s.mu.Unlock()
220 s.lastEncryptRequest = request
221
222 if s.inFailedState {
223 return nil, status.Error(codes.FailedPrecondition, "failed precondition - key disabled")
224 }
225 if len(request.Uid) == 0 {
226 return nil, status.Error(codes.InvalidArgument, "uid is required")
227 }
228
229 buf := make([]byte, base64.StdEncoding.EncodedLen(len(request.Plaintext)))
230 base64.StdEncoding.Encode(buf, request.Plaintext)
231
232 return &kmsapi.EncryptResponse{Ciphertext: buf, KeyId: s.keyID, Annotations: map[string][]byte{"local-kek.kms.kubernetes.io": []byte("encrypted-local-kek")}}, nil
233 }