fix(cloudformation): check CloudFormation requests on the resources they name, from the generated request resources (#8426)

CloudFormation was not read by the request-resources generator, so under IAM
enforcement every CloudFormation request was checked on *: a grant scoped to a
stack or a stack set was refused, and a deny scoped to one never applied.
cloudformation now moves into NAMESPACES (100 operations). One rule is fixed:
an account member named after another record's shape names that record's
account, not the type's (StackInstanceAccount is a StackInstance's), so a
stack instance call is checked on its stack set in the caller's account.

A stack or a stack set named by its name is checked on the ARN CloudFormation
minted for it, which CloudFormationStoredResourceArns finds, reading only, or
with its id open when it does not exist yet. A request naming its stack by
no StackName (a change set by its ARN alone, DescribeStackResources by
PhysicalResourceId alone) stays on *, as the generated data gives it.
DescribeStackSet now returns StackSetARN, which botocore gives and Floci
left out, in the Region of the request naming it.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-15 09:59:32 -04:00
1 parent 9c1e5c1163
commit 0f223224ce
11 files changed
+371 -13

No files matched your search

@@ -0,0 +1,134 @@
package io.floci.aws.services.cloudformation;
import io.floci.aws.testing.EnforcementFixtures;
import io.floci.aws.testing.IamEnforcementProfile;
import io.floci.aws.testing.PartitionCleanup;
import io.floci.aws.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 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.util.Map;
import java.util.UUID;
import static io.floci.aws.testing.EnforcementFixtures.auth;
import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.matchesPattern;
import static org.hamcrest.Matchers.not;
/**
* Under IAM enforcement a CloudFormation request is checked on the stack or the stack set it names, at the ARN
* CloudFormation minted for it, whether the request names it by its name or by that ARN; a stack instance is checked
* on its stack set in the caller's account. A grant scoped to one reaches it and no other.
*/
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
class CloudFormationIamIntegrationTest {
private static final String ACCOUNT = "000000000473";
private static final String REGION = "us-east-1";
private static final String TEMPLATE =
"{\"Resources\":{\"Handle\":{\"Type\":\"AWS::CloudFormation::WaitConditionHandle\"}}}";
@TestHTTPResource
URI endpoint;
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void aStackScopedGrantReachesThatStackByNameOrIdAndNoOther() {
String mine = name();
String other = name();
String stackId = stack(mine);
stack(other);
assertThat(stackId, matchesPattern("arn:aws:cloudformation:" + REGION + ":" + ACCOUNT + ":stack/" + mine
+ "/[0-9a-f-]+"));
String user = user(allow(stackId, "cloudformation:DescribeStacks", "cloudformation:GetTemplate"));
call(user, "DescribeStacks", Map.of("StackName", mine)).statusCode(200)
.body(containsString("<StackId>" + stackId + "</StackId>"));
call(user, "DescribeStacks", Map.of("StackName", stackId)).statusCode(200);
call(user, "GetTemplate", Map.of("StackName", mine)).statusCode(200);
denied(call(user, "DescribeStacks", Map.of("StackName", other)), "cloudformation:DescribeStacks");
}
@Test
void aStackSetIsCheckedOnTheArnCloudFormationGivesIt() {
String mine = name();
String other = name();
stackSet(mine);
stackSet(other);
String arn = call(ACCOUNT, "DescribeStackSet", Map.of("StackSetName", mine)).statusCode(200)
.extract().xmlPath().getString("DescribeStackSetResponse.DescribeStackSetResult.StackSet.StackSetARN");
assertThat(arn, matchesPattern("arn:aws:cloudformation:" + REGION + ":" + ACCOUNT + ":stackset/" + mine
+ ":[0-9a-f-]+"));
String user = user(allow(arn, "cloudformation:DescribeStackSet", "cloudformation:DescribeStackInstance"));
call(user, "DescribeStackSet", Map.of("StackSetName", mine)).statusCode(200);
denied(call(user, "DescribeStackSet", Map.of("StackSetName", other)), "cloudformation:DescribeStackSet");
// A stack instance is checked on its stack set, in the caller's account whatever account the instance is in.
call(user, "DescribeStackInstance", Map.of("StackSetName", mine, "StackInstanceAccount", "111122223333",
"StackInstanceRegion", REGION)).statusCode(not(equalTo(403)));
denied(call(user, "DescribeStackInstance", Map.of("StackSetName", other, "StackInstanceAccount",
"111122223333", "StackInstanceRegion", REGION)), "cloudformation:DescribeStackInstance");
}
private String stack(String name) {
String id = call(ACCOUNT, "CreateStack", Map.of("StackName", name, "TemplateBody", TEMPLATE)).statusCode(200)
.extract().xmlPath().getString("CreateStackResponse.CreateStackResult.StackId");
cleanup.register(() -> call(ACCOUNT, "DeleteStack", Map.of("StackName", name)));
return id;
}
private void stackSet(String name) {
call(ACCOUNT, "CreateStackSet", Map.of("StackSetName", name, "TemplateBody", TEMPLATE)).statusCode(200);
cleanup.register(() -> call(ACCOUNT, "DeleteStackSet", Map.of("StackSetName", name)));
}
private static String name() {
return "iam-" + UUID.randomUUID().toString().substring(0, 8);
}
private static String allow(String resource, String... actions) {
return "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":[\""
+ String.join("\",\"", actions) + "\"],\"Resource\":\"" + resource + "\"}]}";
}
private static void denied(ValidatableResponse response, String action) {
response.statusCode(403).body(containsString("AccessDenied")).body(containsString(action));
}
private String user(String policy) {
String name = "cfn-" + UUID.randomUUID().toString().substring(0, 8);
String authorization = auth(ACCOUNT, "iam");
String accessKeyId = EnforcementFixtures.createUser(cleanup, endpoint.getPort(), authorization, name)
.accessKeyId();
EnforcementFixtures.putUserPolicy(endpoint.getPort(), authorization, name, "p", policy);
return accessKeyId;
}
private ValidatableResponse call(String accessKeyId, String action, Map<String, String> params) {
RequestSpecification request = given().port(endpoint.getPort())
.header("Authorization", auth(accessKeyId, "cloudformation"))
.contentType("application/x-www-form-urlencoded")
.formParam("Action", action)
.formParam("Version", "2010-05-15");
params.forEach(request::formParam);
return request.when().post("/").then();
}
}
@@ -0,0 +1,30 @@
package io.floci.aws.services.cloudformation;
import io.floci.aws.services.cloudformation.model.StackSet;
import io.floci.aws.testing.InMemoryStorageFactory;
import org.junit.jupiter.api.Test;
import java.util.Map;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
class StackSetArnTest {
/**
* A stack set's ARN is in the Region of the request naming it, given to the service, whatever context runs it: a
* Step Functions task calls the handler with no HTTP request, in its own Region, which is not the deployment's.
*/
@Test
void aStackSetsArnIsInTheRegionOfTheRequestNamingIt() {
StackSetService service = new StackSetService(mock(CloudFormationService.class),
new InMemoryStorageFactory("000000000000"));
StackSet set = service.createStackSet("regional", "{\"Resources\":{}}", null, null, Map.of(), null);
String arn = "arn:aws:cloudformation:eu-west-1:000000000000:stackset/" + set.getStackSetId();
assertEquals(arn, service.stackSetArn(set, "eu-west-1"));
assertEquals(Optional.of(arn), service.stackSetArn("regional", "eu-west-1"));
assertEquals(Optional.empty(), service.stackSetArn("missing", "eu-west-1"));
}
}