| 1 |
/* |
| 2 |
Copyright 2023 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 service |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"context" |
| 22 |
"encoding/base64" |
| 23 |
"fmt" |
| 24 |
"math/rand" |
| 25 |
"net" |
| 26 |
"os" |
| 27 |
"path/filepath" |
| 28 |
"testing" |
| 29 |
"time" |
| 30 |
|
| 31 |
"google.golang.org/grpc" |
| 32 |
"google.golang.org/grpc/credentials/insecure" |
| 33 |
|
| 34 |
kmsapi "k8s.io/kms/apis/v2" |
| 35 |
) |
| 36 |
|
| 37 |
const version = "v2" |
| 38 |
|
| 39 |
func TestGRPCService(t *testing.T) { |
| 40 |
t.Parallel() |
| 41 |
|
| 42 |
defaultTimeout := 30 * time.Second |
| 43 |
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) |
| 44 |
t.Cleanup(cancel) |
| 45 |
|
| 46 |
address := filepath.Join(os.TempDir(), "kmsv2.sock") |
| 47 |
plaintext := []byte("lorem ipsum dolor sit amet") |
| 48 |
r := rand.New(rand.NewSource(time.Now().Unix())) |
| 49 |
id, err := makeID(r.Read) |
| 50 |
if err != nil { |
| 51 |
t.Fatal(err) |
| 52 |
} |
| 53 |
|
| 54 |
kmsService := newBase64Service(id) |
| 55 |
server := NewGRPCService(address, defaultTimeout, kmsService) |
| 56 |
go func() { |
| 57 |
if err := server.ListenAndServe(); err != nil { |
| 58 |
panic(err) |
| 59 |
} |
| 60 |
}() |
| 61 |
t.Cleanup(server.Shutdown) |
| 62 |
|
| 63 |
client := newClient(t, address) |
| 64 |
|
| 65 |
// make sure the gRPC server is up before running tests |
| 66 |
ready: |
| 67 |
for { |
| 68 |
select { |
| 69 |
case <-ctx.Done(): |
| 70 |
t.Fatalf("server failed to start in time: %v", ctx.Err()) |
| 71 |
|
| 72 |
default: |
| 73 |
if done := func() bool { |
| 74 |
ctx, cancel := context.WithTimeout(ctx, 3*time.Second) |
| 75 |
defer cancel() |
| 76 |
|
| 77 |
_, err := client.Status(ctx, &kmsapi.StatusRequest{}) |
| 78 |
if err != nil { |
| 79 |
t.Logf("failed to get kms status: %v", err) |
| 80 |
} |
| 81 |
|
| 82 |
return err == nil |
| 83 |
}(); done { |
| 84 |
break ready |
| 85 |
} |
| 86 |
time.Sleep(time.Second) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
t.Run("should be able to encrypt and decrypt through unix domain sockets", func(t *testing.T) { |
| 91 |
t.Parallel() |
| 92 |
|
| 93 |
encRes, err := client.Encrypt(ctx, &kmsapi.EncryptRequest{ |
| 94 |
Plaintext: plaintext, |
| 95 |
Uid: id, |
| 96 |
}) |
| 97 |
if err != nil { |
| 98 |
t.Fatal(err) |
| 99 |
} |
| 100 |
|
| 101 |
if bytes.Equal(plaintext, encRes.Ciphertext) { |
| 102 |
t.Fatal("plaintext and ciphertext shouldn't be equal!") |
| 103 |
} |
| 104 |
|
| 105 |
decRes, err := client.Decrypt(ctx, &kmsapi.DecryptRequest{ |
| 106 |
Ciphertext: encRes.Ciphertext, |
| 107 |
KeyId: encRes.KeyId, |
| 108 |
Annotations: encRes.Annotations, |
| 109 |
Uid: id, |
| 110 |
}) |
| 111 |
if err != nil { |
| 112 |
t.Fatal(err) |
| 113 |
} |
| 114 |
|
| 115 |
if !bytes.Equal(decRes.Plaintext, plaintext) { |
| 116 |
t.Errorf("want: %q, have: %q", plaintext, decRes.Plaintext) |
| 117 |
} |
| 118 |
}) |
| 119 |
|
| 120 |
t.Run("should return status data", func(t *testing.T) { |
| 121 |
t.Parallel() |
| 122 |
|
| 123 |
status, err := client.Status(ctx, &kmsapi.StatusRequest{}) |
| 124 |
if err != nil { |
| 125 |
t.Fatal(err) |
| 126 |
} |
| 127 |
|
| 128 |
if status.Healthz != "ok" { |
| 129 |
t.Errorf("want: %q, have: %q", "ok", status.Healthz) |
| 130 |
} |
| 131 |
if len(status.KeyId) == 0 { |
| 132 |
t.Errorf("want: len(keyID) > 0, have: %d", len(status.KeyId)) |
| 133 |
} |
| 134 |
if status.Version != version { |
| 135 |
t.Errorf("want %q, have: %q", version, status.Version) |
| 136 |
} |
| 137 |
}) |
| 138 |
} |
| 139 |
|
| 140 |
func newClient(t *testing.T, address string) kmsapi.KeyManagementServiceClient { |
| 141 |
t.Helper() |
| 142 |
|
| 143 |
cnn, err := grpc.Dial( |
| 144 |
address, |
| 145 |
grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 146 |
grpc.WithDialer(func(addr string, t time.Duration) (net.Conn, error) { |
| 147 |
return net.Dial("unix", addr) |
| 148 |
}), |
| 149 |
) |
| 150 |
if err != nil { |
| 151 |
t.Fatal(err) |
| 152 |
} |
| 153 |
|
| 154 |
t.Cleanup(func() { _ = cnn.Close() }) |
| 155 |
|
| 156 |
return kmsapi.NewKeyManagementServiceClient(cnn) |
| 157 |
} |
| 158 |
|
| 159 |
type testService struct { |
| 160 |
decrypt func(ctx context.Context, uid string, req *DecryptRequest) ([]byte, error) |
| 161 |
encrypt func(ctx context.Context, uid string, data []byte) (*EncryptResponse, error) |
| 162 |
status func(ctx context.Context) (*StatusResponse, error) |
| 163 |
} |
| 164 |
|
| 165 |
var _ Service = (*testService)(nil) |
| 166 |
|
| 167 |
func (s *testService) Decrypt(ctx context.Context, uid string, req *DecryptRequest) ([]byte, error) { |
| 168 |
return s.decrypt(ctx, uid, req) |
| 169 |
} |
| 170 |
|
| 171 |
func (s *testService) Encrypt(ctx context.Context, uid string, data []byte) (*EncryptResponse, error) { |
| 172 |
return s.encrypt(ctx, uid, data) |
| 173 |
} |
| 174 |
|
| 175 |
func (s *testService) Status(ctx context.Context) (*StatusResponse, error) { |
| 176 |
return s.status(ctx) |
| 177 |
} |
| 178 |
|
| 179 |
func makeID(rand func([]byte) (int, error)) (string, error) { |
| 180 |
b := make([]byte, 10) |
| 181 |
if _, err := rand(b); err != nil { |
| 182 |
return "", err |
| 183 |
} |
| 184 |
|
| 185 |
return base64.StdEncoding.EncodeToString(b), nil |
| 186 |
} |
| 187 |
|
| 188 |
func newBase64Service(keyID string) *testService { |
| 189 |
decrypt := func(_ context.Context, _ string, req *DecryptRequest) ([]byte, error) { |
| 190 |
if req.KeyID != keyID { |
| 191 |
return nil, fmt.Errorf("keyID mismatch. want: %q, have: %q", keyID, req.KeyID) |
| 192 |
} |
| 193 |
|
| 194 |
return base64.StdEncoding.DecodeString(string(req.Ciphertext)) |
| 195 |
} |
| 196 |
|
| 197 |
encrypt := func(_ context.Context, _ string, data []byte) (*EncryptResponse, error) { |
| 198 |
return &EncryptResponse{ |
| 199 |
Ciphertext: []byte(base64.StdEncoding.EncodeToString(data)), |
| 200 |
KeyID: keyID, |
| 201 |
}, nil |
| 202 |
} |
| 203 |
|
| 204 |
status := func(_ context.Context) (*StatusResponse, error) { |
| 205 |
return &StatusResponse{ |
| 206 |
Version: version, |
| 207 |
Healthz: "ok", |
| 208 |
KeyID: keyID, |
| 209 |
}, nil |
| 210 |
} |
| 211 |
|
| 212 |
return &testService{ |
| 213 |
decrypt: decrypt, |
| 214 |
encrypt: encrypt, |
| 215 |
status: status, |
| 216 |
} |
| 217 |
} |