mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -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
@@ -58,6 +58,7 @@ public class KmsJsonHandler {
|
||||
case "GenerateMac" -> handleGenerateMac(request, region);
|
||||
case "VerifyMac" -> handleVerifyMac(request, region);
|
||||
case "CreateAlias" -> handleCreateAlias(request, region);
|
||||
case "UpdateAlias" -> handleUpdateAlias(request, region);
|
||||
case "DeleteAlias" -> handleDeleteAlias(request, region);
|
||||
case "ListAliases" -> handleListAliases(request, region);
|
||||
case "ScheduleKeyDeletion" -> handleScheduleKeyDeletion(request, region);
|
||||
@@ -393,6 +394,11 @@ public class KmsJsonHandler {
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleUpdateAlias(JsonNode request, String region) {
|
||||
service.updateAlias(request.path("AliasName").asText(), request.path("TargetKeyId").asText(), region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleDeleteAlias(JsonNode request, String region) {
|
||||
service.deleteAlias(request.path("AliasName").asText(), region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
|
||||
@@ -227,6 +227,13 @@ public class KmsService {
|
||||
return spec != null && spec.getKeyType() == KmsKeySpec.KeyType.HMAC;
|
||||
}
|
||||
|
||||
// AWS UpdateAlias only requires the current and new key to be "the same type (both
|
||||
// symmetric or both asymmetric or both HMAC)" - not an exact KeySpec match, so e.g.
|
||||
// RSA_2048 and ECC_NIST_P256 are compatible, but SYMMETRIC_DEFAULT and RSA_2048 are not.
|
||||
private static boolean sameKeyFamily(KmsKeySpec a, KmsKeySpec b) {
|
||||
return isHmac(a) == isHmac(b) && (a == KmsKeySpec.SYMMETRIC_DEFAULT) == (b == KmsKeySpec.SYMMETRIC_DEFAULT);
|
||||
}
|
||||
|
||||
private static void validateKeyUsageForSpec(KmsKeyUsage keyUsage, KmsKeySpec spec) {
|
||||
if (isHmac(spec) && KmsKeyUsage.GENERATE_VERIFY_MAC != keyUsage) {
|
||||
throw new AwsException("ValidationException",
|
||||
@@ -605,6 +612,30 @@ public class KmsService {
|
||||
LOG.infov("Created KMS alias: {0} -> {1}", aliasName, key.getKeyId());
|
||||
}
|
||||
|
||||
public void updateAlias(String aliasName, String targetKeyId, String region) {
|
||||
String storageKey = region + "::" + aliasName;
|
||||
KmsAlias existing = aliasStore.get(storageKey)
|
||||
.orElseThrow(() -> new AwsException("NotFoundException", "Alias not found: " + aliasName, 404));
|
||||
|
||||
KmsKey currentKey = resolveKey(existing.getTargetKeyId(), region);
|
||||
KmsKey newKey = resolveKey(targetKeyId, region); // Validate key exists and normalize to plain key ID
|
||||
|
||||
if ("PendingDeletion".equals(newKey.getKeyState())) {
|
||||
throw new AwsException("KMSInvalidStateException",
|
||||
"KMS key " + newKey.getKeyId() + " is pending deletion.", 400);
|
||||
}
|
||||
if (currentKey.getKeyUsage() != newKey.getKeyUsage() || !sameKeyFamily(currentKey.getKeySpec(), newKey.getKeySpec())) {
|
||||
throw new AwsException("ValidationException",
|
||||
"The replacement KMS key must have the same key usage and key type "
|
||||
+ "(symmetric, asymmetric, or HMAC) as the alias's current target key.",
|
||||
400);
|
||||
}
|
||||
|
||||
existing.setTargetKeyId(newKey.getKeyId());
|
||||
aliasStore.put(storageKey, existing);
|
||||
LOG.infov("Updated KMS alias: {0} -> {1}", aliasName, newKey.getKeyId());
|
||||
}
|
||||
|
||||
public void deleteAlias(String aliasName, String region) {
|
||||
String key = region + "::" + aliasName;
|
||||
if (aliasStore.get(key).isEmpty()) {
|
||||
|
||||
Reference in new issue
Block a user