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:
Priamo Germosen authored and GitHub committed 2026-10-18 20:43:25 -05:00
1 parent f0975a9f47
commit 401f5ce2d7
6 files changed
+171 -42

No files matched your search

@@ -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) {
@@ -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;
};
}