feat(bedrock): store resource-based policies on guardrail profiles (#7208)

AWS lets a system-defined guardrail profile carry a resource-based
policy as well as a guardrail, and a guardrail with cross-Region
inference is shared only once each destination-Region profile ARN
carries one. The policy is stored in the Region whose endpoint
received PutResourcePolicy, under the ARN passed, so GetResourcePolicy
on a profile ARN through an endpoint that stored nothing returns {}.
A profile policy may allow only bedrock:ApplyGuardrail:
https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails-resource-based-policies.html

PutResourcePolicy, GetResourcePolicy and DeleteResourcePolicy now
accept a guardrail-profile ARN. Its policy is stored per calling Region
and profile ARN, GetResourcePolicy returns {} when that pair holds
none, and DeleteResourcePolicy clears only that pair. PutResourcePolicy
holds the policy to the guardrail language with bedrock:ApplyGuardrail
as the only action. A profile ARN of another account or partition, or
whose profile id is not of the form <prefix>.guardrail.v<N>:<M>, is
ResourceNotFoundException. Which destination Regions are valid for the
calling Region is not checked, and the policy is not evaluated against
callers.

Closes #6671
This commit is contained in:
galsuchetzky-act authored and GitHub committed 2026-11-02 09:01:14 -04:00
1 parent 0260a54ee2
commit a69b3621a0
4 files changed
+166 -22

No files matched your search

@@ -107,9 +107,6 @@ class BedrockResourcePolicyIntegrationTest {
() -> client.putResourcePolicy(r -> r.resourceArn(unknown).resourcePolicy(policy)));
assertEquals(404, put.statusCode());
assertEquals("ResourceNotFoundException", put.awsErrorDetails().errorCode());
String profile = "arn:aws:bedrock:us-east-1:" + ACCOUNT + ":guardrail-profile/us.guardrail.v1:0";
assertEquals(404, assertThrows(ResourceNotFoundException.class,
() -> client.getResourcePolicy(r -> r.resourceArn(profile))).statusCode());
CreateGuardrailResponse created = create(client);
try {
@@ -142,6 +139,33 @@ class BedrockResourcePolicyIntegrationTest {
}
}
@Test
void aGuardrailProfilePolicyIsKeptUnderTheCallingRegion() {
String profile = "arn:aws:bedrock:us-west-2:" + ACCOUNT + ":guardrail-profile/us.guardrail.v1:0";
try (BedrockClient source = client(ACCOUNT, Region.US_EAST_1);
BedrockClient destination = client(ACCOUNT, Region.US_WEST_2);
BedrockClient otherAccount = client(OTHER_ACCOUNT, Region.US_EAST_1)) {
String policy = rootPolicy(profile, "\"bedrock:ApplyGuardrail\"");
try {
assertEquals("{}", source.getResourcePolicy(r -> r.resourceArn(profile)).resourcePolicy());
source.putResourcePolicy(r -> r.resourceArn(profile).resourcePolicy(policy));
assertEquals(policy, source.getResourcePolicy(r -> r.resourceArn(profile)).resourcePolicy());
assertEquals("{}", destination.getResourcePolicy(r -> r.resourceArn(profile)).resourcePolicy());
assertThrows(ValidationException.class, () -> source.putResourcePolicy(r -> r.resourceArn(profile)
.resourcePolicy(rootPolicy(profile, "\"bedrock:GetGuardrail\""))));
assertThrows(ResourceNotFoundException.class,
() -> otherAccount.getResourcePolicy(r -> r.resourceArn(profile)));
source.deleteResourcePolicy(r -> r.resourceArn(profile));
assertEquals("{}", source.getResourcePolicy(r -> r.resourceArn(profile)).resourcePolicy());
} finally {
source.deleteResourcePolicy(r -> r.resourceArn(profile));
}
}
}
private static CreateGuardrailResponse create(BedrockClient client) {
return create(client, "rp-" + UUID.randomUUID());
}
@@ -40,7 +40,7 @@ class BedrockServiceTest {
@BeforeEach
void setUp() {
kmsService = mock(KmsService.class);
service = new BedrockService(new InMemoryStorage<>(),
service = new BedrockService(new InMemoryStorage<>(), new InMemoryStorage<>(),
new RegionResolver(REGION, ACCOUNT), mapper, kmsService);
}
@@ -562,6 +562,9 @@ class BedrockServiceTest {
assertTrue(service.listGuardrails(null, null, null, "eu-west-1").items().isEmpty());
}
private static final String PROFILE_ARN_US_EAST_2 =
"arn:aws:bedrock:us-east-2:" + ACCOUNT + ":guardrail-profile/us.guardrail.v1:0";
private static String rootPolicy(String resourceArn, String action) {
return """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
@@ -591,8 +594,7 @@ class BedrockServiceTest {
for (String other : List.of(created.getGuardrailId(),
created.getGuardrailArn().replace(ACCOUNT, "111122223333"),
created.getGuardrailArn().replace(":guardrail/", ":custom-model/"),
"arn:aws:bedrock:" + REGION + ":" + ACCOUNT + ":guardrail-profile/us.guardrail.v1:0")) {
created.getGuardrailArn().replace(":guardrail/", ":custom-model/"))) {
AwsException e = assertThrows(AwsException.class, () -> service.getResourcePolicy(other, REGION));
assertEquals("ResourceNotFoundException", e.getErrorCode(), other);
}
@@ -600,4 +602,48 @@ class BedrockServiceTest {
() -> service.putResourcePolicy(created.getGuardrailArn(), policy, "eu-west-1"));
assertEquals("ResourceNotFoundException", otherRegion.getErrorCode());
}
@Test
void guardrailProfilePolicyIsKeptUnderTheRegionThatStoredIt() {
String policy = rootPolicy(PROFILE_ARN_US_EAST_2, "bedrock:ApplyGuardrail");
service.putResourcePolicy(PROFILE_ARN_US_EAST_2, policy, REGION);
assertEquals(policy, service.getResourcePolicy(PROFILE_ARN_US_EAST_2, REGION));
assertEquals("{}", service.getResourcePolicy(PROFILE_ARN_US_EAST_2, "us-east-2"));
}
@Test
void deletingAGuardrailProfilePolicyLeavesTheEmptyPolicy() {
assertEquals("{}", service.getResourcePolicy(PROFILE_ARN_US_EAST_2, REGION));
service.putResourcePolicy(PROFILE_ARN_US_EAST_2, rootPolicy(PROFILE_ARN_US_EAST_2, "bedrock:ApplyGuardrail"),
REGION);
service.deleteResourcePolicy(PROFILE_ARN_US_EAST_2, REGION);
assertEquals("{}", service.getResourcePolicy(PROFILE_ARN_US_EAST_2, REGION));
}
@Test
void guardrailProfilePolicyAllowsOnlyApplyGuardrail() {
AwsException e = assertThrows(AwsException.class, () -> service.putResourcePolicy(PROFILE_ARN_US_EAST_2,
rootPolicy(PROFILE_ARN_US_EAST_2, "bedrock:GetGuardrail"), REGION));
assertEquals("ValidationException", e.getErrorCode());
}
@Test
void guardrailProfileOfAnotherAccountOrPartitionOrWithAMalformedIdIsNotFound() {
String chinaProfile = "arn:aws-cn:bedrock:cn-north-1:" + ACCOUNT + ":guardrail-profile/cn.guardrail.v1:0";
for (String arn : List.of(PROFILE_ARN_US_EAST_2.replace(ACCOUNT, "111122223333"),
PROFILE_ARN_US_EAST_2.replace(":" + ACCOUNT + ":", "::"),
PROFILE_ARN_US_EAST_2.replace("us.guardrail.v1:0", "not-a-profile"),
chinaProfile,
chinaProfile.replace("arn:aws-cn:", "arn:aws:"))) {
AwsException e = assertThrows(AwsException.class, () -> service.getResourcePolicy(arn, REGION));
assertEquals("ResourceNotFoundException", e.getErrorCode(), arn);
AwsException put = assertThrows(AwsException.class, () -> service.putResourcePolicy(arn,
rootPolicy(arn, "bedrock:ApplyGuardrail"), REGION));
assertEquals("ResourceNotFoundException", put.getErrorCode(), arn);
}
}
}