mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(s3): default new buckets to BucketOwnerEnforced (#5782)
* fix(s3): default new buckets to bucket owner enforced Initialize ownership controls at bucket creation and opt ACL-focused tests into legacy ownership modes. Repair existing import grouping so repository-wide Checkstyle passes. * fix(s3): honor bucket ownership and ACL headers Preserve explicit CreateBucket ownership and ACL grants, and reject grants incompatible with enforced ownership. Validate multipart grants at initiation and carry accepted grants through completion. Adapt ACL-focused regressions to opt into legacy ownership modes. * fix(s3): respect account public ACL setting at bucket creation * refactor(s3): share create-bucket ownership XML builder
This commit is contained in:
1 parent
708dc32fa1
commit
d1509c368f
13 files changed
+239
-10
No files matched your search
@@ -451,7 +451,14 @@ public class S3Controller {
|
||||
new String(body, StandardCharsets.UTF_8), "Tag", "Key", "Value");
|
||||
}
|
||||
String region = bucketRegionForCreate(locationConstraint, regionResolver.resolveRegion(httpHeaders));
|
||||
s3Service.createBucket(bucket, region);
|
||||
s3Service.createBucket(bucket, region,
|
||||
httpHeaders.getHeaderString("x-amz-object-ownership"),
|
||||
httpHeaders.getHeaderString("x-amz-acl"),
|
||||
httpHeaders.getHeaderString("x-amz-grant-read"),
|
||||
httpHeaders.getHeaderString("x-amz-grant-write"),
|
||||
httpHeaders.getHeaderString("x-amz-grant-full-control"),
|
||||
httpHeaders.getHeaderString("x-amz-grant-read-acp"),
|
||||
httpHeaders.getHeaderString("x-amz-grant-write-acp"));
|
||||
// CreateBucketConfiguration may carry a <Tags> array; AWS applies those tags to the
|
||||
// new bucket, so a follow-up GetBucketTagging / ListTagsForResource must return them.
|
||||
if (!creationTags.isEmpty()) {
|
||||
|
||||
@@ -120,6 +120,8 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
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> OBJECT_OWNERSHIP_VALUES = Set.of(
|
||||
"BucketOwnerEnforced", "BucketOwnerPreferred", "ObjectWriter");
|
||||
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;
|
||||
@@ -465,7 +467,35 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
}
|
||||
|
||||
public Bucket createBucket(String bucketName, String region) {
|
||||
return createBucket(bucketName, region, null, null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
public Bucket createBucket(String bucketName, String region, String requestedOwnership,
|
||||
String cannedAcl, String grantRead, String grantWrite,
|
||||
String grantFullControl, String grantReadAcp, String grantWriteAcp) {
|
||||
requirePathSafeBucketName(bucketName);
|
||||
String ownership = requestedOwnership == null ? "BucketOwnerEnforced" : requestedOwnership;
|
||||
if (!OBJECT_OWNERSHIP_VALUES.contains(ownership)) {
|
||||
throw new AwsException("InvalidArgument", "Invalid x-amz-object-ownership value: " + ownership, 400);
|
||||
}
|
||||
if (!isBlank(cannedAcl) && (!isBlank(grantRead) || !isBlank(grantWrite)
|
||||
|| !isBlank(grantFullControl) || !isBlank(grantReadAcp) || !isBlank(grantWriteAcp))) {
|
||||
throw new AwsException("InvalidRequest", "Specifying both Canned ACLs and Header Grants is not allowed", 400);
|
||||
}
|
||||
String requestedAcl = resolveObjectAclXml(cannedAcl, grantRead, grantWrite,
|
||||
grantFullControl, grantReadAcp, grantWriteAcp);
|
||||
if ("BucketOwnerEnforced".equals(ownership)
|
||||
&& (!isBlank(grantRead) || !isBlank(grantWrite) || !isBlank(grantReadAcp)
|
||||
|| !isBlank(grantWriteAcp)
|
||||
|| (!isBlank(grantFullControl) && !isOnlyOwnerFullControlGrant(ownerId(), grantFullControl))
|
||||
|| S3AclPublicAccessEvaluator.aclGrantsNonOwner(requestedAcl, ownerId()))) {
|
||||
throw new AwsException("InvalidBucketAclWithObjectOwnership",
|
||||
"Bucket cannot have ACLs set with ObjectOwnership's BucketOwnerEnforced setting", 400);
|
||||
}
|
||||
if (S3AclPublicAccessEvaluator.aclIsPublic(requestedAcl)) {
|
||||
throw new AwsException("InvalidBucketAclWithBlockPublicAccessError",
|
||||
"Bucket cannot have public ACLs set with BlockPublicAccess enabled", 400);
|
||||
}
|
||||
var existing = bucketStore.get(bucketName);
|
||||
if (existing.isPresent()) {
|
||||
Bucket bucket = existing.get();
|
||||
@@ -479,11 +509,23 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
|
||||
Bucket bucket = new Bucket(bucketName);
|
||||
bucket.setRegion(region);
|
||||
bucket.setOwnershipControlsConfiguration(ownershipControlsXml(ownership));
|
||||
bucket.setAcl(requestedAcl);
|
||||
bucketStore.put(bucketName, bucket);
|
||||
LOG.infov("Created bucket: {0} in region: {1}", bucketName, region);
|
||||
return bucket;
|
||||
}
|
||||
|
||||
private static String ownershipControlsXml(String ownership) {
|
||||
return new XmlBuilder()
|
||||
.start("OwnershipControls", AwsNamespaces.S3)
|
||||
.start("Rule")
|
||||
.elem("ObjectOwnership", ownership)
|
||||
.end("Rule")
|
||||
.end("OwnershipControls")
|
||||
.build();
|
||||
}
|
||||
|
||||
private static boolean isDefaultS3Region(String region) {
|
||||
return region == null || region.isBlank() || "us-east-1".equalsIgnoreCase(region); // partition-literal: S3's idempotent CreateBucket rule is literally us-east-1 in every partition
|
||||
}
|
||||
@@ -5688,8 +5730,7 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
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())) {
|
||||
if (!OBJECT_OWNERSHIP_VALUES.contains(ownership.text())) {
|
||||
throw new AwsException("MalformedXML", S3_XML_SCHEMA_ERROR, 400);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user