From 99bc6c977dac30edbf0b4d8b27f43fc1f8fd4a07 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 13 Sep 2026 00:40:37 +0100 Subject: [PATCH] test(cloudformation): pin account scoping of ListStacks and DescribeStackResources (#3475) #2248 reports that ListStacks and DescribeStackResources ignored the caller's account, so assumed-role credentials for a member account got the management account's stacks from two of the three read APIs while DescribeStacks scoped correctly. #3185 fixed that by routing every stack read through the request-scoped account, but did not reference the issue and did not test either operation: its test class never calls ListStacks and only calls DescribeStackResources from the owning account. Removing the account filter from either service method leaves every existing test green. Add the two missing regression tests to CloudFormationAccountOwnershipIntegrationTest. The first checks ListStacks under two 12-digit access keys. The second follows the issue's scenario: the management account creates a stack, assumes a role in the member account, creates a stack with the temporary key, and both ListStacks and DescribeStackResources must scope to the caller in each direction, with the foreign stack reported as ValidationError "does not exist" as AWS does. Both tests fail against the pre-#3185 shape of either method and pass on main. Closes #2248 --- ...mationAccountOwnershipIntegrationTest.java | 72 ++++++++++++++++++- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationAccountOwnershipIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationAccountOwnershipIntegrationTest.java index 1b8dacc700..ac6c66c3e5 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationAccountOwnershipIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationAccountOwnershipIntegrationTest.java @@ -8,6 +8,7 @@ import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.startsWith; @QuarkusTest class CloudFormationAccountOwnershipIntegrationTest { @@ -89,6 +90,60 @@ class CloudFormationAccountOwnershipIntegrationTest { .body(not(containsString("Changed"))); } + @Test + void listStacksIsScopedToCallerAccount() { + createStack(ACCOUNT_1, US_EAST_1, "cfn-list-scope-account-1"); + createStack(ACCOUNT_2, US_EAST_1, "cfn-list-scope-account-2"); + + listStacks(ACCOUNT_1, US_EAST_1) + .statusCode(200) + .body(containsString("cfn-list-scope-account-1")) + .body(not(containsString("cfn-list-scope-account-2"))); + listStacks(ACCOUNT_2, US_EAST_1) + .statusCode(200) + .body(containsString("cfn-list-scope-account-2")) + .body(not(containsString("cfn-list-scope-account-1"))); + } + + @Test + void assumedRoleCredentialsScopeListStacksAndDescribeStackResources() { + createStack(ACCOUNT_1, US_EAST_1, "cfn-management-stack", + "{\"Resources\":{\"ManagementBucket\":{\"Type\":\"AWS::S3::Bucket\"}}}"); + + String memberAccessKeyId = given() + .header("Authorization", auth(ACCOUNT_1, US_EAST_1, "sts")) + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "AssumeRole") + .formParam("RoleArn", "arn:aws:iam::" + ACCOUNT_2 + ":role/cfn-member-deploy-role") + .formParam("RoleSessionName", "cfn-member-session") + .when().post("/").then() + .statusCode(200) + .body("AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId", startsWith("ASIA")) + .extract().path("AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId"); + + createStack(memberAccessKeyId, US_EAST_1, "cfn-member-stack", + "{\"Resources\":{\"MemberBucket\":{\"Type\":\"AWS::S3::Bucket\"}}}"); + + listStacks(memberAccessKeyId, US_EAST_1) + .statusCode(200) + .body(containsString("cfn-member-stack")) + .body(not(containsString("cfn-management-stack"))); + listStacks(ACCOUNT_1, US_EAST_1) + .statusCode(200) + .body(containsString("cfn-management-stack")) + .body(not(containsString("cfn-member-stack"))); + + describeStackResources(memberAccessKeyId, US_EAST_1, "cfn-member-stack") + .statusCode(200) + .body(containsString("MemberBucket")); + describeStackResources(memberAccessKeyId, US_EAST_1, "cfn-management-stack") + .statusCode(400) + .body(containsString("does not exist")); + describeStackResources(ACCOUNT_1, US_EAST_1, "cfn-member-stack") + .statusCode(400) + .body(containsString("does not exist")); + } + private static String createStack(String account, String region, String name) { return createStack(account, region, name, "{\"Resources\":{}}"); } @@ -132,6 +187,13 @@ class CloudFormationAccountOwnershipIntegrationTest { .when().post("/").then(); } + private static io.restassured.response.ValidatableResponse listStacks(String accessKeyId, String region) { + return given().header("Authorization", auth(accessKeyId, region)) + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "ListStacks") + .when().post("/").then(); + } + private static io.restassured.response.ValidatableResponse listExports(String account, String region) { return given().header("Authorization", auth(account, region)) .contentType("application/x-www-form-urlencoded") @@ -139,9 +201,13 @@ class CloudFormationAccountOwnershipIntegrationTest { .when().post("/").then(); } - private static String auth(String account, String region) { - return "AWS4-HMAC-SHA256 Credential=" + account - + "/20260907/" + region + "/cloudformation/aws4_request," + private static String auth(String accessKeyId, String region) { + return auth(accessKeyId, region, "cloudformation"); + } + + private static String auth(String accessKeyId, String region, String service) { + return "AWS4-HMAC-SHA256 Credential=" + accessKeyId + + "/20260907/" + region + "/" + service + "/aws4_request," + " SignedHeaders=host, Signature=abc"; } }