fix(test): acquire IAM fixture cleanup after creation (#6812)

This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-30 07:39:36 -04:00
1 parent 1053d60346
commit ad29f37c67
2 files changed
+100 -13

No files matched your search

@@ -25,10 +25,9 @@ import static io.restassured.RestAssured.given;
* made it and is visible to the next class that lists users or buckets unscoped. Registered with
* {@link PartitionCleanup} as each fixture is made.
*
* <p>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.
* <p>Removal reads back what is attached and returns quietly when the resource is absent.
* Register removal only after creation succeeds: a rejected create may refer to a resource
* owned by another fixture. Once creation succeeds, cleanup also covers later setup failures.
*
* <p>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
@@ -102,16 +101,16 @@ public final class EnforcementFixtures {
/**
* Creates an IAM user holding one access key, registering its {@link #removeUser} with
* {@code cleanup} before the create runs, so the user and its key go after the test whatever
* the test does with them.
* {@code cleanup} after user creation succeeds, so a later key-creation failure still removes
* the owned user without deleting a pre-existing user after a rejected create.
*
* @param authorization an IAM-scoped header for the account the user belongs to, as for
* {@link #removeUser}, which is handed the same one
*/
public static UserCredentials createUser(PartitionCleanup cleanup, int port, String authorization,
String userName) {
cleanup.register(() -> removeUser(port, authorization, userName));
iam(port, authorization, "CreateUser", "UserName", userName).statusCode(200);
cleanup.register(() -> removeUser(port, authorization, userName));
XmlPath key = iam(port, authorization, "CreateAccessKey", "UserName", userName)
.statusCode(200).extract().xmlPath();
return new UserCredentials(
@@ -133,8 +132,8 @@ public final class EnforcementFixtures {
/**
* 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
* create runs, and returns the role's ARN.
* {@code policy} inline, registering its {@link #removeRole} with {@code cleanup} after role
* creation succeeds and before attaching the policy, and returns the role's ARN.
*/
public static String createRole(PartitionCleanup cleanup, int port, String account, String path,
String roleName, String policy) {
@@ -151,9 +150,9 @@ public final class EnforcementFixtures {
public static String createRole(PartitionCleanup cleanup, int port, String account, String path,
String roleName, String trustPolicy, String policy) {
String authorization = auth(account, "iam");
cleanup.register(() -> removeRole(port, authorization, roleName));
iam(port, authorization, "CreateRole", "RoleName", roleName, "Path", path,
"AssumeRolePolicyDocument", trustPolicy).statusCode(200);
cleanup.register(() -> removeRole(port, authorization, roleName));
iam(port, authorization, "PutRolePolicy", "RoleName", roleName, "PolicyName", "p",
"PolicyDocument", policy).statusCode(200);
return "arn:aws:iam::" + account + ":role" + path + roleName;
@@ -218,8 +217,8 @@ public final class EnforcementFixtures {
/**
* Deletes a managed policy, and first the versions and attachments IAM wants gone before it
* will. Returns quietly when the policy is already absent, so a caller can register the
* removal before creating it.
* will. Returns quietly when the policy is already absent. Register removal only after
* successfully creating the policy.
*/
public static void removePolicy(int port, String authorization, String policyArn) {
if (iam(port, authorization, "GetPolicy", "PolicyArn", policyArn)
@@ -406,7 +405,7 @@ public final class EnforcementFixtures {
private record ObjectVersion(String key, String versionId) {
}
private static ValidatableResponse iam(int port, String authorization, String action,
static ValidatableResponse iam(int port, String authorization, String action,
String... params) {
return query(port, authorization, action, params);
}
@@ -4,13 +4,17 @@ import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import java.net.URI;
import java.util.UUID;
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
import static io.github.hectorvent.floci.testing.EnforcementFixtures.iam;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
@@ -19,6 +23,90 @@ class EnforcementFixturesIntegrationTest {
@TestHTTPResource("/")
URI baseUri;
@Test
void rejectedUserCreationPreservesExistingUser() {
String name = "fixture-existing-user-" + UUID.randomUUID();
PartitionCleanup attempted = new PartitionCleanup();
iam(baseUri.getPort(), auth("test", "iam"), "CreateUser", "UserName", name).statusCode(200);
try {
assertThrows(AssertionError.class, () -> EnforcementFixtures.createUser(
attempted, baseUri.getPort(), auth("test", "iam"), name));
attempted.afterEach(mock(ExtensionContext.class));
iam(baseUri.getPort(), auth("test", "iam"), "GetUser", "UserName", name).statusCode(200);
} finally {
EnforcementFixtures.removeUser(baseUri.getPort(), auth("test", "iam"), name);
}
}
@Test
void rejectedRoleCreationPreservesExistingRole() {
String name = "fixture-existing-role-" + UUID.randomUUID();
PartitionCleanup attempted = new PartitionCleanup();
PartitionCleanup existing = new PartitionCleanup();
try {
EnforcementFixtures.createRole(existing, baseUri.getPort(), "000000000000", "/", name, allow());
assertThrows(AssertionError.class, () -> EnforcementFixtures.createRole(
attempted, baseUri.getPort(), "000000000000", "/", name, allow()));
attempted.afterEach(mock(ExtensionContext.class));
iam(baseUri.getPort(), auth("test", "iam"), "GetRole", "RoleName", name).statusCode(200);
} finally {
existing.afterEach(mock(ExtensionContext.class));
}
}
@Test
void successfulUserSetupRemovesOwnedUserAndKey() {
String name = "fixture-owned-user-" + UUID.randomUUID();
PartitionCleanup owned = new PartitionCleanup();
try {
EnforcementFixtures.createUser(owned, baseUri.getPort(), auth("test", "iam"), name);
iam(baseUri.getPort(), auth("test", "iam"), "GetUser", "UserName", name).statusCode(200);
owned.afterEach(mock(ExtensionContext.class));
iam(baseUri.getPort(), auth("test", "iam"), "GetUser", "UserName", name).statusCode(404);
} finally {
EnforcementFixtures.removeUser(baseUri.getPort(), auth("test", "iam"), name);
}
}
@Test
void successfulRoleSetupRemovesOwnedRoleAndPolicy() {
String name = "fixture-owned-role-" + UUID.randomUUID();
PartitionCleanup owned = new PartitionCleanup();
try {
EnforcementFixtures.createRole(owned, baseUri.getPort(), "000000000000", "/", name, allow());
iam(baseUri.getPort(), auth("test", "iam"), "GetRole", "RoleName", name).statusCode(200);
iam(baseUri.getPort(), auth("test", "iam"), "GetRolePolicy", "RoleName", name,
"PolicyName", "p").statusCode(200);
owned.afterEach(mock(ExtensionContext.class));
iam(baseUri.getPort(), auth("test", "iam"), "GetRole", "RoleName", name).statusCode(404);
} finally {
EnforcementFixtures.removeRole(baseUri.getPort(), auth("test", "iam"), name);
}
}
@Test
void failedPolicySetupStillRemovesNewRole() {
String name = "fixture-policy-failure-" + UUID.randomUUID();
PartitionCleanup owned = new PartitionCleanup();
String oversizedPolicy = "{\"Statement\":{\"Effect\":\"Allow\",\"Action\":\"s3:*\",\"Resource\":\""
+ "a".repeat(10_241) + "\"}}";
try {
assertThrows(AssertionError.class, () -> EnforcementFixtures.createRole(
owned, baseUri.getPort(), "000000000000", "/", name, oversizedPolicy));
iam(baseUri.getPort(), auth("test", "iam"), "GetRole", "RoleName", name).statusCode(200);
iam(baseUri.getPort(), auth("test", "iam"), "GetRolePolicy", "RoleName", name,
"PolicyName", "p").statusCode(404);
owned.afterEach(mock(ExtensionContext.class));
iam(baseUri.getPort(), auth("test", "iam"), "GetRole", "RoleName", name).statusCode(404);
} finally {
EnforcementFixtures.removeRole(baseUri.getPort(), auth("test", "iam"), name);
}
}
private static String allow() {
return "{\"Statement\":{\"Effect\":\"Allow\",\"Action\":\"s3:*\",\"Resource\":\"*\"}}";
}
@Test
void removeBucketPurgesNullVersionVersionsAndMarkersAcrossPages() {
String bucket = "fixture-purge-" + UUID.randomUUID();