|
15 | 15 | package common |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "strings" |
18 | 19 | "testing" |
19 | 20 |
|
20 | 21 | "github.com/google/go-cmp/cmp" |
@@ -72,3 +73,201 @@ func TestValidateOCI(t *testing.T) { |
72 | 73 | }) |
73 | 74 | } |
74 | 75 | } |
| 76 | + |
| 77 | +func TestValidAWSKMSRegex(t *testing.T) { |
| 78 | + tests := []struct { |
| 79 | + name string |
| 80 | + ref string |
| 81 | + shouldMatch bool |
| 82 | + }{ |
| 83 | + { |
| 84 | + name: "valid key ID", |
| 85 | + ref: "awskms:///1234abcd-12ab-34cd-56ef-1234567890ab", |
| 86 | + shouldMatch: true, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "valid key ID with endpoint", |
| 90 | + ref: "awskms://localhost:4566/1234abcd-12ab-34cd-56ef-1234567890ab", |
| 91 | + shouldMatch: true, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "valid key ARN", |
| 95 | + ref: "awskms:///arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab", |
| 96 | + shouldMatch: true, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "valid key ARN with endpoint", |
| 100 | + ref: "awskms://localhost:4566/arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab", |
| 101 | + shouldMatch: true, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "valid alias name", |
| 105 | + ref: "awskms:///alias/ExampleAlias", |
| 106 | + shouldMatch: true, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "valid alias name with endpoint", |
| 110 | + ref: "awskms://localhost:4566/alias/ExampleAlias", |
| 111 | + shouldMatch: true, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "valid alias ARN", |
| 115 | + ref: "awskms:///arn:aws:kms:us-east-2:111122223333:alias/ExampleAlias", |
| 116 | + shouldMatch: true, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "valid alias ARN with endpoint", |
| 120 | + ref: "awskms://localhost:4566/arn:aws:kms:us-east-2:111122223333:alias/ExampleAlias", |
| 121 | + shouldMatch: true, |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "invalid format - missing prefix", |
| 125 | + ref: "kms:///1234abcd-12ab-34cd-56ef-1234567890ab", |
| 126 | + shouldMatch: false, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "invalid format - missing slashes", |
| 130 | + ref: "awskms:/1234abcd-12ab-34cd-56ef-1234567890ab", |
| 131 | + shouldMatch: false, |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "invalid format - malformed UUID", |
| 135 | + ref: "awskms:///1234abcd-12ab-34cd-56ef-1234567890", |
| 136 | + shouldMatch: false, |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "invalid format - malformed ARN", |
| 140 | + ref: "awskms:///arn:aws:kms:us-east-2:key/1234abcd-12ab-34cd-56ef-1234567890ab", |
| 141 | + shouldMatch: false, |
| 142 | + }, |
| 143 | + } |
| 144 | + |
| 145 | + for _, test := range tests { |
| 146 | + t.Run(test.name, func(t *testing.T) { |
| 147 | + err := validAWSKMSRegex(test.ref) |
| 148 | + if test.shouldMatch && err != nil { |
| 149 | + t.Errorf("Expected regex to match, but got error: %v", err) |
| 150 | + } |
| 151 | + if !test.shouldMatch && err == nil { |
| 152 | + t.Errorf("Expected regex not to match, but it did") |
| 153 | + } |
| 154 | + }) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func TestValidateAWSKMS(t *testing.T) { |
| 159 | + tests := []struct { |
| 160 | + name string |
| 161 | + kms string |
| 162 | + expectError bool |
| 163 | + errorContains string |
| 164 | + }{ |
| 165 | + // Only ARN formats don't cause errors with the current arn.Parse implementation |
| 166 | + { |
| 167 | + name: "valid key ARN", |
| 168 | + kms: "awskms:///arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab", |
| 169 | + expectError: false, |
| 170 | + }, |
| 171 | + { |
| 172 | + name: "too few parts", |
| 173 | + kms: "awskms://keyid", |
| 174 | + expectError: true, |
| 175 | + errorContains: "malformed AWS KMS format", |
| 176 | + }, |
| 177 | + { |
| 178 | + name: "invalid regex", |
| 179 | + kms: "awskms:///invalid-key-id", |
| 180 | + expectError: true, |
| 181 | + errorContains: "kms key should be in the format", |
| 182 | + }, |
| 183 | + { |
| 184 | + name: "ARN as endpoint", |
| 185 | + kms: "awskms://arn:aws:kms:us-east-2:111122223333/key/1234abcd-12ab-34cd-56ef-1234567890ab", |
| 186 | + expectError: true, |
| 187 | + errorContains: "kms key should be in the format", |
| 188 | + }, |
| 189 | + { |
| 190 | + name: "invalid endpoint", |
| 191 | + kms: "awskms://invalid_endpoint/1234abcd-12ab-34cd-56ef-1234567890ab", |
| 192 | + expectError: true, |
| 193 | + errorContains: "malformed endpoint", |
| 194 | + }, |
| 195 | + } |
| 196 | + |
| 197 | + for _, test := range tests { |
| 198 | + t.Run(test.name, func(t *testing.T) { |
| 199 | + err := validateAWSKMS(test.kms) |
| 200 | + if test.expectError { |
| 201 | + if err == nil { |
| 202 | + t.Errorf("Expected error but got none") |
| 203 | + } else if test.errorContains != "" && !strings.Contains(err.Error(), test.errorContains) { |
| 204 | + t.Errorf("Expected error containing %q but got %q", test.errorContains, err.Error()) |
| 205 | + } |
| 206 | + } else if err != nil { |
| 207 | + t.Errorf("Expected no error but got: %v", err) |
| 208 | + } |
| 209 | + }) |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +func TestValidateKMS(t *testing.T) { |
| 214 | + tests := []struct { |
| 215 | + name string |
| 216 | + kms string |
| 217 | + expectError bool |
| 218 | + errorContains string |
| 219 | + }{ |
| 220 | + { |
| 221 | + name: "valid AWS KMS reference", |
| 222 | + kms: "awskms:///1234abcd-12ab-34cd-56ef-1234567890ab", |
| 223 | + expectError: false, |
| 224 | + }, |
| 225 | + { |
| 226 | + name: "valid Azure KMS reference", |
| 227 | + kms: "azurekms://", |
| 228 | + expectError: false, |
| 229 | + }, |
| 230 | + { |
| 231 | + name: "valid GCP KMS reference", |
| 232 | + kms: "gcpkms://", |
| 233 | + expectError: false, |
| 234 | + }, |
| 235 | + { |
| 236 | + name: "valid HashiVault KMS reference", |
| 237 | + kms: "hashivault://", |
| 238 | + expectError: false, |
| 239 | + }, |
| 240 | + { |
| 241 | + name: "unsupported KMS provider", |
| 242 | + kms: "unsupportedkms://keyid", |
| 243 | + expectError: true, |
| 244 | + errorContains: "malformed KMS format, should be prefixed by any of the supported providers", |
| 245 | + }, |
| 246 | + { |
| 247 | + name: "invalid AWS KMS reference", |
| 248 | + kms: "awskms://invalid", |
| 249 | + expectError: true, |
| 250 | + errorContains: "malformed AWS KMS format", |
| 251 | + }, |
| 252 | + } |
| 253 | + |
| 254 | + for _, test := range tests { |
| 255 | + t.Run(test.name, func(t *testing.T) { |
| 256 | + err := ValidateKMS(test.kms) |
| 257 | + if test.expectError { |
| 258 | + if err == nil { |
| 259 | + t.Errorf("Expected error but got none") |
| 260 | + } else if test.errorContains != "" && !strings.Contains(err.Error(), test.errorContains) { |
| 261 | + t.Errorf("Expected error containing %q but got %q", test.errorContains, err.Error()) |
| 262 | + } |
| 263 | + } else if err != nil { |
| 264 | + // For AWS KMS we do deeper validation which could fail |
| 265 | + if strings.HasPrefix(test.kms, "awskms://") { |
| 266 | + // Skip detailed AWS KMS validation errors as they're tested separately |
| 267 | + } else if err != nil { |
| 268 | + t.Errorf("Expected no error but got: %v", err) |
| 269 | + } |
| 270 | + } |
| 271 | + }) |
| 272 | + } |
| 273 | +} |
0 commit comments