Light, fluffy, and always free - The AWS Local Emulator alternative
fix(iam): name a denied S3 POST in CloudTrail (#5942)
Priamo Germosen401f5ce2d745
diff --git a/docs/services/cloudtrail.md b/docs/services/cloudtrail.mdindex 4073d1490e..48701ca8f6 100644--- a/docs/services/cloudtrail.md+++ b/docs/services/cloudtrail.md@@ -51,8 +51,17 @@ against a bucket matched by an active trail's selectors:Both success and `AwsException` paths emit; the latter populates`errorCode` / `errorMessage` (`NoSuchKey`, `NoSuchBucketPolicy`, etc.).-If `floci.iam.enforcement-enabled` is set, IAM-deny responses also emit-records with `errorCode: "AccessDenied"` for the same op set.+If `floci.services.iam.enforcement-enabled` is set, IAM-deny responses also+emit records with `errorCode: "AccessDenied"`, named after the operation that+was refused. Besides the ops above, that covers the object ACL and tagging+operations, `PutObjectRetention`, `GetObjectAttributes`, and the `POST`+operations `CreateMultipartUpload`, `CompleteMultipartUpload`, `RestoreObject`+and `SelectObjectContent`. A `POST` is named from the request's+sub-resource, since `CreateMultipartUpload` and `CompleteMultipartUpload` are+both checked as `s3:PutObject` and `SelectObjectContent` as `s3:GetObject`. A request+refused for a permission it needs besides its own, such as+`s3:PutObjectTagging` for an upload that sets tags, is recorded as the+operation.## Configurationdiff --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 e937ff77ef..daed584a48 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@@ -1260,7 +1260,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter {if (action == null || !action.startsWith("s3:")) {return;}- String eventName = mapS3ActionToEventName(action, ctx.getMethod());+ String eventName = mapS3ActionToEventName(action, ctx.getMethod(),+ ctx.getUriInfo().getQueryParameters().keySet());if (eventName == null) {return;}@@ -1307,16 +1308,14 @@ public class IamEnforcementFilter implements ContainerRequestFilter {}// Package-private for unit testing.- static String mapS3ActionToEventName(String action, String httpMethod) {+ static String mapS3ActionToEventName(String action, String httpMethod, Set<String> queryParameters) {if (action == null) return null;// Action set sourced from IamActionRegistry — see that file for any// additions. HEAD on an object is bucketed under s3:GetObject by the- // registry, so we distinguish via httpMethod. The POST operations checked as- // s3:PutObject or s3:GetObject (multipart create and complete, Select) cannot- // be told apart from the action, so they get no name rather than a wrong one.- // The GetObjectAttributes actions are the operation its own check refuses.+ // registry, so we distinguish via httpMethod. The GetObjectAttributes actions+ // are the operation its own check refuses.if ("POST".equalsIgnoreCase(httpMethod)) {- return null;+ return postObjectEventName(action, queryParameters);}return switch (action) {case "s3:GetObject", "s3:GetObjectVersion" ->@@ -1336,6 +1335,28 @@ public class IamEnforcementFilter implements ContainerRequestFilter {};}+ /**+ * The operation a denied POST on an object names. Its action alone is not enough: CreateMultipartUpload+ * and CompleteMultipartUpload are both checked as {@code s3:PutObject}, and SelectObjectContent as+ * {@code s3:GetObject}. The sub-resource that chose the action in {@code IamActionRegistry} names the+ * operation, and {@code ?uploads}, which S3Controller runs ahead of an {@code uploadId}, makes a+ * request naming both a create. A POST naming none gets no name rather than a wrong one.+ */+ private static String postObjectEventName(String action, Set<String> queryParameters) {+ return switch (action) {+ case "s3:PutObject" -> {+ if (queryParameters.contains("uploads")) {+ yield "CreateMultipartUpload";+ }+ yield queryParameters.contains("uploadId") ? "CompleteMultipartUpload" : null;+ }+ case "s3:RestoreObject" -> queryParameters.contains("restore") ? "RestoreObject" : null;+ case "s3:GetObject", "s3:GetObjectVersion" ->+ queryParameters.contains("select") ? "SelectObjectContent" : null;+ default -> null;+ };+ }+/** Logs and Secrets Manager targets depend on the serving operation and the request's partition. */private List<String> targetResources(String credentialScope, String action, ContainerRequestContext ctx,String region, String accountId) {diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailService.java b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailService.javaindex 17f2a2e765..d1d697f798 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailService.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailService.java@@ -776,13 +776,14 @@ public class CloudTrailService {return key.startsWith(configKeyPart);}- // Package-private for unit testing.+ // Package-private for unit testing. A read-only event is one that doesn't change a resource, so+ // SelectObjectContent is one although it is sent as a POST.static boolean isReadOnlyEvent(String eventName) {if (eventName == null) return true;return switch (eventName) {case "GetObject", "HeadObject", "ListObjects", "ListObjectsV2",- "GetObjectAcl", "GetObjectTagging", "ListMultipartUploads",- "GetObjectAnnotation", "ListObjectAnnotations" -> true;+ "GetObjectAcl", "GetObjectTagging", "GetObjectAttributes", "ListMultipartUploads",+ "GetObjectAnnotation", "ListObjectAnnotations", "SelectObjectContent" -> true;default -> false;};}diff --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 5fc2a977e5..f280cf8d6d 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,21 @@ class IamEnforcementFilterTest {"x-amz-object-attributes", "ETag", "s3:GetObject", "s3:GetObjectAttributes"));}+ @Test+ void aDeniedPostIsRecordedAsTheOperationItsSubResourceSelects() {+ // CreateMultipartUpload and CompleteMultipartUpload are both checked as s3:PutObject, so the+ // event is named from the request's sub-resource. An upload placing a legal hold also needs+ // s3:PutObjectLegalHold, and a conditional complete needs s3:GetObject.+ MultivaluedHashMap<String, String> create = new MultivaluedHashMap<>();+ create.putSingle("uploads", "");+ assertEquals("CreateMultipartUpload", deniedExtraActionEvent("POST", create,+ "x-amz-object-lock-legal-hold", "ON", "s3:PutObject", "s3:PutObjectLegalHold"));+ MultivaluedHashMap<String, String> complete = new MultivaluedHashMap<>();+ complete.putSingle("uploadId", "u1");+ assertEquals("CompleteMultipartUpload", deniedExtraActionEvent("POST", complete,+ "If-Match", "\"etag\"", "s3:PutObject", "s3:GetObject"));+ }+@Testvoid theGovernanceBypassDecisionIsRecordedForS3ToHonour() {// S3Controller lets a delete bypass a lock only as far as this check approved it, so a lock setdiff --git a/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterUnitTest.java b/src/test/java/io/github/hectorvent/floci/core/common/IamEnforcementFilterUnitTest.javaindex fba9c86a65..5930656f5f 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@@ -2,6 +2,8 @@ package io.github.hectorvent.floci.core.common;import org.junit.jupiter.api.Test;+import java.util.Set;+import static org.junit.jupiter.api.Assertions.assertArrayEquals;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertNull;@@ -18,104 +20,123 @@ class IamEnforcementFilterUnitTest {@Testvoid getObject_overGet_emitsAsGetObject() {assertEquals("GetObject",- IamEnforcementFilter.mapS3ActionToEventName("s3:GetObject", "GET"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:GetObject", "GET", Set.of()));}@Testvoid getObject_overHead_emitsAsHeadObject() {assertEquals("HeadObject",- IamEnforcementFilter.mapS3ActionToEventName("s3:GetObject", "HEAD"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:GetObject", "HEAD", Set.of()));}@Testvoid putObject_emitsAsPutObject() {assertEquals("PutObject",- IamEnforcementFilter.mapS3ActionToEventName("s3:PutObject", "PUT"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:PutObject", "PUT", Set.of()));}@Testvoid deleteObject_emitsAsDeleteObject() {assertEquals("DeleteObject",- IamEnforcementFilter.mapS3ActionToEventName("s3:DeleteObject", "DELETE"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:DeleteObject", "DELETE", Set.of()));}@Testvoid listBucket_emitsAsListObjects() {assertEquals("ListObjects",- IamEnforcementFilter.mapS3ActionToEventName("s3:ListBucket", "GET"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:ListBucket", "GET", Set.of()));}@Testvoid listAllMyBuckets_emitsAsListBuckets() {assertEquals("ListBuckets",- IamEnforcementFilter.mapS3ActionToEventName("s3:ListAllMyBuckets", "GET"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:ListAllMyBuckets", "GET", Set.of()));}@Testvoid getObjectAcl_emitsAsGetObjectAcl() {assertEquals("GetObjectAcl",- IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectAcl", "GET"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectAcl", "GET", Set.of()));}@Testvoid putObjectAcl_emitsAsPutObjectAcl() {assertEquals("PutObjectAcl",- IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectAcl", "PUT"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectAcl", "PUT", Set.of()));}@Testvoid objectTagging_emitsCorrectEventNames() {assertEquals("GetObjectTagging",- IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectTagging", "GET"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectTagging", "GET", Set.of()));assertEquals("PutObjectTagging",- IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectTagging", "PUT"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectTagging", "PUT", Set.of()));assertEquals("DeleteObjectTagging",- IamEnforcementFilter.mapS3ActionToEventName("s3:DeleteObjectTagging", "DELETE"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:DeleteObjectTagging", "DELETE", Set.of()));}@Testvoid versionActions_emitAsTheOperationTheyCheck() {assertEquals("GetObject",- IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersion", "GET"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersion", "GET", Set.of()));assertEquals("HeadObject",- IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersion", "HEAD"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersion", "HEAD", Set.of()));assertEquals("DeleteObject",- IamEnforcementFilter.mapS3ActionToEventName("s3:DeleteObjectVersion", "DELETE"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:DeleteObjectVersion", "DELETE", Set.of()));assertEquals("GetObjectAcl",- IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersionAcl", "GET"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersionAcl", "GET", Set.of()));assertEquals("PutObjectAcl",- IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectVersionAcl", "PUT"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectVersionAcl", "PUT", Set.of()));assertEquals("GetObjectTagging",- IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersionTagging", "GET"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersionTagging", "GET", Set.of()));assertEquals("PutObjectTagging",- IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectVersionTagging", "PUT"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectVersionTagging", "PUT", Set.of()));assertEquals("DeleteObjectTagging",- IamEnforcementFilter.mapS3ActionToEventName("s3:DeleteObjectVersionTagging", "DELETE"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:DeleteObjectVersionTagging", "DELETE", Set.of()));}@Testvoid extraActionChecks_emitAsTheOperationTheyRefuse() {assertEquals("PutObjectRetention",- IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectRetention", "PUT"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectRetention", "PUT", Set.of()));assertEquals("GetObjectAttributes",- IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectAttributes", "GET"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectAttributes", "GET", Set.of()));assertEquals("GetObjectAttributes",- IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersionAttributes", "GET"));+ IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersionAttributes", "GET", Set.of()));+ }++ @Test+ void postOperations_areNamedByTheSubResourceThatChoseTheirAction() {+ // CreateMultipartUpload and CompleteMultipartUpload are both checked as s3:PutObject, and+ // SelectObjectContent as s3:GetObject, so the sub-resource tells which operation ran.+ assertEquals("CreateMultipartUpload",+ IamEnforcementFilter.mapS3ActionToEventName("s3:PutObject", "POST", Set.of("uploads")));+ assertEquals("CompleteMultipartUpload",+ IamEnforcementFilter.mapS3ActionToEventName("s3:PutObject", "POST", Set.of("uploadId")));+ // S3Controller runs ?uploads ahead of an uploadId, so a request naming both creates an upload.+ assertEquals("CreateMultipartUpload",+ IamEnforcementFilter.mapS3ActionToEventName("s3:PutObject", "POST", Set.of("uploads", "uploadId")));+ assertEquals("RestoreObject",+ IamEnforcementFilter.mapS3ActionToEventName("s3:RestoreObject", "POST", Set.of("restore")));+ assertEquals("SelectObjectContent", IamEnforcementFilter.mapS3ActionToEventName("s3:GetObject", "POST",+ Set.of("select", "select-type")));+ assertEquals("SelectObjectContent", IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectVersion",+ "POST", Set.of("select", "select-type", "versionId")));}@Test- void postOperations_returnNullRatherThanTheWrongName() {- // CreateMultipartUpload and CompleteMultipartUpload are checked as s3:PutObject, and- // SelectObjectContent as s3:GetObject; neither name is the operation that ran.- assertNull(IamEnforcementFilter.mapS3ActionToEventName("s3:PutObject", "POST"));- assertNull(IamEnforcementFilter.mapS3ActionToEventName("s3:GetObject", "POST"));+ void postOperations_withoutTheirSubResource_returnNullRatherThanTheWrongName() {+ assertNull(IamEnforcementFilter.mapS3ActionToEventName("s3:PutObject", "POST", Set.of()));+ assertNull(IamEnforcementFilter.mapS3ActionToEventName("s3:GetObject", "POST", Set.of()));+ assertNull(IamEnforcementFilter.mapS3ActionToEventName("s3:RestoreObject", "POST", Set.of()));+ assertNull(IamEnforcementFilter.mapS3ActionToEventName("s3:DeleteObject", "POST", Set.of("delete")));}@Testvoid unknownAction_returnsNull() {- assertNull(IamEnforcementFilter.mapS3ActionToEventName("s3:GetBucketAcl", "GET"));- assertNull(IamEnforcementFilter.mapS3ActionToEventName("dynamodb:Query", "POST"));- assertNull(IamEnforcementFilter.mapS3ActionToEventName(null, "GET"));+ assertNull(IamEnforcementFilter.mapS3ActionToEventName("s3:GetBucketAcl", "GET", Set.of()));+ assertNull(IamEnforcementFilter.mapS3ActionToEventName("dynamodb:Query", "POST", Set.of()));+ assertNull(IamEnforcementFilter.mapS3ActionToEventName(null, "GET", Set.of()));}// --- parseS3Resource ---diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailSelectorMatchingTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailSelectorMatchingTest.javaindex 868e1a0bf2..0358cedae4 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailSelectorMatchingTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailSelectorMatchingTest.java@@ -1,9 +1,24 @@package io.github.hectorvent.floci.services.cloudtrail;+import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.databind.node.ObjectNode;+import io.github.hectorvent.floci.core.common.RegionResolver;+import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;+import io.github.hectorvent.floci.core.storage.StorageFactory;+import io.github.hectorvent.floci.services.cloudtrail.model.DataResource;+import io.github.hectorvent.floci.services.cloudtrail.model.EventSelector;+import io.github.hectorvent.floci.services.iam.IamService;import org.junit.jupiter.api.Test;+import java.util.List;++import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertFalse;import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.ArgumentMatchers.any;+import static org.mockito.ArgumentMatchers.anyString;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.when;/*** Unit tests for {@link CloudTrailService}'s S3 data-resource matcher and@@ -12,6 +27,9 @@ import static org.junit.jupiter.api.Assertions.assertTrue;*/class CloudTrailSelectorMatchingTest {+ private static final String REGION = "us-east-1";+ private static final String ACCOUNT = "000000000000";+// --- matchesS3DataResourceArn ---@Test@@ -144,6 +162,8 @@ class CloudTrailSelectorMatchingTest {assertTrue(CloudTrailService.isReadOnlyEvent("GetObjectAcl"));assertTrue(CloudTrailService.isReadOnlyEvent("GetObjectTagging"));assertTrue(CloudTrailService.isReadOnlyEvent("ListMultipartUploads"));+ assertTrue(CloudTrailService.isReadOnlyEvent("GetObjectAttributes"));+ assertTrue(CloudTrailService.isReadOnlyEvent("SelectObjectContent"));}@Test@@ -151,6 +171,48 @@ class CloudTrailSelectorMatchingTest {assertFalse(CloudTrailService.isReadOnlyEvent("PutObject"));assertFalse(CloudTrailService.isReadOnlyEvent("DeleteObject"));assertFalse(CloudTrailService.isReadOnlyEvent("PutObjectAcl"));+ assertFalse(CloudTrailService.isReadOnlyEvent("PutObjectRetention"));+ assertFalse(CloudTrailService.isReadOnlyEvent("CreateMultipartUpload"));+ assertFalse(CloudTrailService.isReadOnlyEvent("CompleteMultipartUpload"));+ assertFalse(CloudTrailService.isReadOnlyEvent("RestoreObject"));+ }++ // --- a ReadOnly trail ---++ /**+ * A denial is recorded under the name of the operation refused, so a read IAM denies reaches a+ * ReadOnly trail and is marked read-only, while a denied write stays out of it.+ */+ @Test+ void readOnlyTrail_keepsDeniedReadsAsReadOnly_andLeavesOutDeniedWrites() {+ StorageFactory storageFactory = mock(StorageFactory.class);+ when(storageFactory.create(anyString(), anyString(), any()))+ .thenAnswer(invocation -> AccountAwareStorageBackend.inMemory(ACCOUNT));+ CloudTrailService service = new CloudTrailService(storageFactory, new RegionResolver(REGION, ACCOUNT),+ mock(IamService.class), new ObjectMapper());+ service.createTrail(REGION, "reads", "trail-logs", null, null, true, false, false, false);+ service.putEventSelectors(REGION, "reads", List.of(new EventSelector("ReadOnly", false,+ List.of(new DataResource("AWS::S3::Object", List.of("arn:aws:s3:::source/"))), null)));+ service.startLogging(REGION, "reads");++ for (String operation : List.of("SelectObjectContent", "GetObjectAttributes", "CreateMultipartUpload")) {+ service.emitS3DataEvent(CloudTrailService.S3EventInput.builder()+ .region(REGION)+ .eventName(operation)+ .bucketName("source")+ .key("data.csv")+ .errorCode("AccessDenied")+ .errorMessage("Access Denied")+ .build());+ }++ List<ObjectNode> records = service.drainPendingRecords(+ new CloudTrailService.TrailKey(REGION, "reads", REGION));+ assertEquals(List.of("SelectObjectContent", "GetObjectAttributes"),+ records.stream().map(record -> record.path("eventName").asText()).toList());+ for (ObjectNode record : records) {+ assertTrue(record.path("readOnly").asBoolean(), record::toString);+ }}@Test