mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 14:30:28 -05:00
fix(cloudformation): resolve intrinsics in IAM policy documents before storing them (#3739)
Fail stacks when IAM policy document references cannot be resolved.
This commit is contained in:
1 parent
ad20699a6b
commit
573094720d
6 files changed
+280
-24
No files matched your search
+8
-6
@@ -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();
|
||||
|
||||
|
||||
+24
-3
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+6
-9
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user