Light, fluffy, and always free - The AWS Local Emulator alternative
fix(bedrock-agentcore): check tag ARNs against the request account (#6802)
Mohithad2089e66263
diff --git a/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java b/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.javaindex db92cf5860..3bb1ed5b40 100644--- a/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java+++ b/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java@@ -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)diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/BedrockAgentCoreTagIdentityTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/BedrockAgentCoreTagIdentityTest.javaindex 50f418468e..b9b04f0d67 100644--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/BedrockAgentCoreTagIdentityTest.java+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/BedrockAgentCoreTagIdentityTest.java@@ -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() {diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandler.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandler.javaindex 27d9a04dbd..fef0c91a81 100644--- a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandler.java@@ -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;@Injectpublic 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);}diff --git a/src/test/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTaggingIdentityIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTaggingIdentityIntegrationTest.javaindex f1914a2efa..6324f0ca8f 100644--- a/src/test/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTaggingIdentityIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTaggingIdentityIntegrationTest.java@@ -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() {