| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package fakeawskms |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"context" |
| 20 |
"errors" |
| 21 |
"strings" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"github.com/aws/aws-sdk-go-v2/service/kms" |
| 25 |
"github.com/aws/aws-sdk-go-v2/aws" |
| 26 |
) |
| 27 |
|
| 28 |
const validKeyID = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab" |
| 29 |
const validKeyID2 = "arn:aws:kms:us-west-2:123:key/different" |
| 30 |
|
| 31 |
func TestEncyptDecryptWithValidKeyId(t *testing.T) { |
| 32 |
fakeKMS, err := New([]string{validKeyID}) |
| 33 |
if err != nil { |
| 34 |
t.Fatalf("New() err = %s, want nil", err) |
| 35 |
} |
| 36 |
|
| 37 |
plaintext := []byte("plaintext") |
| 38 |
contextValue := "contextValue" |
| 39 |
encryptionContext := map[string]string{"contextName": contextValue} |
| 40 |
|
| 41 |
encRequest := &kms.EncryptInput{ |
| 42 |
KeyId: aws.String(validKeyID), |
| 43 |
Plaintext: plaintext, |
| 44 |
EncryptionContext: encryptionContext, |
| 45 |
} |
| 46 |
|
| 47 |
encResponse, err := fakeKMS.Encrypt(t.Context(), encRequest) |
| 48 |
if err != nil { |
| 49 |
t.Fatalf("fakeKMS.Encrypt(t.Context(), encRequest) err = %s, want nil", err) |
| 50 |
} |
| 51 |
|
| 52 |
ciphertext := encResponse.CiphertextBlob |
| 53 |
|
| 54 |
decRequest := &kms.DecryptInput{ |
| 55 |
KeyId: aws.String(validKeyID), |
| 56 |
CiphertextBlob: ciphertext, |
| 57 |
EncryptionContext: encryptionContext, |
| 58 |
} |
| 59 |
decResponse, err := fakeKMS.Decrypt(t.Context(), decRequest) |
| 60 |
if err != nil { |
| 61 |
t.Fatalf("fakeKMS.Decrypt(t.Context(), decRequest) err = %s, want nil", err) |
| 62 |
} |
| 63 |
if !bytes.Equal(decResponse.Plaintext, plaintext) { |
| 64 |
t.Fatalf("decResponse.Plaintext = %q, want %q", decResponse.Plaintext, plaintext) |
| 65 |
} |
| 66 |
if strings.Compare(*decResponse.KeyId, validKeyID) != 0 { |
| 67 |
t.Fatalf("decResponse.KeyId = %q, want %q", *decResponse.KeyId, validKeyID) |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
otherContextValue := "otherContextValue" |
| 72 |
otherEncryptionContext := map[string]string{"contextName": otherContextValue} |
| 73 |
otherDecRequest := &kms.DecryptInput{ |
| 74 |
KeyId: aws.String(validKeyID), |
| 75 |
CiphertextBlob: ciphertext, |
| 76 |
EncryptionContext: otherEncryptionContext, |
| 77 |
} |
| 78 |
if _, err := fakeKMS.Decrypt(t.Context(), otherDecRequest); err == nil { |
| 79 |
t.Fatal("fakeKMS.Decrypt(t.Context(), otherDecRequest) err = nil, want not nil") |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func TestEncyptWithUnknownKeyID(t *testing.T) { |
| 84 |
fakeKMS, err := New([]string{validKeyID}) |
| 85 |
if err != nil { |
| 86 |
t.Fatalf("New() err = %s, want nil", err) |
| 87 |
} |
| 88 |
|
| 89 |
plaintext := []byte("plaintext") |
| 90 |
contextValue := "contextValue" |
| 91 |
encryptionContext := map[string]string{"contextName": contextValue} |
| 92 |
|
| 93 |
encRequestWithUnknownKeyID := &kms.EncryptInput{ |
| 94 |
KeyId: aws.String(validKeyID2), |
| 95 |
Plaintext: plaintext, |
| 96 |
EncryptionContext: encryptionContext, |
| 97 |
} |
| 98 |
|
| 99 |
if _, err := fakeKMS.Encrypt(t.Context(), encRequestWithUnknownKeyID); err == nil { |
| 100 |
t.Fatal("fakeKMS.Encrypt(encRequestWithvalidKeyID2) err = nil, want not nil") |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
func TestDecryptWithInvalidCiphertext(t *testing.T) { |
| 105 |
fakeKMS, err := New([]string{validKeyID}) |
| 106 |
if err != nil { |
| 107 |
t.Fatalf("New() err = %s, want nil", err) |
| 108 |
} |
| 109 |
|
| 110 |
invalidCiphertext := []byte("plaintext") |
| 111 |
contextValue := "contextValue" |
| 112 |
encryptionContext := map[string]string{"contextName": contextValue} |
| 113 |
|
| 114 |
decRequest := &kms.DecryptInput{ |
| 115 |
CiphertextBlob: invalidCiphertext, |
| 116 |
EncryptionContext: encryptionContext, |
| 117 |
} |
| 118 |
|
| 119 |
if _, err := fakeKMS.Decrypt(t.Context(), decRequest); err == nil { |
| 120 |
t.Fatal("fakeKMS.Decrypt(t.Context(), decRequest) err = nil, want not nil") |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
func TestDecryptWithUnknownKeyId(t *testing.T) { |
| 125 |
fakeKMS, err := New([]string{validKeyID}) |
| 126 |
if err != nil { |
| 127 |
t.Fatalf("New() err = %s, want nil", err) |
| 128 |
} |
| 129 |
|
| 130 |
ciphertext := []byte("invalidCiphertext") |
| 131 |
contextValue := "contextValue" |
| 132 |
encryptionContext := map[string]string{"contextName": contextValue} |
| 133 |
|
| 134 |
decRequest := &kms.DecryptInput{ |
| 135 |
KeyId: aws.String(validKeyID2), |
| 136 |
CiphertextBlob: ciphertext, |
| 137 |
EncryptionContext: encryptionContext, |
| 138 |
} |
| 139 |
|
| 140 |
if _, err := fakeKMS.Decrypt(t.Context(), decRequest); err == nil { |
| 141 |
t.Fatal("fakeKMS.Decrypt(t.Context(), decRequest) err = nil, want not nil") |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func TestDecryptWithWrongKeyId(t *testing.T) { |
| 146 |
fakeKMS, err := New([]string{validKeyID, validKeyID2}) |
| 147 |
if err != nil { |
| 148 |
t.Fatalf("New() err = %s, want nil", err) |
| 149 |
} |
| 150 |
|
| 151 |
plaintext := []byte("plaintext") |
| 152 |
contextValue := "contextValue" |
| 153 |
encryptionContext := map[string]string{"contextName": contextValue} |
| 154 |
|
| 155 |
encRequest := &kms.EncryptInput{ |
| 156 |
KeyId: aws.String(validKeyID), |
| 157 |
Plaintext: plaintext, |
| 158 |
EncryptionContext: encryptionContext, |
| 159 |
} |
| 160 |
|
| 161 |
encResponse, err := fakeKMS.Encrypt(t.Context(), encRequest) |
| 162 |
if err != nil { |
| 163 |
t.Fatalf("fakeKMS.Encrypt(t.Context(), encRequest) err = %s, want nil", err) |
| 164 |
} |
| 165 |
|
| 166 |
ciphertext := encResponse.CiphertextBlob |
| 167 |
|
| 168 |
decRequest := &kms.DecryptInput{ |
| 169 |
KeyId: aws.String(validKeyID2), |
| 170 |
CiphertextBlob: ciphertext, |
| 171 |
EncryptionContext: encryptionContext, |
| 172 |
} |
| 173 |
if _, err := fakeKMS.Decrypt(t.Context(), decRequest); err == nil { |
| 174 |
t.Fatal("fakeKMS.Decrypt(t.Context(), decRequest) err = nil, want not nil") |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
func TestDecryptWithoutKeyId(t *testing.T) { |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
fakeKMS, err := New([]string{validKeyID, validKeyID2}) |
| 183 |
if err != nil { |
| 184 |
t.Fatalf("New() err = %s, want nil", err) |
| 185 |
} |
| 186 |
|
| 187 |
plaintext := []byte("plaintext") |
| 188 |
plaintext2 := []byte("plaintext2") |
| 189 |
contextValue := "contextValue" |
| 190 |
encryptionContext := map[string]string{"contextName": contextValue} |
| 191 |
|
| 192 |
encRequest := &kms.EncryptInput{ |
| 193 |
KeyId: aws.String(validKeyID), |
| 194 |
Plaintext: plaintext, |
| 195 |
EncryptionContext: encryptionContext, |
| 196 |
} |
| 197 |
encResponse, err := fakeKMS.Encrypt(t.Context(), encRequest) |
| 198 |
if err != nil { |
| 199 |
t.Fatalf("fakeKMS.Encrypt(t.Context(), encRequest) err = %s, want nil", err) |
| 200 |
} |
| 201 |
if strings.Compare(*encResponse.KeyId, validKeyID) != 0 { |
| 202 |
t.Fatalf("encResponse.KeyId = %q, want %q", *encResponse.KeyId, validKeyID) |
| 203 |
} |
| 204 |
|
| 205 |
encRequest2 := &kms.EncryptInput{ |
| 206 |
KeyId: aws.String(validKeyID2), |
| 207 |
Plaintext: plaintext2, |
| 208 |
EncryptionContext: encryptionContext, |
| 209 |
} |
| 210 |
encResponse2, err := fakeKMS.Encrypt(t.Context(), encRequest2) |
| 211 |
if err != nil { |
| 212 |
t.Fatalf("fakeKMS.Encrypt(t.Context(), encRequest2) err = %s, want nil", err) |
| 213 |
} |
| 214 |
if strings.Compare(*encResponse2.KeyId, validKeyID2) != 0 { |
| 215 |
t.Fatalf("encResponse2.KeyId = %q, want %q", *encResponse2.KeyId, validKeyID2) |
| 216 |
} |
| 217 |
|
| 218 |
decRequest := &kms.DecryptInput{ |
| 219 |
|
| 220 |
CiphertextBlob: encResponse.CiphertextBlob, |
| 221 |
EncryptionContext: encryptionContext, |
| 222 |
} |
| 223 |
decResponse, err := fakeKMS.Decrypt(t.Context(), decRequest) |
| 224 |
if err != nil { |
| 225 |
t.Fatalf("fakeKMS.Decrypt(t.Context(), decRequest) err = %s, want nil", err) |
| 226 |
} |
| 227 |
if !bytes.Equal(decResponse.Plaintext, plaintext) { |
| 228 |
t.Fatalf("decResponse.Plaintext = %q, want %q", decResponse.Plaintext, plaintext) |
| 229 |
} |
| 230 |
if strings.Compare(*decResponse.KeyId, validKeyID) != 0 { |
| 231 |
t.Fatalf("decResponse.KeyId = %q, want %q", *decResponse.KeyId, validKeyID) |
| 232 |
} |
| 233 |
|
| 234 |
decRequest2 := &kms.DecryptInput{ |
| 235 |
|
| 236 |
CiphertextBlob: encResponse2.CiphertextBlob, |
| 237 |
EncryptionContext: encryptionContext, |
| 238 |
} |
| 239 |
decResponse2, err := fakeKMS.Decrypt(t.Context(), decRequest2) |
| 240 |
if err != nil { |
| 241 |
t.Fatalf("fakeKMS.Decrypt(decRequest2) err = %s, want nil", err) |
| 242 |
} |
| 243 |
if !bytes.Equal(decResponse2.Plaintext, plaintext2) { |
| 244 |
t.Fatalf("decResponse.Plaintext = %q, want %q", decResponse.Plaintext, plaintext2) |
| 245 |
} |
| 246 |
if strings.Compare(*decResponse2.KeyId, validKeyID2) != 0 { |
| 247 |
t.Fatalf("decResponse2.KeyId = %q, want %q", *decResponse2.KeyId, validKeyID2) |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
func TestEncryptDecrypt_withCanceledContext_fails(t *testing.T) { |
| 252 |
fakeKMS, err := New([]string{validKeyID}) |
| 253 |
if err != nil { |
| 254 |
t.Fatalf("New() err = %s, want nil", err) |
| 255 |
} |
| 256 |
|
| 257 |
plaintext := []byte("plaintext") |
| 258 |
contextValue := "contextValue" |
| 259 |
encryptionContext := map[string]string{"contextName": contextValue} |
| 260 |
|
| 261 |
|
| 262 |
encRequest := &kms.EncryptInput{ |
| 263 |
KeyId: aws.String(validKeyID), |
| 264 |
Plaintext: plaintext, |
| 265 |
EncryptionContext: encryptionContext, |
| 266 |
} |
| 267 |
|
| 268 |
encResponse, err := fakeKMS.Encrypt(t.Context(), encRequest) |
| 269 |
if err != nil { |
| 270 |
t.Fatalf("fakeKMS.Encrypt(t.Context(), encRequest) err = %s, want nil", err) |
| 271 |
} |
| 272 |
ciphertext := encResponse.CiphertextBlob |
| 273 |
decRequest := &kms.DecryptInput{ |
| 274 |
KeyId: aws.String(validKeyID), |
| 275 |
CiphertextBlob: ciphertext, |
| 276 |
EncryptionContext: encryptionContext, |
| 277 |
} |
| 278 |
|
| 279 |
ctx := t.Context() |
| 280 |
canceledCtx, cancel := context.WithCancelCause(ctx) |
| 281 |
causeErr := errors.New("cause error message") |
| 282 |
cancel(causeErr) |
| 283 |
|
| 284 |
_, err = fakeKMS.Encrypt(canceledCtx, encRequest) |
| 285 |
if err == nil { |
| 286 |
t.Fatalf("fakeKMS.Encrypt(canceledCtx, encRequest) err = nil, want error") |
| 287 |
} |
| 288 |
_, err = fakeKMS.Decrypt(canceledCtx, decRequest) |
| 289 |
if err == nil { |
| 290 |
t.Fatalf("fakeKMS.Decrypt(canceledCtx, decRequest) err = nil, want error") |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
func TestSerializeEncryptionContext(t *testing.T) { |
| 295 |
uvw := "uvw" |
| 296 |
xyz := "xyz" |
| 297 |
rst := "rst" |
| 298 |
encryptionContext := map[string]string{"def": uvw, "abc": xyz, "ghi": rst} |
| 299 |
|
| 300 |
got := string(serializeEncryptionContext(encryptionContext)) |
| 301 |
want := "{\"abc\":\"xyz\",\"def\":\"uvw\",\"ghi\":\"rst\"}" |
| 302 |
if got != want { |
| 303 |
t.Fatalf("serializeEncryptionContext(context) = %s, want %s", got, want) |
| 304 |
} |
| 305 |
|
| 306 |
gotEscaped := string(serializeEncryptionContext(map[string]string{"a\"b": xyz})) |
| 307 |
wantEscaped := "{\"a\\\"b\":\"xyz\"}" |
| 308 |
if gotEscaped != wantEscaped { |
| 309 |
t.Fatalf("serializeEncryptionContext(context) = %s, want %s", gotEscaped, wantEscaped) |
| 310 |
} |
| 311 |
|
| 312 |
gotEmpty := string(serializeEncryptionContext(map[string]string{})) |
| 313 |
if gotEmpty != "{}" { |
| 314 |
t.Fatalf("serializeEncryptionContext(context) = %s, want %s", gotEmpty, "{}") |
| 315 |
} |
| 316 |
} |