diff --git a/docs/services/iam.md b/docs/services/iam.md index d0c797bb64..7899f58857 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -1163,14 +1163,12 @@ An S3 object operation is checked as the action the S3 documentation gives it: - Reading or heading a version needs `s3:GetObjectVersion`; `s3:GetObject` is not needed. - Deleting a version needs `s3:DeleteObjectVersion`, so a deny on it stops permanent deletes while a plain delete still adds a delete marker. - - A version's ACL and annotations use `s3:GetObjectVersionAcl`, `s3:PutObjectVersionAcl` and the - `s3:*ObjectVersionAnnotation(s)` actions. - - Tagging a version is still checked as the `s3:*ObjectTagging` actions: the tagging operations - act on the current version whatever `versionId` names. + - 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 and annotation requests as the base actions, and - offers no `s3:versionid`. + `s3:DeleteObjectVersion`, but a version's ACL, tagging and annotation requests as the base + actions, and offers no `s3:versionid`. - Deleting a version with an `If-Match` ETag also needs `s3:GetObject`, as a plain conditional delete does: the S3 user guide says the condition is evaluated against the current version. - **Bucket configurations:** @@ -1455,7 +1453,7 @@ floci populates: - `s3:ExistingObjectTag/`: the tags already on the target object version, for `s3:GetObject`, `s3:GetObjectTagging`, `s3:GetObjectAcl`, `s3:PutObjectAcl`, `s3:DeleteObjectTagging`, `s3:PutObjectTagging` and the annotation actions, and for their - version actions (`s3:GetObjectVersion`, `s3:GetObjectVersionAcl` and so on). A `versionId` in the request + version actions (`s3:GetObjectVersion`, `s3:GetObjectVersionTagging` and so on). A `versionId` in the request selects the version whose tags are read. **`s3:DeleteObject` and `s3:PutObject` do not receive this key**, as measured on AWS. An allow conditioned on it denies the delete of a correctly tagged object, and a create cannot be gated on tags an object does not have yet. diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java b/src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java index b513843046..1717ab1782 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 @@ -244,15 +244,16 @@ public class IamConditionContextResolver { // The version actions IamActionRegistry checks for a request naming a versionId carry // the key as their base actions do, per the Service Reference, as do the annotation // actions, and the versionId picks whose tags are read. - case "s3:GetObject", "s3:GetObjectVersion", "s3:GetObjectTagging", "s3:GetObjectAcl", - "s3:GetObjectVersionAcl", "s3:PutObjectAcl", "s3:PutObjectVersionAcl", "s3:DeleteObjectTagging", - "s3:GetObjectAnnotation", "s3:GetObjectVersionAnnotation", "s3:ListObjectAnnotations", - "s3:ListObjectVersionAnnotations", "s3:PutObjectAnnotation", "s3:PutObjectVersionAnnotation", - "s3:DeleteObjectAnnotation", "s3:DeleteObjectVersionAnnotation" -> - s3ExistingObjectTagConditionContext(ctx); + case "s3:GetObject", "s3:GetObjectVersion", "s3:GetObjectTagging", + "s3:GetObjectVersionTagging", "s3:GetObjectAcl", "s3:GetObjectVersionAcl", + "s3:PutObjectAcl", "s3:PutObjectVersionAcl", "s3:DeleteObjectTagging", + "s3:DeleteObjectVersionTagging", "s3:GetObjectAnnotation", "s3:GetObjectVersionAnnotation", + "s3:ListObjectAnnotations", "s3:ListObjectVersionAnnotations", "s3:PutObjectAnnotation", + "s3:PutObjectVersionAnnotation", "s3:DeleteObjectAnnotation", + "s3:DeleteObjectVersionAnnotation" -> s3ExistingObjectTagConditionContext(ctx); case "s3:PutObject" -> s3RequestObjectTagConditionContext(ctx); - case "s3:PutObjectTagging" -> merge(s3ExistingObjectTagConditionContext(ctx), - s3PutObjectTaggingBodyConditionContext(ctx)); + case "s3:PutObjectTagging", "s3:PutObjectVersionTagging" -> merge( + s3ExistingObjectTagConditionContext(ctx), s3PutObjectTaggingBodyConditionContext(ctx)); default -> null; }; } diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java index 6bf73a6d55..ce20d80c87 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 @@ -1157,9 +1157,9 @@ public class IamEnforcementFilter implements ContainerRequestFilter { case "s3:ListAllMyBuckets" -> "ListBuckets"; case "s3:GetObjectAcl", "s3:GetObjectVersionAcl" -> "GetObjectAcl"; case "s3:PutObjectAcl", "s3:PutObjectVersionAcl" -> "PutObjectAcl"; - case "s3:GetObjectTagging" -> "GetObjectTagging"; - case "s3:PutObjectTagging" -> "PutObjectTagging"; - case "s3:DeleteObjectTagging" -> "DeleteObjectTagging"; + case "s3:GetObjectTagging", "s3:GetObjectVersionTagging" -> "GetObjectTagging"; + case "s3:PutObjectTagging", "s3:PutObjectVersionTagging" -> "PutObjectTagging"; + case "s3:DeleteObjectTagging", "s3:DeleteObjectVersionTagging" -> "DeleteObjectTagging"; default -> null; }; } diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java index e2d63d9a28..137727f11d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamActionRegistry.java @@ -487,11 +487,8 @@ public class IamActionRegistry { * The operation an object sub-resource selects, checked in the order S3Controller dispatches * them: a request naming several runs the first, so it is checked as that one. A * {@code versionId} selects the version action AWS checks in place of an action that has one - * (see {@link S3ObjectActions#forVersion}). Tagging keeps its own actions: the tagging - * operations act on the current version whatever {@code versionId} names, so a version tagging - * action would authorize one version and let the request change another. Null where the - * controller falls through to the plain object operation on the current version, which the - * rules check. + * (see {@link S3ObjectActions#forVersion}). Null where the controller falls through to the plain + * object operation on the current version, which the rules check. */ private static String objectSubResourceAction(String method, MultivaluedMap params) { String versionId = params.getFirst("versionId"); @@ -501,7 +498,7 @@ public class IamActionRegistry { yield "s3:ListMultipartUploadParts"; } if (params.containsKey("tagging")) { - yield "s3:GetObjectTagging"; + yield S3ObjectActions.forVersion("s3:GetObjectTagging", versionId); } if (params.containsKey("annotation")) { yield S3ObjectActions.forVersion(annotationReadAction(params), versionId); @@ -525,7 +522,7 @@ public class IamActionRegistry { } case "PUT" -> { if (params.containsKey("tagging")) { - yield "s3:PutObjectTagging"; + yield S3ObjectActions.forVersion("s3:PutObjectTagging", versionId); } if (params.containsKey("retention")) { yield "s3:PutObjectRetention"; @@ -544,7 +541,7 @@ public class IamActionRegistry { } case "DELETE" -> { if (params.containsKey("tagging")) { - yield "s3:DeleteObjectTagging"; + yield S3ObjectActions.forVersion("s3:DeleteObjectTagging", versionId); } if (params.containsKey("annotation")) { yield S3ObjectActions.forVersion("s3:DeleteObjectAnnotation", versionId); 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.java index 5bc88fc975..a4a24e049d 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 @@ -811,7 +811,8 @@ public class S3Controller { if (hasQueryParam(uriInfo, "tagging")) { s3Service.authorizeObjectWrite(bucket, key, "s3:PutObjectTagging", authorization); - return handlePutObjectTagging(bucket, key, readWholeBody(body)); + return handlePutObjectTagging(bucket, key, uriInfo.getQueryParameters().getFirst("versionId"), + readWholeBody(body)); } if (hasQueryParam(uriInfo, "retention")) { s3Service.authorizeObjectWrite(bucket, key, "s3:PutObjectRetention", authorization); @@ -981,7 +982,7 @@ public class S3Controller { if (hasQueryParam(uriInfo, "tagging")) { s3Service.authorizeObjectRead(bucket, key, versionId, "s3:GetObjectTagging", authorization); - return handleGetObjectTagging(bucket, key); + return handleGetObjectTagging(bucket, key, versionId); } if (hasQueryParam(uriInfo, "annotation")) { String annotationNameParam = uriInfo.getQueryParameters().getFirst("annotationName"); @@ -1484,8 +1485,8 @@ public class S3Controller { if (hasQueryParam(uriInfo, "tagging")) { s3Service.authorizeObjectWrite(bucket, key, "s3:DeleteObjectTagging", authorization); - s3Service.deleteObjectTagging(bucket, key); - return Response.noContent().build(); + return withVersionId(Response.noContent(), s3Service.deleteObjectTagging(bucket, key, versionId)) + .build(); } if (hasQueryParam(uriInfo, "annotation")) { boolean bypass = "true".equalsIgnoreCase( @@ -2615,16 +2616,21 @@ public class S3Controller { // --- Object Tagging --- - private Response handlePutObjectTagging(String bucket, String key, byte[] body) { + private Response handlePutObjectTagging(String bucket, String key, String versionId, byte[] body) { String xml = new String(body, StandardCharsets.UTF_8); Map tags = XmlParser.extractPairs(xml, "Tag", "Key", "Value"); - s3Service.putObjectTagging(bucket, key, tags); - return Response.ok().build(); + return withVersionId(Response.ok(), s3Service.putObjectTagging(bucket, key, versionId, tags)).build(); } - private Response handleGetObjectTagging(String bucket, String key) { - Map tags = s3Service.getObjectTagging(bucket, key); - return Response.ok(buildTaggingXml(tags)).type(MediaType.APPLICATION_XML).build(); + private Response handleGetObjectTagging(String bucket, String key, String versionId) { + S3Service.ObjectTaggingResult tagging = s3Service.getObjectTaggingResult(bucket, key, versionId); + return withVersionId(Response.ok(buildTaggingXml(tagging.tags())), tagging.versionId()) + .type(MediaType.APPLICATION_XML).build(); + } + + /** {@code resp} with the id of the version a request acted on, when that version has one. */ + private static Response.ResponseBuilder withVersionId(Response.ResponseBuilder resp, String versionId) { + return versionId != null ? resp.header("x-amz-version-id", versionId) : resp; } private String buildTaggingXml(Map tags) { @@ -3573,10 +3579,12 @@ public class S3Controller { } // S3 InvalidArgument responses carry ArgumentName and ArgumentValue so the SDK can // surface which input was rejected, and EntityTooSmall and EntityTooLarge the proposed and - // allowed sizes. Multipart size errors also identify the rejected part. + // allowed sizes. Multipart size errors also identify the rejected part, and MethodNotAllowed + // the method and the kind of resource that refused it. // They travel through AwsException.extendedData. if (e.getExtendedData() != null) { - for (String field : List.of("ProposedSize", "MinSizeAllowed", "MaxSizeAllowed", "PartNumber", "ETag")) { + for (String field : List.of("Method", "ResourceType", "ProposedSize", "MinSizeAllowed", + "MaxSizeAllowed", "PartNumber", "ETag")) { Object value = e.getExtendedData().get(field); if (value != null) { xmlBuilder.elem(field, value.toString()); @@ -3612,7 +3620,13 @@ public class S3Controller { .elem("RequestId", UUID.randomUUID().toString()) .end("Error") .build(); - return Response.status(e.getHttpStatus()).entity(xml).type(MediaType.APPLICATION_XML).build(); + Response.ResponseBuilder response = Response.status(e.getHttpStatus()) + .entity(xml).type(MediaType.APPLICATION_XML); + // A request refused because it reached a delete marker says so in x-amz-delete-marker. + if (e.getExtendedData() != null && "DeleteMarker".equals(e.getExtendedData().get("ResourceType"))) { + response.header("x-amz-delete-marker", "true"); + } + return response.build(); } private Response checkPreconditions(S3Object obj, String ifMatch, String ifNoneMatch, 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.java index 19047d822e..9a78b220b3 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 @@ -12,15 +12,16 @@ public final class S3ObjectActions { /** * The object actions that have a version counterpart, which AWS checks in their place when the * request names a {@code versionId}. Retention, legal hold, restore and the multipart actions - * have none, and are checked as they are. The tagging actions are left out although AWS gives - * them version counterparts: the tagging operations act on the current version whatever - * {@code versionId} names (see IamActionRegistry). + * have none, and are checked as they are. */ private static final Map VERSION_ACTIONS = Map.ofEntries( Map.entry("s3:GetObject", "s3:GetObjectVersion"), Map.entry("s3:DeleteObject", "s3:DeleteObjectVersion"), Map.entry("s3:GetObjectAcl", "s3:GetObjectVersionAcl"), Map.entry("s3:PutObjectAcl", "s3:PutObjectVersionAcl"), + Map.entry("s3:GetObjectTagging", "s3:GetObjectVersionTagging"), + Map.entry("s3:PutObjectTagging", "s3:PutObjectVersionTagging"), + Map.entry("s3:DeleteObjectTagging", "s3:DeleteObjectVersionTagging"), Map.entry("s3:GetObjectAnnotation", "s3:GetObjectVersionAnnotation"), Map.entry("s3:ListObjectAnnotations", "s3:ListObjectVersionAnnotations"), Map.entry("s3:PutObjectAnnotation", "s3:PutObjectVersionAnnotation"), diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java index ce2eba7553..0340e05b26 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 @@ -2799,24 +2799,108 @@ public class S3Service implements Resettable, ResourceProvider { // --- Object Tagging --- + /** The tags of one object version, and that version's id: null for an object stored without one. */ + public record ObjectTaggingResult(String versionId, Map tags) { + } + public void putObjectTagging(String bucketName, String key, Map tags) { - setCurrentObjectTags(bucketName, key, tags != null ? tags : new HashMap<>()); - LOG.debugv("Put tags on object: {0}/{1}", bucketName, key); + putObjectTagging(bucketName, key, null, tags); + } + + /** + * Replaces the tags of the version {@code versionId} names, or of the current version when it is + * null, and returns that version's id. + */ + public String putObjectTagging(String bucketName, String key, String versionId, Map tags) { + return updateObjectTagging(bucketName, key, versionId, "PUT", tags != null ? tags : new HashMap<>()); } public Map getObjectTagging(String bucketName, String key) { return getObjectTagging(bucketName, key, null); } - /** Tags of one version, or of the current version when {@code versionId} is null or "null". */ + /** + * Tags of one version, or of the current version when {@code versionId} is null or "null". The + * IAM condition context reads {@code s3:ExistingObjectTag} here, so these must be the tags of the + * object a request reaches, and GetObject, HeadObject and the annotation operations still serve + * the current version for "null". GetObjectTagging reads {@link #getObjectTaggingResult}. + */ public Map getObjectTagging(String bucketName, String key, String versionId) { S3Object obj = getStoredObject(bucketName, key, "null".equals(versionId) ? null : versionId); return obj.getTags() != null ? obj.getTags() : Map.of(); } + /** + * The tags of the version {@code versionId} names, or of the current version when it is null, + * with that version's id. "null" names the null version only, and a delete marker is refused. + */ + public ObjectTaggingResult getObjectTaggingResult(String bucketName, String key, String versionId) { + S3Object obj = getTaggingTargetEntry(bucketName, key, versionId, "GET").value(); + return new ObjectTaggingResult(obj.getVersionId(), obj.getTags() != null ? obj.getTags() : Map.of()); + } + public void deleteObjectTagging(String bucketName, String key) { - setCurrentObjectTags(bucketName, key, new HashMap<>()); - LOG.debugv("Deleted tags from object: {0}/{1}", bucketName, key); + deleteObjectTagging(bucketName, key, null); + } + + /** + * Removes the tags of the version {@code versionId} names, or of the current version when it is + * null, and returns that version's id. + */ + public String deleteObjectTagging(String bucketName, String key, String versionId) { + return updateObjectTagging(bucketName, key, versionId, "DELETE", new HashMap<>()); + } + + /** + * Gives the version a tagging request names the tags {@code tags}. It runs under the bucket + * monitor that writes, deletes and the lifecycle sweep hold, so the version is resolved and + * rewritten in one step: a version deleted between the lookup and the write is not written back, + * and the sweep never sees the tags it matched a rule against change before it acts. + */ + private String updateObjectTagging(String bucketName, String key, String versionId, String method, + Map tags) { + Bucket bucket = resolveBucket(bucketName) + .orElseThrow(() -> new AwsException("NoSuchBucket", + "The specified bucket does not exist.", 404)); + synchronized (bucket) { + AccountAwareStorageBackend.OwnedEntry ownedObject = + getTaggingTargetEntry(bucketName, key, versionId, method); + S3Object obj = ownedObject.value(); + obj.setTags(tags); + putObjectMetadataForAccount(ownedObject.account(), bucketName, key, obj); + LOG.debugv("{0} tags of object: {1}/{2} version {3}", method, bucketName, key, obj.getVersionId()); + return obj.getVersionId(); + } + } + + /** + * The version a tagging request acts on: the one {@code versionId} names, or the current version + * when it is null. The literal {@code "null"} names the null version, resolved as + * deleteObjectLocked resolves it: the object at the plain key, when that object has no version + * id. A current version with an id is not it, and floci keeps no null version beside one, so the + * request names a version that does not exist. A delete marker, named or current, has no tags: + * S3 refuses the request with MethodNotAllowed, naming the method and the marker. + */ + private AccountAwareStorageBackend.OwnedEntry getTaggingTargetEntry( + String bucketName, String key, String versionId, String method) { + AccountAwareStorageBackend.OwnedEntry ownedBucket = resolveBucketEntry(bucketName) + .orElseThrow(() -> new AwsException("NoSuchBucket", + "The specified bucket does not exist.", 404)); + boolean nullVersion = "null".equals(versionId); + String storeKey = versionId == null || nullVersion + ? objectKey(bucketName, key) + : versionedKey(bucketName, key, versionId); + S3Object object = resolveObjectForAccount(ownedBucket.account(), storeKey) + .filter(existing -> !nullVersion || existing.getVersionId() == null) + .orElseThrow(() -> versionId != null + ? new AwsException("NoSuchVersion", "The specified version does not exist.", 404) + : new AwsException("NoSuchKey", "The specified key does not exist.", 404)); + if (object.isDeleteMarker()) { + throw new AwsException("MethodNotAllowed", + "The specified method is not allowed against this resource.", 405, + Map.of("Method", method, "ResourceType", "DeleteMarker")); + } + return new AccountAwareStorageBackend.OwnedEntry<>(ownedBucket.account(), object); } // --- Object Annotations --- @@ -6575,23 +6659,6 @@ public class S3Service implements Resettable, ResourceProvider { * still refers to this object, checked under the bucket lock that puts and deletes hold, so a * concurrent overwrite or delete marker is never replaced by the object it superseded. */ - /** - * Replaces the current version's tags under the bucket monitor that writes, deletes and the - * lifecycle sweep hold, so the object is resolved and rewritten in one step: the sweep never - * sees the tags it matched a rule against change before it acts. - */ - private void setCurrentObjectTags(String bucketName, String key, Map tags) { - Bucket bucket = resolveBucket(bucketName) - .orElseThrow(() -> new AwsException("NoSuchBucket", - "The specified bucket does not exist.", 404)); - synchronized (bucket) { - AccountAwareStorageBackend.OwnedEntry ownedObject = - getStoredObjectEntry(bucketName, key, null); - ownedObject.value().setTags(tags); - putObjectMetadataForAccount(ownedObject.account(), bucketName, key, ownedObject.value()); - } - } - private void putObjectMetadataForAccount(String accountId, String bucketName, String key, S3Object object) { Bucket bucket = resolveBucket(bucketName) .orElseThrow(() -> new AwsException("NoSuchBucket", 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.java index 1e8f57d78b..75d3dd1cd3 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 @@ -288,6 +288,23 @@ class IamConditionContextResolverTest { assertFalse(resolver.resolve("s3", "s3:GetObject", request).containsKey("s3:versionid")); } + @Test + void versionTaggingActionsCarryTheVersionAndItsTags() { + ContainerRequestContext request = s3Request("/bucket/object", + "Teampayments"); + request.getUriInfo().getQueryParameters().putSingle("versionId", "v1"); + when(s3Service.getObjectTagging("bucket", "object", "v1")).thenReturn(Map.of("env", "a")); + for (String action : List.of("s3:GetObjectVersionTagging", "s3:DeleteObjectVersionTagging")) { + Map> conditions = resolver.resolve("s3", action, request); + assertEquals(List.of("v1"), conditions.get("s3:versionid"), action); + assertEquals(List.of("a"), conditions.get("s3:ExistingObjectTag/env"), action); + } + Map> put = resolver.resolve("s3", "s3:PutObjectVersionTagging", request); + assertEquals(List.of("v1"), put.get("s3:versionid")); + assertEquals(List.of("a"), put.get("s3:ExistingObjectTag/env")); + assertEquals(List.of("payments"), put.get("s3:RequestObjectTag/Team")); + } + @Test void annotationActionsCarryTheObjectsTags() { when(s3Service.getObjectTagging("bucket", "object", null)).thenReturn(Map.of("env", "a")); diff --git a/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterUnitTest.java b/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterUnitTest.java index 714a2b0e4a..ed94e70ad6 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterUnitTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterUnitTest.java @@ -85,6 +85,12 @@ class IamEnforcementFilterUnitTest { IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersionAcl", "GET")); assertEquals("PutObjectAcl", IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectVersionAcl", "PUT")); + assertEquals("GetObjectTagging", + IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersionTagging", "GET")); + assertEquals("PutObjectTagging", + IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectVersionTagging", "PUT")); + assertEquals("DeleteObjectTagging", + IamEnforcementFilter.mapS3ActionToEventName("s3:DeleteObjectVersionTagging", "DELETE")); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java index 2e465f4cb3..13793d4988 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamActionRegistryTest.java @@ -420,10 +420,12 @@ class IamActionRegistryTest { assertEquals("s3:DeleteObjectVersion", s3Action("DELETE", "/bucket/key", "versionId=v1")); assertEquals("s3:GetObjectVersionAcl", s3Action("GET", "/bucket/key", "acl", "versionId=v1")); assertEquals("s3:PutObjectVersionAcl", s3Action("PUT", "/bucket/key", "acl", "versionId=v1")); - // Tagging keeps its own actions: the tagging operations act on the current version. - assertEquals("s3:GetObjectTagging", s3Action("GET", "/bucket/key", "tagging", "versionId=v1")); - assertEquals("s3:PutObjectTagging", s3Action("PUT", "/bucket/key", "tagging", "versionId=v1")); - assertEquals("s3:DeleteObjectTagging", s3Action("DELETE", "/bucket/key", "tagging", "versionId=v1")); + assertEquals("s3:GetObjectVersionTagging", s3Action("GET", "/bucket/key", "tagging", "versionId=v1")); + assertEquals("s3:PutObjectVersionTagging", s3Action("PUT", "/bucket/key", "tagging", "versionId=v1")); + assertEquals("s3:DeleteObjectVersionTagging", + s3Action("DELETE", "/bucket/key", "tagging", "versionId=v1")); + // "null" names the null version, which is a version as any other. + assertEquals("s3:GetObjectVersionTagging", s3Action("GET", "/bucket/key", "tagging", "versionId=null")); assertEquals("s3:GetObjectVersionAnnotation", s3Action("GET", "/bucket/key", "annotation", "annotationName=n", "versionId=v1")); assertEquals("s3:GetObjectVersionAnnotation", 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.java index bb7cb5f110..4850ca5c3a 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 @@ -243,22 +243,48 @@ class S3ObjectOperationsIamEnforcementIntegrationTest { } @Test - void theTagsOfAVersionAreCheckedAsTheTaggingActions() { - // The tagging operations act on the current version whatever versionId names, so a version - // tagging action alone must not let a caller read or change the current version's tags. + void theTagsOfAVersionNeedTheVersionTaggingActions() { Fixture f = new Fixture("version-tags", true); String versionId = f.seedVersion("a/tagged"); - f.policy(f.allow("s3:GetObjectVersionTagging", "s3:PutObjectVersionTagging")); + f.policy(f.allow("s3:GetObjectTagging", "s3:PutObjectTagging", "s3:DeleteObjectTagging")); denied(f.user().queryParam("tagging", "").queryParam("versionId", versionId) - .when().get(f.path("a/tagged")).then(), "s3:GetObjectTagging"); + .when().get(f.path("a/tagged")).then(), "s3:GetObjectVersionTagging"); denied(f.user().queryParam("tagging", "").queryParam("versionId", versionId) .body("").when().put(f.path("a/tagged")).then(), - "s3:PutObjectTagging"); + "s3:PutObjectVersionTagging"); + denied(f.user().queryParam("tagging", "").queryParam("versionId", versionId) + .when().delete(f.path("a/tagged")).then(), "s3:DeleteObjectVersionTagging"); - f.policy(f.allow("s3:GetObjectTagging")); + f.policy(f.allow("s3:GetObjectVersionTagging", "s3:PutObjectVersionTagging", + "s3:DeleteObjectVersionTagging")); + f.user().queryParam("tagging", "").queryParam("versionId", versionId) + .body("").when().put(f.path("a/tagged")).then().statusCode(200); f.user().queryParam("tagging", "").queryParam("versionId", versionId) .when().get(f.path("a/tagged")).then().statusCode(200); + f.user().queryParam("tagging", "").queryParam("versionId", versionId) + .when().delete(f.path("a/tagged")).then().statusCode(204); + } + + @Test + void theTagsAVersionTaggingConditionReadsAreThoseOfTheVersionItChanges() { + Fixture f = new Fixture("version-tag-condition", true); + String older = f.seedVersion("a/doc", "env=a"); + String current = f.seedVersion("a/doc", "env=b"); + f.policy(f.allowWhen("{\"StringEquals\":{\"s3:ExistingObjectTag/env\":\"a\"}}", + "s3:PutObjectVersionTagging")); + String tagging = "enva" + + "seenyes"; + + f.user().queryParam("tagging", "").queryParam("versionId", older).body(tagging) + .when().put(f.path("a/doc")).then().statusCode(200).header("x-amz-version-id", older); + denied(f.user().queryParam("tagging", "").queryParam("versionId", current).body(tagging) + .when().put(f.path("a/doc")).then(), "s3:PutObjectVersionTagging"); + + f.admin().queryParam("tagging", "").queryParam("versionId", older).when().get(f.path("a/doc")) + .then().statusCode(200).body(containsString("seen")); + f.admin().queryParam("tagging", "").when().get(f.path("a/doc")) + .then().statusCode(200).body(not(containsString("seen"))); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/S3ObjectTagConditionEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/S3ObjectTagConditionEnforcementIntegrationTest.java index d4b8ce9d86..02539d2ed3 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/S3ObjectTagConditionEnforcementIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/S3ObjectTagConditionEnforcementIntegrationTest.java @@ -149,6 +149,21 @@ class S3ObjectTagConditionEnforcementIntegrationTest { assertEquals(403, f.put("a/bad", "%zz=a", null), "an undecodable key"); } + @Test + void aReadNamingTheNullVersionIsAuthorizedAgainstTheVersionItServes() { + Fixture f = new Fixture("nullversion", true); + // Written with versioning on, the key has no null version. S3 answers a read naming one with + // NoSuchVersion, but floci serves it the current version, so that version's tags must decide + // or the deny on them would be skipped. + f.seedVersion("a/secret", "env=b"); + f.policy(allow("\"s3:GetObject\",\"s3:GetObjectVersion\"", f.objects(), null) + "," + + "{\"Effect\":\"Deny\",\"Action\":[\"s3:GetObject\",\"s3:GetObjectVersion\"]," + + "\"Resource\":\"" + f.objects() + "\"," + + "\"Condition\":{\"StringEquals\":{\"s3:ExistingObjectTag/env\":\"b\"}}}"); + assertEquals(403, f.get("a/secret"), "the current version carries the denied tag"); + assertEquals(403, f.getVersion("a/secret", "null"), "the same version, reached through versionId=null"); + } + private static String allow(String actions, String resource, String condition) { String action = actions.startsWith("\"") ? "[" + actions + "]" : "\"" + actions + "\""; return "{\"Effect\":\"Allow\",\"Action\":" + action + ",\"Resource\":\"" + resource + "\"" diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningIntegrationTest.java index cad52aaec3..1381ae6fe6 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningIntegrationTest.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.s3; import io.quarkus.test.junit.QuarkusTest; +import org.hamcrest.Matcher; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; @@ -517,4 +518,78 @@ class S3VersioningIntegrationTest { given().when().delete("/" + bucket + "/key?versionId=" + versionId).then().statusCode(204); given().when().delete("/" + bucket).then().statusCode(204); } + + @Test + @Order(22) + void taggingActsOnTheVersionNamed() { + String bucket = "versioned-tagging-test"; + given().when().put("/" + bucket).then().statusCode(200); + // Written before versioning, "old" is the null version, which has no version id to report. + given().body("v0").when().put("/" + bucket + "/old").then().statusCode(200); + given().body(tagging("unversioned")).when().put("/" + bucket + "/old?tagging") + .then().statusCode(200).header("x-amz-version-id", nullValue()); + given().body("Enabled") + .when().put("/" + bucket + "?versioning").then().statusCode(200); + String v1 = given().body("v1").when().put("/" + bucket + "/key") + .then().statusCode(200).extract().header("x-amz-version-id"); + String v2 = given().body("v2").when().put("/" + bucket + "/key") + .then().statusCode(200).extract().header("x-amz-version-id"); + + given().body(tagging("first")).when().put("/" + bucket + "/key?tagging&versionId=" + v1) + .then().statusCode(200).header("x-amz-version-id", v1); + given().when().get("/" + bucket + "/key?tagging&versionId=" + v1) + .then().statusCode(200).header("x-amz-version-id", v1) + .body(containsString("first")); + given().when().get("/" + bucket + "/key?tagging") + .then().statusCode(200).header("x-amz-version-id", v2) + .body(not(containsString(""))); + given().when().delete("/" + bucket + "/key?tagging&versionId=" + v1) + .then().statusCode(204).header("x-amz-version-id", v1); + given().when().get("/" + bucket + "/key?tagging&versionId=" + v1) + .then().statusCode(200).body(not(containsString(""))); + + // versionId=null names the null version only: "key" was written with versioning on. + given().when().get("/" + bucket + "/old?tagging&versionId=null") + .then().statusCode(200).body(containsString("unversioned")); + given().body(tagging("other")).when().put("/" + bucket + "/key?tagging&versionId=null") + .then().statusCode(404).body(containsString("NoSuchVersion")); + given().when().get("/" + bucket + "/key?tagging") + .then().statusCode(200).body(not(containsString("other"))); + } + + @Test + @Order(23) + void taggingADeleteMarkerIsNotAllowed() { + String bucket = "versioned-tagging-marker-test"; + given().when().put("/" + bucket).then().statusCode(200); + given().body("Enabled") + .when().put("/" + bucket + "?versioning").then().statusCode(200); + given().body("data").when().put("/" + bucket + "/key").then().statusCode(200); + String markerId = given().when().delete("/" + bucket + "/key") + .then().statusCode(204).extract().header("x-amz-version-id"); + + // Refused whether the request names the marker or the marker is the current version. + String key = "/" + bucket + "/key?tagging"; + for (String path : List.of(key, key + "&versionId=" + markerId)) { + given().body(tagging("v")).when().put(path) + .then().statusCode(405).header("x-amz-delete-marker", "true") + .body(deleteMarkerRefused("PUT")); + given().when().get(path) + .then().statusCode(405).header("x-amz-delete-marker", "true") + .body(deleteMarkerRefused("GET")); + given().when().delete(path) + .then().statusCode(405).header("x-amz-delete-marker", "true") + .body(deleteMarkerRefused("DELETE")); + } + } + + private static String tagging(String value) { + return "version" + value + ""; + } + + private static Matcher deleteMarkerRefused(String method) { + return allOf(containsString("MethodNotAllowed"), + containsString("" + method + ""), + containsString("DeleteMarker")); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningServiceTest.java index d7ba4ca15b..5c1fa41b9a 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3VersioningServiceTest.java @@ -13,6 +13,7 @@ import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Arrays; import java.util.List; import java.util.Map; @@ -343,6 +344,83 @@ class S3VersioningServiceTest { assertEquals("keep.txt", objects.get(0).getKey()); } + @Test + void taggingAVersionChangesOnlyThatVersion() { + s3Service.putBucketVersioning("versioned-bucket", "Enabled"); + S3Object v1 = s3Service.putObject("versioned-bucket", "key", + "v1".getBytes(StandardCharsets.UTF_8), "text/plain", null); + S3Object v2 = s3Service.putObject("versioned-bucket", "key", + "v2".getBytes(StandardCharsets.UTF_8), "text/plain", null); + assertEquals(v2.getVersionId(), + s3Service.putObjectTagging("versioned-bucket", "key", null, Map.of("version", "current"))); + + assertEquals(v1.getVersionId(), + s3Service.putObjectTagging("versioned-bucket", "key", v1.getVersionId(), Map.of("version", "first"))); + assertEquals(new S3Service.ObjectTaggingResult(v1.getVersionId(), Map.of("version", "first")), + s3Service.getObjectTaggingResult("versioned-bucket", "key", v1.getVersionId())); + assertEquals(new S3Service.ObjectTaggingResult(v2.getVersionId(), Map.of("version", "current")), + s3Service.getObjectTaggingResult("versioned-bucket", "key", null)); + + assertEquals(v1.getVersionId(), s3Service.deleteObjectTagging("versioned-bucket", "key", v1.getVersionId())); + assertEquals(Map.of(), s3Service.getObjectTaggingResult("versioned-bucket", "key", v1.getVersionId()).tags()); + assertEquals(Map.of("version", "current"), s3Service.getObjectTagging("versioned-bucket", "key")); + } + + @Test + void versionIdNullNamesTheNullVersionAndNoOther() { + // An object written before versioning is the null version. + s3Service.putObject("versioned-bucket", "old", "v0".getBytes(StandardCharsets.UTF_8), "text/plain", null); + s3Service.putBucketVersioning("versioned-bucket", "Enabled"); + assertNull(s3Service.putObjectTagging("versioned-bucket", "old", "null", Map.of("version", "null"))); + assertEquals(new S3Service.ObjectTaggingResult(null, Map.of("version", "null")), + s3Service.getObjectTaggingResult("versioned-bucket", "old", "null")); + + // A key written only with versioning on has no null version, and its current version is + // not one: a request naming the null version fails and leaves the current one alone. + s3Service.putObject("versioned-bucket", "new", "v1".getBytes(StandardCharsets.UTF_8), "text/plain", null); + s3Service.putObjectTagging("versioned-bucket", "new", Map.of("version", "current")); + for (Runnable request : List.of( + () -> s3Service.getObjectTaggingResult("versioned-bucket", "new", "null"), + () -> s3Service.putObjectTagging("versioned-bucket", "new", "null", Map.of("version", "other")), + () -> s3Service.deleteObjectTagging("versioned-bucket", "new", "null"))) { + AwsException e = assertThrows(AwsException.class, request::run); + assertEquals("NoSuchVersion", e.getErrorCode()); + } + assertEquals(Map.of("version", "current"), s3Service.getObjectTagging("versioned-bucket", "new")); + // The condition context's lookup still takes "null" as the current version, which is what + // GetObject serves for it. + assertEquals(Map.of("version", "current"), s3Service.getObjectTagging("versioned-bucket", "new", "null")); + + // An object written while versioning is suspended is the null version too, above the + // versions written before it. + s3Service.putBucketVersioning("versioned-bucket", "Suspended"); + s3Service.putObject("versioned-bucket", "new", "v2".getBytes(StandardCharsets.UTF_8), "text/plain", null); + assertNull(s3Service.deleteObjectTagging("versioned-bucket", "new", "null")); + s3Service.putObjectTagging("versioned-bucket", "new", "null", Map.of("version", "suspended")); + assertEquals(Map.of("version", "suspended"), s3Service.getObjectTagging("versioned-bucket", "new")); + } + + @Test + void taggingADeleteMarkerIsNotAllowed() { + s3Service.putBucketVersioning("versioned-bucket", "Enabled"); + s3Service.putObject("versioned-bucket", "key", "v1".getBytes(StandardCharsets.UTF_8), "text/plain", null); + String markerId = s3Service.deleteObject("versioned-bucket", "key").getVersionId(); + + // The marker is refused whether the request names it or it is the current version. + for (String versionId : Arrays.asList(null, markerId)) { + Map requests = Map.of( + "GET", () -> s3Service.getObjectTaggingResult("versioned-bucket", "key", versionId), + "PUT", () -> s3Service.putObjectTagging("versioned-bucket", "key", versionId, Map.of("k", "v")), + "DELETE", () -> s3Service.deleteObjectTagging("versioned-bucket", "key", versionId)); + requests.forEach((method, request) -> { + AwsException e = assertThrows(AwsException.class, request::run); + assertEquals("MethodNotAllowed", e.getErrorCode()); + assertEquals(405, e.getHttpStatus()); + assertEquals(Map.of("Method", method, "ResourceType", "DeleteMarker"), e.getExtendedData()); + }); + } + } + @Test void copyObjectPreservesSourceTagsByDefault() { s3Service.putBucketVersioning("versioned-bucket", "Enabled");