diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/CborAccessDeniedEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/CborAccessDeniedEnforcementIntegrationTest.java index bda0b94c01..2c9fec4ba8 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/CborAccessDeniedEnforcementIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/CborAccessDeniedEnforcementIntegrationTest.java @@ -1,13 +1,18 @@ package io.github.hectorvent.floci.services.iam; +import io.github.hectorvent.floci.testing.EnforcementFixtures; 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; import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.TestProfile; import io.restassured.response.ValidatableResponse; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.UUID; @@ -22,6 +27,23 @@ import static org.junit.jupiter.api.Assertions.assertTrue; @TestProfile(IamEnforcementProfile.class) class CborAccessDeniedEnforcementIntegrationTest { + /** + * The teardown runs after Quarkus has reset the port RestAssured points at, so it has to say + * where the application is. The test port is random, which leaves this as the only place to + * read it from. + */ + @TestHTTPResource("/") + static URI baseUri; + + /** + * A QuarkusTest shares one application with every other test class, so the user and key this + * test needs would otherwise outlive it and be visible to anything that lists users unscoped. + * Registered before the create runs, and the registered call reads the key back off the user + * rather than being told it, so a failure between the two still takes both away. + */ + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + @BeforeAll static void configureRestAssured() { RestAssuredJsonUtils.configureAwsContentTypes(); @@ -30,6 +52,8 @@ class CborAccessDeniedEnforcementIntegrationTest { @Test void aCborCallTheCallerIsNotAllowedIsRefusedInCbor() { String userName = "cbor-denied-" + UUID.randomUUID().toString().substring(0, 8); + cleanup.register(() -> EnforcementFixtures.removeUser(baseUri.getPort(), + authorization("test", "iam"), userName)); iam("CreateUser", userName); String accessKeyId = iam("CreateAccessKey", userName) .extract().path("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.AccessKeyId"); diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/ExpiredSessionEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/ExpiredSessionEnforcementIntegrationTest.java index 9613f1c36b..d097d21ff7 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/ExpiredSessionEnforcementIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/ExpiredSessionEnforcementIntegrationTest.java @@ -1,14 +1,19 @@ package io.github.hectorvent.floci.services.iam; +import io.github.hectorvent.floci.testing.EnforcementFixtures; 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; import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.TestProfile; import io.restassured.response.Response; import jakarta.inject.Inject; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import java.net.URI; import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.Locale; @@ -31,6 +36,24 @@ class ExpiredSessionEnforcementIntegrationTest { @Inject IamService iamService; + /** + * The teardown runs after Quarkus has reset the port RestAssured points at, so it has to say + * where the application is. The test port is random, which leaves this as the only place to + * read it from. + */ + @TestHTTPResource("/") + static URI baseUri; + + /** + * A QuarkusTest shares one application with every other test class, so the bucket and the + * sessions here would otherwise outlive it. The sessions go too: what this class pins is that + * a refusal leaves them stored, so nothing takes them out until Floci's own sweep runs. Each + * step is registered before it happens and runs independently of the others, so a bucket that + * will not delete cannot strand a session. + */ + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + @BeforeAll static void configureRestAssured() { RestAssuredJsonUtils.configureAwsContentTypes(); @@ -68,6 +91,8 @@ class ExpiredSessionEnforcementIntegrationTest { // A presigned POST carries its credential in the form body, which only S3 reads. String accessKeyId = expiredSession(); String bucket = "expired-session-post-" + UUID.randomUUID().toString().substring(0, 8); + cleanup.register(() -> EnforcementFixtures.removeBucket(baseUri.getPort(), + authorization("test", "s3"), bucket)); given() .header("Authorization", authorization("test", "s3")) .when() @@ -92,6 +117,7 @@ class ExpiredSessionEnforcementIntegrationTest { private String expiredSession() { String accessKeyId = "ASIAEXPIRED" + UUID.randomUUID().toString().replace("-", "").substring(0, 9).toUpperCase(Locale.ROOT); + cleanup.register(() -> iamService.unregisterSession(ACCOUNT_ID, accessKeyId)); iamService.registerSessionForAccount(ACCOUNT_ID, accessKeyId, "secret", "arn:aws:iam::" + ACCOUNT_ID + ":role/expired-session-role", LONG_AGO, null); return accessKeyId; diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/InactiveAccessKeyEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/InactiveAccessKeyEnforcementIntegrationTest.java index 8db9f9debc..a942d6a879 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/InactiveAccessKeyEnforcementIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/InactiveAccessKeyEnforcementIntegrationTest.java @@ -1,14 +1,19 @@ package io.github.hectorvent.floci.services.iam; +import io.github.hectorvent.floci.testing.EnforcementFixtures; 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; import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.TestProfile; import io.restassured.response.Response; import io.restassured.specification.RequestSpecification; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.UUID; @@ -31,6 +36,24 @@ class InactiveAccessKeyEnforcementIntegrationTest { private static final String PUT_OBJECT = """ {"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:PutObject","Resource":"*"}]}"""; + /** + * The teardown runs after Quarkus has reset the port RestAssured points at, so it has to say + * where the application is. The test port is random, which leaves this as the only place to + * read it from. + */ + @TestHTTPResource("/") + static URI baseUri; + + /** + * A QuarkusTest shares one application with every other test class, so a user, an access key + * or a bucket left here outlives this class and is visible to anything that lists them + * unscoped. Each create registers its removal below, before it runs: the registered call + * reads back what is actually attached, so a setup that fails part way through still has the + * whole fixture taken away, and one that created nothing leaves it with nothing to do. + */ + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + @BeforeAll static void configureRestAssured() { RestAssuredJsonUtils.configureAwsContentTypes(); @@ -90,6 +113,8 @@ class InactiveAccessKeyEnforcementIntegrationTest { String suffix = UUID.randomUUID().toString().substring(0, 8); String bucket = "inactive-key-post-" + suffix; String userName = "inactive-key-post-" + suffix; + cleanup.register(() -> EnforcementFixtures.removeBucket(baseUri.getPort(), + authorization("test", "s3"), bucket)); given() .header("Authorization", authorization("test", "s3")) .when() @@ -111,7 +136,9 @@ class InactiveAccessKeyEnforcementIntegrationTest { } /** Creates a user in {@code account}, with an inline policy when one is given, and returns its access key ID. */ - private static String userWithKey(String account, String userName, String policy) { + private String userWithKey(String account, String userName, String policy) { + cleanup.register(() -> EnforcementFixtures.removeUser(baseUri.getPort(), + authorization(account, "iam"), userName)); query(account, "iam", "CreateUser", "UserName", userName).then().statusCode(200); if (policy != null) { query(account, "iam", "PutUserPolicy", "UserName", userName, "PolicyName", "inline", diff --git a/src/test/java/io/github/hectorvent/floci/testing/EnforcementFixtures.java b/src/test/java/io/github/hectorvent/floci/testing/EnforcementFixtures.java new file mode 100644 index 0000000000..9201612485 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/testing/EnforcementFixtures.java @@ -0,0 +1,102 @@ +package io.github.hectorvent.floci.testing; + +import io.restassured.response.ValidatableResponse; +import io.restassured.specification.RequestSpecification; + +import static io.restassured.RestAssured.given; + +/** + * Takes an IAM user or an S3 bucket that an enforcement test created back out of the shared + * application. A {@code @QuarkusTest} shares one application with every other test class, so a + * fixture left behind outlives the class that made it and is visible to the next class that lists + * users or buckets unscoped. Registered with {@link PartitionCleanup} as each fixture is made. + * + *
Both read back what is attached rather than taking it as an argument, and both return + * quietly when the thing is not there. That is what makes them safe to register before the create + * runs: a setup that fails part way through still has the whole fixture taken away, and one that + * created nothing leaves these with nothing to do. + * + *
The port is passed in because Quarkus points RestAssured at the application only for the + * duration of a test method and resets the port afterwards, so a teardown has to say where the + * application is. The test port is random, which leaves an injected {@code @TestHTTPResource} + * URI as the only place to read it from. + */ +public final class EnforcementFixtures { + + private EnforcementFixtures() { + // Utility class, prevent instantiation + } + + /** + * Deletes an IAM user, and first the inline policies and access keys that IAM wants gone + * before it will. The IAM API reference lists "Access keys (DeleteAccessKey)" and "Inline + * policies (DeleteUserPolicy)" among the items to remove "before attempting to delete a + * user", and Floci answers {@code DeleteConflict} for each of them. + * + * @param authorization an IAM-scoped header. Signing with an account id reaches that + * account's namespace as its root stand-in, which is how a user outside + * the default account is removed. + */ + public static void removeUser(int port, String authorization, String userName) { + if (iam(port, authorization, "GetUser", "UserName", userName).extract().statusCode() == 404) { + return; + } + for (String policyName : iam(port, authorization, "ListUserPolicies", "UserName", userName) + .statusCode(200).extract().xmlPath() + .getList("ListUserPoliciesResponse.ListUserPoliciesResult.PolicyNames.member", + String.class)) { + iam(port, authorization, "DeleteUserPolicy", "UserName", userName, + "PolicyName", policyName).statusCode(200); + } + for (String accessKeyId : iam(port, authorization, "ListAccessKeys", "UserName", userName) + .statusCode(200).extract().xmlPath() + .getList("ListAccessKeysResponse.ListAccessKeysResult.AccessKeyMetadata.member" + + ".AccessKeyId", String.class)) { + iam(port, authorization, "DeleteAccessKey", "UserName", userName, + "AccessKeyId", accessKeyId).statusCode(200); + } + iam(port, authorization, "DeleteUser", "UserName", userName).statusCode(200); + } + + /** + * Deletes an S3 bucket, and first whatever it still holds: "All objects (including all object + * versions and delete markers) in the bucket must be deleted before the bucket itself can be + * deleted" (S3 API reference, {@code DeleteBucket}), which Floci answers + * {@code BucketNotEmpty} for. + * + * @param authorization an S3-scoped header for an identity allowed to read and delete here + */ + public static void removeBucket(int port, String authorization, String bucket) { + if (s3(port, authorization).when().head("/" + bucket) + .then().extract().statusCode() == 404) { + return; + } + for (String key : s3(port, authorization) + .queryParam("list-type", "2") + .when() + .get("/" + bucket) + .then() + .statusCode(200) + .extract().xmlPath().getList("ListBucketResult.Contents.Key", String.class)) { + s3(port, authorization).when().delete("/" + bucket + "/" + key) + .then().statusCode(204); + } + s3(port, authorization).when().delete("/" + bucket).then().statusCode(204); + } + + private static ValidatableResponse iam(int port, String authorization, String action, + String... params) { + RequestSpecification request = given().port(port) + .contentType("application/x-www-form-urlencoded") + .header("Authorization", authorization) + .formParam("Action", action); + for (int i = 0; i < params.length; i += 2) { + request = request.formParam(params[i], params[i + 1]); + } + return request.when().post("/").then(); + } + + private static RequestSpecification s3(int port, String authorization) { + return given().port(port).header("Authorization", authorization); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/testing/PartitionCleanup.java b/src/test/java/io/github/hectorvent/floci/testing/PartitionCleanup.java index cf8e3563e7..98d05887ad 100644 --- a/src/test/java/io/github/hectorvent/floci/testing/PartitionCleanup.java +++ b/src/test/java/io/github/hectorvent/floci/testing/PartitionCleanup.java @@ -7,10 +7,11 @@ import java.util.ArrayDeque; import java.util.Deque; /** - * Deletes what a test created in a non-commercial scope, in reverse order, after each test. - * The {@code @QuarkusTest} emulator is shared by every test class, and a resource left behind - * under {@code cn-north-1} shows up in every later cross-region listing (ResourceExplorer scans - * all resources), so a test that signs a foreign partition registers each create here. + * Deletes what a test created, in reverse order, after each test. The {@code @QuarkusTest} + * emulator is shared by every test class, so a resource left behind outlives the class that made + * it: one under {@code cn-north-1} shows up in every later cross-region listing (ResourceExplorer + * scans all resources), and an IAM user or an S3 bucket shows up in the next class to list either + * one unscoped. A test that leaves such a resource registers each create here. * *
{@code
* @RegisterExtension