diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/KmsService.java b/src/main/java/io/github/hectorvent/floci/services/kms/KmsService.java index 4fd0fdf817..cfd00c5d81 100644 --- a/src/main/java/io/github/hectorvent/floci/services/kms/KmsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/kms/KmsService.java @@ -495,6 +495,7 @@ public class KmsService { LOG.infov("Disabled key rotation for KMS key: {0} in {1}", key.getKeyId(), region); } + private static final int ON_DEMAND_ROTATION_LIMIT = 25; public String rotateKeyOnDemand(String keyId, String region) { KmsKey key = resolveKey(keyId, region); if (!key.isEnabled()) { @@ -502,6 +503,12 @@ public class KmsService { "KMS key " + key.getKeyId() + " is disabled.", 400); } validateRotationSupported(key); + if (key.getOnDemandRotationCount() >= ON_DEMAND_ROTATION_LIMIT) { + throw new AwsException("LimitExceededException", + "On-demand rotation quota for KMS key " + key.getKeyId() + " is exceeded.", 400); + } + key.setOnDemandRotationCount(key.getOnDemandRotationCount() + 1); + keyStore.put(region + "::" + key.getKeyId(), key); return key.getKeyId(); } diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/model/KmsKey.java b/src/main/java/io/github/hectorvent/floci/services/kms/model/KmsKey.java index 8d58475cc1..2aa53bc156 100644 --- a/src/main/java/io/github/hectorvent/floci/services/kms/model/KmsKey.java +++ b/src/main/java/io/github/hectorvent/floci/services/kms/model/KmsKey.java @@ -24,6 +24,7 @@ public class KmsKey { private Map tags = new HashMap<>(); private String privateKeyEncoded; private String publicKeyEncoded; + private int onDemandRotationCount; public KmsKey() { this.creationDate = Instant.now().getEpochSecond(); @@ -70,4 +71,7 @@ public class KmsKey { public String getPublicKeyEncoded() { return publicKeyEncoded; } public void setPublicKeyEncoded(String publicKeyEncoded) { this.publicKeyEncoded = publicKeyEncoded; } + + public int getOnDemandRotationCount() { return onDemandRotationCount; } + public void setOnDemandRotationCount(int onDemandRotationCount) { this.onDemandRotationCount = onDemandRotationCount; } } diff --git a/src/test/java/io/github/hectorvent/floci/services/kms/KmsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/kms/KmsServiceTest.java index 9c530eb05c..1a87415854 100644 --- a/src/test/java/io/github/hectorvent/floci/services/kms/KmsServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/kms/KmsServiceTest.java @@ -1027,6 +1027,22 @@ class KmsServiceTest { assertEquals(400, ex.getHttpStatus()); } + @Test + void rotateKeyOnDemandLimitExceededThrows() { + KmsKey key = kmsService.createKey(null, REGION); + + for (int i = 0; i < 25; i++) { + kmsService.rotateKeyOnDemand(key.getKeyId(), REGION); + } + + AwsException ex = assertThrows(AwsException.class, () -> + kmsService.rotateKeyOnDemand(key.getKeyId(), REGION)); + + assertEquals("LimitExceededException", ex.getErrorCode()); + assertEquals(400, ex.getHttpStatus()); + assertEquals(25, kmsService.describeKey(key.getKeyId(), REGION).getOnDemandRotationCount()); + } + // ── Issue #497 — HMAC key specs ───────────────────────────────────────── @ParameterizedTest