mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): check Aurora DSQL requests on the clusters they name, from the generated table (#9011)
Aurora DSQL was built by hand: ResourceArnBuilder read a cluster or
stream from its path, or a tagging route's ARN, IamEnforcementFilter
sent DSQL requests there without their operation, the request resources
table held no DSQL operation, and nothing gave a policy condition a
cluster's tags. dsql moves from HAND_BUILT to NAMESPACES, so the table
gives each operation the cluster or stream its path names, or the ARN
its modeled /tags/{resourceArn} carries, from botocore's REST model;
ListClusters, for which the Service Reference lists no resource type, is
checked on cluster/* from a MEASURED entry citing the AccessDenied
message AWS returned in #6328. ResourceArnBuilder's DSQL branch, with
the path and tagged-ARN helpers only it still used, and the filter's
DSQL case go, and the shared tags controller now serves DSQL's
tagging on its modeled route only, refusing the bare /tags. Only DSQL's
operations change in the table. DsqlStoredResourceArns answers a
cluster's stored tags; TagResource and UntagResource give the cluster a
new map under its lock, and its tags field is volatile.
This commit is contained in:
1 parent
ede9601b82
commit
66dbdd2935
12 files changed
+185
-94
No files matched your search
@@ -1802,11 +1802,9 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
|
||||
/**
|
||||
* Logs, Secrets Manager and KMS targets depend on the serving operation and the request's partition, STS
|
||||
* targets on the serving operation;
|
||||
* DSQL targets on the request's partition, which that service mints its ARNs in, as are
|
||||
* API Gateway's path ARNs; and the
|
||||
* targets of a namespace the table of request resources reads on {@code operation}, the one the request is
|
||||
* dispatched as (see {@link RequestResources}).
|
||||
* targets on the serving operation; API Gateway's path ARNs on the request's partition, which that service mints
|
||||
* them in; and the targets of a namespace the table of request resources reads on {@code operation}, the one the
|
||||
* request is dispatched as (see {@link RequestResources}).
|
||||
*/
|
||||
private List<String> targetResources(String credentialScope, String action, String operation,
|
||||
ContainerRequestContext ctx, String region, String accountId) {
|
||||
@@ -1819,7 +1817,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
if ("sts".equals(credentialScope)) {
|
||||
return arnBuilder.buildStsResources(action, ctx);
|
||||
}
|
||||
if ("dsql".equals(credentialScope) || "apigateway".equals(credentialScope)) {
|
||||
if ("apigateway".equals(credentialScope)) {
|
||||
return arnBuilder.buildResources(credentialScope, ctx, region, accountId, requestPartition());
|
||||
}
|
||||
// ARNs the table renders take the request's partition, which a Region Floci does not know cannot be mapped to.
|
||||
|
||||
@@ -59,10 +59,10 @@ import java.util.Optional;
|
||||
* of a landing zone or an enabled control; GuardDuty, for the tags of a detector or a Malware Protection plan; Cloud
|
||||
* Map, for the tags of a namespace or a service; Elastic Load Balancing, for the tags of a load balancer, a target
|
||||
* group, a listener, a rule or a Classic Load Balancer; SQS, for the tags of a queue; SNS, for the tags of a topic;
|
||||
* Kinesis Data Streams, for the tags of a stream or a consumer; EventBridge Pipes, for the tags of a pipe); looked up
|
||||
* lazily by {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through
|
||||
* {@code Instance<StoredResourceArns>} for the namespace a request or an ARN is of, so IAM never depends on that
|
||||
* service directly.</p>
|
||||
* Kinesis Data Streams, for the tags of a stream or a consumer; EventBridge Pipes, for the tags of a pipe; Aurora DSQL,
|
||||
* for the tags of a cluster); looked up lazily by {@code ResourceArnBuilder} and {@code IamConditionContextResolver}
|
||||
* through {@code Instance<StoredResourceArns>} for the namespace a request or an ARN is of, so IAM never depends on
|
||||
* that service directly.</p>
|
||||
*
|
||||
* <p>Each answer is a lookup of what the service holds, asked for by an entry of the generated table ({@code find},
|
||||
* {@code through}, a default, {@code forwarded}, a type's {@code resourceTagKeys}): never a fact about what an operation acts on, which the table alone
|
||||
|
||||
@@ -350,22 +350,43 @@ public class DsqlService implements Resettable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives the cluster a new map holding its tags and {@code tags}, under its lock, so a lookup that takes no lock
|
||||
* reads one whole set.
|
||||
*/
|
||||
public void tagResource(String region, String arn, Map<String, String> tags) {
|
||||
DsqlCluster cluster = clusterForArn(region, arn, "TagResource");
|
||||
synchronized (lockFor(cluster.getArn())) {
|
||||
cluster.getTags().putAll(tags == null ? Map.of() : tags);
|
||||
Map<String, String> merged = new LinkedHashMap<>(cluster.getTags());
|
||||
merged.putAll(tags == null ? Map.of() : tags);
|
||||
cluster.setTags(merged);
|
||||
clusters.put(key(cluster.getRegion(), cluster.getIdentifier()), cluster);
|
||||
}
|
||||
}
|
||||
|
||||
/** Gives the cluster a new map without {@code tagKeys}, under its lock, as {@link #tagResource}. */
|
||||
public void untagResource(String region, String arn, List<String> tagKeys) {
|
||||
DsqlCluster cluster = clusterForArn(region, arn, "UntagResource");
|
||||
synchronized (lockFor(cluster.getArn())) {
|
||||
(tagKeys == null ? List.<String>of() : tagKeys).forEach(cluster.getTags()::remove);
|
||||
Map<String, String> remaining = new LinkedHashMap<>(cluster.getTags());
|
||||
(tagKeys == null ? List.<String>of() : tagKeys).forEach(remaining::remove);
|
||||
cluster.setTags(remaining);
|
||||
clusters.put(key(cluster.getRegion(), cluster.getIdentifier()), cluster);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the cluster whose ARN is {@code arn} in {@code account}, read in place under the key the handlers
|
||||
* read, its identifier in the ARN's Region, and matched on the whole ARN it records; none when there is no such
|
||||
* cluster.
|
||||
*/
|
||||
Map<String, String> clusterTagsInAccount(String account, String arn, String identifier) {
|
||||
return AccountAwareStorageBackend.readInAccount(clusters, account,
|
||||
key(AwsArnUtils.parse(arn).region(), identifier))
|
||||
.filter(cluster -> arn.equals(cluster.getArn()))
|
||||
.map(DsqlCluster::getTags).orElse(Map.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* Brings every cluster's database back when Floci starts with clusters in its storage, and
|
||||
* finishes a deletion that a stop interrupted.
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package io.floci.aws.services.dsql;
|
||||
|
||||
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 Aurora DSQL holds on a cluster, for the {@code dsql} namespace. */
|
||||
@ApplicationScoped
|
||||
public class DsqlStoredResourceArns implements StoredResourceArns {
|
||||
|
||||
private static final String NAMESPACE = "dsql";
|
||||
private static final String CLUSTER = "cluster/";
|
||||
|
||||
private final DsqlService dsql;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
@Inject
|
||||
public DsqlStoredResourceArns(DsqlService dsql, RegionResolver regionResolver) {
|
||||
this.dsql = dsql;
|
||||
this.regionResolver = regionResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return NAMESPACE;
|
||||
}
|
||||
|
||||
/** A request names a cluster by the identifier in its path, or by its ARN: nothing to find. */
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the cluster 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 a stream's, Floci holding no stream of its own. The cluster is
|
||||
* read in place by its identifier, in the ARN's Region, and matched on the whole ARN it records. A tag without a
|
||||
* value reads as an empty one. Tag calls give the cluster a new map, so the read is of one whole set.
|
||||
*/
|
||||
@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();
|
||||
String resource = parsed.resource();
|
||||
if (!parsed.accountId().equals(account) || !resource.startsWith(CLUSTER)
|
||||
|| resource.indexOf('/', CLUSTER.length()) >= 0) {
|
||||
return Map.of();
|
||||
}
|
||||
return StoredResourceArns.withEmptyValues(
|
||||
dsql.clusterTagsInAccount(account, arn, resource.substring(CLUSTER.length())));
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ public class DsqlCluster {
|
||||
private String status;
|
||||
private Instant creationTime;
|
||||
private boolean deletionProtectionEnabled;
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
private volatile Map<String, String> tags = new LinkedHashMap<>();
|
||||
private String kmsEncryptionKey;
|
||||
private String endpoint;
|
||||
private String clientToken;
|
||||
|
||||
@@ -159,7 +159,7 @@ public class ResourceArnBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* The resources a request is checked against. DSQL ARNs are minted in {@code partition}, the request's, as
|
||||
* The resources a request is checked against. API Gateway ARNs are minted in {@code partition}, the request's, as
|
||||
* that service mints its own, so a Region no partition publishes gets the deployment's partition rather than
|
||||
* {@code aws}.
|
||||
*/
|
||||
@@ -188,7 +188,6 @@ public class ResourceArnBuilder {
|
||||
case "dynamodb" -> buildDynamoDbArns(ctx, region, accountId);
|
||||
case "iam" -> buildIamArns(ctx, region, accountId);
|
||||
case "resource-groups" -> List.of(buildResourceGroupArn(ctx, region, accountId));
|
||||
case "dsql" -> List.of(buildDsqlArn(path, ctx, region, accountId, partition));
|
||||
case "eks" -> List.of(buildEksArn(path, ctx, region, accountId));
|
||||
case "apigateway" -> List.of(buildApiGatewayArn(ctx, region, partition));
|
||||
case "bedrock" -> List.of(buildBedrockArn(path, ctx));
|
||||
@@ -934,29 +933,6 @@ public class ResourceArnBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The resource of an Aurora DSQL route, as the Service Authorization Reference names it: the cluster
|
||||
* a cluster route names, the stream a stream route names (its cluster, to create or list streams), or
|
||||
* the ARN a tagging route names. CreateCluster and ListClusters are checked against
|
||||
* {@code cluster/*}, as DSQL checks them (measured).
|
||||
*/
|
||||
private String buildDsqlArn(String path, ContainerRequestContext ctx, String region, String accountId,
|
||||
String partition) {
|
||||
String cluster = "/cluster".equals(path) ? "*" : segmentAfterPrefix(path, "/cluster/");
|
||||
if (cluster == null) {
|
||||
cluster = segmentAfterPrefix(path, "/clusters/");
|
||||
}
|
||||
String streamCluster = segmentAfterPrefix(path, "/stream/");
|
||||
if (streamCluster != null) {
|
||||
String stream = segmentAfterPrefix(path, "/stream/" + streamCluster + "/");
|
||||
cluster = stream == null ? streamCluster : streamCluster + "/stream/" + stream;
|
||||
}
|
||||
if (cluster != null) {
|
||||
return new AwsArnUtils.Arn(partition, "dsql", region, accountId, "cluster/" + cluster).toString();
|
||||
}
|
||||
return taggedResource(ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* API Gateway authorizes a management call as its HTTP method on the path it names, in the Region serving
|
||||
* it and with no account: {@code GET /restapis/abc/stages} acts on
|
||||
@@ -1117,12 +1093,6 @@ public class ResourceArnBuilder {
|
||||
.filter(arn -> region.equals(AwsArnUtils.regionOrDefault(arn, null)));
|
||||
}
|
||||
|
||||
/** The ARN a shared tagging route names, or {@code *} for any other route. */
|
||||
private String taggedResource(ContainerRequestContext ctx) {
|
||||
String arn = taggedArn(ctx);
|
||||
return AwsArnUtils.looksLikeArn(arn) ? arn : "*";
|
||||
}
|
||||
|
||||
/**
|
||||
* The ARN a shared tagging route acts on, read where its controller reads it: after {@code /tags/} or
|
||||
* {@code /v1/tags/} in the path; on {@code POST /tags}, the body's {@code resourceArn}; and on the other
|
||||
@@ -1158,17 +1128,6 @@ public class ResourceArnBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
/** The path segment right after {@code prefix}, or null when the path doesn't start with it or names none. */
|
||||
private static String segmentAfterPrefix(String path, String prefix) {
|
||||
if (!path.startsWith(prefix)) {
|
||||
return null;
|
||||
}
|
||||
String rest = path.substring(prefix.length());
|
||||
int slash = rest.indexOf('/');
|
||||
String segment = slash < 0 ? rest : rest.substring(0, slash);
|
||||
return segment.isEmpty() ? null : segment;
|
||||
}
|
||||
|
||||
// ── Bedrock ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user