Light, fluffy, and always free - The AWS Local Emulator alternative
fix(bedrock): resolve the KMS key before updating a guardrail (#5710)
Ridwanac838178022e
diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrock/BedrockService.java b/src/main/java/io/github/hectorvent/floci/services/bedrock/BedrockService.javaindex 024ebbddc4..03933f7125 100644--- a/src/main/java/io/github/hectorvent/floci/services/bedrock/BedrockService.java+++ b/src/main/java/io/github/hectorvent/floci/services/bedrock/BedrockService.java@@ -160,11 +160,13 @@ public class BedrockService {String blockedOutputsMessaging = requiredBlockedMessaging(request, "blockedOutputsMessaging");Guardrail guardrail = getGuardrail(identifier, DRAFT_VERSION, region);+ // getGuardrail returns the stored draft, so the key must resolve before any setter runs.+ String kmsKeyArn = resolveKmsKeyArn(textOrNull(request, "kmsKeyId"), region);guardrail.setName(name);guardrail.setDescription(description);guardrail.setBlockedInputMessaging(blockedInputMessaging);guardrail.setBlockedOutputsMessaging(blockedOutputsMessaging);- guardrail.setKmsKeyArn(resolveKmsKeyArn(textOrNull(request, "kmsKeyId"), region));+ guardrail.setKmsKeyArn(kmsKeyArn);guardrail.setUpdatedAt(Instant.now());applyPolicies(guardrail, request, region);diff --git a/src/test/java/io/github/hectorvent/floci/services/bedrock/BedrockIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/bedrock/BedrockIntegrationTest.javaindex 2acba68d8e..0b21283744 100644--- a/src/test/java/io/github/hectorvent/floci/services/bedrock/BedrockIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/bedrock/BedrockIntegrationTest.java@@ -541,6 +541,46 @@ class BedrockIntegrationTest {.body("resourceName", equalTo(arn));}+ @Test+ @Order(23)+ void updateRejectingTheKmsKeyLeavesTheGuardrailUnchanged() {+ String id = given()+ .contentType("application/json")+ .body("{\"name\": \"kms-update-rejected\","+ + " \"blockedInputMessaging\": \"in\","+ + " \"blockedOutputsMessaging\": \"out\"}")+ .when()+ .post("/guardrails")+ .then()+ .statusCode(202)+ .extract().path("guardrailId");++ given()+ .contentType("application/json")+ .body("""+ {+ "name": "kms-update-renamed",+ "blockedInputMessaging": "in v2",+ "blockedOutputsMessaging": "out v2",+ "kmsKeyId": "alias/does-not-exist"+ }+ """)+ .when()+ .put("/guardrails/" + id)+ .then()+ .statusCode(400)+ .body("__type", equalTo("ValidationException"));++ given()+ .when()+ .get("/guardrails/" + id)+ .then()+ .statusCode(200)+ .body("name", equalTo("kms-update-rejected"))+ .body("blockedInputMessaging", equalTo("in"))+ .body("blockedOutputsMessaging", equalTo("out"));+ }+private static String tagListJson(String prefix, int count) {StringBuilder json = new StringBuilder("[");for (int i = 0; i < count; i++) {diff --git a/src/test/java/io/github/hectorvent/floci/services/bedrock/BedrockServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/bedrock/BedrockServiceTest.javaindex b73327110e..58126d33b5 100644--- a/src/test/java/io/github/hectorvent/floci/services/bedrock/BedrockServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/bedrock/BedrockServiceTest.java@@ -532,6 +532,28 @@ class BedrockServiceTest {service.createGuardrailVersion(created.getGuardrailId(), null, REGION).getKmsKeyArn());}+ @Test+ void updateRejectingTheKmsKeyAppliesNothing() {+ doThrow(new AwsException("NotFoundException", "Key not found: missing", 404))+ .when(kmsService).describeKey("missing", REGION);+ Guardrail created = create("kms-rejected");++ ObjectNode request = createRequest("kms-rejected-renamed");+ request.put("description", "should not stick");+ request.put("blockedInputMessaging", "input blocked v2");+ request.put("blockedOutputsMessaging", "output blocked v2");+ request.put("kmsKeyId", "missing");+ AwsException error = assertThrows(AwsException.class,+ () -> service.updateGuardrail(created.getGuardrailId(), request, REGION));+ assertEquals("ValidationException", error.getErrorCode());++ Guardrail draft = service.getGuardrail(created.getGuardrailId(), null, REGION);+ assertEquals("kms-rejected", draft.getName());+ assertNull(draft.getDescription());+ assertEquals("input blocked", draft.getBlockedInputMessaging());+ assertEquals("output blocked", draft.getBlockedOutputsMessaging());+ }+@Testvoid guardrailsAreScopedToTheirRegion() {Guardrail created = create("regional");