From 28ca34ad6c2b10e609a656cb781000a7dced97e7 Mon Sep 17 00:00:00 2001 From: lex00 <121451605+lex00@users.noreply.github.com> Date: Mon, 21 Sep 2026 21:27:54 -0600 Subject: [PATCH] fix(cloudformation): apply an S3 bucket's declared configuration and its policy (#3950) S3CfnProvisioner translated CorsConfiguration and VersioningConfiguration and nothing else, and AWS::S3::BucketPolicy took a physical id and stopped, so a stack reported CREATE_COMPLETE for a bucket with no lifecycle, no public-access block, no default encryption, no tags and no policy. PublicAccessBlockConfiguration, LifecycleConfiguration, BucketEncryption and Tags are now translated into the XML their S3 API calls take and handed to the same service methods, so they are validated and stored exactly as a direct PutBucket* would be. The policy document has its intrinsics resolved and is put on the bucket. The policy's physical id is the bucket name, as the registry schema's primaryIdentifier declares, and deleting the resource removes the policy, tolerating a bucket the stack already deleted. The class javadoc names the properties that are covered and those still dropped. Closes #3947 --- docs/services/cloudformation.md | 2 +- .../provisioners/S3CfnProvisioner.java | 265 ++++++++++++++++-- ...tionS3BucketPropertiesIntegrationTest.java | 229 +++++++++++++++ .../provisioners/S3CfnProvisionerTest.java | 56 +++- tools/docs/cfn_resource_types.yaml | 2 +- 5 files changed, 516 insertions(+), 38 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationS3BucketPropertiesIntegrationTest.java diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.md index c36791a1b5..d09a4eb8e7 100644 --- a/docs/services/cloudformation.md +++ b/docs/services/cloudformation.md @@ -124,7 +124,7 @@ cross-resource references. | Service | Resource types | |---|---| -| S3 | `Bucket`, `BucketPolicy` (accepted; policy not enforced) | +| S3 | `Bucket`, `BucketPolicy` (document stored on the bucket; S3 does not evaluate it) | | SQS | `Queue`, `QueuePolicy` (accepted; policy not enforced) | | SNS | `Topic`, `Subscription`, `TopicPolicy` | | DynamoDB | `Table`, `GlobalTable` | diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/S3CfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/S3CfnProvisioner.java index 90d01327db..dc8109a37e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/S3CfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/S3CfnProvisioner.java @@ -2,24 +2,45 @@ 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.AwsException; 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 org.jboss.logging.Logger; +import java.util.List; import java.util.Set; -import java.util.UUID; /** * Provisions {@code AWS::S3::Bucket} and {@code AWS::S3::BucketPolicy}. * - *
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. + *
A property this class covers is translated into the XML its own S3 API call takes and handed + * to the same service method, so the service validates and stores it exactly as it would a direct + * {@code PutBucket*} request. A property the template does not declare is left alone. + * + *
Covered on {@code AWS::S3::Bucket}: {@code BucketName}, {@code CorsConfiguration}, + * {@code VersioningConfiguration}, {@code PublicAccessBlockConfiguration}, + * {@code BucketEncryption}, {@code LifecycleConfiguration} and {@code Tags}. + * + *
Silently dropped, each of which has a service method waiting for it: + * {@code NotificationConfiguration}, {@code WebsiteConfiguration}, {@code LoggingConfiguration}, + * {@code ObjectLockConfiguration}, {@code OwnershipControls}, {@code ReplicationConfiguration}, + * {@code AccelerateConfiguration}, {@code AccessControl}, and the four numbered configuration + * lists ({@code AnalyticsConfigurations}, {@code IntelligentTieringConfigurations}, + * {@code InventoryConfigurations}, {@code MetricsConfigurations}). Declaring one of these still + * reports CREATE_COMPLETE for a bucket that does not carry it. + * + *
{@code WebsiteURL} is reported whether or not website hosting is configured. That is not a + * gap: the registry schema lists it under {@code readOnlyProperties} and AWS returns the URL for + * every bucket, hosting or no hosting. */ @ApplicationScoped public class S3CfnProvisioner implements CfnResourceProvisioner { + private static final Logger LOG = Logger.getLogger(S3CfnProvisioner.class); + 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; @@ -39,30 +60,12 @@ public class S3CfnProvisioner implements CfnResourceProvisioner { public void provision(StackResource r, JsonNode props, ProvisionContext ctx) { switch (r.getResourceType()) { case BUCKET -> provisionBucket(r, props, ctx); - case BUCKET_POLICY -> r.setPhysicalId(bucketPolicyId(ctx)); + case BUCKET_POLICY -> provisionBucketPolicy(r, props, ctx); default -> throw new IllegalStateException( "S3CfnProvisioner cannot provision " + r.getResourceType()); } } - /** - * The policy's physical id, kept across updates rather than regenerated. - * - *
{@code provision} runs again on every UpdateStack, so minting a fresh id each time made an - * unchanged policy look like a replaced resource and changed what {@code Ref} returned. - * - *
The generated value itself is left alone deliberately. The sources disagree on what it - * should be: the current registry schema gives {@code primaryIdentifier} as - * {@code /properties/Bucket}, while the older schema localstack embeds gives - * {@code /properties/Id} as an md5 of the policy document. Changing what Ref resolves to on that - * evidence would be guessing; keeping the id stable fixes the defect either way. - */ - private String bucketPolicyId(ProvisionContext ctx) { - return ctx.isUpdate() - ? ctx.priorPhysicalId() - : "bucket-policy-" + UUID.randomUUID().toString().substring(0, 8); - } - private void provisionBucket(StackResource r, JsonNode props, ProvisionContext ctx) { String bucketName = ctx.stablePhysicalName(ctx.resolveOptional(props, "BucketName"), r.getLogicalId(), BUCKET_NAME_MAX_LENGTH, true); @@ -76,6 +79,10 @@ public class S3CfnProvisioner implements CfnResourceProvisioner { } applyBucketCorsConfiguration(bucketName, props, ctx); applyBucketVersioningConfiguration(bucketName, props, ctx); + applyPublicAccessBlockConfiguration(bucketName, props, ctx); + applyBucketEncryption(bucketName, props, ctx); + applyLifecycleConfiguration(bucketName, props, ctx); + applyBucketTags(bucketName, props, ctx); r.setPhysicalId(bucketName); r.getAttributes().put("Arn", AwsArnUtils.Arn.of("s3", "", "", bucketName).toString()); r.getAttributes().put("DomainName", bucketName + ".s3.amazonaws.com"); @@ -141,12 +148,222 @@ public class S3CfnProvisioner implements CfnResourceProvisioner { } } + private void applyPublicAccessBlockConfiguration(String bucketName, JsonNode props, ProvisionContext ctx) { + JsonNode pab = declared(props, "PublicAccessBlockConfiguration"); + if (pab == null) { + return; + } + XmlBuilder xml = new XmlBuilder().start("PublicAccessBlockConfiguration", AwsNamespaces.S3); + for (String flag : List.of("BlockPublicAcls", "IgnorePublicAcls", "BlockPublicPolicy", + "RestrictPublicBuckets")) { + appendResolved(xml, flag, pab, flag, ctx); + } + s3Service.putPublicAccessBlock(bucketName, xml.end("PublicAccessBlockConfiguration").build()); + } + + private void applyBucketEncryption(String bucketName, JsonNode props, ProvisionContext ctx) { + JsonNode encryption = declared(props, "BucketEncryption"); + if (encryption == null || !encryption.has("ServerSideEncryptionConfiguration")) { + return; + } + XmlBuilder xml = new XmlBuilder().start("ServerSideEncryptionConfiguration", AwsNamespaces.S3); + for (JsonNode rule : encryption.get("ServerSideEncryptionConfiguration")) { + xml.start("Rule"); + JsonNode byDefault = rule.get("ServerSideEncryptionByDefault"); + if (byDefault != null && !byDefault.isNull()) { + xml.start("ApplyServerSideEncryptionByDefault"); + appendResolved(xml, "SSEAlgorithm", byDefault, "SSEAlgorithm", ctx); + appendResolved(xml, "KMSMasterKeyID", byDefault, "KMSMasterKeyID", ctx); + xml.end("ApplyServerSideEncryptionByDefault"); + } + appendResolved(xml, "BucketKeyEnabled", rule, "BucketKeyEnabled", ctx); + xml.end("Rule"); + } + s3Service.putBucketEncryption(bucketName, xml.end("ServerSideEncryptionConfiguration").build()); + } + + private void applyLifecycleConfiguration(String bucketName, JsonNode props, ProvisionContext ctx) { + JsonNode lifecycle = declared(props, "LifecycleConfiguration"); + if (lifecycle == null || !lifecycle.has("Rules")) { + return; + } + s3Service.putBucketLifecycle(bucketName, lifecycleXml(lifecycle.get("Rules"), ctx), null); + } + + private void applyBucketTags(String bucketName, JsonNode props, ProvisionContext ctx) { + if (declared(props, "Tags") == null) { + return; + } + s3Service.putBucketTagging(bucketName, ctx.resolveTags(props, "Tags")); + } + + /** CloudFormation's lifecycle rule property names, spelled as the S3 API's XML. */ + private String lifecycleXml(JsonNode rules, ProvisionContext ctx) { + XmlBuilder xml = new XmlBuilder().start("LifecycleConfiguration", AwsNamespaces.S3); + for (JsonNode rule : rules) { + xml.start("Rule"); + appendResolved(xml, "ID", rule, "Id", ctx); + appendResolved(xml, "Status", rule, "Status", ctx); + + // S3 requires a Filter (or the deprecated top-level Prefix) on every rule; a rule that + // declares neither applies to the whole bucket, which is an empty prefix filter. + String prefix = ctx.resolveOptional(rule, "Prefix"); + JsonNode tagFilters = rule.get("TagFilters"); + boolean hasTags = tagFilters != null && tagFilters.isArray() && !tagFilters.isEmpty(); + xml.start("Filter"); + if (hasTags && (tagFilters.size() > 1 || (prefix != null && !prefix.isEmpty()))) { + xml.start("And"); + if (prefix != null && !prefix.isEmpty()) { + xml.elem("Prefix", prefix); + } + for (JsonNode tag : tagFilters) { + appendLifecycleTag(xml, tag, ctx); + } + xml.end("And"); + } else if (hasTags) { + appendLifecycleTag(xml, tagFilters.get(0), ctx); + } else { + xml.elem("Prefix", prefix == null ? "" : prefix); + } + xml.end("Filter"); + + String expirationDays = ctx.resolveOptional(rule, "ExpirationInDays"); + String expirationDate = ctx.resolveOptional(rule, "ExpirationDate"); + String deleteMarker = ctx.resolveOptional(rule, "ExpiredObjectDeleteMarker"); + if (isSet(expirationDays) || isSet(expirationDate) || isSet(deleteMarker)) { + xml.start("Expiration"); + if (isSet(expirationDays)) { + xml.elem("Days", expirationDays); + } + if (isSet(expirationDate)) { + xml.elem("Date", expirationDate); + } + if (isSet(deleteMarker)) { + xml.elem("ExpiredObjectDeleteMarker", deleteMarker); + } + xml.end("Expiration"); + } + JsonNode noncurrent = rule.get("NoncurrentVersionExpiration"); + if (noncurrent != null && !noncurrent.isNull()) { + xml.start("NoncurrentVersionExpiration"); + appendResolved(xml, "NoncurrentDays", noncurrent, "NoncurrentDays", ctx); + appendResolved(xml, "NewerNoncurrentVersions", noncurrent, "NewerNoncurrentVersions", ctx); + xml.end("NoncurrentVersionExpiration"); + } + JsonNode abort = rule.get("AbortIncompleteMultipartUpload"); + if (abort != null && !abort.isNull()) { + xml.start("AbortIncompleteMultipartUpload"); + appendResolved(xml, "DaysAfterInitiation", abort, "DaysAfterInitiation", ctx); + xml.end("AbortIncompleteMultipartUpload"); + } + appendLifecycleTransitions(xml, rule.get("Transitions"), "Transition", "Days", ctx); + appendLifecycleTransitions(xml, rule.get("NoncurrentVersionTransitions"), + "NoncurrentVersionTransition", "NoncurrentDays", ctx); + xml.end("Rule"); + } + return xml.end("LifecycleConfiguration").build(); + } + + private void appendLifecycleTransitions(XmlBuilder xml, JsonNode transitions, String element, + String daysElement, ProvisionContext ctx) { + if (transitions == null || !transitions.isArray()) { + return; + } + for (JsonNode transition : transitions) { + xml.start(element); + appendResolved(xml, daysElement, transition, "TransitionInDays", ctx); + appendResolved(xml, "StorageClass", transition, "StorageClass", ctx); + xml.end(element); + } + } + + private void appendLifecycleTag(XmlBuilder xml, JsonNode tag, ProvisionContext ctx) { + xml.start("Tag"); + appendResolved(xml, "Key", tag, "Key", ctx); + appendResolved(xml, "Value", tag, "Value", ctx); + xml.end("Tag"); + } + + private void appendResolved(XmlBuilder xml, String element, JsonNode props, String property, + ProvisionContext ctx) { + String value = ctx.resolveOptional(props, property); + if (isSet(value)) { + xml.elem(element, value); + } + } + + /** The named property when the template declares it with a value, null otherwise. */ + private static JsonNode declared(JsonNode props, String name) { + if (props == null || !props.has(name) || props.get(name).isNull()) { + return null; + } + return props.get(name); + } + + private static boolean isSet(String value) { + return value != null && !value.isBlank(); + } + + /** + * Puts the declared document on the bucket. Before this the policy resource took a physical id + * and stopped, so a stack reported CREATE_COMPLETE for a bucket that carried no policy at all. + * + *
The physical id is the bucket name, which is what the registry schema declares: + * {@code primaryIdentifier} is {@code /properties/Bucket}, and {@code Bucket} is create-only, + * so a template that repoints the policy replaces the resource and the id follows. The type has + * no {@code readOnlyProperties}, so it has no {@code Fn::GetAtt} and none is invented here. + */ + private void provisionBucketPolicy(StackResource r, JsonNode props, ProvisionContext ctx) { + String bucketName = ctx.resolveOptional(props, "Bucket"); + JsonNode document = declared(props, "PolicyDocument"); + if (bucketName == null || bucketName.isBlank()) { + r.setPhysicalId(ctx.isUpdate() ? ctx.priorPhysicalId() : r.getLogicalId()); + return; + } + r.setPhysicalId(bucketName); + if (document == null) { + return; + } + // A document given as a JSON string is already the policy; one given as an object may + // carry Ref / Fn::GetAtt / Fn::Sub anywhere inside it. + String policy = document.isTextual() + ? document.asText() + : ctx.engine().resolveNode(document).toString(); + s3Service.putBucketPolicy(bucketName, policy); + } + @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)) { + // Deleting a non-empty bucket raises BucketNotEmpty, which propagates so the stack + // reports DELETE_FAILED as AWS does. s3Service.deleteBucket(physicalId); + return; + } + if (BUCKET_POLICY.equals(resourceType)) { + deleteBucketPolicyIfPresent(physicalId); + } + } + + /** + * Clears the policy the stack put on the bucket. AWS removes it on stack delete unless the + * resource carries {@code DeletionPolicy: Retain}, and leaving it behind is how a dropped + * policy resource reported DELETE_COMPLETE while the bucket kept its policy. + * + *
A bucket that is already gone is tolerated: the bucket resource in the same stack may be + * deleted first, and there is then nothing left to clear. + */ + private void deleteBucketPolicyIfPresent(String bucketName) { + if (bucketName == null || bucketName.isBlank()) { + return; + } + try { + s3Service.deleteBucketPolicy(bucketName); + } catch (AwsException e) { + if (!"NoSuchBucket".equals(e.getErrorCode())) { + throw e; + } + LOG.debugv("Bucket {0} is already gone, so its policy needs no removal", bucketName); } } } diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationS3BucketPropertiesIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationS3BucketPropertiesIntegrationTest.java new file mode 100644 index 0000000000..44177f6e70 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationS3BucketPropertiesIntegrationTest.java @@ -0,0 +1,229 @@ +package io.github.hectorvent.floci.services.cloudformation; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * An {@code AWS::S3::Bucket} created through CloudFormation carries the configuration its template + * declares, and an {@code AWS::S3::BucketPolicy} puts its document on the bucket. + * + *
Before this, {@code S3CfnProvisioner} translated {@code CorsConfiguration} and
+ * {@code VersioningConfiguration} and nothing else, and the policy resource took a physical id and
+ * stopped, so a stack reported {@code CREATE_COMPLETE} for a bucket with no lifecycle, no
+ * public-access block, no default encryption, no tags and no policy. The template below is the
+ * shape a bootstrap stack deploys: a versioned bucket, locked down, with a transport policy.
+ */
+@QuarkusTest
+class CloudFormationS3BucketPropertiesIntegrationTest {
+
+ private static String template(String bucket, int noncurrentDays) {
+ return """
+ {
+ "Resources": {
+ "recordsBucket": {
+ "Type": "AWS::S3::Bucket",
+ "Properties": {
+ "BucketName": "%s",
+ "VersioningConfiguration": { "Status": "Enabled" },
+ "PublicAccessBlockConfiguration": {
+ "BlockPublicAcls": true, "BlockPublicPolicy": true,
+ "IgnorePublicAcls": true, "RestrictPublicBuckets": true
+ },
+ "LifecycleConfiguration": { "Rules": [ {
+ "Id": "expire-superseded-record-versions",
+ "Status": "Enabled",
+ "NoncurrentVersionExpiration": { "NoncurrentDays": %d },
+ "AbortIncompleteMultipartUpload": { "DaysAfterInitiation": 7 }
+ } ] },
+ "BucketEncryption": { "ServerSideEncryptionConfiguration": [ {
+ "ServerSideEncryptionByDefault": { "SSEAlgorithm": "aws:kms", "KMSMasterKeyID": "arn:aws:kms:us-east-1:000000000000:key/abc" },
+ "BucketKeyEnabled": true
+ } ] },
+ "Tags": [ { "Key": "purpose", "Value": "records" },
+ { "Key": "escaped", "Value": { "Fn::Sub": "${!NotAVariable}-${AWS::Region}" } } ]
+ }
+ },
+ "recordsBucketPolicy": {
+ "Type": "AWS::S3::BucketPolicy",
+ "Properties": {
+ "Bucket": { "Ref": "recordsBucket" },
+ "PolicyDocument": {
+ "Version": "2012-10-17",
+ "Statement": [ {
+ "Sid": "DenyInsecureTransport", "Effect": "Deny", "Principal": "*", "Action": "s3:*",
+ "Resource": [ { "Fn::GetAtt": ["recordsBucket", "Arn"] }, { "Fn::Sub": "${recordsBucket.Arn}/*" } ],
+ "Condition": { "Bool": { "aws:SecureTransport": "false" } }
+ } ]
+ }
+ }
+ }
+ }
+ }
+ """.formatted(bucket, noncurrentDays);
+ }
+
+ @Test
+ void theBucketCarriesWhatItsTemplateDeclares() throws InterruptedException {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String bucket = "cfn-props-bucket-" + suffix;
+ String stackId = stack("CreateStack", "cfn-props-stack-" + suffix, template(bucket, 30));
+ await(stackId, "CREATE_COMPLETE");
+
+ bucketGet(bucket, "versioning").body(containsString("