fix(bedrock-agentcore): check tag ARNs against the request account (#6802)

ListTagsForResource, TagResource and UntagResource compared the ARN's
account with the configured default account, while the create calls mint
ARNs with the request's account. Any caller outside the default account
was answered ResourceNotFoundException for its own resources.

Compare with the request account instead. An ARN naming another account
or region is still answered ResourceNotFoundException.

Closes #6008
This commit is contained in:
Mohith authored and GitHub committed 2026-10-28 07:13:41 -04:00
1 parent 548788840c
commit ad2089e662
4 files changed
+87 -5

No files matched your search

@@ -1578,6 +1578,14 @@ public final class TestFixtures {
.build();
}
public static BedrockAgentCoreControlClient bedrockAgentCoreControlClient(String accountId) {
return BedrockAgentCoreControlClient.builder()
.endpointOverride(ENDPOINT)
.region(REGION)
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accountId, "test")))
.build();
}
public static BedrockAgentCoreClient bedrockAgentCoreClient() {
return BedrockAgentCoreClient.builder()
.endpointOverride(ENDPOINT)
@@ -125,6 +125,45 @@ class BedrockAgentCoreTagIdentityTest {
}
}
@Test
void nonDefaultAccountMemoryTagsRespectIdentity() {
try (BedrockAgentCoreControlClient accountClient =
TestFixtures.bedrockAgentCoreControlClient("111111111111")) {
String name = "memtag" + UUID.randomUUID().toString().replace("-", "").substring(0, 8);
CreateMemoryResponse created = accountClient.createMemory(CreateMemoryRequest.builder()
.name(name).eventExpiryDuration(30).build());
String arn = created.memory().arn();
try {
assertThat(arn).contains(":111111111111:");
accountClient.tagResource(TagResourceRequest.builder()
.resourceArn(arn).tags(Map.of("env", "dev")).build());
assertThat(accountClient.listTagsForResource(ListTagsForResourceRequest.builder()
.resourceArn(arn).build()).tags()).containsEntry("env", "dev");
accountClient.untagResource(UntagResourceRequest.builder()
.resourceArn(arn).tagKeys(List.of("env")).build());
assertThat(accountClient.listTagsForResource(ListTagsForResourceRequest.builder()
.resourceArn(arn).build()).tags()).doesNotContainKey("env");
String[] parts = arn.split(":");
String foreignAccount = arn.replace(":111111111111:", ":222222222222:");
String otherRegion = parts[3].equals("eu-west-3") ? "us-east-1" : "eu-west-3";
String foreignRegion = arn.replace(":" + parts[3] + ":", ":" + otherRegion + ":");
for (String foreign : List.of(foreignAccount, foreignRegion)) {
assertThatThrownBy(() -> accountClient.listTagsForResource(ListTagsForResourceRequest.builder()
.resourceArn(foreign).build())).isInstanceOf(ResourceNotFoundException.class);
assertThatThrownBy(() -> accountClient.tagResource(TagResourceRequest.builder()
.resourceArn(foreign).tags(Map.of("env", "stolen")).build()))
.isInstanceOf(ResourceNotFoundException.class);
assertThatThrownBy(() -> accountClient.untagResource(UntagResourceRequest.builder()
.resourceArn(foreign).tagKeys(List.of("env")).build()))
.isInstanceOf(ResourceNotFoundException.class);
}
} finally {
accountClient.deleteMemory(DeleteMemoryRequest.builder().memoryId(created.memory().id()).build());
}
}
}
@Test
@Order(2)
void workloadIdentityCrud() {
@@ -1,7 +1,7 @@
package io.github.hectorvent.floci.services.bedrockagentcorecontrol;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.TagHandler;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -21,17 +21,17 @@ public class BedrockAgentCoreTagHandler implements TagHandler {
private final BedrockAgentCoreControlService runtimeService;
private final BedrockAgentCoreGatewayService gatewayService;
private final BedrockAgentCoreMemoryService memoryService;
private final EmulatorConfig config;
private final RegionResolver regionResolver;
@Inject
public BedrockAgentCoreTagHandler(BedrockAgentCoreControlService runtimeService,
BedrockAgentCoreGatewayService gatewayService,
BedrockAgentCoreMemoryService memoryService,
EmulatorConfig config) {
RegionResolver regionResolver) {
this.runtimeService = runtimeService;
this.gatewayService = gatewayService;
this.memoryService = memoryService;
this.config = config;
this.regionResolver = regionResolver;
}
@Override
@@ -96,7 +96,7 @@ public class BedrockAgentCoreTagHandler implements TagHandler {
* validation error, so the response does not confirm what exists behind the foreign identity.
*/
private void requireLocalIdentity(String region, String arn, String[] parts) {
if (!region.equals(parts[3]) || !config.defaultAccountId().equals(parts[4])) {
if (!region.equals(parts[3]) || !regionResolver.getAccountId().equals(parts[4])) {
throw new AwsException("ResourceNotFoundException",
"AgentCore resource not found: " + arn, 404);
}
@@ -76,6 +76,41 @@ class BedrockAgentCoreTaggingIdentityIntegrationTest {
.then().statusCode(404);
}
@Test
void nonDefaultAccountCanTagItsMemoryAndRejectsForeignIdentity() {
String authorization = "AWS4-HMAC-SHA256 Credential=111111111111/20261028/us-east-1/bedrock-agentcore/aws4_request";
String arn = given().header("Authorization", authorization).contentType("application/json")
.body("{\"name\":\"accountTagMem\",\"eventExpiryDuration\":30}")
.when().post("/memories/create").then().statusCode(202).extract().path("memory.arn");
try {
given().header("Authorization", authorization).contentType("application/json")
.body("{\"tags\":{\"env\":\"dev\"}}")
.when().post("/tags/" + arn).then().statusCode(204);
given().header("Authorization", authorization).when().get("/tags/" + arn)
.then().statusCode(200).body("tags.env", equalTo("dev"));
given().header("Authorization", authorization).when().delete("/tags/" + arn + "?tagKeys=env")
.then().statusCode(204);
given().header("Authorization", authorization).when().get("/tags/" + arn)
.then().statusCode(200).body("tags.env", nullValue());
for (String foreign : new String[] {arn.replace(":111111111111:", ":222222222222:"),
arn.replace(":us-east-1:", ":eu-west-1:")}) {
given().header("Authorization", authorization).when().get("/tags/" + foreign)
.then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
given().header("Authorization", authorization).contentType("application/json")
.body("{\"tags\":{\"env\":\"stolen\"}}")
.when().post("/tags/" + foreign).then().statusCode(404)
.body("__type", equalTo("ResourceNotFoundException"));
given().header("Authorization", authorization).when().delete("/tags/" + foreign + "?tagKeys=env")
.then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
}
} finally {
String memoryId = arn.substring(arn.lastIndexOf('/') + 1);
given().header("Authorization", authorization).when().delete("/memories/" + memoryId + "/delete")
.then().statusCode(202);
}
}
@Test
@Order(3)
void workloadIdentityCrud() {