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("Enabled")); + + bucketGet(bucket, "publicAccessBlock") + .body(containsString("true")) + .body(containsString("true")) + .body(containsString("true")) + .body(containsString("true")); + + bucketGet(bucket, "lifecycle") + .body(containsString("expire-superseded-record-versions")) + .body(containsString("Enabled")) + .body(containsString("30")) + .body(containsString("7")); + + bucketGet(bucket, "encryption") + .body(containsString("aws:kms")) + .body(containsString("arn:aws:kms:us-east-1:000000000000:key/abc")) + .body(containsString("true")); + + bucketGet(bucket, "tagging").body(containsString("purpose")).body(containsString("records")); + // Fn::Sub: ${!Name} is a literal, ${AWS::Region} a pseudo-parameter, and (in the policy + // below) ${recordsBucket.Arn} the GetAtt shorthand, which used to come back unresolved. + bucketGet(bucket, "tagging").body(containsString("${NotAVariable}-us-east-1")); + + // The policy is on the bucket, with its intrinsics resolved to the bucket's real ARN. + bucketGet(bucket, "policy") + .body(containsString("DenyInsecureTransport")) + .body(containsString("arn:aws:s3:::" + bucket + "/*")) + .body(not(containsString("Fn::"))); + } + + @Test + void aStackUpdateChangesTheLifecycleWindow() throws InterruptedException { + String suffix = Long.toString(System.nanoTime(), 36); + String bucket = "cfn-props-update-" + suffix; + String stackName = "cfn-props-update-stack-" + suffix; + String stackId = stack("CreateStack", stackName, template(bucket, 30)); + await(stackId, "CREATE_COMPLETE"); + + stack("UpdateStack", stackName, template(bucket, 45)); + await(stackId, "UPDATE_COMPLETE"); + + bucketGet(bucket, "lifecycle").body(containsString("45")); + bucketGet(bucket, "versioning").body(containsString("Enabled")); + } + + @Test + void aBucketDeclaringNoneOfItStaysBare() throws InterruptedException { + String suffix = Long.toString(System.nanoTime(), 36); + String bucket = "cfn-props-bare-" + suffix; + String stackId = stack("CreateStack", "cfn-props-bare-stack-" + suffix, """ + { "Resources": { "b": { "Type": "AWS::S3::Bucket", "Properties": { "BucketName": "%s" } } } } + """.formatted(bucket)); + await(stackId, "CREATE_COMPLETE"); + + given().when().get("/" + bucket + "?versioning").then().statusCode(200).body(not(containsString(""))); + given().when().get("/" + bucket + "?lifecycle").then().statusCode(404); + given().when().get("/" + bucket + "?publicAccessBlock").then().statusCode(404); + } + + @Test + void droppingThePolicyResourceTakesThePolicyOffTheBucket() throws InterruptedException { + String suffix = Long.toString(System.nanoTime(), 36); + String bucket = "cfn-props-drop-" + suffix; + String stackName = "cfn-props-drop-stack-" + suffix; + String stackId = stack("CreateStack", stackName, template(bucket, 30)); + await(stackId, "CREATE_COMPLETE"); + bucketGet(bucket, "policy").body(containsString("DenyInsecureTransport")); + + // The same template with the policy resource removed. CloudFormation deletes the dropped + // resource, which must clear the policy rather than report DELETE_COMPLETE and leave it. + stack("UpdateStack", stackName, """ + { "Resources": { "recordsBucket": { "Type": "AWS::S3::Bucket", + "Properties": { "BucketName": "%s" } } } } + """.formatted(bucket)); + await(stackId, "UPDATE_COMPLETE"); + + given().when().get("/" + bucket + "?policy").then().statusCode(404); + } + + @Test + void thePolicyResourceIsIdentifiedByItsBucket() throws InterruptedException { + String suffix = Long.toString(System.nanoTime(), 36); + String bucket = "cfn-props-ref-" + suffix; + String stackId = stack("CreateStack", "cfn-props-ref-stack-" + suffix, template(bucket, 30)); + await(stackId, "CREATE_COMPLETE"); + + // primaryIdentifier in the registry schema is /properties/Bucket, so that is the physical + // id, and it is what makes the policy findable at delete. + String xml = given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DescribeStackResources") + .formParam("StackName", stackId) + .when().post("/").then().statusCode(200).extract().asString(); + assertEquals(bucket, physicalIdOf(xml, "recordsBucketPolicy"), + "the policy's physical id is its bucket: " + xml); + } + + /** The PhysicalResourceId of one member of a DescribeStackResources response. */ + private static String physicalIdOf(String xml, String logicalId) { + String marker = "" + logicalId + ""; + int at = xml.indexOf(marker); + assertTrue(at > 0, logicalId + " is not in the stack: " + xml); + int memberStart = xml.lastIndexOf("", at); + int memberEnd = xml.indexOf("", at); + String member = xml.substring(memberStart, memberEnd); + int idStart = member.indexOf(""); + assertTrue(idStart > 0, logicalId + " has no physical id: " + member); + idStart += "".length(); + return member.substring(idStart, member.indexOf("", idStart)); + } + + private static io.restassured.response.ValidatableResponse bucketGet(String bucket, String subresource) { + return given().when().get("/" + bucket + "?" + subresource).then().statusCode(200); + } + + private static String stack(String action, String stackName, String template) { + String xml = given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", action) + .formParam("StackName", stackName) + .formParam("TemplateBody", template) + .when() + .post("/") + .then() + .statusCode(200) + .extract().asString(); + int start = xml.indexOf("") + "".length(); + return xml.substring(start, xml.indexOf("", start)); + } + + private static void await(String stackId, String status) throws InterruptedException { + String xml = ""; + long deadline = System.currentTimeMillis() + 10_000; + while (System.currentTimeMillis() < deadline) { + xml = given() + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "DescribeStacks") + .formParam("StackName", stackId) + .when().post("/").then().statusCode(200).extract().asString(); + if (xml.contains("" + status + "")) { + return; + } + if (xml.contains("_FAILED") || xml.contains("ROLLBACK_COMPLETE")) { + break; + } + Thread.sleep(50); + } + fail("stack " + stackId + " never reached " + status + ": " + xml); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/S3CfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/S3CfnProvisionerTest.java index 79ce0881f5..851d1b7f64 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/S3CfnProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/S3CfnProvisionerTest.java @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.cloudformation.provisioners; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine; import io.github.hectorvent.floci.services.cloudformation.model.StackResource; import io.github.hectorvent.floci.services.s3.S3Service; @@ -12,9 +13,11 @@ import org.mockito.ArgumentCaptor; import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -198,40 +201,69 @@ class S3CfnProvisionerTest { verify(s3, never()).putBucketVersioning("c", null); } + /** + * primaryIdentifier in the registry schema is /properties/Bucket, so the bucket name is the + * physical id. The type has no readOnlyProperties, so it has no Fn::GetAtt and the attribute + * map stays empty. + */ @Test - void bucketPolicyIsAcceptedWithAPhysicalIdAndNoAttributes() { + void aBucketPolicyIsIdentifiedByItsBucketAndCarriesNoAttributes() { StackResource r = resource("Policy", "AWS::S3::BucketPolicy"); provisioner.provision(r, props(""" {"Bucket": "b", "PolicyDocument": {"Version": "2012-10-17"}} """), ctx); - assertTrue(r.getPhysicalId().startsWith("bucket-policy-"), r.getPhysicalId()); + assertEquals("b", r.getPhysicalId()); assertTrue(r.getAttributes().isEmpty()); } /** - * 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 Ref returned. + * provision runs again on every UpdateStack. The id follows the bucket, which is create-only, + * so an unchanged policy keeps its id and does not look like a replaced resource. */ @Test void aBucketPolicyKeepsItsIdAcrossUpdates() { StackResource r = resource("Policy", "AWS::S3::BucketPolicy"); - r.setPhysicalId("bucket-policy-abc12345"); + r.setPhysicalId("b"); provisioner.provision(r, props(""" {"Bucket": "b", "PolicyDocument": {"Version": "2012-10-17"}} """), - new ProvisionContext(ctx.engine(), REGION, "000000000000", "my-stack", - "bucket-policy-abc12345")); + new ProvisionContext(ctx.engine(), REGION, "000000000000", "my-stack", "b")); - assertEquals("bucket-policy-abc12345", r.getPhysicalId()); + assertEquals("b", r.getPhysicalId()); } @Test - void deletingABucketReachesTheServiceButAPolicyHasNothingToDelete() { + void deletingABucketReachesTheService() { provisioner.delete("AWS::S3::Bucket", "b", REGION); - verify(s3).deleteBucket("b"); - provisioner.delete("AWS::S3::BucketPolicy", "bucket-policy-abc", REGION); - verify(s3, never()).deleteBucket("bucket-policy-abc"); + verify(s3).deleteBucket("b"); + verify(s3, never()).deleteBucketPolicy(anyString()); + } + + @Test + void deletingABucketPolicyClearsItFromTheBucketAndLeavesTheBucket() { + provisioner.delete("AWS::S3::BucketPolicy", "b", REGION); + + verify(s3).deleteBucketPolicy("b"); + verify(s3, never()).deleteBucket(anyString()); + } + + /** The bucket in the same stack may be deleted first, leaving no policy to clear. */ + @Test + void deletingTheBucketPolicyOfAVanishedBucketIsTolerated() { + doThrow(new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404)) + .when(s3).deleteBucketPolicy("gone"); + + provisioner.delete("AWS::S3::BucketPolicy", "gone", REGION); + + verify(s3).deleteBucketPolicy("gone"); + } + + @Test + void aBucketPolicyDeleteThatFailsForAnyOtherReasonPropagates() { + doThrow(new AwsException("AccessDenied", "nope", 403)).when(s3).deleteBucketPolicy("b"); + + assertThrows(AwsException.class, () -> provisioner.delete("AWS::S3::BucketPolicy", "b", REGION)); } } diff --git a/tools/docs/cfn_resource_types.yaml b/tools/docs/cfn_resource_types.yaml index 0d7cf7086d..ba37eefb9d 100644 --- a/tools/docs/cfn_resource_types.yaml +++ b/tools/docs/cfn_resource_types.yaml @@ -149,7 +149,7 @@ service_labels: # Parenthetical shown after a type name. Keyed by the full type. notes: - AWS::S3::BucketPolicy: accepted; policy not enforced + AWS::S3::BucketPolicy: document stored on the bucket; S3 does not evaluate it AWS::SQS::QueuePolicy: accepted; policy not enforced AWS::Lambda::Function: Zip via S3/inline `ZipFile`, and Image AWS::Lambda::EventSourceMapping: SQS, Kinesis, DynamoDB Streams