diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/BedrockAgentCoreControlTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/BedrockAgentCoreControlTest.java index 438dfdbfdd..1870ea5c86 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/BedrockAgentCoreControlTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/BedrockAgentCoreControlTest.java @@ -63,7 +63,7 @@ class BedrockAgentCoreControlTest { assertThat(runtimeId).startsWith(runtimeName + "-"); assertThat(response.agentRuntimeVersion()).isEqualTo("1"); assertThat(response.statusAsString()).isEqualTo("READY"); - assertThat(response.agentRuntimeArn()).contains(":bedrock-agentcore:").contains(":agent/"); + assertThat(response.agentRuntimeArn()).contains(":bedrock-agentcore:").contains(":runtime/" + runtimeId); assertThat(response.workloadIdentityDetails().workloadIdentityArn()).isNotBlank(); } diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/BedrockAgentCoreEndpointTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/BedrockAgentCoreEndpointTest.java index ac05aebb67..4f639d6e2d 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/BedrockAgentCoreEndpointTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/BedrockAgentCoreEndpointTest.java @@ -66,7 +66,7 @@ class BedrockAgentCoreEndpointTest { assertThat(response.endpointName()).isEqualTo("prod"); assertThat(response.targetVersion()).isEqualTo("1"); assertThat(response.statusAsString()).isEqualTo("READY"); - assertThat(response.agentRuntimeEndpointArn()).contains(":agentEndpoint/"); + assertThat(response.agentRuntimeEndpointArn()).contains(":runtime/" + runtimeId + "/runtime-endpoint/prod"); } @Test diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/BedrockAgentCoreInvokeTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/BedrockAgentCoreInvokeTest.java index 82025a4557..8654550094 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/BedrockAgentCoreInvokeTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/BedrockAgentCoreInvokeTest.java @@ -13,7 +13,7 @@ import static org.assertj.core.api.Assertions.*; class BedrockAgentCoreInvokeTest { private static final String ARN = - "arn:aws:bedrock-agentcore:us-east-1:000000000000:agent/abcdef12-3456-7890-abcd-ef1234567890:1"; + "arn:aws:bedrock-agentcore:us-east-1:000000000000:runtime/test-AbCdEfGhIj"; private static final String SESSION_ID = "session-0000000000000000000000000000abc"; @Test diff --git a/docs/design/bedrock-agentcore.md b/docs/design/bedrock-agentcore.md index 13a5aeadf8..7c88413994 100644 --- a/docs/design/bedrock-agentcore.md +++ b/docs/design/bedrock-agentcore.md @@ -95,7 +95,7 @@ All verified against the AWS API reference. **Note verbs and trailing slashes.** | DeleteAgentRuntime | `DELETE /runtimes/{agentRuntimeId}/?clientToken={t}` | 202 | body `{agentRuntimeId, status: DELETING}` | Identity: `agentRuntimeId` = `-<10 [a-zA-Z0-9]>` (`[a-zA-Z][a-zA-Z0-9_]{0,99}-[a-zA-Z0-9]{10}`). -ARN embeds a fresh UUID + version: `arn:aws:bedrock-agentcore:::agent/:`. +Runtime ARN: `arn::bedrock-agentcore:::runtime/`, stable across versions. `agentRuntimeVersion` is a string int starting `"1"`, incremented on Update. `status` = `READY`. ### Agent Runtime Endpoint (control plane) @@ -108,7 +108,7 @@ ARN embeds a fresh UUID + version: `arn:aws:bedrock-agentcore:: | DeleteAgentRuntimeEndpoint | `DELETE /runtimes/{agentRuntimeId}/runtime-endpoints/{endpointName}/?clientToken={t}` | 202 | | ListAgentRuntimeEndpoints | `POST /runtimes/{agentRuntimeId}/runtime-endpoints/?maxResults=&nextToken=` | 200 | -Endpoint ARN `arn:aws:bedrock-agentcore:::agentEndpoint/` (no version). +Endpoint ARN `arn::bedrock-agentcore:::runtime//runtime-endpoint/`. Create body: `name` (required), `agentRuntimeVersion`/`description`/`clientToken`/`tags` (optional). Fields: `liveVersion` (serving) vs `targetVersion` (requested). A `DEFAULT` endpoint is auto-created on runtime create so an unqualified invoke resolves. diff --git a/docs/services/bedrock-agentcore.md b/docs/services/bedrock-agentcore.md index ed00b0c81d..ddeec17f45 100644 --- a/docs/services/bedrock-agentcore.md +++ b/docs/services/bedrock-agentcore.md @@ -90,6 +90,12 @@ runtimes reach `READY` immediately and hold metadata only. See A `DEFAULT` endpoint is created automatically with each runtime, and each runtime is associated with an auto-created, resolvable workload identity. +Legacy `agent/:` runtime ARNs remain usable for invocation validation and tagging. +Legacy `agentEndpoint/` endpoint IDs resolve for tagging and CloudFormation updates and deletes. +Responses use `runtime/` and `runtime//runtime-endpoint/`. +With runtime existence validation enabled, a harness runtime ARN is valid while its harness remains +registered; unknown and deleted runtime ARNs return `ResourceNotFoundException`. + ## Memory events The event data plane is backed by real storage, so what a read returns is what was written rather @@ -112,6 +118,8 @@ rather than on stored events. ## Harnesses +Harness tags use the `harness/` ARN. Tagging a harness runtime-environment ARN is not emulated. + A harness is stored with its configuration and settles in `READY` at once. No agent runtime is started for it, and `InvokeHarness` does not read it. @@ -190,8 +198,9 @@ block is ever produced even when a request supplies `tools`. ## Behavior notes -- `agentRuntimeId` is `-<10 alphanumerics>`; the ARN embeds a UUID and the - version: `arn:aws:bedrock-agentcore:::agent/:`. +- `agentRuntimeId` is `-<10 alphanumerics>`; the runtime ARN is + `arn::bedrock-agentcore:::runtime/` and + stays the same across versions. Endpoint ARNs append `/runtime-endpoint/`, including `DEFAULT`. - `agentRuntimeName` must match `[a-zA-Z][a-zA-Z0-9_]{0,47}` (no hyphens); invalid names return `ValidationException`. - Each `UpdateAgentRuntime` increments the version and preserves prior versions for diff --git a/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreArns.java b/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreArns.java index c521f8338d..734c528f76 100644 --- a/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreArns.java +++ b/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreArns.java @@ -12,9 +12,8 @@ final class BedrockAgentCoreArns { } /** - * The resource of an AgentCore ARN without its qualifier: {@code agent/} for - * {@code arn:aws:bedrock-agentcore:::agent/:}. Null when the value - * is not an ARN. + * The resource of an AgentCore ARN without a colon qualifier, including nested runtime endpoint + * paths. Null when the value is not an ARN. */ static String resource(String arn) { if (!AwsArnUtils.isArn(arn)) { diff --git a/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlController.java b/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlController.java index 8cb48c5fc6..79e2ba6bd2 100644 --- a/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlController.java +++ b/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlController.java @@ -78,7 +78,7 @@ public class BedrockAgentCoreControlController { String version = String.valueOf(runtime.getLatestVersion()); ObjectNode out = objectMapper.createObjectNode(); - out.put("agentRuntimeArn", service.arn(runtime, version, region)); + out.put("agentRuntimeArn", service.arn(runtime, region)); out.put("agentRuntimeId", runtime.getAgentRuntimeId()); out.put("agentRuntimeVersion", version); putInstant(out, "createdAt", runtime.getCreatedAt()); @@ -125,7 +125,7 @@ public class BedrockAgentCoreControlController { String v = snap.getVersion(); ObjectNode out = objectMapper.createObjectNode(); - out.put("agentRuntimeArn", service.arn(runtime, v, region)); + out.put("agentRuntimeArn", service.arn(runtime, region)); out.put("agentRuntimeId", runtime.getAgentRuntimeId()); out.put("agentRuntimeName", runtime.getAgentRuntimeName()); out.put("agentRuntimeVersion", v); @@ -181,7 +181,7 @@ public class BedrockAgentCoreControlController { String version = String.valueOf(runtime.getLatestVersion()); ObjectNode out = objectMapper.createObjectNode(); - out.put("agentRuntimeArn", service.arn(runtime, version, region)); + out.put("agentRuntimeArn", service.arn(runtime, region)); out.put("agentRuntimeId", runtime.getAgentRuntimeId()); out.put("agentRuntimeVersion", version); putInstant(out, "createdAt", runtime.getCreatedAt()); @@ -339,8 +339,8 @@ public class BedrockAgentCoreControlController { private ObjectNode endpointResponse(AgentRuntime runtime, AgentRuntimeEndpoint endpoint, String region, boolean full) { ObjectNode node = objectMapper.createObjectNode(); - node.put("agentRuntimeArn", service.arn(runtime, endpoint.getTargetVersion(), region)); - node.put("agentRuntimeEndpointArn", service.endpointArn(endpoint, region)); + node.put("agentRuntimeArn", service.arn(runtime, region)); + node.put("agentRuntimeEndpointArn", service.endpointArn(runtime.getAgentRuntimeId(), endpoint, region)); node.put("agentRuntimeId", runtime.getAgentRuntimeId()); node.put("endpointName", endpoint.getName()); node.put("status", endpoint.getStatus()); @@ -364,7 +364,7 @@ public class BedrockAgentCoreControlController { private ObjectNode summary(AgentRuntime runtime, String version, String region) { ObjectNode node = objectMapper.createObjectNode(); - node.put("agentRuntimeArn", service.arn(runtime, version, region)); + node.put("agentRuntimeArn", service.arn(runtime, region)); node.put("agentRuntimeId", runtime.getAgentRuntimeId()); node.put("agentRuntimeName", runtime.getAgentRuntimeName()); node.put("agentRuntimeVersion", version); @@ -378,7 +378,7 @@ public class BedrockAgentCoreControlController { private ObjectNode versionSummary(AgentRuntime runtime, AgentRuntimeVersion snap, String region) { ObjectNode node = objectMapper.createObjectNode(); - node.put("agentRuntimeArn", service.arn(runtime, snap.getVersion(), region)); + node.put("agentRuntimeArn", service.arn(runtime, region)); node.put("agentRuntimeId", runtime.getAgentRuntimeId()); node.put("agentRuntimeName", runtime.getAgentRuntimeName()); node.put("agentRuntimeVersion", snap.getVersion()); diff --git a/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java b/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java index b8c5ac6aa6..a9e9ad7bf4 100644 --- a/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java +++ b/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java @@ -26,6 +26,7 @@ import java.util.Optional; import java.util.UUID; import java.util.concurrent.ThreadLocalRandom; import java.util.function.Consumer; +import java.util.function.Predicate; import java.util.regex.Pattern; /** @@ -54,6 +55,7 @@ public class BedrockAgentCoreControlService { private final RegionResolver regionResolver; private final StripedLocks runtimeLocks = new StripedLocks(RUNTIME_LOCK_STRIPES); private final BedrockAgentCoreIdentityService identityService; + private final BedrockAgentCoreHarnessRegistry harnessRegistry; // clientToken idempotency: tokens of completed deletes, so a replayed delete succeeds // instead of 404ing. In-memory only (reset on restart), and each entry expires after // DeletedTokenLedger.TTL, after which a replay 404s like an unknown token. @@ -62,9 +64,10 @@ public class BedrockAgentCoreControlService { @Inject public BedrockAgentCoreControlService(StorageFactory storageFactory, RegionResolver regionResolver, - BedrockAgentCoreIdentityService identityService, Clock clock) { + BedrockAgentCoreIdentityService identityService, Clock clock, + BedrockAgentCoreHarnessRegistry harnessRegistry) { this(storageFactory.create("bedrockagentcore", "bedrock-agentcore-runtimes.json", - new TypeReference>() {}), regionResolver, identityService, clock); + new TypeReference>() {}), regionResolver, identityService, clock, harnessRegistry); } BedrockAgentCoreControlService(StorageBackend storage, RegionResolver regionResolver) { @@ -78,9 +81,16 @@ public class BedrockAgentCoreControlService { BedrockAgentCoreControlService(StorageBackend storage, RegionResolver regionResolver, BedrockAgentCoreIdentityService identityService, Clock clock) { + this(storage, regionResolver, identityService, clock, null); + } + + BedrockAgentCoreControlService(StorageBackend storage, RegionResolver regionResolver, + BedrockAgentCoreIdentityService identityService, Clock clock, + BedrockAgentCoreHarnessRegistry harnessRegistry) { this.storage = storage; this.regionResolver = regionResolver; this.identityService = identityService; + this.harnessRegistry = harnessRegistry; this.deletedRuntimeTokens = new DeletedTokenLedger(clock); this.deletedEndpointTokens = new DeletedTokenLedger(clock); } @@ -289,8 +299,8 @@ public class BedrockAgentCoreControlService { return String.format("%05d", Integer.parseInt(version)); } - public String arn(AgentRuntime runtime, String version, String region) { - return regionResolver.buildArn(ARN_SERVICE, region, "agent/" + runtime.getUuid() + ":" + version); + public String arn(AgentRuntime runtime, String region) { + return regionResolver.buildArn(ARN_SERVICE, region, "runtime/" + runtime.getAgentRuntimeId()); } /** @@ -300,17 +310,24 @@ public class BedrockAgentCoreControlService { */ public boolean runtimeArnExists(String region, String arn) { String resource = BedrockAgentCoreArns.resource(arn); - if (resource == null || !resource.startsWith("agent/")) { + if (resource == null || (!resource.startsWith("runtime/") && !resource.startsWith("agent/"))) { return true; } - String uuid = resource.substring("agent/".length()); - String prefix = keyPrefix(region); - return storage.scan(k -> k.startsWith(prefix)).stream() - .anyMatch(r -> uuid.equals(r.getUuid())); + return findRuntimeByResource(region, resource).isPresent() + || (harnessRegistry != null && harnessRegistry.runtimeArnExists(region, arn)); } - public String endpointArn(AgentRuntimeEndpoint endpoint, String region) { - return regionResolver.buildArn(ARN_SERVICE, region, "agentEndpoint/" + endpoint.getUuid()); + private Optional findRuntimeByResource(String region, String resource) { + boolean legacy = resource.startsWith("agent/"); + String id = resource.substring(legacy ? "agent/".length() : "runtime/".length()); + return storage.scan(k -> k.startsWith(keyPrefix(region))).stream() + .filter(runtime -> id.equals(legacy ? runtime.getUuid() : runtime.getAgentRuntimeId())) + .findFirst(); + } + + public String endpointArn(String runtimeId, AgentRuntimeEndpoint endpoint, String region) { + return regionResolver.buildArn(ARN_SERVICE, region, + "runtime/" + runtimeId + "/runtime-endpoint/" + endpoint.getName()); } // ──────────────────────────── Tagging (Phase 4) ──────────────────────────── @@ -341,13 +358,9 @@ public class BedrockAgentCoreControlService { } } - /** The runtime a runtime ARN names, at any version; ResourceNotFoundException when none does. */ + /** The runtime a runtime ARN names; ResourceNotFoundException when none does. */ public AgentRuntime getAgentRuntimeByArn(String region, String arn) { - String uuid = uuidFromArn(arn); - String prefix = keyPrefix(region); - return storage.scan(k -> k.startsWith(prefix)).stream() - .filter(r -> uuid.equals(r.getUuid())) - .findFirst() + return findRuntimeByResource(region, runtimeResourceFromArn(arn)) .orElseThrow(() -> new AwsException("ResourceNotFoundException", "AgentCore resource not found: " + arn, 404)); } @@ -390,13 +403,12 @@ public class BedrockAgentCoreControlService { "AgentCore resource not found: " + arn, 404)); } - private static String uuidFromArn(String arn) { - // arn:aws:bedrock-agentcore:::agent/: + private static String runtimeResourceFromArn(String arn) { String resource = BedrockAgentCoreArns.resource(arn); - if (resource == null || !resource.startsWith("agent/")) { + if (resource == null || (!resource.startsWith("runtime/") && !resource.startsWith("agent/"))) { throw new AwsException("ValidationException", "Unsupported resource ARN: " + arn, 400); } - return resource.substring("agent/".length()); + return resource; } // ──────────────────────────── Endpoints (Phase 2) ──────────────────────────── @@ -533,32 +545,38 @@ public class BedrockAgentCoreControlService { public record EndpointLocation(AgentRuntime runtime, AgentRuntimeEndpoint endpoint) { } - /** - * The endpoint an endpoint ARN ({@code agentEndpoint/}) names, with its runtime, or empty - * when no runtime in the region holds it. Endpoints are addressed by runtime id and name on the - * API, so this is the only way back from the ARN CloudFormation keeps as the physical id. - */ + /** The endpoint and parent runtime named by a runtime endpoint ARN, or empty when absent. */ public Optional findEndpointByArn(String region, String arn) { String resource = BedrockAgentCoreArns.resource(arn); - if (resource == null || !resource.startsWith("agentEndpoint/")) { + if (resource == null) { return Optional.empty(); } - String uuid = resource.substring("agentEndpoint/".length()); - String prefix = keyPrefix(region); - for (AgentRuntime runtime : storage.scan(k -> k.startsWith(prefix))) { - synchronized (runtimeLock(region, runtime.getAgentRuntimeId())) { - Optional current = storage.get(key(region, runtime.getAgentRuntimeId())); - if (current.isEmpty()) { - continue; - } - for (AgentRuntimeEndpoint endpoint : current.get().getEndpoints()) { - if (uuid.equals(endpoint.getUuid())) { - return Optional.of(new EndpointLocation(current.get(), endpoint)); - } + if (resource.startsWith("agentEndpoint/")) { + String uuid = resource.substring("agentEndpoint/".length()); + for (AgentRuntime candidate : storage.scan(k -> k.startsWith(keyPrefix(region)))) { + Optional found = findEndpoint(region, candidate.getAgentRuntimeId(), + endpoint -> uuid.equals(endpoint.getUuid())); + if (found.isPresent()) { + return found; } } + return Optional.empty(); + } + String[] parts = resource.split("/", -1); + if (parts.length != 4 || !"runtime".equals(parts[0]) || !"runtime-endpoint".equals(parts[2])) { + return Optional.empty(); + } + return findEndpoint(region, parts[1], endpoint -> parts[3].equals(endpoint.getName())); + } + + private Optional findEndpoint(String region, String runtimeId, + Predicate matches) { + synchronized (runtimeLock(region, runtimeId)) { + return storage.get(key(region, runtimeId)).flatMap(runtime -> runtime.getEndpoints().stream() + .filter(matches) + .map(endpoint -> new EndpointLocation(runtime, endpoint)) + .findFirst()); } - return Optional.empty(); } private Object runtimeLock(String region, String runtimeId) { diff --git a/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreHarnessRegistry.java b/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreHarnessRegistry.java index 03feab1e82..55b49d5fed 100644 --- a/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreHarnessRegistry.java +++ b/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreHarnessRegistry.java @@ -194,6 +194,13 @@ public class BedrockAgentCoreHarnessRegistry { return new HarnessView(harness, harness.getCurrentVersion()); } + public synchronized boolean runtimeArnExists(String region, String arn) { + return storage.scan(key -> key.startsWith(keyPrefix(region))).stream() + .flatMap(harness -> harness.getVersions().stream()) + .anyMatch(version -> arn.equals(version.getConfiguration().path("environment") + .path(RUNTIME_ENVIRONMENT).path("agentRuntimeArn").asText(null))); + } + public PaginatedResult list(Integer maxResults, String nextToken, String region) { List all = storage.scan(key -> key.startsWith(keyPrefix(region))); return Pagination.paginate(all, Harness::getHarnessId, maxResults, nextToken, MAX_PAGE, "ValidationException"); diff --git a/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandler.java b/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandler.java index f771590308..241dd16fca 100644 --- a/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandler.java +++ b/src/main/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandler.java @@ -88,11 +88,14 @@ public class BedrockAgentCoreTagHandler implements TagHandler { if (resource.startsWith("gateway/")) { return ResourceKind.GATEWAY; } + if (resource.startsWith("agentEndpoint/")) { + return ResourceKind.ENDPOINT; + } if (resource.startsWith("agent/")) { return ResourceKind.RUNTIME; } - if (resource.startsWith("agentEndpoint/")) { - return ResourceKind.ENDPOINT; + if (resource.startsWith("runtime/")) { + return resource.contains("/runtime-endpoint/") ? ResourceKind.ENDPOINT : ResourceKind.RUNTIME; } if (resource.startsWith("memory/")) { return ResourceKind.MEMORY; diff --git a/src/main/java/io/floci/aws/services/cloudformation/provisioners/BedrockAgentCoreCfnProvisioner.java b/src/main/java/io/floci/aws/services/cloudformation/provisioners/BedrockAgentCoreCfnProvisioner.java index 59fa2a086c..8587feee9d 100644 --- a/src/main/java/io/floci/aws/services/cloudformation/provisioners/BedrockAgentCoreCfnProvisioner.java +++ b/src/main/java/io/floci/aws/services/cloudformation/provisioners/BedrockAgentCoreCfnProvisioner.java @@ -152,7 +152,7 @@ public class BedrockAgentCoreCfnProvisioner implements CfnResourceProvisioner { && Objects.equals(authorizer, prior.getAuthorizerConfiguration()) && Objects.equals(protocolConfiguration, prior.getProtocolConfiguration()) && environment.equals(prior.getEnvironmentVariables())); - String arn = runtimeService.arn(prior, String.valueOf(prior.getLatestVersion()), region); + String arn = runtimeService.arn(prior, region); Map priorTags = runtimeService.getTagsByArn(region, arn); if (configurationChanged || !priorTags.equals(tags)) { ObjectNode snapshot = inPlacePrior(prior.getAgentRuntimeId(), region); @@ -205,7 +205,7 @@ public class BedrockAgentCoreCfnProvisioner implements CfnResourceProvisioner { private void publishRuntime(StackResource r, AgentRuntime runtime, String region) { String version = String.valueOf(runtime.getLatestVersion()); r.setPhysicalId(runtime.getAgentRuntimeId()); - r.getAttributes().put("AgentRuntimeArn", runtimeService.arn(runtime, version, region)); + r.getAttributes().put("AgentRuntimeArn", runtimeService.arn(runtime, region)); r.getAttributes().put("AgentRuntimeId", runtime.getAgentRuntimeId()); r.getAttributes().put("AgentRuntimeVersion", version); r.getAttributes().put("Status", runtime.getStatus()); @@ -263,7 +263,7 @@ public class BedrockAgentCoreCfnProvisioner implements CfnResourceProvisioner { } else { AgentRuntimeEndpoint endpoint = runtimeService.createEndpoint(runtimeId, name, version, description, null, region); - r.setPhysicalId(runtimeService.endpointArn(endpoint, region)); + r.setPhysicalId(runtimeService.endpointArn(runtimeId, endpoint, region)); CfnRollback.guarded(r, ctx, true, () -> runtimeService.deleteEndpoint(runtimeId, name, null, region), () -> undoInPlace(r), () -> publishRuntimeEndpoint(r, runtimeService.getAgentRuntime(runtimeId, region), endpoint, region)); @@ -272,10 +272,10 @@ public class BedrockAgentCoreCfnProvisioner implements CfnResourceProvisioner { private void publishRuntimeEndpoint(StackResource r, AgentRuntime runtime, AgentRuntimeEndpoint endpoint, String region) { - String endpointArn = runtimeService.endpointArn(endpoint, region); + String endpointArn = runtimeService.endpointArn(runtime.getAgentRuntimeId(), endpoint, region); r.setPhysicalId(endpointArn); r.getAttributes().put("AgentRuntimeEndpointArn", endpointArn); - r.getAttributes().put("AgentRuntimeArn", runtimeService.arn(runtime, endpoint.getTargetVersion(), region)); + r.getAttributes().put("AgentRuntimeArn", runtimeService.arn(runtime, region)); r.getAttributes().put("Id", endpoint.getUuid()); r.getAttributes().put("Status", endpoint.getStatus()); r.getAttributes().put("TargetVersion", endpoint.getTargetVersion()); @@ -478,7 +478,7 @@ public class BedrockAgentCoreCfnProvisioner implements CfnResourceProvisioner { node(configuration, "authorizer"), node(configuration, "protocol"), region); } if (prior.has("tags")) { - reconcileRuntimeTags(runtimeService.arn(runtime, String.valueOf(runtime.getLatestVersion()), region), + reconcileRuntimeTags(runtimeService.arn(runtime, region), stringMap(prior.get("tags")), region); } publishRuntime(resource, runtime, region); diff --git a/src/test/java/io/floci/aws/services/bedrockagentcore/BedrockAgentCoreInvokeIntegrationTest.java b/src/test/java/io/floci/aws/services/bedrockagentcore/BedrockAgentCoreInvokeIntegrationTest.java index c3fb9c4295..d075e87a07 100644 --- a/src/test/java/io/floci/aws/services/bedrockagentcore/BedrockAgentCoreInvokeIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/bedrockagentcore/BedrockAgentCoreInvokeIntegrationTest.java @@ -12,7 +12,7 @@ import static org.hamcrest.Matchers.equalTo; class BedrockAgentCoreInvokeIntegrationTest { private static final String ARN = - "arn:aws:bedrock-agentcore:us-east-1:000000000000:agent/abcdef12-3456-7890-abcd-ef1234567890:1"; + "arn:aws:bedrock-agentcore:us-east-1:000000000000:runtime/test-AbCdEfGhIj"; private static final String SESSION_ID = "session-0000000000000000000000000000abc"; private static final String SESSION_HEADER = "X-Amzn-Bedrock-AgentCore-Runtime-Session-Id"; diff --git a/src/test/java/io/floci/aws/services/bedrockagentcore/BedrockAgentCoreInvokeValidateIntegrationTest.java b/src/test/java/io/floci/aws/services/bedrockagentcore/BedrockAgentCoreInvokeValidateIntegrationTest.java index 96227122bd..b091f6935b 100644 --- a/src/test/java/io/floci/aws/services/bedrockagentcore/BedrockAgentCoreInvokeValidateIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/bedrockagentcore/BedrockAgentCoreInvokeValidateIntegrationTest.java @@ -8,6 +8,8 @@ import io.restassured.path.json.JsonPath; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import java.util.UUID; + import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; @@ -52,9 +54,40 @@ class BedrockAgentCoreInvokeValidateIntegrationTest { @Test void invokeUnknownRuntimeReturns404() { String unknown = - "arn:aws:bedrock-agentcore:us-east-1:000000000000:agent/00000000-0000-0000-0000-000000000000:1"; + "arn:aws:bedrock-agentcore:us-east-1:000000000000:runtime/missing-AbCdEfGhIj"; given().contentType("application/json").body("{}") .when().post("/runtimes/" + unknown + "/invocations") .then().statusCode(404); } + + @Test + void invokeUnknownLegacyRuntimeReturns404() { + given().contentType("application/json").body("{}") + .post("/runtimes/arn:aws:bedrock-agentcore:us-east-1:000000000000:agent/unknown:1/invocations") + .then().statusCode(404); + } + + @Test + void harnessRuntimeExistsOnlyWhileItsHarnessIsRegistered() { + String name = "invoke" + UUID.randomUUID().toString().replace("-", ""); + JsonPath harness = given().contentType("application/json").body(""" + {"harnessName":"%s","executionRoleArn":"arn:aws:iam::000000000000:role/harness", + "model":{"bedrockModelConfig":{"modelId":"test-model"}}} + """.formatted(name)).post("/harnesses").then().statusCode(201).extract().jsonPath(); + String id = harness.getString("harness.harnessId"); + String arn = harness.getString("harness.environment.agentCoreRuntimeEnvironment.agentRuntimeArn"); + cleanup.register(() -> given().delete("/harnesses/" + id)); + given().contentType("application/json").body("{}").post("/runtimes/" + arn + "/invocations") + .then().statusCode(200); + given().delete("/harnesses/" + id).then().statusCode(200); + given().contentType("application/json").body("{}").post("/runtimes/" + arn + "/invocations") + .then().statusCode(404); + } + + @Test + void anInventedHarnessRuntimeDoesNotPassValidation() { + given().contentType("application/json").body("{}") + .post("/runtimes/arn:aws:bedrock-agentcore:us-east-1:000000000000:runtime/harness_missing-AbCdEfGhIj/invocations") + .then().statusCode(404); + } } diff --git a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreArnsTest.java b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreArnsTest.java index 4086ea3b9b..cbe098d4ea 100644 --- a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreArnsTest.java +++ b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreArnsTest.java @@ -9,8 +9,8 @@ class BedrockAgentCoreArnsTest { @Test void resourceDropsTheQualifier() { - assertEquals("agent/abc", - BedrockAgentCoreArns.resource("arn:aws:bedrock-agentcore:us-east-1:123456789012:agent/abc:7")); + assertEquals("runtime/test-AbCdEfGhIj", + BedrockAgentCoreArns.resource("arn:aws:bedrock-agentcore:us-east-1:123456789012:runtime/test-AbCdEfGhIj:7")); } @Test @@ -22,7 +22,7 @@ class BedrockAgentCoreArnsTest { @Test void resourceIsNullForAnythingThatIsNotAnArn() { assertNull(BedrockAgentCoreArns.resource(null)); - assertNull(BedrockAgentCoreArns.resource("agent/abc")); + assertNull(BedrockAgentCoreArns.resource("runtime/test-AbCdEfGhIj")); assertNull(BedrockAgentCoreArns.resource("arn:aws:bedrock-agentcore:us-east-1")); } } diff --git a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreConcurrencyFixture.java b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreConcurrencyFixture.java index 1e7f7be5dc..756c6a0079 100644 --- a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreConcurrencyFixture.java +++ b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreConcurrencyFixture.java @@ -32,7 +32,7 @@ final class BedrockAgentCoreConcurrencyFixture { AgentRuntime runtime = service.createAgentRuntime("probe", artifact, network, "arn:aws:iam::000000000000:role/probe", null, null, null, null, null, REGION); runtimeId = runtime.getAgentRuntimeId(); - arn = service.arn(runtime, "1", REGION); + arn = service.arn(runtime, REGION); } AgentRuntime delete() { diff --git a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlIntegrationTest.java b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlIntegrationTest.java index bb72ef3b96..5ea672d5cf 100644 --- a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlIntegrationTest.java @@ -46,7 +46,7 @@ class BedrockAgentCoreControlIntegrationTest { .body("agentRuntimeVersion", equalTo("1")) .body("status", equalTo("READY")) .body("agentRuntimeArn", containsString(":bedrock-agentcore:")) - .body("agentRuntimeArn", containsString(":agent/")) + .body("agentRuntimeArn", containsString(":runtime/")) .body("workloadIdentityDetails.workloadIdentityArn", notNullValue()) .extract().path("agentRuntimeId"); } diff --git a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlServiceTest.java b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlServiceTest.java index cbb9bc2d81..1131ff3cdd 100644 --- a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlServiceTest.java +++ b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreControlServiceTest.java @@ -66,9 +66,9 @@ class BedrockAgentCoreControlServiceTest { assertEquals(1, rt.getLatestVersion()); assertEquals("READY", rt.getStatus()); assertTrue(rt.getWorkloadIdentityArn().contains(":bedrock-agentcore:")); - assertTrue(service.arn(rt, "1", REGION) - .matches("arn:aws:bedrock-agentcore:us-east-1:000000000000:agent/[0-9a-f-]{36}:1"), - service.arn(rt, "1", REGION)); + assertTrue(service.arn(rt, REGION) + .equals("arn:aws:bedrock-agentcore:us-east-1:000000000000:runtime/" + rt.getAgentRuntimeId()), + service.arn(rt, REGION)); } @Test @@ -442,7 +442,7 @@ class BedrockAgentCoreControlServiceTest { String id = rt.getAgentRuntimeId(); AgentRuntimeEndpoint ep = service.createEndpoint(id, "prod", null, null, Map.of("env", "prod"), null, REGION); - String endpointArn = service.endpointArn(ep, REGION); + String endpointArn = service.endpointArn(id, ep, REGION); assertEquals(Map.of("env", "prod"), service.getEndpointTagsByArn(REGION, endpointArn)); @@ -451,7 +451,7 @@ class BedrockAgentCoreControlServiceTest { assertEquals(Map.of("team", "ml"), service.getEndpointTagsByArn(REGION, endpointArn)); assertEquals(Map.of("owner", "runtime"), - service.getTagsByArn(REGION, service.arn(rt, "1", REGION))); + service.getTagsByArn(REGION, service.arn(rt, REGION))); assertTrue(service.getEndpoint(id, "DEFAULT", REGION).getTags().isEmpty()); } @@ -461,7 +461,7 @@ class BedrockAgentCoreControlServiceTest { String id = rt.getAgentRuntimeId(); AgentRuntimeEndpoint ep = service.createEndpoint(id, "prod", null, null, Map.of("fixed", "1"), null, REGION); - String endpointArn = service.endpointArn(ep, REGION); + String endpointArn = service.endpointArn(id, ep, REGION); AtomicInteger inconsistent = new AtomicInteger(); try (ExecutorService executor = Executors.newFixedThreadPool(3)) { Future writer = executor.submit(() -> { @@ -503,7 +503,7 @@ class BedrockAgentCoreControlServiceTest { String id = rt.getAgentRuntimeId(); AgentRuntimeEndpoint ep = service.createEndpoint(id, "prod", null, null, Map.of("env", "prod"), null, REGION); - String endpointArn = service.endpointArn(ep, REGION); + String endpointArn = service.endpointArn(id, ep, REGION); service.deleteEndpoint(id, "prod", null, REGION); diff --git a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreEndpointIntegrationTest.java b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreEndpointIntegrationTest.java index 4376b2aefd..d1f7991be5 100644 --- a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreEndpointIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreEndpointIntegrationTest.java @@ -51,7 +51,7 @@ class BedrockAgentCoreEndpointIntegrationTest { .body("endpointName", equalTo("prod")) .body("targetVersion", equalTo("1")) .body("status", equalTo("READY")) - .body("agentRuntimeEndpointArn", containsString(":agentEndpoint/")); + .body("agentRuntimeEndpointArn", containsString("/runtime-endpoint/prod")); } @Test @@ -199,8 +199,8 @@ class BedrockAgentCoreEndpointIntegrationTest { given().when().delete("/tags/" + endpointArn + "?tagKeys=a") .then().statusCode(404).body("__type", equalTo("ResourceNotFoundException")); - String missingRuntimeArn = endpointArn.replaceAll("agentEndpoint/.*", "agent/missing0000-AbCdEfGhIj"); - String existingRuntimeArn = endpointArn.replaceAll("agentEndpoint/.*", "agent/" + id); + String missingRuntimeArn = endpointArn.replaceAll("runtime/.*", "runtime/missing0000-AbCdEfGhIj"); + String existingRuntimeArn = endpointArn.replaceAll("runtime/.*", "runtime/" + id); for (String runtimeTarget : new String[] {missingRuntimeArn, existingRuntimeArn.replace(":000000000000:", ":222222222222:"), existingRuntimeArn.replace(":us-east-1:", ":eu-west-1:"), diff --git a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCorePersistenceTest.java b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCorePersistenceTest.java index c530db4d92..088d13ac7d 100644 --- a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCorePersistenceTest.java +++ b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCorePersistenceTest.java @@ -56,7 +56,7 @@ class BedrockAgentCorePersistenceTest { "arn:aws:iam::000000000000:role/agent", "v2", Map.of("K", "V"), authorizer, null, REGION); svc.createEndpoint(id, "prod", "1", "prod ep", Map.of("stage", "live"), null, REGION); AgentRuntime before = svc.getAgentRuntime(id, REGION); - svc.tagByArn(REGION, svc.arn(before, "1", REGION), Map.of("env", "prod")); + svc.tagByArn(REGION, svc.arn(before, REGION), Map.of("env", "prod")); before = svc.getAgentRuntime(id, REGION); // Write to disk, then reload into a brand-new backend (simulated restart). diff --git a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreRuntimeArnTest.java b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreRuntimeArnTest.java new file mode 100644 index 0000000000..a333bce7f7 --- /dev/null +++ b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreRuntimeArnTest.java @@ -0,0 +1,197 @@ +package io.floci.aws.services.bedrockagentcorecontrol; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.floci.aws.core.common.AwsException; +import io.floci.aws.core.common.RegionResolver; +import io.floci.aws.core.storage.InMemoryStorage; +import io.floci.aws.services.bedrockagentcorecontrol.model.AgentRuntime; +import io.floci.aws.services.bedrockagentcorecontrol.model.AgentRuntimeEndpoint; +import io.floci.aws.services.cloudformation.CloudFormationTemplateEngine; +import io.floci.aws.services.cloudformation.model.StackResource; +import io.floci.aws.services.cloudformation.provisioners.BedrockAgentCoreCfnProvisioner; +import io.floci.aws.services.cloudformation.provisioners.ProvisionContext; +import io.floci.aws.testing.MutableClock; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; + +class BedrockAgentCoreRuntimeArnTest { + + private static final String REGION = "us-east-1"; + private static final String PREFIX = "arn:aws:bedrock-agentcore:us-east-1:000000000000:runtime/"; + private final ObjectMapper mapper = new ObjectMapper(); + private final RegionResolver resolver = new RegionResolver(REGION, "000000000000"); + private final BedrockAgentCoreControlService service = new BedrockAgentCoreControlService( + new InMemoryStorage<>(), resolver, null, new MutableClock()); + + private ObjectNode artifact() { + return mapper.createObjectNode().set("containerConfiguration", + mapper.createObjectNode().put("containerUri", "123456789012.dkr.ecr/x:latest")); + } + + private ObjectNode network() { + return mapper.createObjectNode().put("networkMode", "PUBLIC"); + } + + private AgentRuntime create(String name) { + return service.createAgentRuntime(name, artifact(), network(), + "arn:aws:iam::000000000000:role/agent", null, null, null, null, null, REGION); + } + + @Test + void runtimeArnUsesTheRuntimeIdAndStaysStableAcrossVersions() { + AgentRuntime runtime = create("versioned"); + String expected = PREFIX + runtime.getAgentRuntimeId(); + assertEquals(expected, service.arn(runtime, REGION)); + AgentRuntime updated = service.updateAgentRuntime(runtime.getAgentRuntimeId(), artifact(), network(), + "arn:aws:iam::000000000000:role/agent", null, null, null, null, REGION); + assertEquals(expected, service.arn(updated, REGION)); + } + + @Test + void runtimeArnResolvesItsIdAndChecksDeletedRuntimes() { + AgentRuntime runtime = create("lookup"); + String arn = PREFIX + runtime.getAgentRuntimeId(); + assertEquals(runtime.getAgentRuntimeId(), service.getAgentRuntimeByArn(REGION, arn).getAgentRuntimeId()); + assertTrue(service.runtimeArnExists(REGION, arn)); + service.deleteAgentRuntime(runtime.getAgentRuntimeId(), null, REGION); + assertFalse(service.runtimeArnExists(REGION, arn)); + } + + @Test + void missingRuntimeArnDoesNotPassTheExistenceCheck() { + assertFalse(service.runtimeArnExists(REGION, PREFIX + "missing-AbCdEfGhIj")); + } + + @Test + void endpointArnIncludesItsParentAndNameIncludingDefault() { + AgentRuntime runtime = create("endpoints"); + AgentRuntimeEndpoint endpoint = service.createEndpoint(runtime.getAgentRuntimeId(), "prod", + null, null, null, REGION); + assertEquals(PREFIX + runtime.getAgentRuntimeId() + "/runtime-endpoint/prod", + service.endpointArn(runtime.getAgentRuntimeId(), endpoint, REGION)); + AgentRuntimeEndpoint defaultEndpoint = service.getEndpoint(runtime.getAgentRuntimeId(), "DEFAULT", REGION); + assertEquals(PREFIX + runtime.getAgentRuntimeId() + "/runtime-endpoint/DEFAULT", + service.endpointArn(runtime.getAgentRuntimeId(), defaultEndpoint, REGION)); + } + + @Test + void endpointLookupUsesTheParentWhenNamesRepeat() { + AgentRuntime first = create("first"); + AgentRuntime second = create("second"); + service.createEndpoint(first.getAgentRuntimeId(), "prod", null, null, null, REGION); + service.createEndpoint(second.getAgentRuntimeId(), "prod", null, null, null, REGION); + String arn = PREFIX + second.getAgentRuntimeId() + "/runtime-endpoint/prod"; + BedrockAgentCoreControlService.EndpointLocation located = service.findEndpointByArn(REGION, arn).orElseThrow(); + assertEquals(second.getAgentRuntimeId(), located.runtime().getAgentRuntimeId()); + assertEquals("prod", located.endpoint().getName()); + assertTrue(service.findEndpointByArn("us-west-2", arn).isEmpty()); + service.deleteEndpoint(second.getAgentRuntimeId(), "prod", null, REGION); + assertTrue(service.findEndpointByArn(REGION, arn).isEmpty()); + } + + @Test + void tagHandlerDispatchesRuntimeAndNestedEndpointArnsSeparately() { + AgentRuntime runtime = create("tagged"); + service.createEndpoint(runtime.getAgentRuntimeId(), "prod", null, null, null, REGION); + String runtimeArn = PREFIX + runtime.getAgentRuntimeId(); + String endpointArn = runtimeArn + "/runtime-endpoint/prod"; + BedrockAgentCoreTagHandler handler = new BedrockAgentCoreTagHandler(service, + mock(BedrockAgentCoreGatewayService.class), mock(BedrockAgentCoreMemoryService.class), + mock(BedrockAgentCoreHarnessRegistry.class), resolver); + handler.tagResource(REGION, runtimeArn, Map.of("owner", "runtime")); + handler.tagResource(REGION, endpointArn, Map.of("owner", "endpoint")); + assertEquals(Map.of("owner", "runtime"), handler.listTags(REGION, runtimeArn)); + assertEquals(Map.of("owner", "endpoint"), handler.listTags(REGION, endpointArn)); + handler.untagResource(REGION, endpointArn, List.of("owner")); + assertTrue(handler.listTags(REGION, endpointArn).isEmpty()); + assertEquals(Map.of("owner", "runtime"), handler.listTags(REGION, runtimeArn)); + } + + @Test + void unknownLegacyRuntimeDoesNotPassExistenceValidation() { + assertFalse(service.runtimeArnExists(REGION, + resolver.buildArn("bedrock-agentcore", REGION, "agent/missing-uuid:1"))); + } + + @Test + void legacyRuntimeExistenceFollowsItsStoredUuidAndDeletion() { + AgentRuntime runtime = create("legacyRuntime"); + String arn = resolver.buildArn("bedrock-agentcore", REGION, "agent/" + runtime.getUuid() + ":1"); + assertTrue(service.runtimeArnExists(REGION, arn)); + service.deleteAgentRuntime(runtime.getAgentRuntimeId(), null, REGION); + assertFalse(service.runtimeArnExists(REGION, arn)); + } + + @Test + void legacyRuntimeAndEndpointTagsRemainSeparate() { + AgentRuntime runtime = create("legacyTags"); + AgentRuntimeEndpoint endpoint = service.createEndpoint(runtime.getAgentRuntimeId(), "prod", + null, null, null, REGION); + String runtimeArn = resolver.buildArn("bedrock-agentcore", REGION, "agent/" + runtime.getUuid() + ":1"); + String endpointArn = resolver.buildArn("bedrock-agentcore", REGION, "agentEndpoint/" + endpoint.getUuid()); + BedrockAgentCoreTagHandler handler = new BedrockAgentCoreTagHandler(service, + mock(BedrockAgentCoreGatewayService.class), mock(BedrockAgentCoreMemoryService.class), + mock(BedrockAgentCoreHarnessRegistry.class), resolver); + handler.tagResource(REGION, runtimeArn, Map.of("scope", "runtime")); + handler.tagResource(REGION, endpointArn, Map.of("scope", "endpoint")); + assertEquals(Map.of("scope", "runtime"), handler.listTags(REGION, runtimeArn)); + assertEquals(Map.of("scope", "endpoint"), handler.listTags(REGION, endpointArn)); + assertEquals(runtime.getAgentRuntimeId(), service.getAgentRuntimeByArn(REGION, runtimeArn).getAgentRuntimeId()); + } + + @Test + void legacyEndpointLookupResolvesItsUuidAndDisappearsAfterDeletion() { + AgentRuntime runtime = create("legacyLookup"); + AgentRuntimeEndpoint endpoint = service.createEndpoint(runtime.getAgentRuntimeId(), "prod", + null, null, null, REGION); + String arn = resolver.buildArn("bedrock-agentcore", REGION, "agentEndpoint/" + endpoint.getUuid()); + assertEquals(runtime.getAgentRuntimeId(), service.findEndpointByArn(REGION, arn).orElseThrow() + .runtime().getAgentRuntimeId()); + assertTrue(service.findEndpointByArn("us-west-2", arn).isEmpty()); + service.deleteEndpoint(runtime.getAgentRuntimeId(), "prod", null, REGION); + assertTrue(service.findEndpointByArn(REGION, arn).isEmpty()); + } + + @Test + void cloudFormationUpdatesAnEndpointWithAPersistedLegacyPhysicalId() { + AgentRuntime runtime = create("legacyUpdate"); + AgentRuntimeEndpoint endpoint = service.createEndpoint(runtime.getAgentRuntimeId(), "prod", + null, "old", null, REGION); + String arn = resolver.buildArn("bedrock-agentcore", REGION, "agentEndpoint/" + endpoint.getUuid()); + StackResource resource = new StackResource(); + resource.setLogicalId("Endpoint"); + resource.setResourceType("AWS::BedrockAgentCore::RuntimeEndpoint"); + resource.setPhysicalId(arn); + resource.setAttributes(new HashMap<>()); + ObjectNode props = mapper.createObjectNode().put("AgentRuntimeId", runtime.getAgentRuntimeId()) + .put("Name", "prod").put("Description", "new"); + CloudFormationTemplateEngine engine = CloudFormationTemplateEngine.standalone("000000000000", REGION, + "legacy-stack", mapper, value -> value); + new BedrockAgentCoreCfnProvisioner(service, mock(BedrockAgentCoreMemoryService.class)).provision( + resource, props, new ProvisionContext(engine, REGION, "000000000000", "legacy-stack", arn)); + assertEquals(service.endpointArn(runtime.getAgentRuntimeId(), endpoint, REGION), resource.getPhysicalId()); + assertEquals("new", service.getEndpoint(runtime.getAgentRuntimeId(), "prod", REGION).getDescription()); + assertEquals(2, service.getAgentRuntime(runtime.getAgentRuntimeId(), REGION).getEndpoints().size()); + } + + @Test + void cloudFormationDeletesAnEndpointWithAPersistedLegacyPhysicalId() { + AgentRuntime runtime = create("legacyDelete"); + AgentRuntimeEndpoint endpoint = service.createEndpoint(runtime.getAgentRuntimeId(), "prod", + null, null, null, REGION); + String arn = resolver.buildArn("bedrock-agentcore", REGION, "agentEndpoint/" + endpoint.getUuid()); + new BedrockAgentCoreCfnProvisioner(service, mock(BedrockAgentCoreMemoryService.class)) + .delete("AWS::BedrockAgentCore::RuntimeEndpoint", arn, REGION); + assertThrows(AwsException.class, () -> service.getEndpoint(runtime.getAgentRuntimeId(), "prod", REGION)); + } +} diff --git a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandlerTest.java b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandlerTest.java index a27ba52a6f..cb5e0f8e9b 100644 --- a/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandlerTest.java +++ b/src/test/java/io/floci/aws/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandlerTest.java @@ -24,7 +24,7 @@ class BedrockAgentCoreTagHandlerTest { private static String endpointArn(String partition) { return "arn:" + partition + ":bedrock-agentcore:" + CUSTOM_REGION + ":" + ACCOUNT - + ":agentEndpoint/abc123"; + + ":runtime/test-AbCdEfGhIj/runtime-endpoint/prod"; } @Test diff --git a/src/test/java/io/floci/aws/services/cloudformation/CloudFormationBedrockAgentCoreIntegrationTest.java b/src/test/java/io/floci/aws/services/cloudformation/CloudFormationBedrockAgentCoreIntegrationTest.java index 0fbdc1ef24..304b537d5c 100644 --- a/src/test/java/io/floci/aws/services/cloudformation/CloudFormationBedrockAgentCoreIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/cloudformation/CloudFormationBedrockAgentCoreIntegrationTest.java @@ -210,7 +210,7 @@ class CloudFormationBedrockAgentCoreIntegrationTest { String runtimeArn = outputs.get("RuntimeArn"); assertEquals(runtimeId, outputs.get("RuntimeRef")); assertTrue(runtimeId.startsWith("cfn_agent_runtime-"), runtimeId); - assertTrue(runtimeArn.matches("arn:aws:bedrock-agentcore:us-east-1:000000000000:agent/[0-9a-f-]{36}:1"), + assertTrue(runtimeArn.matches("arn:aws:bedrock-agentcore:us-east-1:000000000000:runtime/[a-zA-Z][a-zA-Z0-9_]{0,47}-[a-zA-Z0-9]{10}"), runtimeArn); assertEquals("1", outputs.get("RuntimeVersion")); assertEquals("READY", outputs.get("RuntimeStatus")); @@ -219,7 +219,7 @@ class CloudFormationBedrockAgentCoreIntegrationTest { String endpointArn = outputs.get("EndpointArn"); assertEquals(endpointArn, outputs.get("EndpointRef")); - assertTrue(endpointArn.startsWith("arn:aws:bedrock-agentcore:us-east-1:000000000000:agentEndpoint/"), endpointArn); + assertEquals(runtimeArn + "/runtime-endpoint/prod", endpointArn); assertEquals(runtimeArn, outputs.get("EndpointRuntimeArn")); assertEquals("1", outputs.get("EndpointTargetVersion")); assertEquals("READY", outputs.get("EndpointStatus")); diff --git a/src/test/java/io/floci/aws/services/cloudformation/provisioners/BedrockAgentCoreCfnProvisionerTest.java b/src/test/java/io/floci/aws/services/cloudformation/provisioners/BedrockAgentCoreCfnProvisionerTest.java index c605b39e6f..285b768051 100644 --- a/src/test/java/io/floci/aws/services/cloudformation/provisioners/BedrockAgentCoreCfnProvisionerTest.java +++ b/src/test/java/io/floci/aws/services/cloudformation/provisioners/BedrockAgentCoreCfnProvisionerTest.java @@ -46,7 +46,7 @@ class BedrockAgentCoreCfnProvisionerTest { private static final String REGION = "us-east-1"; private static final String RUNTIME_ID = "agent-abcdefghij"; - private static final String RUNTIME_ARN = "arn:aws:bedrock-agentcore:us-east-1:000000000000:agent/uuid:1"; + private static final String RUNTIME_ARN = "arn:aws:bedrock-agentcore:us-east-1:000000000000:runtime/" + RUNTIME_ID; private static final String MEMORY_ID = "mem-abcdefghij"; private static final String MEMORY_ARN = "arn:aws:bedrock-agentcore:us-east-1:000000000000:memory/" + MEMORY_ID; private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/agent"; @@ -127,7 +127,7 @@ class BedrockAgentCoreCfnProvisionerTest { void aFnIfSelectingNoValueInsideNetworkConfigurationIsOmitted() { when(runtimes.createAgentRuntime(any(), any(), any(), any(), any(), any(), any(), any(), any(), any())) .thenReturn(runtime(null, Map.of())); - when(runtimes.arn(any(), any(), eq(REGION))).thenReturn(RUNTIME_ARN); + when(runtimes.arn(any(), eq(REGION))).thenReturn(RUNTIME_ARN); ObjectNode props = runtimeProps(mapper, null, "agents"); // No condition is defined, so Fn::If takes its else branch, as an undefined condition does. props.set("NetworkConfiguration", json(""" @@ -148,7 +148,7 @@ class BedrockAgentCoreCfnProvisionerTest { void aFailedTaggingAfterCreateDeletesTheRuntimeBeforePropagating() { when(runtimes.createAgentRuntime(any(), any(), any(), any(), any(), any(), any(), any(), any(), any())) .thenReturn(runtime(null, Map.of())); - when(runtimes.arn(any(), any(), eq(REGION))).thenReturn(RUNTIME_ARN); + when(runtimes.arn(any(), eq(REGION))).thenReturn(RUNTIME_ARN); doThrow(new AwsException("ThrottlingException", "slow down", 429)) .when(runtimes).tagByArn(eq(REGION), eq(RUNTIME_ARN), anyMap()); StackResource r = resource("AWS::BedrockAgentCore::Runtime"); @@ -173,7 +173,7 @@ class BedrockAgentCoreCfnProvisionerTest { endpoint.setCreatedAt(Instant.EPOCH); endpoint.setLastUpdatedAt(Instant.EPOCH); when(runtimes.createEndpoint(RUNTIME_ID, "prod", null, null, null, REGION)).thenReturn(endpoint); - when(runtimes.endpointArn(endpoint, REGION)).thenReturn("arn:ep"); + when(runtimes.endpointArn(RUNTIME_ID, endpoint, REGION)).thenReturn("arn:ep"); when(runtimes.getAgentRuntime(RUNTIME_ID, REGION)) .thenThrow(new AwsException("InternalServerException", "boom", 500)); ObjectNode props = mapper.createObjectNode(); @@ -190,7 +190,7 @@ class BedrockAgentCoreCfnProvisionerTest { void rollbackUpdateRestoresAnInPlaceRuntimeChangeAsANewVersion() { AgentRuntime prior = runtime("one", Map.of("K", "v")); when(runtimes.getAgentRuntime(RUNTIME_ID, REGION)).thenReturn(prior); - when(runtimes.arn(any(), any(), eq(REGION))).thenReturn(RUNTIME_ARN); + when(runtimes.arn(any(), eq(REGION))).thenReturn(RUNTIME_ARN); when(runtimes.getTagsByArn(REGION, RUNTIME_ARN)).thenReturn(new HashMap<>(Map.of("team", "agents"))); when(runtimes.updateAgentRuntime(eq(RUNTIME_ID), any(), any(), any(), any(), any(), any(), any(), eq(REGION))) .thenReturn(prior); @@ -216,7 +216,7 @@ class BedrockAgentCoreCfnProvisionerTest { void anUnchangedRuntimeIsNeitherUpdatedNorSnapshotted() { AgentRuntime prior = runtime("one", Map.of()); when(runtimes.getAgentRuntime(RUNTIME_ID, REGION)).thenReturn(prior); - when(runtimes.arn(any(), any(), eq(REGION))).thenReturn(RUNTIME_ARN); + when(runtimes.arn(any(), eq(REGION))).thenReturn(RUNTIME_ARN); when(runtimes.getTagsByArn(REGION, RUNTIME_ARN)).thenReturn(new HashMap<>(Map.of("team", "agents"))); StackResource r = resource("AWS::BedrockAgentCore::Runtime"); r.setPhysicalId(RUNTIME_ID); @@ -271,8 +271,8 @@ class BedrockAgentCoreCfnProvisionerTest { .thenReturn(Optional.of(new BedrockAgentCoreControlService.EndpointLocation(runtime, existing))); when(runtimes.updateEndpoint(eq(RUNTIME_ID), eq("prod"), any(), any(), anyBoolean(), eq(REGION))) .thenReturn(existing); - when(runtimes.endpointArn(existing, REGION)).thenReturn("arn:ep"); - when(runtimes.arn(any(), anyString(), eq(REGION))).thenReturn(RUNTIME_ARN); + when(runtimes.endpointArn(RUNTIME_ID, existing, REGION)).thenReturn("arn:ep"); + when(runtimes.arn(any(), eq(REGION))).thenReturn(RUNTIME_ARN); ObjectNode props = mapper.createObjectNode(); props.put("AgentRuntimeId", RUNTIME_ID); props.put("Name", "prod");