mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(cloudformation): resolve dynamic references for all template values (#3301)
CloudFormationTemplateEngine#resolveNode now resolves {{resolve:ssm:...}}
and {{resolve:secretsmanager:...}} syntax in any textual result, not only
the RDS master-credential properties CfnDynamicReferences was previously
wired into. The dynamic-reference stage now lives inside resolve() itself,
so every scalar property resolution goes through it, including plain
property lookups such as ProvisionContext#resolveOptional that call
resolve() directly rather than through resolveNode. ssm-secure stays
rejected everywhere except RDS MasterUsername/MasterUserPassword, which
now skip the general dynamic-reference stage via
resolveWithoutDynamicReferences and resolve it separately with the
permission only those two properties are allowed.
* Fix #2213
Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
1 parent
cae8f9a013
commit
924e35cefe
6 files changed
+492
-12
No files matched your search
+28
-8
@@ -477,7 +477,8 @@ public class CloudFormationResourceProvisioner {
|
||||
public StackResource provisionStandalone(String resourceType, JsonNode properties, String region, String accountId) {
|
||||
CloudFormationTemplateEngine engine = new CloudFormationTemplateEngine(
|
||||
accountId, region, "cloudcontrol", "cloudcontrol",
|
||||
Map.of(), new HashMap<>(), new HashMap<>(), Map.of(), Map.of(), objectMapper, name -> null);
|
||||
Map.of(), new HashMap<>(), new HashMap<>(), Map.of(), Map.of(), objectMapper, name -> null,
|
||||
value -> dynamicReferences.resolveDynamicReferences(value, region, false));
|
||||
return provision("resource", resourceType, properties, engine, region, accountId, "cloudcontrol");
|
||||
}
|
||||
|
||||
@@ -1391,7 +1392,7 @@ public class CloudFormationResourceProvisioner {
|
||||
if (instance != null) {
|
||||
instance = rdsService.modifyDbInstance(
|
||||
id,
|
||||
resolveDynamicReferences(resolveOptional(props, "MasterUserPassword", engine), region, true),
|
||||
resolveDynamicReferences(resolveOptionalWithoutDynamicReferences(props, "MasterUserPassword", engine), region, true),
|
||||
parseBoolProp(props, "EnableIAMDatabaseAuthentication", engine),
|
||||
resolveOptional(props, "DBSubnetGroupName", engine));
|
||||
} else {
|
||||
@@ -1399,8 +1400,8 @@ public class CloudFormationResourceProvisioner {
|
||||
id,
|
||||
resolveOptional(props, "Engine", engine),
|
||||
resolveOptional(props, "EngineVersion", engine),
|
||||
resolveDynamicReferences(resolveOptional(props, "MasterUsername", engine), region, false),
|
||||
resolveDynamicReferences(resolveOptional(props, "MasterUserPassword", engine), region, true),
|
||||
resolveDynamicReferences(resolveOptionalWithoutDynamicReferences(props, "MasterUsername", engine), region, false),
|
||||
resolveDynamicReferences(resolveOptionalWithoutDynamicReferences(props, "MasterUserPassword", engine), region, true),
|
||||
resolveOptional(props, "DBName", engine),
|
||||
firstNonBlank(resolveOptional(props, "DBInstanceClass", engine), "db.t3.micro"),
|
||||
parseIntProp(props, "AllocatedStorage", engine, 20),
|
||||
@@ -1441,7 +1442,7 @@ public class CloudFormationResourceProvisioner {
|
||||
if (cluster != null) {
|
||||
cluster = rdsService.modifyDbCluster(
|
||||
id,
|
||||
resolveDynamicReferences(resolveOptional(props, "MasterUserPassword", engine), region, true),
|
||||
resolveDynamicReferences(resolveOptionalWithoutDynamicReferences(props, "MasterUserPassword", engine), region, true),
|
||||
parseBoolProp(props, "EnableIAMDatabaseAuthentication", engine),
|
||||
parseServerlessV2Capacity(props, "MinCapacity", engine),
|
||||
parseServerlessV2Capacity(props, "MaxCapacity", engine),
|
||||
@@ -1454,9 +1455,9 @@ public class CloudFormationResourceProvisioner {
|
||||
String engineName = resolveOptional(props, "Engine", engine);
|
||||
String engineVersion = resolveOptional(props, "EngineVersion", engine);
|
||||
String masterUsername = resolveDynamicReferences(
|
||||
resolveOptional(props, "MasterUsername", engine), region, false);
|
||||
resolveOptionalWithoutDynamicReferences(props, "MasterUsername", engine), region, false);
|
||||
String masterPassword = resolveDynamicReferences(
|
||||
resolveOptional(props, "MasterUserPassword", engine), region, true);
|
||||
resolveOptionalWithoutDynamicReferences(props, "MasterUserPassword", engine), region, true);
|
||||
String databaseName = resolveOptional(props, "DatabaseName", engine);
|
||||
boolean iamEnabled = parseBoolProp(props, "EnableIAMDatabaseAuthentication", engine);
|
||||
String parameterGroup = resolveOptional(props, "DBClusterParameterGroupName", engine);
|
||||
@@ -5689,9 +5690,28 @@ public class CloudFormationResourceProvisioner {
|
||||
return engine.resolve(props.get(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Like {@link #resolveOptional}, but skips the general dynamic-reference stage that
|
||||
* {@link CloudFormationTemplateEngine#resolve} applies. RDS {@code MasterUsername}/
|
||||
* {@code MasterUserPassword} are the only properties where {@code ssm-secure} is a valid
|
||||
* dynamic reference service, and the general stage rejects {@code ssm-secure} outright since
|
||||
* it is invalid everywhere else; the caller resolves the intrinsic-only result itself via
|
||||
* {@link #resolveDynamicReferences} with the permission only these two properties are allowed.
|
||||
*/
|
||||
private String resolveOptionalWithoutDynamicReferences(JsonNode props, String name,
|
||||
CloudFormationTemplateEngine engine) {
|
||||
if (props == null || !props.has(name) || props.get(name).isNull()) {
|
||||
return null;
|
||||
}
|
||||
return engine.resolveWithoutDynamicReferences(props.get(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves CloudFormation dynamic references in a provisioned property value. Delegates to
|
||||
* {@link CfnDynamicReferences}; the RDS master-credential paths are its only callers today.
|
||||
* {@link CfnDynamicReferences}. {@code allowSsmSecure} is {@code true} only for the RDS
|
||||
* master-credential properties resolved here directly; every other property value reaches
|
||||
* {@link CfnDynamicReferences} through {@link CloudFormationTemplateEngine#resolveNode}, which
|
||||
* disallows {@code ssm-secure} the same way the general path does.
|
||||
*/
|
||||
private String resolveDynamicReferences(String value, String region, boolean allowSsmSecure) {
|
||||
return dynamicReferences.resolveDynamicReferences(value, region, allowSsmSecure);
|
||||
|
||||
+9
-3
@@ -15,6 +15,7 @@ import io.github.hectorvent.floci.services.cloudformation.model.Stack;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackEvent;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.TemplateSummary;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnDynamicReferences;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnRollback;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.UpdateCleanupResult;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
@@ -65,6 +66,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
private final CloudFormationResourceProvisioner provisioner;
|
||||
private final S3Service s3Service;
|
||||
private final SsmService ssmService;
|
||||
private final CfnDynamicReferences dynamicReferences;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final EmulatorConfig config;
|
||||
private final RegionResolver regionResolver;
|
||||
@@ -81,12 +83,14 @@ public class CloudFormationService implements ResourceProvider {
|
||||
|
||||
@Inject
|
||||
public CloudFormationService(CloudFormationResourceProvisioner provisioner, S3Service s3Service,
|
||||
SsmService ssmService, ObjectMapper objectMapper, EmulatorConfig config,
|
||||
SsmService ssmService, CfnDynamicReferences dynamicReferences,
|
||||
ObjectMapper objectMapper, EmulatorConfig config,
|
||||
RegionResolver regionResolver, Clock clock,
|
||||
StorageFactory storageFactory) {
|
||||
this.provisioner = provisioner;
|
||||
this.s3Service = s3Service;
|
||||
this.ssmService = ssmService;
|
||||
this.dynamicReferences = dynamicReferences;
|
||||
this.objectMapper = objectMapper;
|
||||
this.config = config;
|
||||
this.regionResolver = regionResolver;
|
||||
@@ -1135,7 +1139,8 @@ public class CloudFormationService implements ResourceProvider {
|
||||
CloudFormationTemplateEngine engine = new CloudFormationTemplateEngine(
|
||||
accountId, region, stack.getStackName(),
|
||||
stack.getStackId(), resolvedParams, physicalIds, resourceAttrs, conditions, mappings, objectMapper,
|
||||
name -> exports.get(accountExportKey(accountId, exportKey(region, name))));
|
||||
name -> exports.get(accountExportKey(accountId, exportKey(region, name))),
|
||||
value -> dynamicReferences.resolveDynamicReferences(value, region, false));
|
||||
|
||||
StackResource resource = stack.getResources().get(logicalId);
|
||||
StackResource previousResource = resource;
|
||||
@@ -1233,7 +1238,8 @@ public class CloudFormationService implements ResourceProvider {
|
||||
CloudFormationTemplateEngine finalEngine = new CloudFormationTemplateEngine(
|
||||
accountId, region, stack.getStackName(),
|
||||
stack.getStackId(), resolvedParams, physicalIds, resourceAttrs, conditions, mappings, objectMapper,
|
||||
name -> exports.get(accountExportKey(accountId, exportKey(region, name))));
|
||||
name -> exports.get(accountExportKey(accountId, exportKey(region, name))),
|
||||
value -> dynamicReferences.resolveDynamicReferences(value, region, false));
|
||||
|
||||
// Resolve outputs before mutating stack/global export state, so failed updates do not
|
||||
// leave stale or partially registered exports behind.
|
||||
|
||||
+69
-1
@@ -14,6 +14,7 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.UnaryOperator;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
@@ -36,6 +37,7 @@ public class CloudFormationTemplateEngine {
|
||||
private final Map<String, JsonNode> mappings;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final Function<String, String> importValueResolver;
|
||||
private final UnaryOperator<String> dynamicReferenceResolver;
|
||||
|
||||
CloudFormationTemplateEngine(String accountId, String region, String stackName, String stackId,
|
||||
Map<String, String> parameters,
|
||||
@@ -45,6 +47,29 @@ public class CloudFormationTemplateEngine {
|
||||
Map<String, JsonNode> mappings,
|
||||
ObjectMapper objectMapper,
|
||||
Function<String, String> importValueResolver) {
|
||||
this(accountId, region, stackName, stackId, parameters, physicalIds, resourceAttributes,
|
||||
conditions, mappings, objectMapper, importValueResolver, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param dynamicReferenceResolver resolves a value that may contain CloudFormation dynamic
|
||||
* references ({@code {{resolve:ssm:...}}},
|
||||
* {@code {{resolve:secretsmanager:...}}}) against the live
|
||||
* services, leaving a value with no dynamic reference syntax
|
||||
* untouched. {@code null} skips this stage entirely, which
|
||||
* keeps template values that only ever contain intrinsics
|
||||
* (tests, Cloud Control desired-state resolution) free of the
|
||||
* dependency.
|
||||
*/
|
||||
CloudFormationTemplateEngine(String accountId, String region, String stackName, String stackId,
|
||||
Map<String, String> parameters,
|
||||
Map<String, String> physicalIds,
|
||||
Map<String, Map<String, String>> resourceAttributes,
|
||||
Map<String, Boolean> conditions,
|
||||
Map<String, JsonNode> mappings,
|
||||
ObjectMapper objectMapper,
|
||||
Function<String, String> importValueResolver,
|
||||
UnaryOperator<String> dynamicReferenceResolver) {
|
||||
this.accountId = accountId;
|
||||
this.region = region;
|
||||
this.stackName = stackName;
|
||||
@@ -56,9 +81,33 @@ public class CloudFormationTemplateEngine {
|
||||
this.mappings = mappings;
|
||||
this.objectMapper = objectMapper;
|
||||
this.importValueResolver = importValueResolver;
|
||||
this.dynamicReferenceResolver = dynamicReferenceResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a property value, including CloudFormation dynamic reference syntax
|
||||
* ({@code {{resolve:ssm:...}}}, {@code {{resolve:secretsmanager:...}}}) in the result, the same
|
||||
* way {@link #resolveNode} does. This is the entry point every scalar-property resolution in
|
||||
* this codebase goes through, directly or via {@link #resolveNode}, so a dynamic reference is
|
||||
* substituted regardless of which of the two a caller uses.
|
||||
*/
|
||||
public String resolve(JsonNode node) {
|
||||
return resolveDynamicReferences(resolveIntrinsic(node)).asText();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves intrinsics only, leaving any {@code {{resolve:...}}} dynamic reference syntax in the
|
||||
* result untouched. For the one property family where {@code ssm-secure} is a valid dynamic
|
||||
* reference service (RDS {@code MasterUsername}/{@code MasterUserPassword}): {@link #resolve}
|
||||
* rejects {@code ssm-secure} outright since it is invalid everywhere else, so that property has
|
||||
* to skip the general dynamic-reference stage and resolve it separately, with the permission
|
||||
* only that property is allowed.
|
||||
*/
|
||||
public String resolveWithoutDynamicReferences(JsonNode node) {
|
||||
return resolveIntrinsic(node);
|
||||
}
|
||||
|
||||
private String resolveIntrinsic(JsonNode node) {
|
||||
if (node == null || node.isNull() || node.isMissingNode()) {
|
||||
return "";
|
||||
}
|
||||
@@ -116,7 +165,10 @@ public class CloudFormationTemplateEngine {
|
||||
if (node == null || node.isNull() || node.isMissingNode()) {
|
||||
return node;
|
||||
}
|
||||
if (node.isTextual() || node.isNumber() || node.isBoolean()) {
|
||||
if (node.isTextual()) {
|
||||
return resolveDynamicReferences(node.textValue());
|
||||
}
|
||||
if (node.isNumber() || node.isBoolean()) {
|
||||
return node;
|
||||
}
|
||||
if (node.isObject()) {
|
||||
@@ -156,6 +208,22 @@ public class CloudFormationTemplateEngine {
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a value against {@link #dynamicReferenceResolver} when it carries CloudFormation
|
||||
* dynamic reference syntax ({@code {{resolve:ssm:...}}}, {@code {{resolve:secretsmanager:...}}}).
|
||||
* CloudFormation substitutes these for any string property in a template, not only the RDS
|
||||
* master-credential properties that first needed them (see
|
||||
* <a href="https://github.com/floci-io/floci/issues/2213">#2213</a>), so {@link #resolveNode}
|
||||
* applies this to every textual result: a literal string carrying the syntax outright, and the
|
||||
* text an intrinsic function (e.g. {@code Fn::Sub}) produces.
|
||||
*/
|
||||
private TextNode resolveDynamicReferences(String value) {
|
||||
if (dynamicReferenceResolver == null || value == null || !value.contains("{{resolve:")) {
|
||||
return TextNode.valueOf(value);
|
||||
}
|
||||
return TextNode.valueOf(dynamicReferenceResolver.apply(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a node that must be stored as a JSON document (SNS/SQS RedrivePolicy and
|
||||
* FilterPolicy, Step Functions definitions, IAM policy documents) to its string form.
|
||||
|
||||
Reference in new issue
Block a user