mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
refactor: read and check ARNs through AwsArnUtils in Bedrock, AgentCore and SageMaker (#6970)
The arn-probe and arn-split sites in these services now go through looksLikeArn, isArn, parse and regionOrDefault. The four AgentCore services share BedrockAgentCoreArns.resource, which reads an ARN's resource without its qualifier. A well-formed ARN reads the same as before. tools/partition/baseline.tsv drops from 44 to 34. Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
98a3d2a346
commit
5bf89a5061
9 files changed
+85
-35
No files matched your search
@@ -333,7 +333,7 @@ public class BedrockService implements ResourceProvider {
|
||||
return null;
|
||||
}
|
||||
String value = identifier.asText();
|
||||
String profileId = value.startsWith("arn:")
|
||||
String profileId = AwsArnUtils.looksLikeArn(value)
|
||||
? value.substring(value.lastIndexOf('/') + 1)
|
||||
: value;
|
||||
ObjectNode details = objectMapper.createObjectNode();
|
||||
@@ -413,7 +413,7 @@ public class BedrockService implements ResourceProvider {
|
||||
}
|
||||
String guardrailId = resolveGuardrailId(resourceArn);
|
||||
return guardrails.get(storageKey(region, guardrailId, DRAFT_VERSION))
|
||||
.filter(guardrail -> !resourceArn.startsWith("arn:") || resourceArn.equals(guardrail.getGuardrailArn()))
|
||||
.filter(guardrail -> !AwsArnUtils.looksLikeArn(resourceArn) || resourceArn.equals(guardrail.getGuardrailArn()))
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"Resource " + resourceArn + " does not exist.", 404));
|
||||
}
|
||||
@@ -422,7 +422,7 @@ public class BedrockService implements ResourceProvider {
|
||||
if (identifier == null || identifier.isBlank()) {
|
||||
throw new AwsException("ValidationException", "guardrailIdentifier is required", 400);
|
||||
}
|
||||
if (!identifier.startsWith("arn:")) {
|
||||
if (!AwsArnUtils.looksLikeArn(identifier)) {
|
||||
return identifier;
|
||||
}
|
||||
String resource;
|
||||
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package io.github.hectorvent.floci.services.bedrockagentcorecontrol;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
|
||||
/**
|
||||
* Reads the resource out of an AgentCore ARN for the service classes under this package that look
|
||||
* a runtime, endpoint, gateway or memory up by ARN.
|
||||
*/
|
||||
final class BedrockAgentCoreArns {
|
||||
|
||||
private 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.
|
||||
*/
|
||||
static String resource(String arn) {
|
||||
if (!AwsArnUtils.isArn(arn)) {
|
||||
return null;
|
||||
}
|
||||
String resource = AwsArnUtils.parse(arn).resource();
|
||||
int qualifier = resource.indexOf(':');
|
||||
return qualifier < 0 ? resource : resource.substring(0, qualifier);
|
||||
}
|
||||
}
|
||||
+9
-9
@@ -280,11 +280,11 @@ public class BedrockAgentCoreControlService {
|
||||
* validated here — the data plane treats them permissively.
|
||||
*/
|
||||
public boolean runtimeArnExists(String region, String arn) {
|
||||
String[] parts = arn == null ? new String[0] : arn.split(":");
|
||||
if (parts.length < 6 || !parts[5].startsWith("agent/")) {
|
||||
String resource = BedrockAgentCoreArns.resource(arn);
|
||||
if (resource == null || !resource.startsWith("agent/")) {
|
||||
return true;
|
||||
}
|
||||
String uuid = parts[5].substring("agent/".length());
|
||||
String uuid = resource.substring("agent/".length());
|
||||
String prefix = keyPrefix(region);
|
||||
return storage.scan(k -> k.startsWith(prefix)).stream()
|
||||
.anyMatch(r -> uuid.equals(r.getUuid()));
|
||||
@@ -325,11 +325,11 @@ public class BedrockAgentCoreControlService {
|
||||
|
||||
private static String uuidFromArn(String arn) {
|
||||
// arn:aws:bedrock-agentcore:<region>:<account>:agent/<uuid>:<version>
|
||||
String[] parts = arn == null ? new String[0] : arn.split(":");
|
||||
if (parts.length < 6 || !parts[5].startsWith("agent/")) {
|
||||
String resource = BedrockAgentCoreArns.resource(arn);
|
||||
if (resource == null || !resource.startsWith("agent/")) {
|
||||
throw new AwsException("ValidationException", "Unsupported resource ARN: " + arn, 400);
|
||||
}
|
||||
return parts[5].substring("agent/".length());
|
||||
return resource.substring("agent/".length());
|
||||
}
|
||||
|
||||
// ──────────────────────────── Endpoints (Phase 2) ────────────────────────────
|
||||
@@ -453,11 +453,11 @@ public class BedrockAgentCoreControlService {
|
||||
* API, so this is the only way back from the ARN CloudFormation keeps as the physical id.
|
||||
*/
|
||||
public Optional<EndpointLocation> findEndpointByArn(String region, String arn) {
|
||||
String[] parts = arn == null ? new String[0] : arn.split(":");
|
||||
if (parts.length < 6 || !parts[5].startsWith("agentEndpoint/")) {
|
||||
String resource = BedrockAgentCoreArns.resource(arn);
|
||||
if (resource == null || !resource.startsWith("agentEndpoint/")) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String uuid = parts[5].substring("agentEndpoint/".length());
|
||||
String uuid = resource.substring("agentEndpoint/".length());
|
||||
String prefix = keyPrefix(region);
|
||||
for (AgentRuntime runtime : storage.scan(k -> k.startsWith(prefix))) {
|
||||
for (AgentRuntimeEndpoint endpoint : runtime.getEndpoints()) {
|
||||
|
||||
+3
-3
@@ -99,11 +99,11 @@ public class BedrockAgentCoreGatewayService {
|
||||
}
|
||||
|
||||
private Gateway findByArn(String region, String arn) {
|
||||
String[] parts = arn == null ? new String[0] : arn.split(":");
|
||||
if (parts.length < 6 || !parts[5].startsWith("gateway/")) {
|
||||
String resource = BedrockAgentCoreArns.resource(arn);
|
||||
if (resource == null || !resource.startsWith("gateway/")) {
|
||||
throw new AwsException("ValidationException", "Unsupported resource ARN: " + arn, 400);
|
||||
}
|
||||
return get(parts[5].substring("gateway/".length()), region);
|
||||
return get(resource.substring("gateway/".length()), region);
|
||||
}
|
||||
|
||||
public Gateway get(String id, String region) {
|
||||
|
||||
+3
-3
@@ -226,11 +226,11 @@ public class BedrockAgentCoreMemoryService {
|
||||
|
||||
/** The memory a memory ARN names; ValidationException for any other ARN, ResourceNotFoundException when gone. */
|
||||
public Memory getByArn(String region, String arn) {
|
||||
String[] parts = arn == null ? new String[0] : arn.split(":");
|
||||
if (parts.length < 6 || !parts[5].startsWith("memory/")) {
|
||||
String resource = BedrockAgentCoreArns.resource(arn);
|
||||
if (resource == null || !resource.startsWith("memory/")) {
|
||||
throw new AwsException("ValidationException", "Unsupported resource ARN: " + arn, 400);
|
||||
}
|
||||
return get(parts[5].substring("memory/".length()), region);
|
||||
return get(resource.substring("memory/".length()), region);
|
||||
}
|
||||
|
||||
private static String sanitize(String name) {
|
||||
|
||||
+9
-8
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.bedrockagentcorecontrol;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.TagHandler;
|
||||
@@ -69,19 +70,19 @@ public class BedrockAgentCoreTagHandler implements TagHandler {
|
||||
private enum ResourceKind { RUNTIME, GATEWAY, MEMORY }
|
||||
|
||||
private ResourceKind kind(String region, String arn) {
|
||||
String[] parts = arn == null ? new String[0] : arn.split(":");
|
||||
if (parts.length < 6) {
|
||||
String resource = BedrockAgentCoreArns.resource(arn);
|
||||
if (resource == null) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Tagging is not supported for this AgentCore resource: " + arn, 400);
|
||||
}
|
||||
requireLocalIdentity(region, arn, parts);
|
||||
if (parts[5].startsWith("gateway/")) {
|
||||
requireLocalIdentity(region, arn, AwsArnUtils.parse(arn));
|
||||
if (resource.startsWith("gateway/")) {
|
||||
return ResourceKind.GATEWAY;
|
||||
}
|
||||
if (parts[5].startsWith("agent/")) {
|
||||
if (resource.startsWith("agent/")) {
|
||||
return ResourceKind.RUNTIME;
|
||||
}
|
||||
if (parts[5].startsWith("memory/")) {
|
||||
if (resource.startsWith("memory/")) {
|
||||
return ResourceKind.MEMORY;
|
||||
}
|
||||
// Not a runtime, gateway, or memory → not a taggable AgentCore resource.
|
||||
@@ -95,8 +96,8 @@ public class BedrockAgentCoreTagHandler implements TagHandler {
|
||||
* account or region and read or change its tags. Reported as not found rather than a
|
||||
* validation error, so the response does not confirm what exists behind the foreign identity.
|
||||
*/
|
||||
private void requireLocalIdentity(String region, String arn, String[] parts) {
|
||||
if (!region.equals(parts[3]) || !regionResolver.getAccountId().equals(parts[4])) {
|
||||
private void requireLocalIdentity(String region, String arn, AwsArnUtils.Arn parsed) {
|
||||
if (!region.equals(parsed.region()) || !regionResolver.getAccountId().equals(parsed.accountId())) {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"AgentCore resource not found: " + arn, 404);
|
||||
}
|
||||
|
||||
@@ -547,11 +547,11 @@ public class SageMakerService {
|
||||
|
||||
/** ARN shape: {@code arn:aws:sagemaker:<region>:<account>:<type>/<name>}. */
|
||||
private static String arnRegion(String arn) {
|
||||
String[] parts = arn.split(":", 6);
|
||||
if (parts.length < 4 || parts[3].isBlank()) {
|
||||
String region = AwsArnUtils.regionOrDefault(arn, "");
|
||||
if (region.isBlank()) {
|
||||
throw validation("ResourceArn is invalid");
|
||||
}
|
||||
return parts[3];
|
||||
return region;
|
||||
}
|
||||
|
||||
private static String regionKey(String region, String name) {
|
||||
|
||||
Reference in new issue
Block a user