diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java index 4ed53ea943..6d76c88eb0 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java @@ -134,8 +134,7 @@ public class CloudFormationResourceProvisioner { "AWS::IAM::ManagedPolicy", "AWS::IAM::Policy", "AWS::Lambda::Function", - "AWS::Lambda::LayerVersion", - "AWS::Route53::RecordSet"); + "AWS::Lambda::LayerVersion"); /** Reserved attribute keys used to carry custom-resource state to the later Delete invocation. */ private static final String CR_SERVICE_TOKEN_ATTR = "__FlociServiceToken"; @@ -245,7 +244,6 @@ public class CloudFormationResourceProvisioner { case "AWS::IAM::ManagedPolicy" -> provisionIamManagedPolicy(resource, properties, engine, accountId, stackName); case "AWS::IAM::InstanceProfile" -> provisionInstanceProfile(resource, properties, engine, accountId, stackName); - case "AWS::Route53::RecordSet" -> provisionRoute53RecordSet(resource, properties, engine); case "AWS::ApiGateway::RestApi" -> provisionApiGatewayRestApi(resource, properties, engine, region, accountId, stackName); case "AWS::ApiGateway::Resource" -> provisionApiGatewayResource(resource, properties, engine, region); case "AWS::ApiGateway::Authorizer" -> provisionApiGatewayAuthorizer(resource, properties, engine, region); @@ -1962,11 +1960,6 @@ public class CloudFormationResourceProvisioner { } } - private void provisionRoute53RecordSet(StackResource r, JsonNode props, CloudFormationTemplateEngine engine) { - String name = resolveOptional(props, "Name", engine); - r.setPhysicalId(name != null ? name : "record-" + UUID.randomUUID().toString().substring(0, 8)); - } - // ── ApiGateway (V1) ────────────────────────────────────────────────────── private void provisionApiGatewayRestApi(StackResource r, JsonNode props, CloudFormationTemplateEngine engine, diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Route53CfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Route53CfnProvisioner.java index 0f31325d7c..a492aa8637 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Route53CfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Route53CfnProvisioner.java @@ -7,14 +7,32 @@ import io.github.hectorvent.floci.services.route53.Route53Service; import io.github.hectorvent.floci.services.route53.model.VpcAssociation; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; +import org.jboss.logging.Logger; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.UUID; +/** + * Provisions {@code AWS::Route53::HostedZone} and {@code AWS::Route53::RecordSet}. + * + *

