mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(kms): reject CreateAlias on an existing alias name (#8171)
* fix(kms): reject CreateAlias on an existing alias name with AlreadyExistsException * test(kms): cover alias update in CloudFormation and tighten the race test
This commit is contained in:
1 parent
b2fa3e0cfb
commit
a840c31109
5 files changed
+123
-2
No files matched your search
+5
-1
@@ -54,7 +54,11 @@ public class KmsCfnProvisioner implements CfnResourceProvisioner {
|
||||
String aliasName = ctx.resolveOptional(props, "AliasName");
|
||||
String targetKeyId = ctx.resolveOptional(props, "TargetKeyId");
|
||||
if (aliasName != null && targetKeyId != null) {
|
||||
kmsService.createAlias(aliasName, targetKeyId, ctx.region());
|
||||
if (ctx.isUpdate() && aliasName.equals(ctx.priorPhysicalId())) {
|
||||
kmsService.updateAlias(aliasName, targetKeyId, ctx.region());
|
||||
} else {
|
||||
kmsService.createAlias(aliasName, targetKeyId, ctx.region());
|
||||
}
|
||||
}
|
||||
r.setPhysicalId(aliasName != null
|
||||
? aliasName
|
||||
|
||||
@@ -71,6 +71,7 @@ public class KmsService implements ResourceProvider {
|
||||
// Guards the scan-and-mint sequence in ensureAwsManagedKeys so two concurrent first reads of
|
||||
// a region's alias namespace cannot each mint a key for the same alias/aws/* name.
|
||||
private final Object awsManagedKeyLock = new Object();
|
||||
private final Object aliasCreateLock = new Object();
|
||||
|
||||
@Inject
|
||||
public KmsService(StorageFactory storageFactory, RegionResolver regionResolver) {
|
||||
@@ -1421,7 +1422,14 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
String aliasArn = regionResolver.buildArn("kms", region, aliasName);
|
||||
KmsAlias alias = new KmsAlias(aliasName, aliasArn, key.getKeyId());
|
||||
aliasStore.put(region + "::" + aliasName, alias);
|
||||
String storageKey = region + "::" + aliasName;
|
||||
synchronized (aliasCreateLock) {
|
||||
if (aliasStore.get(storageKey).isPresent()) {
|
||||
throw new AwsException("AlreadyExistsException",
|
||||
"An alias with the name " + aliasArn + " already exists", 400);
|
||||
}
|
||||
aliasStore.put(storageKey, alias);
|
||||
}
|
||||
LOG.infov("Created KMS alias: {0} -> {1}", aliasName, key.getKeyId());
|
||||
}
|
||||
|
||||
|
||||
+29
@@ -178,6 +178,35 @@ class LeafCfnProvisionerTest {
|
||||
assertEquals("alias/app", r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updatingAnAliasRetargetsItInPlaceInsteadOfCreatingItAgain() {
|
||||
StackResource r = resource("Alias", "AWS::KMS::Alias");
|
||||
ProvisionContext update = new ProvisionContext(engine, REGION, "000000000000", "my-stack",
|
||||
"alias/app");
|
||||
|
||||
provisioner.provision(r, props("""
|
||||
{"AliasName": "alias/app", "TargetKeyId": "k-456"}
|
||||
"""), update);
|
||||
|
||||
verify(kms).updateAlias("alias/app", "k-456", REGION);
|
||||
verify(kms, never()).createAlias(anyString(), anyString(), anyString());
|
||||
assertEquals("alias/app", r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updatingAnAliasToANewNameCreatesTheNewAlias() {
|
||||
StackResource r = resource("Alias", "AWS::KMS::Alias");
|
||||
ProvisionContext update = new ProvisionContext(engine, REGION, "000000000000", "my-stack",
|
||||
"alias/old");
|
||||
|
||||
provisioner.provision(r, props("""
|
||||
{"AliasName": "alias/new", "TargetKeyId": "k-456"}
|
||||
"""), update);
|
||||
|
||||
verify(kms).createAlias("alias/new", "k-456", REGION);
|
||||
verify(kms, never()).updateAlias(anyString(), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aliasWithoutATargetIsNotCreatedButStillGetsAPhysicalId() {
|
||||
StackResource r = resource("Alias", "AWS::KMS::Alias");
|
||||
|
||||
@@ -37,6 +37,7 @@ import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.emptyString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
@@ -2580,6 +2581,24 @@ class KmsIntegrationTest {
|
||||
assertEquals(pair.getString("PrivateKeyPlaintext"), plaintext);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAliasOnExistingNameReturnsAlreadyExistsAndKeepsTheFirstTarget() {
|
||||
String first = createKeyArn("SYMMETRIC_DEFAULT", "ENCRYPT_DECRYPT");
|
||||
String second = createKeyArn("SYMMETRIC_DEFAULT", "ENCRYPT_DECRYPT");
|
||||
String alias = "alias/dup-" + UUID.randomUUID();
|
||||
callKms("CreateAlias", "{\"AliasName\":\"%s\",\"TargetKeyId\":\"%s\"}".formatted(alias, first))
|
||||
.then().statusCode(200);
|
||||
|
||||
callKms("CreateAlias", "{\"AliasName\":\"%s\",\"TargetKeyId\":\"%s\"}".formatted(alias, second))
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("AlreadyExistsException"))
|
||||
.body("message", containsString(":alias/dup-"));
|
||||
|
||||
callKms("DescribeKey", "{\"KeyId\":\"%s\"}".formatted(alias))
|
||||
.then().statusCode(200)
|
||||
.body("KeyMetadata.Arn", equalTo(first));
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateDataKeyPairWithoutPlaintextReturnsAPublicKeyThatMatchesTheEncryptedPrivateKey() throws Exception {
|
||||
String keyArn = createKeyArn("SYMMETRIC_DEFAULT", "ENCRYPT_DECRYPT");
|
||||
|
||||
@@ -985,6 +985,67 @@ class KmsServiceTest {
|
||||
assertEquals(key.getKeyId(), aliases.getFirst().getTargetKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAliasOnExistingNameThrowsAndKeepsTarget() {
|
||||
KmsKey keyA = kmsService.createKey(null, REGION);
|
||||
KmsKey keyB = kmsService.createKey(null, REGION);
|
||||
kmsService.createAlias("alias/dup", keyA.getKeyId(), REGION);
|
||||
|
||||
AwsException sameTarget = assertThrows(AwsException.class, () ->
|
||||
kmsService.createAlias("alias/dup", keyA.getKeyId(), REGION));
|
||||
AwsException otherTarget = assertThrows(AwsException.class, () ->
|
||||
kmsService.createAlias("alias/dup", keyB.getKeyId(), REGION));
|
||||
|
||||
assertEquals("AlreadyExistsException", sameTarget.getErrorCode());
|
||||
assertEquals("AlreadyExistsException", otherTarget.getErrorCode());
|
||||
assertEquals(400, otherTarget.getHttpStatus());
|
||||
assertEquals(keyA.getKeyId(), customerAliases(REGION).getFirst().getTargetKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAliasWithSameNameInAnotherRegionSucceeds() {
|
||||
KmsKey keyA = kmsService.createKey(null, REGION);
|
||||
KmsKey keyB = kmsService.createKey(null, "eu-west-1");
|
||||
kmsService.createAlias("alias/dup", keyA.getKeyId(), REGION);
|
||||
|
||||
kmsService.createAlias("alias/dup", keyB.getKeyId(), "eu-west-1");
|
||||
|
||||
assertEquals(1, customerAliases("eu-west-1").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void concurrentCreateAliasOnSameNameAdmitsExactlyOne() throws Exception {
|
||||
KmsKey keyA = kmsService.createKey(null, REGION);
|
||||
KmsKey keyB = kmsService.createKey(null, REGION);
|
||||
ExecutorService pool = Executors.newFixedThreadPool(2);
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
try {
|
||||
List<Future<Boolean>> results = new ArrayList<>();
|
||||
for (KmsKey key : List.of(keyA, keyB)) {
|
||||
results.add(pool.submit(() -> {
|
||||
start.await();
|
||||
try {
|
||||
kmsService.createAlias("alias/race", key.getKeyId(), REGION);
|
||||
return true;
|
||||
} catch (AwsException e) {
|
||||
assertEquals("AlreadyExistsException", e.getErrorCode());
|
||||
return false;
|
||||
}
|
||||
}));
|
||||
}
|
||||
start.countDown();
|
||||
int created = 0;
|
||||
for (Future<Boolean> result : results) {
|
||||
if (result.get(10, TimeUnit.SECONDS)) {
|
||||
created++;
|
||||
}
|
||||
}
|
||||
assertEquals(1, created);
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAliasWithoutPrefixThrows() {
|
||||
KmsKey key = kmsService.createKey(null, REGION);
|
||||
|
||||
Reference in new issue
Block a user