diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlController.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlController.java index ef969cc03f..dd04619f5d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlController.java +++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlController.java @@ -72,12 +72,9 @@ public class BedrockAgentCoreControlController { stringMap(req.get("environmentVariables")), obj(req, "authorizerConfiguration"), obj(req, "protocolConfiguration"), + stringMap(req.get("tags")), text(req, "clientToken"), region); - Map tags = stringMap(req.get("tags")); - if (tags != null) { - runtime.getTags().putAll(tags); - } String version = String.valueOf(runtime.getLatestVersion()); ObjectNode out = objectMapper.createObjectNode(); diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java index 74d6a2ecc7..b37d18c272 100644 --- a/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java +++ b/src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java @@ -85,6 +85,14 @@ public class BedrockAgentCoreControlService { String roleArn, String description, Map environmentVariables, JsonNode authorizerConfiguration, JsonNode protocolConfiguration, String clientToken, String region) { + return createAgentRuntime(name, artifact, networkConfiguration, roleArn, description, environmentVariables, + authorizerConfiguration, protocolConfiguration, null, clientToken, region); + } + + public AgentRuntime createAgentRuntime(String name, JsonNode artifact, JsonNode networkConfiguration, + String roleArn, String description, Map environmentVariables, + JsonNode authorizerConfiguration, JsonNode protocolConfiguration, + Map tags, String clientToken, String region) { if (clientToken != null) { Optional replay = storage.scan(k -> k.startsWith(keyPrefix(region))).stream() .filter(r -> clientToken.equals(r.getClientToken())) @@ -139,6 +147,9 @@ public class BedrockAgentCoreControlService { } runtime.getVersions().add(snapshot(runtime, "1", now)); runtime.getEndpoints().add(newEndpoint("DEFAULT", "1", null, now)); + if (tags != null) { + runtime.getTags().putAll(tags); + } storage.put(key(region, id), runtime); LOG.infov("Created AgentCore runtime {0} (id={1})", name, id); diff --git a/src/test/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCorePersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCorePersistenceTest.java index f02ad08d98..a5fa4fedb9 100644 --- a/src/test/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCorePersistenceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCorePersistenceTest.java @@ -6,17 +6,23 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.InMemoryStorage; import io.github.hectorvent.floci.core.storage.PersistentStorage; +import io.github.hectorvent.floci.core.storage.WalStorage; import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.AgentRuntime; import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.Memory; +import jakarta.ws.rs.core.HttpHeaders; +import jakarta.ws.rs.core.Response; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; /** * Persistence-across-restart coverage: writes a fully-populated agent runtime through @@ -83,6 +89,45 @@ class BedrockAgentCorePersistenceTest { assertTrue(loaded.getEndpoints().stream().anyMatch(e -> "DEFAULT".equals(e.getName()))); } + @Test + void tagsSentToCreateAgentRuntimeAreInTheWalRecord(@TempDir Path dir) throws IOException { + TypeReference> ref = new TypeReference<>() {}; + Path wal = dir.resolve("runtimes.wal"); + Path walAfterCrash = dir.resolve("runtimes-after-crash.wal"); + WalStorage writer = + new WalStorage<>(dir.resolve("runtimes.json"), wal, ref, 3_600_000L); + try { + writer.load(); + RegionResolver regionResolver = new RegionResolver(REGION, "000000000000"); + BedrockAgentCoreControlController controller = new BedrockAgentCoreControlController( + new BedrockAgentCoreControlService(writer, regionResolver), regionResolver, mapper); + + Response response = controller.createAgentRuntime(mock(HttpHeaders.class), """ + { + "agentRuntimeName": "walAgent", + "agentRuntimeArtifact": {"containerConfiguration": {"containerUri": "x:latest"}}, + "networkConfiguration": {"networkMode": "PUBLIC"}, + "roleArn": "arn:aws:iam::000000000000:role/agent", + "tags": {"team": "ml"} + }"""); + assertEquals(202, response.getStatus()); + String id = ((ObjectNode) response.getEntity()).path("agentRuntimeId").asText(); + + // A copy of the WAL is what a crash leaves behind: nothing later rewrites the runtime. + Files.copy(wal, walAfterCrash); + WalStorage reader = + new WalStorage<>(dir.resolve("runtimes-after-crash.json"), walAfterCrash, ref, 3_600_000L); + try { + reader.load(); + assertEquals(Map.of("team", "ml"), reader.get("runtime:" + REGION + ":" + id).orElseThrow().getTags()); + } finally { + reader.shutdown(); + } + } finally { + writer.shutdown(); + } + } + @Test void memorySurvivesPersistentStorageRoundTrip(@TempDir Path dir) { BedrockAgentCoreMemoryService svc = new BedrockAgentCoreMemoryService( 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.java index 6324f0ca8f..067db36cc3 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 @@ -13,6 +13,7 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; +import static org.junit.jupiter.api.Assertions.assertEquals; @QuarkusTest @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @@ -76,6 +77,35 @@ class BedrockAgentCoreTaggingIdentityIntegrationTest { .then().statusCode(404); } + @Test + void aClientTokenReplayKeepsTheTagsOfTheOriginalCreate() { + String clientToken = "replay-create-runtime-tags-token-0001"; + String firstArn = createRuntimeWithTags(clientToken, "{\"team\":\"ml\"}"); + String replayArn = createRuntimeWithTags(clientToken, "{\"env\":\"dev\"}"); + + assertEquals(firstArn, replayArn); + given().when().get("/tags/" + firstArn) + .then().statusCode(200) + .body("tags.team", equalTo("ml")) + .body("tags.env", nullValue()); + } + + private static String createRuntimeWithTags(String clientToken, String tags) { + return given().contentType("application/json") + .body(""" + { + "agentRuntimeName": "replayTagAgent", + "agentRuntimeArtifact": {"containerConfiguration": {"containerUri": "x:latest"}}, + "networkConfiguration": {"networkMode": "PUBLIC"}, + "roleArn": "arn:aws:iam::000000000000:role/agent", + "clientToken": "%s", + "tags": %s + }""".formatted(clientToken, tags)) + .when().put("/runtimes/") + .then().statusCode(202) + .extract().path("agentRuntimeArn"); + } + @Test void nonDefaultAccountCanTagItsMemoryAndRejectsForeignIdentity() { String authorization = "AWS4-HMAC-SHA256 Credential=111111111111/20261028/us-east-1/bedrock-agentcore/aws4_request";