mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
f045d7d0a8
commit
a4eb854614
9 files changed
+586
-29
No files matched your search
+86
@@ -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 + " } } } }";
|
||||
}
|
||||
|
||||
@Test
|
||||
void theBucketCarriesWhatItsTemplateDeclares() throws InterruptedException {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
|
||||
+37
-8
@@ -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());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 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");
|
||||
|
||||
|
||||
+48
@@ -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);
|
||||
}
|
||||
|
||||
+145
-1
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user