fix(bedrock): resolve the KMS key before updating a guardrail (#5710)

UpdateGuardrail set the name, description and blocked messaging on the
stored DRAFT before resolving kmsKeyId. getGuardrail returns the live
stored object, so a request rejected for an unusable key still left those
fields applied, while updatedAt, the policies and kmsKeyArn kept their old
values.

The key is now resolved into a local before any setter runs, the same
validate-before-mutate order #2501 gave UpdateMemory. It stays after the
guardrail lookup, so an unknown guardrail is still reported as
ResourceNotFoundException first.

Fixes #5654
This commit is contained in:
Ridwan authored and GitHub committed 2026-10-15 14:16:04 -04:00
1 parent 1e423420ed
commit ac83817802
3 files changed
+65 -1

No files matched your search

@@ -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);
@@ -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++) {
@@ -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());
}
@Test
void guardrailsAreScopedToTheirRegion() {
Guardrail created = create("regional");