feat(kms): add GetKeyRotationStatus, EnableKeyRotation, and DisableKeyRotation (#290) (#322)

Add key rotation management with persisted state on the KmsKey model.
Rotation is only supported for symmetric ENCRYPT_DECRYPT keys, matching
AWS behavior. Includes validation that rejects asymmetric keys with
UnsupportedOperationException.
This commit is contained in:
TiM authored and GitHub committed 2026-04-09 23:51:45 -05:00
1 parent 473fece1c5
commit 1fa7ae7551
5 files changed
+103

No files matched your search

@@ -50,6 +50,9 @@ public class KmsJsonHandler {
case "ListResourceTags" -> handleListResourceTags(request, region);
case "GetKeyPolicy" -> handleGetKeyPolicy(request, region);
case "PutKeyPolicy" -> handlePutKeyPolicy(request, region);
case "GetKeyRotationStatus" -> handleGetKeyRotationStatus(request, region);
case "EnableKeyRotation" -> handleEnableKeyRotation(request, region);
case "DisableKeyRotation" -> handleDisableKeyRotation(request, region);
default -> Response.status(400)
.entity(new AwsErrorResponse("UnsupportedOperation", "Operation " + action + " is not supported."))
.build();
@@ -272,6 +275,24 @@ public class KmsJsonHandler {
return Response.ok(objectMapper.createObjectNode()).build();
}
private Response handleGetKeyRotationStatus(JsonNode request, String region) {
String keyId = request.path("KeyId").asText();
boolean enabled = service.getKeyRotationStatus(keyId, region);
ObjectNode response = objectMapper.createObjectNode();
response.put("KeyRotationEnabled", enabled);
return Response.ok(response).build();
}
private Response handleEnableKeyRotation(JsonNode request, String region) {
service.enableKeyRotation(request.path("KeyId").asText(), region);
return Response.ok(objectMapper.createObjectNode()).build();
}
private Response handleDisableKeyRotation(JsonNode request, String region) {
service.disableKeyRotation(request.path("KeyId").asText(), region);
return Response.ok(objectMapper.createObjectNode()).build();
}
private ObjectNode keyToNode(KmsKey k) {
ObjectNode node = objectMapper.createObjectNode();
node.put("AWSAccountId", "000000000000");
@@ -107,6 +107,40 @@ public class KmsService {
LOG.infov("Updated key policy for KMS key: {0} in {1}", key.getKeyId(), region);
}
// ──────────────────────────── Key Rotation ────────────────────────────
public boolean getKeyRotationStatus(String keyId, String region) {
KmsKey key = resolveKey(keyId, region);
validateRotationSupported(key);
return key.isKeyRotationEnabled();
}
public void enableKeyRotation(String keyId, String region) {
KmsKey key = resolveKey(keyId, region);
validateRotationSupported(key);
key.setKeyRotationEnabled(true);
keyStore.put(region + "::" + key.getKeyId(), key);
LOG.infov("Enabled key rotation for KMS key: {0} in {1}", key.getKeyId(), region);
}
public void disableKeyRotation(String keyId, String region) {
KmsKey key = resolveKey(keyId, region);
validateRotationSupported(key);
key.setKeyRotationEnabled(false);
keyStore.put(region + "::" + key.getKeyId(), key);
LOG.infov("Disabled key rotation for KMS key: {0} in {1}", key.getKeyId(), region);
}
private void validateRotationSupported(KmsKey key) {
if (!"ENCRYPT_DECRYPT".equals(key.getKeyUsage())
|| !"SYMMETRIC_DEFAULT".equals(key.getCustomerMasterKeySpec())) {
throw new AwsException(
"UnsupportedOperationException",
"You cannot perform this operation on a non-symmetric key or a key with non-ENCRYPT_DECRYPT key usage.",
400);
}
}
// ──────────────────────────── Aliases ────────────────────────────
public void createAlias(String aliasName, String targetKeyId, String region) {
@@ -20,6 +20,7 @@ public class KmsKey {
private long creationDate;
private long deletionDate;
private String policy;
private boolean keyRotationEnabled = false;
private Map<String, String> tags = new HashMap<>();
public KmsKey() {
@@ -56,6 +57,9 @@ public class KmsKey {
public String getPolicy() { return policy; }
public void setPolicy(String policy) { this.policy = policy; }
public boolean isKeyRotationEnabled() { return keyRotationEnabled; }
public void setKeyRotationEnabled(boolean keyRotationEnabled) { this.keyRotationEnabled = keyRotationEnabled; }
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags; }
}