fix(s3): enforce ownership controls and restore CFN updates (#5712)

* fix(cloudformation): apply S3 bucket OwnershipControls

* fix(s3): validate ownership controls in service

* fix(s3): return MalformedXML for invalid ownership controls

* fix(s3): enforce ownership controls and restore bucket updates

* fix(s3): reject ownership enforcement for nonprivate ACLs
This commit is contained in:
XuJian authored and GitHub committed 2026-10-15 20:30:26 -04:00
1 parent f045d7d0a8
commit a4eb854614
9 files changed
+586 -29

No files matched your search

@@ -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 an
@@ -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())
@@ -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;
}
}
}
@@ -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));