Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(iam): authorize S3 Control routes as their own s3 actions (#6681)

Matheus Oliveira97ecbdd656c6
diff --git a/docs/services/s3.md b/docs/services/s3.md
index 50049bd7be..6b3221c67a 100644
--- a/docs/services/s3.md
+++ b/docs/services/s3.md
@@ -497,6 +497,21 @@ A single public statement makes the whole policy public. As on AWS, `RestrictPub
The s3control `PutPublicAccessBlock`, `GetPublicAccessBlock` and `DeletePublicAccessBlock` operations are keyed by the `x-amz-account-id` header, stored per account, and persisted like the rest of S3's state so a restart does not silently drop the control. As in AWS, the header must name the caller's own account; a mismatch returns `AccessDenied` (403). Floci keeps one deliberate exception: the configured default (management) account may act on any account, because Floci's launched Lambdas run on placeholder credentials that resolve to the management account instead of assuming a role in the target account, which is how LZA's `Custom::PutPublicAccessBlock` resource reaches this API.
+### IAM enforcement of S3 Control
+
+With IAM enforcement on, every `/v20180820` route is authorized as the `s3:` action the Service Authorization Reference gives it, against the resource the route names, because S3 Control clients sign with the `s3` scope. The s3control model's routes and the actions are vendored from AWS (`rest-routes.json` and `iam-authorized-actions.json`, regenerated by `make aws-data-sync`), and `IamRestRouteCoverageTest` checks every route Floci serves against them. The account-level Block Public Access is the one exception, written down where that data is checked: the reference files S3's and S3 Control's `GetPublicAccessBlock`, `PutPublicAccessBlock` and `DeletePublicAccessBlock` under the bucket actions, while the S3 User Guide's [Block Public Access](https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-control-block-public-access.html) page gives the account level `s3:PutAccountPublicAccessBlock` to write and `s3:GetAccountPublicAccessBlock` to read, and a delete the same action as the write it undoes.
+
+| Operation | Action | Resource |
+|---|---|---|
+| `ListTagsForResource`, `TagResource`, `UntagResource` | `s3:ListTagsForResource`, `s3:TagResource`, `s3:UntagResource` | The ARN the route names, as written |
+| `ListAccessPoints` | `s3:ListAccessPoints` | All resources (`*`; AWS publishes no resource type, so a bucket-scoped statement grants nothing) |
+| `PutPublicAccessBlock`, `DeletePublicAccessBlock` | `s3:PutAccountPublicAccessBlock` | All resources (`*`) |
+| `GetPublicAccessBlock` | `s3:GetAccountPublicAccessBlock` | All resources (`*`) |
+
+A tagging route carries the bucket's ARN in its path, and both documented forms arrive on the wire: the global form `arn:<partition>:s3:::<name>` that the Go SDK and Terraform's provider send, and the regional form `arn:<partition>:s3:<region>:<account>:bucket/<name>` that the operation's own request pattern and the AWS CLI examples use. The ARN as the caller wrote it is the resource the request is checked against, so a policy may name either form, and the ARN is read once, by the IAM check, the bucket-policy lookup and the handler together: the regional form's account is the account whose bucket the request acts on, so a request naming another account's bucket reads and writes that account's bucket, and answers `NoSuchBucket` when that account holds no bucket of the name, never the caller's same-named one. Query-string parameters the control plane ignores, such as `?tagging`, do not re-point the authorization at a data-plane action.
+
+A denial answers in S3 Control's own REST-XML `<ErrorResponse>` envelope, which follows the route rather than the resolved action, so the s3control SDK deserializers read the message that names the missing action, which Terraform's AWS provider matches to fall back from `ListTagsForResource` to `GetBucketTagging`.
+
## Not Implemented
These AWS S3 features have no handler in Floci. Calls will return an error (typically `404` or `NoSuchBucket`-style):
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java
index e262c00bbe..813bd92ab5 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java
@@ -1783,6 +1783,12 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
// 403, the status the Kinesis and CloudWatch common errors give AccessDeniedException.
return cborError(ctx, "AccessDeniedException", accessDeniedMessage(action), 403);
}
+ // The route decides the envelope, not the resolved action: a request on an S3 Control
+ // route is S3 Control's however its action reads, and only its ErrorResponse wrapper
+ // reaches the s3control SDK deserializers (issue #557).
+ if (IamActionRegistry.isS3ControlRequest(credentialScope, ctx)) {
+ return s3ControlXmlAccessDenied(accessDeniedMessage(action));
+ }
return accessDeniedResponse(action, credentialScope, ctx.getMediaType(), resourceArn, protocol);
}
@@ -1862,6 +1868,31 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
return s3Xml("AccessDenied", message, resourcePath, 403);
}
+ /**
+ * S3 Control's own access-denied answer, which unlike S3's error body wraps the error in an
+ * {@code <ErrorResponse>} the s3control SDK deserializers read (issue #557): the bare
+ * {@code <Error>} S3 answers with collapses to {@code UnknownError} there, hiding the message
+ * that names the action a caller is missing, which Terraform's AWS provider matches to fall
+ * back from {@code ListTagsForResource} to {@code GetBucketTagging}. The same envelope as
+ * {@code S3ControlController}'s, and no {@code Resource} element: the ARN a tagging route
+ * names is the resource, not a bucket and key.
+ */
+ private static Response s3ControlXmlAccessDenied(String message) {
+ String requestId = UUID.randomUUID().toString();
+ String xml = new XmlBuilder()
+ .raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
+ .start("ErrorResponse", AwsNamespaces.S3_CONTROL)
+ .start("Error")
+ .elem("Code", "AccessDenied")
+ .elem("Message", message)
+ .elem("RequestId", requestId)
+ .end("Error")
+ .elem("RequestId", requestId)
+ .end("ErrorResponse")
+ .build();
+ return Response.status(403).type(MediaType.APPLICATION_XML).entity(xml).build();
+ }
+
private static Response s3Xml(String code, String message, String resourcePath, int status) {
XmlBuilder xml = new XmlBuilder()
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java
index 81dfeb1759..4360a80695 100644
--- a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java
+++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java
@@ -36,10 +36,12 @@ public class IamActionRegistry {
/**
* A route and the action it is checked as. A rule naming a query parameter matches only a request
* carrying it with that value, as {@code GET /layers?find=LayerVersion} is GetLayerVersionByArn
- * where {@code GET /layers} is ListLayers.
+ * where {@code GET /layers} is ListLayers. A rule flagged to match before the S3 query
+ * overrides is one of the S3 Control routes, which the control plane answers whatever its
+ * query string carries.
*/
private record ActionRule(String service, String method, Pattern pathPattern, String queryParameter,
- String queryValue, String action) {
+ String queryValue, String action, boolean beforeS3QueryOverrides) {
boolean matches(String scope, String requestMethod, String path, ContainerRequestContext ctx) {
return service.equals(scope) && method.equals(requestMethod) && pathPattern.matcher(path).find()
@@ -48,7 +50,36 @@ public class IamActionRegistry {
}
}
+ /**
+ * The routes of the S3 Control model, checked as the actions the Service Reference gives
+ * them by {@code IamRestRouteCoverageTest}. S3 Control clients sign with the s3 scope
+ * (botocore's s3control model declares signingName "s3"), so its operations are
+ * {@code s3:} actions, and its routes must be matched before the S3 data-plane query
+ * overrides: the control plane answers a tagging route whatever its query string carries,
+ * so a parameter such as {@code ?tagging} must not re-point the authorization at the
+ * data-plane object tagging it suggests, and the data-plane rules below would otherwise
+ * read {@code /v20180820/...} as a bucket named v20180820.
+ *
+ * <p>The account-level Block Public Access is the one route the Service Reference cannot
+ * settle, because it files S3's and S3 Control's GetPublicAccessBlock, PutPublicAccessBlock
+ * and DeletePublicAccessBlock under the bucket actions; the S3 User Guide's Block Public
+ * Access page gives the account level its own actions, and the exception is written down
+ * where the data is checked. ListAccessPoints and the account actions have no resource
+ * type, so IAM checks them against the wildcard rather than a bucket.
+ */
private static final List<ActionRule> RULES = List.of(
+ // ── S3 Control ───────────────────────────────────────────────────────────
+ s3ControlRule("GET", "^/v20180820/tags/.+", "s3:ListTagsForResource"),
+ s3ControlRule("POST", "^/v20180820/tags/.+", "s3:TagResource"),
+ s3ControlRule("DELETE", "^/v20180820/tags/.+", "s3:UntagResource"),
+ s3ControlRule("GET", "^/v20180820/accesspoint/?$", "s3:ListAccessPoints"),
+ s3ControlRule("PUT", "^/v20180820/configuration/publicAccessBlock/?$",
+ "s3:PutAccountPublicAccessBlock"),
+ s3ControlRule("GET", "^/v20180820/configuration/publicAccessBlock/?$",
+ "s3:GetAccountPublicAccessBlock"),
+ s3ControlRule("DELETE", "^/v20180820/configuration/publicAccessBlock/?$",
+ "s3:PutAccountPublicAccessBlock"),
+
// ── S3 ─────────────────────────────────────────────────────────────────
rule("s3", "GET", "^/?$", "s3:ListAllMyBuckets"),
rule("s3", "PUT", "^/[^/]+/?$", "s3:CreateBucket"),
@@ -519,7 +550,35 @@ public class IamActionRegistry {
private static ActionRule rule(String service, String method, String path, String queryParameter,
String queryValue, String action) {
return new ActionRule(service, method, Pattern.compile(path, Pattern.CASE_INSENSITIVE), queryParameter,
- queryValue, action);
+ queryValue, action, false);
+ }
+
+ private static ActionRule s3ControlRule(String method, String path, String action) {
+ return new ActionRule("s3", method, Pattern.compile(path, Pattern.CASE_INSENSITIVE), null, null,
+ action, true);
+ }
+
+ /**
+ * Whether the request is one of the S3 Control routes, as the rules flag them. A denial then
+ * answers in S3 Control's own REST-XML envelope, the only one the s3control SDK
+ * deserializers read (issue #557), so the route decides the shape and not the action: the
+ * data plane resolves actions a control route never shares, and an action-name list would
+ * have to track every route to stay right. A request whose route cannot be read, which only
+ * a synthetic context is, is not a control one.
+ */
+ public static boolean isS3ControlRequest(String credentialScope, ContainerRequestContext ctx) {
+ if (!"s3".equals(credentialScope) || ctx.getMethod() == null || ctx.getUriInfo() == null
+ || ctx.getUriInfo().getRequestUri() == null) {
+ return false;
+ }
+ String method = ctx.getMethod().toUpperCase();
+ String path = rawPath(ctx.getUriInfo());
+ for (ActionRule rule : RULES) {
+ if (rule.beforeS3QueryOverrides() && rule.matches(credentialScope, method, path, ctx)) {
+ return true;
+ }
+ }
+ return false;
}
/**
@@ -589,12 +648,20 @@ public class IamActionRegistry {
// bucket's one trailing slash, so an S3 path is matched as sent.
String path = "s3".equals(credentialScope) ? rawPath(ctx.getUriInfo()) : routedPath(ctx.getUriInfo());
- // S3 sub-resource override: the URL path alone doesn't distinguish
- // s3:GetObjectAcl / s3:PutObjectAcl from s3:GetObject / s3:PutObject
- // — only the {@code ?acl} query parameter does. Without this hook,
- // an actor with s3:GetObject permission could read ACLs that their
- // policy intends to forbid.
if ("s3".equals(credentialScope)) {
+ // An S3 Control route is matched before the query overrides below, which would read the
+ // query string of a route the control plane answers whatever it carries: ?tagging on a
+ // tagging route is caller-supplied noise, not the object sub-resource it names.
+ for (ActionRule rule : RULES) {
+ if (rule.beforeS3QueryOverrides() && rule.matches(credentialScope, method, path, ctx)) {
+ return rule.action();
+ }
+ }
+ // S3 sub-resource override: the URL path alone doesn't distinguish
+ // s3:GetObjectAcl / s3:PutObjectAcl from s3:GetObject / s3:PutObject:
+ // only the {@code ?acl} query parameter does. Without this hook,
+ // an actor with s3:GetObject permission could read ACLs that their
+ // policy intends to forbid.
String s3SubAction = resolveS3SubResourceAction(method, ctx);
if (s3SubAction != null) {
return s3SubAction;
diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java
index 9bb59b6260..95cb30f3d2 100644
--- a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java
+++ b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java
@@ -18,6 +18,7 @@ import io.github.hectorvent.floci.services.kms.KmsService;
import io.github.hectorvent.floci.services.lambda.LambdaArnUtils;
import io.github.hectorvent.floci.services.lambda.durable.DurableTokens;
import io.github.hectorvent.floci.services.lambdamicrovms.LambdaMicrovmsService;
+import io.github.hectorvent.floci.services.s3.S3ControlArnUtils;
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Instance;
@@ -66,6 +67,9 @@ public class ResourceArnBuilder {
*/
private static final String UNASSIGNED_SECRET_SUFFIX = "-??????";
+ /** Where an S3 Control tagging route carries the ARN of the resource it acts on. */
+ private static final String S3_CONTROL_TAGS_PREFIX = "/v20180820/tags/";
+
private final ObjectMapper objectMapper;
/**
* Looked up lazily through a provider: this builder is constructed by the enforcement filter,
@@ -173,7 +177,7 @@ public class ResourceArnBuilder {
}
String path = ctx.getUriInfo().getPath();
return switch (credentialScope) {
- case "s3" -> List.of(buildS3Arn(path, region));
+ case "s3" -> buildS3Resources(operation, path, region);
case "lambda" -> List.of(buildLambdaArn(path, ctx, region, accountId));
case "sqs" -> List.of(buildSqsArn(ctx, region, accountId));
case "sns" -> List.of(buildSnsArn(ctx, region, accountId));
@@ -803,6 +807,52 @@ public class ResourceArnBuilder {
return AwsArnUtils.Arn.global(partition, "s3", "", stripped).toString();
}
+ /**
+ * The resources an s3-scope request names, which an S3 Control operation reads differently
+ * from an S3 data-plane path. S3 Control clients sign with the s3 scope and its operations
+ * are {@code s3:} actions (the Service Authorization Reference lists them under Amazon S3),
+ * so the operation, not the path, tells the two apart: a tagging route carries the ARN of the
+ * bucket or access point it acts on in its path, and the request is checked against that ARN
+ * as written, the way every ARN-carrying request is; the Service Authorization Reference
+ * gives ListAccessPoints and the account-level Block Public Access no resource type, so
+ * those are checked against the wildcard alone, as the no-resource actions of other
+ * namespaces are (the KMS list operations), and a bucket-scoped statement grants them
+ * nothing. Any other operation is the data plane, checked against the bucket or object its
+ * path names.
+ */
+ private List<String> buildS3Resources(String operation, String path, String region) {
+ if (operation == null) {
+ return List.of(buildS3Arn(path, region));
+ }
+ return switch (operation) {
+ case "s3:ListTagsForResource", "s3:TagResource", "s3:UntagResource" ->
+ List.of(taggedS3ControlArn(path));
+ case "s3:ListAccessPoints", "s3:PutAccountPublicAccessBlock",
+ "s3:GetAccountPublicAccessBlock" -> List.of("*");
+ default -> List.of(buildS3Arn(path, region));
+ };
+ }
+
+ /**
+ * The ARN an S3 Control tagging route names in its path, read with the one parse the handler
+ * and the policy lookup share: the Go SDK, and so Terraform's provider, percent-encodes the
+ * ARN's colons and slashes, so the path may still carry them. A path naming no ARN leaves
+ * the wildcard, which the handler answers {@code InvalidRequest} under, and nothing is
+ * rebuilt from the bucket it names: the ARN as the caller wrote it is the resource.
+ */
+ private static String taggedS3ControlArn(String path) {
+ String tagged = path.startsWith(S3_CONTROL_TAGS_PREFIX)
+ ? path.substring(S3_CONTROL_TAGS_PREFIX.length()) : null;
+ if (tagged == null) {
+ return "*";
+ }
+ try {
+ return S3ControlArnUtils.parse(tagged).map(AwsArnUtils.Arn::toString).orElse("*");
+ } catch (IllegalArgumentException malformedEncoding) {
+ return "*";
+ }
+ }
+
// ── Lambda ──────────────────────────────────────────────────────────────────
/**
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3ControlArnUtils.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3ControlArnUtils.java
new file mode 100644
index 0000000000..f10cc20f32
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3ControlArnUtils.java
@@ -0,0 +1,64 @@
+package io.github.hectorvent.floci.services.s3;
+
+import io.github.hectorvent.floci.core.common.AwsArnUtils;
+
+import java.net.URLDecoder;
+import java.nio.charset.StandardCharsets;
+import java.util.Optional;
+
+/**
+ * The resource ARN an S3 Control tagging route names in its path, read the one way every reader
+ * of it shares: decoded once, because the Go SDK v2, and so Terraform's provider, percent-encodes
+ * the ARN's colons and slashes while the Java SDK sends them literally, then parsed with
+ * {@link AwsArnUtils#parse}. Two documented forms name a bucket (the S3 Control API reference,
+ * ListTagsForResource): the regional {@code arn:<partition>:s3:<region>:<account>:bucket/<name>}
+ * the operation's own request pattern and the AWS CLI examples use, and the global
+ * {@code arn:<partition>:s3:::<name>}. The regional form's account and region are the ones the
+ * handler, the policy lookup and the IAM check all act on, so a request naming another account's
+ * bucket acts on that account's bucket and never on the caller's same-named one.
+ */
+public final class S3ControlArnUtils {
+
+ /** The regional form's resource prefix: {@code bucket/<name>}. */
+ private static final String BUCKET_RESOURCE_PREFIX = "bucket/";
+
+ private S3ControlArnUtils() {}
+
+ /**
+ * The tagging route's ARN, decoded as its handler decodes it and parsed once. Empty when the
+ * value is null, blank or not an s3 ARN; {@link IllegalArgumentException} when its
+ * percent-encoding is malformed.
+ */
+ public static Optional<AwsArnUtils.Arn> parse(String resourceArn) {
+ if (resourceArn == null || resourceArn.isBlank()) {
+ return Optional.empty();
+ }
+ String decoded = URLDecoder.decode(resourceArn, StandardCharsets.UTF_8);
+ return AwsArnUtils.isArnFor(decoded, "s3") ? Optional.of(AwsArnUtils.parse(decoded)) : Optional.empty();
+ }
+
+ /**
+ * Whether {@code arn} is the regional form: the only S3 ARN that fills its region and carries
+ * its bucket after {@code bucket/}. No S3 data-plane ARN fills the region, so this reading can
+ * never collide with the data-plane one of a bucket actually named {@code bucket}.
+ */
+ public static boolean regionalBucketForm(AwsArnUtils.Arn arn) {
+ return !arn.region().isEmpty() && arn.resource().startsWith(BUCKET_RESOURCE_PREFIX);
+ }
+
+ /**
+ * Whether the ARN names a bucket the tagging routes act on: the regional form, or the global
+ * one whose whole resource is the bucket's name. Anything else, an access point ARN included,
+ * names a resource these routes do not serve.
+ */
+ public static boolean namesBucket(AwsArnUtils.Arn arn) {
+ return regionalBucketForm(arn)
+ || (arn.region().isEmpty() && arn.accountId().isEmpty()
+ && !arn.resource().isEmpty() && arn.resource().indexOf('/') < 0);
+ }
+
+ /** The bucket the ARN names: after {@code bucket/} in the regional form, the whole resource in the global one. */
+ public static String bucketName(AwsArnUtils.Arn arn) {
+ return regionalBucketForm(arn) ? arn.resource().substring(BUCKET_RESOURCE_PREFIX.length()) : arn.resource();
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3ControlController.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3ControlController.java
index 1abd4b83a6..cf7f8a13c5 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3ControlController.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3ControlController.java
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsNamespaces;
import io.github.hectorvent.floci.core.common.RequestContext;
@@ -24,11 +25,11 @@ import jakarta.ws.rs.core.Response;
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
-import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.UUID;
/**
@@ -75,8 +76,9 @@ public class S3ControlController {
@HeaderParam("x-amz-account-id") String accountId) {
try {
- String bucketName = extractBucketName(resourceArn);
- Map<String, String> tags = s3Service.getBucketTagging(bucketName);
+ AwsArnUtils.Arn arn = taggedBucketArn(resourceArn);
+ Map<String, String> tags = s3Service.getBucketTaggingForAccount(arn.accountId(),
+ S3ControlArnUtils.bucketName(arn));
XmlBuilder xml = new XmlBuilder()
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
@@ -278,10 +280,10 @@ public class S3ControlController {
byte[] body) {
try {
- String bucketName = extractBucketName(resourceArn);
+ AwsArnUtils.Arn arn = taggedBucketArn(resourceArn);
String xml = new String(body, StandardCharsets.UTF_8);
Map<String, String> tags = XmlParser.extractPairs(xml, "Tag", "Key", "Value");
- s3Service.putBucketTagging(bucketName, tags);
+ s3Service.putBucketTaggingForAccount(arn.accountId(), S3ControlArnUtils.bucketName(arn), tags);
return Response.noContent().build();
} catch (AwsException e) {
return xmlErrorResponse(e);
@@ -302,10 +304,12 @@ public class S3ControlController {
@QueryParam("tagKeys") List<String> tagKeys) {
try {
- String bucketName = extractBucketName(resourceArn);
- Map<String, String> existing = new HashMap<>(s3Service.getBucketTagging(bucketName));
+ AwsArnUtils.Arn arn = taggedBucketArn(resourceArn);
+ String bucketName = S3ControlArnUtils.bucketName(arn);
+ Map<String, String> existing =
+ new HashMap<>(s3Service.getBucketTaggingForAccount(arn.accountId(), bucketName));
tagKeys.forEach(existing::remove);
- s3Service.putBucketTagging(bucketName, existing);
+ s3Service.putBucketTaggingForAccount(arn.accountId(), bucketName, existing);
return Response.noContent().build();
} catch (AwsException e) {
return xmlErrorResponse(e);
@@ -313,47 +317,24 @@ public class S3ControlController {
}
/**
- * Parse the bucket name out of an S3 bucket ARN path parameter.
- *
- * <p>The AWS Go SDK v2 (used by Terraform) percent-encodes the ARN's
- * colons and slashes in the request path, while the Java SDK sends them
- * literally. We decode defensively so both forms work, and so routing
- * frameworks that leave {@code %2F} encoded in path segments don't break
- * us.
- *
- * <p>Two valid ARN forms are accepted:
- * <ul>
- * <li>S3 Control ARN: {@code arn:aws:s3:<region>:<account>:bucket/<name>}</li>
- * <li>Plain S3 ARN: {@code arn:aws:s3:::<name>} — sent by Go SDK v2 / Terraform provider v6</li>
- * </ul>
+ * The bucket ARN a tagging route names, decoded and parsed the one way every reader of it
+ * shares: the handler here, the IAM resource the request is checked against, and the bucket
+ * policy lookup. The regional form carries the account and region of the bucket it names, and
+ * the global form the caller's own account, so a request naming another account's bucket acts
+ * on that account's bucket, never on the caller's same-named one.
*/
- private String extractBucketName(String resourceArn) {
- String decoded;
+ private static AwsArnUtils.Arn taggedBucketArn(String resourceArn) {
+ Optional<AwsArnUtils.Arn> arn;
try {
- decoded = URLDecoder.decode(resourceArn, StandardCharsets.UTF_8);
+ arn = S3ControlArnUtils.parse(resourceArn);
} catch (IllegalArgumentException e) {
throw new AwsException("InvalidRequest",
"Malformed percent-encoding in resource ARN: " + e.getMessage(), 400);
}
-
- // Form 1: arn:<partition>:s3:<region>:<account>:bucket/<name>
- int idx = decoded.lastIndexOf(":bucket/");
- if (idx >= 0) {
- return decoded.substring(idx + ":bucket/".length());
- }
-
- // Form 2: arn:<partition>:s3:::<name> (plain S3 ARN — no region, no account)
- // Go SDK v2 / Terraform provider v6 sends this form for general-purpose buckets.
- String[] parts = decoded.split(":", 6);
- if (parts.length == 6 && "s3".equals(parts[2])
- && parts[3].isEmpty() && parts[4].isEmpty()
- && !parts[5].isEmpty() && !parts[5].contains("/")) {
- return parts[5];
- }
-
- throw new AwsException("InvalidRequest",
- "Unsupported resource type. Only S3 bucket ARNs are supported " +
- "(arn:aws:s3:<region>:<account>:bucket/<name> or arn:aws:s3:::<name>).", 400); // partition-literal: AWS's message text
+ return arn.filter(S3ControlArnUtils::namesBucket)
+ .orElseThrow(() -> new AwsException("InvalidRequest",
+ "Unsupported resource type. Only S3 bucket ARNs are supported " +
+ "(arn:aws:s3:<region>:<account>:bucket/<name> or arn:aws:s3:::<name>).", 400)); // partition-literal: AWS's message text
}
/**
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3ResourcePolicyProvider.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3ResourcePolicyProvider.java
index 955cbf06b3..ac9aa2a844 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3ResourcePolicyProvider.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3ResourcePolicyProvider.java
@@ -26,11 +26,14 @@ public class S3ResourcePolicyProvider implements ResourcePolicyProvider {
if (!"s3".equalsIgnoreCase(credentialScope) || resourceArn == null) {
return List.of();
}
- String bucketName = extractBucketName(resourceArn);
+ AwsArnUtils.Arn arn = s3Arn(resourceArn);
+ String bucketName = arn == null ? null : extractBucketName(arn);
if (bucketName == null || bucketName.isEmpty() || "*".equals(bucketName)) {
return List.of();
}
- Optional<S3Service.BucketPolicyInfo> policyInfo = s3Service.findBucketPolicyInfo(bucketName);
+ Optional<S3Service.BucketPolicyInfo> policyInfo = S3ControlArnUtils.regionalBucketForm(arn)
+ ? s3Service.findBucketPolicyInfoForAccount(arn.accountId(), bucketName)
+ : s3Service.findBucketPolicyInfo(bucketName);
return policyInfo.map(info -> List.of(new ResourcePolicy(info.policy(), info.ownerAccountId())))
.orElseGet(List::of);
}
@@ -50,12 +53,26 @@ public class S3ResourcePolicyProvider implements ResourcePolicyProvider {
if (!"s3".equalsIgnoreCase(credentialScope) || resourceArn == null) {
return resourceArn;
}
- String bucketName = extractBucketName(resourceArn);
- if (bucketName == null || bucketName.isEmpty() || "*".equals(bucketName)) {
+ AwsArnUtils.Arn arn = s3Arn(resourceArn);
+ if (arn == null || S3ControlArnUtils.regionalBucketForm(arn)) {
+ return resourceArn;
+ }
+ String bucketName = extractBucketName(arn);
+ if (bucketName.isEmpty() || "*".equals(bucketName)) {
return resourceArn;
}
- String resource = AwsArnUtils.resourceIfArnFor(resourceArn, "s3").orElseThrow();
- return AwsArnUtils.Arn.global(s3Service.bucketPartition(bucketName), "s3", "", resource).toString();
+ return AwsArnUtils.Arn.global(s3Service.bucketPartition(bucketName), "s3", "", arn.resource()).toString();
+ }
+
+ /**
+ * The request's resource, parsed without decoding: every ARN reaching here is already in its
+ * decoded form, the data plane's from the request path and a control tagging one re-minted
+ * by the rule that read it off the wire, so decoding again would rewrite an object key like
+ * {@code a+b.txt} before IAM checks it. Null when it is not an s3 ARN, which leaves the
+ * request its identity policy alone: no bucket policy applies, as when the bucket has none.
+ */
+ private static AwsArnUtils.Arn s3Arn(String resourceArn) {
+ return AwsArnUtils.isArnFor(resourceArn, "s3") ? AwsArnUtils.parse(resourceArn) : null;
}
/**
@@ -63,11 +80,21 @@ public class S3ResourcePolicyProvider implements ResourcePolicyProvider {
* no policy for a China or GovCloud bucket, which silently changed the access decision.
*/
static String extractBucketName(String resourceArn) {
- String tail = AwsArnUtils.resourceIfArnFor(resourceArn, "s3").orElse(null);
- if (tail == null) {
- return null;
+ AwsArnUtils.Arn arn = s3Arn(resourceArn);
+ return arn == null ? null : extractBucketName(arn);
+ }
+
+ /**
+ * The bucket of a parsed S3 ARN: the one after {@code bucket/} in the regional form a
+ * control tagging route names, so a policy is looked up from the bucket the handler acts on
+ * and not from a bucket named {@code bucket} with the real name as its key, and the bucket
+ * before the first slash in every data-plane form.
+ */
+ static String extractBucketName(AwsArnUtils.Arn arn) {
+ if (S3ControlArnUtils.regionalBucketForm(arn)) {
+ return S3ControlArnUtils.bucketName(arn);
}
- int slash = tail.indexOf('/');
- return slash < 0 ? tail : tail.substring(0, slash);
+ int slash = arn.resource().indexOf('/');
+ return slash < 0 ? arn.resource() : arn.resource().substring(0, slash);
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java
index 9e0f718ef9..741917b02c 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java
@@ -3761,21 +3761,71 @@ public class S3Service implements Resettable, ResourceProvider {
// --- Bucket Tagging ---
public void putBucketTagging(String bucketName, Map<String, String> tags) {
- Bucket bucket = bucketStore.get(bucketName)
- .orElseThrow(() -> new AwsException("NoSuchBucket",
- "The specified bucket does not exist.", 404));
- bucket.setTags(tags != null ? tags : new java.util.HashMap<>());
- bucketStore.put(bucketName, bucket);
- LOG.debugv("Put tags on bucket: {0}", bucketName);
+ putBucketTaggingForAccount(null, bucketName, tags);
}
public Map<String, String> getBucketTagging(String bucketName) {
- Bucket bucket = bucketStore.get(bucketName)
- .orElseThrow(() -> new AwsException("NoSuchBucket",
- "The specified bucket does not exist.", 404));
+ return getBucketTaggingForAccount(null, bucketName);
+ }
+
+ /**
+ * The tags of the bucket a control-plane tagging ARN names: the bucket of that name in
+ * {@code accountId}'s partition, the calling account's when the ARN carries none, as the
+ * global form does not. A bucket another account owns is read exactly where it lives and
+ * never fallen back to the caller's same-named one: the ARN names another account's bucket
+ * or no bucket at all, and either way the answer is {@code NoSuchBucket}.
+ */
+ public Map<String, String> getBucketTaggingForAccount(String accountId, String bucketName) {
+ Bucket bucket = bucketForAccount(accountId, bucketName);
return bucket.getTags() != null ? bucket.getTags() : Map.of();
}
+ /** Replaces the tags of the bucket a control-plane tagging ARN names, in the partition it names. */
+ public void putBucketTaggingForAccount(String accountId, String bucketName, Map<String, String> tags) {
+ Bucket bucket = bucketForAccount(accountId, bucketName);
+ bucket.setTags(tags != null ? tags : new HashMap<>());
+ writeBucketForAccount(namedAccount(accountId), bucketName, bucket);
+ LOG.debugv("Put tags on bucket {0} of account {1}", bucketName, namedAccount(accountId));
+ }
+
+ /**
+ * The bucket of that name in the account a tagging ARN names, resolved the same way for the
+ * tag read, the tag write and the policy lookup. The calling account's own partition is read
+ * through {@link StorageBackend#get}, which also migrates pre-multi-account data; another
+ * account's is read exactly, and a store with no partitions at all has the one bucket the
+ * name can mean.
+ */
+ private Bucket bucketForAccount(String accountId, String bucketName) {
+ String account = namedAccount(accountId);
+ AccountAwareStorageBackend<Bucket> aware = accountAwareBucketStore();
+ Optional<Bucket> bucket = account.equals(ownerId()) || aware == null
+ ? bucketStore.get(bucketName)
+ : aware.getForAccount(account, bucketName);
+ return bucket.orElseThrow(() -> new AwsException("NoSuchBucket",
+ "The specified bucket does not exist.", 404));
+ }
+
+ /** Writes a bucket back to the partition {@link #bucketForAccount} read it from. */
+ private void writeBucketForAccount(String account, String bucketName, Bucket bucket) {
+ AccountAwareStorageBackend<Bucket> aware = accountAwareBucketStore();
+ if (!account.equals(ownerId()) && aware != null) {
+ aware.putForAccount(account, bucketName, bucket);
+ } else {
+ bucketStore.put(bucketName, bucket);
+ }
+ }
+
+ /** The account an ARN names: its own account field, the calling account's when it carries none. */
+ private String namedAccount(String accountId) {
+ return accountId != null && !accountId.isBlank() ? accountId : ownerId();
+ }
+
+ @SuppressWarnings("unchecked")
+ private AccountAwareStorageBackend<Bucket> accountAwareBucketStore() {
+ return bucketStore instanceof AccountAwareStorageBackend<?> aware
+ ? (AccountAwareStorageBackend<Bucket>) aware : null;
+ }
+
public void putBucketWebsite(String bucketName, WebsiteConfiguration config) {
Bucket bucket = bucketStore.get(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket",
@@ -5437,6 +5487,23 @@ public class S3Service implements Resettable, ResourceProvider {
.map(entry -> new BucketPolicyInfo(entry.value().getPolicy(), entry.account()));
}
+ /**
+ * The policy of the bucket a control-plane tagging ARN names, in the account the ARN names:
+ * the same bucket {@link #getBucketTaggingForAccount} reads the tags of, so a bucket policy
+ * denying the control actions reaches the bucket the handler acts on. The calling account
+ * keeps {@link #findBucketPolicyInfo}'s resolution, which spans partitions under the global
+ * bucket namespace.
+ */
+ public Optional<BucketPolicyInfo> findBucketPolicyInfoForAccount(String accountId, String bucketName) {
+ String account = namedAccount(accountId);
+ AccountAwareStorageBackend<Bucket> aware = accountAwareBucketStore();
+ if (account.equals(ownerId()) || aware == null) {
+ return findBucketPolicyInfo(bucketName);
+ }
+ return aware.getForAccount(account, bucketName)
+ .map(bucket -> new BucketPolicyInfo(bucket.getPolicy(), account));
+ }
+
public Optional<String> findBucketPolicy(String bucketName) {
return findBucketPolicyInfo(bucketName)
.map(BucketPolicyInfo::policy)
diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java
index a25daec38f..ea77bde73c 100644
--- a/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java
@@ -22,7 +22,9 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;
@@ -579,6 +581,84 @@ class IamActionRegistryTest {
assertEquals("s3:PutBucketTagging", s3Action("DELETE", "/bucket", "tagging"));
}
+ @Test
+ void s3ControlRoutesResolveToTheActionsAwsChecks() {
+ String bucketArn = "arn:aws:s3:::repro-bucket";
+ assertEquals("s3:ListTagsForResource", s3Action("GET", "/v20180820/tags/" + bucketArn));
+ assertEquals("s3:TagResource", s3Action("POST", "/v20180820/tags/" + bucketArn));
+ assertEquals("s3:UntagResource", s3Action("DELETE", "/v20180820/tags/" + bucketArn, "tagKeys=Env"));
+ // The Go SDK v2, and so Terraform's provider, percent-encodes the ARN's colons and slashes.
+ String encoded = "arn%3Aaws%3As3%3Aus-east-1%3A000000000000%3Abucket%2Frepro-bucket";
+ assertEquals("s3:ListTagsForResource", s3Action("GET", "/v20180820/tags/" + encoded));
+ assertEquals("s3:TagResource", s3Action("POST", "/v20180820/tags/" + encoded));
+ assertEquals("s3:UntagResource", s3Action("DELETE", "/v20180820/tags/" + encoded, "tagKeys=Env"));
+ assertEquals("s3:ListAccessPoints", s3Action("GET", "/v20180820/accesspoint", "bucket=b"));
+ // The account-level Block Public Access: the User Guide requires PutAccountPublicAccessBlock
+ // or GetAccountPublicAccessBlock, and a delete the same action as the write it undoes.
+ assertEquals("s3:PutAccountPublicAccessBlock",
+ s3Action("PUT", "/v20180820/configuration/publicAccessBlock"));
+ assertEquals("s3:GetAccountPublicAccessBlock",
+ s3Action("GET", "/v20180820/configuration/publicAccessBlock"));
+ assertEquals("s3:PutAccountPublicAccessBlock",
+ s3Action("DELETE", "/v20180820/configuration/publicAccessBlock"));
+ }
+
+ @Test
+ void s3ControlRulesClaimOnlyTheRoutesTheControllerServes() {
+ // PUT is no S3 Control tagging route, so a bucket actually named v20180820
+ // keeps its data-plane actions: the resource stays its own bucket and key.
+ assertEquals("s3:PutObject", s3Action("PUT", "/v20180820/tags/key.txt"));
+ assertEquals("s3:GetObject", s3Action("GET", "/v20180820/other/key.txt"));
+ assertEquals("s3:ListBucket", s3Action("GET", "/v20180820"));
+ assertEquals("s3:CreateBucket", s3Action("PUT", "/v20180820"));
+ }
+
+ /**
+ * A tagging route is matched before the data-plane query overrides: S3ControlController
+ * answers it whatever the query string carries, so {@code ?tagging} on the control route
+ * must not have the call checked as the object tagging of a bucket named v20180820, which a
+ * caller holding only that object action would pass while reading the bucket's tags, and a
+ * caller holding only the control action would fail.
+ */
+ @Test
+ void s3ControlRoutesWinOverTheDataPlaneQueryOverrides() {
+ String bucketArn = "arn:aws:s3:::repro-bucket";
+ assertEquals("s3:ListTagsForResource", s3Action("GET", "/v20180820/tags/" + bucketArn, "tagging"));
+ assertEquals("s3:ListTagsForResource",
+ s3Action("GET", "/v20180820/tags/" + bucketArn, "tagging", "versionId=1"));
+ assertEquals("s3:TagResource", s3Action("POST", "/v20180820/tags/" + bucketArn, "tagging"));
+ assertEquals("s3:UntagResource",
+ s3Action("DELETE", "/v20180820/tags/" + bucketArn, "tagging", "tagKeys=Env"));
+ // The Go SDK v2, and so Terraform's provider, percent-encodes the ARN's colons and slashes.
+ String encoded = "arn%3Aaws%3As3%3Aus-east-1%3A000000000000%3Abucket%2Frepro-bucket";
+ assertEquals("s3:ListTagsForResource", s3Action("GET", "/v20180820/tags/" + encoded, "tagging"));
+ // The data-plane object tagging the query names keeps its own action on its own path.
+ assertEquals("s3:GetObjectTagging", s3Action("GET", "/bucket/key.txt", "tagging"));
+ assertEquals("s3:GetObjectVersionTagging", s3Action("GET", "/bucket/key.txt", "tagging", "versionId=1"));
+ }
+
+ /**
+ * The envelope a denial answers in follows the route and not the action: a request the
+ * control controller serves is an S3 Control one however its query reads, and a data-plane
+ * request is not one, even one travelling under the control prefix, as into a bucket
+ * actually named v20180820.
+ */
+ @Test
+ void theControlRoutesDecideWhetherARequestIsAnS3ControlOne() {
+ assertTrue(IamActionRegistry.isS3ControlRequest("s3",
+ mockCtx("GET", "/v20180820/tags/arn:aws:s3:::repro-bucket",
+ new MultivaluedHashMap<>(), null, "")));
+ assertTrue(IamActionRegistry.isS3ControlRequest("s3",
+ mockCtx("PUT", "/v20180820/configuration/publicAccessBlock",
+ new MultivaluedHashMap<>(), null, "")));
+ assertFalse(IamActionRegistry.isS3ControlRequest("s3",
+ mockCtx("GET", "/v20180820/other/key.txt", new MultivaluedHashMap<>(), null, "")));
+ assertFalse(IamActionRegistry.isS3ControlRequest("s3",
+ mockCtx("GET", "/bucket/key.txt", new MultivaluedHashMap<>(), null, "")));
+ assertFalse(IamActionRegistry.isS3ControlRequest("lambda",
+ mockCtx("GET", "/2015-03-31/functions", new MultivaluedHashMap<>(), null, "")));
+ }
+
@Test
void s3AccelerateYieldsToSubresourcesDispatchedFirst() {
MultivaluedMap<String, String> withRequestPayment = new MultivaluedHashMap<>();
diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamRestRouteCoverageTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamRestRouteCoverageTest.java
index 1dea965726..be31cd3df4 100644
--- a/src/test/java/io/github/hectorvent/floci/services/iam/IamRestRouteCoverageTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamRestRouteCoverageTest.java
@@ -13,6 +13,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -55,6 +56,32 @@ class IamRestRouteCoverageTest {
private static final Set<String> FULLY_ROUTED_MODELS =
Set.of("scheduler", "pipes", "eks", "lambda", "lambda-microvms", "lambda-core", "dsql");
+ /**
+ * The S3 Control operations Floci serves, the rest-routes entry of which is a route the
+ * registry maps. Every other operation of the s3control model has no route in Floci: the
+ * request falls through to the S3 data plane and is checked as the object or bucket
+ * operation that runs.
+ */
+ private static final Set<String> SERVED_S3_CONTROL_OPERATIONS = Set.of(
+ "DeletePublicAccessBlock", "GetPublicAccessBlock", "ListAccessPoints", "ListTagsForResource",
+ "PutPublicAccessBlock", "TagResource", "UntagResource");
+
+ /**
+ * The account-level Block Public Access, the one route the Service Reference cannot settle.
+ * It files S3's and S3 Control's GetPublicAccessBlock, PutPublicAccessBlock and
+ * DeletePublicAccessBlock under the bucket actions (s3:GetBucketPublicAccessBlock,
+ * s3:PutBucketPublicAccessBlock and s3:DeleteBucketPublicAccessBlock), one action between
+ * the two clients, although the account level is a different permission. The S3 User
+ * Guide's Block Public Access page gives the account level s3:PutAccountPublicAccessBlock
+ * to write and s3:GetAccountPublicAccessBlock to read, and a delete needs the same action
+ * as the write it undoes.
+ * https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-control-block-public-access.html
+ */
+ private static final Map<String, String> ACCOUNT_PUBLIC_ACCESS_BLOCK_ACTIONS = Map.of(
+ "GetPublicAccessBlock", "s3:GetAccountPublicAccessBlock",
+ "PutPublicAccessBlock", "s3:PutAccountPublicAccessBlock",
+ "DeletePublicAccessBlock", "s3:PutAccountPublicAccessBlock");
+
private final IamActionRegistry registry = new IamActionRegistry();
@TestFactory
@@ -100,6 +127,39 @@ class IamRestRouteCoverageTest {
return tests.stream();
}
+ /**
+ * Every route of the s3control model, which signs with the s3 scope. A route Floci serves is
+ * checked as the action the Service Reference gives the operation, with the account-level
+ * Block Public Access as its one written-down exception; a route Floci does not serve runs
+ * as the S3 data plane, so it must not resolve to any of the served routes' actions.
+ */
+ @TestFactory
+ Stream<DynamicTest> everyS3ControlRouteIsCheckedAsTheServiceReferenceGivesIt() throws IOException {
+ JsonNode models = load("/aws/rest-routes.json").path("models");
+ JsonNode authorized = load("/aws/iam-authorized-actions.json").path("authorizedActions");
+ Map<String, String> served = new HashMap<>();
+ for (String operation : SERVED_S3_CONTROL_OPERATIONS) {
+ served.put(operation, ACCOUNT_PUBLIC_ACCESS_BLOCK_ACTIONS.getOrDefault(operation,
+ authorized.path("s3").path(operation).asText("s3:" + operation)));
+ }
+ List<DynamicTest> tests = new ArrayList<>();
+ Iterator<Map.Entry<String, JsonNode>> operations =
+ models.path("s3control").path("operations").fields();
+ while (operations.hasNext()) {
+ Map.Entry<String, JsonNode> operation = operations.next();
+ Route route = new Route("s3", "s3control", operation.getKey(),
+ operation.getValue().path("method").asText(),
+ operation.getValue().path("requestUri").asText());
+ String expected = served.get(operation.getKey());
+ tests.add(expected == null
+ ? DynamicTest.dynamicTest("s3control " + operation.getKey() + " (not served)",
+ () -> assertFalse(served.containsValue(resolve(route)), route.toString()))
+ : DynamicTest.dynamicTest("s3control " + operation.getKey(),
+ () -> assertEquals(expected, resolve(route), route.toString())));
+ }
+ return tests.stream();
+ }
+
/** Every model route of {@code operation} among the models Floci authorizes under {@code scope}. */
private static List<Route> routesOf(JsonNode models, String scope, String operation) {
List<Route> routes = new ArrayList<>();
diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java
index 91d7fda01c..8e3cf86649 100644
--- a/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilderTest.java
@@ -1526,6 +1526,74 @@ class ResourceArnBuilderTest {
assertEquals("arn:aws:s3:::my-bucket/folder/", arn);
}
+ // ── S3 Control ─────────────────────────────────────────────────────────────
+
+ /**
+ * An S3 Control tagging route names the resource it acts on by its ARN, and that ARN as
+ * written is what the request is checked against. The data-plane reading of the path, which
+ * issue #6537 reports, checked s3:GetObject on a bucket named v20180820, so a policy
+ * allowing the tagging action on the real bucket never matched. The ARN is the caller's own
+ * naming of the resource, kept as the ARN-carrying requests of other services are: the plain
+ * general-purpose form and the directory-bucket form the Go SDK and Terraform send alike.
+ */
+ @Test
+ void s3ControlTaggingIsCheckedAgainstTheArnTheRouteNames() {
+ for (String action : List.of("s3:ListTagsForResource", "s3:TagResource", "s3:UntagResource")) {
+ when(uriInfo.getPath()).thenReturn("/v20180820/tags/arn:aws:s3:::repro-bucket");
+ assertEquals(List.of("arn:aws:s3:::repro-bucket"),
+ builder.buildResources("s3", action, ctx, "us-east-1", ACCOUNT, "aws"), action);
+ when(uriInfo.getPath())
+ .thenReturn("/v20180820/tags/arn:aws:s3:us-east-1:000000000000:bucket/repro-bucket");
+ assertEquals(List.of("arn:aws:s3:us-east-1:000000000000:bucket/repro-bucket"),
+ builder.buildResources("s3", action, ctx, "us-east-1", ACCOUNT, "aws"), action);
+ }
+ }
+
+ /** A tagging route naming no ARN stays on the wildcard, under which the handler refuses it. */
+ @Test
+ void s3ControlTaggingNamingNoArnLeavesTheWildcard() {
+ when(uriInfo.getPath()).thenReturn("/v20180820/tags/not-an-arn");
+ assertEquals(List.of("*"),
+ builder.buildResources("s3", "s3:ListTagsForResource", ctx, "us-east-1", ACCOUNT, "aws"));
+ }
+
+ /**
+ * AWS gives s3:ListAccessPoints and the account-level Block Public Access actions no
+ * resource type, so the request is checked against the wildcard alone, as the no-resource
+ * actions of other namespaces are (the KMS list operations): a bucket-scoped statement,
+ * {@code arn:<partition>:s3:::*} or any other ARN, grants them nothing, and a NotResource
+ * naming any bucket denies the call.
+ */
+ @Test
+ void s3ControlAccountRoutesAreCheckedAgainstTheWildcardAlone() {
+ when(uriInfo.getPath()).thenReturn("/v20180820/accesspoint");
+ assertEquals(List.of("*"),
+ builder.buildResources("s3", "s3:ListAccessPoints", ctx, "us-east-1", ACCOUNT, "aws"));
+ when(uriInfo.getPath()).thenReturn("/v20180820/configuration/publicAccessBlock");
+ assertEquals(List.of("*"),
+ builder.buildResources("s3", "s3:PutAccountPublicAccessBlock", ctx, "us-east-1", ACCOUNT, "aws"));
+ assertEquals(List.of("*"),
+ builder.buildResources("s3", "s3:GetAccountPublicAccessBlock", ctx, "us-east-1", ACCOUNT, "aws"));
+ assertEquals(List.of("*"),
+ builder.buildResources("s3", "s3:PutAccountPublicAccessBlock", ctx, "us-gov-west-1", ACCOUNT,
+ "aws-us-gov"));
+ }
+
+ /**
+ * The operation tells S3 Control's requests from the data plane, not the path: a request a
+ * data-plane action claims under the same prefix, a bucket actually named v20180820, keeps
+ * the bucket and object ARN of its own path.
+ */
+ @Test
+ void aDataPlaneOperationUnderTheS3ControlPrefixKeepsItsOwnArn() {
+ when(uriInfo.getPath()).thenReturn("/v20180820/tags/key.txt");
+ assertEquals(List.of("arn:aws:s3:::v20180820/tags/key.txt"),
+ builder.buildResources("s3", "s3:PutObject", ctx, "us-east-1", ACCOUNT, "aws"));
+ when(uriInfo.getPath()).thenReturn("/v20180820");
+ assertEquals(List.of("arn:aws:s3:::v20180820"),
+ builder.buildResources("s3", "s3:ListBucket", ctx, "us-east-1", ACCOUNT, "aws"));
+ }
+
// ── The vendored table of request resources ─────────────────────────────────
/**
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ControlIamEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ControlIamEnforcementIntegrationTest.java
new file mode 100644
index 0000000000..5fdf568f6f
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ControlIamEnforcementIntegrationTest.java
@@ -0,0 +1,542 @@
+package io.github.hectorvent.floci.services.s3;
+
+import io.github.hectorvent.floci.testing.EnforcementFixtures;
+import io.github.hectorvent.floci.testing.IamEnforcementProfile;
+import io.github.hectorvent.floci.testing.PartitionCleanup;
+import io.quarkus.test.common.http.TestHTTPResource;
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.TestProfile;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import java.net.URI;
+import java.util.UUID;
+
+import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.not;
+
+/**
+ * With {@code iam.enforcement-enabled=true}, S3 Control's routes are authorized as the {@code s3:}
+ * actions the Service Authorization Reference gives them (S3 Control clients sign with the s3
+ * scope), against the resources those routes name, not as the data-plane object operations their
+ * {@code /v20180820} paths resemble. Issue #6537: a ListTagsForResource was checked as
+ * s3:GetObject on a "bucket" named v20180820, so Terraform's AWS provider, which reads every
+ * bucket's tags through S3 Control, saw 403 under enforcement and never fell back, leaving the
+ * bucket unrefreshable.
+ *
+ * <p>The deny answer is also S3 Control's own REST-XML envelope, not S3's bare {@code <Error>}: the
+ * s3control SDK deserializers read only the {@code <ErrorResponse>} wrapper (issue #557), and the
+ * provider falls back when the message names {@code s3:ListTagsForResource}.
+ */
+@QuarkusTest
+@TestProfile(IamEnforcementProfile.class)
+class S3ControlIamEnforcementIntegrationTest {
+
+ @TestHTTPResource
+ static URI baseUri;
+
+ @RegisterExtension
+ final PartitionCleanup cleanup = new PartitionCleanup();
+
+ private static final String ACCOUNT = "000000000000";
+ private static final String OTHER_ACCOUNT = "222222222222";
+ private static final String S3_CONTROL_ERROR_ENVELOPE =
+ "<ErrorResponse xmlns=\"http://awss3control.amazonaws.com/doc/2018-08-20/\">";
+
+ @Test
+ void listTagsForResourceIsAllowedByItsOwnActionOnTheBucket() {
+ String bucket = bucketWithTags("control-allow");
+ String user = userWith("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:ListTagsForResource","Resource":"arn:aws:s3:::%1$s"}]}"""
+ .formatted(bucket));
+
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .when()
+ .get("/v20180820/tags/arn:aws:s3:::" + bucket)
+ .then()
+ .statusCode(200)
+ .body(containsString("<ListTagsForResourceResult"))
+ .body(containsString("<Key>Env</Key>"))
+ .body(containsString("<Value>dev</Value>"));
+ }
+
+ @Test
+ void listTagsForResourceAcceptsTheGoSdkWireFormOfTheSameArn() {
+ String bucket = bucketWithTags("control-encoded");
+ String user = userWith("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:ListTagsForResource","Resource":"arn:aws:s3:::%1$s"}]}"""
+ .formatted(bucket));
+
+ String encodedArn = "arn%3Aaws%3As3%3A%3A%3A" + bucket;
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .when()
+ .get("/v20180820/tags/" + encodedArn)
+ .then()
+ .statusCode(200)
+ .body(containsString("<Key>Env</Key>"));
+ }
+
+ @Test
+ void aWildcardS3PolicyOnTheBucketAllowsListTagsForResource() {
+ String bucket = bucketWithTags("control-wildcard");
+ String user = userWith("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:*","Resource":["arn:aws:s3:::%1$s","arn:aws:s3:::%1$s/*"]}]}"""
+ .formatted(bucket));
+
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .when()
+ .get("/v20180820/tags/arn:aws:s3:::" + bucket)
+ .then()
+ .statusCode(200);
+ }
+
+ /**
+ * The leak issue #6537 reports in the other direction: with the request checked as
+ * s3:GetObject, an object-read policy authorized reading a bucket's tags through the control
+ * plane. The tagging action is its own, so nothing below s3:ListTagsForResource grants it.
+ */
+ @Test
+ void anObjectReadPolicyDoesNotAuthorizeListTagsForResource() {
+ String bucket = bucketWithTags("control-no-objects");
+ String user = userWith("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:GetObject","Resource":"*"}]}""");
+
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .when()
+ .get("/v20180820/tags/arn:aws:s3:::" + bucket)
+ .then()
+ .statusCode(403)
+ .body(containsString(S3_CONTROL_ERROR_ENVELOPE))
+ .body(containsString("<Code>AccessDenied</Code>"))
+ .body(containsString("is not authorized to perform: s3:ListTagsForResource"));
+ }
+
+ /**
+ * A query parameter the control plane ignores must not re-point the authorization at the
+ * data-plane action it names: S3ControlController reads the bucket's tags whatever
+ * {@code ?tagging} says, so a caller holding only s3:GetObjectTagging on everything is
+ * denied, and a caller holding only the control action is allowed.
+ */
+ @Test
+ void aTaggingQueryOnTheControlRouteDoesNotRepointTheAuthorization() {
+ String bucket = bucketWithTags("control-query");
+ String objectTagger = userWith("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:GetObjectTagging","Resource":"*"}]}""");
+
+ given()
+ .header("Authorization", auth(objectTagger, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .when()
+ .get("/v20180820/tags/arn:aws:s3:::" + bucket + "?tagging")
+ .then()
+ .statusCode(403)
+ .body(containsString("is not authorized to perform: s3:ListTagsForResource"));
+
+ String controlReader = userWith("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:ListTagsForResource","Resource":"arn:aws:s3:::%1$s"}]}"""
+ .formatted(bucket));
+
+ given()
+ .header("Authorization", auth(controlReader, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .when()
+ .get("/v20180820/tags/arn:aws:s3:::" + bucket + "?tagging")
+ .then()
+ .statusCode(200)
+ .body(containsString("<Key>Env</Key>"));
+ }
+
+ /**
+ * The regional ARN form, the one the tagging operation's own request pattern and the AWS
+ * CLI examples use, is the request's resource as written: a policy naming it authorizes the
+ * read, because the ARN is not rewritten into the global form a data-plane path mints.
+ */
+ @Test
+ void listTagsForResourceIsAuthorizedByAPolicyOnTheRegionalArnForm() {
+ String bucket = bucketWithTags("control-regional");
+ String user = userWith("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:ListTagsForResource",
+ "Resource":"arn:aws:s3:us-east-1:000000000000:bucket/%1$s"}]}"""
+ .formatted(bucket));
+
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .when()
+ .get("/v20180820/tags/arn:aws:s3:us-east-1:000000000000:bucket/" + bucket)
+ .then()
+ .statusCode(200)
+ .body(containsString("<ListTagsForResourceResult"))
+ .body(containsString("<Key>Env</Key>"));
+ }
+
+ /**
+ * A bucket policy is resolved from the bucket the tagged ARN names, not from the bucket the
+ * data-plane reading of the ARN's resource part would find: the regional form's resource is
+ * {@code bucket/<name>}, and the real bucket's policy denying the control action must deny a
+ * caller whose identity policy allows it.
+ */
+ @Test
+ void aBucketPolicyOnTheTaggedBucketDeniesTheControlRead() {
+ String bucket = bucketWithTags("control-bucket-policy");
+ given()
+ .header("Authorization", auth("test", "s3"))
+ .contentType("application/json")
+ .body("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Deny","Principal":"*","Action":"s3:ListTagsForResource",
+ "Resource":"arn:aws:s3:us-east-1:000000000000:bucket/%1$s"}]}"""
+ .formatted(bucket))
+ .when()
+ .put("/" + bucket + "?policy")
+ .then()
+ .statusCode(200);
+
+ String user = userWith("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:ListTagsForResource","Resource":"*"}]}""");
+
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .when()
+ .get("/v20180820/tags/arn:aws:s3:us-east-1:000000000000:bucket/" + bucket)
+ .then()
+ .statusCode(403)
+ .body(containsString("is not authorized to perform: s3:ListTagsForResource"));
+ }
+
+ /** The data-plane tagging read is a different action from the control-plane one, as on AWS. */
+ @Test
+ void getBucketTaggingDoesNotAuthorizeListTagsForResource() {
+ String bucket = bucketWithTags("control-dataplane");
+ String user = userWith("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:GetBucketTagging","Resource":"arn:aws:s3:::%1$s"}]}"""
+ .formatted(bucket));
+
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .when()
+ .get("/v20180820/tags/arn:aws:s3:::" + bucket)
+ .then()
+ .statusCode(403)
+ .body(containsString("is not authorized to perform: s3:ListTagsForResource"));
+ }
+
+ @Test
+ void tagResourceAndUntagResourceAreCheckedAsTheirOwnActions() {
+ String bucket = bucketWithTags("control-write");
+ String user = userWith("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":["s3:TagResource","s3:UntagResource","s3:ListTagsForResource"],
+ "Resource":"arn:aws:s3:::%1$s"}]}"""
+ .formatted(bucket));
+
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .contentType("application/xml")
+ .body("<Tagging><Tags><Tag><Key>Owner</Key><Value>team</Value></Tag></Tags></Tagging>")
+ .when()
+ .post("/v20180820/tags/arn:aws:s3:::" + bucket)
+ .then()
+ .statusCode(204);
+
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .when()
+ .get("/v20180820/tags/arn:aws:s3:::" + bucket)
+ .then()
+ .statusCode(200)
+ .body(containsString("<Key>Owner</Key>"));
+
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .queryParam("tagKeys", "Owner")
+ .when()
+ .delete("/v20180820/tags/arn:aws:s3:::" + bucket)
+ .then()
+ .statusCode(204);
+ }
+
+ @Test
+ void anObjectWritePolicyDoesNotAuthorizeTagResource() {
+ String bucket = bucketWithTags("control-no-write");
+ String user = userWith("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:PutObject","Resource":"*"}]}""");
+
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .contentType("application/xml")
+ .body("<Tagging><Tags><Tag><Key>Owner</Key><Value>team</Value></Tag></Tags></Tagging>")
+ .when()
+ .post("/v20180820/tags/arn:aws:s3:::" + bucket)
+ .then()
+ .statusCode(403)
+ .body(containsString("is not authorized to perform: s3:TagResource"));
+ }
+
+ /**
+ * The account-level Block Public Access is a security control, so the account's own actions
+ * gate it: the S3 User Guide requires s3:PutAccountPublicAccessBlock and
+ * s3:GetAccountPublicAccessBlock, and a delete the same action as the write it undoes. The
+ * bucket-level s3:PutBucketPublicAccessBlock must not reach it.
+ */
+ @Test
+ void accountPublicAccessBlockIsAuthorizedAsTheAccountActions() {
+ String user = userWith("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":["s3:PutAccountPublicAccessBlock","s3:GetAccountPublicAccessBlock"],
+ "Resource":"*"}]}""");
+ try {
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .body("<PublicAccessBlockConfiguration><BlockPublicAcls>true</BlockPublicAcls>"
+ + "</PublicAccessBlockConfiguration>")
+ .when()
+ .put("/v20180820/configuration/publicAccessBlock")
+ .then()
+ .statusCode(200);
+
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .when()
+ .get("/v20180820/configuration/publicAccessBlock")
+ .then()
+ .statusCode(200)
+ .body(containsString("<BlockPublicAcls>true</BlockPublicAcls>"));
+ } finally {
+ given()
+ .header("Authorization", auth("test", "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .when()
+ .delete("/v20180820/configuration/publicAccessBlock")
+ .then()
+ .statusCode(204);
+ }
+ }
+
+ @Test
+ void aBucketLevelPublicAccessBlockPolicyDoesNotAuthorizeTheAccountControl() {
+ String user = userWith("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:PutBucketPublicAccessBlock","Resource":"*"}]}""");
+
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .body("<PublicAccessBlockConfiguration><BlockPublicAcls>true</BlockPublicAcls>"
+ + "</PublicAccessBlockConfiguration>")
+ .when()
+ .put("/v20180820/configuration/publicAccessBlock")
+ .then()
+ .statusCode(403)
+ .body(containsString("is not authorized to perform: s3:PutAccountPublicAccessBlock"));
+ }
+
+ @Test
+ void listAccessPointsIsAuthorizedAsItsOwnAction() {
+ String user = userWith("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:ListAccessPoints","Resource":"*"}]}""");
+
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .when()
+ .get("/v20180820/accesspoint")
+ .then()
+ .statusCode(200)
+ .body(containsString("<AccessPointList"));
+ }
+
+ @Test
+ void aListBucketPolicyDoesNotAuthorizeListAccessPoints() {
+ String user = userWith("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":["s3:ListBucket","s3:ListAllMyBuckets"],"Resource":"*"}]}""");
+
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .when()
+ .get("/v20180820/accesspoint")
+ .then()
+ .statusCode(403)
+ .body(containsString("is not authorized to perform: s3:ListAccessPoints"));
+ }
+
+ /**
+ * The regional form names the account whose bucket it acts on, so a grant on
+ * {@code arn:aws:s3:us-east-1:222222222222:bucket/<name>} authorizes a caller in another
+ * account, whom the bucket's own policy admits, to read and change the tags of
+ * 222222222222's bucket, and never of its own same-named one, which the plain bucket name
+ * would otherwise resolve to. The bucket's policy is found in the account the ARN names,
+ * the same bucket the read and the write act on.
+ */
+ @Test
+ void aRegionalArnActsOnTheBucketOfTheAccountItNames() {
+ String bucket = "control-cross-account-" + UUID.randomUUID().toString().substring(0, 8);
+ String regionalArn = "arn:aws:s3:us-east-1:" + OTHER_ACCOUNT + ":bucket/" + bucket;
+ cleanup.register(() -> EnforcementFixtures.removeBucket(baseUri.getPort(),
+ auth(OTHER_ACCOUNT, "s3"), bucket));
+ cleanup.register(() -> EnforcementFixtures.removeBucket(baseUri.getPort(),
+ auth("test", "s3"), bucket));
+ createBucketWithTag(bucket, "Team", "other", auth(OTHER_ACCOUNT, "s3"));
+ createBucketWithTag(bucket, "Team", "own", auth("test", "s3"));
+
+ String userName = "s3-control-cross-" + UUID.randomUUID().toString().substring(0, 8);
+ EnforcementFixtures.UserCredentials credentials = EnforcementFixtures.createUser(cleanup,
+ baseUri.getPort(), auth("test", "iam"), userName);
+ String userArn = "arn:aws:iam::" + ACCOUNT + ":user/" + userName;
+ String identityPolicy = """
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":["s3:ListTagsForResource","s3:TagResource"],
+ "Resource":"%1$s"}]}"""
+ .formatted(regionalArn);
+ EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth("test", "iam"), userName, "p",
+ identityPolicy);
+ String user = credentials.accessKeyId();
+
+ given()
+ .header("Authorization", auth(OTHER_ACCOUNT, "s3"))
+ .contentType("application/json")
+ .body("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Principal":{"AWS":"%1$s"},
+ "Action":["s3:ListTagsForResource","s3:TagResource"],
+ "Resource":"%2$s"}]}"""
+ .formatted(userArn, regionalArn))
+ .when()
+ .put("/" + bucket + "?policy")
+ .then()
+ .statusCode(200);
+
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .when()
+ .get("/v20180820/tags/" + regionalArn)
+ .then()
+ .statusCode(200)
+ .body(containsString("<Key>Team</Key>"))
+ .body(containsString("<Value>other</Value>"));
+
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .contentType("application/xml")
+ .body("<Tagging><Tags><Tag><Key>Owner</Key><Value>team</Value></Tag></Tags></Tagging>")
+ .when()
+ .post("/v20180820/tags/" + regionalArn)
+ .then()
+ .statusCode(204);
+
+ given()
+ .header("Authorization", auth(OTHER_ACCOUNT, "s3"))
+ .when()
+ .get("/" + bucket + "?tagging")
+ .then()
+ .statusCode(200)
+ .body(containsString("<Key>Owner</Key>"))
+ .body(not(containsString("<Value>own</Value>")));
+
+ given()
+ .header("Authorization", auth("test", "s3"))
+ .when()
+ .get("/" + bucket + "?tagging")
+ .then()
+ .statusCode(200)
+ .body(containsString("<Value>own</Value>"))
+ .body(not(containsString("<Key>Owner</Key>")));
+ }
+
+ /**
+ * A tagging ARN naming a bucket its account does not own answers {@code NoSuchBucket}, not
+ * the tags of the caller's same-named bucket: the handler acts on the bucket the ARN names
+ * or on nothing.
+ */
+ @Test
+ void aRegionalArnNamingABucketItsAccountDoesNotOwnAnswersNoSuchBucket() {
+ String bucket = "control-absent-" + UUID.randomUUID().toString().substring(0, 8);
+ cleanup.register(() -> EnforcementFixtures.removeBucket(baseUri.getPort(),
+ auth("test", "s3"), bucket));
+ createBucketWithTag(bucket, "Team", "own", auth("test", "s3"));
+
+ String user = userWith("""
+ {"Version":"2012-10-17","Statement":[
+ {"Effect":"Allow","Action":"s3:ListTagsForResource","Resource":"*"}]}""");
+
+ given()
+ .header("Authorization", auth(user, "s3"))
+ .header("x-amz-account-id", ACCOUNT)
+ .when()
+ .get("/v20180820/tags/arn:aws:s3:us-east-1:" + OTHER_ACCOUNT + ":bucket/" + bucket)
+ .then()
+ .statusCode(404)
+ .body(containsString(S3_CONTROL_ERROR_ENVELOPE))
+ .body(containsString("<Code>NoSuchBucket</Code>"));
+ }
+
+ /** A bucket created by the root stand-in, tagged through the data plane, as Terraform reads it. */
+ private String bucketWithTags(String prefix) {
+ String bucket = prefix + "-" + UUID.randomUUID().toString().substring(0, 8);
+ cleanup.register(() -> EnforcementFixtures.removeBucket(baseUri.getPort(),
+ auth("test", "s3"), bucket));
+ createBucketWithTag(bucket, "Env", "dev", auth("test", "s3"));
+ return bucket;
+ }
+
+ /** A bucket carrying one tag, created and tagged through the data plane under {@code authorization}. */
+ private static void createBucketWithTag(String bucket, String key, String value, String authorization) {
+ given()
+ .header("Authorization", authorization)
+ .when()
+ .put("/" + bucket)
+ .then()
+ .statusCode(200);
+ given()
+ .header("Authorization", authorization)
+ .contentType("application/xml")
+ .body("<Tagging><TagSet><Tag><Key>" + key + "</Key><Value>" + value
+ + "</Value></Tag></TagSet></Tagging>")
+ .when()
+ .put("/" + bucket + "?tagging")
+ .then()
+ .statusCode(204);
+ }
+
+ /** The access key of a new IAM user holding {@code policy} inline. */
+ private String userWith(String policy) {
+ String userName = "s3-control-" + UUID.randomUUID().toString().substring(0, 8);
+ EnforcementFixtures.UserCredentials credentials = EnforcementFixtures.createUser(cleanup,
+ baseUri.getPort(), auth("test", "iam"), userName);
+ EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth("test", "iam"), userName, "p", policy);
+ return credentials.accessKeyId();
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ResourcePolicyProviderTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ResourcePolicyProviderTest.java
index 8895c5f0c6..74150363e5 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3ResourcePolicyProviderTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ResourcePolicyProviderTest.java
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.s3;
+import io.github.hectorvent.floci.services.iam.ResourcePolicyProvider.ResourcePolicy;
import io.github.hectorvent.floci.testing.PartitionMatrix.PartitionCase;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@@ -7,9 +8,14 @@ import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
+import java.util.List;
+import java.util.Optional;
+
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
@@ -54,4 +60,64 @@ class S3ResourcePolicyProviderTest {
assertEquals("arn:aws-cn:sqs:cn-north-1:000000000000:q",
provider.resolveResourceArn("sqs", "arn:aws-cn:sqs:cn-north-1:000000000000:q"));
}
+
+ /**
+ * The regional form an S3 Control tagging route names is the caller's own naming of the
+ * resource, which the tagging operations' own request pattern and the AWS CLI examples use,
+ * so it is kept as written: rewriting it into the global form would deny a policy written
+ * against the regional form, and would read its resource part as a bucket named bucket
+ * with the real name as its key.
+ */
+ @Test
+ void controlTaggingArnsKeepTheBucketTheyName() {
+ assertEquals("audit-source", S3ResourcePolicyProvider.extractBucketName(
+ "arn:aws:s3:us-east-1:000000000000:bucket/audit-source"));
+ assertEquals("audit-source", S3ResourcePolicyProvider.extractBucketName(
+ "arn:aws-cn:s3:cn-north-1:000000000000:bucket/audit-source"));
+
+ S3ResourcePolicyProvider provider = new S3ResourcePolicyProvider(mock(S3Service.class));
+ assertEquals("arn:aws:s3:us-east-1:000000000000:bucket/audit-source",
+ provider.resolveResourceArn("s3", "arn:aws:s3:us-east-1:000000000000:bucket/audit-source"));
+ assertEquals("arn:aws-cn:s3:cn-north-1:000000000000:bucket/audit-source",
+ provider.resolveResourceArn("s3", "arn:aws-cn:s3:cn-north-1:000000000000:bucket/audit-source"));
+ }
+
+ /**
+ * The provider reads ARNs that are already decoded, so it never decodes: an object key
+ * carrying a plus sign or a literal percent sequence keeps its bytes before IAM checks it.
+ */
+ @Test
+ void anObjectKeyIsNotDecodedAgainBeforeThePolicyCheck() {
+ assertEquals("example", S3ResourcePolicyProvider.extractBucketName(
+ "arn:aws:s3:::example/a+b.txt"));
+ S3Service s3Service = mock(S3Service.class);
+ when(s3Service.bucketPartition("example")).thenReturn("aws");
+ S3ResourcePolicyProvider provider = new S3ResourcePolicyProvider(s3Service);
+ assertEquals("arn:aws:s3:::example/a+b.txt",
+ provider.resolveResourceArn("s3", "arn:aws:s3:::example/a+b.txt"));
+ assertEquals("arn:aws:s3:::example/report%20file.txt",
+ provider.resolveResourceArn("s3", "arn:aws:s3:::example/report%20file.txt"));
+ }
+
+ /**
+ * A regional tagging ARN's policy is looked up in the account the ARN names, the same
+ * account whose bucket the handler reads and writes, so a caller in one account cannot be
+ * governed by the bucket policy of its own same-named bucket when the ARN names another
+ * account's.
+ */
+ @Test
+ void controlTaggingArnsLookTheBucketPolicyUpInTheAccountTheyName() {
+ S3Service s3Service = mock(S3Service.class);
+ when(s3Service.findBucketPolicyInfoForAccount("222222222222", "audit-source"))
+ .thenReturn(Optional.of(new S3Service.BucketPolicyInfo("{}", "222222222222")));
+ S3ResourcePolicyProvider provider = new S3ResourcePolicyProvider(s3Service);
+
+ List<ResourcePolicy> policies = provider.getResourcePolicies("s3",
+ "arn:aws:s3:us-east-1:222222222222:bucket/audit-source");
+
+ assertEquals(1, policies.size());
+ assertEquals("222222222222", policies.getFirst().ownerAccountId());
+ verify(s3Service).findBucketPolicyInfoForAccount("222222222222", "audit-source");
+ verify(s3Service, never()).findBucketPolicyInfo("audit-source");
+ }
}
diff --git a/src/test/resources/aws/rest-routes.json b/src/test/resources/aws/rest-routes.json
index 8f6c76e041..57a48a3887 100644
--- a/src/test/resources/aws/rest-routes.json
+++ b/src/test/resources/aws/rest-routes.json
@@ -612,6 +612,109 @@
"WriteGetObjectResponse": {"method": "POST", "requestUri": "/WriteGetObjectResponse"}
}
},
+ "s3control": {
+ "credentialScope": "s3",
+ "apiVersion": "2018-08-20",
+ "operations": {
+ "AssociateAccessGrantsIdentityCenter": {"method": "POST", "requestUri": "/v20180820/accessgrantsinstance/identitycenter"},
+ "CreateAccessGrant": {"method": "POST", "requestUri": "/v20180820/accessgrantsinstance/grant"},
+ "CreateAccessGrantsInstance": {"method": "POST", "requestUri": "/v20180820/accessgrantsinstance"},
+ "CreateAccessGrantsLocation": {"method": "POST", "requestUri": "/v20180820/accessgrantsinstance/location"},
+ "CreateAccessPoint": {"method": "PUT", "requestUri": "/v20180820/accesspoint/{name}"},
+ "CreateAccessPointForObjectLambda": {"method": "PUT", "requestUri": "/v20180820/accesspointforobjectlambda/{name}"},
+ "CreateBucket": {"method": "PUT", "requestUri": "/v20180820/bucket/{name}"},
+ "CreateJob": {"method": "POST", "requestUri": "/v20180820/jobs"},
+ "CreateMultiRegionAccessPoint": {"method": "POST", "requestUri": "/v20180820/async-requests/mrap/create"},
+ "CreateStorageLensGroup": {"method": "POST", "requestUri": "/v20180820/storagelensgroup"},
+ "DeleteAccessGrant": {"method": "DELETE", "requestUri": "/v20180820/accessgrantsinstance/grant/{id}"},
+ "DeleteAccessGrantsInstance": {"method": "DELETE", "requestUri": "/v20180820/accessgrantsinstance"},
+ "DeleteAccessGrantsInstanceResourcePolicy": {"method": "DELETE", "requestUri": "/v20180820/accessgrantsinstance/resourcepolicy"},
+ "DeleteAccessGrantsLocation": {"method": "DELETE", "requestUri": "/v20180820/accessgrantsinstance/location/{id}"},
+ "DeleteAccessPoint": {"method": "DELETE", "requestUri": "/v20180820/accesspoint/{name}"},
+ "DeleteAccessPointForObjectLambda": {"method": "DELETE", "requestUri": "/v20180820/accesspointforobjectlambda/{name}"},
+ "DeleteAccessPointPolicy": {"method": "DELETE", "requestUri": "/v20180820/accesspoint/{name}/policy"},
+ "DeleteAccessPointPolicyForObjectLambda": {"method": "DELETE", "requestUri": "/v20180820/accesspointforobjectlambda/{name}/policy"},
+ "DeleteAccessPointScope": {"method": "DELETE", "requestUri": "/v20180820/accesspoint/{name}/scope"},
+ "DeleteBucket": {"method": "DELETE", "requestUri": "/v20180820/bucket/{name}"},
+ "DeleteBucketLifecycleConfiguration": {"method": "DELETE", "requestUri": "/v20180820/bucket/{name}/lifecycleconfiguration"},
+ "DeleteBucketPolicy": {"method": "DELETE", "requestUri": "/v20180820/bucket/{name}/policy"},
+ "DeleteBucketReplication": {"method": "DELETE", "requestUri": "/v20180820/bucket/{name}/replication"},
+ "DeleteBucketTagging": {"method": "DELETE", "requestUri": "/v20180820/bucket/{name}/tagging"},
+ "DeleteJobTagging": {"method": "DELETE", "requestUri": "/v20180820/jobs/{id}/tagging"},
+ "DeleteMultiRegionAccessPoint": {"method": "POST", "requestUri": "/v20180820/async-requests/mrap/delete"},
+ "DeletePublicAccessBlock": {"method": "DELETE", "requestUri": "/v20180820/configuration/publicAccessBlock"},
+ "DeleteStorageLensConfiguration": {"method": "DELETE", "requestUri": "/v20180820/storagelens/{storagelensid}"},
+ "DeleteStorageLensConfigurationTagging": {"method": "DELETE", "requestUri": "/v20180820/storagelens/{storagelensid}/tagging"},
+ "DeleteStorageLensGroup": {"method": "DELETE", "requestUri": "/v20180820/storagelensgroup/{name}"},
+ "DescribeJob": {"method": "GET", "requestUri": "/v20180820/jobs/{id}"},
+ "DescribeMultiRegionAccessPointOperation": {"method": "GET", "requestUri": "/v20180820/async-requests/mrap/{request_token+}"},
+ "DissociateAccessGrantsIdentityCenter": {"method": "DELETE", "requestUri": "/v20180820/accessgrantsinstance/identitycenter"},
+ "GetAccessGrant": {"method": "GET", "requestUri": "/v20180820/accessgrantsinstance/grant/{id}"},
+ "GetAccessGrantsInstance": {"method": "GET", "requestUri": "/v20180820/accessgrantsinstance"},
+ "GetAccessGrantsInstanceForPrefix": {"method": "GET", "requestUri": "/v20180820/accessgrantsinstance/prefix"},
+ "GetAccessGrantsInstanceResourcePolicy": {"method": "GET", "requestUri": "/v20180820/accessgrantsinstance/resourcepolicy"},
+ "GetAccessGrantsLocation": {"method": "GET", "requestUri": "/v20180820/accessgrantsinstance/location/{id}"},
+ "GetAccessPoint": {"method": "GET", "requestUri": "/v20180820/accesspoint/{name}"},
+ "GetAccessPointConfigurationForObjectLambda": {"method": "GET", "requestUri": "/v20180820/accesspointforobjectlambda/{name}/configuration"},
+ "GetAccessPointForObjectLambda": {"method": "GET", "requestUri": "/v20180820/accesspointforobjectlambda/{name}"},
+ "GetAccessPointPolicy": {"method": "GET", "requestUri": "/v20180820/accesspoint/{name}/policy"},
+ "GetAccessPointPolicyForObjectLambda": {"method": "GET", "requestUri": "/v20180820/accesspointforobjectlambda/{name}/policy"},
+ "GetAccessPointPolicyStatus": {"method": "GET", "requestUri": "/v20180820/accesspoint/{name}/policyStatus"},
+ "GetAccessPointPolicyStatusForObjectLambda": {"method": "GET", "requestUri": "/v20180820/accesspointforobjectlambda/{name}/policyStatus"},
+ "GetAccessPointScope": {"method": "GET", "requestUri": "/v20180820/accesspoint/{name}/scope"},
+ "GetBucket": {"method": "GET", "requestUri": "/v20180820/bucket/{name}"},
+ "GetBucketLifecycleConfiguration": {"method": "GET", "requestUri": "/v20180820/bucket/{name}/lifecycleconfiguration"},
+ "GetBucketPolicy": {"method": "GET", "requestUri": "/v20180820/bucket/{name}/policy"},
+ "GetBucketReplication": {"method": "GET", "requestUri": "/v20180820/bucket/{name}/replication"},
+ "GetBucketTagging": {"method": "GET", "requestUri": "/v20180820/bucket/{name}/tagging"},
+ "GetBucketVersioning": {"method": "GET", "requestUri": "/v20180820/bucket/{name}/versioning"},
+ "GetDataAccess": {"method": "GET", "requestUri": "/v20180820/accessgrantsinstance/dataaccess"},
+ "GetJobTagging": {"method": "GET", "requestUri": "/v20180820/jobs/{id}/tagging"},
+ "GetMultiRegionAccessPoint": {"method": "GET", "requestUri": "/v20180820/mrap/instances/{name+}"},
+ "GetMultiRegionAccessPointPolicy": {"method": "GET", "requestUri": "/v20180820/mrap/instances/{name+}/policy"},
+ "GetMultiRegionAccessPointPolicyStatus": {"method": "GET", "requestUri": "/v20180820/mrap/instances/{name+}/policystatus"},
+ "GetMultiRegionAccessPointRoutes": {"method": "GET", "requestUri": "/v20180820/mrap/instances/{mrap+}/routes"},
+ "GetPublicAccessBlock": {"method": "GET", "requestUri": "/v20180820/configuration/publicAccessBlock"},
+ "GetStorageLensConfiguration": {"method": "GET", "requestUri": "/v20180820/storagelens/{storagelensid}"},
+ "GetStorageLensConfigurationTagging": {"method": "GET", "requestUri": "/v20180820/storagelens/{storagelensid}/tagging"},
+ "GetStorageLensGroup": {"method": "GET", "requestUri": "/v20180820/storagelensgroup/{name}"},
+ "ListAccessGrants": {"method": "GET", "requestUri": "/v20180820/accessgrantsinstance/grants"},
+ "ListAccessGrantsInstances": {"method": "GET", "requestUri": "/v20180820/accessgrantsinstances"},
+ "ListAccessGrantsLocations": {"method": "GET", "requestUri": "/v20180820/accessgrantsinstance/locations"},
+ "ListAccessPoints": {"method": "GET", "requestUri": "/v20180820/accesspoint"},
+ "ListAccessPointsForDirectoryBuckets": {"method": "GET", "requestUri": "/v20180820/accesspointfordirectory"},
+ "ListAccessPointsForObjectLambda": {"method": "GET", "requestUri": "/v20180820/accesspointforobjectlambda"},
+ "ListCallerAccessGrants": {"method": "GET", "requestUri": "/v20180820/accessgrantsinstance/caller/grants"},
+ "ListJobs": {"method": "GET", "requestUri": "/v20180820/jobs"},
+ "ListMultiRegionAccessPoints": {"method": "GET", "requestUri": "/v20180820/mrap/instances"},
+ "ListRegionalBuckets": {"method": "GET", "requestUri": "/v20180820/bucket"},
+ "ListStorageLensConfigurations": {"method": "GET", "requestUri": "/v20180820/storagelens"},
+ "ListStorageLensGroups": {"method": "GET", "requestUri": "/v20180820/storagelensgroup"},
+ "ListTagsForResource": {"method": "GET", "requestUri": "/v20180820/tags/{resourceArn+}"},
+ "PutAccessGrantsInstanceResourcePolicy": {"method": "PUT", "requestUri": "/v20180820/accessgrantsinstance/resourcepolicy"},
+ "PutAccessPointConfigurationForObjectLambda": {"method": "PUT", "requestUri": "/v20180820/accesspointforobjectlambda/{name}/configuration"},
+ "PutAccessPointPolicy": {"method": "PUT", "requestUri": "/v20180820/accesspoint/{name}/policy"},
+ "PutAccessPointPolicyForObjectLambda": {"method": "PUT", "requestUri": "/v20180820/accesspointforobjectlambda/{name}/policy"},
+ "PutAccessPointScope": {"method": "PUT", "requestUri": "/v20180820/accesspoint/{name}/scope"},
+ "PutBucketLifecycleConfiguration": {"method": "PUT", "requestUri": "/v20180820/bucket/{name}/lifecycleconfiguration"},
+ "PutBucketPolicy": {"method": "PUT", "requestUri": "/v20180820/bucket/{name}/policy"},
+ "PutBucketReplication": {"method": "PUT", "requestUri": "/v20180820/bucket/{name}/replication"},
+ "PutBucketTagging": {"method": "PUT", "requestUri": "/v20180820/bucket/{name}/tagging"},
+ "PutBucketVersioning": {"method": "PUT", "requestUri": "/v20180820/bucket/{name}/versioning"},
+ "PutJobTagging": {"method": "PUT", "requestUri": "/v20180820/jobs/{id}/tagging"},
+ "PutMultiRegionAccessPointPolicy": {"method": "POST", "requestUri": "/v20180820/async-requests/mrap/put-policy"},
+ "PutPublicAccessBlock": {"method": "PUT", "requestUri": "/v20180820/configuration/publicAccessBlock"},
+ "PutStorageLensConfiguration": {"method": "PUT", "requestUri": "/v20180820/storagelens/{storagelensid}"},
+ "PutStorageLensConfigurationTagging": {"method": "PUT", "requestUri": "/v20180820/storagelens/{storagelensid}/tagging"},
+ "SubmitMultiRegionAccessPointRoutes": {"method": "PATCH", "requestUri": "/v20180820/mrap/instances/{mrap+}/routes"},
+ "TagResource": {"method": "POST", "requestUri": "/v20180820/tags/{resourceArn+}"},
+ "UntagResource": {"method": "DELETE", "requestUri": "/v20180820/tags/{resourceArn+}"},
+ "UpdateAccessGrantsLocation": {"method": "PUT", "requestUri": "/v20180820/accessgrantsinstance/location/{id}"},
+ "UpdateJobPriority": {"method": "POST", "requestUri": "/v20180820/jobs/{id}/priority"},
+ "UpdateJobStatus": {"method": "POST", "requestUri": "/v20180820/jobs/{id}/status"},
+ "UpdateStorageLensGroup": {"method": "PUT", "requestUri": "/v20180820/storagelensgroup/{name}"}
+ }
+ },
"scheduler": {
"credentialScope": "scheduler",
"apiVersion": "2021-06-30",
diff --git a/tools/aws/regen_rest_routes.py b/tools/aws/regen_rest_routes.py
index fc90ecc563..3b132e8629 100644
--- a/tools/aws/regen_rest_routes.py
+++ b/tools/aws/regen_rest_routes.py
@@ -41,6 +41,7 @@ MODELS: dict[str, str] = {
"lambda-microvms": "lambda",
"pipes": "pipes",
"s3": "s3",
+ "s3control": "s3",
"scheduler": "scheduler",
}