mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
Migrates AWS::S3::Bucket and AWS::S3::BucketPolicy out of the switch. Behaviour-preserving: bodies are ported verbatim apart from the mechanical rewrites onto ProvisionContext, so the bucket name, the five Fn::GetAtt attributes and the CORS XML are unchanged. The CORS and versioning helpers move with the slice; all four of appendCorsRuleElements' call sites are inside applyBucketCorsConfiguration, so nothing is left behind. Unlike the leaf slice, s3Service stays on the monolith: Lambda code from an S3 location and the ApiGatewayV2 OpenAPI body both still read through it. Only the two switch arms and the provisioning methods leave. Delete keeps its shape. A bucket policy has nothing to remove, and a non-empty bucket still raises BucketNotEmpty so the stack reports DELETE_FAILED as AWS does, rather than reporting a clean delete.
This commit is contained in:
1 parent
26ae4ef78a
commit
8034c13106
4 files changed
+314
-83
No files matched your search
-81
@@ -271,8 +271,6 @@ public class CloudFormationResourceProvisioner {
|
||||
"AWS::RDS::DBSubnetGroup",
|
||||
"AWS::Route53::HostedZone",
|
||||
"AWS::Route53::RecordSet",
|
||||
"AWS::S3::Bucket",
|
||||
"AWS::S3::BucketPolicy",
|
||||
"AWS::SNS::Subscription",
|
||||
"AWS::SNS::Topic",
|
||||
"AWS::SecretsManager::Secret",
|
||||
@@ -425,7 +423,6 @@ public class CloudFormationResourceProvisioner {
|
||||
return resource;
|
||||
}
|
||||
switch (resourceType) {
|
||||
case "AWS::S3::Bucket" -> provisionS3Bucket(resource, properties, engine, region, accountId, stackName);
|
||||
case "AWS::SNS::Topic" -> provisionSnsTopic(resource, properties, engine, region, accountId, stackName);
|
||||
case "AWS::SNS::Subscription" -> provisionSnsSubscription(resource, properties, engine, region);
|
||||
case "AWS::DynamoDB::Table", "AWS::DynamoDB::GlobalTable" ->
|
||||
@@ -442,7 +439,6 @@ public class CloudFormationResourceProvisioner {
|
||||
case "AWS::SecretsManager::Secret" -> provisionSecret(resource, properties, engine, region, accountId, stackName);
|
||||
case "AWS::SecretsManager::SecretTargetAttachment" ->
|
||||
provisionSecretTargetAttachment(resource, properties, engine, region, stackName);
|
||||
case "AWS::S3::BucketPolicy" -> provisionS3BucketPolicy(resource, properties, engine);
|
||||
case "AWS::Route53::HostedZone" -> provisionRoute53HostedZone(resource, properties, engine);
|
||||
case "AWS::Route53::RecordSet" -> provisionRoute53RecordSet(resource, properties, engine);
|
||||
case "AWS::Events::Rule" -> provisionEventBridgeRule(resource, properties, engine, region, stackName);
|
||||
@@ -713,7 +709,6 @@ public class CloudFormationResourceProvisioner {
|
||||
return;
|
||||
}
|
||||
switch (resourceType) {
|
||||
case "AWS::S3::Bucket" -> s3Service.deleteBucket(physicalId);
|
||||
case "AWS::SNS::Topic" -> snsService.deleteTopic(physicalId, region);
|
||||
case "AWS::SNS::Subscription" -> snsService.unsubscribe(physicalId, region);
|
||||
case "AWS::DynamoDB::Table" -> deleteDynamoTableSafe(physicalId, region);
|
||||
@@ -775,23 +770,6 @@ public class CloudFormationResourceProvisioner {
|
||||
|
||||
// ── S3 ────────────────────────────────────────────────────────────────────
|
||||
|
||||
private void provisionS3Bucket(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
|
||||
String region, String accountId, String stackName) {
|
||||
String bucketName = resolveOptional(props, "BucketName", engine);
|
||||
if (bucketName == null || bucketName.isBlank()) {
|
||||
bucketName = generatePhysicalName(stackName, r.getLogicalId(), 63, true);
|
||||
}
|
||||
s3Service.createBucket(bucketName, region);
|
||||
applyBucketCorsConfiguration(bucketName, props, engine);
|
||||
applyBucketVersioningConfiguration(bucketName, props, engine);
|
||||
r.setPhysicalId(bucketName);
|
||||
r.getAttributes().put("Arn", AwsArnUtils.Arn.of("s3", "", "", bucketName).toString());
|
||||
r.getAttributes().put("DomainName", bucketName + ".s3.amazonaws.com");
|
||||
r.getAttributes().put("RegionalDomainName", bucketName + ".s3." + region + ".amazonaws.com");
|
||||
r.getAttributes().put("WebsiteURL", "http://" + bucketName + ".s3-website." + region + ".amazonaws.com");
|
||||
r.getAttributes().put("BucketName", bucketName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the optional {@code CorsConfiguration} property of {@code AWS::S3::Bucket} by translating
|
||||
* the CloudFormation {@code CorsRules} list into the S3 CORS XML document the bucket stores and
|
||||
@@ -801,61 +779,6 @@ public class CloudFormationResourceProvisioner {
|
||||
* absent or has no rules, any existing CORS configuration is cleared so the bucket matches the
|
||||
* template. Clearing is a harmless no-op on create since a freshly created bucket has none.
|
||||
*/
|
||||
private void applyBucketCorsConfiguration(String bucketName, JsonNode props,
|
||||
CloudFormationTemplateEngine engine) {
|
||||
JsonNode corsRules = null;
|
||||
if (props != null && props.has("CorsConfiguration") && !props.get("CorsConfiguration").isNull()) {
|
||||
corsRules = props.get("CorsConfiguration").get("CorsRules");
|
||||
}
|
||||
if (corsRules == null || !corsRules.isArray() || corsRules.isEmpty()) {
|
||||
s3Service.deleteBucketCors(bucketName);
|
||||
return;
|
||||
}
|
||||
XmlBuilder xml = new XmlBuilder().start("CORSConfiguration", AwsNamespaces.S3);
|
||||
for (JsonNode rule : corsRules) {
|
||||
xml.start("CORSRule");
|
||||
xml.elem("ID", resolveOptional(rule, "Id", engine));
|
||||
appendCorsRuleElements(xml, rule.get("AllowedHeaders"), "AllowedHeader", engine);
|
||||
appendCorsRuleElements(xml, rule.get("AllowedMethods"), "AllowedMethod", engine);
|
||||
appendCorsRuleElements(xml, rule.get("AllowedOrigins"), "AllowedOrigin", engine);
|
||||
appendCorsRuleElements(xml, rule.get("ExposedHeaders"), "ExposeHeader", engine);
|
||||
String maxAge = resolveOptional(rule, "MaxAge", engine);
|
||||
if (maxAge != null && !maxAge.isBlank()) {
|
||||
xml.elem("MaxAgeSeconds", maxAge);
|
||||
}
|
||||
xml.end("CORSRule");
|
||||
}
|
||||
xml.end("CORSConfiguration");
|
||||
s3Service.putBucketCors(bucketName, xml.build());
|
||||
}
|
||||
|
||||
private void applyBucketVersioningConfiguration(String bucketName, JsonNode props,
|
||||
CloudFormationTemplateEngine engine) {
|
||||
if (props == null || !props.has("VersioningConfiguration")
|
||||
|| props.get("VersioningConfiguration").isNull()) {
|
||||
return;
|
||||
}
|
||||
String status = resolveOptional(props.get("VersioningConfiguration"), "Status", engine);
|
||||
if (status != null && !status.isBlank()) {
|
||||
s3Service.putBucketVersioning(bucketName, status);
|
||||
}
|
||||
}
|
||||
|
||||
private void appendCorsRuleElements(XmlBuilder xml, JsonNode values, String elementName,
|
||||
CloudFormationTemplateEngine engine) {
|
||||
if (values == null || !values.isArray()) {
|
||||
return;
|
||||
}
|
||||
for (JsonNode value : values) {
|
||||
if (value != null && !value.isNull()) {
|
||||
String resolved = engine.resolve(value);
|
||||
if (resolved != null && !resolved.isBlank()) {
|
||||
xml.elem(elementName, resolved);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ── EC2 networking ─────────────────────────────────────────────────────────
|
||||
// Each method delegates to Ec2Service so the resource really exists (describe-subnets,
|
||||
@@ -5129,10 +5052,6 @@ public class CloudFormationResourceProvisioner {
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
private void provisionS3BucketPolicy(StackResource r, JsonNode props, CloudFormationTemplateEngine engine) {
|
||||
r.setPhysicalId("bucket-policy-" + UUID.randomUUID().toString().substring(0, 8));
|
||||
}
|
||||
|
||||
|
||||
private void provisionIamUser(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
|
||||
String stackName) {
|
||||
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsNamespaces;
|
||||
import io.github.hectorvent.floci.core.common.XmlBuilder;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Provisions {@code AWS::S3::Bucket} and {@code AWS::S3::BucketPolicy}.
|
||||
*
|
||||
* <p>The bucket policy is accepted and given a physical id but not enforced: Floci does not
|
||||
* evaluate S3 policies, and failing the resource would break every template that attaches one.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class S3CfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static final String BUCKET = "AWS::S3::Bucket";
|
||||
private static final String BUCKET_POLICY = "AWS::S3::BucketPolicy";
|
||||
private static final int BUCKET_NAME_MAX_LENGTH = 63;
|
||||
|
||||
private final S3Service s3Service;
|
||||
|
||||
public S3CfnProvisioner(S3Service s3Service) {
|
||||
this.s3Service = s3Service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> resourceTypes() {
|
||||
return Set.of(BUCKET, BUCKET_POLICY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void provision(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
switch (r.getResourceType()) {
|
||||
case BUCKET -> provisionBucket(r, props, ctx);
|
||||
case BUCKET_POLICY ->
|
||||
r.setPhysicalId("bucket-policy-" + UUID.randomUUID().toString().substring(0, 8));
|
||||
default -> throw new IllegalStateException(
|
||||
"S3CfnProvisioner cannot provision " + r.getResourceType());
|
||||
}
|
||||
}
|
||||
|
||||
private void provisionBucket(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
String bucketName = ctx.resolveOptional(props, "BucketName");
|
||||
if (bucketName == null || bucketName.isBlank()) {
|
||||
bucketName = ctx.generatePhysicalName(r.getLogicalId(), BUCKET_NAME_MAX_LENGTH, true);
|
||||
}
|
||||
s3Service.createBucket(bucketName, ctx.region());
|
||||
applyBucketCorsConfiguration(bucketName, props, ctx);
|
||||
applyBucketVersioningConfiguration(bucketName, props, ctx);
|
||||
r.setPhysicalId(bucketName);
|
||||
r.getAttributes().put("Arn", AwsArnUtils.Arn.of("s3", "", "", bucketName).toString());
|
||||
r.getAttributes().put("DomainName", bucketName + ".s3.amazonaws.com");
|
||||
r.getAttributes().put("RegionalDomainName", bucketName + ".s3." + ctx.region() + ".amazonaws.com");
|
||||
r.getAttributes().put("WebsiteURL",
|
||||
"http://" + bucketName + ".s3-website." + ctx.region() + ".amazonaws.com");
|
||||
r.getAttributes().put("BucketName", bucketName);
|
||||
}
|
||||
|
||||
private void applyBucketCorsConfiguration(String bucketName, JsonNode props, ProvisionContext ctx) {
|
||||
JsonNode corsRules = null;
|
||||
if (props != null && props.has("CorsConfiguration") && !props.get("CorsConfiguration").isNull()) {
|
||||
corsRules = props.get("CorsConfiguration").get("CorsRules");
|
||||
}
|
||||
if (corsRules == null || !corsRules.isArray() || corsRules.isEmpty()) {
|
||||
s3Service.deleteBucketCors(bucketName);
|
||||
return;
|
||||
}
|
||||
XmlBuilder xml = new XmlBuilder().start("CORSConfiguration", AwsNamespaces.S3);
|
||||
for (JsonNode rule : corsRules) {
|
||||
xml.start("CORSRule");
|
||||
xml.elem("ID", ctx.resolveOptional(rule, "Id"));
|
||||
appendCorsRuleElements(xml, rule.get("AllowedHeaders"), "AllowedHeader", ctx);
|
||||
appendCorsRuleElements(xml, rule.get("AllowedMethods"), "AllowedMethod", ctx);
|
||||
appendCorsRuleElements(xml, rule.get("AllowedOrigins"), "AllowedOrigin", ctx);
|
||||
appendCorsRuleElements(xml, rule.get("ExposedHeaders"), "ExposeHeader", ctx);
|
||||
String maxAge = ctx.resolveOptional(rule, "MaxAge");
|
||||
if (maxAge != null && !maxAge.isBlank()) {
|
||||
xml.elem("MaxAgeSeconds", maxAge);
|
||||
}
|
||||
xml.end("CORSRule");
|
||||
}
|
||||
xml.end("CORSConfiguration");
|
||||
s3Service.putBucketCors(bucketName, xml.build());
|
||||
}
|
||||
|
||||
private void appendCorsRuleElements(XmlBuilder xml, JsonNode values, String elementName,
|
||||
ProvisionContext ctx) {
|
||||
if (values == null || !values.isArray()) {
|
||||
return;
|
||||
}
|
||||
for (JsonNode value : values) {
|
||||
if (value != null && !value.isNull()) {
|
||||
String resolved = ctx.engine().resolve(value);
|
||||
if (resolved != null && !resolved.isBlank()) {
|
||||
xml.elem(elementName, resolved);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void applyBucketVersioningConfiguration(String bucketName, JsonNode props,
|
||||
ProvisionContext ctx) {
|
||||
if (props == null || !props.has("VersioningConfiguration")
|
||||
|| props.get("VersioningConfiguration").isNull()) {
|
||||
return;
|
||||
}
|
||||
String status = ctx.resolveOptional(props.get("VersioningConfiguration"), "Status");
|
||||
if (status != null && !status.isBlank()) {
|
||||
s3Service.putBucketVersioning(bucketName, status);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String resourceType, String physicalId, String region) {
|
||||
// A bucket policy has no backing resource to remove. Deleting a non-empty bucket raises
|
||||
// BucketNotEmpty, which propagates so the stack reports DELETE_FAILED as AWS does.
|
||||
if (BUCKET.equals(resourceType)) {
|
||||
s3Service.deleteBucket(physicalId);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user