mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): name a denied S3 POST in CloudTrail (#5942)
Under IAM enforcement a denied S3 request is recorded in CloudTrail as the operation that was refused, but a denied POST on an object was not recorded at all. CreateMultipartUpload and CompleteMultipartUpload are both checked as s3:PutObject and SelectObjectContent as s3:GetObject, so the action alone could not say which operation ran, and RestoreObject had no name. A denied POST is now named from the sub-resource that chose its action: ?uploads is CreateMultipartUpload, an uploadId CompleteMultipartUpload, ?restore RestoreObject and ?select SelectObjectContent, with ?uploads ahead of an uploadId as S3Controller runs them. The S3 user guide lists all four among the object data events CloudTrail logs. A POST naming none of them still gets no name rather than a wrong one. SelectObjectContent and GetObjectAttributes were missing from the read-only events, so a trail logging only reads dropped their denials and other trails recorded them as writes. CloudTrail counts an event that changes no resource as read-only, and neither one changes the object.
This commit is contained in:
1 parent
f0975a9f47
commit
401f5ce2d7
6 files changed
+171
-42
No files matched your search
@@ -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"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theGovernanceBypassDecisionIsRecordedForS3ToHonour() {
|
||||
// S3Controller lets a delete bypass a lock only as far as this check approved it, so a lock set
|
||||
|
||||
+51
-30
@@ -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 {
|
||||
@Test
|
||||
void getObject_overGet_emitsAsGetObject() {
|
||||
assertEquals("GetObject",
|
||||
IamEnforcementFilter.mapS3ActionToEventName("s3:GetObject", "GET"));
|
||||
IamEnforcementFilter.mapS3ActionToEventName("s3:GetObject", "GET", Set.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getObject_overHead_emitsAsHeadObject() {
|
||||
assertEquals("HeadObject",
|
||||
IamEnforcementFilter.mapS3ActionToEventName("s3:GetObject", "HEAD"));
|
||||
IamEnforcementFilter.mapS3ActionToEventName("s3:GetObject", "HEAD", Set.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObject_emitsAsPutObject() {
|
||||
assertEquals("PutObject",
|
||||
IamEnforcementFilter.mapS3ActionToEventName("s3:PutObject", "PUT"));
|
||||
IamEnforcementFilter.mapS3ActionToEventName("s3:PutObject", "PUT", Set.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteObject_emitsAsDeleteObject() {
|
||||
assertEquals("DeleteObject",
|
||||
IamEnforcementFilter.mapS3ActionToEventName("s3:DeleteObject", "DELETE"));
|
||||
IamEnforcementFilter.mapS3ActionToEventName("s3:DeleteObject", "DELETE", Set.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listBucket_emitsAsListObjects() {
|
||||
assertEquals("ListObjects",
|
||||
IamEnforcementFilter.mapS3ActionToEventName("s3:ListBucket", "GET"));
|
||||
IamEnforcementFilter.mapS3ActionToEventName("s3:ListBucket", "GET", Set.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listAllMyBuckets_emitsAsListBuckets() {
|
||||
assertEquals("ListBuckets",
|
||||
IamEnforcementFilter.mapS3ActionToEventName("s3:ListAllMyBuckets", "GET"));
|
||||
IamEnforcementFilter.mapS3ActionToEventName("s3:ListAllMyBuckets", "GET", Set.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getObjectAcl_emitsAsGetObjectAcl() {
|
||||
assertEquals("GetObjectAcl",
|
||||
IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectAcl", "GET"));
|
||||
IamEnforcementFilter.mapS3ActionToEventName("s3:GetObjectAcl", "GET", Set.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void putObjectAcl_emitsAsPutObjectAcl() {
|
||||
assertEquals("PutObjectAcl",
|
||||
IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectAcl", "PUT"));
|
||||
IamEnforcementFilter.mapS3ActionToEventName("s3:PutObjectAcl", "PUT", Set.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 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()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 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()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 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_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_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_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")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 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 ---
|
||||
|
||||
+62
@@ -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
|
||||
|
||||
Reference in new issue
Block a user