Light, fluffy, and always free - The AWS Local Emulator alternative
Add KMS key state and description operations (#1251)
Jason van Zyl6e4087b0f4a9
diff --git a/docs/services/index.md b/docs/services/index.mdindex a418708b06..1892d4d0ae 100644--- a/docs/services/index.md+++ b/docs/services/index.md@@ -22,7 +22,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac| [IAM](iam.md) | `POST /` with `Action=` param | Query | 68 || [STS](sts.md) | `POST /` with `Action=` param | Query | 7 || [Cognito](cognito.md) | `POST /` + `X-Amz-Target: AWSCognitoIdentityProviderService.*` | JSON 1.1 | 43 |-| [KMS](kms.md) | `POST /` + `X-Amz-Target: TrentService.*` | JSON 1.1 | 23 |+| [KMS](kms.md) | `POST /` + `X-Amz-Target: TrentService.*` | JSON 1.1 | 34 || [Kinesis](kinesis.md) | `POST /` + `X-Amz-Target: Kinesis_20131202.*` | JSON 1.1 | 24 || [Secrets Manager](secrets-manager.md) | `POST /` + `X-Amz-Target: secretsmanager.*` | JSON 1.1 | 16 || [Step Functions](step-functions.md) | `POST /` + `X-Amz-Target: AmazonStatesService.*` | JSON 1.1 | 19 |diff --git a/docs/services/kms.md b/docs/services/kms.mdindex 53953a1cfe..bf9d8dbe0b 100644--- a/docs/services/kms.md+++ b/docs/services/kms.md@@ -8,6 +8,8 @@| Action | Description ||---|---|| `CreateKey` | Create a new KMS key |+| `GenerateRandom` | Generate random bytes |+| `GetPublicKey` | Get public key material for asymmetric keys || `DescribeKey` | Get key metadata || `ListKeys` | List all keys || `Encrypt` | Encrypt plaintext with a key |@@ -17,16 +19,20 @@| `GenerateDataKeyWithoutPlaintext` | Generate only the encrypted data key || `Sign` | Sign a message with an asymmetric key || `Verify` | Verify a signature |+| `GenerateMac` | Generate a MAC with an HMAC key |+| `VerifyMac` | Verify a MAC with an HMAC key || `CreateAlias` | Create a friendly name for a key || `DeleteAlias` | Remove an alias || `ListAliases` | List all aliases || `ScheduleKeyDeletion` | Mark a key for deletion || `CancelKeyDeletion` | Cancel pending deletion |+| `DisableKey` | Disable a key || `TagResource` | Tag a key || `UntagResource` | Remove tags || `ListResourceTags` | List tags || `GetKeyPolicy` | Get a key's resource policy || `PutKeyPolicy` | Update a key's resource policy |+| `UpdateKeyDescription` | Update a key's description || `GetKeyRotationStatus` | Check if automatic key rotation is enabled || `EnableKeyRotation` | Enable automatic key rotation (symmetric keys only) || `DisableKeyRotation` | Disable automatic key rotation |diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/KmsJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/kms/KmsJsonHandler.javaindex a1f83ffac4..d35601b21c 100644--- a/src/main/java/io/github/hectorvent/floci/services/kms/KmsJsonHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/kms/KmsJsonHandler.java@@ -65,9 +65,11 @@ public class KmsJsonHandler {case "ListResourceTags" -> handleListResourceTags(request, region);case "GetKeyPolicy" -> handleGetKeyPolicy(request, region);case "PutKeyPolicy" -> handlePutKeyPolicy(request, region);+ case "UpdateKeyDescription" -> handleUpdateKeyDescription(request, region);case "GetKeyRotationStatus" -> handleGetKeyRotationStatus(request, region);case "EnableKeyRotation" -> handleEnableKeyRotation(request, region);case "DisableKeyRotation" -> handleDisableKeyRotation(request, region);+ case "DisableKey" -> handleDisableKey(request, region);case "RotateKeyOnDemand" -> handleRotateKeyOnDemand(request, region);default -> Response.status(400).entity(new AwsErrorResponse("UnsupportedOperation", "Operation " + action + " is not supported."))@@ -475,6 +477,22 @@ public class KmsJsonHandler {return Response.ok(objectMapper.createObjectNode()).build();}+ private Response handleUpdateKeyDescription(JsonNode request, String region) {+ service.updateKeyDescription(+ request.path("KeyId").asText(),+ requiredText(request, "Description"),+ region);+ return Response.ok(objectMapper.createObjectNode()).build();+ }++ private String requiredText(JsonNode request, String field) {+ JsonNode value = request.path(field);+ if (value.isMissingNode() || value.isNull()) {+ throw new AwsException("ValidationException", field + " is required", 400);+ }+ return value.asText();+ }+private Response handleGetKeyRotationStatus(JsonNode request, String region) {String keyId = request.path("KeyId").asText();boolean enabled = service.getKeyRotationStatus(keyId, region);@@ -493,6 +511,11 @@ public class KmsJsonHandler {return Response.ok(objectMapper.createObjectNode()).build();}+ private Response handleDisableKey(JsonNode request, String region) {+ service.disableKey(request.path("KeyId").asText(), region);+ return Response.ok(objectMapper.createObjectNode()).build();+ }+private Response handleRotateKeyOnDemand(JsonNode request, String region) {String keyId = service.rotateKeyOnDemand(request.path("KeyId").asText(), region);ObjectNode response = objectMapper.createObjectNode();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.javaindex 6d8f969a04..2a5e9b8fb3 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@@ -489,6 +489,13 @@ public class KmsService {LOG.infov("Updated key policy for KMS key: {0} in {1}", key.getKeyId(), region);}+ public void updateKeyDescription(String keyId, String description, String region) {+ KmsKey key = resolveKey(keyId, region);+ key.setDescription(description);+ keyStore.put(region + "::" + key.getKeyId(), key);+ LOG.infov("Updated description for KMS key: {0} in {1}", key.getKeyId(), region);+ }+// ──────────────────────────── Key Rotation ────────────────────────────public boolean getKeyRotationStatus(String keyId, String region) {@@ -516,6 +523,14 @@ public class KmsService {LOG.infov("Disabled key rotation for KMS key: {0} in {1}", key.getKeyId(), region);}+ public void disableKey(String keyId, String region) {+ KmsKey key = resolveKey(keyId, region);+ key.setEnabled(false);+ key.setKeyState("Disabled");+ keyStore.put(region + "::" + key.getKeyId(), key);+ LOG.infov("Disabled 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);diff --git a/src/test/java/io/github/hectorvent/floci/services/kms/KmsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/kms/KmsIntegrationTest.javaindex 2e704d19c4..539b3f735f 100644--- a/src/test/java/io/github/hectorvent/floci/services/kms/KmsIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/kms/KmsIntegrationTest.java@@ -24,6 +24,52 @@ class KmsIntegrationTest {RestAssuredJsonUtils.configureAwsContentTypes();}+ @Test+ void updateKeyDescriptionRoundTripThroughJsonHandler() {+ String keyId = given()+ .header("X-Amz-Target", "TrentService.CreateKey")+ .contentType(KMS_CONTENT_TYPE)+ .body("""+ {+ "Description": "old description"+ }+ """)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("KeyMetadata.KeyId", notNullValue())+ .extract().jsonPath().getString("KeyMetadata.KeyId");++ given()+ .header("X-Amz-Target", "TrentService.UpdateKeyDescription")+ .contentType(KMS_CONTENT_TYPE)+ .body("""+ {+ "KeyId": "%s",+ "Description": "new description"+ }+ """.formatted(keyId))+ .when()+ .post("/")+ .then()+ .statusCode(200);++ given()+ .header("X-Amz-Target", "TrentService.DescribeKey")+ .contentType(KMS_CONTENT_TYPE)+ .body("""+ {+ "KeyId": "%s"+ }+ """.formatted(keyId))+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("KeyMetadata.Description", equalTo("new description"));+ }+@Testvoid generateMacAndVerifyMacRoundTripThroughJsonHandler() {String keyId = given()@@ -297,6 +343,70 @@ class KmsIntegrationTest {.body("KeyId", equalTo(keyId));}+ @Test+ void disableKeyUpdatesDescribeKeyState() {+ String keyId = given()+ .header("X-Amz-Target", "TrentService.CreateKey")+ .contentType(KMS_CONTENT_TYPE)+ .body("{\"Description\":\"disable-key\"}")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract()+ .path("KeyMetadata.KeyId");++ given()+ .header("X-Amz-Target", "TrentService.DisableKey")+ .contentType(KMS_CONTENT_TYPE)+ .body("""+ {"KeyId":"%s"}+ """.formatted(keyId))+ .when()+ .post("/")+ .then()+ .statusCode(200);++ given()+ .header("X-Amz-Target", "TrentService.DescribeKey")+ .contentType(KMS_CONTENT_TYPE)+ .body("""+ {"KeyId":"%s"}+ """.formatted(keyId))+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("KeyMetadata.Enabled", equalTo(false))+ .body("KeyMetadata.KeyState", equalTo("Disabled"));+ }++ @Test+ void updateKeyDescriptionRequiresDescription() {+ String keyId = given()+ .header("X-Amz-Target", "TrentService.CreateKey")+ .contentType(KMS_CONTENT_TYPE)+ .body("{\"Description\":\"missing-description-update\"}")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract()+ .path("KeyMetadata.KeyId");++ given()+ .header("X-Amz-Target", "TrentService.UpdateKeyDescription")+ .contentType(KMS_CONTENT_TYPE)+ .body("""+ {"KeyId":"%s"}+ """.formatted(keyId))+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("ValidationException"));+ }+@Testvoid listGrantsReturnsEmptyGrantListThroughJsonHandler() {String keyId = given()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.javaindex 1a87415854..a6c4936819 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@@ -61,6 +61,16 @@ class KmsServiceTest {assertEquals("Enabled", key.getKeyState());}+ @Test+ void updateKeyDescriptionPersistsDescription() {+ KmsKey key = kmsService.createKey("old description", REGION);++ kmsService.updateKeyDescription(key.getKeyId(), "new description", REGION);++ KmsKey updated = kmsService.describeKey(key.getKeyId(), REGION);+ assertEquals("new description", updated.getDescription());+ }+@Testvoid listKeys() {kmsService.createKey("key1", REGION);