Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): enforce ownership controls and restore CFN updates (#5712)
XuJiana4eb854614bc
diff --git a/docs/services/s3.md b/docs/services/s3.mdindex fa633f3098..dafc7e9c77 100644--- a/docs/services/s3.md+++ b/docs/services/s3.md@@ -341,6 +341,8 @@ When a request is denied by bucket policy enforcement, Floci returns HTTP 403 wiFloci enforces all four Block Public Access settings. A bucket's configuration is combined with the bucket owner account's and the most restrictive of the two wins, which for four independent booleans is a per-flag OR: a flag set at either level is in force.+With `BucketOwnerEnforced` object ownership, ACL writes are rejected and existing object ACL grants no longer provide access. Before enabling it, the bucket ACL must grant access only to the owner; otherwise `PutBucketOwnershipControls` returns `InvalidBucketAclWithObjectOwnership` (400).+| Setting | What it does | Needs `enforce-auth` ||---|---|---|| `BlockPublicPolicy` | `PutBucketPolicy` returns `AccessDenied` (403) when the submitted policy is public | No |diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.javaindex 59614b62fc..1171e41212 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/CfnRollback.java@@ -87,6 +87,7 @@ public final class CfnRollback {* {@code rollbackUpdate}.*/public static final String DASHBOARD_UPDATE_SNAPSHOT_ATTR = "__FlociDashboardUpdateSnapshot";+ public static final String S3_BUCKET_UPDATE_SNAPSHOT_ATTR = "__FlociS3BucketUpdateSnapshot";/*** Holds the description, event pattern and retention an EventBridge archive carried before andiff --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.javaindex b79b12afc0..15af61ffdf 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@@ -26,19 +26,20 @@ import java.util.Set;* Provisions {@code AWS::S3::Bucket} and {@code AWS::S3::BucketPolicy}.** <p>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, except that- * an update removing {@code NotificationConfiguration} clears it.+ * to the same S3 service method used by a direct {@code PutBucket*} request. Ownership controls+ * are validated here before the service stores their XML. A property the template does not declare is left alone, except that+ * an update removing {@code NotificationConfiguration} or {@code OwnershipControls} clears it.** <p>Covered on {@code AWS::S3::Bucket}: {@code BucketName}, {@code CorsConfiguration},* {@code VersioningConfiguration}, {@code PublicAccessBlockConfiguration},- * {@code BucketEncryption}, {@code LifecycleConfiguration}, {@code NotificationConfiguration} and+ * {@code BucketEncryption}, {@code LifecycleConfiguration},+ * {@code OwnershipControls}, {@code NotificationConfiguration} and* {@code Tags}. {@code NotificationConfiguration} is the exception to the XML translation: its* service method takes the parsed model, so the provisioner builds that model instead.** <p><b>Silently dropped</b>, each of which has a service method waiting for it:* {@code WebsiteConfiguration}, {@code LoggingConfiguration},- * {@code ObjectLockConfiguration}, {@code OwnershipControls}, {@code ReplicationConfiguration},+ * {@code ObjectLockConfiguration}, {@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@@ -87,9 +88,15 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {// and only reconcile the bucket's configuration. A replacing update derives a different// name and still creates, hence reusesPriorEntity rather than isUpdate.Unwind unwind = new Unwind();+ r.getAttributes().remove(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR);try {// Built before anything is created, so a configuration that fails validation creates nothing.NotificationConfiguration notifications = notificationConfiguration(props, ctx);+ String ownershipControlsXml = ownershipControlsXml(props, ctx);+ if (ctx.reusesPriorEntity(bucketName)) {+ r.getAttributes().put(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR,+ s3Service.snapshotBucketConfiguration(bucketName));+ }if (!ctx.reusesPriorEntity(bucketName)) {// In us-east-1 CreateBucket also answers for a bucket the account already owns,// which this resource must never delete.@@ -113,6 +120,7 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {applyBucketEncryption(bucketName, props, ctx);applyLifecycleConfiguration(bucketName, props, ctx);applyBucketTags(bucketName, props, ctx);+ applyBucketOwnershipControls(bucketName, ownershipControlsXml, ctx);} catch (RuntimeException failure) {unwind(r, bucketName, unwind, ctx, failure);throw failure;@@ -228,6 +236,40 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {s3Service.putBucketLifecycle(bucketName, lifecycleXml(lifecycle.get("Rules"), ctx), null);}+ private String ownershipControlsXml(JsonNode props, ProvisionContext ctx) {+ JsonNode declaredNode = declared(props, "OwnershipControls");+ if (declaredNode == null) {+ return null;+ }+ JsonNode controls = ctx.engine().resolveNodeOmittingNoValue(declaredNode);+ if (controls == null || controls.isMissingNode() || controls.isNull()) {+ return null;+ }+ String path = "#/OwnershipControls";+ requireObject(controls, path);+ JsonNode rules = declaredArray(controls, "Rules", path);+ XmlBuilder xml = new XmlBuilder().start("OwnershipControls", AwsNamespaces.S3);+ for (int index = 0; rules != null && index < rules.size(); index++) {+ String rulePath = path + "/Rules/" + index;+ JsonNode rule = requireObject(rules.get(index), rulePath);+ String ownership = required(rule, "ObjectOwnership", rulePath);+ xml.start("Rule");+ xml.elem("ObjectOwnership", ownership);+ xml.end("Rule");+ }+ String document = xml.end("OwnershipControls").build();+ S3Service.validateOwnershipControls(document);+ return document;+ }++ private void applyBucketOwnershipControls(String bucketName, String ownershipControlsXml,+ ProvisionContext ctx) {+ if (ownershipControlsXml != null) {+ s3Service.putBucketOwnershipControls(bucketName, ownershipControlsXml);+ } else if (ctx.reusesPriorEntity(bucketName) && ctx.previouslyDeclared("OwnershipControls")) {+ s3Service.deleteBucketOwnershipControls(bucketName);+ }+ }private void applyBucketTags(String bucketName, JsonNode props, ProvisionContext ctx) {if (declared(props, "Tags") == null) {return;@@ -241,19 +283,14 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {private NotificationConfiguration replacedNotifications;}- /**- * Undoes what a failed provision changed. On a failed update the engine restores the previous- * resource record and reports it rolled back unless the provisioner records why it could not- * be, so the bucket itself has to be put back: one this attempt created is deleted, and a- * notification configuration it replaced is put back. The other properties have no restore;- * the notification configuration goes ahead of them, so a destination S3 rejects leaves them- * untouched.- */+ /** Restores an in-place bucket update or removes a newly created bucket after a failed provision. */private void unwind(StackResource r, String bucketName, Unwind unwind, ProvisionContext ctx,RuntimeException failure) {try {if (unwind.created) {s3Service.deleteBucket(bucketName);+ } else if (r.getAttributes().containsKey(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR)) {+ rollbackUpdate(r);} else if (unwind.replacedNotifications != null) {// Valid when it was put; its destinations are not checked again on the way back.s3Service.putBucketNotificationConfiguration(bucketName, unwind.replacedNotifications, true);@@ -268,6 +305,32 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {}}+ @Override+ public boolean rollbackUpdate(StackResource resource) {+ if (!BUCKET.equals(resource.getResourceType())) {+ return false;+ }+ String snapshot = resource.getAttributes().get(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR);+ if (snapshot == null) {+ return false;+ }+ s3Service.restoreBucketConfiguration(resource.getPhysicalId(), snapshot);+ resource.getAttributes().remove(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR);+ return true;+ }++ @Override+ public UpdateCleanupResult completeUpdate(StackResource resource) {+ return resource.getAttributes().containsKey(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR)+ ? new UpdateCleanupResult(true, true, null, 0, null)+ : UpdateCleanupResult.notApplicable();+ }++ @Override+ public void clearUpdate(StackResource resource) {+ resource.getAttributes().remove(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR);+ }+private static boolean sameConfiguration(NotificationConfiguration desired, NotificationConfiguration current) {return desired.getQueueConfigurations().equals(current.getQueueConfigurations())&& desired.getTopicConfigurations().equals(current.getTopicConfigurations())diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3AclPublicAccessEvaluator.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3AclPublicAccessEvaluator.javaindex e343a420cf..0eb988e083 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3AclPublicAccessEvaluator.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3AclPublicAccessEvaluator.java@@ -54,4 +54,18 @@ final class S3AclPublicAccessEvaluator {return false;}}++ static boolean aclGrantsNonOwner(String acl, String ownerId) {+ if (acl == null || acl.isBlank()) {+ return false;+ }+ try {+ S3AclPolicy policy = S3AclPolicy.parse(acl);+ return policy.grants().isEmpty() || policy.grants().stream()+ .anyMatch(grant -> !grant.grantee().isCanonicalUser(ownerId));+ } catch (S3AclPolicy.AclParseException e) {+ LOG.debugv(e, "Failed to parse S3 ACL for ownership enforcement");+ return true;+ }+ }}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.javaindex 98c2a9ed07..b9450d4bfd 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@@ -1,6 +1,8 @@package io.github.hectorvent.floci.services.s3;+import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.core.type.TypeReference;+import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.config.EmulatorConfig;@@ -113,6 +115,8 @@ public class S3Service implements Resettable, ResourceProvider {private static final String AUTHENTICATED_USERS_GROUP_URI = "http://acs.amazonaws.com/groups/global/AuthenticatedUsers";private static final String LOG_DELIVERY_GROUP_URI = "http://acs.amazonaws.com/groups/s3/LogDelivery";private static final String LEGACY_ACCESS_KEY_ID = "test";+ private static final String S3_XML_SCHEMA_ERROR =+ "The XML you provided was not well-formed or did not validate against our published schema.";private static final Set<String> SUPPORTED_SERVER_SIDE_ENCRYPTION_VALUES = Set.of("AES256", "aws:kms", "aws:kms:dsse", "aws:fsx");private static final String SSE_C_ALGORITHM = "AES256";private static final int SSE_C_KEY_BYTES = 32;@@ -644,6 +648,7 @@ public class S3Service implements Resettable, ResourceProvider {AccountAwareStorageBackend.OwnedEntry<Bucket> checkedBucket = resolveBucketEntry(bucketName).orElseThrow(() -> new AwsException("NoSuchBucket","The specified bucket does not exist.", 404));+ rejectUploadAclWhenOwnershipEnforced(checkedBucket.value(), checkedBucket.account(), effectiveOptions);rejectConflictingServerSideEncryption(normalizeServerSideEncryption(effectiveOptions.getServerSideEncryption()),validateSseCustomerKey(effectiveOptions.getSseCustomerAlgorithm(),@@ -825,6 +830,7 @@ public class S3Service implements Resettable, ResourceProvider {object.setSseCustomerAlgorithm(sseCustomerKey.algorithm());object.setSseCustomerKeyMd5(sseCustomerKey.keyMd5());}+ rejectUploadAclWhenOwnershipEnforced(bucket, bucketOwnerAccount, effectiveOptions);String objectAcl = resolveObjectAclXml(bucketOwnerAccount,effectiveOptions.getAcl(), effectiveOptions.getGrantRead(),effectiveOptions.getGrantWrite(), effectiveOptions.getGrantFullControl(),@@ -1134,7 +1140,7 @@ public class S3Service implements Resettable, ResourceProvider {throw new AwsException("AccessDenied", "Access Denied", 403);}if (decision == S3PublicAccessEvaluator.PublicAccessDecision.ALLOW- || canonicalUserObjectAclAllowsRead(bucketName, key, canonicalUserId)) {+ || canonicalUserObjectAclAllowsRead(bucket, bucketName, key, canonicalUserId)) {return;}throw new AwsException("AccessDenied", "Access Denied", 403);@@ -1271,6 +1277,9 @@ public class S3Service implements Resettable, ResourceProvider {}private boolean publicBucketAclAllowsWrite(Bucket bucket) {+ if (ownershipIsEnforced(bucket)) {+ return false;+ }return Optional.ofNullable(bucket.getAcl()).map(S3AclPublicAccessEvaluator::aclAllowsPublicWrite).orElse(false);@@ -1313,7 +1322,8 @@ public class S3Service implements Resettable, ResourceProvider {return;}if (!blockPublicAccess.ignorePublicAcls()) {- if (key != null && isObjectDataReadAction(action) && publicObjectAclAllowsRead(bucketName, key, versionId)) {+ if (key != null && isObjectDataReadAction(action)+ && publicObjectAclAllowsRead(bucket, bucketName, key, versionId)) {return;}if (key == null && "s3:ListBucket".equals(action) && publicBucketAclAllowsRead(bucket)) {@@ -1530,12 +1540,18 @@ public class S3Service implements Resettable, ResourceProvider {}private boolean publicBucketAclAllowsRead(Bucket bucket) {+ if (ownershipIsEnforced(bucket)) {+ return false;+ }return Optional.ofNullable(bucket.getAcl()).map(S3AclPublicAccessEvaluator::aclAllowsPublicRead).orElse(false);}- private boolean publicObjectAclAllowsRead(String bucketName, String key, String versionId) {+ private boolean publicObjectAclAllowsRead(Bucket bucket, String bucketName, String key, String versionId) {+ if (ownershipIsEnforced(bucket)) {+ return false;+ }String storeKey = versionId != null ? versionedKey(bucketName, key, versionId) : objectKey(bucketName, key);return objectStore.get(storeKey).filter(object -> !object.isDeleteMarker())@@ -1545,8 +1561,8 @@ public class S3Service implements Resettable, ResourceProvider {}private boolean canonicalUserObjectAclAllowsRead(- String bucketName, String key, String canonicalUserId) {- if (canonicalUserId == null || canonicalUserId.isBlank()) {+ Bucket bucket, String bucketName, String key, String canonicalUserId) {+ if (ownershipIsEnforced(bucket) || canonicalUserId == null || canonicalUserId.isBlank()) {return false;}return objectStore.get(objectKey(bucketName, key))@@ -2504,6 +2520,78 @@ public class S3Service implements Resettable, ResourceProvider {return bucket.getVersioningStatus();}+ /** Captures only the bucket settings CloudFormation can change during an in-place update. */+ public String snapshotBucketConfiguration(String bucketName) {+ Bucket bucket = bucketStore.get(bucketName)+ .orElseThrow(() -> new AwsException("NoSuchBucket",+ "The specified bucket does not exist.", 404));+ ObjectNode snapshot = objectMapper.createObjectNode();+ snapshot.put("bucketName", bucketName);+ snapshot.put("versioningStatus", bucket.getVersioningStatus());+ snapshot.put("corsConfiguration", bucket.getCorsConfiguration());+ snapshot.put("publicAccessBlockConfiguration", bucket.getPublicAccessBlockConfiguration());+ snapshot.put("publicAccessBlockDeleted", bucket.isPublicAccessBlockDeleted());+ snapshot.put("encryptionConfiguration", bucket.getEncryptionConfiguration());+ snapshot.put("lifecycleConfiguration", bucket.getLifecycleConfiguration());+ snapshot.put("transitionDefaultMinimumObjectSize", bucket.getTransitionDefaultMinimumObjectSize());+ snapshot.put("ownershipControlsConfiguration", bucket.getOwnershipControlsConfiguration());+ snapshot.set("tags", objectMapper.valueToTree(bucket.getTags()));+ snapshot.set("notificationConfiguration", objectMapper.valueToTree(bucket.getNotificationConfiguration()));+ return snapshot.toString();+ }++ /** Restores a CloudFormation update snapshot without revalidating notification destinations. */+ public void restoreBucketConfiguration(String bucketName, String rawSnapshot) {+ JsonNode snapshot;+ NotificationConfiguration notifications;+ Map<String, String> tags;+ try {+ snapshot = objectMapper.readTree(rawSnapshot);+ if (snapshot == null || !snapshot.isObject() || !bucketName.equals(snapshot.path("bucketName").asText())+ || !snapshot.has("notificationConfiguration") || !snapshot.path("tags").isObject()) {+ throw new IllegalArgumentException("Invalid bucket configuration snapshot for " + bucketName);+ }+ JsonNode notificationNode = snapshot.get("notificationConfiguration");+ notifications = notificationNode.isNull() ? null+ : objectMapper.treeToValue(notificationNode, NotificationConfiguration.class);+ tags = objectMapper.convertValue(snapshot.get("tags"), new TypeReference<Map<String, String>>() {});+ } catch (JsonProcessingException e) {+ throw new IllegalStateException("Could not read bucket configuration snapshot for " + bucketName, e);+ }+ String versioning = snapshotText(snapshot, "versioningStatus");+ String cors = snapshotText(snapshot, "corsConfiguration");+ String publicAccessBlock = snapshotText(snapshot, "publicAccessBlockConfiguration");+ String encryption = snapshotText(snapshot, "encryptionConfiguration");+ String lifecycle = snapshotText(snapshot, "lifecycleConfiguration");+ String transitionSize = snapshotText(snapshot, "transitionDefaultMinimumObjectSize");+ String ownership = snapshotText(snapshot, "ownershipControlsConfiguration");+ if (!snapshot.path("publicAccessBlockDeleted").isBoolean()) {+ throw new IllegalArgumentException("Missing bucket public access block snapshot state");+ }+ Bucket bucket = bucketStore.get(bucketName)+ .orElseThrow(() -> new AwsException("NoSuchBucket",+ "The specified bucket does not exist.", 404));+ bucket.setVersioningStatus(versioning);+ bucket.setCorsConfiguration(cors);+ bucket.setPublicAccessBlockConfiguration(publicAccessBlock);+ bucket.setPublicAccessBlockDeleted(snapshot.path("publicAccessBlockDeleted").asBoolean());+ bucket.setEncryptionConfiguration(encryption);+ bucket.setLifecycleConfiguration(lifecycle);+ bucket.setTransitionDefaultMinimumObjectSize(transitionSize);+ bucket.setOwnershipControlsConfiguration(ownership);+ bucket.setTags(tags);+ bucket.setNotificationConfiguration(notifications);+ bucketStore.put(bucketName, bucket);+ }++ private static String snapshotText(JsonNode snapshot, String field) {+ JsonNode value = snapshot.get(field);+ if (value == null) {+ throw new IllegalArgumentException("Missing bucket configuration snapshot field: " + field);+ }+ return value.isNull() ? null : value.asText();+ }+public record ListVersionsResult(List<S3Object> versions, List<String> commonPrefixes, boolean isTruncated,String nextKeyMarker, String nextVersionIdMarker) {}@@ -3824,6 +3912,13 @@ public class S3Service implements Resettable, ResourceProvider {Map<String, String> tagging) {return withMultipartBucketReadLock(bucket, () -> {ensureBucketExists(bucket);+ Bucket destination = resolveBucket(bucket)+ .orElseThrow(() -> new AwsException("NoSuchBucket",+ "The specified bucket does not exist.", 404));+ if (ownershipIsEnforced(destination) && !isBlank(acl)+ && !"bucket-owner-full-control".equals(acl)) {+ throw new AwsException("AccessControlListNotSupported", "The bucket does not allow ACLs", 400);+ }if (acl != null && !acl.isBlank()) {cannedObjectAclXml(acl);}@@ -4847,13 +4942,15 @@ public class S3Service implements Resettable, ResourceProvider {public String getBucketAcl(String bucketName) {Bucket bucket = bucketStore.get(bucketName).orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));- return bucket.getAcl() != null ? bucket.getAcl() : defaultAclXml(ownerId(), DEFAULT_OWNER_DISPLAY_NAME);+ return !ownershipIsEnforced(bucket) && bucket.getAcl() != null+ ? bucket.getAcl() : defaultAclXml(ownerId(), DEFAULT_OWNER_DISPLAY_NAME);}public void putBucketAcl(String bucketName, String bodyAcl, String cannedAcl, String grantRead,String grantWrite, String grantFullControl, String grantReadAcp, String grantWriteAcp) {Bucket bucket = bucketStore.get(bucketName).orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));+ rejectAclWhenOwnershipEnforced(bucket);String resolvedAcl = resolveObjectAclXml(cannedAcl, grantRead, grantWrite, grantFullControl, grantReadAcp, grantWriteAcp);String newAcl = resolvedAcl != null ? resolvedAcl : (bodyAcl.isBlank() ? null : bodyAcl);@@ -4866,7 +4963,9 @@ public class S3Service implements Resettable, ResourceProvider {AccountAwareStorageBackend.OwnedEntry<S3Object> ownedObject =getStoredObjectEntry(bucketName, key, versionId);S3Object obj = ownedObject.value();- return obj.getAcl() != null+ Bucket bucket = resolveBucket(bucketName)+ .orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));+ return !ownershipIsEnforced(bucket) && obj.getAcl() != null? obj.getAcl(): defaultAclXml(ownedObject.account(), DEFAULT_OWNER_DISPLAY_NAME);}@@ -4877,6 +4976,9 @@ public class S3Service implements Resettable, ResourceProvider {AccountAwareStorageBackend.OwnedEntry<S3Object> ownedObject =getStoredObjectEntry(bucketName, key, versionId);S3Object obj = ownedObject.value();+ Bucket bucket = resolveBucket(bucketName)+ .orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));+ rejectAclWhenOwnershipEnforced(bucket);String resolvedAcl = resolveObjectAclXml(ownedObject.account(), cannedAcl, grantRead,grantWrite, grantFullControl, grantReadAcp, grantWriteAcp);String newAcl = resolvedAcl != null ? resolvedAcl : (bodyAcl.isBlank() ? null : bodyAcl);@@ -4885,6 +4987,47 @@ public class S3Service implements Resettable, ResourceProvider {putObjectMetadataForAccount(ownedObject.account(), bucketName, key, obj);}+ private static void rejectAclWhenOwnershipEnforced(Bucket bucket) {+ if (ownershipIsEnforced(bucket)) {+ throw new AwsException("AccessControlListNotSupported", "The bucket does not allow ACLs", 400);+ }+ }++ private static void rejectUploadAclWhenOwnershipEnforced(Bucket bucket, String ownerAccount,+ PutObjectOptions options) {+ if (!ownershipIsEnforced(bucket)) {+ return;+ }+ boolean otherGrants = !isBlank(options.getGrantRead()) || !isBlank(options.getGrantWrite())+ || !isBlank(options.getGrantReadAcp()) || !isBlank(options.getGrantWriteAcp());+ boolean ownerGrant = isOnlyOwnerFullControlGrant(ownerAccount, options.getGrantFullControl());+ boolean withoutAcl = isBlank(options.getAcl())+ && (isBlank(options.getGrantFullControl()) || ownerGrant);+ boolean ownerFullControlAcl = "bucket-owner-full-control".equals(options.getAcl())+ && isBlank(options.getGrantFullControl());+ if (!otherGrants && (withoutAcl || ownerFullControlAcl)) {+ return;+ }+ throw new AwsException("AccessControlListNotSupported", "The bucket does not allow ACLs", 400);+ }++ private static boolean isOnlyOwnerFullControlGrant(String ownerAccount, String header) {+ if (isBlank(header)) {+ return false;+ }+ for (String grantee : header.split(",", -1)) {+ if (!("id=\"" + ownerAccount + "\"").equals(grantee.trim())) {+ return false;+ }+ }+ return true;+ }++ private static boolean ownershipIsEnforced(Bucket bucket) {+ return XmlParser.containsValue(bucket.getOwnershipControlsConfiguration(),+ "ObjectOwnership", "BucketOwnerEnforced");+ }+/*** Returns the bucket's server-side encryption configuration as XML.* <p>@@ -5065,10 +5208,37 @@ public class S3Service implements Resettable, ResourceProvider {public void putBucketOwnershipControls(String bucketName, String ownershipControlsXml) {Bucket bucket = bucketStore.get(bucketName).orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));+ validateOwnershipControls(ownershipControlsXml);+ if (XmlParser.containsValue(ownershipControlsXml, "ObjectOwnership", "BucketOwnerEnforced")+ && S3AclPublicAccessEvaluator.aclGrantsNonOwner(bucket.getAcl(), ownerId())) {+ throw new AwsException("InvalidBucketAclWithObjectOwnership",+ "Bucket cannot have ACLs set with ObjectOwnership's BucketOwnerEnforced setting", 400);+ }bucket.setOwnershipControlsConfiguration(ownershipControlsXml);bucketStore.put(bucketName, bucket);}+ /** Validates the same ownership document for direct S3 calls and CloudFormation templates. */+ public static void validateOwnershipControls(String xml) {+ if (!"OwnershipControls".equals(XmlParser.rootElementName(xml))) {+ throw new AwsException("MalformedXML", S3_XML_SCHEMA_ERROR, 400);+ }+ XmlParser.XmlElement controls = XmlParser.extractElementTree(xml, "OwnershipControls");+ long ruleCount = controls.count("Rule");+ if (ruleCount != 1) {+ throw new AwsException("MalformedXML", S3_XML_SCHEMA_ERROR, 400);+ }+ XmlParser.XmlElement rule = controls.child("Rule");+ XmlParser.XmlElement ownership = rule.child("ObjectOwnership");+ if (rule.count("ObjectOwnership") != 1 || ownership == null) {+ throw new AwsException("MalformedXML", S3_XML_SCHEMA_ERROR, 400);+ }+ if (!Set.of("BucketOwnerPreferred", "ObjectWriter", "BucketOwnerEnforced")+ .contains(ownership.text())) {+ throw new AwsException("MalformedXML", S3_XML_SCHEMA_ERROR, 400);+ }+ }+public void deleteBucketOwnershipControls(String bucketName) {Bucket bucket = bucketStore.get(bucketName).orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404));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.javaindex eeed3b154c..caee36dac7 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@@ -69,6 +69,92 @@ class CloudFormationS3BucketPropertiesIntegrationTest {""".formatted(bucket, noncurrentDays);}+ @Test+ void ownershipControlsFollowTheTemplateWithoutErasingUnmanagedChanges() throws InterruptedException {+ String suffix = Long.toString(System.nanoTime(), 36);+ String bucket = "cfn-ownership-" + suffix;+ String stackName = "cfn-ownership-stack-" + suffix;+ String stackId = stack("CreateStack", stackName, ownershipTemplate(bucket, "BucketOwnerEnforced", "create"));+ await(stackId, "CREATE_COMPLETE");+ bucketGet(bucket, "ownershipControls").body(containsString("<ObjectOwnership>BucketOwnerEnforced</ObjectOwnership>"));+ given().header("x-amz-acl", "public-read")+ .when().put("/" + bucket + "?acl")+ .then().statusCode(400).body(containsString("AccessControlListNotSupported"));+ bucketGet(bucket, "acl");++ stack("UpdateStack", stackName, ownershipTemplate(bucket, "ObjectWriter", "update"));+ await(stackId, "UPDATE_COMPLETE");+ bucketGet(bucket, "ownershipControls").body(containsString("<ObjectOwnership>ObjectWriter</ObjectOwnership>"));++ stack("UpdateStack", stackName, ownershipTemplate(bucket, null, "removed"));+ await(stackId, "UPDATE_COMPLETE");+ given().when().get("/" + bucket + "?ownershipControls").then().statusCode(404);++ given().contentType("application/xml")+ .body("<OwnershipControls><Rule><ObjectOwnership>BucketOwnerPreferred</ObjectOwnership></Rule></OwnershipControls>")+ .when().put("/" + bucket + "?ownershipControls").then().statusCode(200);+ stack("UpdateStack", stackName, ownershipTemplate(bucket, null, "unmanaged"));+ await(stackId, "UPDATE_COMPLETE");+ bucketGet(bucket, "ownershipControls").body(containsString("<ObjectOwnership>BucketOwnerPreferred</ObjectOwnership>"));+ }++ @Test+ void failedUpdateRestoresChangedOwnershipControls() throws InterruptedException {+ assertFailedUpdateRestoresOwnershipControls("ObjectWriter");+ }++ @Test+ void failedUpdateRestoresRemovedOwnershipControls() throws InterruptedException {+ assertFailedUpdateRestoresOwnershipControls(null);+ }++ private static void assertFailedUpdateRestoresOwnershipControls(String attemptedOwnership)+ throws InterruptedException {+ String suffix = Long.toString(System.nanoTime(), 36);+ String bucket = "cfn-ownership-rollback-" + suffix;+ String stackName = "cfn-ownership-rollback-stack-" + suffix;+ String stackId = stack("CreateStack", stackName,+ ownershipTemplate(bucket, "BucketOwnerEnforced", "initial"));+ await(stackId, "CREATE_COMPLETE");+ given().body("""+ <CORSConfiguration><CORSRule><AllowedOrigin>https://initial.example</AllowedOrigin>+ <AllowedMethod>GET</AllowedMethod></CORSRule></CORSConfiguration>+ """)+ .when().put("/" + bucket + "?cors").then().statusCode(200);++ String controls = attemptedOwnership == null ? "" : """+ , "OwnershipControls": { "Rules": [{ "ObjectOwnership": "%s" }] }+ """.formatted(attemptedOwnership);+ String update = """+ { "Resources": {+ "Bucket": { "Type": "AWS::S3::Bucket",+ "Properties": { "BucketName": "%s", "Tags": [+ { "Key": "marker", "Value": "attempted" } ],+ "VersioningConfiguration": { "Status": "Enabled" },+ "PublicAccessBlockConfiguration": { "BlockPublicAcls": false }+ %s } },+ "Broken": { "Type": "AWS::S3::Bucket", "DependsOn": "Bucket",+ "Properties": { "BucketName": "cfn-broken-%s",+ "VersioningConfiguration": { "Status": "Bogus" } } }+ } }+ """.formatted(bucket, controls, suffix);+ stack("UpdateStack", stackName, update);+ await(stackId, "UPDATE_ROLLBACK_COMPLETE");+ bucketGet(bucket, "ownershipControls")+ .body(containsString("<ObjectOwnership>BucketOwnerEnforced</ObjectOwnership>"));+ bucketGet(bucket, "tagging").body(containsString("<Value>initial</Value>"));+ bucketGet(bucket, "versioning").body(not(containsString("<Status>Enabled</Status>")));+ bucketGet(bucket, "publicAccessBlock").body(containsString("<BlockPublicAcls>true</BlockPublicAcls>"));+ bucketGet(bucket, "cors").body(containsString("https://initial.example"));+ }+ private static String ownershipTemplate(String bucket, String ownership, String marker) {+ String controls = ownership == null ? "" : ", \"OwnershipControls\": { \"Rules\": "+ + "[{ \"ObjectOwnership\": \"" + ownership + "\" }] }";+ return "{ \"Resources\": { \"Bucket\": { \"Type\": \"AWS::S3::Bucket\", \"Properties\": { "+ + "\"BucketName\": \"" + bucket + "\", \"Tags\": [{ \"Key\": \"marker\", \"Value\": \""+ + marker + "\" }]" + controls + " } } } }";+ }+@Testvoid theBucketCarriesWhatItsTemplateDeclares() throws InterruptedException {String suffix = Long.toString(System.nanoTime(), 36);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.javaindex a6bdf02c8d..63b14e4386 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@@ -57,6 +57,7 @@ class S3CfnProvisionerTest {when(engine.resolveNodeOmittingNoValue(any())).thenAnswer(i -> i.getArgument(0));// A bucket carrying a configuration no test declares, so every declared one is a change.when(s3.getBucketNotificationConfiguration(anyString())).thenReturn(queueNotification("existing"));+ when(s3.snapshotBucketConfiguration(anyString())).thenReturn("snapshot");ctx = new ProvisionContext(engine, REGION, "000000000000", "my-stack");}@@ -81,6 +82,37 @@ class S3CfnProvisionerTest {return r;}+ @ParameterizedTest+ @CsvSource(delimiter = '|', textBlock = """+ {"Rules":[{"ObjectOwnership":"Bogus"}]} | The XML you provided was not well-formed or did not validate against our published schema.+ {"Rules":[{}]} | Model validation failed (#/OwnershipControls/Rules/0: required key [ObjectOwnership] not found)+ {"Rules":[{"ObjectOwnership":["BucketOwnerEnforced"]}]} | Model validation failed (#/OwnershipControls/Rules/0/ObjectOwnership: expected type: String, found: JSONArray)+ {"Rules":[]} | The XML you provided was not well-formed or did not validate against our published schema.+ {"Rules":"not-an-array"} | Model validation failed (#/OwnershipControls/Rules: expected type: JSONArray, found: String)+ """)+ void invalidOwnershipControlsFailBeforeCreatingTheBucket(String controls, String message) {+ AwsException error = assertThrows(AwsException.class,+ () -> provisionBucket("{\"BucketName\":\"my-bucket\",\"OwnershipControls\":" + controls + "}"));++ assertEquals(message, error.getMessage());+ verify(s3, never()).createBucket(anyString(), anyString());+ verify(s3, never()).putBucketOwnershipControls(anyString(), anyString());+ }++ @Test+ void multipleOwnershipRulesFailBeforeCreatingTheBucket() {+ AwsException error = assertThrows(AwsException.class, () -> provisionBucket("""+ {"BucketName":"my-bucket","OwnershipControls":{"Rules":[+ {"ObjectOwnership":"ObjectWriter"},+ {"ObjectOwnership":"BucketOwnerEnforced"}]}}+ """));++ assertEquals("The XML you provided was not well-formed or did not validate against our published schema.",+ error.getMessage());+ verify(s3, never()).createBucket(anyString(), anyString());+ verify(s3, never()).putBucketOwnershipControls(anyString(), anyString());+ }+@Testvoid refIsTheBucketNameAndGetAttExposesTheDocumentedAttributes() {StackResource r = provisionBucket("""@@ -528,13 +560,9 @@ class S3CfnProvisionerTest {return config;}- /**- * A property that fails after the configuration was put has the replaced configuration put back,- * since the engine reports a failed update as rolled back. Its destinations were valid when it- * was put, so they are not checked again.- */+ /** A later failure restores all changed bucket settings from the in-place update snapshot. */@Test- void aLaterFailurePutsTheReplacedNotificationConfigurationBack() {+ void aLaterFailureRestoresTheBucketSnapshot() {NotificationConfiguration before = queueNotification("old");when(s3.getBucketNotificationConfiguration("b")).thenReturn(before);doThrow(new AwsException("MalformedXML", "bad", 400)).when(s3).putBucketVersioning("b", "Bogus");@@ -549,7 +577,8 @@ class S3CfnProvisionerTest {event -> { }, props("{\"BucketName\": \"b\"}"))));verify(s3).putBucketNotificationConfiguration(eq("b"), any());- verify(s3).putBucketNotificationConfiguration("b", before, true);+ verify(s3).restoreBucketConfiguration("b", "snapshot");+ assertFalse(r.getAttributes().containsKey(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR));assertFalse(r.getAttributes().containsKey(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR));}@@ -570,7 +599,7 @@ class S3CfnProvisionerTest {when(s3.getBucketNotificationConfiguration("b")).thenReturn(queueNotification("old"));doThrow(new AwsException("MalformedXML", "bad", 400)).when(s3).putBucketVersioning("b", "Bogus");doThrow(new AwsException("InternalError", "disk", 500))- .when(s3).putBucketNotificationConfiguration(eq("b"), any(), eq(true));+ .when(s3).restoreBucketConfiguration("b", "snapshot");StackResource r = resource("Bucket", "AWS::S3::Bucket");r.setPhysicalId("b");diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.javaindex 0135ecad4f..aaf50d6b16 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java@@ -2403,6 +2403,54 @@ class S3AuthEnforcementIntegrationTest {.body(equalTo("private data"));}+ @Test+ @Order(58)+ void enforcedOwnershipIgnoresExistingPublicAclsUntilReenabled() {+ String bucket = "auth-enforced-acl-bucket";+ String controls = """+ <OwnershipControls xmlns="http://s3.amazonaws.com/doc/2006-03-01/">+ <Rule><ObjectOwnership>BucketOwnerEnforced</ObjectOwnership></Rule>+ </OwnershipControls>+ """;+ given().filter(LOCAL_SIGNER).when().put("/" + bucket).then().statusCode(200);+ given().filter(LOCAL_SIGNER).header("x-amz-acl", "public-read").body("public data")+ .when().put("/" + bucket + "/data.txt")+ .then().statusCode(200);+ given().filter(LOCAL_SIGNER).header("x-amz-acl", "public-read")+ .when().put("/" + bucket + "?acl")+ .then().statusCode(200);+ given().when().get("/" + bucket + "/data.txt").then().statusCode(200);+ given().when().get("/" + bucket + "?list-type=2").then().statusCode(200);++ given().filter(LOCAL_SIGNER).body(controls)+ .when().put("/" + bucket + "?ownershipControls")+ .then().statusCode(400).body(containsString("InvalidBucketAclWithObjectOwnership"));+ given().filter(LOCAL_SIGNER).when().get("/" + bucket + "?ownershipControls")+ .then().statusCode(404);+ given().filter(LOCAL_SIGNER).header("x-amz-acl", "private")+ .when().put("/" + bucket + "?acl")+ .then().statusCode(200);+ given().filter(LOCAL_SIGNER).body(controls)+ .when().put("/" + bucket + "?ownershipControls")+ .then().statusCode(200);++ given().when().get("/" + bucket + "/data.txt").then().statusCode(403);+ given().when().get("/" + bucket + "?list-type=2").then().statusCode(403);+ given().filter(LOCAL_SIGNER).when().get("/" + bucket + "?acl")+ .then().statusCode(200).body(not(containsString("AllUsers")));+ given().filter(LOCAL_SIGNER).when().get("/" + bucket + "/data.txt?acl")+ .then().statusCode(200).body(not(containsString("AllUsers")));++ given().filter(LOCAL_SIGNER).when().delete("/" + bucket + "?ownershipControls")+ .then().statusCode(204);+ given().when().get("/" + bucket + "/data.txt").then().statusCode(200);+ given().when().get("/" + bucket + "?list-type=2").then().statusCode(403);+ given().filter(LOCAL_SIGNER).header("x-amz-acl", "public-read")+ .when().put("/" + bucket + "?acl")+ .then().statusCode(200);+ given().when().get("/" + bucket + "?list-type=2").then().statusCode(200);+ }+private static String publicObjectActionPolicy(String bucket, String action) {return publicObjectActionPolicy(bucket, "*", action);}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3OwnershipControlsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3OwnershipControlsIntegrationTest.javaindex 94da98fe72..de90bbf0aa 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3OwnershipControlsIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3OwnershipControlsIntegrationTest.java@@ -21,6 +21,8 @@ class S3OwnershipControlsIntegrationTest {</Rule></OwnershipControls>""";+ private static final String SCHEMA_MESSAGE =+ "The XML you provided was not well-formed or did not validate against our published schema.";@Test@Order(1)@@ -68,6 +70,120 @@ class S3OwnershipControlsIntegrationTest {@Test@Order(5)+ void invalidDirectOwnershipControlsDoNotOverwriteTheStoredMode() {+ given()+ .body(OWNERSHIP_CONTROLS_XML.replace("BucketOwnerPreferred", "NotAnOwnershipMode"))+ .when()+ .put("/" + BUCKET + "?ownershipControls")+ .then()+ .statusCode(400)+ .body(containsString("<Code>MalformedXML</Code>"))+ .body(containsString(SCHEMA_MESSAGE));++ given()+ .body("<OwnershipControls xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"/>")+ .when()+ .put("/" + BUCKET + "?ownershipControls")+ .then()+ .statusCode(400)+ .body(containsString("<Code>MalformedXML</Code>"))+ .body(containsString(SCHEMA_MESSAGE));++ given()+ .body(OWNERSHIP_CONTROLS_XML.replace("<ObjectOwnership>BucketOwnerPreferred</ObjectOwnership>", ""))+ .when()+ .put("/" + BUCKET + "?ownershipControls")+ .then()+ .statusCode(400)+ .body(containsString("<Code>MalformedXML</Code>"))+ .body(containsString(SCHEMA_MESSAGE));++ given()+ .body(OWNERSHIP_CONTROLS_XML.replace("</OwnershipControls>",+ "<Rule><ObjectOwnership>ObjectWriter</ObjectOwnership></Rule></OwnershipControls>"))+ .when()+ .put("/" + BUCKET + "?ownershipControls")+ .then()+ .statusCode(400)+ .body(containsString("<Code>MalformedXML</Code>"))+ .body(containsString(SCHEMA_MESSAGE));++ given()+ .when()+ .get("/" + BUCKET + "?ownershipControls")+ .then()+ .statusCode(200)+ .body(containsString("<ObjectOwnership>BucketOwnerPreferred</ObjectOwnership>"));+ }++ @Test+ @Order(6)+ void enforcedOwnershipRejectsAclWritesAndDisallowedUploadAcls() {+ given()+ .body("object body")+ .when()+ .put("/" + BUCKET + "/owned-object")+ .then()+ .statusCode(200);++ given()+ .body(OWNERSHIP_CONTROLS_XML.replace("BucketOwnerPreferred", "BucketOwnerEnforced"))+ .when()+ .put("/" + BUCKET + "?ownershipControls")+ .then()+ .statusCode(200);++ given()+ .header("x-amz-acl", "public-read")+ .when()+ .put("/" + BUCKET + "?acl")+ .then()+ .statusCode(400)+ .body(containsString("AccessControlListNotSupported"));++ given()+ .header("x-amz-acl", "public-read")+ .when()+ .put("/" + BUCKET + "/owned-object?acl")+ .then()+ .statusCode(400)+ .body(containsString("AccessControlListNotSupported"));++ given()+ .header("x-amz-acl", "public-read")+ .body("new object")+ .when()+ .put("/" + BUCKET + "/rejected-object")+ .then()+ .statusCode(400)+ .body(containsString("AccessControlListNotSupported"));++ given()+ .header("x-amz-acl", "public-read")+ .when()+ .post("/" + BUCKET + "/rejected-multipart?uploads")+ .then()+ .statusCode(400)+ .body(containsString("AccessControlListNotSupported"));++ given()+ .body("new object")+ .when()+ .put("/" + BUCKET + "/no-acl-object")+ .then()+ .statusCode(200);++ given()+ .header("x-amz-acl", "bucket-owner-full-control")+ .body("new object")+ .when()+ .put("/" + BUCKET + "/accepted-object")+ .then()+ .statusCode(200);+ }++ @Test+ @Order(7)void deleteOwnershipControlsReturns204() {given().when()@@ -77,7 +193,7 @@ class S3OwnershipControlsIntegrationTest {}@Test- @Order(6)+ @Order(8)void getOwnershipControlsAfterDeleteReturns404() {given().when()@@ -86,4 +202,32 @@ class S3OwnershipControlsIntegrationTest {.statusCode(404).body(containsString("OwnershipControlsNotFoundError"));}++ @Test+ @Order(9)+ void enforcedOwnershipRejectsBucketAclGrantedToAnotherCanonicalUser() {+ String bucket = BUCKET + "-nonowner-grant";+ String acl = """+ <AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">+ <Owner><ID>000000000000</ID></Owner>+ <AccessControlList>+ <Grant><Grantee><ID>000000000000</ID></Grantee><Permission>FULL_CONTROL</Permission></Grant>+ <Grant><Grantee><ID>111111111111</ID></Grantee><Permission>READ</Permission></Grant>+ </AccessControlList>+ </AccessControlPolicy>+ """;+ String enforced = OWNERSHIP_CONTROLS_XML.replace("BucketOwnerPreferred", "BucketOwnerEnforced");++ given().when().put("/" + bucket).then().statusCode(200);+ given().body(acl).when().put("/" + bucket + "?acl").then().statusCode(200);+ given().body(enforced).when().put("/" + bucket + "?ownershipControls")+ .then().statusCode(400).body(containsString("InvalidBucketAclWithObjectOwnership"));+ given().when().get("/" + bucket + "?ownershipControls")+ .then().statusCode(404);++ given().header("x-amz-acl", "private").when().put("/" + bucket + "?acl")+ .then().statusCode(200);+ given().body(enforced).when().put("/" + bucket + "?ownershipControls")+ .then().statusCode(200);+ }}