Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(s3): check the extra IAM actions a CreateBucket needs (#5961)

Aymen JAMAAOUI09b14e9d1087
diff --git a/docs/services/iam.md b/docs/services/iam.md
index 22c2c9b58b..1abd46f56b 100644
--- a/docs/services/iam.md
+++ b/docs/services/iam.md
@@ -1246,6 +1246,14 @@ a Query `Action` field on it does not change the action it is checked as, and it
still percent-encoded, as the router matches it, so an encoded `/` inside a parameter cannot make
the route's rule miss.
+An S3 `CreateBucket` is checked as `s3:CreateBucket`, and also needs one action for each header
+that configures the new bucket, as the CreateBucket API reference lists them:
+`s3:PutBucketAcl` for a canned ACL other than `private` or a grant header,
+`s3:PutBucketObjectLockConfiguration` and `s3:PutBucketVersioning` for
+`x-amz-bucket-object-lock-enabled: true`, and `s3:PutBucketOwnershipControls` for
+`x-amz-object-ownership`. They are checked before the bucket is created, so a denied request
+creates nothing.
+
An S3 object operation is checked as the action the S3 documentation gives it:
- **Multipart uploads:**
- `CreateMultipartUpload`, `UploadPart` and `CompleteMultipartUpload` are checked as
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 a7b5d7cf4f..2e05442d3a 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
@@ -459,6 +459,7 @@ public class S3Controller {
new String(body, StandardCharsets.UTF_8), "Tag", "Key", "Value");
}
String region = bucketRegionForCreate(locationConstraint, regionResolver.resolveRegion(httpHeaders));
+ authorizeBucketCreationSettings(httpHeaders, uriInfo, bucket, region);
s3Service.createBucket(bucket, region,
httpHeaders.getHeaderString("x-amz-object-ownership"),
httpHeaders.getHeaderString("x-amz-acl"),
@@ -4496,6 +4497,24 @@ public class S3Controller {
s3Service.authorizeObjectCreationSetting(destBucket, destKey, action, authorization, null);
}
+ /**
+ * Authorizes the actions a CreateBucket needs besides {@code s3:CreateBucket}
+ * (see {@link S3ObjectActions#bucketCreationActions}) on the new bucket. It runs before the bucket
+ * is created, so a denied request creates nothing.
+ */
+ private void authorizeBucketCreationSettings(HttpHeaders httpHeaders, UriInfo uriInfo, String bucket,
+ String region) {
+ List<String> actions = S3ObjectActions.bucketCreationActions(httpHeaders::getHeaderString);
+ if (actions.isEmpty()) {
+ return;
+ }
+ String credential = iamCredential(httpHeaders, uriInfo);
+ String resource = S3PublicAccessEvaluator.bucketArn(AwsRegions.partitionFor(region), bucket);
+ for (String action : actions) {
+ iamEnforcementFilter.authorizeAdditionalResource(credential, action, resource);
+ }
+ }
+
/**
* The credential IAM evaluates a check S3Controller makes itself against: the Authorization header,
* or a presigned request's query credential. A blank header counts as absent, as it does in
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 0b2eae236e..d4efc0e9ed 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
@@ -105,6 +105,32 @@ public final class S3ObjectActions {
return actions;
}
+ /**
+ * The actions a CreateBucket needs besides {@code s3:CreateBucket}, one for each thing its headers
+ * set on the new bucket, as the CreateBucket API reference lists them: an ACL other than
+ * {@code private}, or any grant header, needs s3:PutBucketAcl; Object Lock needs
+ * s3:PutBucketObjectLockConfiguration and s3:PutBucketVersioning; an Object Ownership needs
+ * s3:PutBucketOwnershipControls. A blank header counts as absent.
+ *
+ * @param header reads one request header, null when it is absent
+ */
+ public static List<String> bucketCreationActions(Function<String, String> header) {
+ List<String> actions = new ArrayList<>();
+ String cannedAcl = header.apply("x-amz-acl");
+ if ((!isBlank(cannedAcl) && !"private".equals(cannedAcl))
+ || GRANT_HEADERS.stream().anyMatch(name -> !isBlank(header.apply(name)))) {
+ actions.add("s3:PutBucketAcl");
+ }
+ if ("true".equalsIgnoreCase(header.apply("x-amz-bucket-object-lock-enabled"))) {
+ actions.add("s3:PutBucketObjectLockConfiguration");
+ actions.add("s3:PutBucketVersioning");
+ }
+ if (!isBlank(header.apply("x-amz-object-ownership"))) {
+ actions.add("s3:PutBucketOwnershipControls");
+ }
+ return actions;
+ }
+
/**
* The condition keys an object creation offers {@code s3:PutObject} and each of
* {@link #objectCreationActions}: {@code s3:RequestObjectTag/<key>} and
@@ -262,4 +288,8 @@ public final class S3ObjectActions {
return null;
}
}
+
+ private static boolean isBlank(String value) {
+ return value == null || value.isBlank();
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3BucketCreationIamEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3BucketCreationIamEnforcementIntegrationTest.java
new file mode 100644
index 0000000000..5cc5f3d8ee
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3BucketCreationIamEnforcementIntegrationTest.java
@@ -0,0 +1,164 @@
+package io.github.hectorvent.floci.services.s3;
+
+import io.github.hectorvent.floci.testing.EnforcementFixtures;
+import io.github.hectorvent.floci.testing.IamEnforcementProfile;
+import io.github.hectorvent.floci.testing.PartitionCleanup;
+import io.quarkus.test.common.http.TestHTTPResource;
+import io.quarkus.test.junit.QuarkusTest;
+import io.quarkus.test.junit.TestProfile;
+import io.restassured.specification.RequestSpecification;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import java.net.URI;
+import java.util.List;
+import java.util.UUID;
+
+import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.containsString;
+
+/**
+ * A CreateBucket whose headers configure the new bucket needs more than {@code s3:CreateBucket}. The
+ * CreateBucket API reference lists them: s3:PutBucketAcl for an ACL other than {@code private} or a
+ * grant header, s3:PutBucketObjectLockConfiguration and s3:PutBucketVersioning for Object Lock, and
+ * s3:PutBucketOwnershipControls for {@code x-amz-object-ownership}. A denied request creates nothing.
+ */
+@QuarkusTest
+@TestProfile(IamEnforcementProfile.class)
+class S3BucketCreationIamEnforcementIntegrationTest {
+
+ private static final String ADMIN = "test";
+
+ @TestHTTPResource("/")
+ static URI baseUri;
+
+ @RegisterExtension
+ final PartitionCleanup cleanup = new PartitionCleanup();
+
+ @Test
+ void createBucket_objectLockWithoutBothPermissions_isDeniedAndCreatesNothing() {
+ String user = uniqueName("create-lock-deny");
+ String accessKeyId = createUser(user);
+
+ userPolicy(user, List.of("s3:CreateBucket"));
+ assertDenied(accessKeyId, uniqueName("lock-none"), "x-amz-bucket-object-lock-enabled", "true");
+
+ userPolicy(user, List.of("s3:CreateBucket", "s3:PutBucketObjectLockConfiguration"));
+ assertDenied(accessKeyId, uniqueName("lock-no-versioning"), "x-amz-bucket-object-lock-enabled", "true");
+
+ userPolicy(user, List.of("s3:CreateBucket", "s3:PutBucketVersioning"));
+ assertDenied(accessKeyId, uniqueName("lock-no-config"), "x-amz-bucket-object-lock-enabled", "true");
+ }
+
+ @Test
+ void createBucket_objectLockWithBothPermissions_createsALockedBucket() {
+ String user = uniqueName("create-lock-allow");
+ String accessKeyId = createUser(user);
+ userPolicy(user, List.of("s3:CreateBucket", "s3:PutBucketObjectLockConfiguration", "s3:PutBucketVersioning"));
+ String bucket = uniqueName("lock-allowed");
+
+ assertCreated(accessKeyId, bucket, "x-amz-bucket-object-lock-enabled", "true");
+
+ admin().when().get("/" + bucket + "?object-lock")
+ .then().statusCode(200).body(containsString("<ObjectLockEnabled>Enabled</ObjectLockEnabled>"));
+ }
+
+ @Test
+ void createBucket_objectOwnership_needsPutBucketOwnershipControls() {
+ String user = uniqueName("create-ownership");
+ String accessKeyId = createUser(user);
+
+ userPolicy(user, List.of("s3:CreateBucket"));
+ assertDenied(accessKeyId, uniqueName("ownership-denied"), "x-amz-object-ownership", "BucketOwnerPreferred");
+
+ userPolicy(user, List.of("s3:CreateBucket", "s3:PutBucketOwnershipControls"));
+ assertCreated(accessKeyId, uniqueName("ownership-allowed"), "x-amz-object-ownership", "BucketOwnerPreferred");
+ }
+
+ @Test
+ void createBucket_privateCannedAcl_needsOnlyCreateBucket() {
+ String user = uniqueName("create-private");
+ String accessKeyId = createUser(user);
+ userPolicy(user, List.of("s3:CreateBucket"));
+
+ assertCreated(accessKeyId, uniqueName("acl-private"), "x-amz-acl", "private");
+ }
+
+ @Test
+ void createBucket_otherCannedAcl_needsPutBucketAcl() {
+ String user = uniqueName("create-canned");
+ String accessKeyId = createUser(user);
+ userPolicy(user, List.of("s3:CreateBucket"));
+
+ assertDenied(accessKeyId, uniqueName("acl-canned"), "x-amz-acl", "authenticated-read");
+ }
+
+ @Test
+ void createBucket_grantHeader_needsPutBucketAcl() {
+ String user = uniqueName("create-grant");
+ String accessKeyId = createUser(user);
+
+ userPolicy(user, List.of("s3:CreateBucket", "s3:PutBucketOwnershipControls"));
+ String denied = uniqueName("grant-denied");
+ createRequest(accessKeyId, "x-amz-object-ownership", "ObjectWriter")
+ .header("x-amz-grant-read", "id=\"123456789012\"")
+ .when().put("/" + denied)
+ .then().statusCode(403).body(containsString("AccessDenied"));
+ assertMissing(denied);
+
+ userPolicy(user, List.of("s3:CreateBucket", "s3:PutBucketOwnershipControls", "s3:PutBucketAcl"));
+ String allowed = uniqueName("grant-allowed");
+ cleanup.register(() -> deleteBucket(allowed));
+ createRequest(accessKeyId, "x-amz-object-ownership", "ObjectWriter")
+ .header("x-amz-grant-read", "id=\"123456789012\"")
+ .when().put("/" + allowed)
+ .then().statusCode(200);
+ }
+
+ private void assertDenied(String accessKeyId, String bucket, String header, String value) {
+ createRequest(accessKeyId, header, value)
+ .when().put("/" + bucket)
+ .then().statusCode(403).body(containsString("AccessDenied"));
+ assertMissing(bucket);
+ }
+
+ private void assertCreated(String accessKeyId, String bucket, String header, String value) {
+ cleanup.register(() -> deleteBucket(bucket));
+ createRequest(accessKeyId, header, value)
+ .when().put("/" + bucket)
+ .then().statusCode(200);
+ admin().when().head("/" + bucket).then().statusCode(200);
+ }
+
+ private static RequestSpecification createRequest(String accessKeyId, String header, String value) {
+ return given().header("Authorization", auth(accessKeyId, "s3")).header(header, value);
+ }
+
+ private static void assertMissing(String bucket) {
+ admin().when().head("/" + bucket).then().statusCode(404);
+ }
+
+ private static void deleteBucket(String bucket) {
+ admin().when().delete("/" + bucket).then().statusCode(204);
+ }
+
+ private static RequestSpecification admin() {
+ return given().header("Authorization", auth(ADMIN, "s3"));
+ }
+
+ private static String uniqueName(String prefix) {
+ return prefix + "-" + UUID.randomUUID().toString().substring(0, 8);
+ }
+
+ private String createUser(String user) {
+ return EnforcementFixtures.createUser(cleanup, baseUri.getPort(), auth(ADMIN, "iam"), user).accessKeyId();
+ }
+
+ private void userPolicy(String user, List<String> actions) {
+ String actionList = String.join("\",\"", actions);
+ String statement = "{\"Effect\":\"Allow\",\"Action\":[\"" + actionList + "\"],\"Resource\":\"*\"}";
+ EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth(ADMIN, "iam"), user, "p",
+ "{\"Version\":\"2012-10-17\",\"Statement\":[" + statement + "]}");
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/testing/EnforcementFixtures.java b/src/test/java/io/github/hectorvent/floci/testing/EnforcementFixtures.java
index b7ac14dfc6..259d018bca 100644
--- a/src/test/java/io/github/hectorvent/floci/testing/EnforcementFixtures.java
+++ b/src/test/java/io/github/hectorvent/floci/testing/EnforcementFixtures.java
@@ -112,6 +112,18 @@ public final class EnforcementFixtures {
key.getString("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.SecretAccessKey"));
}
+ /**
+ * Puts {@code policyDocument} inline on a user, as {@code policyName}, replacing any policy of that
+ * name. The user's removal deletes it with the user.
+ *
+ * @param authorization an IAM-scoped header for the account the user belongs to
+ */
+ public static void putUserPolicy(int port, String authorization, String userName, String policyName,
+ String policyDocument) {
+ iam(port, authorization, "PutUserPolicy", "UserName", userName, "PolicyName", policyName,
+ "PolicyDocument", policyDocument).statusCode(200);
+ }
+
/**
* Creates a role under {@code path} that the root of its own {@code account} may assume, with
* {@code policy} inline, registering its {@link #removeRole} with {@code cleanup} before the