| 1 | // Copyright 2022 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package fakeawskms |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "strings" |
| 20 | "testing" |
| 21 | |
| 22 | "github.com/aws/aws-sdk-go/aws" |
| 23 | "github.com/aws/aws-sdk-go/service/kms" |
| 24 | ) |
| 25 | |
| 26 | const validKeyID = "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab" |
| 27 | const validKeyID2 = "arn:aws:kms:us-west-2:123:key/different" |
| 28 | |
| 29 | func TestEncyptDecryptWithValidKeyId(t *testing.T) { |
| 30 | fakeKMS, err := New([]string{validKeyID}) |
| 31 | if err != nil { |
| 32 | t.Fatalf("New() err = %s, want nil", err) |
| 33 | } |
| 34 | |
| 35 | plaintext := []byte("plaintext") |
| 36 | contextValue := "contextValue" |
| 37 | context := map[string]*string{"contextName": &contextValue} |
| 38 | |
| 39 | encRequest := &kms.EncryptInput{ |
| 40 | KeyId: aws.String(validKeyID), |
| 41 | Plaintext: plaintext, |
| 42 | EncryptionContext: context, |
| 43 | } |
| 44 | |
| 45 | encResponse, err := fakeKMS.Encrypt(encRequest) |
| 46 | if err != nil { |
| 47 | t.Fatalf("fakeKMS.Encrypt(encRequest) err = %s, want nil", err) |
| 48 | } |
| 49 | |
| 50 | ciphertext := encResponse.CiphertextBlob |
| 51 | |
| 52 | decRequest := &kms.DecryptInput{ |
| 53 | KeyId: aws.String(validKeyID), |
| 54 | CiphertextBlob: ciphertext, |
| 55 | EncryptionContext: context, |
| 56 | } |
| 57 | decResponse, err := fakeKMS.Decrypt(decRequest) |
| 58 | if err != nil { |
| 59 | t.Fatalf("fakeKMS.Decrypt(decRequest) err = %s, want nil", err) |
| 60 | } |
| 61 | if !bytes.Equal(decResponse.Plaintext, plaintext) { |
| 62 | t.Fatalf("decResponse.Plaintext = %q, want %q", decResponse.Plaintext, plaintext) |
| 63 | } |
| 64 | if strings.Compare(*decResponse.KeyId, validKeyID) != 0 { |
| 65 | t.Fatalf("decResponse.KeyId = %q, want %q", *decResponse.KeyId, validKeyID) |
| 66 | } |
| 67 | |
| 68 | // decrypt with a different context should fail |
| 69 | otherContextValue := "otherContextValue" |
| 70 | otherContext := map[string]*string{"contextName": &otherContextValue} |
| 71 | otherDecRequest := &kms.DecryptInput{ |
| 72 | KeyId: aws.String(validKeyID), |
| 73 | CiphertextBlob: ciphertext, |
| 74 | EncryptionContext: otherContext, |
| 75 | } |
| 76 | if _, err := fakeKMS.Decrypt(otherDecRequest); err == nil { |
| 77 | t.Fatal("fakeKMS.Decrypt(otherDecRequest) err = nil, want not nil") |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestEncyptWithUnknownKeyID(t *testing.T) { |
| 82 | fakeKMS, err := New([]string{validKeyID}) |
| 83 | if err != nil { |
| 84 | t.Fatalf("New() err = %s, want nil", err) |
| 85 | } |
| 86 | |
| 87 | plaintext := []byte("plaintext") |
| 88 | contextValue := "contextValue" |
| 89 | context := map[string]*string{"contextName": &contextValue} |
| 90 | |
| 91 | encRequestWithUnknownKeyID := &kms.EncryptInput{ |
| 92 | KeyId: aws.String(validKeyID2), |
| 93 | Plaintext: plaintext, |
| 94 | EncryptionContext: context, |
| 95 | } |
| 96 | |
| 97 | if _, err := fakeKMS.Encrypt(encRequestWithUnknownKeyID); err == nil { |
| 98 | t.Fatal("fakeKMS.Encrypt(encRequestWithvalidKeyID2) err = nil, want not nil") |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | func TestDecryptWithInvalidCiphertext(t *testing.T) { |
| 103 | fakeKMS, err := New([]string{validKeyID}) |
| 104 | if err != nil { |
| 105 | t.Fatalf("New() err = %s, want nil", err) |
| 106 | } |
| 107 | |
| 108 | invalidCiphertext := []byte("plaintext") |
| 109 | contextValue := "contextValue" |
| 110 | context := map[string]*string{"contextName": &contextValue} |
| 111 | |
| 112 | decRequest := &kms.DecryptInput{ |
| 113 | CiphertextBlob: invalidCiphertext, |
| 114 | EncryptionContext: context, |
| 115 | } |
| 116 | |
| 117 | if _, err := fakeKMS.Decrypt(decRequest); err == nil { |
| 118 | t.Fatal("fakeKMS.Decrypt(decRequest) err = nil, want not nil") |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func TestDecryptWithUnknownKeyId(t *testing.T) { |
| 123 | fakeKMS, err := New([]string{validKeyID}) |
| 124 | if err != nil { |
| 125 | t.Fatalf("New() err = %s, want nil", err) |
| 126 | } |
| 127 | |
| 128 | ciphertext := []byte("invalidCiphertext") |
| 129 | contextValue := "contextValue" |
| 130 | context := map[string]*string{"contextName": &contextValue} |
| 131 | |
| 132 | decRequest := &kms.DecryptInput{ |
| 133 | KeyId: aws.String(validKeyID2), |
| 134 | CiphertextBlob: ciphertext, |
| 135 | EncryptionContext: context, |
| 136 | } |
| 137 | |
| 138 | if _, err := fakeKMS.Decrypt(decRequest); err == nil { |
| 139 | t.Fatal("fakeKMS.Decrypt(decRequest) err = nil, want not nil") |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | func TestDecryptWithWrongKeyId(t *testing.T) { |
| 144 | fakeKMS, err := New([]string{validKeyID, validKeyID2}) |
| 145 | if err != nil { |
| 146 | t.Fatalf("New() err = %s, want nil", err) |
| 147 | } |
| 148 | |
| 149 | plaintext := []byte("plaintext") |
| 150 | contextValue := "contextValue" |
| 151 | context := map[string]*string{"contextName": &contextValue} |
| 152 | |
| 153 | encRequest := &kms.EncryptInput{ |
| 154 | KeyId: aws.String(validKeyID), |
| 155 | Plaintext: plaintext, |
| 156 | EncryptionContext: context, |
| 157 | } |
| 158 | |
| 159 | encResponse, err := fakeKMS.Encrypt(encRequest) |
| 160 | if err != nil { |
| 161 | t.Fatalf("fakeKMS.Encrypt(encRequest) err = %s, want nil", err) |
| 162 | } |
| 163 | |
| 164 | ciphertext := encResponse.CiphertextBlob |
| 165 | |
| 166 | decRequest := &kms.DecryptInput{ |
| 167 | KeyId: aws.String(validKeyID2), // wrong key id |
| 168 | CiphertextBlob: ciphertext, |
| 169 | EncryptionContext: context, |
| 170 | } |
| 171 | if _, err := fakeKMS.Decrypt(decRequest); err == nil { |
| 172 | t.Fatal("fakeKMS.Decrypt(decRequest) err = nil, want not nil") |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | func TestDecryptWithoutKeyId(t *testing.T) { |
| 177 | // setting the keyId in DecryptInput is not required, see |
| 178 | // https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/kms#DecryptInput |
| 179 | |
| 180 | fakeKMS, err := New([]string{validKeyID, validKeyID2}) |
| 181 | if err != nil { |
| 182 | t.Fatalf("New() err = %s, want nil", err) |
| 183 | } |
| 184 | |
| 185 | plaintext := []byte("plaintext") |
| 186 | plaintext2 := []byte("plaintext2") |
| 187 | contextValue := "contextValue" |
| 188 | context := map[string]*string{"contextName": &contextValue} |
| 189 | |
| 190 | encRequest := &kms.EncryptInput{ |
| 191 | KeyId: aws.String(validKeyID), |
| 192 | Plaintext: plaintext, |
| 193 | EncryptionContext: context, |
| 194 | } |
| 195 | encResponse, err := fakeKMS.Encrypt(encRequest) |
| 196 | if err != nil { |
| 197 | t.Fatalf("fakeKMS.Encrypt(encRequest) err = %s, want nil", err) |
| 198 | } |
| 199 | if strings.Compare(*encResponse.KeyId, validKeyID) != 0 { |
| 200 | t.Fatalf("encResponse.KeyId = %q, want %q", *encResponse.KeyId, validKeyID) |
| 201 | } |
| 202 | |
| 203 | encRequest2 := &kms.EncryptInput{ |
| 204 | KeyId: aws.String(validKeyID2), |
| 205 | Plaintext: plaintext2, |
| 206 | EncryptionContext: context, |
| 207 | } |
| 208 | encResponse2, err := fakeKMS.Encrypt(encRequest2) |
| 209 | if err != nil { |
| 210 | t.Fatalf("fakeKMS.Encrypt(encRequest2) err = %s, want nil", err) |
| 211 | } |
| 212 | if strings.Compare(*encResponse2.KeyId, validKeyID2) != 0 { |
| 213 | t.Fatalf("encResponse2.KeyId = %q, want %q", *encResponse2.KeyId, validKeyID2) |
| 214 | } |
| 215 | |
| 216 | decRequest := &kms.DecryptInput{ |
| 217 | // KeyId is not set |
| 218 | CiphertextBlob: encResponse.CiphertextBlob, |
| 219 | EncryptionContext: context, |
| 220 | } |
| 221 | decResponse, err := fakeKMS.Decrypt(decRequest) |
| 222 | if err != nil { |
| 223 | t.Fatalf("fakeKMS.Decrypt(decRequest) err = %s, want nil", err) |
| 224 | } |
| 225 | if !bytes.Equal(decResponse.Plaintext, plaintext) { |
| 226 | t.Fatalf("decResponse.Plaintext = %q, want %q", decResponse.Plaintext, plaintext) |
| 227 | } |
| 228 | if strings.Compare(*decResponse.KeyId, validKeyID) != 0 { |
| 229 | t.Fatalf("decResponse.KeyId = %q, want %q", *decResponse.KeyId, validKeyID) |
| 230 | } |
| 231 | |
| 232 | decRequest2 := &kms.DecryptInput{ |
| 233 | // KeyId is not set |
| 234 | CiphertextBlob: encResponse2.CiphertextBlob, |
| 235 | EncryptionContext: context, |
| 236 | } |
| 237 | decResponse2, err := fakeKMS.Decrypt(decRequest2) |
| 238 | if err != nil { |
| 239 | t.Fatalf("fakeKMS.Decrypt(decRequest2) err = %s, want nil", err) |
| 240 | } |
| 241 | if !bytes.Equal(decResponse2.Plaintext, plaintext2) { |
| 242 | t.Fatalf("decResponse.Plaintext = %q, want %q", decResponse.Plaintext, plaintext2) |
| 243 | } |
| 244 | if strings.Compare(*decResponse2.KeyId, validKeyID2) != 0 { |
| 245 | t.Fatalf("decResponse2.KeyId = %q, want %q", *decResponse2.KeyId, validKeyID2) |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | func TestSerializeContext(t *testing.T) { |
| 250 | uvw := "uvw" |
| 251 | xyz := "xyz" |
| 252 | rst := "rst" |
| 253 | context := map[string]*string{"def": &uvw, "abc": &xyz, "ghi": &rst} |
| 254 | |
| 255 | got := string(serializeContext(context)) |
| 256 | want := "{\"abc\":\"xyz\",\"def\":\"uvw\",\"ghi\":\"rst\"}" |
| 257 | if got != want { |
| 258 | t.Fatalf("SerializeContext(context) = %s, want %s", got, want) |
| 259 | } |
| 260 | |
| 261 | gotEscaped := string(serializeContext(map[string]*string{"a\"b": &xyz})) |
| 262 | wantEscaped := "{\"a\\\"b\":\"xyz\"}" |
| 263 | if gotEscaped != wantEscaped { |
| 264 | t.Fatalf("SerializeContext(context) = %s, want %s", gotEscaped, wantEscaped) |
| 265 | } |
| 266 | |
| 267 | gotEmpty := string(serializeContext(map[string]*string{})) |
| 268 | if gotEmpty != "{}" { |
| 269 | t.Fatalf("SerializeContext(context) = %s, want %s", gotEmpty, "{}") |
| 270 | } |
| 271 | } |