From 4793f1aa775147c58596a275c5766e9c478cdf7d Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Fri, 9 Oct 2026 21:16:21 -0400 Subject: [PATCH] test(iam): leave no role or instance profile behind in the service-linked role suite (#5322) Most tests in IamServiceLinkedRoleIntegrationTest create a role as a fixture and have no reason to remove it, so the class finished with a dozen roles still under /aws-service-role/, plus the instance profile the protection test needs. A QuarkusTest shares one application across classes, so those outlived the class and were visible to anything listing roles or instance profiles unscoped. Nothing fails on it today, because every unscoped ListRoles in the tree picks its own role out of the result by name instead of asserting over the whole list, which makes this a hazard waiting for the first test that does not. The cleanup is an @AfterAll. Quarkus points RestAssured at the application only for the duration of a test method and resets the port afterwards, so a teardown going through RestAssured has to say where the application is; the test port is random, which leaves the injected test URI as the only place to read it from. With the port supplied, the application answers normally there, and a failure in @AfterAll is reported by surefire against the class, so a cleanup that stops working does not pass quietly. It creates a role of its own before sweeping and asserts the listing sees it, because nothing else in this class pins ListRoles by PathPrefix. Without that, a listing that stopped returning anything for the path would leave the sweep with nothing to delete and the run would still finish clean. Deleting the instance profile expects 200 for the same reason: this action answers NoSuchEntity once the profile is gone, so the status is the evidence that there was something to clean. Once at the end rather than after each test, on purpose. Orders 1 to 8 are one narrative where a role created by one test is the next one's fixture and the delete in order 6 is itself under test, so cleaning between tests would take it apart. Which roles are this class's own is decided against a snapshot of the path taken before its first test, rather than by a list of the principals it uses or by sweeping the path whole. A list goes stale the first time a test is added. The whole path is not this class's to clear: other classes create service-linked roles in the same shared application, and a class running later can still be holding one. A class placed after this one in the same run saw /aws-service-role/ come back empty with the sweep unscoped, and sees ElastiCache's role, created by an earlier class, still there with the snapshot in place. The profile goes by name, since other classes keep their own profiles at the same path. The snapshot is taken on the first test rather than in @BeforeAll, where the injected URI is still null and nothing can reach the application yet. If no test got that far, the teardown returns without sweeping: it created nothing and has no record of what was already there, and failing there would bury whatever stopped the snapshot. The profile delete is guarded by a flag the creating test sets, because a focused run of a single method fires the teardown too and would otherwise demand a profile nothing had made. The probe role the teardown creates is removed in a finally, so an assertion that stops the sweep cannot leave it behind; nothing is asserted there, since a throw would bury whatever failed first. Two kinds of role live under that path and the cleanup has to know both. Most are service-linked and only DeleteServiceLinkedRole will take them, while the protection tests park ordinary roles there with CreateRole, and that action answers NoSuchEntity for those because the mark comes from the minting action rather than from the path. A 404 while deleting is what surfaced the difference, and only a 404 falls through to DeleteRole: any other status leaves the role for the closing assertion to name rather than hiding behind a second attempt. One fixture sits outside that path. The test that puts DeleteServiceLinkedRole in front of an ordinary role parks it at the account root, where a listing by PathPrefix never reaches it, so that test removes it itself with DeleteRole. Expecting 200 there is also the assertion that the rejected delete left the role alone, which is what the sibling test at a service-linked path reaches for GetRole to say. Also renumbers a duplicate @Order(24), which left two tests with an unspecified order relative to each other in a class that is ordered on purpose. Refs #5293 --- .../IamServiceLinkedRoleIntegrationTest.java | 179 +++++++++++++++++- 1 file changed, 175 insertions(+), 4 deletions(-) diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamServiceLinkedRoleIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamServiceLinkedRoleIntegrationTest.java index db806f2c75..0076f861f5 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/IamServiceLinkedRoleIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamServiceLinkedRoleIntegrationTest.java @@ -1,12 +1,17 @@ package io.github.hectorvent.floci.services.iam; +import io.quarkus.test.common.http.TestHTTPResource; import io.quarkus.test.junit.QuarkusTest; import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeEach; 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 java.net.URI; +import java.util.ArrayList; import java.util.List; import static io.restassured.RestAssured.given; @@ -14,6 +19,7 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.matchesRegex; import static org.hamcrest.Matchers.startsWith; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Service-linked roles exist so Terraform's aws_iam_service_linked_role can apply and destroy @@ -29,11 +35,47 @@ class IamServiceLinkedRoleIntegrationTest { private static final String SERVICE = "es.amazonaws.com"; + private static final String SERVICE_LINKED_ROLE_PATH = "/aws-service-role/"; + + /** Created by the protection test and the only entity here that is not a role. */ + private static final String PROTECTION_PROBE_PROFILE = "protectprobe-profile"; + private static final String OTHER_ACCOUNT_AUTH_HEADER = "AWS4-HMAC-SHA256 Credential=222222222222/20260227/us-east-1/iam/aws4_request"; private static String deletionTaskId; + /** + * Set by the test that creates the profile. A focused run of one method still fires the + * teardown, so without this it would ask for a profile nothing had made. + */ + private static boolean protectionProfileCreated; + + /** + * The roles already under {@code /aws-service-role/} when this class started. A QuarkusTest + * shares one application with every other test class, so these belong to someone else and + * the teardown has to leave them where they are. Taken on the first test rather than in + * {@code @BeforeAll}, where the injected URI is still null and nothing can reach the + * application yet. + */ + private static List rolesAlreadyThere; + + /** + * Quarkus points RestAssured at the application for the duration of a test method and + * resets the port afterwards, so teardown has to say where the application is. The test + * port is random ({@code quarkus.http.test-port: 0}), which leaves the injected URI as + * the only place to read it from. + */ + @TestHTTPResource("/") + static URI baseUri; + + @BeforeEach + void notePreExistingServiceRoles() { + if (rolesAlreadyThere == null) { + rolesAlreadyThere = rolesUnderTheServiceRolePath(); + } + } + @Test @Order(1) void createServiceLinkedRolePlacesItUnderTheServiceRolePath() { @@ -391,6 +433,19 @@ class IamServiceLinkedRoleIntegrationTest { .then() .statusCode(404) .body("ErrorResponse.Error.Code", equalTo("NoSuchEntity")); + + // The role is parked at the account root, so the teardown's sweep of the service-role + // path never sees it and it has to go from here. Expecting 200 is also the assertion + // that the rejected call above left it alone, the way the sibling test reaches for + // GetRole: this action answers NoSuchEntity for a role that is already gone. + given() + .formParam("Action", "DeleteRole") + .formParam("RoleName", "NotServiceLinked") + .header("Authorization", AUTH_HEADER) + .when() + .post("/") + .then() + .statusCode(200); } private static void createServiceLinkedRole(String principal) { @@ -520,17 +575,18 @@ class IamServiceLinkedRoleIntegrationTest { given() .formParam("Action", "CreateInstanceProfile") - .formParam("InstanceProfileName", "protectprobe-profile") + .formParam("InstanceProfileName", PROTECTION_PROBE_PROFILE) .header("Authorization", AUTH_HEADER) .when() .post("/") .then() .statusCode(200); + protectionProfileCreated = true; refusedAsUnmodifiable("AddRoleToInstanceProfile", - "InstanceProfileName", "protectprobe-profile", "RoleName", roleName); + "InstanceProfileName", PROTECTION_PROBE_PROFILE, "RoleName", roleName); refusedAsUnmodifiable("RemoveRoleFromInstanceProfile", - "InstanceProfileName", "protectprobe-profile", "RoleName", roleName); + "InstanceProfileName", PROTECTION_PROBE_PROFILE, "RoleName", roleName); // The trust policy is the one an attacker would rewrite, so pin that it survived intact. given() @@ -593,7 +649,7 @@ class IamServiceLinkedRoleIntegrationTest { } @Test - @Order(24) + @Order(31) void cloud9UsesTheAwsCanonicalRoleName() { given() .formParam("Action", "CreateServiceLinkedRole") @@ -741,4 +797,119 @@ class IamServiceLinkedRoleIntegrationTest { .body("CreateServiceLinkedRoleResponse.CreateServiceLinkedRoleResult.Role.RoleName", equalTo("AWSServiceRoleForApplicationAutoScaling_SageMakerEndpoint_sixchr")); } + + /** The class spells its requests out in full; the teardown needs one line, so it gets this. */ + private static RequestSpecification iam(String action) { + return given().port(baseUri.getPort()) + .header("Authorization", AUTH_HEADER) + .formParam("Action", action); + } + + private static List rolesUnderTheServiceRolePath() { + return iam("ListRoles") + .formParam("PathPrefix", SERVICE_LINKED_ROLE_PATH) + .when() + .post("/") + .then() + .statusCode(200) + .extract().xmlPath() + .getList("ListRolesResponse.ListRolesResult.Roles.member.RoleName", String.class); + } + + /** What is under the path now, minus what was already there when this class started. */ + private static List rolesThisClassAdded() { + List added = new ArrayList<>(rolesUnderTheServiceRolePath()); + added.removeAll(rolesAlreadyThere); + return added; + } + + /** + * Deletes everything this class creates: the roles it left under {@code /aws-service-role/} + * and the instance profile the protection test needs. The tests here make those as fixtures + * and mostly have no reason to remove them, and a QuarkusTest shares one application across + * classes, so without this the leftovers outlive the class and are visible to anything that + * lists roles or instance profiles unscoped. + * + *

Once at the end rather than after each test, on purpose: orders 1 to 8 are a single + * narrative where one test's role is the next one's fixture, and the delete in order 6 is + * itself under test. Cleaning between tests would take that apart. + * + *

Which roles are this class's own is decided by comparing the path against the snapshot + * taken before the first test, not by a list of the principals used here: a list goes stale + * the moment a test is added, and sweeping the whole path takes roles that other classes + * made and a later class can still be holding. The profile goes by name, since other classes + * keep their own profiles at the same path. + */ + @AfterAll + static void cleanup() { + if (rolesAlreadyThere == null) { + // No test of this class got as far as the snapshot, so it created nothing and there + // is no record of what was already here. Sweeping without that record would take + // other classes' roles, and failing here would bury whatever stopped the snapshot. + return; + } + + // A role created here and then found in the listing is what keeps the closing assertion + // honest. If the listing ever stopped returning anything for this path, the sweep below + // would delete nothing and the empty result would still read as clean. + String sentinel = iam("CreateServiceLinkedRole") + .formParam("AWSServiceName", "cleanupprobe.amazonaws.com") + .when() + .post("/") + .then() + .statusCode(200) + .extract().xmlPath() + .getString("CreateServiceLinkedRoleResponse.CreateServiceLinkedRoleResult.Role.RoleName"); + + try { + List leftOver = rolesThisClassAdded(); + assertTrue(leftOver.contains(sentinel), + "the path listing should see the role just created, found: " + leftOver); + + for (String roleName : leftOver) { + // Two kinds live under this path. Most are service-linked and only + // DeleteServiceLinkedRole will take them. A couple are ordinary roles the protection + // tests park there with CreateRole, and that action answers NoSuchEntity for those, + // because the mark comes from the action that minted the role, not from its path. + // Any other status leaves the role in place for the closing assertion to name. + int status = iam("DeleteServiceLinkedRole") + .formParam("RoleName", roleName) + .when() + .post("/") + .then() + .extract().statusCode(); + if (status == 404) { + iam("DeleteRole") + .formParam("RoleName", roleName) + .when() + .post("/") + .then() + .statusCode(200); + } + } + + List remaining = rolesThisClassAdded(); + assertTrue(remaining.isEmpty(), + "the class should leave no service-linked role behind, found: " + remaining); + + if (protectionProfileCreated) { + // Expecting 200 is also the assertion that the profile was there to clean: this + // action answers NoSuchEntity for one that is already gone. + iam("DeleteInstanceProfile") + .formParam("InstanceProfileName", PROTECTION_PROBE_PROFILE) + .when() + .post("/") + .then() + .statusCode(200); + } + } finally { + // The sweep above normally takes the probe with it, so a 404 here is the ordinary + // outcome. Nothing is asserted, both because there is nothing left to learn and + // because a throw here would bury whatever failed above. + iam("DeleteServiceLinkedRole") + .formParam("RoleName", sentinel) + .when() + .post("/"); + } + } }