|
| 1 | +package yckms |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + yckms "github.com/yandex-cloud/go-genproto/yandex/cloud/kms/v1" |
| 7 | + kms "github.com/yandex-cloud/go-sdk/gen/kmscrypto" |
| 8 | + "google.golang.org/grpc" |
| 9 | + "google.golang.org/grpc/codes" |
| 10 | + "google.golang.org/grpc/credentials/insecure" |
| 11 | + "google.golang.org/grpc/status" |
| 12 | + "google.golang.org/grpc/test/bufconn" |
| 13 | + "net" |
| 14 | + "testing" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + dummyKey = "my:dummy:ru-east-1:12345678:key/920aff2e-c5f1-4040-943a-047fa387b27e+my:dummy:iam::927034868273:role/sops-dev" |
| 22 | + anotherDummyKey = "my:dummy:ru-east-1:12345678:key/920aff2e-c5f1-4040-943a-047fa387b27e+my:dummy:iam::927034868273" |
| 23 | + dummyKeys = dummyKey + ", " + anotherDummyKey |
| 24 | + decodedKey = "I want to be a DJ" |
| 25 | +) |
| 26 | + |
| 27 | +const bufSize = 1024 * 1024 |
| 28 | + |
| 29 | +var lis *bufconn.Listener |
| 30 | + |
| 31 | +func init() { |
| 32 | + lis = bufconn.Listen(bufSize) |
| 33 | + s := grpc.NewServer() |
| 34 | + yckms.RegisterSymmetricCryptoServiceServer(s, mockSymmetricCryptoServiceServer{}) |
| 35 | + go func() { |
| 36 | + if err := s.Serve(lis); err != nil { |
| 37 | + log.Fatalf("Server exited with error: %v", err) |
| 38 | + } |
| 39 | + }() |
| 40 | +} |
| 41 | + |
| 42 | +func bufDialer(context.Context, string) (net.Conn, error) { |
| 43 | + return lis.Dial() |
| 44 | +} |
| 45 | + |
| 46 | +type mockSymmetricCryptoServiceServer struct { |
| 47 | +} |
| 48 | + |
| 49 | +func (mockSymmetricCryptoServiceServer) Encrypt(context.Context, *yckms.SymmetricEncryptRequest) (*yckms.SymmetricEncryptResponse, error) { |
| 50 | + key, _ := base64.StdEncoding.DecodeString("test") |
| 51 | + return &yckms.SymmetricEncryptResponse{ |
| 52 | + Ciphertext: key, |
| 53 | + }, nil |
| 54 | +} |
| 55 | +func (mockSymmetricCryptoServiceServer) Decrypt(context.Context, *yckms.SymmetricDecryptRequest) (*yckms.SymmetricDecryptResponse, error) { |
| 56 | + return &yckms.SymmetricDecryptResponse{ |
| 57 | + Plaintext: []byte(decodedKey), |
| 58 | + }, nil |
| 59 | +} |
| 60 | +func (mockSymmetricCryptoServiceServer) ReEncrypt(context.Context, *yckms.SymmetricReEncryptRequest) (*yckms.SymmetricReEncryptResponse, error) { |
| 61 | + return nil, status.Errorf(codes.Unimplemented, "method ReEncrypt not implemented") |
| 62 | +} |
| 63 | +func (mockSymmetricCryptoServiceServer) GenerateDataKey(context.Context, *yckms.GenerateDataKeyRequest) (*yckms.GenerateDataKeyResponse, error) { |
| 64 | + return nil, status.Errorf(codes.Unimplemented, "method GenerateDataKey not implemented") |
| 65 | +} |
| 66 | + |
| 67 | +func TestNewMasterKeyFromKeyID(t *testing.T) { |
| 68 | + key, err := NewMasterKeyFromKeyID(dummyKey) |
| 69 | + assert.NoError(t, err) |
| 70 | + assert.Equal(t, dummyKey, key.KeyID) |
| 71 | + assert.NotNil(t, key.CreationDate) |
| 72 | +} |
| 73 | + |
| 74 | +func TestNewMasterKeyFromKeyIDString(t *testing.T) { |
| 75 | + keys, err := NewMasterKeyFromKeyIDString(dummyKeys) |
| 76 | + assert.NoError(t, err) |
| 77 | + assert.Len(t, keys, 2) |
| 78 | + |
| 79 | + k1 := keys[0] |
| 80 | + k2 := keys[1] |
| 81 | + |
| 82 | + assert.Equal(t, dummyKey, k1.KeyID) |
| 83 | + assert.Equal(t, anotherDummyKey, k2.KeyID) |
| 84 | +} |
| 85 | + |
| 86 | +func TestMasterKey_Encrypt(t *testing.T) { |
| 87 | + t.Run("encrypt", func(t *testing.T) { |
| 88 | + kmsClient, err := createTestKMSClient() |
| 89 | + assert.NoError(t, err) |
| 90 | + |
| 91 | + key := &MasterKey{ |
| 92 | + client: kmsClient, |
| 93 | + } |
| 94 | + |
| 95 | + dataKey := []byte(decodedKey) |
| 96 | + assert.NoError(t, key.Encrypt(dataKey)) |
| 97 | + |
| 98 | + decodedCipher, err := base64.StdEncoding.DecodeString(key.EncryptedKey) |
| 99 | + assert.NoError(t, err) |
| 100 | + |
| 101 | + decrypted, err := kmsClient.Decrypt(context.TODO(), &yckms.SymmetricDecryptRequest{ |
| 102 | + KeyId: key.KeyID, |
| 103 | + Ciphertext: decodedCipher, |
| 104 | + }) |
| 105 | + if assert.NoError(t, err) { |
| 106 | + assert.NotNil(t, decrypted) |
| 107 | + assert.Equal(t, dataKey, decrypted.Plaintext) |
| 108 | + } |
| 109 | + }) |
| 110 | +} |
| 111 | + |
| 112 | +func TestMasterKey_Decrypt(t *testing.T) { |
| 113 | + t.Run("decrypt", func(t *testing.T) { |
| 114 | + kmsClient, err := createTestKMSClient() |
| 115 | + assert.NoError(t, err) |
| 116 | + |
| 117 | + key := &MasterKey{ |
| 118 | + client: kmsClient, |
| 119 | + } |
| 120 | + |
| 121 | + dataKey := []byte(decodedKey) |
| 122 | + out, err := kmsClient.Encrypt(context.TODO(), &yckms.SymmetricEncryptRequest{ |
| 123 | + KeyId: dummyKey, |
| 124 | + Plaintext: dataKey, |
| 125 | + }) |
| 126 | + assert.NoError(t, err) |
| 127 | + |
| 128 | + key.EncryptedKey = base64.StdEncoding.EncodeToString(out.Ciphertext) |
| 129 | + got, err := key.Decrypt() |
| 130 | + assert.NoError(t, err) |
| 131 | + assert.Equal(t, dataKey, got) |
| 132 | + }) |
| 133 | +} |
| 134 | + |
| 135 | +func TestMasterKey_EncryptedDataKey(t *testing.T) { |
| 136 | + key := &MasterKey{EncryptedKey: "some key"} |
| 137 | + assert.EqualValues(t, key.EncryptedKey, key.EncryptedDataKey()) |
| 138 | +} |
| 139 | + |
| 140 | +func TestMasterKey_SetEncryptedDataKey(t *testing.T) { |
| 141 | + key := &MasterKey{} |
| 142 | + data := []byte("some data") |
| 143 | + key.SetEncryptedDataKey(data) |
| 144 | + assert.EqualValues(t, data, key.EncryptedKey) |
| 145 | +} |
| 146 | + |
| 147 | +func TestMasterKey_NeedsRotation(t *testing.T) { |
| 148 | + t.Run("false", func(t *testing.T) { |
| 149 | + k := &MasterKey{} |
| 150 | + k.CreationDate = time.Now().UTC() |
| 151 | + |
| 152 | + assert.False(t, k.NeedsRotation()) |
| 153 | + }) |
| 154 | + |
| 155 | + t.Run("true", func(t *testing.T) { |
| 156 | + k := &MasterKey{} |
| 157 | + k.CreationDate = time.Now().UTC().Add(-kmsTTL - 1) |
| 158 | + |
| 159 | + assert.True(t, k.NeedsRotation()) |
| 160 | + }) |
| 161 | +} |
| 162 | + |
| 163 | +func TestMasterKey_ToString(t *testing.T) { |
| 164 | + key, err := NewMasterKeyFromKeyID(dummyKey) |
| 165 | + assert.NoError(t, err) |
| 166 | + |
| 167 | + assert.Equal(t, dummyKey, key.ToString()) |
| 168 | +} |
| 169 | + |
| 170 | +func TestMasterKey_ToMap(t *testing.T) { |
| 171 | + key, err := NewMasterKeyFromKeyID(dummyKey) |
| 172 | + assert.NoError(t, err) |
| 173 | + |
| 174 | + data := []byte("some data") |
| 175 | + key.SetEncryptedDataKey(data) |
| 176 | + |
| 177 | + res := key.ToMap() |
| 178 | + assert.Equal(t, dummyKey, res["key_id"]) |
| 179 | + assert.Equal(t, key.CreationDate.UTC().Format(time.RFC3339), res["created_at"]) |
| 180 | + assert.Equal(t, "some data", res["enc"]) |
| 181 | +} |
| 182 | + |
| 183 | +func createTestKMSClient() (*kms.SymmetricCryptoServiceClient, error) { |
| 184 | + return kms.NewKMSCrypto(func(ctx context.Context) (*grpc.ClientConn, error) { |
| 185 | + return grpc.DialContext( |
| 186 | + ctx, |
| 187 | + "bufnet", |
| 188 | + grpc.WithContextDialer(bufDialer), |
| 189 | + grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 190 | + ) |
| 191 | + }).SymmetricCrypto(), nil |
| 192 | +} |
0 commit comments