A record set is a name-only resource here, as it was in the legacy + * {@code CloudFormationResourceProvisioner} it moved out of: nothing is written to the zone, + * {@code Ref} is the record name (or a generated {@code record-} id when the template gives + * none), and no {@code Fn::GetAtt} attribute is published. The registry schema lists {@code Id} + * as read-only, but the type has no registry handlers and the CloudFormation resource + * specification gives it no attributes, so {@code Ref} is its only reference. Writing the record + * through {@code ChangeResourceRecordSets} is a follow-up. + */ @ApplicationScoped public class Route53CfnProvisioner implements CfnResourceProvisioner { + static final String HOSTED_ZONE = "AWS::Route53::HostedZone"; + static final String RECORD_SET = "AWS::Route53::RecordSet"; + + private static final Logger LOG = Logger.getLogger(Route53CfnProvisioner.class); + private final Route53Service route53Service; @Inject @@ -24,11 +42,26 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner { @Override public Set resourceTypes() { - return Set.of("AWS::Route53::HostedZone"); + return Set.of(HOSTED_ZONE, RECORD_SET); } @Override public void provision(StackResource resource, JsonNode props, ProvisionContext ctx) { + switch (resource.getResourceType()) { + case "AWS::Route53::HostedZone" -> provisionHostedZone(resource, props, ctx); + case "AWS::Route53::RecordSet" -> provisionRecordSet(resource, props, ctx); + default -> throw new IllegalStateException( + "Route53CfnProvisioner received an unsupported type: " + resource.getResourceType()); + } + } + + private void provisionRecordSet(StackResource resource, JsonNode props, ProvisionContext ctx) { + String name = ctx.resolveOptional(props, "Name"); + String id = name != null ? name : "record-" + UUID.randomUUID().toString().substring(0, 8); + resource.setPhysicalId(id); + } + + private void provisionHostedZone(StackResource resource, JsonNode props, ProvisionContext ctx) { String name = ctx.resolveOptional(props, "Name"); if (name == null || name.isBlank()) { throw new IllegalArgumentException("AWS::Route53::HostedZone requires Name"); @@ -126,7 +159,16 @@ public class Route53CfnProvisioner implements CfnResourceProvisioner { @Override public void delete(String resourceType, String physicalId, String region) { - route53Service.deleteHostedZone(physicalId); + switch (resourceType) { + case "AWS::Route53::HostedZone" -> route53Service.deleteHostedZone(physicalId); + // Nothing was written for the record, so there is nothing to remove; the warning + // mirrors the legacy dispatcher's default arm so the gap stays visible in the log. + case "AWS::Route53::RecordSet" -> LOG.warnv( + "No delete implemented for resource type {0}: {1} is not removed here.", + resourceType, physicalId); + default -> throw new IllegalStateException( + "Route53CfnProvisioner received an unsupported type: " + resourceType); + } } private List parseVpcs(JsonNode node) { diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRoute53RecordSetIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRoute53RecordSetIntegrationTest.java new file mode 100644 index 0000000000..885b85f50b --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationRoute53RecordSetIntegrationTest.java @@ -0,0 +1,134 @@ +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 java.time.Duration; + +import static io.restassured.RestAssured.given; +import static org.awaitility.Awaitility.await; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * {@code AWS::Route53::RecordSet} through the per-service provisioner: {@code Ref} is the record + * name. It is read back through an SSM parameter the template writes, because a stack status + * alone proves nothing: an unowned type is stubbed CREATE_COMPLETE with an {@code arn:aws:stub} + * physical id, which the parameter would expose. + */ +@QuarkusTest +class CloudFormationRoute53RecordSetIntegrationTest { + + private static final String CFN_AUTH = + "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request"; + private static final String SSM_AUTH = + "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ssm/aws4_request"; + private static final Duration STACK_DELETE_TIMEOUT = Duration.ofSeconds(30); + private static final Duration STACK_DELETE_POLL_INTERVAL = Duration.ofMillis(50); + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void recordSetRefResolvesToTheRecordName() { + String suffix = Long.toString(System.nanoTime(), 36); + String stackName = "r53-recordset-" + suffix; + String recordName = "www." + suffix + ".cfn-it.example.com"; + String template = """ + { + "Resources": { + "Zone": { + "Type": "AWS::Route53::HostedZone", + "Properties": {"Name": "%s.cfn-it.example.com"} + }, + "Www": { + "Type": "AWS::Route53::RecordSet", + "Properties": { + "HostedZoneId": {"Ref": "Zone"}, + "Name": "%s", + "Type": "A", + "TTL": "300", + "ResourceRecords": ["10.0.0.1"] + } + }, + "RefParam": { + "Type": "AWS::SSM::Parameter", + "Properties": {"Name": "/r53-recordset/%s/ref", "Type": "String", "Value": {"Ref": "Www"}} + } + } + } + """.formatted(suffix, recordName, suffix); + + createStack(stackName, template); + assertStackStatus(stackName, "CREATE_COMPLETE"); + try { + assertEquals(recordName, parameterValue("/r53-recordset/" + suffix + "/ref")); + } finally { + deleteStack(stackName); + awaitStackGone(stackName); + } + } + + private 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 void assertStackStatus(String stackName, String status) { + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", CFN_AUTH) + .formParam("Action", "DescribeStacks") + .formParam("StackName", stackName) + .when().post("/").then().statusCode(200) + .body(containsString("" + status + "")); + } + + private 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 String parameterValue(String name) { + return given() + .contentType("application/x-amz-json-1.1") + .header("Authorization", SSM_AUTH) + .header("X-Amz-Target", "AmazonSSM.GetParameter") + .body("{\"Name\":\"" + name + "\"}") + .when().post("/").then().statusCode(200) + .extract().jsonPath().getString("Parameter.Value"); + } + + private void awaitStackGone(String stackName) { + await() + .atMost(STACK_DELETE_TIMEOUT) + .pollInterval(STACK_DELETE_POLL_INTERVAL) + .untilAsserted(() -> { + 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("DELETE_FAILED")) { + fail("stack delete failed: " + body); + } + assertTrue(body.contains("does not exist"), "stack still exists: " + body); + }); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Route53CfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Route53CfnProvisionerTest.java index 8d9af05201..8bf4de5547 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Route53CfnProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Route53CfnProvisionerTest.java @@ -15,6 +15,7 @@ import java.util.Set; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.argThat; @@ -22,6 +23,7 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; class Route53CfnProvisionerTest { @@ -366,4 +368,64 @@ class Route53CfnProvisionerTest { + "(tracked zone missing), so the generic rollback walker must treat this " + "resource as restored instead of failing rollback as unimplemented"); } + + @Test + void recordSetUsesItsNameAsRefPublishesNoAttributesAndWritesNothingToTheZone() { + Route53Service service = mock(Route53Service.class); + ObjectMapper mapper = new ObjectMapper(); + JsonNode props = mapper.createObjectNode() + .put("HostedZoneId", "Z123456789") + .put("Name", "www.example.com") + .put("Type", "A"); + CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class); + when(engine.resolve(any())).thenAnswer(invocation -> invocation.getArgument(0).asText()); + ProvisionContext context = new ProvisionContext(engine, "us-east-1", "623666680275", "dns-stack"); + StackResource resource = new StackResource(); + resource.setLogicalId("Alias"); + resource.setResourceType("AWS::Route53::RecordSet"); + + new Route53CfnProvisioner(service).provision(resource, props, context); + + assertEquals("www.example.com", resource.getPhysicalId()); + assertEquals(Set.of(), resource.getAttributes().keySet()); + verifyNoInteractions(service); + } + + @Test + void recordSetWithoutANameGetsAGeneratedRecordId() { + Route53Service service = mock(Route53Service.class); + ObjectMapper mapper = new ObjectMapper(); + JsonNode props = mapper.createObjectNode().put("Type", "A"); + CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class); + when(engine.resolve(any())).thenAnswer(invocation -> invocation.getArgument(0).asText()); + ProvisionContext context = new ProvisionContext(engine, "us-east-1", "623666680275", "dns-stack"); + StackResource resource = new StackResource(); + resource.setLogicalId("Alias"); + resource.setResourceType("AWS::Route53::RecordSet"); + + new Route53CfnProvisioner(service).provision(resource, props, context); + + assertNotNull(resource.getPhysicalId()); + assertTrue(resource.getPhysicalId().matches("record-[0-9a-f]{8}"), resource.getPhysicalId()); + assertTrue(resource.getAttributes().isEmpty(), resource.getAttributes().toString()); + verifyNoInteractions(service); + } + + @Test + void deletingARecordSetLeavesHostedZonesAlone() { + Route53Service service = mock(Route53Service.class); + + new Route53CfnProvisioner(service).delete("AWS::Route53::RecordSet", "www.example.com", "us-east-1"); + + verifyNoInteractions(service); + } + + @Test + void deletingAHostedZoneStillDeletesTheZone() { + Route53Service service = mock(Route53Service.class); + + new Route53CfnProvisioner(service).delete("AWS::Route53::HostedZone", "Z123456789", "us-east-1"); + + verify(service).deleteHostedZone("Z123456789"); + } } diff --git a/src/test/resources/cloudformation/getatt-attribute-gaps.tsv b/src/test/resources/cloudformation/getatt-attribute-gaps.tsv index c731a7b2df..124d6ae7cf 100644 --- a/src/test/resources/cloudformation/getatt-attribute-gaps.tsv +++ b/src/test/resources/cloudformation/getatt-attribute-gaps.tsv @@ -46,5 +46,6 @@ AWS::RDS::DBInstance SecondaryAvailabilityZone multi-AZ secondary placement is n AWS::RDS::DBInstance StatusInfos read-replica status detail is not emulated AWS::Redshift::Cluster DeferMaintenanceIdentifier Floci does not emulate deferred maintenance windows AWS::Redshift::Cluster MasterPasswordSecretArn only set when ManageMasterPassword is true, which Floci rejects +AWS::Route53::RecordSet Id schema gives no description; Ref returns the record name and the legacy spec gives the type no attributes AWS::SQS::QueuePolicy Id schema gives no description; the policy has no backing entity to take an id from AWS::SecretsManager::Secret Id Fn::GetAtt Id on a secret is its ARN, which the provisioner already sets as the physical id and as Arn; closable, deferred to a follow-up PR to keep this one a pure move diff --git a/src/test/resources/cloudformation/supported-resource-types.tsv b/src/test/resources/cloudformation/supported-resource-types.tsv index 1af2a716d2..5ed7678bed 100644 --- a/src/test/resources/cloudformation/supported-resource-types.tsv +++ b/src/test/resources/cloudformation/supported-resource-types.tsv @@ -129,7 +129,7 @@ AWS::Redshift::ClusterParameterGroup RedshiftClusterCfnProvisioner AWS::Redshift::ClusterSecurityGroup RedshiftClusterCfnProvisioner AWS::Redshift::ClusterSubnetGroup RedshiftClusterCfnProvisioner AWS::Route53::HostedZone Route53CfnProvisioner -AWS::Route53::RecordSet LEGACY_SWITCH +AWS::Route53::RecordSet Route53CfnProvisioner AWS::S3::Bucket S3CfnProvisioner AWS::S3::BucketPolicy S3CfnProvisioner AWS::SNS::Subscription SnsCfnProvisioner