fix(iam): authorize S3 object operations as the actions S3 documents (#5756)

Under IAM enforcement, CreateMultipartUpload, CompleteMultipartUpload, RestoreObject and
SelectObjectContent were never checked against the caller's identity policies. They are POST
requests, the registry had no S3 POST rule, and an action no rule maps is allowed. A user with no S3
permission could start and complete multipart uploads, restore objects and read them through
Select.

Many other S3 requests were checked as the wrong action:
- ListParts and AbortMultipartUpload as s3:GetObject and s3:DeleteObject.
- Object retention and legal hold as s3:GetObject and s3:PutObject, so a user allowed only to write
  objects could set a retention period or a legal hold.
- Object annotations as the plain object actions.
- A request naming a versionId as the current-version action. A deny on s3:DeleteObjectVersion, the
  usual guard against permanent deletes, did not stop one, and reading a version needed s3:GetObject
  rather than s3:GetObjectVersion.
- Analytics, inventory and Intelligent-Tiering configurations as s3:ListBucket, s3:CreateBucket or
  s3:DeleteBucket.
- DeleteBucketTagging as s3:DeleteBucketTagging, which is not an IAM action. A policy naming it
  granted the call, and one granting s3:PutBucketTagging, the action AWS checks, was refused.
- A request naming two sub-resources as the first one the registry looked for. The controller runs
  a different one: GET ?acl&tagging returns the tags while needing only s3:GetObjectAcl.

The object sub-resources are now resolved per method, in the order S3Controller dispatches them. A
versionId selects the version action AWS checks for a read, head, delete, ACL or annotation
request, and for Select, which S3Controller runs on the version named; the bucket configurations
follow the controller's bucket chains. Tagging keeps the tagging actions: the tagging operations
still act on the current version whatever versionId names, so a version tagging action would
authorize one version and let the request change another.

The actions come from the S3 user guide's "Required permissions for S3 API operations" and
multipart permissions tables, the API reference and the Service Reference:
- s3:PutObject for creating and completing an upload;
- s3:RestoreObject, and s3:GetObject for Select;
- s3:AbortMultipartUpload and s3:ListMultipartUploadParts;
- the retention, legal-hold, version and configuration actions;
- s3:PutBucketTagging for DeleteBucketTagging, and each configuration's Put action for its delete.

A write carrying If-Match already needed s3:GetObject as well. The conditional-writes guide lists
PutObject, CopyObject and CompleteMultipartUpload as the operations that take the header, so now
that completing an upload is checked as s3:PutObject it gets that check too, while
CreateMultipartUpload and UploadPart, which ignore the header, no longer do.

A conditional delete with an If-Match ETag already needed s3:GetObject. A delete naming a version is
now s3:DeleteObjectVersion, so that check covers it too, still as s3:GetObject: the
conditional-deletes guide evaluates the condition against the current version only.

Condition keys, per the Service Reference:
- s3:versionid is given to the version actions.
- s3:ExistingObjectTag is given to the version actions and the annotation actions, read from the
  version the request names.
- DeleteBucketTagging, checked as s3:PutBucketTagging, keeps the bucket's tags in its context.

CloudTrail denials name the version actions' operations, and give no name to a POST operation
rather than a wrong one.

The actions an operation needs besides its own, S3 behaviour behind these checks and S3's own
bucket-policy check follow separately (#5716).

Refs #5716
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-15 22:14:39 -05:00
1 parent c23641457a
commit 21184df828
11 files changed
+967 -51

No files matched your search

+47 -6
View File
@@ -1147,6 +1147,43 @@ a Query `Action` field on it does not change the action it is checked as, and it
still percent-encoded, as the router matches it, so an encoded `/` inside a parameter cannot make
the route's rule miss.
An S3 object operation is checked as the action the S3 documentation gives it:
- **Multipart uploads:**
- `CreateMultipartUpload`, `UploadPart` and `CompleteMultipartUpload` are checked as
`s3:PutObject`.
- `AbortMultipartUpload` is checked as `s3:AbortMultipartUpload`.
- `ListParts` is checked as `s3:ListMultipartUploadParts`.
- **Restore and select:** `RestoreObject` is checked as `s3:RestoreObject`, and
`SelectObjectContent` as `s3:GetObject`, or `s3:GetObjectVersion` when it names a version.
- **Object lock:** object retention and legal hold use their own actions: `s3:GetObjectRetention`,
`s3:PutObjectRetention`, `s3:GetObjectLegalHold` and `s3:PutObjectLegalHold`.
- **Annotations:** object annotations use the `s3:*ObjectAnnotation` actions, and
`s3:ListObjectAnnotations` when no annotation name is given.
- **Versions:** a `versionId` selects the version action AWS checks.
- Reading or heading a version needs `s3:GetObjectVersion`; `s3:GetObject` is not needed.
- Deleting a version needs `s3:DeleteObjectVersion`, so a deny on it stops permanent deletes
while a plain delete still adds a delete marker.
- A version's ACL and annotations use `s3:GetObjectVersionAcl`, `s3:PutObjectVersionAcl` and the
`s3:*ObjectVersionAnnotation(s)` actions.
- Tagging a version is still checked as the `s3:*ObjectTagging` actions: the tagging operations
act on the current version whatever `versionId` names.
- S3's own access check under `floci.services.s3.enforce-auth`, which evaluates bucket policies
and ACLs, checks reading, selecting from or deleting a version as `s3:GetObjectVersion` or
`s3:DeleteObjectVersion`, but a version's ACL and annotation requests as the base actions, and
offers no `s3:versionid`.
- Deleting a version with an `If-Match` ETag also needs `s3:GetObject`, as a plain conditional
delete does: the S3 user guide says the condition is evaluated against the current version.
- **Bucket configurations:**
- Analytics, inventory and Intelligent-Tiering configurations use
`s3:Get/PutAnalyticsConfiguration`, `s3:Get/PutInventoryConfiguration` and
`s3:Get/PutIntelligentTieringConfiguration`.
- Removing a configuration needs its Put action.
- `DeleteBucketTagging` is checked as `s3:PutBucketTagging`, since AWS has no
`s3:DeleteBucketTagging` action.
A request naming several of these sub-resources is checked as the one S3 runs. `GET ?acl&tagging`
returns the tags, so it needs `s3:GetObjectTagging`.
A request that names its operation, in a Query `Action`, an `X-Amz-Target` header or a Smithy RPC v2
path (`/service/<service>/operation/<operation>`), is authorized as the action AWS checks for that
operation, which is usually the operation's own name. An RPC v2 call is checked as the service and
@@ -1411,14 +1448,15 @@ floci populates:
`s3:PutBucketTagging` (the `<Tagging>` body).
- `aws:ResourceTag/<key>`: the target resource's current tags, for `ec2:CreateTags`,
`ec2:DeleteTags`, `ec2:TerminateInstances` and `ec2:DescribeInstances` (the first
`ResourceId.N` or `InstanceId.N`), and for `s3:GetBucketTagging`, `s3:DeleteBucketTagging`
and `s3:DeleteBucket` (the bucket). A request naming several EC2 resources is evaluated
`ResourceId.N` or `InstanceId.N`), and for `s3:GetBucketTagging`, `s3:DeleteBucket` and the
`s3:PutBucketTagging` that `DeleteBucketTagging` is checked as (the bucket). A request naming several EC2 resources is evaluated
once per resource and denied when any of them fails the condition, as on AWS.
`logs:CreateLogStream` and `logs:DeleteLogStream` use the owning log group's tags.
- `s3:ExistingObjectTag/<key>`: the tags already on the target object version, for
`s3:GetObject`, `s3:GetObjectTagging`, `s3:GetObjectAcl`, `s3:PutObjectAcl`,
`s3:DeleteObjectTagging` and `s3:PutObjectTagging`. A `versionId` in the request selects the
version whose tags are read. **`s3:DeleteObject` and `s3:PutObject` do not receive this key**, as measured on AWS.
`s3:DeleteObjectTagging`, `s3:PutObjectTagging` and the annotation actions, and for their
version actions (`s3:GetObjectVersion`, `s3:GetObjectVersionAcl` and so on). A `versionId` in the request
selects the version whose tags are read. **`s3:DeleteObject` and `s3:PutObject` do not receive this key**, as measured on AWS.
An allow conditioned on it denies the delete of a correctly tagged object, and a create cannot
be gated on tags an object does not have yet.
- `s3:RequestObjectTag/<key>`: a tag the request asks to attach. `s3:PutObject` reads these
@@ -1433,9 +1471,12 @@ floci populates:
`ForAllValues:StringEquals` can limit all requested keys without a `Null` guard; an
omitted or empty tag set still follows the absent-key rules above. Existing object tags
do not contribute to this requested set. See the [AWS object-tag policy guidance](https://docs.aws.amazon.com/AmazonS3/latest/userguide/tagging-and-policies.html).
- `s3:versionid`: the `versionId` of the request, for the version actions it selects.
- A `PutObject` carrying `If-Match` is authorized as `s3:GetObject` as well, and that second
check is made without the object's tags in the context, as measured on AWS. `If-None-Match`
needs no such permission.
check is made without the object's tags in the context, as measured on AWS. `CopyObject` and
`CompleteMultipartUpload`, the other operations the S3 user guide lists for `If-Match`, get the
same check; `CreateMultipartUpload` and `UploadPart` ignore the header. `If-None-Match` needs no
such permission.
- `aws:PrincipalArn`: the caller's ARN, resolved from the signing access key. It is the
IAM-user ARN for a user access key, and `arn:aws:iam::<account>:root` for the bare account-id
key (floci's account-root principal), matching the ARN shape AWS itself reports for the account
@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.Tag;
import io.github.hectorvent.floci.services.iam.ResourceArnBuilder;
import io.github.hectorvent.floci.services.s3.S3ObjectActions;
import io.github.hectorvent.floci.services.s3.S3Service;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Instance;
@@ -67,6 +68,7 @@ public class IamConditionContextResolver {
public static final String EXISTING_OBJECT_TAG_PREFIX = "s3:ExistingObjectTag/";
public static final String REQUEST_OBJECT_TAG_PREFIX = "s3:RequestObjectTag/";
public static final String REQUEST_OBJECT_TAG_KEYS = "s3:RequestObjectTagKeys";
public static final String VERSION_ID = "s3:versionid";
private final Instance<DynamoDbFacade> dynamoDbFacade;
private final Instance<Ec2Service> ec2Service;
@@ -214,22 +216,40 @@ public class IamConditionContextResolver {
// ── S3 ──────────────────────────────────────────────────────────────────────
private Map<String, List<String>> s3ConditionContext(String action, ContainerRequestContext ctx) {
Map<String, List<String>> conditions = s3ActionConditionContext(action, ctx);
// A version action is checked only for a request naming a version, and carries its id.
if (S3ObjectActions.isVersionAction(action) && ctx.getUriInfo() != null) {
String versionId = ctx.getUriInfo().getQueryParameters().getFirst("versionId");
if (versionId != null) {
conditions = merge(conditions, Map.of(VERSION_ID, List.of(versionId)));
}
}
return conditions;
}
private Map<String, List<String>> s3ActionConditionContext(String action, ContainerRequestContext ctx) {
return switch (action) {
case "s3:ListBucket" -> s3BucketListConditionContext(ctx.getUriInfo().getQueryParameters());
case "s3:PutBucketTagging" -> s3PutBucketTaggingConditionContext(ctx);
case "s3:GetBucketTagging", "s3:DeleteBucketTagging", "s3:DeleteBucket" ->
s3BucketResourceTagConditionContext(ctx);
// DeleteBucketTagging is checked as s3:PutBucketTagging, and sends no tags of its own:
// what it has is the bucket's current tags, as GetBucketTagging does.
case "s3:PutBucketTagging" -> "DELETE".equalsIgnoreCase(ctx.getMethod())
? s3BucketResourceTagConditionContext(ctx)
: s3PutBucketTaggingConditionContext(ctx);
case "s3:GetBucketTagging", "s3:DeleteBucket" -> s3BucketResourceTagConditionContext(ctx);
// Object tags. Which action is given which key is what real AWS was measured to do,
// not what reads naturally: s3:DeleteObject is given neither key, and s3:PutObject is
// given the tags of the REQUEST only, never those of an object it is about to
// overwrite. See S3ObjectTagConditionEnforcementIntegrationTest.
//
// s3:GetObjectVersion is deliberately absent. IamActionRegistry authorizes a GET
// carrying ?versionId= as s3:GetObject, and IamEnforcementFilter is the only caller
// of resolve, so an arm for it would never be reached. The versionId is read from the
// request instead, below.
case "s3:GetObject", "s3:GetObjectTagging", "s3:GetObjectAcl",
"s3:PutObjectAcl", "s3:DeleteObjectTagging" -> s3ExistingObjectTagConditionContext(ctx);
// The version actions IamActionRegistry checks for a request naming a versionId carry
// the key as their base actions do, per the Service Reference, as do the annotation
// actions, and the versionId picks whose tags are read.
case "s3:GetObject", "s3:GetObjectVersion", "s3:GetObjectTagging", "s3:GetObjectAcl",
"s3:GetObjectVersionAcl", "s3:PutObjectAcl", "s3:PutObjectVersionAcl", "s3:DeleteObjectTagging",
"s3:GetObjectAnnotation", "s3:GetObjectVersionAnnotation", "s3:ListObjectAnnotations",
"s3:ListObjectVersionAnnotations", "s3:PutObjectAnnotation", "s3:PutObjectVersionAnnotation",
"s3:DeleteObjectAnnotation", "s3:DeleteObjectVersionAnnotation" ->
s3ExistingObjectTagConditionContext(ctx);
case "s3:PutObject" -> s3RequestObjectTagConditionContext(ctx);
case "s3:PutObjectTagging" -> merge(s3ExistingObjectTagConditionContext(ctx),
s3PutObjectTaggingBodyConditionContext(ctx));
@@ -446,7 +446,10 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
"s3", "", decodedPath.substring(1)).toString());
}
// IAM's resource glob treats * and ? in object keys as patterns. An internal URL
// credential is narrower: it may authorize only its literal object and action.
// credential is narrower: it may authorize only its literal object and action. That
// action is never a version action: a generated URL signs only its X-Amz-* parameters,
// so it names no versionId, and PreSignedUrlFilter refuses one appended to it as a
// signature mismatch before this check.
if (!scope.action().equals(action) || resources.isEmpty()
|| resources.stream().anyMatch(resource -> !scope.resourceArn().equals(resource))) {
ctx.abortWith(accessDeniedForRequest(action, credentialScope, ctx));
@@ -520,15 +523,21 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
// against real AWS: under a GetObject allow conditioned on s3:ExistingObjectTag the
// conditional write is AccessDenied, under a GetObject allow scoped by prefix alone it
// succeeds, and with no GetObject at all it is denied. If-None-Match needs no such
// permission.
if ("s3:PutObject".equals(action) && ctx.getHeaderString("If-Match") != null) {
// permission. The user guide's conditional writes page lists PutObject, CopyObject and
// CompleteMultipartUpload as the operations that take If-Match; CreateMultipartUpload and
// UploadPart, also checked as s3:PutObject, ignore it, so it adds nothing to them.
if ("s3:PutObject".equals(action) && evaluatesIfMatch(ctx) && ctx.getHeaderString("If-Match") != null) {
abortIfDenied(ctx, caller, principal, "s3:GetObject", credentialScope, resources,
withoutObjectTags(targetContexts), region, accountId, akid);
}
// A DeleteObject conditioned on an ETag reveals whether the object still has it, and the
// conditional-deletes guide requires s3:GetObject for it; If-Match: * (existence only)
// needs just s3:DeleteObject. The read is checked the way the PutObject case above is.
if ("s3:DeleteObject".equals(action) && isPlainDeleteObject(ctx)
// needs just the delete. A delete naming a version, checked as s3:DeleteObjectVersion, is
// checked the same way: the guide says the condition is evaluated against the current
// version only, so the read is of that one, s3:GetObject. The read is checked the way the
// PutObject case above is.
if (("s3:DeleteObject".equals(action) || "s3:DeleteObjectVersion".equals(action))
&& isPlainDeleteObject(ctx)
&& isETagCondition(ctx.getHeaderString("If-Match"))) {
abortIfDenied(ctx, caller, principal, "s3:GetObject", credentialScope, resources,
withoutObjectTags(targetContexts), region, accountId, akid);
@@ -536,10 +545,22 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
}
// The DELETE subresources S3Controller.deleteObject routes away from DeleteObject (?uploadId= is
// AbortMultipartUpload, which the action registry also files under s3:DeleteObject). Everything
// else, including the x-id and X-Amz-* parameters SDKs and presigned URLs add, is a DeleteObject.
// AbortMultipartUpload). Everything else, including the x-id and X-Amz-* parameters SDKs and
// presigned URLs add, is a DeleteObject.
private static final Set<String> NON_DELETE_OBJECT_SUBRESOURCES = Set.of("uploadId", "tagging", "annotation");
// PutObject and CopyObject are any PUT S3Controller does not run as UploadPart or UploadPartCopy,
// which name both an uploadId and a partNumber; CompleteMultipartUpload is a POST naming its
// upload, without ?uploads.
private static boolean evaluatesIfMatch(ContainerRequestContext ctx) {
MultivaluedMap<String, String> params = ctx.getUriInfo().getQueryParameters();
boolean namesUpload = params.containsKey("uploadId");
if ("PUT".equalsIgnoreCase(ctx.getMethod())) {
return !(namesUpload && params.containsKey("partNumber"));
}
return "POST".equalsIgnoreCase(ctx.getMethod()) && namesUpload && !params.containsKey("uploads");
}
private static boolean isPlainDeleteObject(ContainerRequestContext ctx) {
return "DELETE".equalsIgnoreCase(ctx.getMethod())
&& ctx.getUriInfo().getQueryParameters().keySet().stream()
@@ -1125,15 +1146,21 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
if (action == null) return null;
// Action set sourced from IamActionRegistry — see that file for any
// additions. HEAD on an object is bucketed under s3:GetObject by the
// registry, so we distinguish via httpMethod.
// registry, so we distinguish via httpMethod. The POST operations checked as
// s3:PutObject or s3:GetObject (multipart create and complete, Select) cannot
// be told apart from the action, so they get no name rather than a wrong one.
if ("POST".equalsIgnoreCase(httpMethod)) {
return null;
}
return switch (action) {
case "s3:GetObject" -> "HEAD".equalsIgnoreCase(httpMethod) ? "HeadObject" : "GetObject";
case "s3:GetObject", "s3:GetObjectVersion" ->
"HEAD".equalsIgnoreCase(httpMethod) ? "HeadObject" : "GetObject";
case "s3:PutObject" -> "PutObject";
case "s3:DeleteObject" -> "DeleteObject";
case "s3:DeleteObject", "s3:DeleteObjectVersion" -> "DeleteObject";
case "s3:ListBucket" -> "ListObjects";
case "s3:ListAllMyBuckets" -> "ListBuckets";
case "s3:GetObjectAcl" -> "GetObjectAcl";
case "s3:PutObjectAcl" -> "PutObjectAcl";
case "s3:GetObjectAcl", "s3:GetObjectVersionAcl" -> "GetObjectAcl";
case "s3:PutObjectAcl", "s3:PutObjectVersionAcl" -> "PutObjectAcl";
case "s3:GetObjectTagging" -> "GetObjectTagging";
case "s3:PutObjectTagging" -> "PutObjectTagging";
case "s3:DeleteObjectTagging" -> "DeleteObjectTagging";
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.core.common.AwsProtocolClaimFilter;
import io.github.hectorvent.floci.core.common.AwsQueryServiceResolver;
import io.github.hectorvent.floci.core.common.ProtocolClaim;
import io.github.hectorvent.floci.core.common.WireProtocol;
import io.github.hectorvent.floci.services.s3.S3ObjectActions;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.core.MediaType;
@@ -411,7 +412,10 @@ public class IamActionRegistry {
sub("requestPayment", "s3:GetBucketRequestPayment"),
sub("accelerate", "s3:GetAccelerateConfiguration"),
sub("replication", "s3:GetReplicationConfiguration"),
sub("metrics", "s3:GetMetricsConfiguration"));
sub("metrics", "s3:GetMetricsConfiguration"),
sub("intelligent-tiering", "s3:GetIntelligentTieringConfiguration"),
sub("analytics", "s3:GetAnalyticsConfiguration"),
sub("inventory", "s3:GetInventoryConfiguration"));
private static final List<SubResourceAction> PUT_BUCKET_SUBRESOURCES = List.of(
sub("notification", "s3:PutBucketNotification"),
@@ -430,13 +434,16 @@ public class IamActionRegistry {
sub("requestPayment", "s3:PutBucketRequestPayment"),
sub("accelerate", "s3:PutAccelerateConfiguration"),
sub("replication", "s3:PutReplicationConfiguration"),
sub("metrics", "s3:PutMetricsConfiguration"));
sub("metrics", "s3:PutMetricsConfiguration"),
sub("intelligent-tiering", "s3:PutIntelligentTieringConfiguration"),
sub("analytics", "s3:PutAnalyticsConfiguration"),
sub("inventory", "s3:PutInventoryConfiguration"));
// AWS gives only DeleteBucketPolicy and DeleteBucketWebsite their own action; removing any other
// sub-resource is authorised by the same Put* action that sets it. ?accelerate is absent because
// S3Controller rejects DELETE on it with 405, so no mapping should claim the request.
private static final List<SubResourceAction> DELETE_BUCKET_SUBRESOURCES = List.of(
sub("tagging", "s3:DeleteBucketTagging"),
sub("tagging", "s3:PutBucketTagging"),
sub("website", "s3:DeleteBucketWebsite"),
sub("policy", "s3:DeleteBucketPolicy"),
sub("cors", "s3:PutBucketCORS"),
@@ -445,7 +452,10 @@ public class IamActionRegistry {
sub("publicAccessBlock", "s3:PutBucketPublicAccessBlock"),
sub("ownershipControls", "s3:PutBucketOwnershipControls"),
sub("replication", "s3:PutReplicationConfiguration"),
sub("metrics", "s3:PutMetricsConfiguration"));
sub("metrics", "s3:PutMetricsConfiguration"),
sub("intelligent-tiering", "s3:PutIntelligentTieringConfiguration"),
sub("analytics", "s3:PutAnalyticsConfiguration"),
sub("inventory", "s3:PutInventoryConfiguration"));
private static String resolveS3SubResourceAction(String method, ContainerRequestContext ctx) {
MultivaluedMap<String, String> params = ctx.getUriInfo().getQueryParameters();
@@ -473,23 +483,98 @@ public class IamActionRegistry {
return null;
}
/**
* The operation an object sub-resource selects, checked in the order S3Controller dispatches
* them: a request naming several runs the first, so it is checked as that one. A
* {@code versionId} selects the version action AWS checks in place of an action that has one
* (see {@link S3ObjectActions#forVersion}). Tagging keeps its own actions: the tagging
* operations act on the current version whatever {@code versionId} names, so a version tagging
* action would authorize one version and let the request change another. Null where the
* controller falls through to the plain object operation on the current version, which the
* rules check.
*/
private static String objectSubResourceAction(String method, MultivaluedMap<String, String> params) {
if (params.containsKey("acl")) {
return switch (method) {
case "GET" -> "s3:GetObjectAcl";
case "PUT" -> "s3:PutObjectAcl";
default -> null;
};
}
if (params.containsKey("tagging")) {
return switch (method) {
case "GET" -> "s3:GetObjectTagging";
case "PUT" -> "s3:PutObjectTagging";
case "DELETE" -> "s3:DeleteObjectTagging";
default -> null;
};
}
return null;
String versionId = params.getFirst("versionId");
return switch (method) {
case "GET" -> {
if (params.containsKey("uploadId")) {
yield "s3:ListMultipartUploadParts";
}
if (params.containsKey("tagging")) {
yield "s3:GetObjectTagging";
}
if (params.containsKey("annotation")) {
yield S3ObjectActions.forVersion(annotationReadAction(params), versionId);
}
if (params.containsKey("retention")) {
yield "s3:GetObjectRetention";
}
if (params.containsKey("legal-hold")) {
yield "s3:GetObjectLegalHold";
}
if (params.containsKey("acl")) {
yield S3ObjectActions.forVersion("s3:GetObjectAcl", versionId);
}
yield versionId != null ? "s3:GetObjectVersion" : null;
}
case "HEAD" -> {
if (params.containsKey("annotation")) {
yield S3ObjectActions.forVersion(annotationReadAction(params), versionId);
}
yield versionId != null ? "s3:GetObjectVersion" : null;
}
case "PUT" -> {
if (params.containsKey("tagging")) {
yield "s3:PutObjectTagging";
}
if (params.containsKey("retention")) {
yield "s3:PutObjectRetention";
}
if (params.containsKey("legal-hold")) {
yield "s3:PutObjectLegalHold";
}
if (params.containsKey("acl")) {
yield S3ObjectActions.forVersion("s3:PutObjectAcl", versionId);
}
// A PUT naming an uploadId and a partNumber is UploadPart, which the rules check as
// s3:PutObject, as they do PutObject and CopyObject.
yield params.containsKey("annotation")
? S3ObjectActions.forVersion("s3:PutObjectAnnotation", versionId)
: null;
}
case "DELETE" -> {
if (params.containsKey("tagging")) {
yield "s3:DeleteObjectTagging";
}
if (params.containsKey("annotation")) {
yield S3ObjectActions.forVersion("s3:DeleteObjectAnnotation", versionId);
}
if (params.containsKey("uploadId")) {
yield "s3:AbortMultipartUpload";
}
yield versionId != null ? "s3:DeleteObjectVersion" : null;
}
case "POST" -> {
if (params.containsKey("uploads")) {
yield "s3:PutObject";
}
if (params.containsKey("restore")) {
yield "s3:RestoreObject";
}
if (params.containsKey("select")) {
// S3Controller selects from the version a versionId names.
yield S3ObjectActions.forVersion("s3:GetObject", versionId);
}
yield params.containsKey("uploadId") ? "s3:PutObject" : null;
}
default -> null;
};
}
/** A named annotation is GetObjectAnnotation; an empty or absent name lists them, as S3Controller does. */
private static String annotationReadAction(MultivaluedMap<String, String> params) {
String name = params.getFirst("annotationName");
return name != null && !name.isEmpty() ? "s3:GetObjectAnnotation" : "s3:ListObjectAnnotations";
}
}
@@ -0,0 +1,41 @@
package io.github.hectorvent.floci.services.s3;
import java.util.Map;
/**
* The version actions AWS checks for an S3 object request that names a {@code versionId}, in place of
* the action the request is otherwise checked as. The source is the Service Authorization Reference
* for S3, which gives each its own action.
*/
public final class S3ObjectActions {
/**
* The object actions that have a version counterpart, which AWS checks in their place when the
* request names a {@code versionId}. Retention, legal hold, restore and the multipart actions
* have none, and are checked as they are. The tagging actions are left out although AWS gives
* them version counterparts: the tagging operations act on the current version whatever
* {@code versionId} names (see IamActionRegistry).
*/
private static final Map<String, String> VERSION_ACTIONS = Map.ofEntries(
Map.entry("s3:GetObject", "s3:GetObjectVersion"),
Map.entry("s3:DeleteObject", "s3:DeleteObjectVersion"),
Map.entry("s3:GetObjectAcl", "s3:GetObjectVersionAcl"),
Map.entry("s3:PutObjectAcl", "s3:PutObjectVersionAcl"),
Map.entry("s3:GetObjectAnnotation", "s3:GetObjectVersionAnnotation"),
Map.entry("s3:ListObjectAnnotations", "s3:ListObjectVersionAnnotations"),
Map.entry("s3:PutObjectAnnotation", "s3:PutObjectVersionAnnotation"),
Map.entry("s3:DeleteObjectAnnotation", "s3:DeleteObjectVersionAnnotation"));
private S3ObjectActions() {
}
/** What a request naming {@code versionId} is checked as: {@code action}'s version counterpart, if it has one. */
public static String forVersion(String action, String versionId) {
return versionId == null ? action : VERSION_ACTIONS.getOrDefault(action, action);
}
/** True for an action AWS checks only for a request naming a version, which carries {@code s3:versionid}. */
public static boolean isVersionAction(String action) {
return VERSION_ACTIONS.containsValue(action);
}
}
@@ -273,6 +273,31 @@ class IamConditionContextResolverTest {
assertFalse(conditions.containsKey("s3:RequestObjectTagKeys"));
}
@Test
void versionActionsCarryTheVersionTheyName() {
ContainerRequestContext request = s3Request("/bucket/object", "");
request.getUriInfo().getQueryParameters().putSingle("versionId", "v1");
when(s3Service.getObjectTagging("bucket", "object", "v1")).thenReturn(Map.of("env", "a"));
Map<String, List<String>> read = resolver.resolve("s3", "s3:GetObjectVersion", request);
assertEquals(List.of("v1"), read.get("s3:versionid"));
assertEquals(List.of("a"), read.get("s3:ExistingObjectTag/env"));
// DeleteObjectVersion carries the id but, as DeleteObject, not the object's tags.
assertEquals(Map.of("s3:versionid", List.of("v1")),
resolver.resolve("s3", "s3:DeleteObjectVersion", request));
// A base action never carries it.
assertFalse(resolver.resolve("s3", "s3:GetObject", request).containsKey("s3:versionid"));
}
@Test
void annotationActionsCarryTheObjectsTags() {
when(s3Service.getObjectTagging("bucket", "object", null)).thenReturn(Map.of("env", "a"));
for (String action : List.of("s3:GetObjectAnnotation", "s3:ListObjectAnnotations",
"s3:PutObjectAnnotation", "s3:DeleteObjectAnnotation")) {
assertEquals(List.of("a"), resolver.resolve("s3", action, s3Request("/bucket/object", ""))
.get("s3:ExistingObjectTag/env"), action);
}
}
@Test
void resolvesDynamoDbLeadingKeysForGetItem() {
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
@@ -458,11 +483,16 @@ class IamConditionContextResolverTest {
void bucketActionsResolveResourceTagsFromTheBucketsCurrentTags() {
when(s3Service.getBucketTagging("my-bucket")).thenReturn(Map.of("Owner", "alice"));
for (String action : List.of("s3:GetBucketTagging", "s3:DeleteBucketTagging", "s3:DeleteBucket")) {
for (String action : List.of("s3:GetBucketTagging", "s3:DeleteBucket")) {
Map<String, List<String>> conditions =
resolver.resolve("s3", action, s3Request("/my-bucket", ""));
assertEquals(List.of("alice"), conditions.get("aws:ResourceTag/Owner"), action);
}
// DeleteBucketTagging, which AWS checks as s3:PutBucketTagging.
ContainerRequestContext deleteTagging = s3Request("/my-bucket", "");
when(deleteTagging.getMethod()).thenReturn("DELETE");
assertEquals(List.of("alice"), resolver.resolve("s3", "s3:PutBucketTagging", deleteTagging)
.get("aws:ResourceTag/Owner"));
}
@Test
@@ -73,6 +73,28 @@ class IamEnforcementFilterUnitTest {
IamEnforcementFilter.mapS3ActionToEventName("s3:DeleteObjectTagging", "DELETE"));
}
@Test
void versionActions_emitAsTheOperationTheyCheck() {
assertEquals("GetObject",
IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersion", "GET"));
assertEquals("HeadObject",
IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersion", "HEAD"));
assertEquals("DeleteObject",
IamEnforcementFilter.mapS3ActionToEventName("s3:DeleteObjectVersion", "DELETE"));
assertEquals("GetObjectAcl",
IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersionAcl", "GET"));
assertEquals("PutObjectAcl",
IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectVersionAcl", "PUT"));
}
@Test
void postOperations_returnNullRatherThanTheWrongName() {
// CreateMultipartUpload and CompleteMultipartUpload are checked as s3:PutObject, and
// SelectObjectContent as s3:GetObject; neither name is the operation that ran.
assertNull(IamEnforcementFilter.mapS3ActionToEventName("s3:PutObject", "POST"));
assertNull(IamEnforcementFilter.mapS3ActionToEventName("s3:GetObject", "POST"));
}
@Test
void unknownAction_returnsNull() {
assertNull(IamEnforcementFilter.mapS3ActionToEventName("s3:GetBucketAcl", "GET"));
@@ -18,6 +18,7 @@ import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -362,6 +363,106 @@ class IamActionRegistryTest {
registry.resolve("s3", mockCtx("PUT", "/bucket/key.txt", accelerate, null, "")));
}
@Test
void s3PostOperationsResolveToTheActionsAwsChecks() {
assertEquals("s3:PutObject", s3Action("POST", "/bucket/key", "uploads"));
assertEquals("s3:PutObject", s3Action("POST", "/bucket/key", "uploadId=u1"));
assertEquals("s3:RestoreObject", s3Action("POST", "/bucket/key", "restore"));
assertEquals("s3:GetObject", s3Action("POST", "/bucket/key", "select", "select-type=2"));
// No bucket-level multipart POST is checked: S3VirtualHostFilter moves a virtual-hosted
// upload's bucket into the path before IAM sees it, so the upload reaches the object rules.
assertNull(s3Action("POST", "/key", "uploads"));
assertNull(s3Action("POST", "/key", "uploadId=u1"));
// DeleteObjects and a browser-form PostObject are checked per key in S3Controller.
assertNull(s3Action("POST", "/bucket", "delete"));
assertNull(s3Action("POST", "/bucket"));
}
@Test
void s3MultipartUploadOperationsResolveToTheirOwnActions() {
assertEquals("s3:ListMultipartUploadParts", s3Action("GET", "/bucket/key", "uploadId=u1"));
assertEquals("s3:AbortMultipartUpload", s3Action("DELETE", "/bucket/key", "uploadId=u1"));
assertEquals("s3:PutObject", s3Action("PUT", "/bucket/key", "uploadId=u1", "partNumber=1"));
assertEquals("s3:ListBucketMultipartUploads", s3Action("GET", "/bucket", "uploads"));
}
@Test
void s3ObjectLockAndAnnotationSubresourcesResolveToTheirOwnActions() {
assertEquals("s3:GetObjectRetention", s3Action("GET", "/bucket/key", "retention"));
assertEquals("s3:PutObjectRetention", s3Action("PUT", "/bucket/key", "retention"));
assertEquals("s3:GetObjectLegalHold", s3Action("GET", "/bucket/key", "legal-hold"));
assertEquals("s3:PutObjectLegalHold", s3Action("PUT", "/bucket/key", "legal-hold"));
assertEquals("s3:GetObjectAnnotation", s3Action("GET", "/bucket/key", "annotation", "annotationName=n"));
assertEquals("s3:ListObjectAnnotations", s3Action("GET", "/bucket/key", "annotation"));
assertEquals("s3:ListObjectAnnotations", s3Action("GET", "/bucket/key", "annotation", "annotationName="));
assertEquals("s3:GetObjectAnnotation", s3Action("HEAD", "/bucket/key", "annotation", "annotationName=n"));
assertEquals("s3:PutObjectAnnotation", s3Action("PUT", "/bucket/key", "annotation"));
assertEquals("s3:DeleteObjectAnnotation", s3Action("DELETE", "/bucket/key", "annotation"));
}
@Test
void s3ObjectSubresourcesFollowTheOrderTheControllerDispatchesThem() {
assertEquals("s3:GetObjectTagging", s3Action("GET", "/bucket/key", "acl", "tagging"));
assertEquals("s3:PutObjectTagging", s3Action("PUT", "/bucket/key", "acl", "tagging"));
assertEquals("s3:ListMultipartUploadParts", s3Action("GET", "/bucket/key", "acl", "uploadId=u1"));
assertEquals("s3:GetObjectRetention", s3Action("GET", "/bucket/key", "acl", "retention"));
assertEquals("s3:DeleteObjectTagging", s3Action("DELETE", "/bucket/key", "acl", "tagging"));
assertEquals("s3:DeleteObjectAnnotation", s3Action("DELETE", "/bucket/key", "annotation", "uploadId=u1"));
assertEquals("s3:PutObject", s3Action("POST", "/bucket/key", "uploads", "restore"));
assertEquals("s3:RestoreObject", s3Action("POST", "/bucket/key", "restore", "uploadId=u1"));
assertEquals("s3:GetObject", s3Action("POST", "/bucket/key", "select", "uploadId=u1"));
}
@Test
void s3VersionedObjectRequestsResolveToTheVersionActions() {
assertEquals("s3:GetObjectVersion", s3Action("GET", "/bucket/key", "versionId=v1"));
assertEquals("s3:GetObjectVersion", s3Action("HEAD", "/bucket/key", "versionId=v1"));
assertEquals("s3:DeleteObjectVersion", s3Action("DELETE", "/bucket/key", "versionId=v1"));
assertEquals("s3:GetObjectVersionAcl", s3Action("GET", "/bucket/key", "acl", "versionId=v1"));
assertEquals("s3:PutObjectVersionAcl", s3Action("PUT", "/bucket/key", "acl", "versionId=v1"));
// Tagging keeps its own actions: the tagging operations act on the current version.
assertEquals("s3:GetObjectTagging", s3Action("GET", "/bucket/key", "tagging", "versionId=v1"));
assertEquals("s3:PutObjectTagging", s3Action("PUT", "/bucket/key", "tagging", "versionId=v1"));
assertEquals("s3:DeleteObjectTagging", s3Action("DELETE", "/bucket/key", "tagging", "versionId=v1"));
assertEquals("s3:GetObjectVersionAnnotation",
s3Action("GET", "/bucket/key", "annotation", "annotationName=n", "versionId=v1"));
assertEquals("s3:GetObjectVersionAnnotation",
s3Action("HEAD", "/bucket/key", "annotation", "annotationName=n", "versionId=v1"));
assertEquals("s3:ListObjectVersionAnnotations",
s3Action("GET", "/bucket/key", "annotation", "versionId=v1"));
assertEquals("s3:PutObjectVersionAnnotation",
s3Action("PUT", "/bucket/key", "annotation", "annotationName=n", "versionId=v1"));
assertEquals("s3:DeleteObjectVersionAnnotation",
s3Action("DELETE", "/bucket/key", "annotation", "annotationName=n", "versionId=v1"));
// GetObjectAttributes of a version is checked as the read of that version.
assertEquals("s3:GetObjectVersion", s3Action("GET", "/bucket/key", "attributes", "versionId=v1"));
// S3Controller selects from the version a versionId names.
assertEquals("s3:GetObjectVersion", s3Action("POST", "/bucket/key", "select", "versionId=v1"));
// Retention and legal hold have no version action; the upload operations take no version.
assertEquals("s3:GetObjectRetention", s3Action("GET", "/bucket/key", "retention", "versionId=v1"));
assertEquals("s3:PutObjectLegalHold", s3Action("PUT", "/bucket/key", "legal-hold", "versionId=v1"));
assertEquals("s3:RestoreObject", s3Action("POST", "/bucket/key", "restore", "versionId=v1"));
assertEquals("s3:GetObject", s3Action("GET", "/bucket/key"));
}
@Test
void s3BucketConfigurationsResolveToTheirOwnActions() {
for (String config : List.of("analytics", "inventory", "intelligent-tiering")) {
String name = switch (config) {
case "analytics" -> "Analytics";
case "inventory" -> "Inventory";
default -> "IntelligentTiering";
};
assertEquals("s3:Get" + name + "Configuration", s3Action("GET", "/bucket", config, "id=c1"));
assertEquals("s3:Get" + name + "Configuration", s3Action("GET", "/bucket", config));
assertEquals("s3:Put" + name + "Configuration", s3Action("PUT", "/bucket", config, "id=c1"));
// AWS checks removing a configuration as the Put action that sets it.
assertEquals("s3:Put" + name + "Configuration", s3Action("DELETE", "/bucket", config, "id=c1"));
}
// s3:DeleteBucketTagging is not an IAM action; AWS checks DeleteBucketTagging as PutBucketTagging.
assertEquals("s3:PutBucketTagging", s3Action("DELETE", "/bucket", "tagging"));
}
@Test
void s3AccelerateYieldsToSubresourcesDispatchedFirst() {
MultivaluedMap<String, String> withRequestPayment = new MultivaluedHashMap<>();
@@ -474,7 +575,7 @@ class IamActionRegistryTest {
MultivaluedMap<String, String> withTagging = new MultivaluedHashMap<>();
withTagging.add("replication", "");
withTagging.add("tagging", "");
assertEquals("s3:DeleteBucketTagging",
assertEquals("s3:PutBucketTagging",
registry.resolve("s3", mockCtx("DELETE", "/bucket", withTagging, null, "")));
}
@@ -556,6 +657,16 @@ class IamActionRegistryTest {
// -------------------------------------------------------------------------
/** The action of an S3 request whose query names {@code params}, each {@code name} or {@code name=value}. */
private String s3Action(String method, String path, String... params) {
MultivaluedMap<String, String> query = new MultivaluedHashMap<>();
for (String param : params) {
int eq = param.indexOf('=');
query.add(eq < 0 ? param : param.substring(0, eq), eq < 0 ? "" : param.substring(eq + 1));
}
return registry.resolve("s3", mockCtx(method, path, query, null, ""));
}
private static ContainerRequestContext mockCtx(String method, String path,
MultivaluedMap<String, String> queryParams,
MediaType mediaType, String body) {
@@ -0,0 +1,514 @@
package io.github.hectorvent.floci.services.iam;
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 io.restassured.path.xml.XmlPath;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import java.net.URI;
import java.util.List;
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;
/**
* Each S3 operation is checked as the action AWS documents for it: the multipart upload operations
* and the other POST operations, which no rule mapped, the multipart operations that were checked as
* a plain read or delete, the object-lock operations, the version actions a versionId selects, and
* the bucket configurations. A request naming several sub-resources is checked as the one S3 runs,
* and the version actions carry the version id and the version's tags.
*/
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
class S3ObjectOperationsIamEnforcementIntegrationTest {
private static final String ADMIN = "test";
private static final String EXISTING_A = "{\"StringEquals\":{\"s3:ExistingObjectTag/env\":\"a\"}}";
@TestHTTPResource("/")
static URI baseUri;
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@Test
void creatingAMultipartUploadNeedsPutObject() {
Fixture f = new Fixture("create", false);
f.policy(f.allow("s3:GetObject"));
denied(f.user().queryParam("uploads", "").when().post(f.path("a/big")).then(), "s3:PutObject");
f.policy(f.allow("s3:PutObject"));
f.user().queryParam("uploads", "").when().post(f.path("a/big")).then().statusCode(200);
}
@Test
void completingAMultipartUploadNeedsPutObject() {
Fixture f = new Fixture("complete", false);
Upload upload = f.uploadOnePart("a/big");
f.policy(f.allow("s3:GetObject"));
denied(f.complete(upload), "s3:PutObject");
f.policy(f.allow("s3:PutObject"));
f.complete(upload).statusCode(200);
}
@Test
void onlyTheWritesThatTakeIfMatchNeedGetObjectForIt() {
// The user guide lists PutObject, CopyObject and CompleteMultipartUpload as the operations
// that take If-Match. Creating an upload and uploading a part ignore it, so it adds no
// s3:GetObject check to them, while completing the upload with it does.
Fixture f = new Fixture("if-match", false);
f.seed("a/big");
String etag = f.etag("a/big");
f.policy(f.allow("s3:PutObject"));
String uploadId = f.user().header("If-Match", etag).queryParam("uploads", "")
.when().post(f.path("a/big")).then().statusCode(200)
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
cleanup.register(() -> f.admin().queryParam("uploadId", uploadId).when().delete(f.path("a/big")));
String partEtag = f.user().header("If-Match", etag)
.queryParam("uploadId", uploadId).queryParam("partNumber", 1).body("part one")
.when().put(f.path("a/big")).then().statusCode(200).extract().header("ETag");
// A PUT naming an upload but no part number is not UploadPart: S3Controller runs it as a
// PutObject, which compares If-Match.
denied(f.user().header("If-Match", etag).queryParam("uploadId", uploadId).body("replaced")
.when().put(f.path("a/big")).then(), "s3:GetObject");
Upload upload = new Upload("a/big", uploadId, partEtag);
// A POST naming both ?uploads and an uploadId creates an upload, which S3Controller runs first.
String another = f.user().header("If-Match", etag).queryParam("uploads", "").queryParam("uploadId", uploadId)
.when().post(f.path("a/big")).then().statusCode(200)
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
cleanup.register(() -> f.admin().queryParam("uploadId", another).when().delete(f.path("a/big")));
denied(f.complete(upload, etag), "s3:GetObject");
f.policy(f.allow("s3:PutObject", "s3:GetObject"));
f.complete(upload, etag).statusCode(200);
}
@Test
void abortingAMultipartUploadNeedsAbortMultipartUpload() {
Fixture f = new Fixture("abort", false);
Upload upload = f.uploadOnePart("a/big");
f.policy(f.allow("s3:DeleteObject"));
denied(f.user().queryParam("uploadId", upload.id()).when().delete(f.path(upload.key())).then(),
"s3:AbortMultipartUpload");
f.policy(f.allow("s3:AbortMultipartUpload"));
f.user().queryParam("uploadId", upload.id()).when().delete(f.path(upload.key())).then().statusCode(204);
}
@Test
void listingThePartsOfAnUploadNeedsListMultipartUploadParts() {
Fixture f = new Fixture("parts", false);
Upload upload = f.uploadOnePart("a/big");
f.policy(f.allow("s3:GetObject"));
denied(f.user().queryParam("uploadId", upload.id()).when().get(f.path(upload.key())).then(),
"s3:ListMultipartUploadParts");
f.policy(f.allow("s3:ListMultipartUploadParts"));
f.user().queryParam("uploadId", upload.id()).when().get(f.path(upload.key())).then().statusCode(200);
}
@Test
void restoringAnObjectNeedsRestoreObject() {
Fixture f = new Fixture("restore", false);
f.seed("a/archived");
f.policy(f.allow("s3:GetObject", "s3:PutObject"));
denied(f.restore("a/archived"), "s3:RestoreObject");
f.policy(f.allow("s3:RestoreObject"));
f.restore("a/archived").statusCode(202);
}
@Test
void selectingFromAnObjectNeedsGetObject() {
Fixture f = new Fixture("select", false);
f.seed("a/rows.csv");
f.policy(f.allow("s3:PutObject"));
denied(f.select("a/rows.csv"), "s3:GetObject");
f.policy(f.allow("s3:GetObject"));
f.select("a/rows.csv").statusCode(200);
}
@Test
void selectingFromAVersionNeedsGetObjectVersion() {
// S3Controller selects from the version named, so it is checked as reading that version.
Fixture f = new Fixture("select-version", true);
String versionId = f.seedVersion("a/rows.csv");
f.policy(f.allow("s3:GetObject"));
denied(f.select("a/rows.csv", versionId), "s3:GetObjectVersion");
f.policy(f.allow("s3:GetObjectVersion"));
f.select("a/rows.csv", versionId).statusCode(200);
}
@Test
void retentionAndLegalHoldNeedTheirOwnActions() {
Fixture f = new Fixture("lock", true);
f.seed("a/locked");
f.policy(f.allow("s3:GetObject", "s3:PutObject"));
denied(f.putRetention("a/locked", "2031-01-01T00:00:00Z"), "s3:PutObjectRetention");
denied(f.user().queryParam("retention", "").when().get(f.path("a/locked")).then(),
"s3:GetObjectRetention");
denied(f.user().queryParam("legal-hold", "").when().get(f.path("a/locked")).then(),
"s3:GetObjectLegalHold");
denied(f.user().queryParam("legal-hold", "")
.body("<LegalHold><Status>OFF</Status></LegalHold>")
.when().put(f.path("a/locked")).then(), "s3:PutObjectLegalHold");
f.policy(f.allow("s3:PutObjectRetention", "s3:GetObjectRetention"));
f.putRetention("a/locked", "2031-01-01T00:00:00Z").statusCode(200);
f.user().queryParam("retention", "").when().get(f.path("a/locked")).then().statusCode(200);
}
@Test
void aRequestNamingTwoSubresourcesIsCheckedAsTheOneThatRuns() {
// S3 serves ?tagging before ?acl, so this request returns the object's tags.
Fixture f = new Fixture("order", false);
f.seed("a/tagged");
f.policy(f.allow("s3:GetObjectAcl"));
denied(f.user().queryParam("acl", "").queryParam("tagging", "").when().get(f.path("a/tagged")).then(),
"s3:GetObjectTagging");
f.policy(f.allow("s3:GetObjectTagging"));
f.user().queryParam("acl", "").queryParam("tagging", "").when().get(f.path("a/tagged")).then()
.statusCode(200)
.body(containsString("<Tagging"));
}
@Test
void readingAVersionNeedsGetObjectVersion() {
// The API reference: with a versionId, s3:GetObjectVersion is needed and s3:GetObject is not.
Fixture f = new Fixture("version-read", true);
String versionId = f.seedVersion("a/doc");
f.policy(f.allow("s3:GetObject"));
denied(f.user().queryParam("versionId", versionId).when().get(f.path("a/doc")).then(),
"s3:GetObjectVersion");
f.user().when().get(f.path("a/doc")).then().statusCode(200);
f.policy(f.allow("s3:GetObjectVersion"));
f.user().queryParam("versionId", versionId).when().get(f.path("a/doc")).then().statusCode(200);
}
@Test
void aDenyOnDeleteObjectVersionStopsAPermanentDelete() {
Fixture f = new Fixture("version-delete", true);
String versionId = f.seedVersion("a/kept");
f.policy(f.allow("s3:*") + ",{\"Effect\":\"Deny\",\"Action\":\"s3:DeleteObjectVersion\","
+ "\"Resource\":\"arn:aws:s3:::" + f.bucket + "/*\"}");
denied(f.user().queryParam("versionId", versionId).when().delete(f.path("a/kept")).then(),
"s3:DeleteObjectVersion");
// A plain delete only adds a delete marker, which s3:DeleteObject allows.
f.user().when().delete(f.path("a/kept")).then().statusCode(204);
}
@Test
void aConditionalDeleteOfAVersionAlsoNeedsGetObject() {
// The conditional-deletes guide: an ETag condition needs s3:GetObject beside the delete, and
// is evaluated against the current version only, so the read is of that one.
Fixture f = new Fixture("version-cas", true);
String versionId = f.seedVersion("a/doc");
String etag = f.etag("a/doc");
f.policy(f.allow("s3:DeleteObjectVersion"));
denied(f.user().header("If-Match", etag).queryParam("versionId", versionId)
.when().delete(f.path("a/doc")).then(), "s3:GetObject");
f.user().header("If-Match", "*").queryParam("versionId", versionId)
.when().delete(f.path("a/doc")).then().statusCode(204);
String next = f.seedVersion("a/doc");
f.policy(f.allow("s3:DeleteObjectVersion", "s3:GetObject"));
f.user().header("If-Match", f.etag("a/doc")).queryParam("versionId", next)
.when().delete(f.path("a/doc")).then().statusCode(204);
}
@Test
void theTagsOfAVersionAreCheckedAsTheTaggingActions() {
// The tagging operations act on the current version whatever versionId names, so a version
// tagging action alone must not let a caller read or change the current version's tags.
Fixture f = new Fixture("version-tags", true);
String versionId = f.seedVersion("a/tagged");
f.policy(f.allow("s3:GetObjectVersionTagging", "s3:PutObjectVersionTagging"));
denied(f.user().queryParam("tagging", "").queryParam("versionId", versionId)
.when().get(f.path("a/tagged")).then(), "s3:GetObjectTagging");
denied(f.user().queryParam("tagging", "").queryParam("versionId", versionId)
.body("<Tagging><TagSet/></Tagging>").when().put(f.path("a/tagged")).then(),
"s3:PutObjectTagging");
f.policy(f.allow("s3:GetObjectTagging"));
f.user().queryParam("tagging", "").queryParam("versionId", versionId)
.when().get(f.path("a/tagged")).then().statusCode(200);
}
@Test
void aVersionConditionMatchesTheVersionNamed() {
Fixture f = new Fixture("version-id", true);
String allowed = f.seedVersion("a/doc");
String other = f.seedVersion("a/doc");
f.policy(f.allowWhen("{\"StringEquals\":{\"s3:versionid\":\"" + allowed + "\"}}",
"s3:GetObjectVersion"));
f.user().queryParam("versionId", allowed).when().get(f.path("a/doc")).then().statusCode(200);
denied(f.user().queryParam("versionId", other).when().get(f.path("a/doc")).then(), "s3:GetObjectVersion");
}
@Test
void theAnnotationsOfAVersionNeedTheVersionAnnotationActions() {
Fixture f = new Fixture("version-annotation", true);
String versionId = f.seedVersion("a/doc");
f.admin().queryParam("annotation", "").queryParam("annotationName", "labels")
.queryParam("versionId", versionId).body("reviewed")
.when().put(f.path("a/doc")).then().statusCode(200);
f.policy(f.allow("s3:GetObjectAnnotation", "s3:ListObjectAnnotations", "s3:PutObjectAnnotation",
"s3:DeleteObjectAnnotation"));
denied(f.annotation(versionId).when().get(f.path("a/doc")).then(), "s3:GetObjectVersionAnnotation");
denied(f.user().queryParam("annotation", "").queryParam("versionId", versionId)
.when().get(f.path("a/doc")).then(), "s3:ListObjectVersionAnnotations");
denied(f.annotation(versionId).body("changed").when().put(f.path("a/doc")).then(),
"s3:PutObjectVersionAnnotation");
denied(f.annotation(versionId).when().delete(f.path("a/doc")).then(), "s3:DeleteObjectVersionAnnotation");
f.policy(f.allow("s3:GetObjectVersionAnnotation", "s3:DeleteObjectVersionAnnotation"));
f.annotation(versionId).when().get(f.path("a/doc")).then().statusCode(200);
f.annotation(versionId).when().delete(f.path("a/doc")).then().statusCode(204);
}
@Test
void anAnnotationIsAuthorizedAgainstTheObjectsTags() {
Fixture f = new Fixture("annotation-tags", false);
f.seed("a/ours");
f.admin().header("x-amz-tagging", "env=b").body("theirs").when().put(f.path("a/theirs"))
.then().statusCode(200);
for (String key : List.of("a/ours", "a/theirs")) {
f.admin().queryParam("annotation", "").queryParam("annotationName", "labels").body("x")
.when().put(f.path(key)).then().statusCode(200);
}
f.policy(f.allowWhen(EXISTING_A, "s3:GetObjectAnnotation"));
f.annotation(null).when().get(f.path("a/ours")).then().statusCode(200);
denied(f.annotation(null).when().get(f.path("a/theirs")).then(), "s3:GetObjectAnnotation");
}
@Test
void bucketConfigurationsAndTagRemovalUseTheActionsAwsChecks() {
Fixture f = new Fixture("bucket-config", false);
f.policy(f.allowOnBucket("s3:ListBucket", "s3:CreateBucket", "s3:DeleteBucket"));
denied(f.user().queryParam("analytics", "").queryParam("id", "a1").when().get("/" + f.bucket).then(),
"s3:GetAnalyticsConfiguration");
denied(f.user().queryParam("inventory", "").when().get("/" + f.bucket).then(),
"s3:GetInventoryConfiguration");
denied(f.user().queryParam("intelligent-tiering", "").queryParam("id", "t1")
.when().delete("/" + f.bucket).then(), "s3:PutIntelligentTieringConfiguration");
// s3:DeleteBucketTagging is no IAM action: AWS checks DeleteBucketTagging as s3:PutBucketTagging.
f.policy(f.allowOnBucket("s3:DeleteBucketTagging"));
denied(f.user().queryParam("tagging", "").when().delete("/" + f.bucket).then(), "s3:PutBucketTagging");
f.policy(f.allowOnBucket("s3:PutBucketTagging"));
f.user().queryParam("tagging", "").when().delete("/" + f.bucket).then().statusCode(204);
}
private static void denied(ValidatableResponse response, String action) {
response.statusCode(403)
.body(containsString("AccessDenied"))
.body(containsString(action));
}
private static String retention(String until) {
return """
<Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Mode>GOVERNANCE</Mode>
<RetainUntilDate>%s</RetainUntilDate>
</Retention>
""".formatted(until);
}
private record Upload(String key, String id, String partEtag) {}
/** One bucket seeded as the admin, and one IAM user whose only policy each test replaces. */
private final class Fixture {
final String bucket;
final String userName;
final String accessKeyId;
Fixture(String name, boolean objectLock) {
String suffix = Long.toString(System.nanoTime(), 36);
bucket = "s3ops-" + name + "-" + suffix;
userName = "s3ops-" + name + "-" + suffix;
cleanup.register(() -> removeBucket(objectLock));
RequestSpecification create = admin();
if (objectLock) {
create = create.header("x-amz-bucket-object-lock-enabled", "true");
}
create.when().put("/" + bucket).then().statusCode(200);
accessKeyId = EnforcementFixtures.createUser(cleanup, baseUri.getPort(),
auth(ADMIN, "iam"), userName).accessKeyId();
}
RequestSpecification admin() {
return given().header("Authorization", auth(ADMIN, "s3"));
}
RequestSpecification user() {
return given().header("Authorization", auth(accessKeyId, "s3"));
}
String path(String key) {
return "/" + bucket + "/" + key;
}
String allowOnBucket(String... actions) {
return "{\"Effect\":\"Allow\",\"Action\":[\"" + String.join("\",\"", actions)
+ "\"],\"Resource\":\"arn:aws:s3:::" + bucket + "\"}";
}
String allowWhen(String condition, String... actions) {
String allow = allow(actions);
return allow.substring(0, allow.length() - 1) + ",\"Condition\":" + condition + "}";
}
String allow(String... actions) {
return "{\"Effect\":\"Allow\",\"Action\":[\"" + String.join("\",\"", actions)
+ "\"],\"Resource\":\"arn:aws:s3:::" + bucket + "/*\"}";
}
void policy(String statement) {
given().formParam("Action", "PutUserPolicy").formParam("UserName", userName)
.formParam("PolicyName", "p")
.formParam("PolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":[" + statement + "]}")
.header("Authorization", auth(ADMIN, "iam")).when().post("/").then().statusCode(200);
}
void seed(String key) {
admin().header("x-amz-tagging", "env=a").body("id,name\n1,one\n")
.when().put(path(key)).then().statusCode(200);
}
/** Writes a version of {@code key} as the admin and returns its version id. */
String seedVersion(String key) {
return seedVersion(key, "env=a");
}
String seedVersion(String key, String tagging) {
return admin().header("x-amz-tagging", tagging).body("version")
.when().put(path(key)).then().statusCode(200).extract().header("x-amz-version-id");
}
/** A user request on the annotation "labels", of the version {@code versionId} names when set. */
RequestSpecification annotation(String versionId) {
RequestSpecification request = user().queryParam("annotation", "").queryParam("annotationName", "labels");
return versionId == null ? request : request.queryParam("versionId", versionId);
}
String etag(String key) {
return admin().when().head(path(key)).then().statusCode(200).extract().header("ETag");
}
Upload uploadOnePart(String key) {
String uploadId = admin().queryParam("uploads", "").when().post(path(key))
.then().statusCode(200)
.extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");
cleanup.register(() -> admin().queryParam("uploadId", uploadId).when().delete(path(key)));
String partEtag = admin().queryParam("uploadId", uploadId).queryParam("partNumber", 1)
.body("part one")
.when().put(path(key)).then().statusCode(200).extract().header("ETag");
return new Upload(key, uploadId, partEtag);
}
ValidatableResponse complete(Upload upload) {
return complete(upload, null);
}
/** Completes {@code upload} as the user, with {@code ifMatch} as its If-Match header when set. */
ValidatableResponse complete(Upload upload, String ifMatch) {
RequestSpecification request = user();
if (ifMatch != null) {
request = request.header("If-Match", ifMatch);
}
return request.queryParam("uploadId", upload.id())
.contentType("application/xml")
.body("<CompleteMultipartUpload><Part><PartNumber>1</PartNumber><ETag>"
+ upload.partEtag() + "</ETag></Part></CompleteMultipartUpload>")
.when().post(path(upload.key())).then();
}
ValidatableResponse restore(String key) {
return user().queryParam("restore", "").contentType("application/xml")
.body("<RestoreRequest><Days>1</Days></RestoreRequest>")
.when().post(path(key)).then();
}
ValidatableResponse select(String key) {
return select(key, null);
}
ValidatableResponse select(String key, String versionId) {
RequestSpecification request = user();
if (versionId != null) {
request = request.queryParam("versionId", versionId);
}
return request.queryParam("select", "").queryParam("select-type", "2")
.contentType("application/xml")
.body("""
<SelectObjectContentRequest>
<Expression>SELECT * FROM S3Object</Expression>
<ExpressionType>SQL</ExpressionType>
<InputSerialization><CSV><FileHeaderInfo>USE</FileHeaderInfo></CSV></InputSerialization>
<OutputSerialization><CSV/></OutputSerialization>
</SelectObjectContentRequest>
""")
.when().post(path(key)).then();
}
ValidatableResponse putRetention(String key, String until) {
return user().queryParam("retention", "").body(retention(until)).when().put(path(key)).then();
}
/**
* Removes every version and delete marker, bypassing governance retention where a test set
* one, then the bucket. No test sets compliance retention or a legal hold, which nothing
* could remove.
*/
void removeBucket(boolean objectLock) {
if (!objectLock) {
EnforcementFixtures.removeBucket(baseUri.getPort(), auth(ADMIN, "s3"), bucket);
return;
}
XmlPath versions = admin().queryParam("versions", "").when().get("/" + bucket)
.then().statusCode(200).extract().xmlPath();
for (String entry : List.of("Version", "DeleteMarker")) {
List<String> keys = versions.getList("ListVersionsResult." + entry + ".Key", String.class);
List<String> ids = versions.getList("ListVersionsResult." + entry + ".VersionId", String.class);
for (int i = 0; i < keys.size(); i++) {
admin().header("x-amz-bypass-governance-retention", "true")
.queryParam("versionId", ids.get(i))
.when().delete(path(keys.get(i))).then().statusCode(204);
}
}
admin().when().delete("/" + bucket).then().statusCode(204);
}
}
}
@@ -129,10 +129,12 @@ class S3ObjectTagConditionEnforcementIntegrationTest {
// Two versions of one key with different tags. The current version carries the tag the
// policy allows; the one under it does not.
String denied = f.seedVersion("a/rolled", "env=b");
f.seedVersion("a/rolled", "env=a");
String allowed = f.seedVersion("a/rolled", "env=a");
f.policy(allow("s3:GetObject", f.objects(), EXISTING_A));
// A read naming a version is checked as s3:GetObjectVersion, which carries the key too.
f.policy(allow("\"s3:GetObject\",\"s3:GetObjectVersion\"", f.objects(), EXISTING_A));
assertEquals(200, f.get("a/rolled"), "the current version carries the allowed tag");
assertEquals(200, f.getVersion("a/rolled", allowed), "the same version, named by its id");
assertEquals(403, f.getVersion("a/rolled", denied),
"the older version carries a foreign tag, so its own tags decide");
}
@@ -198,6 +198,29 @@ class GeneratedPreSignedUrlAuthEnforcementIntegrationTest {
.then().statusCode(200).body(equalTo("untagged"));
}
@Test
void aGeneratedUrlCannotBeTurnedToAnotherVersion() {
// A generated URL is scoped to its object's own action and signs only its X-Amz-*
// parameters, so a versionId appended to it breaks the signature: it never reaches the
// scope check as a request for a version.
String bucket = "generated-presign-version-" + UUID.randomUUID().toString().substring(0, 8);
given().filter(OWNER).when().put("/" + bucket).then().statusCode(200);
given().filter(OWNER).body("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")
.when().put("/" + bucket + "?versioning").then().statusCode(200);
String older = given().filter(OWNER).body("older").when().put("/" + bucket + "/object.txt")
.then().statusCode(200).extract().header("x-amz-version-id");
given().filter(OWNER).body("current").when().put("/" + bucket + "/object.txt").then().statusCode(200);
URI uri = URI.create(presignGenerator.generatePresignedUrl(
"http://localhost:" + RestAssured.port, bucket, "object.txt", "GET", 60));
String pathAndQuery = uri.getRawPath() + "?" + uri.getRawQuery();
given().urlEncodingEnabled(false).when().get(pathAndQuery)
.then().statusCode(200).body(equalTo("current"));
given().urlEncodingEnabled(false).when().get(pathAndQuery + "&versionId=" + older)
.then().statusCode(403).body("Error.Code", equalTo("SignatureDoesNotMatch"));
}
@Test
void generatedUrlsKeepLeadingSlashKeysDistinctUnderEnforcedIam() throws Exception {
String bucket = "generated-presign-slash-" + UUID.randomUUID().toString().substring(0, 8);