mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): resolve sequence-form GetAtt references (#8080)
This commit is contained in:
1 parent
f55f9f229b
commit
6ace00acc7
6 files changed
+398
-57
No files matched your search
@@ -1189,6 +1189,15 @@ public class CloudFormationService implements ResourceProvider {
|
||||
if (!replacedResourceIds.isEmpty() || !inPlaceChangedAttributes.isEmpty()) {
|
||||
Set<String> allResourceIds = new LinkedHashSet<>();
|
||||
newResources.fieldNames().forEachRemaining(allResourceIds::add);
|
||||
Map<String, String> physicalIds = new HashMap<>();
|
||||
for (StackResource resource : stack.resourcesSnapshot().values()) {
|
||||
if (resource.getPhysicalId() != null) {
|
||||
physicalIds.put(resource.getLogicalId(), resource.getPhysicalId());
|
||||
}
|
||||
}
|
||||
CloudFormationTemplateEngine dependencyEngine = new CloudFormationTemplateEngine(
|
||||
ownerAccount(stack), region, stack.getStackName(), stack.getStackId(),
|
||||
newResolvedParams, physicalIds, Map.of(), newConditions, Map.of(), objectMapper, null);
|
||||
|
||||
List<String> sortedLogicalIds = topologicalSort(newResources, newConditions);
|
||||
boolean anyNewChange = true;
|
||||
@@ -1209,7 +1218,8 @@ public class CloudFormationService implements ResourceProvider {
|
||||
boolean referencesChangedAttr = false;
|
||||
if (!dependsOnReplaced) {
|
||||
for (Map.Entry<String, Set<String>> entry : inPlaceChangedAttributes.entrySet()) {
|
||||
if (referencesAnyAttribute(newDef.path("Properties"), entry.getKey(), entry.getValue(), newConditions)) {
|
||||
if (referencesAnyAttribute(newDef.path("Properties"), entry.getKey(), entry.getValue(),
|
||||
newConditions, dependencyEngine)) {
|
||||
referencesChangedAttr = true;
|
||||
break;
|
||||
}
|
||||
@@ -2539,7 +2549,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
for (String candidateId : sortedLogicalIds) {
|
||||
if (!changedResourceIds.contains(candidateId)) {
|
||||
if (referencesAnyAttribute(resources.path(candidateId).path("Properties"),
|
||||
logicalId, changedAttrNames, conditions)) {
|
||||
logicalId, changedAttrNames, conditions, engine)) {
|
||||
changedResourceIds.add(candidateId);
|
||||
}
|
||||
}
|
||||
@@ -5612,61 +5622,41 @@ public class CloudFormationService implements ResourceProvider {
|
||||
* is updated in-place with modified attributes (e.g. LatestVersionNumber of LaunchTemplate).
|
||||
*/
|
||||
private boolean referencesAnyAttribute(JsonNode node, String targetLogicalId, Set<String> targetAttrNames,
|
||||
Map<String, Boolean> conditions) {
|
||||
Map<String, Boolean> conditions, CloudFormationTemplateEngine engine) {
|
||||
if (node == null || node.isNull() || node.isMissingNode()) {
|
||||
return false;
|
||||
}
|
||||
if (node.isObject()) {
|
||||
if (node.has("Fn::GetAtt")) {
|
||||
JsonNode getAtt = node.get("Fn::GetAtt");
|
||||
String logicalId = null;
|
||||
if (getAtt.isArray() && getAtt.size() >= 2) {
|
||||
logicalId = getAtt.get(0).asText();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 1; i < getAtt.size(); i++) {
|
||||
if (i > 1) {
|
||||
sb.append('.');
|
||||
}
|
||||
sb.append(getAtt.get(i).asText());
|
||||
}
|
||||
if (targetLogicalId.equals(logicalId)
|
||||
&& (targetAttrNames.contains(sb.toString()) || targetAttrNames.contains(getAtt.get(1).asText()))) {
|
||||
return true;
|
||||
}
|
||||
} else if (getAtt.isTextual()) {
|
||||
String[] parts = getAtt.textValue().split("\\.", 2);
|
||||
logicalId = parts[0];
|
||||
String attrName = parts.length > 1 ? parts[1] : null;
|
||||
if (targetLogicalId.equals(logicalId) && attrName != null && targetAttrNames.contains(attrName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (node.has("Fn::GetAtt")
|
||||
&& engine.referencesGetAttAttribute(node.get("Fn::GetAtt"), targetLogicalId, targetAttrNames)) {
|
||||
return true;
|
||||
}
|
||||
if (node.has("Fn::If")) {
|
||||
JsonNode fnIf = node.get("Fn::If");
|
||||
if (fnIf.isArray() && fnIf.size() == 3) {
|
||||
if (conditions != null && conditions.containsKey(fnIf.get(0).asText())) {
|
||||
boolean condition = conditions.get(fnIf.get(0).asText());
|
||||
return referencesAnyAttribute(fnIf.get(condition ? 1 : 2), targetLogicalId, targetAttrNames, conditions);
|
||||
return referencesAnyAttribute(fnIf.get(condition ? 1 : 2), targetLogicalId, targetAttrNames,
|
||||
conditions, engine);
|
||||
}
|
||||
return referencesAnyAttribute(fnIf.get(1), targetLogicalId, targetAttrNames, conditions)
|
||||
|| referencesAnyAttribute(fnIf.get(2), targetLogicalId, targetAttrNames, conditions);
|
||||
return referencesAnyAttribute(fnIf.get(1), targetLogicalId, targetAttrNames, conditions, engine)
|
||||
|| referencesAnyAttribute(fnIf.get(2), targetLogicalId, targetAttrNames, conditions, engine);
|
||||
}
|
||||
}
|
||||
if (node.has("Fn::Sub")) {
|
||||
if (subReferencesAttribute(node.get("Fn::Sub"), targetLogicalId, targetAttrNames, conditions)) {
|
||||
if (subReferencesAttribute(node.get("Fn::Sub"), targetLogicalId, targetAttrNames, conditions, engine)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for (Iterator<JsonNode> it = node.elements(); it.hasNext(); ) {
|
||||
if (referencesAnyAttribute(it.next(), targetLogicalId, targetAttrNames, conditions)) {
|
||||
if (referencesAnyAttribute(it.next(), targetLogicalId, targetAttrNames, conditions, engine)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else if (node.isArray()) {
|
||||
for (JsonNode item : node) {
|
||||
if (referencesAnyAttribute(item, targetLogicalId, targetAttrNames, conditions)) {
|
||||
if (referencesAnyAttribute(item, targetLogicalId, targetAttrNames, conditions, engine)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -5675,7 +5665,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
}
|
||||
|
||||
private boolean subReferencesAttribute(JsonNode sub, String targetLogicalId, Set<String> targetAttrNames,
|
||||
Map<String, Boolean> conditions) {
|
||||
Map<String, Boolean> conditions, CloudFormationTemplateEngine engine) {
|
||||
String template;
|
||||
Set<String> explicitVars = new HashSet<>();
|
||||
|
||||
@@ -5685,7 +5675,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
template = sub.get(0).asText();
|
||||
if (sub.size() >= 2 && sub.get(1).isObject()) {
|
||||
sub.get(1).fieldNames().forEachRemaining(explicitVars::add);
|
||||
if (referencesAnyAttribute(sub.get(1), targetLogicalId, targetAttrNames, conditions)) {
|
||||
if (referencesAnyAttribute(sub.get(1), targetLogicalId, targetAttrNames, conditions, engine)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
+28
-7
@@ -18,6 +18,7 @@ import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.UnaryOperator;
|
||||
@@ -933,15 +934,35 @@ public class CloudFormationTemplateEngine {
|
||||
return parts == null ? TextNode.valueOf("") : resolveGetAttNodeParts(parts[0], parts[1]);
|
||||
}
|
||||
|
||||
private static String[] getAttParts(JsonNode getAtt) {
|
||||
if (getAtt.isArray() && getAtt.size() == 2) {
|
||||
return new String[] {getAtt.get(0).asText(), getAtt.get(1).asText()};
|
||||
/** Matches a GetAtt dependency using the same operands as attribute value resolution. */
|
||||
public boolean referencesGetAttAttribute(JsonNode getAtt, String logicalId, Set<String> attributeNames) {
|
||||
String[] parts = getAttParts(getAtt);
|
||||
return parts != null && logicalId.equals(parts[0]) && attributeNames.contains(parts[1]);
|
||||
}
|
||||
|
||||
private String[] getAttParts(JsonNode getAtt) {
|
||||
String errorMessage = "Template error: every Fn::GetAtt object requires two non-empty parameters, "
|
||||
+ "the resource name and the resource attribute";
|
||||
String[] parts;
|
||||
if (getAtt.isArray()) {
|
||||
if (getAtt.size() != 2) {
|
||||
throw new AwsException("ValidationError", errorMessage, 400);
|
||||
}
|
||||
JsonNode attribute = getAtt.get(1);
|
||||
String attributeName = attribute.has("Ref")
|
||||
? resolveRef(attribute.get("Ref").asText()) : attribute.asText();
|
||||
parts = new String[] {getAtt.get(0).asText(), attributeName};
|
||||
} else if (getAtt.isTextual()) {
|
||||
errorMessage = "Template error: if specifying one argument to Fn::GetAtt, "
|
||||
+ "that argument must be a non-empty string in format <LogicalId>.<Attribute>";
|
||||
parts = getAtt.textValue().split("\\.", 2);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
if (getAtt.isTextual()) {
|
||||
String[] parts = getAtt.textValue().split("\\.", 2);
|
||||
return new String[] {parts[0], parts.length > 1 ? parts[1] : ""};
|
||||
if (parts.length != 2 || parts[0].isEmpty() || parts[1].isEmpty()) {
|
||||
throw new AwsException("ValidationError", errorMessage, 400);
|
||||
}
|
||||
return null;
|
||||
return parts;
|
||||
}
|
||||
|
||||
private Map<String, String> findAttributeSource(String logicalId, String attrName) {
|
||||
|
||||
@@ -12,9 +12,7 @@ import org.yaml.snakeyaml.nodes.ScalarNode;
|
||||
import org.yaml.snakeyaml.nodes.SequenceNode;
|
||||
import org.yaml.snakeyaml.nodes.Tag;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -99,19 +97,10 @@ public class CloudFormationYamlParser {
|
||||
}
|
||||
|
||||
private Object fnGetAtt(Node n) {
|
||||
String raw = scalar(n);
|
||||
// !GetAtt Resource.Attribute
|
||||
int dot = raw.indexOf('.');
|
||||
List<String> parts = new ArrayList<>();
|
||||
if (dot > 0) {
|
||||
parts.add(raw.substring(0, dot));
|
||||
parts.add(raw.substring(dot + 1));
|
||||
} else {
|
||||
parts.add(raw);
|
||||
if (n instanceof SequenceNode) {
|
||||
return fnMap("Fn::GetAtt", n);
|
||||
}
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("Fn::GetAtt", parts);
|
||||
return map;
|
||||
return scalarMap("Fn::GetAtt", scalar(n));
|
||||
}
|
||||
|
||||
private Object construct(Node n) {
|
||||
|
||||
Reference in new issue
Block a user