mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(bedrock-agentcore): use runtime IDs and endpoint names in ARNs (#8088)
* fix(bedrock-agentcore): use runtime IDs and endpoint names in ARNs Keep runtime ARNs stable across versions and use the parent runtime ID and endpoint name for endpoint ARNs. Update API responses, ARN lookups, tagging dispatch and CloudFormation references together. * fix(bedrock-agentcore): preserve legacy and harness runtime lookups
This commit is contained in:
1 parent
8bdf86420b
commit
f2a0bfce4e
23 files changed
+360
-94
No files matched your search
+2
-3
@@ -12,9 +12,8 @@ final class BedrockAgentCoreArns {
|
||||
}
|
||||
|
||||
/**
|
||||
* The resource of an AgentCore ARN without its qualifier: {@code agent/<uuid>} for
|
||||
* {@code arn:aws:bedrock-agentcore:<region>:<account>:agent/<uuid>:<version>}. 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)) {
|
||||
|
||||
+7
-7
@@ -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());
|
||||
|
||||
+58
-40
@@ -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<Map<String, AgentRuntime>>() {}), regionResolver, identityService, clock);
|
||||
new TypeReference<Map<String, AgentRuntime>>() {}), regionResolver, identityService, clock, harnessRegistry);
|
||||
}
|
||||
|
||||
BedrockAgentCoreControlService(StorageBackend<String, AgentRuntime> storage, RegionResolver regionResolver) {
|
||||
@@ -78,9 +81,16 @@ public class BedrockAgentCoreControlService {
|
||||
|
||||
BedrockAgentCoreControlService(StorageBackend<String, AgentRuntime> storage, RegionResolver regionResolver,
|
||||
BedrockAgentCoreIdentityService identityService, Clock clock) {
|
||||
this(storage, regionResolver, identityService, clock, null);
|
||||
}
|
||||
|
||||
BedrockAgentCoreControlService(StorageBackend<String, AgentRuntime> 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<AgentRuntime> 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:<region>:<account>:agent/<uuid>:<version>
|
||||
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/<uuid>}) 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<EndpointLocation> 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<AgentRuntime> 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<EndpointLocation> 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<EndpointLocation> findEndpoint(String region, String runtimeId,
|
||||
Predicate<AgentRuntimeEndpoint> 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) {
|
||||
|
||||
+7
@@ -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<Harness> list(Integer maxResults, String nextToken, String region) {
|
||||
List<Harness> all = storage.scan(key -> key.startsWith(keyPrefix(region)));
|
||||
return Pagination.paginate(all, Harness::getHarnessId, maxResults, nextToken, MAX_PAGE, "ValidationException");
|
||||
|
||||
+5
-2
@@ -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;
|
||||
|
||||
+6
-6
@@ -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<String, String> 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);
|
||||
|
||||
Reference in new issue
Block a user