mirror of
https://github.com/floci-io/floci.git
synced 2026-12-06 03:28:18 -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
+317
@@ -0,0 +1,317 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* CloudFormation resolves {@code {{resolve:...}}} dynamic references for any string property in a
|
||||
* template, not only the RDS master-credential properties that first needed them (issue #2213).
|
||||
* These tests exercise that general path end to end through a real stack deploy, using a Lambda
|
||||
* {@code Environment.Variables} entry the way the AWS documentation's own example does.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class CloudFormationDynamicReferenceIntegrationTest {
|
||||
|
||||
private static final String SSM_CONTENT_TYPE = "application/x-amz-json-1.1";
|
||||
private static final String SM_CONTENT_TYPE = "application/x-amz-json-1.1";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createStack_lambdaEnvironmentVariableResolvesSsmDynamicReference() {
|
||||
given()
|
||||
.header("X-Amz-Target", "AmazonSSM.PutParameter")
|
||||
.contentType(SSM_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"Name": "/cfn-dynref/url",
|
||||
"Value": "https://real.example.com",
|
||||
"Type": "String",
|
||||
"Overwrite": true
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"MyFunction": {
|
||||
"Type": "AWS::Lambda::Function",
|
||||
"Properties": {
|
||||
"FunctionName": "cfn-dynref-ssm-func",
|
||||
"Runtime": "nodejs20.x",
|
||||
"Handler": "index.handler",
|
||||
"Role": "arn:aws:iam::000000000000:role/cfn-test-lambda-role",
|
||||
"Environment": {
|
||||
"Variables": {
|
||||
"URL": "{{resolve:ssm:/cfn-dynref/url}}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", "cfn-dynref-ssm-stack")
|
||||
.formParam("TemplateBody", template)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackId>"));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", "cfn-dynref-ssm-stack")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/2015-03-31/functions/cfn-dynref-ssm-func")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Configuration.Environment.Variables.URL", equalTo("https://real.example.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createStack_lambdaEnvironmentVariableResolvesSecretsManagerDynamicReference() {
|
||||
given()
|
||||
.header("X-Amz-Target", "secretsmanager.CreateSecret")
|
||||
.contentType(SM_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"Name": "cfn-dynref-secret",
|
||||
"SecretString": "{\\"apiKey\\":\\"s3cr3t-value\\"}"
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"MyFunction": {
|
||||
"Type": "AWS::Lambda::Function",
|
||||
"Properties": {
|
||||
"FunctionName": "cfn-dynref-sm-func",
|
||||
"Runtime": "nodejs20.x",
|
||||
"Handler": "index.handler",
|
||||
"Role": "arn:aws:iam::000000000000:role/cfn-test-lambda-role",
|
||||
"Environment": {
|
||||
"Variables": {
|
||||
"API_KEY": "{{resolve:secretsmanager:cfn-dynref-secret:SecretString:apiKey}}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", "cfn-dynref-sm-stack")
|
||||
.formParam("TemplateBody", template)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackId>"));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", "cfn-dynref-sm-stack")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/2015-03-31/functions/cfn-dynref-sm-func")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Configuration.Environment.Variables.API_KEY", equalTo("s3cr3t-value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createStack_snsTopicNameResolvesSsmDynamicReferenceThroughResolveOptional() {
|
||||
given()
|
||||
.header("X-Amz-Target", "AmazonSSM.PutParameter")
|
||||
.contentType(SSM_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"Name": "/cfn-dynref/topic-name",
|
||||
"Value": "cfn-dynref-resolved-topic",
|
||||
"Type": "String",
|
||||
"Overwrite": true
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"MyTopic": {
|
||||
"Type": "AWS::SNS::Topic",
|
||||
"Properties": {
|
||||
"TopicName": "{{resolve:ssm:/cfn-dynref/topic-name}}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", "cfn-dynref-sns-stack")
|
||||
.formParam("TemplateBody", template)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackId>"));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", "cfn-dynref-sns-stack")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "ListTopics")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("cfn-dynref-resolved-topic"))
|
||||
.body(org.hamcrest.Matchers.not(containsString("{{resolve:ssm:")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createStack_ssmSecureDynamicReferenceOnANonRdsResourceFailsResourceCreation() {
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"MyTopic": {
|
||||
"Type": "AWS::SNS::Topic",
|
||||
"Properties": {
|
||||
"TopicName": "{{resolve:ssm-secure:/cfn-dynref/topic-name}}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", "cfn-dynref-ssm-secure-non-rds-stack")
|
||||
.formParam("TemplateBody", template)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackId>"));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", "cfn-dynref-ssm-secure-non-rds-stack")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackStatus>ROLLBACK_COMPLETE</StackStatus>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createStack_lambdaEnvironmentVariableLeavesPlainLiteralStringUntouched() {
|
||||
String template = """
|
||||
{
|
||||
"Resources": {
|
||||
"MyFunction": {
|
||||
"Type": "AWS::Lambda::Function",
|
||||
"Properties": {
|
||||
"FunctionName": "cfn-dynref-literal-func",
|
||||
"Runtime": "nodejs20.x",
|
||||
"Handler": "index.handler",
|
||||
"Role": "arn:aws:iam::000000000000:role/cfn-test-lambda-role",
|
||||
"Environment": {
|
||||
"Variables": {
|
||||
"PLAIN": "just-a-normal-value",
|
||||
"BRACES": "not-a-dynamic-reference-{{example}}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateStack")
|
||||
.formParam("StackName", "cfn-dynref-literal-stack")
|
||||
.formParam("TemplateBody", template)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackId>"));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", "cfn-dynref-literal-stack")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/2015-03-31/functions/cfn-dynref-literal-func")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("Configuration.Environment.Variables.PLAIN", equalTo("just-a-normal-value"))
|
||||
.body("Configuration.Environment.Variables.BRACES",
|
||||
equalTo("not-a-dynamic-reference-{{example}}"));
|
||||
}
|
||||
}
|
||||
+2
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.Stack;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.CfnDynamicReferences;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import io.github.hectorvent.floci.services.ssm.SsmService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -47,6 +48,7 @@ class CloudFormationServiceRollbackTest {
|
||||
provisioner,
|
||||
mock(S3Service.class),
|
||||
mock(SsmService.class),
|
||||
mock(CfnDynamicReferences.class),
|
||||
new ObjectMapper(),
|
||||
config,
|
||||
mock(RegionResolver.class),
|
||||
|
||||
+67
@@ -10,10 +10,12 @@ import org.junit.jupiter.params.provider.ValueSource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
class CloudFormationTemplateEngineTest {
|
||||
|
||||
@@ -370,4 +372,69 @@ class CloudFormationTemplateEngineTest {
|
||||
assertEquals(java.util.List.of("subnet-default"),
|
||||
eFalse.resolveStringList(json("{\"Fn::If\":[\"UseCustom\",[\"subnet-prefix\",{\"Fn::Split\":[\",\",{\"Fn::ImportValue\":\"Subnets\"}]}],[\"subnet-default\"]]}")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reproduces #2213: a Lambda {@code Environment.Variables} entry (or any other plain string
|
||||
* template value) carrying {@code {{resolve:...}}} syntax reached the deployed resource as the
|
||||
* literal text because {@code resolveNode}, the general-purpose path every non-RDS property goes
|
||||
* through, had no dynamic-reference stage.
|
||||
*/
|
||||
@Test
|
||||
void resolveNodeResolvesDynamicReferenceInPlainStringValue() {
|
||||
UnaryOperator<String> resolver = value -> {
|
||||
assertEquals("{{resolve:ssm:/demo/url}}", value);
|
||||
return "https://real.example.com";
|
||||
};
|
||||
CloudFormationTemplateEngine e = new CloudFormationTemplateEngine("000000000000",
|
||||
"us-east-1", "my-stack", "stack/id", Map.of(), Map.of(), Map.of(), Map.of(),
|
||||
Map.of(), mapper, (Function<String, String>) name -> null, resolver);
|
||||
|
||||
assertEquals("https://real.example.com",
|
||||
e.resolveNode(json("\"{{resolve:ssm:/demo/url}}\"")).asText());
|
||||
}
|
||||
|
||||
/**
|
||||
* The same dynamic-reference stage applies to the text an intrinsic function produces, since a
|
||||
* literal {@code {{resolve:...}}} embedded in an {@code Fn::Sub} template survives substitution
|
||||
* untouched and reaches resolveNode's final string.
|
||||
*/
|
||||
@Test
|
||||
void resolveNodeResolvesDynamicReferenceProducedByIntrinsic() {
|
||||
UnaryOperator<String> resolver = value -> {
|
||||
assertEquals("prefix-{{resolve:ssm:/demo/url}}", value);
|
||||
return "prefix-https://real.example.com";
|
||||
};
|
||||
CloudFormationTemplateEngine e = new CloudFormationTemplateEngine("000000000000",
|
||||
"us-east-1", "my-stack", "stack/id", Map.of(), Map.of(), Map.of(), Map.of(),
|
||||
Map.of(), mapper, (Function<String, String>) name -> null, resolver);
|
||||
|
||||
assertEquals("prefix-https://real.example.com", e.resolveNode(
|
||||
json("{\"Fn::Sub\": \"prefix-{{resolve:ssm:/demo/url}}\"}")).asText());
|
||||
}
|
||||
|
||||
/**
|
||||
* A plain literal with no dynamic reference syntax must never reach the resolver: it is left
|
||||
* exactly as written, and a resolver that throws proves it was not invoked.
|
||||
*/
|
||||
@Test
|
||||
void resolveNodeLeavesPlainLiteralStringUntouched() {
|
||||
UnaryOperator<String> resolver = value -> fail("dynamic reference resolver must not be "
|
||||
+ "invoked for a value with no {{resolve:...}} syntax: " + value);
|
||||
CloudFormationTemplateEngine e = new CloudFormationTemplateEngine("000000000000",
|
||||
"us-east-1", "my-stack", "stack/id", Map.of(), Map.of(), Map.of(), Map.of(),
|
||||
Map.of(), mapper, (Function<String, String>) name -> null, resolver);
|
||||
|
||||
assertEquals("just-a-normal-value", e.resolveNode(json("\"just-a-normal-value\"")).asText());
|
||||
}
|
||||
|
||||
/**
|
||||
* With no dynamic-reference resolver configured (the 11-argument constructor every other test
|
||||
* in this class uses), resolveNode leaves {@code {{resolve:...}}} syntax exactly as written
|
||||
* instead of failing: callers that never need dynamic references stay decoupled from them.
|
||||
*/
|
||||
@Test
|
||||
void resolveNodeWithNoResolverLeavesDynamicReferenceSyntaxVerbatim() {
|
||||
assertEquals("{{resolve:ssm:/demo/url}}",
|
||||
engine().resolveNode(json("\"{{resolve:ssm:/demo/url}}\"")).asText());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user