diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/DynamoDbTransactionPolicyBoundaryIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/DynamoDbTransactionPolicyBoundaryIntegrationTest.java index 0012296a70..55af90bb15 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/DynamoDbTransactionPolicyBoundaryIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/DynamoDbTransactionPolicyBoundaryIntegrationTest.java @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.iam; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.testing.IamEnforcementProfile; import io.github.hectorvent.floci.testing.PartitionCleanup; import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; import io.quarkus.test.common.http.TestHTTPResource; @@ -30,8 +31,16 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertAll; +/** + * The boundaries of IAM authorization for DynamoDB transaction items: the caller's account and + * region, foreign ARNs, session policies, service control policies and request shape checks. + * + *

{@code scpDeniesUnderlyingItemActionForMemberRootAndUser} relies on the SCP enforcement that + * {@link IamEnforcementProfile} turns on next to IAM enforcement, and removes the organization it + * creates once it ends. + */ @QuarkusTest -@TestProfile(ScpEnforcementLeaveOrganizationIntegrationTest.ScpEnforcementProfile.class) +@TestProfile(IamEnforcementProfile.class) class DynamoDbTransactionPolicyBoundaryIntegrationTest { @RegisterExtension diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/ScpEnforcementLeaveOrganizationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/ScpEnforcementLeaveOrganizationIntegrationTest.java index bb19c3d0b8..e822b6aaec 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/ScpEnforcementLeaveOrganizationIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/ScpEnforcementLeaveOrganizationIntegrationTest.java @@ -1,15 +1,18 @@ package io.github.hectorvent.floci.services.iam; +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.QuarkusTestProfile; import io.quarkus.test.junit.TestProfile; import io.restassured.config.EncoderConfig; import io.restassured.http.ContentType; import io.restassured.path.json.JsonPath; import io.restassured.response.Response; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; -import java.util.Map; +import java.net.URI; import java.util.UUID; import static io.restassured.RestAssured.config; @@ -26,9 +29,13 @@ import static org.hamcrest.Matchers.containsString; * stop the member from leaving. This test drives the full Organizations control plane over HTTP * (management sets up org + OU + deny-SCP + member) and asserts that the member's * {@code LeaveOrganization} is denied with 403 while a non-denied action still succeeds.

+ * + *

SCP enforcement comes from {@link IamEnforcementProfile}, which this class shares with the + * other IAM enforcement tests, so it removes the organization, OU, SCP and member account it + * builds once the test ends.

*/ @QuarkusTest -@TestProfile(ScpEnforcementLeaveOrganizationIntegrationTest.ScpEnforcementProfile.class) +@TestProfile(IamEnforcementProfile.class) class ScpEnforcementLeaveOrganizationIntegrationTest { private static final String MGMT = "000000000000"; @@ -38,12 +45,19 @@ class ScpEnforcementLeaveOrganizationIntegrationTest { {"Sid":"DenyLeaveOrg","Effect":"Deny", "Action":"organizations:LeaveOrganization","Resource":"*"}]}"""; + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + + @TestHTTPResource + URI endpoint; + @Test void scpDeniesMemberLeaveOrganizationButAllowsOtherActions() { String suffix = UUID.randomUUID().toString().substring(0, 8); // --- management sets up the organization ----------------------------------------------- org(MGMT, "CreateOrganization", "{\"FeatureSet\":\"ALL\"}").then().statusCode(200); + cleanup.register(() -> org(MGMT, "DeleteOrganization", "{}").then().statusCode(200)); // A new root has no policy types enabled; SCPs only participate once EnablePolicyType // turns them on. FullAWSAccess is already attached, so enabling them denies nothing alone. @@ -57,19 +71,28 @@ class ScpEnforcementLeaveOrganizationIntegrationTest { + "\"Description\":\"deny leave org\",\"Content\":" + jsonString(DENY_LEAVE_ORG_SCP) + "}").asString()) .getString("Policy.PolicySummary.Id"); + cleanup.register(() -> org(MGMT, "DeletePolicy", "{\"PolicyId\":\"" + policyId + "\"}") + .then().statusCode(200)); String ouId = JsonPath.from(org(MGMT, "CreateOrganizationalUnit", "{\"ParentId\":\"" + rootId + "\",\"Name\":\"Denied-" + suffix + "\"}").asString()) .getString("OrganizationalUnit.Id"); + cleanup.register(() -> org(MGMT, "DeleteOrganizationalUnit", + "{\"OrganizationalUnitId\":\"" + ouId + "\"}").then().statusCode(200)); org(MGMT, "AttachPolicy", "{\"PolicyId\":\"" + policyId + "\",\"TargetId\":\"" + ouId + "\"}") .then().statusCode(200); + cleanup.register(() -> org(MGMT, "DetachPolicy", + "{\"PolicyId\":\"" + policyId + "\",\"TargetId\":\"" + ouId + "\"}") + .then().statusCode(200)); // CreateAccount is synchronous in floci: the account id is present on the returned status. String memberId = JsonPath.from(org(MGMT, "CreateAccount", "{\"AccountName\":\"member-" + suffix + "\",\"Email\":\"member-" + suffix + "@floci.test\"}") .asString()).getString("CreateAccountStatus.AccountId"); + cleanup.register(() -> org(MGMT, "RemoveAccountFromOrganization", + "{\"AccountId\":\"" + memberId + "\"}").then().statusCode(200)); org(MGMT, "MoveAccount", "{\"AccountId\":\"" + memberId + "\",\"SourceParentId\":\"" + rootId @@ -103,8 +126,9 @@ class ScpEnforcementLeaveOrganizationIntegrationTest { .body(containsString("AccessDeniedException")); } - private static Response org(String account, String action, String body) { + private Response org(String account, String action, String body) { return given() + .port(endpoint.getPort()) .config(config().encoderConfig(EncoderConfig.encoderConfig() .encodeContentTypeAs("application/x-amz-json-1.1", ContentType.JSON))) .header("Authorization", auth(account)) @@ -136,13 +160,4 @@ class ScpEnforcementLeaveOrganizationIntegrationTest { } return sb.append('"').toString(); } - - public static final class ScpEnforcementProfile implements QuarkusTestProfile { - @Override - public Map getConfigOverrides() { - return Map.of( - "floci.services.iam.enforcement-enabled", "true", - "floci.services.organizations.scp-enforcement-enabled", "true"); - } - } } diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostAuthEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostAuthEnforcementIntegrationTest.java index e9473f8864..f730f73b01 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostAuthEnforcementIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostAuthEnforcementIntegrationTest.java @@ -1,23 +1,29 @@ package io.github.hectorvent.floci.services.s3; +import io.github.hectorvent.floci.testing.EnforcementFixtures.UserCredentials; +import io.github.hectorvent.floci.testing.PartitionCleanup; +import io.github.hectorvent.floci.testing.S3IamEnforcementProfile; import io.github.hectorvent.floci.testutil.S3RequestSigner; +import io.quarkus.test.common.http.TestHTTPResource; import io.quarkus.test.junit.QuarkusTest; -import io.quarkus.test.junit.QuarkusTestProfile; import io.quarkus.test.junit.TestProfile; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.extension.RegisterExtension; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; +import java.net.URI; import java.nio.charset.StandardCharsets; import java.time.Instant; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.Base64; -import java.util.Map; +import static io.github.hectorvent.floci.testing.EnforcementFixtures.createUser; +import static io.github.hectorvent.floci.testing.EnforcementFixtures.removeBucket; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.notNullValue; @@ -26,27 +32,27 @@ import static org.hamcrest.Matchers.notNullValue; * Regression coverage for presigned POST (browser-form multipart upload) rejecting fabricated * or absent credentials once {@code floci.services.s3.enforce-auth} is enabled: see the fix in * {@code S3Controller#validatePresignedPostAuth}. + * + *

It runs on {@link S3IamEnforcementProfile}, whose global bucket namespace is inert here: every + * bucket the class creates is reached only from the account that owns it. */ @QuarkusTest -@TestProfile(S3PresignedPostAuthEnforcementIntegrationTest.S3AuthProfile.class) +@TestProfile(S3IamEnforcementProfile.class) @TestMethodOrder(MethodOrderer.OrderAnnotation.class) class S3PresignedPostAuthEnforcementIntegrationTest { - public static final class S3AuthProfile implements QuarkusTestProfile { - @Override - public Map getConfigOverrides() { - return Map.of( - "floci.services.s3.enforce-auth", "true", - "floci.services.iam.enforcement-enabled", "true"); - } - } - private static final String BUCKET = "presigned-post-auth-bucket"; private static final String LEGACY_ACCESS_KEY_ID = "test"; private static final String LEGACY_SECRET_KEY = "test"; // Matches the "20260101" date embedded in every credential scope built by this test class. private static final String AMZ_DATE = "20260101T000000Z"; + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + + @TestHTTPResource + URI endpoint; + @Test @Order(1) void createBucket() { @@ -339,9 +345,9 @@ class S3PresignedPostAuthEnforcementIntegrationTest { void rejectsGenuineSignatureWhenIdentityPolicyDeniesPutObject() { String key = "uploads/iam-denied.txt"; String userName = "presigned-post-denied-user"; - String[] credentials = createUserAndAccessKey(userName); - String accessKeyId = credentials[0]; - String secretKey = credentials[1]; + UserCredentials credentials = createUser(cleanup, endpoint.getPort(), IAM_ROOT_AUTH, userName); + String accessKeyId = credentials.accessKeyId(); + String secretKey = credentials.secretAccessKey(); putUserPolicy(userName, "DenyPutObject", """ {"Version":"2012-10-17","Statement":[ {"Effect":"Deny","Action":"s3:PutObject","Resource":"*"} @@ -373,9 +379,9 @@ class S3PresignedPostAuthEnforcementIntegrationTest { String key = "uploads/iam-allowed.txt"; String fileContent = "uploaded by a permitted IAM user"; String userName = "presigned-post-allowed-user"; - String[] credentials = createUserAndAccessKey(userName); - String accessKeyId = credentials[0]; - String secretKey = credentials[1]; + UserCredentials credentials = createUser(cleanup, endpoint.getPort(), IAM_ROOT_AUTH, userName); + String accessKeyId = credentials.accessKeyId(); + String secretKey = credentials.secretAccessKey(); putUserPolicy(userName, "AllowPutObject", """ {"Version":"2012-10-17","Statement":[ {"Effect":"Allow","Action":"s3:PutObject","Resource":"arn:aws:s3:::%1$s/*"} @@ -407,9 +413,9 @@ class S3PresignedPostAuthEnforcementIntegrationTest { String key = "uploads/bucket-policy-only.txt"; String fileContent = "uploaded under a bucket-policy-only grant"; String userName = "presigned-post-bucket-policy-user"; - String[] credentials = createUserAndAccessKey(userName); - String accessKeyId = credentials[0]; - String secretKey = credentials[1]; + UserCredentials credentials = createUser(cleanup, endpoint.getPort(), IAM_ROOT_AUTH, userName); + String accessKeyId = credentials.accessKeyId(); + String secretKey = credentials.secretAccessKey(); createBucket(bucket); putBucketPolicy(bucket, """ @@ -446,9 +452,9 @@ class S3PresignedPostAuthEnforcementIntegrationTest { String bucket = "presigned-post-bucket-policy-deny-bucket"; String key = "uploads/bucket-policy-denied.txt"; String userName = "presigned-post-bucket-policy-denied-user"; - String[] credentials = createUserAndAccessKey(userName); - String accessKeyId = credentials[0]; - String secretKey = credentials[1]; + UserCredentials credentials = createUser(cleanup, endpoint.getPort(), IAM_ROOT_AUTH, userName); + String accessKeyId = credentials.accessKeyId(); + String secretKey = credentials.secretAccessKey(); putUserPolicy(userName, "AllowPutObject", """ {"Version":"2012-10-17","Statement":[ {"Effect":"Allow","Action":"s3:PutObject","Resource":"*"} @@ -497,9 +503,9 @@ class S3PresignedPostAuthEnforcementIntegrationTest { String bucket = "presigned-post-tag-condition-bucket"; String key = "uploads/tag-condition.txt"; String userName = "presigned-post-tag-condition-user"; - String[] credentials = createUserAndAccessKey(userName); - String accessKeyId = credentials[0]; - String secretKey = credentials[1]; + UserCredentials credentials = createUser(cleanup, endpoint.getPort(), IAM_ROOT_AUTH, userName); + String accessKeyId = credentials.accessKeyId(); + String secretKey = credentials.secretAccessKey(); createBucket(bucket); putBucketPolicy(bucket, """ @@ -532,7 +538,18 @@ class S3PresignedPostAuthEnforcementIntegrationTest { .body("Error.Code", equalTo("AccessDenied")); } - private static void createBucket(String bucket) { + /** Removes {@link #BUCKET}, which the tests from {@code createBucket} on share, with the objects they uploaded. */ + @Test + @Order(99) + void cleanup_removeTheSharedBucket() { + removeBucket(endpoint.getPort(), S3RequestSigner.signedAs(LEGACY_ACCESS_KEY_ID, LEGACY_SECRET_KEY), + BUCKET); + } + + /** Creates {@code bucket} as the account root, having first registered its removal. */ + private void createBucket(String bucket) { + cleanup.register(() -> removeBucket(endpoint.getPort(), + S3RequestSigner.signedAs(LEGACY_ACCESS_KEY_ID, LEGACY_SECRET_KEY), bucket)); given() .filter(S3RequestSigner.signedAs(LEGACY_ACCESS_KEY_ID, LEGACY_SECRET_KEY)) .when() @@ -556,33 +573,6 @@ class S3PresignedPostAuthEnforcementIntegrationTest { "AWS4-HMAC-SHA256 Credential=" + LEGACY_ACCESS_KEY_ID + "/20260101/us-east-1/iam/aws4_request" + ", SignedHeaders=host, Signature=abc"; - /** Returns {accessKeyId, secretAccessKey} for a freshly created IAM user. */ - private static String[] createUserAndAccessKey(String userName) { - given() - .formParam("Action", "CreateUser") - .formParam("UserName", userName) - .header("Authorization", IAM_ROOT_AUTH) - .when() - .post("/") - .then() - .statusCode(200); - - io.restassured.response.ExtractableResponse response = given() - .formParam("Action", "CreateAccessKey") - .formParam("UserName", userName) - .header("Authorization", IAM_ROOT_AUTH) - .when() - .post("/") - .then() - .statusCode(200) - .extract(); - - return new String[] { - response.path("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.AccessKeyId"), - response.path("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.SecretAccessKey") - }; - } - private static void putUserPolicy(String userName, String policyName, String policyDocument) { given() .formParam("Action", "PutUserPolicy") 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 d14ed09122..b818fbb3fb 100644 --- a/src/test/java/io/github/hectorvent/floci/testing/EnforcementFixtures.java +++ b/src/test/java/io/github/hectorvent/floci/testing/EnforcementFixtures.java @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.testing; +import io.restassured.filter.Filter; import io.restassured.path.xml.XmlPath; import io.restassured.response.ValidatableResponse; import io.restassured.specification.RequestSpecification; @@ -8,6 +9,7 @@ import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; +import java.util.function.Supplier; import static io.restassured.RestAssured.given; @@ -221,13 +223,29 @@ public final class EnforcementFixtures { * delete object versions, and delete the bucket */ public static void removeBucket(int port, String authorization, String bucket) { - if (s3(port, authorization).when().head("/" + bucket) + removeBucket(() -> s3(port, authorization), bucket); + } + + /** + * {@link #removeBucket(int, String, String)} under a profile that turns on + * {@code floci.services.s3.enforce-auth}, where S3 verifies every header signature and a + * fixed {@code Authorization} header is refused. Each request is signed by {@code signer}. + * + * @param signer a request signer, such as {@code S3RequestSigner}, for an identity allowed to + * list bucket versions, delete object versions, and delete the bucket + */ + public static void removeBucket(int port, Filter signer, String bucket) { + removeBucket(() -> given().port(port).filter(signer), bucket); + } + + private static void removeBucket(Supplier s3, String bucket) { + if (s3.get().when().head("/" + bucket) .then().extract().statusCode() == 404) { return; } List previousPage = List.of(); while (true) { - XmlPath listing = s3(port, authorization).queryParam("versions", "") + XmlPath listing = s3.get().queryParam("versions", "") .queryParam("max-keys", 10) .when().get("/" + bucket) .then().statusCode(200).extract().xmlPath(); @@ -243,14 +261,14 @@ public final class EnforcementFixtures { for (ObjectVersion version : page) { String encodedKey = URLEncoder.encode(version.key(), StandardCharsets.UTF_8) .replace("+", "%20").replace("%2F", "/"); - s3(port, authorization).urlEncodingEnabled(false) + s3.get().urlEncodingEnabled(false) .queryParam("versionId", version.versionId()) .when().delete("/" + bucket + "/" + encodedKey) .then().statusCode(204); } previousPage = page; } - s3(port, authorization).when().delete("/" + bucket).then().statusCode(204); + s3.get().when().delete("/" + bucket).then().statusCode(204); } /** diff --git a/src/test/java/io/github/hectorvent/floci/testing/IamEnforcementProfile.java b/src/test/java/io/github/hectorvent/floci/testing/IamEnforcementProfile.java index bf5c70cb6f..779e43c94a 100644 --- a/src/test/java/io/github/hectorvent/floci/testing/IamEnforcementProfile.java +++ b/src/test/java/io/github/hectorvent/floci/testing/IamEnforcementProfile.java @@ -5,14 +5,34 @@ import io.quarkus.test.junit.QuarkusTestProfile; import java.util.Map; /** - * Turns IAM enforcement on, and nothing else. Quarkus restarts and re-augments the application - * once per distinct profile class, so the classes that need only this override share this one - * rather than each declaring an identical nested profile. + * IAM policy enforcement, with service control policy enforcement next to it, for the classes + * that test what an IAM identity may do. Quarkus builds the application once per distinct + * profile class, so they share this one rather than each declaring their own. + * + *

The overrides, and who relies on them: + *

+ * + *

SCP enforcement is inert for every other class. An SCP only applies to a member account of an + * organization whose root has SCPs enabled, and the management account is exempt, as in AWS. No + * other class creates an organization, so the SCP lookup finds no ceiling for any account they use + * and the decision is the one IAM enforcement alone makes. */ public class IamEnforcementProfile implements QuarkusTestProfile { @Override public Map getConfigOverrides() { - return Map.of("floci.services.iam.enforcement-enabled", "true"); + return Map.of( + "floci.services.iam.enforcement-enabled", "true", + "floci.services.organizations.scp-enforcement-enabled", "true"); } } diff --git a/src/test/java/io/github/hectorvent/floci/testing/S3IamEnforcementProfile.java b/src/test/java/io/github/hectorvent/floci/testing/S3IamEnforcementProfile.java index fb39c07058..51d2060dbf 100644 --- a/src/test/java/io/github/hectorvent/floci/testing/S3IamEnforcementProfile.java +++ b/src/test/java/io/github/hectorvent/floci/testing/S3IamEnforcementProfile.java @@ -6,7 +6,37 @@ import java.util.Map; /** * S3 auth enforcement, IAM enforcement and the global bucket namespace together, for the classes - * that need all three. {@link S3EnforceAuthProfile} covers the narrower case. + * that need all three. {@link S3EnforceAuthProfile} covers the narrower case. Quarkus builds the + * application once per distinct profile class, so they share this one rather than each declaring + * their own. + * + *

The overrides, and who relies on them: + *

+ * + *

Each override is inert for the classes it does not name, which pass with it off. S3 auth + * enforcement only adds refusals, and every refusal those classes assert comes from IAM as well. + * {@code GeneratedPreSignedUrlAuthEnforcementIntegrationTest} and + * {@code S3PresignedPostAuthEnforcementIntegrationTest} reach each bucket only from the account + * that owns it, so the namespace never comes into play for them. */ public class S3IamEnforcementProfile implements QuarkusTestProfile {