Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): check bucket policies for the actions IAM checks (#5913)
Priamo Germosenc17bd49096e3
diff --git a/docs/services/iam.md b/docs/services/iam.mdindex 2137703516..88901902ce 100644--- a/docs/services/iam.md+++ b/docs/services/iam.md@@ -1227,7 +1227,8 @@ An S3 object operation is checked as the action the S3 documentation gives it:- `s3:PutObjectLegalHold` for `x-amz-object-lock-legal-hold`.S3's own access check under `floci.services.s3.enforce-auth`, which evaluates bucket policies- and ACLs, checks only `s3:PutObject` for these operations.+ and ACLs, checks the same actions, and offers the bucket policy the tag and Object Lock keys the+ request sets.- **Restore and select:** `RestoreObject` is checked as `s3:RestoreObject`, and`SelectObjectContent` as `s3:GetObject`, or `s3:GetObjectVersion` when it names a version.- **Attributes:** `GetObjectAttributes` needs `s3:GetObject` and `s3:GetObjectAttributes`, as its@@ -1238,10 +1239,16 @@ An S3 object operation is checked as the action the S3 documentation gives it:- **Object lock:**- Object retention and legal hold use their own actions: `s3:GetObjectRetention`,`s3:PutObjectRetention`, `s3:GetObjectLegalHold` and `s3:PutObjectLegalHold`.- - A `PutObjectRetention` or `DeleteObject` that sends `x-amz-bypass-governance-retention: true`- also needs `s3:BypassGovernanceRetention`, as their API references say. The S3 user guide says- the permission does not apply to annotation operations, so deleting an annotation does not- need it.+ - A request that sends `x-amz-bypass-governance-retention: true` and overrides a governance-mode+ retention also needs `s3:BypassGovernanceRetention`: a `PutObjectRetention` that shortens or+ removes one, or a `DeleteObject` or `DeleteObjects` entry that removes a version under one. The+ S3 user guide's permissions table requires it "if you want to delete an object that's+ protected by governance mode" and to "bypass the governance mode of an Object Lock retention+ configuration", so the header on a request that overrides no lock asks for nothing more. A+ delete that names no version on a versioned bucket only adds a delete marker, and overrides+ none. The request bypasses only a lock the check found: one set after the check is refused. The+ user guide also says the permission does not apply to annotation operations, so deleting an+ annotation does not need it.- **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.@@ -1252,9 +1259,7 @@ An S3 object operation is checked as the action the S3 documentation gives it:- A version's ACL, tags and annotations use `s3:GetObjectVersionAcl`, `s3:PutObjectVersionAcl`,the `s3:*ObjectVersionTagging` actions and the `s3:*ObjectVersionAnnotation(s)` actions.- 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, tagging and annotation requests as the base- actions, and offers no `s3:versionid`.+ and ACLs, checks the same version actions, but offers no `s3:versionid`.- Deleting a version with an `If-Match` ETag also needs `s3:GetObject`, as a plain conditionaldelete does: the S3 user guide says the condition is evaluated against the current version.- **Bucket configurations:**@@ -1585,8 +1590,8 @@ floci populates:- `s3:RequestObjectTag/<key>`: a tag the request asks to attach. `s3:PutObject` reads thesefrom the `x-amz-tagging` header, or its query parameter when the header is absent,and `s3:PutObjectTagging` from the `<Tagging>` body. A `CopyObject` offers them only with- `x-amz-tagging-directive: REPLACE`: under the default `COPY` the copy keeps the source's tags and- ignores the header. Any pair+ `x-amz-tagging-directive: REPLACE`, from the same header or query source the copy then applies:+ under the default `COPY` the copy keeps the source's tags and ignores both. Any pairthat does not decode is dropped, so a policy conditioned on the key denies such a request. Whereenforcement lets it through, the handler still answers a malformed header with`400 InvalidTag`.diff --git a/docs/services/s3.md b/docs/services/s3.mdindex 21e44fe27c..57e673c9d4 100644--- a/docs/services/s3.md+++ b/docs/services/s3.md@@ -365,6 +365,7 @@ When `FLOCI_SERVICES_S3_ENFORCE_AUTH` or `FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED- The bucket's attached policy document (configured via `PutBucketPolicy`) is retrieved.- The caller's authenticated principal ARN is resolved from the signing credentials (for example, `arn:aws:iam::<account>:user/<name>`, `arn:aws:iam::<account>:role/<name>`, or `arn:aws:iam::<account>:root`).- Policy statements are evaluated for the requested S3 action (e.g. `s3:GetObject`, `s3:PutObject`, `s3:PutBucketPolicy`) and resource ARN (`arn:aws:s3:::bucket` or `arn:aws:s3:::bucket/key`).+- An object request is checked as the actions IAM checks for it (see [IAM](iam.md)): the version action when it names a `versionId`, and the actions it needs besides its own, such as `s3:PutObjectTagging` for an object created with tags. The policy is offered the tags and Object Lock settings the request sets (`s3:RequestObjectTag/<key>`, `s3:RequestObjectTagKeys` and the `s3:object-lock-*` keys) besides the keys that say who is calling, but not the other S3 keys, such as `s3:ExistingObjectTag` or `s3:versionid`. An anonymous request's public-policy check is offered none. An anonymous object creation that a public bucket ACL's `WRITE` grant admits may also apply its ACL, tags and Object Lock settings, since an ACL grant names no actions, unless a public bucket policy decides the setting's action.### Supported Principal Typesdiff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java b/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.javaindex ddf87d7280..5e1fe9a887 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java@@ -13,7 +13,10 @@ 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.S3ObjectKeys;import io.github.hectorvent.floci.services.s3.S3Service;+import io.quarkus.vertx.http.runtime.CurrentVertxRequest;+import io.vertx.ext.web.RoutingContext;import jakarta.enterprise.context.ApplicationScoped;import jakarta.enterprise.inject.Instance;import jakarta.inject.Inject;@@ -29,6 +32,8 @@ import java.net.URLDecoder;import java.nio.charset.Charset;import java.nio.charset.StandardCharsets;import java.time.Clock;+import java.time.Instant;+import java.time.format.DateTimeParseException;import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.LinkedHashSet;@@ -82,6 +87,7 @@ public class IamConditionContextResolver {private final Instance<CloudWatchLogsService> logsService;private final RequestContext requestContext;private final EmulatorConfig config;+ private final CurrentVertxRequest currentVertxRequest;private final Clock clock;/**@@ -94,8 +100,10 @@ public class IamConditionContextResolver {Instance<S3Service> s3Service,Instance<CloudWatchLogsService> logsService,RequestContext requestContext,- EmulatorConfig config) {- this(dynamoDbFacade, ec2Service, s3Service, logsService, requestContext, config, Clock.systemUTC());+ EmulatorConfig config,+ CurrentVertxRequest currentVertxRequest) {+ this(dynamoDbFacade, ec2Service, s3Service, logsService, requestContext, config, currentVertxRequest,+ Clock.systemUTC());}IamConditionContextResolver(Instance<DynamoDbFacade> dynamoDbFacade,@@ -104,6 +112,7 @@ public class IamConditionContextResolver {Instance<CloudWatchLogsService> logsService,RequestContext requestContext,EmulatorConfig config,+ CurrentVertxRequest currentVertxRequest,Clock clock) {this.dynamoDbFacade = dynamoDbFacade;this.ec2Service = ec2Service;@@ -111,6 +120,7 @@ public class IamConditionContextResolver {this.logsService = logsService;this.requestContext = requestContext;this.config = config;+ this.currentVertxRequest = currentVertxRequest;this.clock = clock;}@@ -313,17 +323,13 @@ public class IamConditionContextResolver {"s3:DeleteObjectVersionAnnotation" -> s3ExistingObjectTagConditionContext(ctx);// The Object Lock keys come from what the request sets: an object creation's headers, the// retention and legal-hold bodies.- case "s3:PutObject" -> S3ObjectActions.objectCreationConditions(- ctx::getHeaderString, s3RequestTagging(ctx), clock.instant());- case "s3:PutObjectRetention" -> {- String body = bodyText(ctx);- yield S3ObjectActions.objectLockConditions(XmlParser.extractFirst(body, "Mode", null),- XmlParser.extractFirst(body, "RetainUntilDate", null), null, clock.instant());- }- case "s3:PutObjectLegalHold" -> S3ObjectActions.objectLockConditions(null, null,- XmlParser.extractFirst(bodyText(ctx), "Status", null), clock.instant());+ case "s3:PutObject" -> S3ObjectActions.objectCreationConditions(ctx::getHeaderString,+ s3RequestTagging(ctx), S3ObjectActions.isCopy(ctx.getMethod(), ctx::getHeaderString),+ clock.instant());+ case "s3:PutObjectRetention" -> S3ObjectActions.retentionBodyConditions(bodyText(ctx), clock.instant());+ case "s3:PutObjectLegalHold" -> S3ObjectActions.legalHoldBodyConditions(bodyText(ctx), clock.instant());case "s3:PutObjectTagging", "s3:PutObjectVersionTagging" -> merge(- s3ExistingObjectTagConditionContext(ctx), s3PutObjectTaggingBodyConditionContext(ctx));+ s3ExistingObjectTagConditionContext(ctx), S3ObjectActions.taggingBodyConditions(bodyText(ctx)));default -> null;};}@@ -333,6 +339,38 @@ public class IamConditionContextResolver {return body == null ? null : new String(body, StandardCharsets.UTF_8);}+ /**+ * Whether an S3 delete or retention change that sends {@code x-amz-bypass-governance-retention}+ * overrides governance-mode retention, which is when AWS requires {@code s3:BypassGovernanceRetention}+ * for it: a delete that removes a version under a governance lock, or a retention change that+ * shortens or removes one (see {@link S3Service#deleteOverridesGovernanceRetention} and+ * {@link S3Service#retentionChangeOverridesGovernance}). A retention date that does not parse+ * overrides nothing, since the change is refused as malformed.+ */+ boolean s3OverridesGovernanceRetention(String action, ContainerRequestContext ctx) {+ if (ctx.getUriInfo() == null || !s3Service.isResolvable()) {+ return false;+ }+ String bucket = s3BucketName(ctx.getUriInfo().getPath());+ String key = s3RequestObjectKey(ctx, bucket);+ if (key == null) {+ return false;+ }+ String versionId = ctx.getUriInfo().getQueryParameters().getFirst("versionId");+ if (!"s3:PutObjectRetention".equals(action)) {+ return s3Service.get().deleteOverridesGovernanceRetention(bucket, key, versionId);+ }+ String date = XmlParser.extractFirst(bodyText(ctx), "RetainUntilDate", null);+ Instant retainUntil;+ try {+ retainUntil = date == null ? null : Instant.parse(date);+ } catch (DateTimeParseException e) {+ LOG.debugv("Retention date {0} does not parse, so the change overrides no lock", date);+ return false;+ }+ return s3Service.get().retentionChangeOverridesGovernance(bucket, key, versionId, retainUntil);+ }+/*** {@code s3:ExistingObjectTag/<key>} from the tags of the version this request targets. An* object that does not exist, or has no tags, offers no keys, so a {@code StringEquals} on one@@ -346,10 +384,9 @@ public class IamConditionContextResolver {if (ctx.getUriInfo() == null || !s3Service.isResolvable()) {return null;}- String path = ctx.getUriInfo().getPath();- String bucket = s3BucketName(path);- String key = s3ObjectKey(path);- if (bucket == null || key == null) {+ String bucket = s3BucketName(ctx.getUriInfo().getPath());+ String key = s3RequestObjectKey(ctx, bucket);+ if (key == null) {return null;}String versionId = ctx.getUriInfo().getQueryParameters().getFirst("versionId");@@ -382,23 +419,6 @@ public class IamConditionContextResolver {return header;}- /** {@code s3:RequestObjectTag/<key>} from the {@code <Tagging>} XML body of PutObjectTagging. */- private Map<String, List<String>> s3PutObjectTaggingBodyConditionContext(ContainerRequestContext ctx) {- byte[] body = bufferEntity(ctx);- if (body == null || body.length == 0) {- return null;- }- Map<String, String> tags = XmlParser.extractPairs(- new String(body, StandardCharsets.UTF_8), "Tag", "Key", "Value");- if (tags.isEmpty()) {- return null;- }- Map<String, List<String>> conditions = new LinkedHashMap<>();- tags.forEach((k, v) -> conditions.put(REQUEST_OBJECT_TAG_PREFIX + k, List.of(v)));- conditions.put(REQUEST_OBJECT_TAG_KEYS, new ArrayList<>(tags.keySet()));- return conditions;- }-private static Map<String, List<String>> merge(Map<String, List<String>> a, Map<String, List<String>> b) {if (a == null) {return b;@@ -411,8 +431,29 @@ public class IamConditionContextResolver {return merged;}+ /**+ * The object key an S3 request names, null for a bucket-level request. It is read from the path+ * as it arrived, as {@code S3Controller} reads it, since the normalized path drops a key's+ * leading slash and so names another object. Without a current Vert.x request, as in a unit+ * test, the request URI's raw path stands in for it.+ */+ private String s3RequestObjectKey(ContainerRequestContext ctx, String bucket) {+ if (bucket == null) {+ return null;+ }+ String requestUriPath = ctx.getUriInfo().getRequestUri() == null+ ? null : ctx.getUriInfo().getRequestUri().getRawPath();+ RoutingContext current = currentVertxRequest == null ? null : currentVertxRequest.getCurrent();+ String rawPath = current != null && current.request() != null ? current.request().path() : requestUriPath;+ String key = rawPath == null ? null : S3ObjectKeys.fromRawPath(rawPath, requestUriPath, bucket);+ if (key == null) {+ key = s3PathObjectKey(ctx.getUriInfo().getPath());+ }+ return key == null || key.isEmpty() ? null : key;+ }+/** The object key of a path-style (or filter-rewritten) S3 path, null for a bucket-level one. */- private static String s3ObjectKey(String path) {+ private static String s3PathObjectKey(String path) {String stripped = path.startsWith("/") ? path.substring(1) : path;int slash = stripped.indexOf('/');if (slash < 0 || slash == stripped.length() - 1) {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.javaindex 99a2f88f80..ddb1f07326 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@@ -460,7 +460,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter {// permissions to the scoped session.if ("s3:PutObject".equals(action) && isObjectCreation(ctx)) {List<String> creationActions =- S3ObjectActions.objectCreationActions(ctx::getHeaderString, requestTagging(ctx));+ S3ObjectActions.objectCreationActions(ctx::getHeaderString, requestTagging(ctx),+ S3ObjectActions.isCopy(ctx.getMethod(), ctx::getHeaderString));if (!creationActions.isEmpty()) {ctx.abortWith(accessDeniedForRequest(creationActions.getFirst(), credentialScope, ctx));return;@@ -565,14 +566,21 @@ public class IamEnforcementFilter implements ContainerRequestFilter {withoutObjectTags(targetContexts), region, accountId, akid)) {return;}- // Asking to bypass governance-mode retention on a retention change or a delete takes- // s3:BypassGovernanceRetention as well, as the API reference says for both. The user guide- // says the permission does not apply to annotation operations.+ // A retention change or a delete that overrides governance-mode retention takes+ // s3:BypassGovernanceRetention as well. The user guide's permissions table requires it "if you+ // want to delete an object that's protected by governance mode" and to "bypass the governance+ // mode of an Object Lock retention configuration", so the header on a request that overrides+ // no lock asks for nothing more. The user guide says the permission does not apply to+ // annotation operations. The decision is recorded for S3Controller, which lets the operation+ // bypass a lock only when this approved it, so a lock set after this check is not bypassed.if (GOVERNANCE_BYPASS_ACTIONS.contains(action)- && "true".equalsIgnoreCase(ctx.getHeaderString("x-amz-bypass-governance-retention"))- && abortIfDenied(ctx, caller, principal, "s3:BypassGovernanceRetention", action, credentialScope,- resources, targetContexts, region, accountId, akid)) {- return;+ && "true".equalsIgnoreCase(ctx.getHeaderString("x-amz-bypass-governance-retention"))) {+ boolean overrides = conditionContextResolver.s3OverridesGovernanceRetention(action, ctx);+ if (overrides && abortIfDenied(ctx, caller, principal, "s3:BypassGovernanceRetention", action,+ credentialScope, resources, targetContexts, region, accountId, akid)) {+ return;+ }+ requestContext.setIamGovernanceBypass(overrides);}// An object created with an ACL, tags, a retention period or a legal hold also needs that// setting's own action, as the permissions table says for PutObject, CopyObject and@@ -580,7 +588,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter {// request's tags and Object Lock settings.if ("s3:PutObject".equals(action) && isObjectCreation(ctx)) {for (String creationAction- : S3ObjectActions.objectCreationActions(ctx::getHeaderString, requestTagging(ctx))) {+ : S3ObjectActions.objectCreationActions(ctx::getHeaderString, requestTagging(ctx),+ S3ObjectActions.isCopy(ctx.getMethod(), ctx::getHeaderString))) {if (abortIfDenied(ctx, caller, principal, creationAction, action, credentialScope, resources,targetContexts, region, accountId, akid)) {return;diff --git a/src/main/java/io/github/hectorvent/floci/core/common/RequestContext.java b/src/main/java/io/github/hectorvent/floci/core/common/RequestContext.javaindex cb7adced20..fff6427267 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/RequestContext.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/RequestContext.java@@ -15,6 +15,7 @@ public class RequestContext {private String partition;private String accessKeyId;private String sessionToken;+ private Boolean iamGovernanceBypass;public String getAccountId() {return accountId;@@ -60,4 +61,18 @@ public class RequestContext {public void setSessionToken(String sessionToken) {this.sessionToken = sessionToken;}++ /**+ * IAM's decision on an S3 delete or retention change that sends+ * {@code x-amz-bypass-governance-retention}: true when it overrides a governance lock and the caller+ * may bypass it, false when it overrides none, so no bypass permission was checked, and null when+ * IAM did not decide. S3Controller lets the operation bypass a lock only as far as this allows.+ */+ public Boolean getIamGovernanceBypass() {+ return iamGovernanceBypass;+ }++ public void setIamGovernanceBypass(Boolean iamGovernanceBypass) {+ this.iamGovernanceBypass = iamGovernanceBypass;+ }}diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.javaindex 4a2c26edbd..77745eb4e1 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java@@ -64,6 +64,7 @@ import java.net.URLDecoder;import java.nio.charset.StandardCharsets;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;+import java.time.Clock;import java.time.Instant;import java.time.ZoneId;import java.time.format.DateTimeFormatter;@@ -74,6 +75,7 @@ import java.util.Arrays;import java.util.Base64;import java.util.EnumMap;import java.util.HashMap;+import java.util.HashSet;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.List;@@ -131,6 +133,7 @@ public class S3Controller {private final IamService iamService;private final IamEnforcementFilter iamEnforcementFilter;private final EmulatorConfig config;+ private final Clock clock;@Injectpublic S3Controller(S3Service s3Service, S3SelectService s3SelectService,@@ -143,7 +146,8 @@ public class S3Controller {RequestContext requestContext,IamService iamService,IamEnforcementFilter iamEnforcementFilter,- EmulatorConfig config) {+ EmulatorConfig config,+ Clock clock) {this.s3Service = s3Service;this.s3SelectService = s3SelectService;this.regionResolver = regionResolver;@@ -156,6 +160,7 @@ public class S3Controller {this.iamService = iamService;this.iamEnforcementFilter = iamEnforcementFilter;this.config = config;+ this.clock = clock;}private void emitCloudTrailEvent(String eventName, String bucket, String key,@@ -819,27 +824,40 @@ public class S3Controller {S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired(s3Service.isAuthEnforced(), httpHeaders, uriInfo);+ // The sub-resource actions are checked as IAM checks them: as their version counterpart+ // when the request names a version, and offered the tags or Object Lock settings the+ // body sets.+ String versionId = uriInfo.getQueryParameters().getFirst("versionId");if (hasQueryParam(uriInfo, "tagging")) {- s3Service.authorizeObjectWrite(bucket, key, "s3:PutObjectTagging", authorization);- return handlePutObjectTagging(bucket, key, uriInfo.getQueryParameters().getFirst("versionId"),- readWholeBody(body));+ byte[] tags = readWholeBody(body);+ s3Service.authorizeObjectWrite(bucket, key, versionId, "s3:PutObjectTagging", authorization,+ S3ObjectActions.taggingBodyConditions(new String(tags, StandardCharsets.UTF_8)));+ return handlePutObjectTagging(bucket, key, versionId, tags);}if (hasQueryParam(uriInfo, "retention")) {- s3Service.authorizeObjectWrite(bucket, key, "s3:PutObjectRetention", authorization);- if ("true".equalsIgnoreCase(httpHeaders.getHeaderString("x-amz-bypass-governance-retention"))) {+ String retention = new String(readWholeBody(body), StandardCharsets.UTF_8);+ s3Service.authorizeObjectWrite(bucket, key, null, "s3:PutObjectRetention", authorization,+ S3ObjectActions.retentionBodyConditions(retention, clock.instant()));+ // The bypass is needed only for a change that shortens or removes a governance lock.+ boolean bypass = "true".equalsIgnoreCase(+ httpHeaders.getHeaderString("x-amz-bypass-governance-retention"));+ boolean overrides = bypass && s3Service.retentionChangeOverridesGovernance(+ bucket, key, versionId, retentionRetainUntil(retention));+ if (overrides && s3Service.isAuthEnforced()) {s3Service.authorizeObjectWrite(bucket, key, "s3:BypassGovernanceRetention", authorization);}- return handlePutObjectRetention(bucket, key,- uriInfo.getQueryParameters().getFirst("versionId"), httpHeaders, readWholeBody(body));+ return handlePutObjectRetention(bucket, key, versionId, retention, effectiveBypass(bypass, overrides));}if (hasQueryParam(uriInfo, "legal-hold")) {- s3Service.authorizeObjectWrite(bucket, key, "s3:PutObjectLegalHold", authorization);- return handlePutObjectLegalHold(bucket, key,- uriInfo.getQueryParameters().getFirst("versionId"), readWholeBody(body));+ byte[] legalHold = readWholeBody(body);+ s3Service.authorizeObjectWrite(bucket, key, null, "s3:PutObjectLegalHold", authorization,+ S3ObjectActions.legalHoldBodyConditions(+ new String(legalHold, StandardCharsets.UTF_8), clock.instant()));+ return handlePutObjectLegalHold(bucket, key, versionId, legalHold);}if (hasQueryParam(uriInfo, "acl")) {- s3Service.authorizeObjectWrite(bucket, key, "s3:PutObjectAcl", authorization);- s3Service.putObjectAcl(bucket, key, uriInfo.getQueryParameters().getFirst("versionId"),+ s3Service.authorizeObjectWrite(bucket, key, versionId, "s3:PutObjectAcl", authorization, null);+ s3Service.putObjectAcl(bucket, key, versionId,new String(readWholeBody(body), StandardCharsets.UTF_8),httpHeaders.getHeaderString("x-amz-acl"),httpHeaders.getHeaderString("x-amz-grant-read"),@@ -851,7 +869,7 @@ public class S3Controller {}if (hasQueryParam(uriInfo, "annotation")) {- s3Service.authorizeObjectWrite(bucket, key, "s3:PutObjectAnnotation", authorization);+ s3Service.authorizeObjectWrite(bucket, key, versionId, "s3:PutObjectAnnotation", authorization, null);return handlePutObjectAnnotation(bucket, key, readWholeBody(body), uriInfo, httpHeaders);}@@ -883,7 +901,8 @@ public class S3Controller {if (copySource != null && !copySource.isEmpty()) {ObjectLockHeaders lock = objectLockHeaders(httpHeaders);- s3Service.authorizeObjectWrite(bucket, key, "s3:PutObject", authorization);+ authorizeObjectCreation(bucket, key, httpHeaders, uriInfo, true, authorization);+ authorizeConditionalWriteRead(bucket, key, ifMatch, authorization);return handleCopyObject(copySource, bucket, key, contentType, httpHeaders, uriInfo, authorization,lock);}@@ -904,7 +923,8 @@ public class S3Controller {String sseCustomerKey = httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key");String sseCustomerKeyMd5 = httpHeaders.getHeaderString("x-amz-server-side-encryption-customer-key-MD5");String cannedAcl = httpHeaders.getHeaderString("x-amz-acl");- s3Service.authorizePutObject(bucket, key, authorization);+ authorizeObjectCreation(bucket, key, httpHeaders, uriInfo, false, authorization);+ authorizeConditionalWriteRead(bucket, key, ifMatch, authorization);requireDeclaredSizeWithin(httpHeaders, contentEncoding, contentSha256, S3Service.MAX_PUT_OBJECT_SIZE);S3Object obj = s3Service.putObject(bucket, key,decodedBody(body, contentEncoding, contentSha256, trailerHeader(httpHeaders, uriInfo)),@@ -1025,6 +1045,8 @@ public class S3Controller {}}if (hasQueryParam(uriInfo, "attributes")) {+ // GetObjectAttributes needs the read beside its own action, as IAM checks it.+ s3Service.authorizeGetObject(bucket, key, versionId, authorization);s3Service.authorizeObjectRead(bucket, key, versionId, "s3:GetObjectAttributes", authorization);// Merge all x-amz-object-attributes header values (SDK may send multiple lines)List<String> attrHeaders = httpHeaders.getRequestHeader("x-amz-object-attributes");@@ -1493,17 +1515,17 @@ public class S3Controller {s3Service.isAuthEnforced(), httpHeaders, uriInfo);if (hasQueryParam(uriInfo, "tagging")) {- s3Service.authorizeObjectWrite(bucket, key, "s3:DeleteObjectTagging", authorization);+ s3Service.authorizeObjectWrite(bucket, key, versionId, "s3:DeleteObjectTagging", authorization, null);return withVersionId(Response.noContent(), s3Service.deleteObjectTagging(bucket, key, versionId)).build();}if (hasQueryParam(uriInfo, "annotation")) {+ // The user guide: "The BypassGovernanceRetention permission does not apply to annotation+ // operations", so the bypass header asks for no permission of its own here.boolean bypass = "true".equalsIgnoreCase(httpHeaders.getHeaderString("x-amz-bypass-governance-retention"));- s3Service.authorizeObjectWrite(bucket, key, "s3:DeleteObjectAnnotation", authorization);- if (bypass) {- s3Service.authorizeObjectWrite(bucket, key, "s3:BypassGovernanceRetention", authorization);- }+ s3Service.authorizeObjectWrite(bucket, key, versionId, "s3:DeleteObjectAnnotation", authorization,+ null);return handleDeleteObjectAnnotation(bucket, key, uriInfo, httpHeaders, bypass);}if (uploadId != null) {@@ -1516,10 +1538,13 @@ public class S3Controller {String ifMatch = httpHeaders.getHeaderString("If-Match");s3Service.authorizeDeleteObject(bucket, key, versionId, authorization);authorizeDeleteETagRead(bucket, key, ifMatch, authorization);- if (bypass) {+ // The bypass is needed only for a delete that removes a version under a governance lock.+ boolean overrides = bypass && s3Service.deleteOverridesGovernanceRetention(bucket, key, versionId);+ if (overrides && s3Service.isAuthEnforced()) {s3Service.authorizeObjectWrite(bucket, key, "s3:BypassGovernanceRetention", authorization);}- S3Object result = s3Service.deleteObject(bucket, key, versionId, bypass, ifMatch);+ S3Object result = s3Service.deleteObject(bucket, key, versionId, effectiveBypass(bypass, overrides),+ ifMatch);var resp = Response.noContent();if (result != null) {if (result.isDeleteMarker()) {@@ -1594,7 +1619,7 @@ public class S3Controller {if (hasQueryParam(uriInfo, "uploads")) {ObjectLockHeaders lock = objectLockHeaders(httpHeaders);- s3Service.authorizeObjectWrite(bucket, key, "s3:PutObject", authorization);+ authorizeObjectCreation(bucket, key, httpHeaders, uriInfo, false, authorization);MultipartUpload upload = s3Service.initiateMultipartUpload(bucket, key, contentType,extractUserMetadata(httpHeaders, uriInfo),resolveHeaderOrQueryParam(httpHeaders, uriInfo, "x-amz-checksum-type"),@@ -1659,6 +1684,7 @@ public class S3Controller {if (uploadId != null) {s3Service.authorizeObjectWrite(bucket, key, "s3:PutObject", authorization);+ authorizeConditionalWriteRead(bucket, key, ifMatch, authorization);List<CompletedPart> completedParts = parseCompleteMultipartBody(new String(body, StandardCharsets.UTF_8));List<Integer> partNumbers = completedParts.stream().map(CompletedPart::partNumber).toList();Response preconditionResponse = checkWritePreconditions(bucket, key, ifMatch, ifNoneMatch);@@ -1725,19 +1751,28 @@ public class S3Controller {String iamCredential = iamCredential(httpHeaders, uriInfo);List<XmlParser.KeyVersion> authorizedEntries = new ArrayList<>();List<S3Service.DeleteError> authorizationErrors = new ArrayList<>();+ // With an access check on, an entry may bypass a lock only when the bypass was checked for it,+ // so a lock set after the checks is refused rather than bypassed unchecked.+ boolean checked = s3Service.isAuthEnforced()+ || (config.services().iam().enforcementEnabled() && iamCredential != null);+ Set<XmlParser.KeyVersion> bypassingEntries = new HashSet<>();for (XmlParser.KeyVersion entry : entries) {try {authorizeDeleteEntryIdentity(iamCredential, bucket, entry);s3Service.authorizeDeleteObject(bucket, entry.key(), entry.versionId(), authorization);authorizeDeleteETagRead(bucket, entry.key(), entry.eTag(), authorization);- if (bypass && s3Service.isGovernanceRetentionActive(- bucket, entry.key(), entry.versionId())) {+ boolean overrides = bypass && s3Service.deleteOverridesGovernanceRetention(+ bucket, entry.key(), entry.versionId());+ if (overrides) {// The bypass is a permission of its own, so IAM evaluates it against the caller's// policies as it does the delete, not only the bucket-policy check that follows.authorizeEntryIdentity(iamCredential, "s3:BypassGovernanceRetention", bucket, entry.key());s3Service.authorizeObjectWrite(bucket, entry.key(),"s3:BypassGovernanceRetention", authorization);}+ if (checked ? overrides : bypass) {+ bypassingEntries.add(entry);+ }authorizedEntries.add(entry);} catch (AwsException e) {authorizationErrors.add(new S3Service.DeleteError(@@ -1745,7 +1780,8 @@ public class S3Controller {}}- S3Service.DeleteObjectsResult result = s3Service.deleteObjects(bucket, authorizedEntries, bypass);+ S3Service.DeleteObjectsResult result =+ s3Service.deleteObjects(bucket, authorizedEntries, bypassingEntries::contains);XmlBuilder builder = new XmlBuilder().raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")@@ -1787,6 +1823,57 @@ public class S3Controller {.end("Error");}+ /**+ * Checks a PutObject, CopyObject or CreateMultipartUpload against the bucket policy as the+ * enforcement filter checks it against identity policies: s3:PutObject, and the action each+ * setting it applies to the new object needs besides it (see+ * {@link S3ObjectActions#objectCreationActions}). Each is offered the condition keys the request+ * sets, its tags and Object Lock settings. An anonymous creation that a public bucket ACL admits+ * may apply its settings (see {@link S3Service#authorizeObjectCreationSetting}).+ */+ private void authorizeObjectCreation(String bucket, String key, HttpHeaders httpHeaders, UriInfo uriInfo,+ boolean copy, S3Service.RequestAuthorization authorization) {+ if (!s3Service.isAuthEnforced()) {+ return;+ }+ String tagging = resolveInlineTaggingSource(httpHeaders.getHeaderString("x-amz-tagging"), uriInfo);+ Map<String, List<String>> conditions =+ S3ObjectActions.objectCreationConditions(httpHeaders::getHeaderString, tagging, copy, clock.instant());+ s3Service.authorizeObjectWrite(bucket, key, null, "s3:PutObject", authorization, conditions);+ for (String action : S3ObjectActions.objectCreationActions(httpHeaders::getHeaderString, tagging, copy)) {+ s3Service.authorizeObjectCreationSetting(bucket, key, action, authorization, conditions);+ }+ }++ /**+ * The bypass a DeleteObject or PutObjectRetention passes to S3Service. With no access check on, the+ * header as sent. With one on, only for a request that {@code overrides} a governance lock and whose+ * bypass permission was checked: S3's own check approves it in the handler, and IAM's in+ * IamEnforcementFilter, which records its decision in RequestContext. A lock set after the checks is+ * then refused, not bypassed without the permission.+ */+ private boolean effectiveBypass(boolean bypass, boolean overrides) {+ return effectiveBypass(bypass, overrides, s3Service.isAuthEnforced(), requestContext.getIamGovernanceBypass());+ }++ // Package-private for unit testing.+ static boolean effectiveBypass(boolean bypass, boolean overrides, boolean s3Checks, Boolean iamDecision) {+ if (!s3Checks && iamDecision == null) {+ return bypass;+ }+ return bypass && overrides && !Boolean.FALSE.equals(iamDecision);+ }++ // A PutObject, CopyObject or CompleteMultipartUpload carrying If-Match compares against the object+ // it replaces, which IAM checks as s3:GetObject besides the write; the bucket policy is checked+ // for it the same way.+ private void authorizeConditionalWriteRead(String bucket, String key, String ifMatch,+ S3Service.RequestAuthorization authorization) {+ if (ifMatch != null) {+ s3Service.authorizeGetObject(bucket, key, null, authorization);+ }+ }+// A conditional delete against an ETag reveals whether the object still has it, so S3 requires// s3:GetObject as well as s3:DeleteObject. The existence check (If-Match: *) needs only the delete.private void authorizeDeleteETagRead(String bucket, String key, String ifMatch,@@ -2873,20 +2960,21 @@ public class S3Controller {// --- Object Retention ---- private Response handlePutObjectRetention(String bucket, String key, String versionId,- HttpHeaders httpHeaders, byte[] body) {- String xml = new String(body, StandardCharsets.UTF_8);- String mode = XmlParser.extractFirst(xml, "Mode", null);+ // The RetainUntilDate of a <Retention> body, null when it has none, which removes the retention.+ private static Instant retentionRetainUntil(String xml) {String dateStr = XmlParser.extractFirst(xml, "RetainUntilDate", null);- Instant retainUntil;try {- retainUntil = dateStr != null ? Instant.parse(dateStr) : null;+ return dateStr != null ? Instant.parse(dateStr) : null;} catch (DateTimeParseException | IllegalArgumentException e) {throw new AwsException("MalformedXML","The XML you provided was not well-formed.", 400);}- boolean bypass = "true".equalsIgnoreCase(- httpHeaders.getHeaderString("x-amz-bypass-governance-retention"));+ }++ private Response handlePutObjectRetention(String bucket, String key, String versionId, String xml,+ boolean bypass) {+ String mode = XmlParser.extractFirst(xml, "Mode", null);+ Instant retainUntil = retentionRetainUntil(xml);s3Service.putObjectRetention(bucket, key, versionId, mode, retainUntil, bypass);return Response.ok().build();}@@ -3076,7 +3164,7 @@ public class S3Controller {String copySseKmsKeyId = httpHeaders.getHeaderString("x-amz-server-side-encryption-aws-kms-key-id");String cannedAcl = httpHeaders.getHeaderString("x-amz-acl");String taggingDirective = httpHeaders.getHeaderString("x-amz-tagging-directive");- String taggingHeader = httpHeaders.getHeaderString("x-amz-tagging");+ String taggingHeader = resolveInlineTaggingSource(httpHeaders.getHeaderString("x-amz-tagging"), uriInfo);Map<String, String> replacementTagging = "REPLACE".equalsIgnoreCase(taggingDirective)? (taggingHeader != null ? parseInlineTaggingHeader(taggingHeader) : Map.of()): null;@@ -4155,8 +4243,10 @@ public class S3Controller {}/**- * Resolves the inline {@code x-amz-tagging} value for PutObject and CreateMultipartUpload.- * The signed request header wins; otherwise the value comes from the {@code x-amz-tagging}+ * Resolves the inline {@code x-amz-tagging} value for PutObject, CreateMultipartUpload and+ * a CopyObject that replaces its source's tags, and for the bucket-policy check of all three+ * ({@link #authorizeObjectCreation}), so a policy sees the tags the object gets. The signed+ * request header wins; otherwise the value comes from the {@code x-amz-tagging}* query parameter, where SDK presigners hoist the header while signing only {@code host}.* JAX-RS decodes the query value once, which yields the same URL-encoded {@code k=v&k=v}* string the header form carries, so both sources feed@@ -4227,51 +4317,20 @@ public class S3Controller {private String extractObjectKey(UriInfo uriInfo, String bucket, String fallbackKey) {validateRawUri();String rawPath = currentVertxRequest.getCurrent().request().path();- String bucketPrefix = "/" + bucket + "/";- String rawKey;- if (isVirtualHostedRawPath(uriInfo, bucket, rawPath)) {- // Virtual-hosted style: S3VirtualHostFilter rewrote the JAX-RS URI to "/<bucket>" +- // path, but the RAW path is still the key alone. Searching it for "/<bucket>/" finds- // that string only when the KEY happens to contain the bucket's name as a segment,- // and everything up to that segment was then dropped from the key.- rawKey = rawPath.substring(1);- } else {- int prefixIndex = rawPath.indexOf(bucketPrefix);- if (prefixIndex < 0) {- // Should not happen on standard /{bucket}/{key:.+} routes, but can happen when- // requests are rerouted from a single-segment route (e.g. createBucket / handleBucketPost)- String pathKey = uriInfo.getPathParameters().getFirst("key");- String resolvedKey = pathKey != null ? pathKey : fallbackKey;- validateKeyNoTraversal(resolvedKey);- return resolvedKey;- }- rawKey = rawPath.substring(prefixIndex + bucketPrefix.length());- }- String key = URLDecoder.decode(rawKey.replace("+", "%2B"), StandardCharsets.UTF_8);+ // A virtual-hosted raw path is the key alone (see S3ObjectKeys.isVirtualHostedRawPath):+ // searching it for "/<bucket>/" finds that string only when the KEY happens to contain the+ // bucket's name as a segment, and everything up to that segment was then dropped from the key.+ String key = S3ObjectKeys.fromRawPath(rawPath, uriInfo.getRequestUri().getRawPath(), bucket);+ if (key == null) {+ // Should not happen on standard /{bucket}/{key:.+} routes, but can happen when+ // requests are rerouted from a single-segment route (e.g. createBucket / handleBucketPost)+ String pathKey = uriInfo.getPathParameters().getFirst("key");+ key = pathKey != null ? pathKey : fallbackKey;+ }validateKeyNoTraversal(key);return key;}- /**- * True when the raw request path carries no bucket prefix because the bucket came from the- * Host header: the rewritten request URI is then exactly "/" + bucket + rawPath, which is- * what {@link S3VirtualHostFilter} produces and what a path-style request never is. JAX-RS- * collapses consecutive slashes before the filter sees the path, so a leading-slash key- * ({@code //file.txt} on the wire) is rewritten to {@code /<bucket>/file.txt}; the collapsed- * form is compared as well so that key keeps its slash.- */- private static boolean isVirtualHostedRawPath(UriInfo uriInfo, String bucket, String rawPath) {- if (rawPath.isEmpty() || rawPath.charAt(0) != '/') {- return false;- }- String rewritten = uriInfo.getRequestUri().getRawPath();- if (rewritten == null || rewritten.equals(rawPath)) {- return false;- }- return rewritten.equals("/" + bucket + rawPath)- || rewritten.equals("/" + bucket + rawPath.replaceAll("/{2,}", "/"));- }-private void validateKeyNoTraversal(String key) {if (key == null) return;try {@@ -4399,13 +4458,16 @@ public class S3Controller {* and the permissions table makes a copy that puts the object with tags need s3:PutObjectTagging* on the destination, as an object created with tags does. IamEnforcementFilter checks the tags a* request sends; only the source tells whether a copy carries any, so this checks them here, once- * {@link #authorizeCopySourceRead} has allowed the source to be read. The source is read only when- * IAM enforcement is on and the request names a credential, the header's or a presigned URL's.+ * {@link #authorizeCopySourceRead} has allowed the source to be read, against IAM and against the+ * bucket policy under {@code floci.services.s3.enforce-auth}. The source is read only when one of+ * them applies: IAM enforcement is on and the request names a credential, the header's or a+ * presigned URL's, or S3's own check is on.*/private void authorizeCopiedTags(HttpHeaders httpHeaders, UriInfo uriInfo, CopySourceRef source,String destBucket, String destKey, S3Service.RequestAuthorization authorization) {String credential = iamCredential(httpHeaders, uriInfo);- if (!config.services().iam().enforcementEnabled() || credential == null+ boolean checksIdentity = config.services().iam().enforcementEnabled() && credential != null;+ if ((!checksIdentity && !s3Service.isAuthEnforced())|| "REPLACE".equalsIgnoreCase(httpHeaders.getHeaderString("x-amz-tagging-directive"))) {return;}@@ -4423,11 +4485,15 @@ public class S3Controller {return;}String action = "s3:PutObjectTagging";- String resource = S3PublicAccessEvaluator.objectArn(s3Service.bucketPartition(destBucket), destBucket, destKey);- S3Service.SignedPrincipalResourcePolicyEvaluation resourcePolicyEvaluation =- s3Service.signedPrincipalResourcePolicyDecision(destBucket, action, resource, authorization);- iamEnforcementFilter.authorizeAdditionalResource(credential, action, resource,- resourcePolicyEvaluation.decision(), resourcePolicyEvaluation.resourceOwnerAccountId());+ if (checksIdentity) {+ String resource = S3PublicAccessEvaluator.objectArn(+ s3Service.bucketPartition(destBucket), destBucket, destKey);+ S3Service.SignedPrincipalResourcePolicyEvaluation resourcePolicyEvaluation =+ s3Service.signedPrincipalResourcePolicyDecision(destBucket, action, resource, authorization);+ iamEnforcementFilter.authorizeAdditionalResource(credential, action, resource,+ resourcePolicyEvaluation.decision(), resourcePolicyEvaluation.resourceOwnerAccountId());+ }+ s3Service.authorizeObjectCreationSetting(destBucket, destKey, action, authorization, null);}/**diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3ObjectActions.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3ObjectActions.javaindex 25827d9833..0b2eae236e 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3ObjectActions.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3ObjectActions.java@@ -1,6 +1,7 @@package io.github.hectorvent.floci.services.s3;import io.github.hectorvent.floci.core.common.IamConditionContextResolver;+import io.github.hectorvent.floci.core.common.XmlParser;import org.jboss.logging.Logger;import java.net.URLDecoder;@@ -57,6 +58,16 @@ public final class S3ObjectActions {return versionId == null ? action : VERSION_ACTIONS.getOrDefault(action, action);}+ /**+ * Whether an object creation is a CopyObject: a PUT naming a source in {@code x-amz-copy-source},+ * as S3Controller runs it. A CreateMultipartUpload ignores that header and applies its own tags, so+ * the header alone does not make a request a copy.+ */+ public static boolean isCopy(String method, Function<String, String> header) {+ String copySource = header.apply("x-amz-copy-source");+ return "PUT".equalsIgnoreCase(method) && copySource != null && !copySource.isEmpty();+ }+/** 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);@@ -73,13 +84,15 @@ public final class S3ObjectActions {* @param header reads one request header, null when it is absent* @param tagging the request's {@code x-amz-tagging}, from its header or, for a presigned URL, its* query string+ * @param copy whether the request is a CopyObject (see {@link #isCopy})*/- public static List<String> objectCreationActions(Function<String, String> header, String tagging) {+ public static List<String> objectCreationActions(Function<String, String> header, String tagging,+ boolean copy) {List<String> actions = new ArrayList<>();if (header.apply("x-amz-acl") != null || GRANT_HEADERS.stream().anyMatch(name -> header.apply(name) != null)) {actions.add("s3:PutObjectAcl");}- if (tagging != null && !tagging.isBlank() && appliesRequestTags(header)) {+ if (tagging != null && !tagging.isBlank() && appliesRequestTags(header, copy)) {actions.add("s3:PutObjectTagging");}if (header.apply("x-amz-object-lock-mode") != null@@ -101,12 +114,13 @@ public final class S3ObjectActions {** @param header reads one request header, null when it is absent* @param tagging the request's {@code x-amz-tagging}, as {@link #objectCreationActions} takes it+ * @param copy whether the request is a CopyObject (see {@link #isCopy})* @param now the moment {@code s3:object-lock-remaining-retention-days} counts from*/public static Map<String, List<String>> objectCreationConditions(Function<String, String> header, String tagging,- Instant now) {+ boolean copy, Instant now) {Map<String, List<String>> conditions = new LinkedHashMap<>();- Map<String, List<String>> tags = appliesRequestTags(header) ? requestObjectTagConditions(tagging) : null;+ Map<String, List<String>> tags = appliesRequestTags(header, copy) ? requestObjectTagConditions(tagging) : null;if (tags != null) {conditions.putAll(tags);}@@ -124,9 +138,8 @@ public final class S3ObjectActions {* CreateMultipartUpload's always, a CopyObject's only under {@code x-amz-tagging-directive:* REPLACE}. Under the default {@code COPY} a copy keeps the source's tags and ignores the header.*/- private static boolean appliesRequestTags(Function<String, String> header) {- return header.apply("x-amz-copy-source") == null- || "REPLACE".equalsIgnoreCase(header.apply("x-amz-tagging-directive"));+ private static boolean appliesRequestTags(Function<String, String> header, boolean copy) {+ return !copy || "REPLACE".equalsIgnoreCase(header.apply("x-amz-tagging-directive"));}/**@@ -158,6 +171,36 @@ public final class S3ObjectActions {return conditions.isEmpty() ? null : conditions;}+ /**+ * {@code s3:RequestObjectTag/<key>} and {@code s3:RequestObjectTagKeys} from the {@code <Tagging>}+ * XML body of a PutObjectTagging. Null when it names no tag.+ */+ public static Map<String, List<String>> taggingBodyConditions(String body) {+ if (body == null || body.isEmpty()) {+ return null;+ }+ Map<String, String> tags = XmlParser.extractPairs(body, "Tag", "Key", "Value");+ if (tags.isEmpty()) {+ return null;+ }+ Map<String, List<String>> conditions = new LinkedHashMap<>();+ tags.forEach((name, value) -> conditions.put(+ IamConditionContextResolver.REQUEST_OBJECT_TAG_PREFIX + name, List.of(value)));+ conditions.put(IamConditionContextResolver.REQUEST_OBJECT_TAG_KEYS, new ArrayList<>(tags.keySet()));+ return conditions;+ }++ /** The Object Lock keys a PutObjectRetention's {@code <Retention>} body sets (see {@link #objectLockConditions}). */+ public static Map<String, List<String>> retentionBodyConditions(String body, Instant now) {+ return objectLockConditions(XmlParser.extractFirst(body, "Mode", null),+ XmlParser.extractFirst(body, "RetainUntilDate", null), null, now);+ }++ /** The Object Lock key a PutObjectLegalHold's {@code <LegalHold>} body sets (see {@link #objectLockConditions}). */+ public static Map<String, List<String>> legalHoldBodyConditions(String body, Instant now) {+ return objectLockConditions(null, null, XmlParser.extractFirst(body, "Status", null), now);+ }+/*** {@code s3:object-lock-mode}, {@code s3:object-lock-retain-until-date},* {@code s3:object-lock-remaining-retention-days} and {@code s3:object-lock-legal-hold} from thediff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3ObjectKeys.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3ObjectKeys.javanew file mode 100644index 0000000000..6a190fbcb3--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3ObjectKeys.java@@ -0,0 +1,62 @@+package io.github.hectorvent.floci.services.s3;++import java.net.URLDecoder;+import java.nio.charset.StandardCharsets;++/**+ * Reads the object key a request names from its path as it arrived. JAX-RS collapses consecutive+ * slashes in {@code UriInfo}, so a key that starts with a slash ({@code /email.txt}, sent as+ * {@code /bucket//email.txt}) would otherwise be read as another key; {@link S3Controller} and the+ * IAM condition resolver both read it here, so they look at the same object.+ */+public final class S3ObjectKeys {++ private S3ObjectKeys() {+ }++ /**+ * The decoded key in {@code rawPath}, the path as the client sent it, for a request on+ * {@code bucket}. {@code requestUriPath} is the request URI's raw path, which+ * {@link S3VirtualHostFilter} rewrites to {@code /<bucket>/<key>} for a virtual-hosted request,+ * whose raw path is then the key alone. Returns null when the raw path does not carry the+ * bucket, or does not decode.+ */+ public static String fromRawPath(String rawPath, String requestUriPath, String bucket) {+ String rawKey;+ if (isVirtualHostedRawPath(rawPath, requestUriPath, bucket)) {+ rawKey = rawPath.substring(1);+ } else {+ String bucketPrefix = "/" + bucket + "/";+ int prefixIndex = rawPath.indexOf(bucketPrefix);+ if (prefixIndex < 0) {+ return null;+ }+ rawKey = rawPath.substring(prefixIndex + bucketPrefix.length());+ }+ try {+ return URLDecoder.decode(rawKey.replace("+", "%2B"), StandardCharsets.UTF_8);+ } catch (IllegalArgumentException ignored) {+ // A malformed escape names no key; S3Controller refuses the request as BadRequest.+ return null;+ }+ }++ /**+ * True when the raw request path carries no bucket prefix because the bucket came from the+ * Host header: the rewritten request URI is then exactly "/" + bucket + rawPath, which is+ * what {@link S3VirtualHostFilter} produces and what a path-style request never is. JAX-RS+ * collapses consecutive slashes before the filter sees the path, so a leading-slash key+ * ({@code //file.txt} on the wire) is rewritten to {@code /<bucket>/file.txt}; the collapsed+ * form is compared as well so that key keeps its slash.+ */+ static boolean isVirtualHostedRawPath(String rawPath, String requestUriPath, String bucket) {+ if (rawPath.isEmpty() || rawPath.charAt(0) != '/') {+ return false;+ }+ if (requestUriPath == null || requestUriPath.equals(rawPath)) {+ return false;+ }+ return requestUriPath.equals("/" + bucket + rawPath)+ || requestUriPath.equals("/" + bucket + rawPath.replaceAll("/{2,}", "/"));+ }+}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.javaindex 427d0df13d..f365167eb6 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@@ -93,6 +93,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;import java.util.function.Consumer;import java.util.function.Function;import java.util.function.LongConsumer;+import java.util.function.Predicate;import java.util.function.Supplier;import java.util.regex.Matcher;import java.util.regex.Pattern;@@ -1221,7 +1222,7 @@ public class S3Service implements Resettable, ResourceProvider {: RequestAuthorization.unsigned();if (requestAuthorization.signed()) {String bucketArn = S3PublicAccessEvaluator.bucketArn(bucketPartition(bucketName), bucketName);- authorizeSignedBucketPolicy(bucketName, null, action, bucketArn, requestAuthorization);+ authorizeSignedBucketPolicy(bucketName, null, action, bucketArn, requestAuthorization, null);return;}@@ -1247,9 +1248,14 @@ public class S3Service implements Resettable, ResourceProvider {throw accessDeniedException(bucketName, null);}+ /**+ * Authorizes a read of one object, or of the version {@code versionId} names, which is checked as+ * the action's version counterpart (see {@link S3ObjectActions#forVersion}), as IAM checks it.+ */void authorizeObjectRead(String bucketName, String key, String versionId, String action, RequestAuthorization authorization) {String objectArn = S3PublicAccessEvaluator.objectArn(bucketPartition(bucketName), bucketName, key);- authorizeS3Read(bucketName, key, versionId, action, objectArn, authorization);+ authorizeS3Read(bucketName, key, versionId, S3ObjectActions.forVersion(action, versionId), objectArn,+ authorization);}void authorizePutObject(String bucketName, String key, RequestAuthorization authorization) {@@ -1262,17 +1268,51 @@ public class S3Service implements Resettable, ResourceProvider {}void authorizeObjectWrite(String bucketName, String key, String action, RequestAuthorization authorization) {+ authorizeObjectWrite(bucketName, key, null, action, authorization, null);+ }++ /**+ * Authorizes a write of one object, or of the version {@code versionId} names, which is checked as+ * the action's version counterpart (see {@link S3ObjectActions#forVersion}), as IAM checks it. A+ * signed request's bucket policy is evaluated with {@code requestConditions}, the condition keys+ * the request itself offers, such as the tags and Object Lock settings it applies, besides the+ * ones that say who is calling. An anonymous request's public-policy check has no condition+ * context.+ */+ void authorizeObjectWrite(String bucketName, String key, String versionId, String requestedAction,+ RequestAuthorization authorization, Map<String, List<String>> requestConditions) {+ authorizeObjectWrite(bucketName, key, versionId, requestedAction, authorization, requestConditions, false);+ }++ /**+ * Authorizes a setting an object creation applies besides creating the object, its ACL, tags,+ * retention or legal hold (see {@link S3ObjectActions#objectCreationActions}), as+ * {@link #authorizeObjectWrite} authorizes the creation. A public bucket policy that decides the+ * setting's action decides it for an anonymous request. Where none does, the public bucket ACL's+ * WRITE grant that admits an anonymous creation admits the settings it is created with, as it did+ * before they were checked: an ACL grant names no actions to tell them apart by.+ */+ void authorizeObjectCreationSetting(String bucketName, String key, String action,+ RequestAuthorization authorization,+ Map<String, List<String>> requestConditions) {+ authorizeObjectWrite(bucketName, key, null, action, authorization, requestConditions, true);+ }++ private void authorizeObjectWrite(String bucketName, String key, String versionId, String requestedAction,+ RequestAuthorization authorization, Map<String, List<String>> requestConditions,+ boolean creationSetting) {if (!enforceAuth) {return;}+ String action = S3ObjectActions.forVersion(requestedAction, versionId);authorizeSignedRequest(authorization);RequestAuthorization requestAuthorization = authorization != null? authorization: RequestAuthorization.unsigned();if (requestAuthorization.signed()) {String objectArn = S3PublicAccessEvaluator.objectArn(bucketPartition(bucketName), bucketName, key);- authorizeSignedBucketPolicy(bucketName, key, action, objectArn, requestAuthorization);+ authorizeSignedBucketPolicy(bucketName, key, action, objectArn, requestAuthorization, requestConditions);return;}@@ -1295,7 +1335,7 @@ public class S3Service implements Resettable, ResourceProvider {}return;}- if (!blockPublicAccess.ignorePublicAcls() && isObjectCreationAction(action)+ if (!blockPublicAccess.ignorePublicAcls() && (creationSetting || isObjectCreationAction(action))&& !readableObjectExists(bucketName, key) && publicBucketAclAllowsWrite(bucket)) {return;}@@ -1344,7 +1384,7 @@ public class S3Service implements Resettable, ResourceProvider {: RequestAuthorization.unsigned();if (requestAuthorization.signed()) {- authorizeSignedBucketPolicy(bucketName, key, action, resourceArn, requestAuthorization);+ authorizeSignedBucketPolicy(bucketName, key, action, resourceArn, requestAuthorization, null);return;}@@ -1395,7 +1435,8 @@ public class S3Service implements Resettable, ResourceProvider {String key,String action,String resourceArn,- RequestAuthorization authorization) {+ RequestAuthorization authorization,+ Map<String, List<String>> requestConditions) {if (!isKnownAccessKey(authorization)) {throw new AwsException("InvalidAccessKeyId","The AWS Access Key Id you provided does not exist in our records.", 403);@@ -1437,7 +1478,7 @@ public class S3Service implements Resettable, ResourceProvider {RequestPrincipal.caller(caller),action,resourceArn,- bucketPolicyContext(caller));+ bucketPolicyContext(caller, requestConditions));if (decision == ResourcePolicyDecision.EXPLICIT_DENY) {throw accessDeniedException(bucketName, key);@@ -1478,10 +1519,15 @@ public class S3Service implements Resettable, ResourceProvider {* never a service, and the keys that say who is calling are the ones* {@link CallerArns#putPrincipalKeys} writes, so a bucket policy sees the same principal* whether S3 evaluates it or the enforcement filter does. For a role session- * {@code aws:PrincipalArn} is the role's ARN.+ * {@code aws:PrincipalArn} is the role's ARN. {@code requestConditions}, when present, are the keys+ * the request itself offers, such as an object creation's tags and Object Lock settings.*/- private static Map<String, List<String>> bucketPolicyContext(CallerArns caller) {+ private static Map<String, List<String>> bucketPolicyContext(CallerArns caller,+ Map<String, List<String>> requestConditions) {Map<String, List<String>> context = new HashMap<>();+ if (requestConditions != null) {+ context.putAll(requestConditions);+ }context.put("aws:PrincipalIsAWSService", List.of("false"));if (caller != null) {caller.putPrincipalKeys(context);@@ -1535,7 +1581,7 @@ public class S3Service implements Resettable, ResourceProvider {RequestPrincipal.caller(caller.get()),action,resourceArn,- bucketPolicyContext(caller.get()));+ bucketPolicyContext(caller.get(), null));return new SignedPrincipalResourcePolicyEvaluation(decision, ownedBucket.account());}@@ -1642,6 +1688,51 @@ public class S3Service implements Resettable, ResourceProvider {}}+ /**+ * Whether deleting the version {@code versionId} names, the current version when it is null,+ * would remove an object under active governance-mode retention: the deletes the user guide's+ * permissions table requires {@code s3:BypassGovernanceRetention} for ("Required if you want to+ * delete an object that's protected by governance mode"). It follows where+ * {@link #deleteObjectLocked} checks the lock, so a delete that only adds a delete marker over a+ * versioned object overrides nothing.+ */+ public boolean deleteOverridesGovernanceRetention(String bucketName, String key, String versionId) {+ Optional<AccountAwareStorageBackend.OwnedEntry<Bucket>> bucket = resolveBucketEntry(bucketName);+ if (bucket.isEmpty()) {+ return false;+ }+ if (versionId == null && bucket.get().value().isVersioningEnabled()) {+ boolean currentIsVersioned = objectStore.get(objectKey(bucketName, key))+ .map(current -> current.getVersionId() != null)+ .orElse(true);+ if (currentIsVersioned) {+ return false;+ }+ }+ return isGovernanceRetentionActive(bucketName, key, versionId);+ }++ /**+ * Whether setting {@code retainUntil} on the version {@code versionId} names would shorten or+ * remove its active governance-mode retention, the change {@link #putObjectRetention} refuses+ * without a bypass and the permissions table requires {@code s3:BypassGovernanceRetention} for+ * ("Required if you want to bypass the governance mode of an Object Lock retention+ * configuration"). A null {@code retainUntil} removes the retention.+ */+ public boolean retentionChangeOverridesGovernance(String bucketName, String key, String versionId,+ Instant retainUntil) {+ // A missing bucket, object or version has no retention to override, and the change itself+ // reports it missing.+ return resolveBucketEntry(bucketName)+ .flatMap(bucket -> findStoredObject(bucket, bucketName, key, versionId))+ .filter(object -> !object.isDeleteMarker())+ .filter(object -> "GOVERNANCE".equals(object.getObjectLockMode()))+ .map(S3Object::getRetainUntilDate)+ .filter(lockedUntil -> Instant.now().isBefore(lockedUntil))+ .filter(lockedUntil -> retainUntil == null || retainUntil.isBefore(lockedUntil))+ .isPresent();+ }+private void checkLockProtection(S3Object obj, boolean bypassGovernance) {if ("ON".equals(obj.getLegalHoldStatus())) {throw new AwsException("AccessDenied", "Object has an active legal hold", 403);@@ -1904,8 +1995,7 @@ public class S3Service implements Resettable, ResourceProvider {.orElseThrow(() -> new AwsException("NoSuchBucket","The specified bucket does not exist.", 404));- S3Object object = resolveObjectForAccount(ownedBucket.account(), versionStoreKey(bucketName, key, versionId))- .filter(existing -> isNamedVersion(existing, versionId))+ S3Object object = findStoredObject(ownedBucket, bucketName, key, versionId).orElseThrow(() -> versionId != null? new AwsException("NoSuchVersion", "The specified version does not exist.", 404): new AwsException("NoSuchKey", "The specified key does not exist.", 404));@@ -1915,6 +2005,13 @@ public class S3Service implements Resettable, ResourceProvider {return new AccountAwareStorageBackend.OwnedEntry<>(ownedBucket.account(), object);}+ /** The stored version {@code versionId} names, the current one when it is null, delete markers included. */+ private Optional<S3Object> findStoredObject(AccountAwareStorageBackend.OwnedEntry<Bucket> ownedBucket,+ String bucketName, String key, String versionId) {+ return resolveObjectForAccount(ownedBucket.account(), versionStoreKey(bucketName, key, versionId))+ .filter(existing -> isNamedVersion(existing, versionId));+ }+/*** Where the version {@code versionId} names is stored: the current version (null) and the null* version ({@code "null"}) at the plain object key, where floci keeps the null version until a@@ -2824,13 +2921,19 @@ public class S3Service implements Resettable, ResourceProvider {public DeleteObjectsResult deleteObjects(String bucketName, List<XmlParser.KeyVersion> entries,boolean bypassGovernance) {+ return deleteObjects(bucketName, entries, entry -> bypassGovernance);+ }++ /** Deletes each entry, bypassing governance retention only for the entries {@code bypassGovernance} names. */+ public DeleteObjectsResult deleteObjects(String bucketName, List<XmlParser.KeyVersion> entries,+ Predicate<XmlParser.KeyVersion> bypassGovernance) {ensureBucketExists(bucketName);List<DeleteResult> deleted = new ArrayList<>();List<DeleteError> errors = new ArrayList<>();for (XmlParser.KeyVersion entry : entries) {try {- S3Object result = deleteObject(bucketName, entry.key(), entry.versionId(), bypassGovernance,- entry.eTag());+ S3Object result = deleteObject(bucketName, entry.key(), entry.versionId(),+ bypassGovernance.test(entry), entry.eTag());if (result != null && result.isDeleteMarker()) {deleted.add(new DeleteResult(entry.key(), entry.versionId(), true, result.getVersionId()));} else {diff --git a/src/test/java/io/github/hectorvent/floci/core/common/IamConditionContextResolverTest.java b/src/test/java/io/github/hectorvent/floci/core/common/IamConditionContextResolverTest.javaindex 8fe2df3d07..4672bce536 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/IamConditionContextResolverTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/IamConditionContextResolverTest.java@@ -90,7 +90,8 @@ class IamConditionContextResolverTest {config = mock(EmulatorConfig.class);when(config.defaultRegion()).thenReturn("us-east-1");resolver = new IamConditionContextResolver(- dynamoDbFacadeInstance, ec2ServiceInstance, s3ServiceInstance, logsServiceInstance, requestContext, config);+ dynamoDbFacadeInstance, ec2ServiceInstance, s3ServiceInstance, logsServiceInstance, requestContext, config,+ null);}@Test@@ -359,7 +360,7 @@ class IamConditionContextResolverTest {@SuppressWarnings("unchecked")private IamConditionContextResolver resolverAt(Instant now) {return new IamConditionContextResolver(mock(Instance.class), mock(Instance.class), mock(Instance.class),- mock(Instance.class), requestContext, config, Clock.fixed(now, ZoneOffset.UTC));+ mock(Instance.class), requestContext, config, null, Clock.fixed(now, ZoneOffset.UTC));}@Testdiff --git a/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterTest.java b/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterTest.javaindex ad8249a31f..5fc2a977e5 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterTest.java@@ -2031,6 +2031,46 @@ class IamEnforcementFilterTest {"x-amz-object-attributes", "ETag", "s3:GetObject", "s3:GetObjectAttributes"));}+ @Test+ void theGovernanceBypassDecisionIsRecordedForS3ToHonour() {+ // S3Controller lets a delete bypass a lock only as far as this check approved it, so a lock set+ // after the check is refused rather than bypassed without the permission.+ assertEquals(Boolean.FALSE, governanceBypassDecision(false));+ assertEquals(Boolean.TRUE, governanceBypassDecision(true));+ }++ /** Runs an allowed versioned delete sending the bypass header and returns the decision recorded. */+ private Boolean governanceBypassDecision(boolean overrides) {+ ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);+ UriInfo uriInfo = mock(UriInfo.class);+ MultivaluedHashMap<String, String> query = new MultivaluedHashMap<>();+ query.putSingle("versionId", "v1");+ when(uriInfo.getPath()).thenReturn("/bucket/object");+ when(uriInfo.getQueryParameters()).thenReturn(query);+ when(containerRequest.getUriInfo()).thenReturn(uriInfo);+ when(containerRequest.getMethod()).thenReturn("DELETE");+ String auth = "AWS4-HMAC-SHA256 Credential=AKIABYPASS/20260706/us-east-1/s3/aws4_request, "+ + "SignedHeaders=host, Signature=abc";+ requestContext.setAccountId("222233334444");+ requestContext.setRegion("us-east-1");+ requestContext.setIamGovernanceBypass(null);+ when(accountResolver.extractAccessKeyId(auth)).thenReturn("AKIABYPASS");+ when(containerRequest.getHeaderString("Authorization")).thenReturn(auth);+ when(containerRequest.getHeaderString("x-amz-bypass-governance-retention")).thenReturn("true");+ when(actionRegistry.resolve("s3", containerRequest)).thenReturn("s3:DeleteObjectVersion");+ when(iamService.resolveCallerContext(eq("AKIABYPASS"), any())).thenReturn(CallerContext.of(List.of()));+ when(arnBuilder.buildResources("s3", containerRequest, "us-east-1", "222233334444"))+ .thenReturn(List.of("arn:aws:s3:::bucket/object"));+ when(evaluator.evaluateResolvedResourcePolicy(any(), any(), any(), any(), any(), any()))+ .thenReturn(IamPolicyEvaluator.Decision.ALLOW);+ when(conditionContextResolver.s3OverridesGovernanceRetention(any(), any())).thenReturn(overrides);++ newFilter(mock(CurrentVertxRequest.class), mock(CloudTrailService.class)).filter(containerRequest);++ verify(containerRequest, never()).abortWith(any());+ return requestContext.getIamGovernanceBypass();+ }+/*** Runs an S3 request whose own action is allowed and whose extra action is denied, asserts the* denial names the extra action, and returns the event name CloudTrail recorded for it.@@ -2058,6 +2098,8 @@ class IamEnforcementFilterTest {.thenReturn(IamPolicyEvaluator.Decision.ALLOW);when(evaluator.evaluateResolvedResourcePolicy(any(), any(), any(), eq(deniedAction), any(), any())).thenReturn(IamPolicyEvaluator.Decision.DENY);+ // A bypass header counts only on a request that overrides a governance lock.+ when(conditionContextResolver.s3OverridesGovernanceRetention(any(), any())).thenReturn(true);CloudTrailService cloudTrail = mock(CloudTrailService.class);newFilter(mock(CurrentVertxRequest.class), cloudTrail).filter(containerRequest);diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbFacadeTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbFacadeTest.javaindex 8a5ab76e00..3437c39795 100644--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbFacadeTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbFacadeTest.java@@ -152,7 +152,8 @@ class DynamoDbFacadeTest {RequestContext requestContext = new RequestContext();requestContext.setRegion(OTHER_REGION);IamConditionContextResolver resolver = new IamConditionContextResolver(dynamoDb,- mock(Instance.class), mock(Instance.class), mock(Instance.class), requestContext, mock(EmulatorConfig.class));+ mock(Instance.class), mock(Instance.class), mock(Instance.class), requestContext, mock(EmulatorConfig.class),+ null);ContainerRequestContext request = mock(ContainerRequestContext.class);JsonNode body = mapper.readTree("""{"TableName": "recorded-table", "Key": {"id": {"S": "USER_alice"}}}diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/S3ObjectOperationsIamEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/S3ObjectOperationsIamEnforcementIntegrationTest.javaindex 5886d0a98d..6c2850c8be 100644--- a/src/test/java/io/github/hectorvent/floci/services/iam/S3ObjectOperationsIamEnforcementIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/iam/S3ObjectOperationsIamEnforcementIntegrationTest.java@@ -12,13 +12,21 @@ import io.restassured.specification.RequestSpecification;import org.junit.jupiter.api.Test;import org.junit.jupiter.api.extension.RegisterExtension;+import java.io.IOException;+import java.io.UncheckedIOException;import java.net.URI;+import java.net.http.HttpClient;+import java.net.http.HttpRequest;+import java.net.http.HttpResponse;import java.util.List;+import java.util.Map;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;+import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertTrue;/*** Each S3 operation is checked as the action AWS documents for it: the multipart upload operations@@ -188,7 +196,9 @@ class S3ObjectOperationsIamEnforcementIntegrationTest {.body(retention("2031-01-01T00:00:00Z")).when().put(f.path("a/governed")).then().statusCode(200);+ // Extending the lock overrides nothing, so the header asks for nothing more; shortening it does.f.policy(f.allow("s3:PutObjectRetention"));+ f.putRetention("a/governed", "2032-01-01T00:00:00Z", true).statusCode(200);denied(f.putRetention("a/governed", "2030-01-01T00:00:00Z", true), "s3:BypassGovernanceRetention");f.policy(f.allow("s3:PutObjectRetention", "s3:BypassGovernanceRetention"));@@ -196,18 +206,23 @@ class S3ObjectOperationsIamEnforcementIntegrationTest {}@Test- void deletingWithTheBypassNeedsBypassGovernanceRetention() {- // The DeleteObject API reference: "To use this header, you must have the- // s3:BypassGovernanceRetention permission", whether or not the request names a version.+ void deletingWithTheBypassNeedsBypassGovernanceRetentionOnlyToRemoveALockedVersion() {+ // The permissions table: s3:BypassGovernanceRetention is "Required if you want to delete an+ // object that's protected by governance mode", so the header on a delete that removes no+ // locked version asks for nothing more.Fixture f = new Fixture("delete-bypass", true);String versionId = f.seedVersion("a/governed");+ String free = f.seedVersion("a/free");f.admin().queryParam("retention", "").queryParam("versionId", versionId).body(retention("2031-01-01T00:00:00Z")).when().put(f.path("a/governed")).then().statusCode(200);f.policy(f.allow("s3:DeleteObject", "s3:DeleteObjectVersion"));- denied(f.user().header("x-amz-bypass-governance-retention", "true")- .when().delete(f.path("a/governed")).then(), "s3:BypassGovernanceRetention");+ // An unlocked version, and a delete that only adds a delete marker over the locked one.+ f.user().header("x-amz-bypass-governance-retention", "true").queryParam("versionId", free)+ .when().delete(f.path("a/free")).then().statusCode(204);+ f.user().header("x-amz-bypass-governance-retention", "true")+ .when().delete(f.path("a/governed")).then().statusCode(204);denied(f.user().header("x-amz-bypass-governance-retention", "true").queryParam("versionId", versionId).when().delete(f.path("a/governed")).then(), "s3:BypassGovernanceRetention");@@ -216,6 +231,44 @@ class S3ObjectOperationsIamEnforcementIntegrationTest {.when().delete(f.path("a/governed")).then().statusCode(204);}+ @Test+ void theBypassIsDecidedOnTheKeyARequestNamesEvenWithALeadingSlash() {+ // A key that starts with "/" travels as "//" after the bucket, which JAX-RS collapses to the+ // key without it. The lock is looked up on the key the request names, so a governance lock on+ // "/governed" still needs s3:BypassGovernanceRetention to shorten or delete.+ Fixture f = new Fixture("slash-bypass", true);+ HttpResponse<String> put = f.raw("PUT", "/governed", null, ADMIN, Map.of(), "version");+ assertEquals(200, put.statusCode(), put.body());+ String versionId = put.headers().firstValue("x-amz-version-id").orElseThrow();+ String version = "versionId=" + versionId;+ assertEquals(200, f.raw("PUT", "/governed", "retention&" + version, ADMIN, Map.of(),+ retention("2031-01-01T00:00:00Z")).statusCode());+ Map<String, String> bypass = Map.of("x-amz-bypass-governance-retention", "true");++ f.policy(f.allow("s3:PutObjectRetention", "s3:DeleteObjectVersion"));+ rawDenied(f.raw("PUT", "/governed", "retention&" + version, f.accessKeyId, bypass,+ retention("2030-01-01T00:00:00Z")), "s3:BypassGovernanceRetention");+ rawDenied(f.raw("DELETE", "/governed", version, f.accessKeyId, bypass, null),+ "s3:BypassGovernanceRetention");++ f.policy(f.allow("s3:DeleteObjectVersion", "s3:BypassGovernanceRetention"));+ assertEquals(204, f.raw("DELETE", "/governed", version, f.accessKeyId, bypass, null).statusCode());+ }++ @Test+ void anExistingObjectTagConditionReadsTheTagsOfTheKeyARequestNames() {+ // "/doc" and "doc" are two objects; a read of "/doc" is authorized against its own tags.+ Fixture f = new Fixture("slash-tags", true);+ assertEquals(200, f.raw("PUT", "/doc", null, ADMIN, Map.of("x-amz-tagging", "env=a"), "slash").statusCode());+ f.admin().header("x-amz-tagging", "env=b").body("plain").when().put(f.path("doc")).then().statusCode(200);++ f.policy(f.allowWhen("{\"StringEquals\":{\"s3:ExistingObjectTag/env\":\"a\"}}", "s3:GetObject"));+ HttpResponse<String> slash = f.raw("GET", "/doc", null, f.accessKeyId, Map.of(), null);+ assertEquals(200, slash.statusCode(), slash.body());+ assertEquals("slash", slash.body());+ denied(f.user().when().get(f.path("doc")).then(), "s3:GetObject");+ }+@Testvoid deletingAnAnnotationWithTheBypassHeaderNeedsNoBypassPermission() {// The user guide: "The BypassGovernanceRetention permission does not apply to annotation@@ -268,6 +321,10 @@ class S3ObjectOperationsIamEnforcementIntegrationTest {.when().put(f.path("a/grant")).then(), "s3:PutObjectAcl");denied(f.user().header("x-amz-tagging", "env=a").queryParam("uploads", "").when().post(f.path("a/upload")).then(), "s3:PutObjectTagging");+ // A multipart upload ignores x-amz-copy-source and applies its own tags, so the header does not+ // make it a copy whose tags come from a source.+ denied(f.user().header("x-amz-tagging", "env=a").header("x-amz-copy-source", f.bucket + "/a/source")+ .queryParam("uploads", "").when().post(f.path("a/upload")).then(), "s3:PutObjectTagging");denied(f.user().header("x-amz-acl", "private").queryParam("uploads", "").when().post(f.path("a/upload")).then(), "s3:PutObjectAcl");denied(f.copy().header("x-amz-tagging-directive", "REPLACE").header("x-amz-tagging", "env=a")@@ -575,6 +632,11 @@ class S3ObjectOperationsIamEnforcementIntegrationTest {.body(containsString(action));}+ private static void rawDenied(HttpResponse<String> response, String action) {+ assertEquals(403, response.statusCode(), response.body());+ assertTrue(response.body().contains("AccessDenied") && response.body().contains(action), response.body());+ }+private static String retention(String until) {return """<Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/">@@ -618,6 +680,28 @@ class S3ObjectOperationsIamEnforcementIntegrationTest {return "/" + bucket + "/" + key;}+ /**+ * Sends a request on {@code key} with the path as written, signed as {@code accessKeyId}:+ * RestAssured, like JAX-RS, collapses the double slash a key starting with "/" travels as.+ */+ HttpResponse<String> raw(String method, String key, String query, String accessKeyId,+ Map<String, String> headers, String body) {+ URI uri = URI.create("http://localhost:" + baseUri.getPort() + path(key) + (query == null ? "" : "?" + query));+ HttpRequest.Builder request = HttpRequest.newBuilder(uri)+ .header("Authorization", auth(accessKeyId, "s3"))+ .method(method, body == null+ ? HttpRequest.BodyPublishers.noBody() : HttpRequest.BodyPublishers.ofString(body));+ headers.forEach(request::header);+ try (HttpClient client = HttpClient.newHttpClient()) {+ return client.send(request.build(), HttpResponse.BodyHandlers.ofString());+ } catch (IOException e) {+ throw new UncheckedIOException(e);+ } catch (InterruptedException e) {+ Thread.currentThread().interrupt();+ throw new IllegalStateException(e);+ }+ }+String allowOnBucket(String... actions) {return "{\"Effect\":\"Allow\",\"Action\":[\"" + String.join("\",\"", actions)+ "\"],\"Resource\":\"arn:aws:s3:::" + bucket + "\"}";@@ -782,6 +866,11 @@ class S3ObjectOperationsIamEnforcementIntegrationTest {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++) {+ if (keys.get(i).startsWith("/")) {+ assertEquals(204, raw("DELETE", keys.get(i), "versionId=" + ids.get(i), ADMIN,+ Map.of("x-amz-bypass-governance-retention", "true"), null).statusCode());+ continue;+ }admin().header("x-amz-bypass-governance-retention", "true").queryParam("versionId", ids.get(i)).when().delete(path(keys.get(i))).then().statusCode(204);diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.javaindex 8fe1a4be6c..2703f460b2 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java@@ -1311,21 +1311,25 @@ class S3AuthEnforcementIntegrationTest {@Test@Order(42)- void deleteObjectDoesNotBypassGovernanceRetentionWithoutDistinctPermission() {- given().filter(LOCAL_SIGNER).when().put("/" + BYPASS_BUCKET).then().statusCode(200);-- given()- .filter(LOCAL_SIGNER)+ void deleteObjectNeedsTheBypassPermissionOnlyToRemoveAGovernanceLockedVersion() {+ // The permissions table requires s3:BypassGovernanceRetention "if you want to delete an object+ // that's protected by governance mode", so the header alone asks for nothing more.+ given().filter(LOCAL_SIGNER).header("x-amz-bucket-object-lock-enabled", "true")+ .when().put("/" + BYPASS_BUCKET).then().statusCode(200);+ String free = given().filter(LOCAL_SIGNER).body("unlocked")+ .when().put("/" + BYPASS_BUCKET + "/free.txt")+ .then().statusCode(200).extract().header("x-amz-version-id");+ String locked = given().filter(LOCAL_SIGNER)+ .header("x-amz-object-lock-mode", "GOVERNANCE")+ .header("x-amz-object-lock-retain-until-date", "2031-01-01T00:00:00Z").body("locked candidate")- .when()- .put("/" + BYPASS_BUCKET + "/" + BYPASS_KEY)- .then()- .statusCode(200);+ .when().put("/" + BYPASS_BUCKET + "/" + BYPASS_KEY)+ .then().statusCode(200).extract().header("x-amz-version-id");given().filter(LOCAL_SIGNER).contentType("application/json")- .body(publicObjectActionPolicy(BYPASS_BUCKET, "s3:DeleteObject"))+ .body(publicObjectActionPolicy(BYPASS_BUCKET, "s3:DeleteObjectVersion")).when().put("/" + BYPASS_BUCKET + "?policy").then()@@ -1333,6 +1337,14 @@ class S3AuthEnforcementIntegrationTest {given().header("x-amz-bypass-governance-retention", "true")+ .queryParam("versionId", free)+ .when()+ .delete("/" + BYPASS_BUCKET + "/free.txt")+ .then()+ .statusCode(204);+ given()+ .header("x-amz-bypass-governance-retention", "true")+ .queryParam("versionId", locked).when().delete("/" + BYPASS_BUCKET + "/" + BYPASS_KEY).then()@@ -1350,6 +1362,7 @@ class S3AuthEnforcementIntegrationTest {given().header("x-amz-bypass-governance-retention", "true")+ .queryParam("versionId", locked).when().delete("/" + BYPASS_BUCKET + "/" + BYPASS_KEY).then()@@ -1363,7 +1376,7 @@ class S3AuthEnforcementIntegrationTest {"Statement": {"Effect": "Allow","Principal": "*",- "Action": ["s3:DeleteObject", "s3:BypassGovernanceRetention"],+ "Action": ["s3:DeleteObjectVersion", "s3:BypassGovernanceRetention"],"Resource": "arn:aws:s3:::%s/*"}}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ControllerGovernanceBypassTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ControllerGovernanceBypassTest.javanew file mode 100644index 0000000000..2a6c253308--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ControllerGovernanceBypassTest.java@@ -0,0 +1,37 @@+package io.github.hectorvent.floci.services.s3;++import org.junit.jupiter.api.Test;++import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertTrue;++/**+ * The bypass a delete or retention change hands S3Service. With an access check on, it is the+ * header only for a request that overrides a governance lock and whose bypass permission the checks+ * approved, so a lock set after the checks is refused rather than bypassed without the permission.+ */+class S3ControllerGovernanceBypassTest {++ @Test+ void withNoCheckOnTheHeaderIsHandedOnAsSent() {+ assertTrue(S3Controller.effectiveBypass(true, false, false, null));+ assertFalse(S3Controller.effectiveBypass(false, false, false, null));+ }++ @Test+ void s3sOwnCheckHandsItOnOnlyForARequestThatOverridesALock() {+ assertTrue(S3Controller.effectiveBypass(true, true, true, null));+ assertFalse(S3Controller.effectiveBypass(true, false, true, null));+ assertFalse(S3Controller.effectiveBypass(false, true, true, null));+ }++ @Test+ void anIamCheckThatFoundNoLockToOverrideHandsItOnForNone() {+ // IAM saw no lock, so it checked no bypass permission: a lock S3Controller sees afterwards was+ // set after IAM's check, and the operation must refuse it.+ assertFalse(S3Controller.effectiveBypass(true, true, false, false));+ assertFalse(S3Controller.effectiveBypass(true, true, true, false));+ assertTrue(S3Controller.effectiveBypass(true, true, false, true));+ assertFalse(S3Controller.effectiveBypass(true, false, false, true));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectOperationsBucketPolicyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectOperationsBucketPolicyIntegrationTest.javanew file mode 100644index 0000000000..8c9fb9502e--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectOperationsBucketPolicyIntegrationTest.java@@ -0,0 +1,356 @@+package io.github.hectorvent.floci.services.s3;++import io.github.hectorvent.floci.testing.S3EnforceAuthProfile;+import io.github.hectorvent.floci.testutil.S3RequestSigner;+import io.quarkus.test.junit.QuarkusTest;+import io.quarkus.test.junit.TestProfile;+import io.restassured.RestAssured;+import io.restassured.response.Response;+import io.restassured.specification.RequestSpecification;+import org.junit.jupiter.api.Test;++import java.net.URI;+import java.util.UUID;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.containsString;++/**+ * S3's own bucket-policy check, with IAM enforcement off, checks an object request as the actions+ * the enforcement filter does: the version action a versionId selects, s3:GetObject beside+ * s3:GetObjectAttributes and beside a write carrying If-Match, and the actions an object created+ * with tags, an ACL or Object Lock settings, or copied from a tagged object, needs besides+ * s3:PutObject. The policy is offered the tag and Object Lock keys the request sets, and+ * s3:BypassGovernanceRetention is asked for only by a request that overrides a governance lock.+ * Each case is an explicit Deny in the bucket policy refusing the bucket owner.+ */+@QuarkusTest+@TestProfile(S3EnforceAuthProfile.class)+class S3ObjectOperationsBucketPolicyIntegrationTest {++ private static final S3RequestSigner OWNER = S3RequestSigner.signedAs("test", "test");++ @Test+ void aRequestNamingAVersionIsCheckedAsTheVersionAction() {+ String bucket = bucket(true);+ String versionId = owner().header("x-amz-tagging", "env=a").body("v1").when().put(bucket + "/doc")+ .then().statusCode(200).extract().header("x-amz-version-id");+ owner().queryParam("annotation", "").queryParam("annotationName", "labels").body("x")+ .when().put(bucket + "/doc").then().statusCode(200);+ policy(bucket, deny("s3:GetObjectVersionTagging", "s3:PutObjectVersionTagging",+ "s3:DeleteObjectVersionTagging", "s3:GetObjectVersionAcl", "s3:GetObjectVersionAnnotation",+ "s3:PutObjectVersionAnnotation", "s3:DeleteObjectVersionAnnotation"));++ owner().queryParam("tagging", "").queryParam("versionId", versionId).when().get(bucket + "/doc")+ .then().statusCode(403);+ owner().queryParam("tagging", "").queryParam("versionId", versionId)+ .body("<Tagging><TagSet/></Tagging>").when().put(bucket + "/doc").then().statusCode(403);+ owner().queryParam("tagging", "").queryParam("versionId", versionId).when().delete(bucket + "/doc")+ .then().statusCode(403);+ owner().queryParam("acl", "").queryParam("versionId", versionId).when().get(bucket + "/doc")+ .then().statusCode(403);+ owner().queryParam("annotation", "").queryParam("annotationName", "labels")+ .queryParam("versionId", versionId).when().get(bucket + "/doc").then().statusCode(403);+ owner().queryParam("annotation", "").queryParam("annotationName", "labels")+ .queryParam("versionId", versionId).body("y").when().put(bucket + "/doc").then().statusCode(403);+ owner().queryParam("annotation", "").queryParam("annotationName", "labels")+ .queryParam("versionId", versionId).when().delete(bucket + "/doc").then().statusCode(403);++ // Without a versionId the base actions are checked, which the policy leaves alone.+ owner().queryParam("tagging", "").when().get(bucket + "/doc").then().statusCode(200);+ owner().queryParam("acl", "").when().get(bucket + "/doc").then().statusCode(200);+ owner().queryParam("annotation", "").queryParam("annotationName", "labels")+ .when().get(bucket + "/doc").then().statusCode(200);+ }++ @Test+ void getObjectAttributesNeedsTheReadAsWell() {+ String bucket = bucket(false);+ owner().body("data").when().put(bucket + "/doc").then().statusCode(200);++ policy(bucket, deny("s3:GetObject"));+ attributes(bucket + "/doc").then().statusCode(403);++ policy(bucket, deny("s3:GetObjectAttributes"));+ attributes(bucket + "/doc").then().statusCode(403);+ owner().when().get(bucket + "/doc").then().statusCode(200);+ }++ @Test+ void aWriteCarryingIfMatchNeedsTheReadAsWell() {+ String bucket = bucket(false);+ String source = bucket(false);+ String eTag = owner().body("v1").when().put(bucket + "/doc").then().statusCode(200).extract().header("ETag");+ owner().body("copied").when().put(source + "/doc").then().statusCode(200);+ policy(bucket, deny("s3:GetObject"));++ owner().header("If-Match", eTag).body("v2").when().put(bucket + "/doc").then().statusCode(403);+ owner().header("If-Match", eTag).header("x-amz-copy-source", source + "/doc")+ .when().put(bucket + "/doc").then().statusCode(403);+ String uploadId = owner().queryParam("uploads", "").when().post(bucket + "/doc")+ .then().statusCode(200).extract().xmlPath().getString("InitiateMultipartUploadResult.UploadId");+ String partETag = owner().queryParam("uploadId", uploadId).queryParam("partNumber", 1).body("v3")+ .when().put(bucket + "/doc").then().statusCode(200).extract().header("ETag");+ String parts = "<CompleteMultipartUpload><Part><PartNumber>1</PartNumber><ETag>" + partETag+ + "</ETag></Part></CompleteMultipartUpload>";+ owner().header("If-Match", eTag).queryParam("uploadId", uploadId).body(parts)+ .when().post(bucket + "/doc").then().statusCode(403);++ // The same writes without the condition need only the write.+ owner().queryParam("uploadId", uploadId).body(parts).when().post(bucket + "/doc").then().statusCode(200);+ owner().body("v4").when().put(bucket + "/doc").then().statusCode(200);+ }++ @Test+ void anObjectCreatedWithTagsOrAnAclNeedsThoseActions() {+ String bucket = bucket(false);+ policy(bucket, deny("s3:PutObjectTagging", "s3:PutObjectAcl"));++ owner().header("x-amz-tagging", "env=a").body("tagged").when().put(bucket + "/tagged")+ .then().statusCode(403);+ owner().header("x-amz-acl", "bucket-owner-full-control").body("acl").when().put(bucket + "/acl")+ .then().statusCode(403);+ owner().header("x-amz-tagging", "env=a").queryParam("uploads", "").when().post(bucket + "/upload")+ .then().statusCode(403);+ owner().body("plain").when().put(bucket + "/plain").then().statusCode(200);+ // A multipart upload ignores x-amz-copy-source and applies its own tags, so the header does not+ // make it a copy whose tags come from a source.+ owner().header("x-amz-tagging", "env=a").header("x-amz-copy-source", bucket + "/plain")+ .queryParam("uploads", "").when().post(bucket + "/upload").then().statusCode(403);+ }++ @Test+ void aCopyOfATaggedObjectNeedsPutObjectTagging() {+ String bucket = bucket(false);+ owner().header("x-amz-tagging", "env=a").body("tagged").when().put(bucket + "/tagged")+ .then().statusCode(200);+ owner().body("plain").when().put(bucket + "/plain").then().statusCode(200);+ policy(bucket, deny("s3:PutObjectTagging"));++ owner().header("x-amz-copy-source", bucket + "/tagged").when().put(bucket + "/copy")+ .then().statusCode(403);+ owner().header("x-amz-copy-source", bucket + "/plain").header("x-amz-tagging-directive", "REPLACE")+ .header("x-amz-tagging", "env=b").when().put(bucket + "/copy").then().statusCode(403);+ owner().header("x-amz-copy-source", bucket + "/plain").when().put(bucket + "/copy")+ .then().statusCode(200);+ }++ @Test+ void aReplacingCopyAppliesTheTagsItIsCheckedForFromAPresignedQuery() throws Exception {+ // A presigner may move x-amz-tagging into the query, where PutObject already reads it; a copy+ // replacing its source's tags reads it there too, so the object gets the tags the policy saw.+ String bucket = bucket(false);+ owner().body("plain").when().put(bucket + "/plain").then().statusCode(200);+ policy(bucket, denyWhen("{\"StringEquals\":{\"s3:RequestObjectTag/env\":\"secret\"}}", "s3:PutObject"));++ presignedCopy(bucket + "/plain", bucket + "/secret?x-amz-tagging=env%3Dsecret").then().statusCode(403);+ presignedCopy(bucket + "/plain", bucket + "/copy?x-amz-tagging=env%3Db").then().statusCode(200);+ owner().queryParam("tagging", "").when().get(bucket + "/copy").then().statusCode(200)+ .body(containsString("<Value>b</Value>"));+ }++ @Test+ void anAnonymousCreationAPublicBucketAclAdmitsMayApplyItsSettings() {+ // The bucket ACL's WRITE grant admits the creation with its ACL and tags, as before they were+ // checked; a public bucket policy that names a setting's action still decides it.+ String bucket = bucket(false);+ owner().body("<OwnershipControls><Rule><ObjectOwnership>ObjectWriter</ObjectOwnership></Rule></OwnershipControls>")+ .when().put(bucket + "?ownershipControls").then().statusCode(200);+ owner().header("x-amz-acl", "public-read-write").when().put(bucket + "?acl").then().statusCode(200);++ given().header("x-amz-acl", "bucket-owner-full-control").body("acl").when().put(bucket + "/acl")+ .then().statusCode(200);+ given().header("x-amz-tagging", "env=a").body("tagged").when().put(bucket + "/tagged")+ .then().statusCode(200);++ policy(bucket, deny("s3:PutObjectTagging"));+ given().header("x-amz-tagging", "env=a").body("tagged").when().put(bucket + "/denied")+ .then().statusCode(403);+ given().body("plain").when().put(bucket + "/plain").then().statusCode(200);+ }++ @Test+ void anObjectCreationOffersItsTagAndObjectLockKeys() {+ String bucket = lockedBucket();+ policy(bucket, denyWhen("{\"StringEquals\":{\"s3:RequestObjectTag/env\":\"secret\"}}",+ "s3:PutObjectTagging")+ + "," + denyWhen("{\"StringEquals\":{\"s3:object-lock-mode\":\"COMPLIANCE\"}}",+ "s3:PutObjectRetention")+ + "," + denyWhen("{\"StringEquals\":{\"s3:object-lock-legal-hold\":\"ON\"}}",+ "s3:PutObjectLegalHold")+ + "," + denyWhen("{\"ForAnyValue:StringEquals\":{\"s3:RequestObjectTagKeys\":\"blocked\"}}",+ "s3:PutObject"));++ owner().header("x-amz-tagging", "env=secret").body("x").when().put(bucket + "/secret")+ .then().statusCode(403);+ owner().header("x-amz-tagging", "env=secret").queryParam("uploads", "").when().post(bucket + "/upload")+ .then().statusCode(403);+ owner().header("x-amz-tagging", "env=open").body("x").when().put(bucket + "/open")+ .then().statusCode(200);+ owner().header("x-amz-tagging", "blocked=yes").body("x").when().put(bucket + "/blocked")+ .then().statusCode(403);+ owner().header("x-amz-object-lock-mode", "COMPLIANCE")+ .header("x-amz-object-lock-retain-until-date", "2031-01-01T00:00:00Z")+ .body("x").when().put(bucket + "/compliance").then().statusCode(403);+ owner().header("x-amz-object-lock-mode", "GOVERNANCE")+ .header("x-amz-object-lock-retain-until-date", "2031-01-01T00:00:00Z")+ .body("x").when().put(bucket + "/governance").then().statusCode(200);+ owner().header("x-amz-object-lock-legal-hold", "ON").body("x").when().put(bucket + "/held")+ .then().statusCode(403);+ owner().header("x-amz-object-lock-legal-hold", "OFF").body("x").when().put(bucket + "/released")+ .then().statusCode(200);+ }++ @Test+ void theTaggingRetentionAndLegalHoldBodiesOfferTheirKeys() {+ String bucket = lockedBucket();+ owner().body("x").when().put(bucket + "/doc").then().statusCode(200);+ policy(bucket, denyWhen("{\"StringEquals\":{\"s3:RequestObjectTag/env\":\"secret\"}}",+ "s3:PutObjectTagging")+ + "," + denyWhen("{\"StringEquals\":{\"s3:object-lock-mode\":\"COMPLIANCE\"}}",+ "s3:PutObjectRetention")+ + "," + denyWhen("{\"NumericGreaterThan\":{\"s3:object-lock-remaining-retention-days\":\"3650\"}}",+ "s3:PutObjectRetention")+ + "," + denyWhen("{\"StringEquals\":{\"s3:object-lock-legal-hold\":\"ON\"}}",+ "s3:PutObjectLegalHold"));++ owner().queryParam("tagging", "").body(tagging("secret")).when().put(bucket + "/doc").then().statusCode(403);+ owner().queryParam("tagging", "").body(tagging("open")).when().put(bucket + "/doc").then().statusCode(200);+ owner().queryParam("retention", "").body(retention("COMPLIANCE", "2031-01-01T00:00:00Z"))+ .when().put(bucket + "/doc").then().statusCode(403);+ owner().queryParam("retention", "").body(retention("GOVERNANCE", "2045-01-01T00:00:00Z"))+ .when().put(bucket + "/doc").then().statusCode(403);+ owner().queryParam("retention", "").body(retention("GOVERNANCE", "2031-01-01T00:00:00Z"))+ .when().put(bucket + "/doc").then().statusCode(200);+ owner().queryParam("legal-hold", "").body(legalHold("ON")).when().put(bucket + "/doc").then().statusCode(403);+ owner().queryParam("legal-hold", "").body(legalHold("OFF")).when().put(bucket + "/doc").then().statusCode(200);+ }++ @Test+ void onlyARequestOverridingAGovernanceLockNeedsTheBypassPermission() {+ // The permissions table requires s3:BypassGovernanceRetention "if you want to delete an object+ // that's protected by governance mode" and to "bypass the governance mode of an Object Lock+ // retention configuration".+ String bucket = lockedBucket();+ String locked = owner().header("x-amz-object-lock-mode", "GOVERNANCE")+ .header("x-amz-object-lock-retain-until-date", "2031-01-01T00:00:00Z")+ .body("locked").when().put(bucket + "/locked").then().statusCode(200)+ .extract().header("x-amz-version-id");+ String free = owner().body("free").when().put(bucket + "/free").then().statusCode(200)+ .extract().header("x-amz-version-id");+ owner().header("x-amz-object-lock-mode", "GOVERNANCE")+ .header("x-amz-object-lock-retain-until-date", "2031-01-01T00:00:00Z")+ .body("batch").when().put(bucket + "/batch").then().statusCode(200);+ policy(bucket, deny("s3:BypassGovernanceRetention"));++ // Extending the lock, deleting an unlocked version, and adding a delete marker override nothing.+ bypass().queryParam("retention", "").queryParam("versionId", locked)+ .body(retention("GOVERNANCE", "2032-01-01T00:00:00Z"))+ .when().put(bucket + "/locked").then().statusCode(200);+ bypass().queryParam("versionId", free).when().delete(bucket + "/free").then().statusCode(204);+ bypass().when().delete(bucket + "/locked").then().statusCode(204);+ bypass().contentType("application/xml").body("<Delete><Object><Key>batch</Key></Object></Delete>")+ .when().post(bucket + "?delete").then().statusCode(200)+ .body(containsString("<Deleted><Key>batch</Key>"));++ // Shortening the lock and deleting the locked version override it.+ bypass().queryParam("retention", "").queryParam("versionId", locked)+ .body(retention("GOVERNANCE", "2030-01-01T00:00:00Z"))+ .when().put(bucket + "/locked").then().statusCode(403);+ bypass().queryParam("versionId", locked).when().delete(bucket + "/locked").then().statusCode(403);+ }++ @Test+ void aRetentionChangeOnAMissingObjectOrBucketIsReportedMissing() {+ String bucket = lockedBucket();+ bypass().queryParam("retention", "").body(retention("GOVERNANCE", "2031-01-01T00:00:00Z"))+ .when().put(bucket + "/missing").then().statusCode(404).body(containsString("NoSuchKey"));+ bypass().queryParam("retention", "").body(retention("GOVERNANCE", "2031-01-01T00:00:00Z"))+ .when().put("/s3ops-policy-missing-" + UUID.randomUUID() + "/doc").then().statusCode(404)+ .body(containsString("NoSuchBucket"));+ }++ @Test+ void deletingAnAnnotationWithTheBypassHeaderNeedsNoBypassPermission() {+ // The user guide: "The BypassGovernanceRetention permission does not apply to annotation+ // operations."+ String bucket = lockedBucket();+ owner().body("x").when().put(bucket + "/doc").then().statusCode(200);+ owner().queryParam("annotation", "").queryParam("annotationName", "labels").body("x")+ .when().put(bucket + "/doc").then().statusCode(200);+ owner().queryParam("retention", "").body(retention("GOVERNANCE", "2031-01-01T00:00:00Z"))+ .when().put(bucket + "/doc").then().statusCode(200);+ policy(bucket, deny("s3:BypassGovernanceRetention"));++ bypass().queryParam("annotation", "").queryParam("annotationName", "labels")+ .when().delete(bucket + "/doc").then().statusCode(204);+ }++ private static RequestSpecification owner() {+ return given().filter(OWNER);+ }++ private static RequestSpecification bypass() {+ return owner().header("x-amz-bypass-governance-retention", "true");+ }++ /** A CopyObject replacing the source's tags, presigned by the owner over the destination's own query. */+ private static Response presignedCopy(String source, String destinationAndQuery) throws Exception {+ String query = OWNER.presignedQuery("PUT",+ URI.create("http://localhost:" + RestAssured.port + destinationAndQuery), 300);+ String destination = destinationAndQuery.substring(0, destinationAndQuery.indexOf('?'));+ return given().urlEncodingEnabled(false).header("x-amz-copy-source", source)+ .header("x-amz-tagging-directive", "REPLACE").when().put(destination + "?" + query);+ }++ private static Response attributes(String path) {+ return owner().queryParam("attributes", "").header("x-amz-object-attributes", "ETag").when().get(path);+ }++ /** A new bucket, with versioning on when asked, as its path. */+ private static String bucket(boolean versioned) {+ String bucket = "/s3ops-policy-" + UUID.randomUUID();+ owner().when().put(bucket).then().statusCode(200);+ if (versioned) {+ owner().body("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")+ .when().put(bucket + "?versioning").then().statusCode(200);+ }+ return bucket;+ }++ /** A new bucket with Object Lock enabled, as its path. */+ private static String lockedBucket() {+ String bucket = "/s3ops-policy-" + UUID.randomUUID();+ owner().header("x-amz-bucket-object-lock-enabled", "true").when().put(bucket).then().statusCode(200);+ return bucket;+ }++ private static String tagging(String env) {+ return "<Tagging><TagSet><Tag><Key>env</Key><Value>" + env + "</Value></Tag></TagSet></Tagging>";+ }++ private static String retention(String mode, String until) {+ return "<Retention><Mode>" + mode + "</Mode><RetainUntilDate>" + until + "</RetainUntilDate></Retention>";+ }++ private static String legalHold(String status) {+ return "<LegalHold><Status>" + status + "</Status></LegalHold>";+ }++ private static String denyWhen(String condition, String... actions) {+ String deny = deny(actions);+ return deny.substring(0, deny.length() - 1) + ",\"Condition\":" + condition + "}";+ }++ private static String deny(String... actions) {+ return "{\"Effect\":\"Deny\",\"Principal\":\"*\",\"Action\":[\"" + String.join("\",\"", actions)+ + "\"],\"Resource\":\"arn:aws:s3:::%s/*\"}";+ }++ private static void policy(String bucket, String statement) {+ owner().contentType("application/json")+ .body("{\"Version\":\"2012-10-17\",\"Statement\":[" + statement.replace("%s", bucket.substring(1))+ + "]}")+ .when().put(bucket + "?policy").then().statusCode(200);+ }+}