mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(kms): implement UpdateAlias (#2324)
* feat(kms): implement UpdateAlias Repoints an existing alias at a different KMS key, following the existing createAlias/deleteAlias/resolveKey patterns. Required by the Terraform AWS provider, which treats a target_key_id change on aws_kms_alias as an in-place update. * fix(kms): reject invalid replacement keys in UpdateAlias UpdateAlias accepted any existing key as a replacement target, including keys pending deletion or with a different key usage/spec than the alias's current target. Real AWS KMS rejects both: the replacement key must be in a compatible state and match the current target's key usage and key spec. * fix(kms): compare key type instead of exact key spec in UpdateAlias The exact KeySpec comparison rejected valid updates between same-type keys with different specs (e.g. RSA_2048 to RSA_3072), which AWS KMS accepts as long as both keys share the same key type and usage. * test(kms): add Terraform compatibility test for UpdateAlias Verifies the exact scenario from #2291: the Terraform AWS provider treats target_key_id on aws_kms_alias as an in-place update, so re-applying with a different target must call UpdateAlias and update the alias without a destroy/recreate. Self-contained config, mirroring the eni-destroy-tf pattern, since it needs a second apply with a different var rather than the shared single-apply main.tf. * fix(kms): compare key family instead of KeyType in UpdateAlias Comparing the fine-grained KeyType enum rejected valid cross-family asymmetric updates (e.g. RSA_2048 to ECC_NIST_P256) that AWS KMS accepts. AWS only requires the current and new key to be "the same type (both symmetric or both asymmetric or both HMAC)" - a coarser three-way split - so compare on that instead.
This commit is contained in:
1 parent
b45de1bb7d
commit
984886f697
8 files changed
+460
No files matched your search
@@ -1183,4 +1183,184 @@ class KmsIntegrationTest {
|
||||
.then()
|
||||
.body("__type", equalTo("NotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAliasRoundTripThroughJsonHandler() {
|
||||
String keyId1 = given()
|
||||
.header("X-Amz-Target", "TrentService.CreateKey")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("KeyMetadata.KeyId");
|
||||
|
||||
String keyId2 = given()
|
||||
.header("X-Amz-Target", "TrentService.CreateKey")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("KeyMetadata.KeyId");
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.CreateAlias")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"AliasName\":\"alias/update-alias-test\",\"TargetKeyId\":\"" + keyId1 + "\"}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.UpdateAlias")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"AliasName\":\"alias/update-alias-test\",\"TargetKeyId\":\"" + keyId2 + "\"}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.ListAliases")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"KeyId\":\"" + keyId2 + "\"}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Aliases.find { it.AliasName == 'alias/update-alias-test' }.TargetKeyId", equalTo(keyId2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAliasReturnsNotFoundForUnknownAlias() {
|
||||
String keyId = given()
|
||||
.header("X-Amz-Target", "TrentService.CreateKey")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("KeyMetadata.KeyId");
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.UpdateAlias")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"AliasName\":\"alias/non-existent\",\"TargetKeyId\":\"" + keyId + "\"}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body("__type", equalTo("NotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAliasReturnsNotFoundForUnknownTargetKey() {
|
||||
String keyId = given()
|
||||
.header("X-Amz-Target", "TrentService.CreateKey")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("KeyMetadata.KeyId");
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.CreateAlias")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"AliasName\":\"alias/update-alias-missing-target\",\"TargetKeyId\":\"" + keyId + "\"}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.UpdateAlias")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"AliasName\":\"alias/update-alias-missing-target\",\"TargetKeyId\":\"non-existent-key\"}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body("__type", equalTo("NotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAliasReturnsInvalidStateForPendingDeletionTarget() {
|
||||
String keyId1 = given()
|
||||
.header("X-Amz-Target", "TrentService.CreateKey")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("KeyMetadata.KeyId");
|
||||
|
||||
String keyId2 = given()
|
||||
.header("X-Amz-Target", "TrentService.CreateKey")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("KeyMetadata.KeyId");
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.ScheduleKeyDeletion")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"KeyId\":\"" + keyId2 + "\",\"PendingWindowInDays\":7}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.CreateAlias")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"AliasName\":\"alias/update-alias-pending-deletion\",\"TargetKeyId\":\"" + keyId1 + "\"}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.UpdateAlias")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"AliasName\":\"alias/update-alias-pending-deletion\",\"TargetKeyId\":\"" + keyId2 + "\"}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("KMSInvalidStateException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAliasReturnsValidationForIncompatibleKeyUsage() {
|
||||
String symmetricKeyId = given()
|
||||
.header("X-Amz-Target", "TrentService.CreateKey")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("KeyMetadata.KeyId");
|
||||
|
||||
String hmacKeyId = given()
|
||||
.header("X-Amz-Target", "TrentService.CreateKey")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"KeyUsage\":\"GENERATE_VERIFY_MAC\",\"KeySpec\":\"HMAC_256\"}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("KeyMetadata.KeyId");
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.CreateAlias")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"AliasName\":\"alias/update-alias-incompatible-usage\",\"TargetKeyId\":\"" + symmetricKeyId + "\"}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "TrentService.UpdateAlias")
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body("{\"AliasName\":\"alias/update-alias-incompatible-usage\",\"TargetKeyId\":\"" + hmacKeyId + "\"}")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"));
|
||||
}
|
||||
}
|
||||
@@ -501,6 +501,102 @@ class KmsServiceTest {
|
||||
kmsService.createAlias("alias/test", "no-such-key", REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAlias() {
|
||||
KmsKey keyA = kmsService.createKey(null, REGION);
|
||||
KmsKey keyB = kmsService.createKey(null, REGION);
|
||||
kmsService.createAlias("alias/my-key", keyA.getKeyId(), REGION);
|
||||
|
||||
kmsService.updateAlias("alias/my-key", keyB.getKeyId(), REGION);
|
||||
|
||||
List<KmsAlias> aliases = kmsService.listAliases(REGION);
|
||||
assertEquals(1, aliases.size());
|
||||
assertEquals(keyB.getKeyId(), aliases.getFirst().getTargetKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAliasNotFoundThrows() {
|
||||
KmsKey key = kmsService.createKey(null, REGION);
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
kmsService.updateAlias("alias/missing", key.getKeyId(), REGION));
|
||||
|
||||
assertEquals("NotFoundException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAliasForNonExistentTargetKeyThrows() {
|
||||
KmsKey key = kmsService.createKey(null, REGION);
|
||||
kmsService.createAlias("alias/my-key", key.getKeyId(), REGION);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
kmsService.updateAlias("alias/my-key", "no-such-key", REGION));
|
||||
|
||||
assertEquals("NotFoundException", ex.getErrorCode());
|
||||
assertEquals(key.getKeyId(), kmsService.listAliases(REGION).getFirst().getTargetKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAliasForPendingDeletionTargetThrows() {
|
||||
KmsKey keyA = kmsService.createKey(null, REGION);
|
||||
KmsKey keyB = kmsService.createKey(null, REGION);
|
||||
kmsService.createAlias("alias/my-key", keyA.getKeyId(), REGION);
|
||||
kmsService.scheduleKeyDeletion(keyB.getKeyId(), 7, REGION);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
kmsService.updateAlias("alias/my-key", keyB.getKeyId(), REGION));
|
||||
|
||||
assertEquals("KMSInvalidStateException", ex.getErrorCode());
|
||||
assertEquals(keyA.getKeyId(), kmsService.listAliases(REGION).getFirst().getTargetKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAliasForIncompatibleKeyUsageThrows() {
|
||||
KmsKey symmetricKey = kmsService.createKey(null, REGION);
|
||||
KmsKey hmacKey = kmsService.createKey("hmac key", "GENERATE_VERIFY_MAC", "HMAC_256", null, Map.of(), REGION);
|
||||
kmsService.createAlias("alias/my-key", symmetricKey.getKeyId(), REGION);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
kmsService.updateAlias("alias/my-key", hmacKey.getKeyId(), REGION));
|
||||
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals(symmetricKey.getKeyId(), kmsService.listAliases(REGION).getFirst().getTargetKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAliasAllowsSameTypeDifferentSpec() {
|
||||
KmsKey rsa2048 = kmsService.createKey("rsa 2048", "SIGN_VERIFY", "RSA_2048", null, Map.of(), REGION);
|
||||
KmsKey rsa3072 = kmsService.createKey("rsa 3072", "SIGN_VERIFY", "RSA_3072", null, Map.of(), REGION);
|
||||
kmsService.createAlias("alias/my-key", rsa2048.getKeyId(), REGION);
|
||||
|
||||
kmsService.updateAlias("alias/my-key", rsa3072.getKeyId(), REGION);
|
||||
|
||||
assertEquals(rsa3072.getKeyId(), kmsService.listAliases(REGION).getFirst().getTargetKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAliasAllowsSameUsageDifferentAsymmetricFamily() {
|
||||
KmsKey rsaKey = kmsService.createKey("rsa key", "SIGN_VERIFY", "RSA_2048", null, Map.of(), REGION);
|
||||
KmsKey eccKey = kmsService.createKey("ecc key", "SIGN_VERIFY", "ECC_NIST_P256", null, Map.of(), REGION);
|
||||
kmsService.createAlias("alias/my-key", rsaKey.getKeyId(), REGION);
|
||||
|
||||
kmsService.updateAlias("alias/my-key", eccKey.getKeyId(), REGION);
|
||||
|
||||
assertEquals(eccKey.getKeyId(), kmsService.listAliases(REGION).getFirst().getTargetKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAliasRejectsSymmetricToAsymmetricEvenWithSameUsage() {
|
||||
KmsKey symmetricKey = kmsService.createKey(null, REGION);
|
||||
KmsKey rsaEncryptKey = kmsService.createKey("rsa encrypt key", "ENCRYPT_DECRYPT", "RSA_2048", null, Map.of(), REGION);
|
||||
kmsService.createAlias("alias/my-key", symmetricKey.getKeyId(), REGION);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
kmsService.updateAlias("alias/my-key", rsaEncryptKey.getKeyId(), REGION));
|
||||
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertEquals(symmetricKey.getKeyId(), kmsService.listAliases(REGION).getFirst().getTargetKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteAlias() {
|
||||
KmsKey key = kmsService.createKey(null, REGION);
|
||||
|
||||
Reference in new issue
Block a user