mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(iam): read Bedrock from the generated tables, its routes vendored (#9169)
Bedrock leaves ResourceArnBuilder's own branch and the registry's three resource-policy rules: its requests are matched on their botocore routes and checked on the resources the request-resources table reads. regen_rest_routes.py vendors the bedrock, bedrock-agent and bedrock-runtime models, all signed for the bedrock scope; bedrock-agent's are needed because its PutResourcePolicy carries resourceArn in its path where bedrock's carries it in its body, a member the two clients locate apart. regen_iam_request_resources.py reads bedrock instead of leaving it to the builder, so a guardrail request is checked on the guardrail its path names and a resource-policy request on the ARN it carries. The generator's reader of an ARN pattern's type words also reads one whose account is optional, so the tag actions are checked on the guardrail or application inference profile ARN they carry, and regen_iam_tag_on_create.py reads bedrock, so a create carrying tags also needs bedrock:TagResource. BedrockStoredResourceArns gives IAM the tags Bedrock holds on a guardrail or an application inference profile, for aws:ResourceTag, read in place and matched on the whole ARN; a guardrail's untag now replaces its tag map rather than editing it, so a reader never sees half of one, and both models keep their tags volatile.
This commit is contained in:
1 parent
cff222a6f1
commit
5b4bc0e44e
17 files changed
+847
-92
No files matched your search
@@ -15,6 +15,7 @@ import io.floci.aws.core.common.SmithyValidationErrors;
|
||||
import io.floci.aws.core.resource.ExplorerResource;
|
||||
import io.floci.aws.core.resource.ResourceProvider;
|
||||
import io.floci.aws.core.resource.SupportedResourceType;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.bedrock.model.ApplicationInferenceProfile;
|
||||
@@ -698,12 +699,37 @@ public class BedrockService implements ResourceProvider {
|
||||
return;
|
||||
}
|
||||
Guardrail guardrail = findByArn(resourceArn, region);
|
||||
if (guardrail.getTags() != null && tagKeys != null) {
|
||||
tagKeys.forEach(guardrail.getTags()::remove);
|
||||
Map<String, String> remaining = new HashMap<>(guardrail.getTags() != null ? guardrail.getTags() : Map.of());
|
||||
if (tagKeys != null) {
|
||||
tagKeys.forEach(remaining::remove);
|
||||
}
|
||||
guardrail.setTags(remaining);
|
||||
guardrails.put(storageKey(region, guardrail.getGuardrailId(), DRAFT_VERSION), guardrail);
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the guardrail or application inference profile whose ARN is {@code arn} in {@code account}, read in
|
||||
* place under the key the tag handlers read, in the ARN's Region, and matched on the whole ARN it records; none
|
||||
* when there is no such resource. Tag calls give the resource a new map, so the read is of one whole set.
|
||||
*/
|
||||
Map<String, String> tagsInAccount(String account, String arn) {
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
String resource = parsed.resource();
|
||||
Optional<Map<String, String>> tags = Optional.empty();
|
||||
if (resource.startsWith(GUARDRAIL_RESOURCE_PREFIX)) {
|
||||
tags = AccountAwareStorageBackend.readInAccount(guardrails, account,
|
||||
storageKey(parsed.region(), resource.substring(GUARDRAIL_RESOURCE_PREFIX.length()), DRAFT_VERSION))
|
||||
.filter(guardrail -> arn.equals(guardrail.getGuardrailArn())).map(Guardrail::getTags);
|
||||
} else if (resource.startsWith(APPLICATION_INFERENCE_PROFILE_PREFIX)) {
|
||||
tags = AccountAwareStorageBackend.readInAccount(applicationInferenceProfiles, account,
|
||||
applicationProfileKey(parsed.region(),
|
||||
resource.substring(APPLICATION_INFERENCE_PROFILE_PREFIX.length())))
|
||||
.filter(profile -> arn.equals(profile.getInferenceProfileArn()))
|
||||
.map(ApplicationInferenceProfile::getTags);
|
||||
}
|
||||
return tags.orElse(Map.of());
|
||||
}
|
||||
|
||||
// Resource discovery (Resource Groups Tagging API, Resource Explorer)
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package io.floci.aws.services.bedrock;
|
||||
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* IAM's lookup of the tags Amazon Bedrock holds on a guardrail or an application inference profile, for the
|
||||
* {@code bedrock} namespace.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class BedrockStoredResourceArns implements StoredResourceArns {
|
||||
|
||||
private static final String NAMESPACE = "bedrock";
|
||||
|
||||
private final BedrockService bedrock;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
@Inject
|
||||
public BedrockStoredResourceArns(BedrockService bedrock, RegionResolver regionResolver) {
|
||||
this.bedrock = bedrock;
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return NAMESPACE;
|
||||
}
|
||||
|
||||
/** A request names what it acts on by its id or its ARN: nothing to find. */
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the guardrail or application inference profile whose ARN is {@code arn}, in the request's account,
|
||||
* where the tag handlers read: an ARN of another account names none, and neither does one of a type Floci holds
|
||||
* no tags on. The resource is read in place and matched on the whole ARN it records.
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
if (!AwsArnUtils.isArnFor(arn, NAMESPACE)) {
|
||||
return Map.of();
|
||||
}
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
String account = regionResolver.getAccountId();
|
||||
if (!parsed.accountId().equals(account) || parsed.region().isBlank()) {
|
||||
return Map.of();
|
||||
}
|
||||
return StoredResourceArns.withEmptyValues(bedrock.tagsInAccount(account, arn));
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ public class ApplicationInferenceProfile {
|
||||
private List<String> models;
|
||||
private Instant createdAt;
|
||||
private Instant updatedAt;
|
||||
private Map<String, String> tags;
|
||||
private volatile Map<String, String> tags;
|
||||
private String accountId;
|
||||
private String region;
|
||||
private String clientRequestToken;
|
||||
|
||||
@@ -19,7 +19,7 @@ public class Guardrail {
|
||||
private String kmsKeyArn;
|
||||
private Instant createdAt;
|
||||
private Instant updatedAt;
|
||||
private Map<String, String> tags;
|
||||
private volatile Map<String, String> tags;
|
||||
private String accountId;
|
||||
private String resourcePolicy;
|
||||
|
||||
|
||||
@@ -373,11 +373,6 @@ public class IamActionRegistry {
|
||||
rule("eks", "POST", "^/tags(/.+)?$", "eks:TagResource"),
|
||||
rule("eks", "DELETE", "^/tags(/.+)?$", "eks:UntagResource"),
|
||||
|
||||
// ── Bedrock ────────────────────────────────────────────────────────────
|
||||
rule("bedrock", "POST", "^/resource-policy$", "bedrock:PutResourcePolicy"),
|
||||
rule("bedrock", "GET", "^/resource-policy/.+", "bedrock:GetResourcePolicy"),
|
||||
rule("bedrock", "DELETE", "^/resource-policy/.+", "bedrock:DeleteResourcePolicy"),
|
||||
|
||||
// ── SES v2 ─────────────────────────────────────────────────────────────
|
||||
// SES v1 is a Query API and resolves from its Action like any other; v2 is REST-JSON under
|
||||
// /v2/email, so every operation needs its own rule. One rule per operation in the SES v2
|
||||
|
||||
@@ -60,8 +60,6 @@ public class ResourceArnBuilder {
|
||||
/** The segment a tagging route names before the ARN it tags, which it captures whole, slashes included. */
|
||||
private static final String API_GATEWAY_TAGS_SEGMENT = "tags";
|
||||
|
||||
/** The route of Bedrock's resource-policy actions, which the ARN of the resource they act on may follow. */
|
||||
private static final String BEDROCK_RESOURCE_POLICY_ROUTE = "/resource-policy";
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
/**
|
||||
@@ -151,12 +149,10 @@ public class ResourceArnBuilder {
|
||||
if (operation != null && operation.startsWith(credentialScope + ":") && RequestResources.reads(operation)) {
|
||||
return namedResources(credentialScope, operation, ctx, region, accountId, partition);
|
||||
}
|
||||
String path = IamActionRegistry.decodedPath(ctx);
|
||||
return switch (credentialScope) {
|
||||
case "dynamodb" -> buildDynamoDbArns(ctx, region, accountId);
|
||||
case "iam" -> buildIamArns(ctx, region, accountId);
|
||||
case "apigateway" -> List.of(buildApiGatewayArn(ctx, region, partition));
|
||||
case "bedrock" -> List.of(buildBedrockArn(path, ctx));
|
||||
default -> namedResources(credentialScope, operation, ctx, region, accountId, partition);
|
||||
};
|
||||
}
|
||||
@@ -838,24 +834,6 @@ public class ResourceArnBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Bedrock ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* The resource of a Bedrock route. The resource-policy actions act on the resource whose ARN they carry,
|
||||
* in the path, or in the body's {@code resourceArn} for {@code PutResourcePolicy}, as the Service
|
||||
* Authorization Reference gives them; every other route, and a value that is not an ARN, is {@code *}.
|
||||
*/
|
||||
private String buildBedrockArn(String path, ContainerRequestContext ctx) {
|
||||
String resourceArn = null;
|
||||
if (path.startsWith(BEDROCK_RESOURCE_POLICY_ROUTE + "/")) {
|
||||
resourceArn = path.substring(BEDROCK_RESOURCE_POLICY_ROUTE.length() + 1);
|
||||
} else if (BEDROCK_RESOURCE_POLICY_ROUTE.equals(path) && "POST".equalsIgnoreCase(ctx.getMethod())) {
|
||||
JsonNode body = readJsonBody(ctx);
|
||||
resourceArn = body == null ? null : body.path("resourceArn").asText(null);
|
||||
}
|
||||
return AwsArnUtils.isArn(resourceArn) ? resourceArn : "*";
|
||||
}
|
||||
|
||||
// ── DynamoDB ─────────────────────────────────────────────────────────────────
|
||||
private String buildDynamoDbArn(ContainerRequestContext ctx, String region, String accountId) {
|
||||
List<String> arns = buildDynamoDbArns(ctx, region, accountId);
|
||||
|
||||
Reference in new issue
Block a user