diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java index f48a2f3e87..0221f4790d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java @@ -4893,20 +4893,38 @@ public class S3Service implements Resettable, ResourceProvider { bucketStore.put(bucketName, bucket); } + /** + * What AWS reports for a bucket nobody configured: since April 2023 every new bucket starts with + * all four Block Public Access settings on. This is only what GetPublicAccessBlock answers. The + * bucket stores no configuration, so enforcement on the write paths is unchanged. + */ + static final String DEFAULT_PUBLIC_ACCESS_BLOCK_XML = new XmlBuilder() + .start("PublicAccessBlockConfiguration", AwsNamespaces.S3) + .elem("BlockPublicAcls", true) + .elem("IgnorePublicAcls", true) + .elem("BlockPublicPolicy", true) + .elem("RestrictPublicBuckets", true) + .end("PublicAccessBlockConfiguration") + .build(); + public String getPublicAccessBlock(String bucketName) { Bucket bucket = bucketStore.get(bucketName) .orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404)); - if (bucket.getPublicAccessBlockConfiguration() == null) { + if (bucket.getPublicAccessBlockConfiguration() != null) { + return bucket.getPublicAccessBlockConfiguration(); + } + if (bucket.isPublicAccessBlockDeleted()) { throw new AwsException("NoSuchPublicAccessBlockConfiguration", "The public access block configuration was not found", 404); } - return bucket.getPublicAccessBlockConfiguration(); + return DEFAULT_PUBLIC_ACCESS_BLOCK_XML; } public void putPublicAccessBlock(String bucketName, String xml) { Bucket bucket = bucketStore.get(bucketName) .orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404)); bucket.setPublicAccessBlockConfiguration(xml); + bucket.setPublicAccessBlockDeleted(false); bucketStore.put(bucketName, bucket); } @@ -4914,6 +4932,7 @@ public class S3Service implements Resettable, ResourceProvider { Bucket bucket = bucketStore.get(bucketName) .orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404)); bucket.setPublicAccessBlockConfiguration(null); + bucket.setPublicAccessBlockDeleted(true); bucketStore.put(bucketName, bucket); } diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/model/Bucket.java b/src/main/java/io/github/hectorvent/floci/services/s3/model/Bucket.java index aba414dd02..07c4842e41 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/model/Bucket.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/model/Bucket.java @@ -27,6 +27,7 @@ public class Bucket { private String encryptionConfiguration; // XML string private String replicationConfiguration; // XML string private String publicAccessBlockConfiguration; // XML string + private boolean publicAccessBlockDeleted; private String ownershipControlsConfiguration; // XML string private String requestPaymentPayer; // "BucketOwner" (default) or "Requester"; null until first PUT private String accelerateStatus; // "Enabled" or "Suspended"; null until first PUT @@ -113,6 +114,12 @@ public class Bucket { this.publicAccessBlockConfiguration = publicAccessBlockConfiguration; } + /** True once DeletePublicAccessBlock removed the configuration; a never-configured bucket is false. */ + public boolean isPublicAccessBlockDeleted() { return publicAccessBlockDeleted; } + public void setPublicAccessBlockDeleted(boolean publicAccessBlockDeleted) { + this.publicAccessBlockDeleted = publicAccessBlockDeleted; + } + public String getOwnershipControlsConfiguration() { return ownershipControlsConfiguration; } public void setOwnershipControlsConfiguration(String ownershipControlsConfiguration) { this.ownershipControlsConfiguration = ownershipControlsConfiguration; 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 index 44177f6e70..eeed3b154c 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationS3BucketPropertiesIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationS3BucketPropertiesIntegrationTest.java @@ -133,7 +133,9 @@ class CloudFormationS3BucketPropertiesIntegrationTest { 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); + // A bucket nobody configured reports the AWS default rather than a 404. + given().when().get("/" + bucket + "?publicAccessBlock").then().statusCode(200) + .body(containsString("true")); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3DefaultPublicAccessBlockTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3DefaultPublicAccessBlockTest.java new file mode 100644 index 0000000000..5398de57c8 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3DefaultPublicAccessBlockTest.java @@ -0,0 +1,78 @@ +package io.github.hectorvent.floci.services.s3; + +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.services.s3.model.Bucket; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * Since April 2023 AWS creates every bucket with all four Block Public Access settings on, so + * GetPublicAccessBlock on a bucket nobody configured returns them rather than a 404. Only a bucket + * whose configuration was deleted answers NoSuchPublicAccessBlockConfiguration. + */ +class S3DefaultPublicAccessBlockTest { + + @TempDir + Path tempDir; + + private S3Service s3Service; + + @BeforeEach + void setUp() { + s3Service = new S3Service(new InMemoryStorage<>(), new InMemoryStorage<>(), tempDir.resolve("s3"), false); + } + + @Test + void newBucketReportsAllFourSettingsBlocked() { + s3Service.createBucket("fresh", "us-east-1"); + + S3BlockPublicAccessSettings settings = S3BlockPublicAccessSettings.parse(s3Service.getPublicAccessBlock("fresh")); + + assertEquals(new S3BlockPublicAccessSettings(true, true, true, true), settings); + } + + @Test + void deletingTheConfigurationLeavesNothingToRead() { + s3Service.createBucket("deleted", "us-east-1"); + s3Service.deletePublicAccessBlock("deleted"); + + AwsException e = assertThrows(AwsException.class, () -> s3Service.getPublicAccessBlock("deleted")); + + assertEquals("NoSuchPublicAccessBlockConfiguration", e.getErrorCode()); + assertEquals(404, e.getHttpStatus()); + } + + @Test + void anExplicitConfigurationReplacesTheDefault() { + s3Service.createBucket("custom", "us-east-1"); + String xml = "false" + + "falsefalse" + + "false"; + s3Service.putPublicAccessBlock("custom", xml); + + assertEquals(xml, s3Service.getPublicAccessBlock("custom")); + } + + @Test + void theDefaultIsReportedNotStoredSoEnforcementIsUnchanged() { + Bucket bucket = s3Service.createBucket("report-only", "us-east-1"); + + assertEquals(null, bucket.getPublicAccessBlockConfiguration()); + } + + @Test + void repeatingCreateBucketKeepsAChangedConfiguration() { + s3Service.createBucket("again", "us-east-1"); + s3Service.deletePublicAccessBlock("again"); + s3Service.createBucket("again", "us-east-1"); + + assertThrows(AwsException.class, () -> s3Service.getPublicAccessBlock("again")); + } +}