mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(bedrock-agentcore): support memory tagging and dropped fields (#2501)
* fix(bedrock-agentcore): tag memory resources and honor dropped CreateMemory/UpdateMemory fields Three divergences from AWS in the Memory control plane (#2316): - Memory ARNs were not taggable: the shared /tags/{arn} handler dispatched only agent/ and gateway/ segments, returning ValidationException for memory/ ARNs even though AWS supports TagResource on memories. - CreateMemory silently dropped tags, encryptionKeyArn, and memoryExecutionRoleArn: accepted with 202, never stored. - UpdateMemory ignored eventExpiryDuration (and memoryExecutionRoleArn), applying only description. Memory now persists all three fields, the tag handler dispatches three ways (with the foreign-account 404 guard covering memories), and UpdateMemory applies description, eventExpiryDuration (validated 3-365), and memoryExecutionRoleArn. Matching AWS, the Memory response shape never embeds tags; they surface only via ListTagsForResource. * fix(bedrock-agentcore): validate UpdateMemory input before mutating stored state A rejected update (out-of-range eventExpiryDuration) could leave the description already applied, because get() returns the live stored object and validation ran after the first setter. Validate all inputs up front, and pin the no-partial-mutation behavior with unit and integration tests. Also deduplicates the @Order(3) collision in BedrockAgentCoreMemoryIntegrationTest. Addresses review feedback from pgermosen.
This commit is contained in:
12 files changed
+453
-41
No files matched your search
+48
-7
@@ -11,6 +11,7 @@ import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@@ -22,13 +23,24 @@ class BedrockAgentCoreMemoryIntegrationTest {
|
||||
@Order(1)
|
||||
void createMemory() {
|
||||
memoryId = given().contentType("application/json")
|
||||
.body("{\"name\":\"myMemory\",\"eventExpiryDuration\":30}")
|
||||
.body("""
|
||||
{
|
||||
"name": "myMemory",
|
||||
"eventExpiryDuration": 30,
|
||||
"encryptionKeyArn": "arn:aws:kms:us-east-1:000000000000:key/mem-key",
|
||||
"memoryExecutionRoleArn": "arn:aws:iam::000000000000:role/mem-role",
|
||||
"tags": {"team": "core"}
|
||||
}""")
|
||||
.when().post("/memories/create")
|
||||
.then().statusCode(202)
|
||||
.body("memory.id", notNullValue())
|
||||
.body("memory.name", equalTo("myMemory"))
|
||||
.body("memory.status", equalTo("ACTIVE"))
|
||||
.body("memory.arn", containsString(":memory/"))
|
||||
.body("memory.encryptionKeyArn", equalTo("arn:aws:kms:us-east-1:000000000000:key/mem-key"))
|
||||
.body("memory.memoryExecutionRoleArn", equalTo("arn:aws:iam::000000000000:role/mem-role"))
|
||||
// The AWS Memory shape has no tags field; tags surface only via ListTagsForResource.
|
||||
.body("memory.tags", nullValue())
|
||||
.extract().path("memory.id");
|
||||
}
|
||||
|
||||
@@ -38,25 +50,54 @@ class BedrockAgentCoreMemoryIntegrationTest {
|
||||
given().when().get("/memories/" + memoryId + "/details")
|
||||
.then().statusCode(200)
|
||||
.body("memory.name", equalTo("myMemory"))
|
||||
.body("memory.eventExpiryDuration", equalTo(30));
|
||||
.body("memory.eventExpiryDuration", equalTo(30))
|
||||
.body("memory.encryptionKeyArn", equalTo("arn:aws:kms:us-east-1:000000000000:key/mem-key"))
|
||||
.body("memory.memoryExecutionRoleArn", equalTo("arn:aws:iam::000000000000:role/mem-role"));
|
||||
|
||||
given().contentType("application/json").body("{}")
|
||||
.when().post("/memories/")
|
||||
.then().statusCode(200)
|
||||
.body("memories.id", hasItem(memoryId));
|
||||
.body("memories.id", hasItem(memoryId))
|
||||
// ListMemories returns slim summaries without the create-time fields.
|
||||
.body("memories.encryptionKeyArn", hasItem(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void updateMemory() {
|
||||
given().contentType("application/json").body("{\"description\":\"updated\"}")
|
||||
given().contentType("application/json")
|
||||
.body("{\"description\":\"updated\",\"eventExpiryDuration\":60,"
|
||||
+ "\"memoryExecutionRoleArn\":\"arn:aws:iam::000000000000:role/mem-role-2\"}")
|
||||
.when().put("/memories/" + memoryId + "/update")
|
||||
.then().statusCode(202)
|
||||
.body("memory.description", equalTo("updated"));
|
||||
.body("memory.description", equalTo("updated"))
|
||||
.body("memory.eventExpiryDuration", equalTo(60))
|
||||
.body("memory.memoryExecutionRoleArn", equalTo("arn:aws:iam::000000000000:role/mem-role-2"));
|
||||
|
||||
given().when().get("/memories/" + memoryId + "/details")
|
||||
.then().statusCode(200)
|
||||
.body("memory.eventExpiryDuration", equalTo(60))
|
||||
.body("memory.memoryExecutionRoleArn", equalTo("arn:aws:iam::000000000000:role/mem-role-2"))
|
||||
.body("memory.encryptionKeyArn", equalTo("arn:aws:kms:us-east-1:000000000000:key/mem-key"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void updateMemoryRejectsOutOfRangeExpiryWithoutPartialMutation() {
|
||||
given().contentType("application/json")
|
||||
.body("{\"description\":\"should-not-stick\",\"eventExpiryDuration\":2}")
|
||||
.when().put("/memories/" + memoryId + "/update")
|
||||
.then().statusCode(400);
|
||||
|
||||
// The rejected update must not have applied any of its fields.
|
||||
given().when().get("/memories/" + memoryId + "/details")
|
||||
.then().statusCode(200)
|
||||
.body("memory.description", equalTo("updated"))
|
||||
.body("memory.eventExpiryDuration", equalTo(60));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void deleteMemory() {
|
||||
given().when().delete("/memories/" + memoryId + "/delete")
|
||||
.then().statusCode(202)
|
||||
@@ -67,7 +108,7 @@ class BedrockAgentCoreMemoryIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
@Order(6)
|
||||
void createMemoryRequiresExpiry() {
|
||||
given().contentType("application/json").body("{\"name\":\"noExpiry\"}")
|
||||
.when().post("/memories/create")
|
||||
@@ -75,7 +116,7 @@ class BedrockAgentCoreMemoryIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(7)
|
||||
void eventExpiryDurationBoundaries() {
|
||||
// In range (3..365) → accepted.
|
||||
for (int v : new int[]{3, 365}) {
|
||||
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
package io.github.hectorvent.floci.services.bedrockagentcorecontrol;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.Memory;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class BedrockAgentCoreMemoryServiceTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String KEY_ARN = "arn:aws:kms:us-east-1:000000000000:key/mem-key";
|
||||
private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/mem-role";
|
||||
|
||||
private BedrockAgentCoreMemoryService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new BedrockAgentCoreMemoryService(
|
||||
new InMemoryStorage<>(), new RegionResolver(REGION, "000000000000"));
|
||||
}
|
||||
|
||||
private Memory create(String name) {
|
||||
return service.create(name, 30, "desc", KEY_ARN, ROLE_ARN,
|
||||
Map.of("team", "core"), null, REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createPersistsAllFields() {
|
||||
Memory memory = create("myMemory");
|
||||
assertTrue(memory.getMemoryId().matches("myMemory-[a-zA-Z0-9]{10}"), memory.getMemoryId());
|
||||
assertEquals(KEY_ARN, memory.getEncryptionKeyArn());
|
||||
assertEquals(ROLE_ARN, memory.getMemoryExecutionRoleArn());
|
||||
assertEquals("core", memory.getTags().get("team"));
|
||||
|
||||
Memory fetched = service.get(memory.getMemoryId(), REGION);
|
||||
assertEquals(KEY_ARN, fetched.getEncryptionKeyArn());
|
||||
assertEquals(ROLE_ARN, fetched.getMemoryExecutionRoleArn());
|
||||
assertEquals("core", fetched.getTags().get("team"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void clientTokenReplayReturnsOriginalAndIgnoresNewFields() {
|
||||
Memory first = service.create("tokMem", 30, null, null, null, null, "tok-1", REGION);
|
||||
Memory replay = service.create("otherName", 60, null, KEY_ARN, ROLE_ARN,
|
||||
Map.of("late", "tag"), "tok-1", REGION);
|
||||
assertEquals(first.getMemoryId(), replay.getMemoryId());
|
||||
assertEquals("tokMem", replay.getName());
|
||||
assertNull(replay.getEncryptionKeyArn());
|
||||
assertTrue(replay.getTags().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateAppliesFieldsAndLeavesNullsUntouched() {
|
||||
Memory memory = create("updMemory");
|
||||
service.update(memory.getMemoryId(), null, 90, null, REGION);
|
||||
|
||||
Memory after = service.get(memory.getMemoryId(), REGION);
|
||||
assertEquals(90, after.getEventExpiryDuration());
|
||||
assertEquals("desc", after.getDescription());
|
||||
assertEquals(ROLE_ARN, after.getMemoryExecutionRoleArn());
|
||||
|
||||
service.update(memory.getMemoryId(), "new desc", null,
|
||||
"arn:aws:iam::000000000000:role/mem-role-2", REGION);
|
||||
after = service.get(memory.getMemoryId(), REGION);
|
||||
assertEquals("new desc", after.getDescription());
|
||||
assertEquals(90, after.getEventExpiryDuration());
|
||||
assertEquals("arn:aws:iam::000000000000:role/mem-role-2", after.getMemoryExecutionRoleArn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateRejectsOutOfRangeExpiry() {
|
||||
Memory memory = create("badUpd");
|
||||
for (int v : new int[]{2, 366}) {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> service.update(memory.getMemoryId(), null, v, null, REGION));
|
||||
assertEquals(400, e.getHttpStatus());
|
||||
}
|
||||
assertEquals(30, service.get(memory.getMemoryId(), REGION).getEventExpiryDuration());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectedUpdateAppliesNothing() {
|
||||
Memory memory = create("atomicUpd");
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.update(
|
||||
memory.getMemoryId(), "should-not-stick", 999,
|
||||
"arn:aws:iam::000000000000:role/should-not-stick", REGION));
|
||||
assertEquals(400, e.getHttpStatus());
|
||||
|
||||
Memory after = service.get(memory.getMemoryId(), REGION);
|
||||
assertEquals("desc", after.getDescription());
|
||||
assertEquals(30, after.getEventExpiryDuration());
|
||||
assertEquals(ROLE_ARN, after.getMemoryExecutionRoleArn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagOperationsRoundTripThroughTheArn() {
|
||||
Memory memory = create("tagMemory");
|
||||
String arn = service.arn(memory, REGION);
|
||||
|
||||
assertEquals(Map.of("team", "core"), service.getTagsByArn(REGION, arn));
|
||||
|
||||
service.tagByArn(REGION, arn, Map.of("env", "prod"));
|
||||
assertEquals("prod", service.getTagsByArn(REGION, arn).get("env"));
|
||||
assertEquals("core", service.getTagsByArn(REGION, arn).get("team"));
|
||||
|
||||
service.untagByArn(REGION, arn, List.of("env"));
|
||||
assertNull(service.getTagsByArn(REGION, arn).get("env"));
|
||||
assertEquals("core", service.getTagsByArn(REGION, arn).get("team"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByArnRejectsNonMemoryArnAndUnknownId() {
|
||||
AwsException wrongType = assertThrows(AwsException.class, () -> service.getTagsByArn(REGION,
|
||||
"arn:aws:bedrock-agentcore:us-east-1:000000000000:gateway/gw-abc1234567"));
|
||||
assertEquals(400, wrongType.getHttpStatus());
|
||||
|
||||
AwsException unknown = assertThrows(AwsException.class, () -> service.getTagsByArn(REGION,
|
||||
"arn:aws:bedrock-agentcore:us-east-1:000000000000:memory/nosuchmem-abc1234567"));
|
||||
assertEquals(404, unknown.getHttpStatus());
|
||||
}
|
||||
}
|
||||
+34
@@ -7,6 +7,7 @@ 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.services.bedrockagentcorecontrol.model.AgentRuntime;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.Memory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
@@ -81,4 +82,37 @@ class BedrockAgentCorePersistenceTest {
|
||||
assertTrue(loaded.getEndpoints().stream().anyMatch(e -> "prod".equals(e.getName())));
|
||||
assertTrue(loaded.getEndpoints().stream().anyMatch(e -> "DEFAULT".equals(e.getName())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void memorySurvivesPersistentStorageRoundTrip(@TempDir Path dir) {
|
||||
BedrockAgentCoreMemoryService svc = new BedrockAgentCoreMemoryService(
|
||||
new InMemoryStorage<>(), new RegionResolver(REGION, "000000000000"));
|
||||
|
||||
Memory created = svc.create("persistMem", 45, "a memory",
|
||||
"arn:aws:kms:us-east-1:000000000000:key/mem-key",
|
||||
"arn:aws:iam::000000000000:role/mem-role",
|
||||
Map.of("team", "core"), null, REGION);
|
||||
String id = created.getMemoryId();
|
||||
svc.tagByArn(REGION, svc.arn(created, REGION), Map.of("env", "prod"));
|
||||
Memory before = svc.get(id, REGION);
|
||||
|
||||
Path file = dir.resolve("bedrock-agentcore-memories.json");
|
||||
TypeReference<Map<String, Memory>> ref = new TypeReference<>() {};
|
||||
PersistentStorage<String, Memory> writer = new PersistentStorage<>(file, ref);
|
||||
writer.put("memory:" + REGION + ":" + id, before);
|
||||
writer.flush();
|
||||
|
||||
PersistentStorage<String, Memory> reader = new PersistentStorage<>(file, ref);
|
||||
reader.load();
|
||||
Memory loaded = reader.get("memory:" + REGION + ":" + id).orElseThrow();
|
||||
|
||||
assertEquals("persistMem", loaded.getName());
|
||||
assertEquals(45, loaded.getEventExpiryDuration());
|
||||
assertEquals("arn:aws:kms:us-east-1:000000000000:key/mem-key", loaded.getEncryptionKeyArn());
|
||||
assertEquals("arn:aws:iam::000000000000:role/mem-role", loaded.getMemoryExecutionRoleArn());
|
||||
assertEquals("core", loaded.getTags().get("team"));
|
||||
assertEquals("prod", loaded.getTags().get("env"));
|
||||
assertNotNull(loaded.getCreatedAt());
|
||||
assertNotNull(loaded.getUpdatedAt());
|
||||
}
|
||||
}
|
||||
+45
-3
@@ -139,10 +139,52 @@ class BedrockAgentCoreTaggingIdentityIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void taggingUnsupportedResourceReturns400() {
|
||||
// Memory/endpoint ARNs are not taggable (AWS: runtime + gateway only).
|
||||
void memoryTagRoundTrip() {
|
||||
// Memories are AWS-taggable; CreateMemory accepts a tags map.
|
||||
String memArn = given().contentType("application/json")
|
||||
.body("{\"name\":\"tagMem\",\"eventExpiryDuration\":30,\"tags\":{\"team\":\"core\"}}")
|
||||
.when().post("/memories/create")
|
||||
.then().statusCode(202)
|
||||
.extract().path("memory.arn");
|
||||
|
||||
// tags provided at create time are retained
|
||||
given().when().get("/tags/" + memArn)
|
||||
.then().statusCode(200).body("tags.team", equalTo("core"));
|
||||
|
||||
given().contentType("application/json").body("{\"tags\":{\"env\":\"prod\"}}")
|
||||
.when().post("/tags/" + memArn).then().statusCode(204);
|
||||
given().when().get("/tags/" + memArn)
|
||||
.then().statusCode(200)
|
||||
.body("tags.env", equalTo("prod"))
|
||||
.body("tags.team", equalTo("core"));
|
||||
|
||||
given().when().delete("/tags/" + memArn + "?tagKeys=env").then().statusCode(204);
|
||||
given().when().get("/tags/" + memArn)
|
||||
.then().statusCode(200)
|
||||
.body("tags.env", nullValue())
|
||||
.body("tags.team", equalTo("core"));
|
||||
|
||||
// The identity guard covers memories too: a foreign-account ARN must not resolve.
|
||||
String foreignAccount = memArn.replace(":000000000000:", ":111111111111:");
|
||||
given().when().get("/tags/" + foreignAccount).then().statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void taggingNonexistentMemoryReturns404() {
|
||||
given().contentType("application/json").body("{\"tags\":{\"x\":\"y\"}}")
|
||||
.when().post("/tags/arn:aws:bedrock-agentcore:us-east-1:000000000000:memory/mymem-abc1234567")
|
||||
.when().post("/tags/arn:aws:bedrock-agentcore:us-east-1:000000000000:memory/nosuchmem-abc1234567")
|
||||
.then().statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void taggingUnsupportedResourceReturns400() {
|
||||
// Workload identity ARNs are not taggable (AWS: runtime, gateway, memory, and the
|
||||
// built-in tool resources only).
|
||||
given().contentType("application/json").body("{\"tags\":{\"x\":\"y\"}}")
|
||||
.when().post("/tags/arn:aws:bedrock-agentcore:us-east-1:000000000000:"
|
||||
+ "workload-identity-directory/default/workload-identity/wid_test_1")
|
||||
.then().statusCode(400);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user