mirror of
https://github.com/floci-io/floci.git
synced 2026-12-03 02:07:58 -05:00
refactor(cloudformation): move the RDS types into RdsCfnProvisioner (#3459)
Moves all seven AWS::RDS types out of the monolith switch into a per-service provisioner. Method bodies are carried over unchanged except for the six master-credential call sites described below; the rest is plumbing. The seven types delete by physical id alone, so the id-only delete override serves all of them and none appears in DELETE_NEEDS_STACK_RESOURCE. Unlike the ECS, ELBv2, Cognito, EC2-networking, Events and Batch batches, this one does not take its service out of the monolith. SecretTargetAttachment is still served there and reads an instance's or cluster's endpoint through RdsService to build its connection detail, so the field, its constructor parameter and its import all stay. Four helpers the RDS block shared with other types stay too, copied into the provisioner: sameNameExistingResource, deleteRenamedResource, parseIntProp and parseCfnTags. firstNonBlank, parseBoolProp and parseOptionalIntProp had no non-RDS callers left and moved outright. Name generation now goes through ProvisionContext.generatePhysicalName rather than a copy: the two implementations were already byte-identical, down to the 12-character suffix. MasterUsername and MasterUserPassword keep the opt-out #3301 gave them. The engine resolves dynamic references for every property now, with ssm-secure rejected, so those two properties read through resolveOptionalWithoutDynamicReferences and resolve their own references with ssm-secure allowed. Reading them with the ordinary resolveOptional compiles and leaves the suite green while breaking ssm-secure on the only resource AWS permits it on, so the monolith's helper moves across with the code rather than collapsing into the ordinary path. CloudFormationRdsDynamicReferenceIntegrationTest is new and covers that, because nothing did. RdsCfnProvisionerTest builds its engine without a dynamic-reference resolver, so the general stage is off there and its assertions hold either way, and CloudFormationDynamicReferenceIntegrationTest has no RDS case. The ssm-secure test fails with ROLLBACK_COMPLETE when the opt-out is reverted, and the plain ssm test guards the other direction. The 69 existing RdsCfnProvisionerTest tests and the 2 integration tests pass unchanged, which is the guard that the move preserved behaviour: both build through CfnProvisionerFixture, so they exercise the new provisioner without knowing it exists. The fixture's CfnDynamicReferences wiring had to move ahead of the registry build, since the provisioner takes it as a constructor argument. Making the types provisioner-owned rather than LEGACY_SWITCH puts them under CfnSchemaCoverageTest for the first time, which the switch was exempt from. The 19 attributes it found are recorded as gaps here, unchanged from what the switch set. Two of them are closable and a follow-up PR closes them, kept out of this one so it stays a pure move; Endpoint and ReadEndpoint are recorded rather than set because the dotted sub-attributes already carry them, which is the form AWS's GetAtt actually exposes. Monolith 5,942 to 5,458 lines, legacy types 39 to 32.
This commit is contained in:
1 parent
341736fc39
commit
29e9ae9221
6 files changed
+899
-507
No files matched your search
+21
-7
@@ -62,6 +62,7 @@ import io.github.hectorvent.floci.services.cloudformation.provisioners.LambdaEve
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.LambdaVersionAliasCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.LogsCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.PipesCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.RdsCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.Route53CfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.S3CfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.SchedulerScheduleGroupCfnProvisioner;
|
||||
@@ -215,6 +216,7 @@ final class CfnProvisionerFixture {
|
||||
* behind silently.
|
||||
*/
|
||||
private List<CfnResourceProvisioner> inferredProvisioners() {
|
||||
ensureDynamicReferences();
|
||||
List<CfnResourceProvisioner> discovered = new ArrayList<>();
|
||||
discovered.add(new CdkMetadataCfnProvisioner());
|
||||
if (s3Service != null) {
|
||||
@@ -316,6 +318,9 @@ final class CfnProvisionerFixture {
|
||||
if (batchService != null) {
|
||||
discovered.add(new BatchCfnProvisioner(batchService));
|
||||
}
|
||||
if (rdsService != null) {
|
||||
discovered.add(new RdsCfnProvisioner(rdsService, dynamicReferences));
|
||||
}
|
||||
if (wafV2Service != null) {
|
||||
discovered.add(new WafV2CfnProvisioner(wafV2Service));
|
||||
}
|
||||
@@ -586,6 +591,21 @@ final class CfnProvisionerFixture {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wires {@link CfnDynamicReferences} from the services already named, the way CDI does in
|
||||
* production, so a test resolving {{@code resolve:ssm:...}} does not have to know that
|
||||
* resolution moved out of the provisioner.
|
||||
*
|
||||
* <p>Called before the registry is built, not only from {@link #build()}: RdsCfnProvisioner
|
||||
* takes it as a constructor argument, so inferring the provisioners needs it in hand.
|
||||
*/
|
||||
private void ensureDynamicReferences() {
|
||||
if (dynamicReferences == null) {
|
||||
dynamicReferences = new CfnDynamicReferences(
|
||||
secretsManagerService, ssmService, objectMapper);
|
||||
}
|
||||
}
|
||||
|
||||
/** The registry {@link #build()} would use: explicit if the test chose one, else inferred. */
|
||||
CloudFormationResourceRegistry buildRegistry() {
|
||||
return registryChosenByTest
|
||||
@@ -597,13 +617,7 @@ final class CfnProvisionerFixture {
|
||||
if (!registryChosenByTest) {
|
||||
resourceRegistry = buildRegistry();
|
||||
}
|
||||
if (dynamicReferences == null) {
|
||||
// Wire it from the services already named, the way CDI does in production, so a
|
||||
// test resolving {{resolve:ssm:...}} or {{resolve:secretsmanager:...}} does not
|
||||
// have to know that resolution moved out of the provisioner.
|
||||
dynamicReferences = new CfnDynamicReferences(
|
||||
secretsManagerService, ssmService, objectMapper);
|
||||
}
|
||||
ensureDynamicReferences();
|
||||
return new CloudFormationResourceProvisioner(
|
||||
s3Service,
|
||||
snsService,
|
||||
|
||||
+200
@@ -0,0 +1,200 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* RDS master credentials are the only properties where {@code {{resolve:ssm-secure:...}}} is
|
||||
* legal. Every other property resolves dynamic references through the template engine's general
|
||||
* stage, which rejects {@code ssm-secure} outright, so the two RDS credential properties opt out
|
||||
* of that stage and resolve their own references with the elevated permission.
|
||||
*
|
||||
* <p>That opt-out is invisible to the rest of the suite. {@code RdsCfnProvisionerTest} builds its
|
||||
* engine without a dynamic-reference resolver, so the general stage is disabled there and its
|
||||
* assertions hold whether or not the opt-out survives.
|
||||
* {@code CloudFormationDynamicReferenceIntegrationTest} covers the general path and the
|
||||
* {@code ssm-secure} rejection, but has no RDS case. Without the tests below, dropping the opt-out
|
||||
* leaves the whole suite green and breaks {@code ssm-secure} on the one resource that accepts it.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(CloudFormationRdsDynamicReferenceIntegrationTest.RdsMockProfile.class)
|
||||
class CloudFormationRdsDynamicReferenceIntegrationTest {
|
||||
|
||||
private static final String CFN_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request";
|
||||
private static final String SSM_CONTENT_TYPE = "application/x-amz-json-1.1";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
/**
|
||||
* The regression gate. A master password reading from an SSM {@code SecureString} must reach
|
||||
* {@code CREATE_COMPLETE}. If the credential properties stop opting out of the general stage,
|
||||
* that stage rejects the reference and the stack rolls back instead.
|
||||
*/
|
||||
@Test
|
||||
void createStack_rdsMasterPasswordResolvesSsmSecureDynamicReference() throws Exception {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String parameterName = "/cfn-rds-dynref/secure-password-" + suffix;
|
||||
String stackName = "cfn-rds-dynref-secure-stack-" + suffix;
|
||||
String databaseId = "cfn-rds-dynref-secure-db-" + suffix;
|
||||
putParameter(parameterName, "s3cret-from-ssm", "SecureString");
|
||||
boolean stackCreated = false;
|
||||
|
||||
try {
|
||||
createStack(stackName, template(databaseId,
|
||||
"{{resolve:ssm-secure:" + parameterName + "}}"));
|
||||
stackCreated = true;
|
||||
|
||||
String describeXml = describeStack(stackName);
|
||||
assertTrue(describeXml.contains("<StackStatus>CREATE_COMPLETE</StackStatus>"),
|
||||
"an ssm-secure master password is legal on RDS and must provision: " + describeXml);
|
||||
} finally {
|
||||
if (stackCreated) {
|
||||
deleteStack(stackName);
|
||||
awaitStackDeleted(stackName);
|
||||
}
|
||||
deleteParameter(parameterName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The other direction. Opting out of the general stage must not cost the credential properties
|
||||
* the ordinary {@code ssm} reference, which they resolve themselves.
|
||||
*/
|
||||
@Test
|
||||
void createStack_rdsMasterPasswordResolvesPlainSsmDynamicReference() throws Exception {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String parameterName = "/cfn-rds-dynref/plain-password-" + suffix;
|
||||
String stackName = "cfn-rds-dynref-plain-stack-" + suffix;
|
||||
String databaseId = "cfn-rds-dynref-plain-db-" + suffix;
|
||||
putParameter(parameterName, "plain-from-ssm", "String");
|
||||
boolean stackCreated = false;
|
||||
|
||||
try {
|
||||
createStack(stackName, template(databaseId,
|
||||
"{{resolve:ssm:" + parameterName + "}}"));
|
||||
stackCreated = true;
|
||||
|
||||
String describeXml = describeStack(stackName);
|
||||
assertTrue(describeXml.contains("<StackStatus>CREATE_COMPLETE</StackStatus>"),
|
||||
"a plain ssm master password must still resolve: " + describeXml);
|
||||
} finally {
|
||||
if (stackCreated) {
|
||||
deleteStack(stackName);
|
||||
awaitStackDeleted(stackName);
|
||||
}
|
||||
deleteParameter(parameterName);
|
||||
}
|
||||
}
|
||||
|
||||
private static String template(String databaseId, String masterPassword) {
|
||||
return """
|
||||
{
|
||||
"Resources": {
|
||||
"Database": {
|
||||
"Type": "AWS::RDS::DBInstance",
|
||||
"Properties": {
|
||||
"DBInstanceIdentifier": "%s",
|
||||
"Engine": "postgres",
|
||||
"MasterUsername": "dbadmin",
|
||||
"MasterUserPassword": "%s",
|
||||
"DBName": "appdb",
|
||||
"DBInstanceClass": "db.t3.micro",
|
||||
"AllocatedStorage": 20
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(databaseId, masterPassword);
|
||||
}
|
||||
|
||||
private static void putParameter(String name, String value, String type) {
|
||||
given()
|
||||
.header("X-Amz-Target", "AmazonSSM.PutParameter")
|
||||
.contentType(SSM_CONTENT_TYPE)
|
||||
.body("""
|
||||
{
|
||||
"Name": "%s",
|
||||
"Value": "%s",
|
||||
"Type": "%s",
|
||||
"Overwrite": true
|
||||
}
|
||||
""".formatted(name, value, type))
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static void deleteParameter(String name) {
|
||||
given()
|
||||
.header("X-Amz-Target", "AmazonSSM.DeleteParameter")
|
||||
.contentType(SSM_CONTENT_TYPE)
|
||||
.body("{\"Name\":\"" + name + "\"}")
|
||||
.when().post("/");
|
||||
}
|
||||
|
||||
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 String describeStack(String stackName) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", stackName)
|
||||
.when().post("/").then().statusCode(200).extract().asString();
|
||||
}
|
||||
|
||||
private static void deleteStack(String stackName) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DeleteStack")
|
||||
.formParam("StackName", stackName)
|
||||
.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static void awaitStackDeleted(String stackName) throws InterruptedException {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
String body = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", stackName)
|
||||
.when().post("/").then().extract().asString();
|
||||
if (body.contains("does not exist")) {
|
||||
return;
|
||||
}
|
||||
if (body.contains("<StackStatus>DELETE_FAILED</StackStatus>")) {
|
||||
fail("stack delete failed: " + body);
|
||||
}
|
||||
Thread.sleep(50);
|
||||
}
|
||||
fail("stack " + stackName + " was not deleted within the timeout");
|
||||
}
|
||||
|
||||
public static final class RdsMockProfile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.services.rds.mock", "true");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user