Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cloudformation): resolve intrinsics in IAM policy documents before storing them (#3739)

Chris Jovanovski573094720dbd
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java
index a2c6a869e2..14cac70fa8 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java
@@ -1503,6 +1503,12 @@ public class CloudFormationResourceProvisioner {
// ── IAM Policy ────────────────────────────────────────────────────────────
+ private static String resolvePolicyDocument(JsonNode props, CloudFormationTemplateEngine engine) {
+ JsonNode documentNode = props != null ? props.get("PolicyDocument") : null;
+ String resolved = documentNode != null ? engine.resolveJsonAttributeStrict(documentNode) : null;
+ return resolved != null ? resolved : "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
+ }
+
/**
* Provisions {@code AWS::IAM::Policy}, which in AWS is an <em>inline</em> policy embedded in the
* named roles/users/groups (equivalent to PutRolePolicy/PutUserPolicy/PutGroupPolicy) — <em>not</em>
@@ -1525,9 +1531,7 @@ public class CloudFormationResourceProvisioner {
? previousPolicyName
: generatePhysicalName(stackName, r.getLogicalId(), 128, false);
}
- String document = props != null && props.has("PolicyDocument")
- ? props.get("PolicyDocument").toString()
- : "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
+ String document = resolvePolicyDocument(props, engine);
final String name = policyName;
final String doc = document;
@@ -1684,9 +1688,7 @@ public class CloudFormationResourceProvisioner {
if (policyName == null || policyName.isBlank()) {
policyName = generatePhysicalName(stackName, r.getLogicalId(), 128, false);
}
- String document = props != null && props.has("PolicyDocument")
- ? props.get("PolicyDocument").toString()
- : "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
+ String document = resolvePolicyDocument(props, engine);
List<String> roleNames = resolveStringList(props, "Roles", engine);
String existingArn = r.getPhysicalId();
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.java
index a48be90335..fbf118b1e2 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationTemplateEngine.java
@@ -38,6 +38,7 @@ public class CloudFormationTemplateEngine {
private final ObjectMapper objectMapper;
private final Function<String, String> importValueResolver;
private final UnaryOperator<String> dynamicReferenceResolver;
+ private final List<String> unresolvedIntrinsics = new ArrayList<>();
CloudFormationTemplateEngine(String accountId, String region, String stackName, String stackId,
Map<String, String> parameters,
@@ -244,6 +245,19 @@ public class CloudFormationTemplateEngine {
return resolved.isTextual() ? resolved.asText() : resolved.toString();
}
+ public String resolveJsonAttributeStrict(JsonNode node) {
+ int before = unresolvedIntrinsics.size();
+ String resolved = resolveJsonAttribute(node);
+ if (unresolvedIntrinsics.size() > before) {
+ List<String> raised = unresolvedIntrinsics.subList(before, unresolvedIntrinsics.size());
+ throw new AwsException("ValidationError",
+ "Policy document contains unresolved CloudFormation intrinsics: "
+ + String.join(", ", raised)
+ + ". Storing them would silently void the statements that use them.", 400);
+ }
+ return resolved;
+ }
+
private String resolveRef(String name) {
// Pseudo-parameters
return switch (name) {
@@ -262,6 +276,7 @@ public class CloudFormationTemplateEngine {
yield parameters.get(name);
}
LOG.debugv("Unresolved Ref: {0}", name);
+ unresolvedIntrinsics.add("Ref " + name);
yield name;
}
};
@@ -294,8 +309,13 @@ public class CloudFormationTemplateEngine {
String varName = template.substring(i + 2, end);
if (vars.containsKey(varName)) {
result.append(vars.get(varName));
- } else if (varName.contains("!")) {
- // Fn::GetAtt shorthand: ${LogicalId.Attr}
+ } else if (varName.startsWith("!")) {
+ // ${!Literal} is Fn::Sub's escape sequence for a literal ${Literal}: emit it
+ // verbatim with no substitution. It is NOT the Fn::GetAtt shorthand.
+ result.append("${").append(varName, 1, varName.length()).append('}');
+ } else if (!varName.startsWith("AWS::") && varName.contains(".")) {
+ // Fn::GetAtt shorthand: ${LogicalId.Attr}. A logical id and a parameter name
+ // are both alphanumeric, so a dot can only mean an attribute reference.
String[] parts = varName.split("\\.", 2);
result.append(resolveGetAttParts(parts[0], parts.length > 1 ? parts[1] : ""));
} else {
@@ -563,7 +583,8 @@ public class CloudFormationTemplateEngine {
if (attrs != null && attrs.containsKey(attrName)) {
return attrs.get(attrName);
}
- LOG.debugv("Unresolved GetAtt: {0}.{1}", logicalId, attrName);
+ LOG.warnv("Unresolved GetAtt: {0}.{1}", logicalId, attrName);
+ unresolvedIntrinsics.add("Fn::GetAtt " + logicalId + "." + attrName);
return logicalId + "." + attrName;
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamRoleCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamRoleCfnProvisioner.java
index 19a39e4e02..7383dcfeca 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamRoleCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamRoleCfnProvisioner.java
@@ -58,14 +58,11 @@ public class IamRoleCfnProvisioner implements CfnResourceProvisioner {
throw new AwsException("ValidationError",
"Updating RoleName requires resource replacement, which is not supported.", 400);
}
- String assumeDoc = props == null
- ? null
- : ctx.engine().resolveJsonAttribute(props.path("AssumeRolePolicyDocument"));
-
- if (assumeDoc == null) {
- assumeDoc = "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
- }
-
+ JsonNode assumeDocNode = props != null ? props.get("AssumeRolePolicyDocument") : null;
+ String resolvedAssumeDoc = assumeDocNode != null ? ctx.engine().resolveJsonAttributeStrict(assumeDocNode) : null;
+ String assumeDoc = resolvedAssumeDoc != null
+ ? resolvedAssumeDoc
+ : "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
String path = ctx.resolveOptional(props, "Path");
if (path == null) {
path = "/";
@@ -171,7 +168,7 @@ public class IamRoleCfnProvisioner implements CfnResourceProvisioner {
+ " has no PolicyDocument.", 400);
}
iamService.putRolePolicy(resolvedRoleName, policyName,
- ctx.engine().resolveJsonAttribute(document));
+ ctx.engine().resolveJsonAttributeStrict(document));
inlineWrittenByThisAttempt.add(policyName);
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamUserCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamUserCfnProvisioner.java
index 39edba43e6..f1a9b83a55 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamUserCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamUserCfnProvisioner.java
@@ -144,7 +144,7 @@ public class IamUserCfnProvisioner implements CfnResourceProvisioner {
+ " has no PolicyDocument.", 400);
}
iamService.putUserPolicy(resolvedUserName, policyName,
- ctx.engine().resolveJsonAttribute(document));
+ ctx.engine().resolveJsonAttributeStrict(document));
inlineWrittenByThisAttempt.add(policyName);
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationPolicyDocumentIntrinsicsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationPolicyDocumentIntrinsicsIntegrationTest.java
new file mode 100644
index 0000000000..54a3f81f19
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationPolicyDocumentIntrinsicsIntegrationTest.java
@@ -0,0 +1,231 @@
+package io.github.hectorvent.floci.services.cloudformation;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.response.Response;
+import org.junit.jupiter.api.Test;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.containsString;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@QuarkusTest
+class CloudFormationPolicyDocumentIntrinsicsIntegrationTest {
+
+ private static final String CFN_AUTH =
+ "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request";
+ private static final String IAM_AUTH =
+ "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/iam/aws4_request";
+
+ private static String stackWithSubShorthandPolicy(String roleName, String bucketName, String policyName) {
+ return """
+ {
+ "Resources": {
+ "StagingBucket": {
+ "Type": "AWS::S3::Bucket",
+ "Properties": {"BucketName": "%s"}
+ },
+ "Role": {
+ "Type": "AWS::IAM::Role",
+ "Properties": {
+ "RoleName": "%s",
+ "AssumeRolePolicyDocument": {
+ "Version": "2012-10-17",
+ "Statement": [{
+ "Effect": "Allow",
+ "Principal": {"Service": "lambda.amazonaws.com"},
+ "Action": "sts:AssumeRole"
+ }]
+ }
+ }
+ },
+ "DefaultPolicy": {
+ "Type": "AWS::IAM::Policy",
+ "Properties": {
+ "PolicyName": "%s",
+ "PolicyDocument": {
+ "Version": "2012-10-17",
+ "Statement": [{
+ "Sid": {"Fn::Sub": "keep${!ThisLiteral}asis"},
+ "Effect": "Allow",
+ "Action": ["s3:GetObject", "s3:ListBucket"],
+ "Resource": [
+ {"Fn::Sub": "${StagingBucket.Arn}"},
+ {"Fn::Sub": "${StagingBucket.Arn}/*"}
+ ]
+ }]
+ },
+ "Roles": [{"Ref": "Role"}]
+ }
+ }
+ }
+ }
+ """.formatted(bucketName, roleName, policyName);
+ }
+
+ /** Same shape, but the referenced logical id does not exist anywhere in the template. */
+ private static String stackWithUnresolvablePolicyResource(String roleName, String policyName) {
+ return """
+ {
+ "Resources": {
+ "Role": {
+ "Type": "AWS::IAM::Role",
+ "Properties": {
+ "RoleName": "%s",
+ "AssumeRolePolicyDocument": {
+ "Version": "2012-10-17",
+ "Statement": [{
+ "Effect": "Allow",
+ "Principal": {"Service": "lambda.amazonaws.com"},
+ "Action": "sts:AssumeRole"
+ }]
+ }
+ }
+ },
+ "DefaultPolicy": {
+ "Type": "AWS::IAM::Policy",
+ "Properties": {
+ "PolicyName": "%s",
+ "PolicyDocument": {
+ "Version": "2012-10-17",
+ "Statement": [{
+ "Effect": "Allow",
+ "Action": "s3:GetObject",
+ "Resource": {"Fn::GetAtt": ["NoSuchBucket", "Arn"]}
+ }]
+ },
+ "Roles": [{"Ref": "Role"}]
+ }
+ }
+ }
+ }
+ """.formatted(roleName, policyName);
+ }
+
+ /** An inline user policy whose document references a logical id that does not exist. */
+ private static String stackWithUnresolvableUserPolicy(String userName, String policyName) {
+ return """
+ {
+ "Resources": {
+ "User": {
+ "Type": "AWS::IAM::User",
+ "Properties": {
+ "UserName": "%s",
+ "Policies": [{
+ "PolicyName": "%s",
+ "PolicyDocument": {
+ "Version": "2012-10-17",
+ "Statement": [{
+ "Effect": "Allow",
+ "Action": "s3:GetObject",
+ "Resource": {"Fn::GetAtt": ["NoSuchBucket", "Arn"]}
+ }]
+ }
+ }]
+ }
+ }
+ }
+ }
+ """.formatted(userName, policyName);
+ }
+
+ private static void createStack(String stackName, String template) {
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", CFN_AUTH)
+ .formParam("Action", "CreateStack")
+ .formParam("StackName", stackName)
+ .formParam("TemplateBody", template)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ }
+
+ private static Response cfnQuery(String action, String stackName) {
+ return given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", CFN_AUTH)
+ .formParam("Action", action)
+ .formParam("StackName", stackName)
+ .when()
+ .post("/");
+ }
+
+ @Test
+ void subGetAttShorthandInAPolicyDocumentResolvesToTheRealArn() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "cfn-sub-shorthand-" + suffix;
+ String bucketName = "sub-shorthand-bucket-" + suffix;
+ String roleName = "sub-shorthand-role-" + suffix;
+ String policyName = "sub-shorthand-policy-" + suffix;
+
+ createStack(stackName, stackWithSubShorthandPolicy(roleName, bucketName, policyName));
+ cfnQuery("DescribeStacks", stackName).then().statusCode(200)
+ .body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"));
+
+ String storedPolicy = given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", IAM_AUTH)
+ .formParam("Action", "GetRolePolicy")
+ .formParam("RoleName", roleName)
+ .formParam("PolicyName", policyName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract().asString();
+
+ // The stored document names the real bucket, both bucket-level and object-level.
+ assertTrue(storedPolicy.contains("arn:aws:s3:::" + bucketName),
+ "expected the resolved bucket ARN in the stored policy, got: " + storedPolicy);
+ assertTrue(storedPolicy.contains("arn:aws:s3:::" + bucketName + "/*"),
+ "expected the resolved object ARN in the stored policy, got: " + storedPolicy);
+ // ...and not the ARN-shaped literal the broken shorthand produced.
+ assertFalse(storedPolicy.contains("StagingBucket.Arn"),
+ "policy still carries the unresolved shorthand: " + storedPolicy);
+ // ${!Literal} is an escape, not a GetAtt: it survives verbatim, minus the bang.
+ assertTrue(storedPolicy.contains("keep${ThisLiteral}asis"),
+ "expected the Fn::Sub literal escape to survive, got: " + storedPolicy);
+ }
+
+ @Test
+ void unresolvableIntrinsicInAPolicyDocumentFailsTheResourceInsteadOfStoringAName() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "cfn-unresolvable-policy-" + suffix;
+ String roleName = "unresolvable-role-" + suffix;
+ String policyName = "unresolvable-policy-" + suffix;
+
+ createStack(stackName, stackWithUnresolvablePolicyResource(roleName, policyName));
+
+ String status = cfnQuery("DescribeStacks", stackName).then().statusCode(200).extract().asString();
+ assertFalse(status.contains("<StackStatus>CREATE_COMPLETE</StackStatus>"),
+ "an unresolved intrinsic in a policy document must not deploy: " + status);
+
+ String events = cfnQuery("DescribeStackEvents", stackName).then().statusCode(200).extract().asString();
+ assertTrue(events.contains("unresolved CloudFormation intrinsics"),
+ "expected a failure reason naming the unresolved intrinsic, got: " + events);
+ assertTrue(events.contains("NoSuchBucket.Arn"),
+ "expected the failure reason to name the intrinsic that failed, got: " + events);
+ }
+
+ @Test
+ void unresolvableIntrinsicInAnInlineUserPolicyFailsTheResourceInsteadOfStoringAName() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "cfn-unresolvable-user-policy-" + suffix;
+ String userName = "unresolvable-user-" + suffix;
+ String policyName = "unresolvable-user-policy-" + suffix;
+
+ createStack(stackName, stackWithUnresolvableUserPolicy(userName, policyName));
+
+ String status = cfnQuery("DescribeStacks", stackName).then().statusCode(200).extract().asString();
+ assertFalse(status.contains("<StackStatus>CREATE_COMPLETE</StackStatus>"),
+ "an unresolved intrinsic in an inline user policy must not deploy: " + status);
+
+ String events = cfnQuery("DescribeStackEvents", stackName).then().statusCode(200).extract().asString();
+ assertTrue(events.contains("unresolved CloudFormation intrinsics"),
+ "expected a failure reason naming the unresolved intrinsic, got: " + events);
+ assertTrue(events.contains("NoSuchBucket.Arn"),
+ "expected the failure reason to name the intrinsic that failed, got: " + events);
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamRoleCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamRoleCfnProvisionerTest.java
index bca004c66e..d213e4dd6d 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamRoleCfnProvisionerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamRoleCfnProvisionerTest.java
@@ -57,14 +57,19 @@ class IamRoleCfnProvisionerTest {
// tests use it just walks the array and calls resolve(...) per element, stubbed above.
when(engine.resolveStringList(any())).thenCallRealMethod();
- when(engine.resolveJsonAttribute(any())).thenAnswer(inv -> {
- JsonNode node = inv.getArgument(0);
- return node != null && node.isTextual() ? node.asText() : node.toString();
- });
+ when(engine.resolveJsonAttribute(any())).thenAnswer(inv -> resolvedDocument(inv.getArgument(0)));
+ when(engine.resolveJsonAttributeStrict(any())).thenAnswer(inv -> resolvedDocument(inv.getArgument(0)));
return new ProvisionContext(engine, "us-east-1", ACCOUNT_ID, "test-stack");
}
+ private static String resolvedDocument(JsonNode node) {
+ if (node == null) {
+ return null;
+ }
+ return node.isTextual() ? node.asText() : node.toString();
+ }
+
private StackResource resource() {
StackResource r = new StackResource();
r.setLogicalId("AppRole");
@@ -400,7 +405,7 @@ void assumeRolePolicyIntrinsicsAreResolved() {
}
""");
- when(engine.resolveJsonAttribute(any())).thenReturn(
+ when(engine.resolveJsonAttributeStrict(any())).thenReturn(
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Principal\":{\"AWS\":\"arn:aws:iam::" + ACCOUNT_ID + ":root\"},"
+ "\"Action\":\"sts:AssumeRole\"}]}"