mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(s3): support bucket analytics and inventory configurations (#3213)
PutBucketAnalyticsConfiguration, GetBucketAnalyticsConfiguration, ListBucketAnalyticsConfigurations and DeleteBucketAnalyticsConfiguration, plus the four inventory counterparts, follow the shape the metrics and intelligent-tiering configurations already use. Each body is validated against the published schema and re-serialized in the AWS element order, stored on the bucket record under its id, and returned from the single and list reads. The PUT and DELETE forms are routed ahead of the unqualified CreateBucket and DeleteBucket so they cannot fall through.
This commit is contained in:
1 parent
ec2d678e6d
commit
703744c9fb
11 files changed
+1383
-2
No files matched your search
+217
@@ -0,0 +1,217 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class S3AnalyticsConfigurationIntegrationTest {
|
||||
|
||||
private static final String BUCKET = "analytics-int-test";
|
||||
|
||||
private static String configuration(String id, String filter) {
|
||||
return """
|
||||
<AnalyticsConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<Id>%s</Id>
|
||||
%s
|
||||
<StorageClassAnalysis>
|
||||
<DataExport>
|
||||
<OutputSchemaVersion>V_1</OutputSchemaVersion>
|
||||
<Destination>
|
||||
<S3BucketDestination>
|
||||
<Format>CSV</Format>
|
||||
<BucketAccountId>123456789012</BucketAccountId>
|
||||
<Bucket>arn:aws:s3:::analytics-destination</Bucket>
|
||||
<Prefix>exports/</Prefix>
|
||||
</S3BucketDestination>
|
||||
</Destination>
|
||||
</DataExport>
|
||||
</StorageClassAnalysis>
|
||||
</AnalyticsConfiguration>
|
||||
""".formatted(id, filter);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createBucket() {
|
||||
given()
|
||||
.when()
|
||||
.put("/" + BUCKET)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code PUT /{bucket}?analytics} must be handled before the unqualified CreateBucket, which
|
||||
* would otherwise answer with BucketAlreadyOwnedByYou. Real S3 stores the configuration and
|
||||
* returns 204.
|
||||
*/
|
||||
@Test
|
||||
@Order(2)
|
||||
void putAnalyticsConfigurationIsNotTreatedAsCreateBucket() {
|
||||
given()
|
||||
.body(configuration("EntireBucket", ""))
|
||||
.when()
|
||||
.put("/" + BUCKET + "?analytics&id=EntireBucket")
|
||||
.then()
|
||||
.statusCode(204)
|
||||
.body(not(containsString("BucketAlreadyOwnedByYou")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void getAnalyticsConfigurationReturnsWhatWasStored() {
|
||||
given()
|
||||
.when()
|
||||
.get("/" + BUCKET + "?analytics&id=EntireBucket")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<AnalyticsConfiguration"))
|
||||
.body(containsString("<Id>EntireBucket</Id>"))
|
||||
.body(containsString("<OutputSchemaVersion>V_1</OutputSchemaVersion>"))
|
||||
.body(containsString("<BucketAccountId>123456789012</BucketAccountId>"))
|
||||
.body(containsString("<Bucket>arn:aws:s3:::analytics-destination</Bucket>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void putFilteredConfigurationKeepsTheFilter() {
|
||||
given()
|
||||
.body(configuration("Filtered", """
|
||||
<Filter>
|
||||
<And>
|
||||
<Prefix>logs/</Prefix>
|
||||
<Tag><Key>env</Key><Value>prod</Value></Tag>
|
||||
<Tag><Key>team</Key><Value>core</Value></Tag>
|
||||
</And>
|
||||
</Filter>
|
||||
"""))
|
||||
.when()
|
||||
.put("/" + BUCKET + "?analytics&id=Filtered")
|
||||
.then()
|
||||
.statusCode(204);
|
||||
|
||||
// AWS repeats <Tag> inside <And> with no wrapping element.
|
||||
given()
|
||||
.when()
|
||||
.get("/" + BUCKET + "?analytics&id=Filtered")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<And><Prefix>logs/</Prefix>"
|
||||
+ "<Tag><Key>env</Key><Value>prod</Value></Tag>"
|
||||
+ "<Tag><Key>team</Key><Value>core</Value></Tag></And>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void listWithoutAnIdReturnsEveryConfiguration() {
|
||||
given()
|
||||
.when()
|
||||
.get("/" + BUCKET + "?analytics")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<ListBucketAnalyticsConfigurationResult"))
|
||||
.body(containsString("<Id>EntireBucket</Id>"))
|
||||
.body(containsString("<Id>Filtered</Id>"))
|
||||
.body(containsString("<IsTruncated>false</IsTruncated>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void idInTheBodyMustMatchTheIdInTheQuery() {
|
||||
given()
|
||||
.body(configuration("Different", ""))
|
||||
.when()
|
||||
.put("/" + BUCKET + "?analytics&id=Mismatch")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("MalformedXML"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void unknownIdIsNotFound() {
|
||||
given()
|
||||
.when()
|
||||
.get("/" + BUCKET + "?analytics&id=absent")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body(containsString("NoSuchConfiguration"));
|
||||
|
||||
// AWS does not treat deleting an absent configuration as a no-op.
|
||||
given()
|
||||
.when()
|
||||
.delete("/" + BUCKET + "?analytics&id=absent")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body(containsString("NoSuchConfiguration"));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code DELETE /{bucket}?analytics} must not fall through to the unqualified DeleteBucket
|
||||
* and remove the whole bucket.
|
||||
*/
|
||||
@Test
|
||||
@Order(8)
|
||||
void deleteAnalyticsConfigurationDoesNotDeleteBucket() {
|
||||
given()
|
||||
.when()
|
||||
.delete("/" + BUCKET + "?analytics&id=EntireBucket")
|
||||
.then()
|
||||
.statusCode(204);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + BUCKET + "?analytics&id=Filtered")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + BUCKET + "?analytics&id=EntireBucket")
|
||||
.then()
|
||||
.statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void aRequestWithoutAnIdIsRefusedRatherThanGuessedAt() {
|
||||
given()
|
||||
.body(configuration("EntireBucket", ""))
|
||||
.when()
|
||||
.put("/" + BUCKET + "?analytics")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("InvalidArgument"));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.delete("/" + BUCKET + "?analytics")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("InvalidArgument"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void unqualifiedDeleteStillRemovesBucket() {
|
||||
given()
|
||||
.when()
|
||||
.delete("/" + BUCKET)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
given()
|
||||
.when()
|
||||
.get("/" + BUCKET + "?analytics")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body(containsString("NoSuchBucket"));
|
||||
}
|
||||
}
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class S3AnalyticsConfigurationTest {
|
||||
|
||||
private static final String NS = "http://s3.amazonaws.com/doc/2006-03-01/";
|
||||
|
||||
private static final String EXPORT = "<StorageClassAnalysis><DataExport>"
|
||||
+ "<OutputSchemaVersion>V_1</OutputSchemaVersion>"
|
||||
+ "<Destination><S3BucketDestination>"
|
||||
+ "<Format>CSV</Format>"
|
||||
+ "<BucketAccountId>123456789012</BucketAccountId>"
|
||||
+ "<Bucket>arn:aws:s3:::destination-bucket</Bucket>"
|
||||
+ "<Prefix>reports/</Prefix>"
|
||||
+ "</S3BucketDestination></Destination>"
|
||||
+ "</DataExport></StorageClassAnalysis>";
|
||||
|
||||
private static String body(String inner) {
|
||||
return "<AnalyticsConfiguration xmlns=\"" + NS + "\">" + inner + "</AnalyticsConfiguration>";
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesAnIdWithAnEmptyAnalysisAndNoFilter() {
|
||||
S3AnalyticsConfiguration parsed = S3AnalyticsConfiguration.parse(
|
||||
body("<Id>whole</Id><StorageClassAnalysis/>"));
|
||||
|
||||
assertEquals("whole", parsed.id());
|
||||
assertEquals("<Id>whole</Id><StorageClassAnalysis></StorageClassAnalysis>", parsed.innerXml());
|
||||
}
|
||||
|
||||
@Test
|
||||
void serializesTheExportInAwsOrderWhateverOrderItArrivedIn() {
|
||||
String shuffled = "<StorageClassAnalysis><DataExport>"
|
||||
+ "<Destination><S3BucketDestination>"
|
||||
+ "<Prefix>reports/</Prefix>"
|
||||
+ "<Bucket>arn:aws:s3:::destination-bucket</Bucket>"
|
||||
+ "<BucketAccountId>123456789012</BucketAccountId>"
|
||||
+ "<Format>CSV</Format>"
|
||||
+ "</S3BucketDestination></Destination>"
|
||||
+ "<OutputSchemaVersion>V_1</OutputSchemaVersion>"
|
||||
+ "</DataExport></StorageClassAnalysis>";
|
||||
|
||||
assertEquals("<Id>a</Id>" + EXPORT,
|
||||
S3AnalyticsConfiguration.parse(body("<Id>a</Id>" + shuffled)).innerXml());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesEachSingleFilterPredicate() {
|
||||
assertEquals("<Id>a</Id><Filter><Prefix>logs/</Prefix></Filter><StorageClassAnalysis></StorageClassAnalysis>",
|
||||
S3AnalyticsConfiguration.parse(body(
|
||||
"<Id>a</Id><Filter><Prefix>logs/</Prefix></Filter><StorageClassAnalysis/>")).innerXml());
|
||||
|
||||
assertEquals("<Id>a</Id><Filter><Tag><Key>env</Key><Value>prod</Value></Tag></Filter>"
|
||||
+ "<StorageClassAnalysis></StorageClassAnalysis>",
|
||||
S3AnalyticsConfiguration.parse(body(
|
||||
"<Id>a</Id><Filter><Tag><Key>env</Key><Value>prod</Value></Tag></Filter>"
|
||||
+ "<StorageClassAnalysis/>")).innerXml());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesAnAndConjunctionKeepingEveryTag() {
|
||||
String parsed = S3AnalyticsConfiguration.parse(body("""
|
||||
<Id>a</Id>
|
||||
<Filter>
|
||||
<And>
|
||||
<Prefix>logs/</Prefix>
|
||||
<Tag><Key>env</Key><Value>prod</Value></Tag>
|
||||
<Tag><Key>team</Key><Value>core</Value></Tag>
|
||||
</And>
|
||||
</Filter>
|
||||
<StorageClassAnalysis/>
|
||||
""")).innerXml();
|
||||
|
||||
assertEquals("<Id>a</Id><Filter><And><Prefix>logs/</Prefix>"
|
||||
+ "<Tag><Key>env</Key><Value>prod</Value></Tag>"
|
||||
+ "<Tag><Key>team</Key><Value>core</Value></Tag></And></Filter>"
|
||||
+ "<StorageClassAnalysis></StorageClassAnalysis>", parsed);
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapesValuesOnTheWayBackOut() {
|
||||
String parsed = S3AnalyticsConfiguration.parse(
|
||||
body("<Id>a&b</Id><Filter><Prefix>x<y</Prefix></Filter><StorageClassAnalysis/>")).innerXml();
|
||||
|
||||
assertEquals("<Id>a&b</Id><Filter><Prefix>x<y</Prefix></Filter>"
|
||||
+ "<StorageClassAnalysis></StorageClassAnalysis>", parsed);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsBodiesThatDoNotMatchTheSchema() {
|
||||
// Missing id, missing analysis, stray elements, wrong root element, empty and non-XML
|
||||
// bodies are all MalformedXML on AWS.
|
||||
for (String invalid : new String[]{
|
||||
body(""),
|
||||
body("<Id> </Id><StorageClassAnalysis/>"),
|
||||
body("<StorageClassAnalysis/>"),
|
||||
body("<Id>a</Id>"),
|
||||
body("<Id>a</Id><Unknown/><StorageClassAnalysis/>"),
|
||||
body("<Id>a</Id><Filter></Filter><StorageClassAnalysis/>"),
|
||||
body("<Id>a</Id><Filter><And><Prefix>p</Prefix></And></Filter><StorageClassAnalysis/>"),
|
||||
body("<Id>a</Id><Filter><Tag><Key>k</Key></Tag></Filter><StorageClassAnalysis/>"),
|
||||
"<SomethingElse><Id>a</Id></SomethingElse>",
|
||||
"not xml at all",
|
||||
""}) {
|
||||
AwsException e = assertThrows(AwsException.class, () -> S3AnalyticsConfiguration.parse(invalid),
|
||||
() -> "expected rejection of: " + invalid);
|
||||
assertEquals("MalformedXML", e.getErrorCode());
|
||||
assertEquals(400, e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsExportsOutsideTheSchema() {
|
||||
String unknownVersion = EXPORT.replace("V_1", "V_2");
|
||||
String unknownFormat = EXPORT.replace("CSV", "ORC");
|
||||
String missingBucket = EXPORT.replace("<Bucket>arn:aws:s3:::destination-bucket</Bucket>", "");
|
||||
String missingVersion = EXPORT.replace("<OutputSchemaVersion>V_1</OutputSchemaVersion>", "");
|
||||
String strayElement = EXPORT.replace("<Format>CSV</Format>", "<Format>CSV</Format><Region>eu</Region>");
|
||||
|
||||
for (String invalid : new String[]{
|
||||
unknownVersion, unknownFormat, missingBucket, missingVersion, strayElement}) {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> S3AnalyticsConfiguration.parse(body("<Id>a</Id>" + invalid)),
|
||||
() -> "expected rejection of: " + invalid);
|
||||
assertEquals("MalformedXML", e.getErrorCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
+214
@@ -0,0 +1,214 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class S3InventoryConfigurationIntegrationTest {
|
||||
|
||||
private static final String BUCKET = "inventory-int-test";
|
||||
|
||||
private static String configuration(String id, String extras) {
|
||||
return """
|
||||
<InventoryConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<Id>%s</Id>
|
||||
<IsEnabled>true</IsEnabled>
|
||||
<Destination>
|
||||
<S3BucketDestination>
|
||||
<Format>CSV</Format>
|
||||
<AccountId>123456789012</AccountId>
|
||||
<Bucket>arn:aws:s3:::inventory-destination</Bucket>
|
||||
<Prefix>inventory/</Prefix>
|
||||
</S3BucketDestination>
|
||||
</Destination>
|
||||
<Schedule>
|
||||
<Frequency>Daily</Frequency>
|
||||
</Schedule>
|
||||
<IncludedObjectVersions>All</IncludedObjectVersions>
|
||||
%s
|
||||
</InventoryConfiguration>
|
||||
""".formatted(id, extras);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createBucket() {
|
||||
given()
|
||||
.when()
|
||||
.put("/" + BUCKET)
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code PUT /{bucket}?inventory} must be handled before the unqualified CreateBucket, which
|
||||
* would otherwise answer with BucketAlreadyOwnedByYou. Real S3 stores the configuration and
|
||||
* returns 204.
|
||||
*/
|
||||
@Test
|
||||
@Order(2)
|
||||
void putInventoryConfigurationIsNotTreatedAsCreateBucket() {
|
||||
given()
|
||||
.body(configuration("report1", ""))
|
||||
.when()
|
||||
.put("/" + BUCKET + "?inventory&id=report1")
|
||||
.then()
|
||||
.statusCode(204)
|
||||
.body(not(containsString("BucketAlreadyOwnedByYou")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void getInventoryConfigurationReturnsWhatWasStored() {
|
||||
given()
|
||||
.when()
|
||||
.get("/" + BUCKET + "?inventory&id=report1")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<InventoryConfiguration"))
|
||||
.body(containsString("<Id>report1</Id>"))
|
||||
.body(containsString("<IsEnabled>true</IsEnabled>"))
|
||||
.body(containsString("<Frequency>Daily</Frequency>"))
|
||||
.body(containsString("<IncludedObjectVersions>All</IncludedObjectVersions>"))
|
||||
.body(containsString("<Bucket>arn:aws:s3:::inventory-destination</Bucket>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void putConfigurationWithFilterAndOptionalFieldsKeepsThem() {
|
||||
given()
|
||||
.body(configuration("report2", """
|
||||
<Filter><Prefix>docs/</Prefix></Filter>
|
||||
<OptionalFields>
|
||||
<Field>Size</Field>
|
||||
<Field>LastModifiedDate</Field>
|
||||
</OptionalFields>
|
||||
"""))
|
||||
.when()
|
||||
.put("/" + BUCKET + "?inventory&id=report2")
|
||||
.then()
|
||||
.statusCode(204);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + BUCKET + "?inventory&id=report2")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<Filter><Prefix>docs/</Prefix></Filter>"))
|
||||
.body(containsString("<OptionalFields><Field>Size</Field><Field>LastModifiedDate</Field></OptionalFields>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void listWithoutAnIdReturnsEveryConfiguration() {
|
||||
given()
|
||||
.when()
|
||||
.get("/" + BUCKET + "?inventory")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<ListInventoryConfigurationsResult"))
|
||||
.body(containsString("<Id>report1</Id>"))
|
||||
.body(containsString("<Id>report2</Id>"))
|
||||
.body(containsString("<IsTruncated>false</IsTruncated>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void idInTheBodyMustMatchTheIdInTheQuery() {
|
||||
given()
|
||||
.body(configuration("Different", ""))
|
||||
.when()
|
||||
.put("/" + BUCKET + "?inventory&id=Mismatch")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("MalformedXML"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void unknownIdIsNotFound() {
|
||||
given()
|
||||
.when()
|
||||
.get("/" + BUCKET + "?inventory&id=absent")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body(containsString("NoSuchConfiguration"));
|
||||
|
||||
// AWS does not treat deleting an absent configuration as a no-op.
|
||||
given()
|
||||
.when()
|
||||
.delete("/" + BUCKET + "?inventory&id=absent")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body(containsString("NoSuchConfiguration"));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code DELETE /{bucket}?inventory} must not fall through to the unqualified DeleteBucket
|
||||
* and remove the whole bucket.
|
||||
*/
|
||||
@Test
|
||||
@Order(8)
|
||||
void deleteInventoryConfigurationDoesNotDeleteBucket() {
|
||||
given()
|
||||
.when()
|
||||
.delete("/" + BUCKET + "?inventory&id=report1")
|
||||
.then()
|
||||
.statusCode(204);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + BUCKET + "?inventory&id=report2")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/" + BUCKET + "?inventory&id=report1")
|
||||
.then()
|
||||
.statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void aRequestWithoutAnIdIsRefusedRatherThanGuessedAt() {
|
||||
given()
|
||||
.body(configuration("report1", ""))
|
||||
.when()
|
||||
.put("/" + BUCKET + "?inventory")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("InvalidArgument"));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.delete("/" + BUCKET + "?inventory")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("InvalidArgument"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void unqualifiedDeleteStillRemovesBucket() {
|
||||
given()
|
||||
.when()
|
||||
.delete("/" + BUCKET)
|
||||
.then()
|
||||
.statusCode(204);
|
||||
given()
|
||||
.when()
|
||||
.get("/" + BUCKET + "?inventory")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body(containsString("NoSuchBucket"));
|
||||
}
|
||||
}
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class S3InventoryConfigurationTest {
|
||||
|
||||
private static final String NS = "http://s3.amazonaws.com/doc/2006-03-01/";
|
||||
|
||||
private static final String DESTINATION = "<Destination><S3BucketDestination>"
|
||||
+ "<Format>CSV</Format>"
|
||||
+ "<AccountId>123456789012</AccountId>"
|
||||
+ "<Bucket>arn:aws:s3:::destination-bucket</Bucket>"
|
||||
+ "<Prefix>inventory/</Prefix>"
|
||||
+ "</S3BucketDestination></Destination>";
|
||||
|
||||
private static final String MINIMAL_DESTINATION = "<Destination><S3BucketDestination>"
|
||||
+ "<Format>CSV</Format>"
|
||||
+ "<Bucket>arn:aws:s3:::destination-bucket</Bucket>"
|
||||
+ "</S3BucketDestination></Destination>";
|
||||
|
||||
private static String body(String inner) {
|
||||
return "<InventoryConfiguration xmlns=\"" + NS + "\">" + inner + "</InventoryConfiguration>";
|
||||
}
|
||||
|
||||
private static String required(String destination) {
|
||||
return "<Id>report1</Id><IsEnabled>true</IsEnabled>" + destination
|
||||
+ "<Schedule><Frequency>Daily</Frequency></Schedule>"
|
||||
+ "<IncludedObjectVersions>All</IncludedObjectVersions>";
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesTheRequiredElementsInAwsOrderWhateverOrderTheyArrivedIn() {
|
||||
String shuffled = "<IncludedObjectVersions>All</IncludedObjectVersions>"
|
||||
+ "<Schedule><Frequency>Daily</Frequency></Schedule>"
|
||||
+ "<IsEnabled>true</IsEnabled>"
|
||||
+ MINIMAL_DESTINATION
|
||||
+ "<Id>report1</Id>";
|
||||
|
||||
S3InventoryConfiguration parsed = S3InventoryConfiguration.parse(body(shuffled));
|
||||
|
||||
assertEquals("report1", parsed.id());
|
||||
assertEquals(required(MINIMAL_DESTINATION), parsed.innerXml());
|
||||
}
|
||||
|
||||
@Test
|
||||
void keepsTheFilterOptionalFieldsAndEncryption() {
|
||||
String encrypted = DESTINATION.replace("</S3BucketDestination>",
|
||||
"<Encryption><SSE-KMS><KeyId>arn:aws:kms:us-east-1:123456789012:key/k</KeyId></SSE-KMS></Encryption>"
|
||||
+ "</S3BucketDestination>");
|
||||
String parsed = S3InventoryConfiguration.parse(body(
|
||||
"<Id>report1</Id><IsEnabled>false</IsEnabled>"
|
||||
+ "<Filter><Prefix>docs/</Prefix></Filter>"
|
||||
+ encrypted
|
||||
+ "<IncludedObjectVersions>Current</IncludedObjectVersions>"
|
||||
+ "<Schedule><Frequency>Weekly</Frequency></Schedule>"
|
||||
+ "<OptionalFields><Field>Size</Field><Field>ETag</Field></OptionalFields>")).innerXml();
|
||||
|
||||
assertEquals("<Id>report1</Id><IsEnabled>false</IsEnabled>" + encrypted
|
||||
+ "<Schedule><Frequency>Weekly</Frequency></Schedule>"
|
||||
+ "<Filter><Prefix>docs/</Prefix></Filter>"
|
||||
+ "<IncludedObjectVersions>Current</IncludedObjectVersions>"
|
||||
+ "<OptionalFields><Field>Size</Field><Field>ETag</Field></OptionalFields>", parsed);
|
||||
}
|
||||
|
||||
@Test
|
||||
void serializesSseS3AsAnEmptyElement() {
|
||||
String parsed = S3InventoryConfiguration.parse(body(required(
|
||||
MINIMAL_DESTINATION.replace("</S3BucketDestination>",
|
||||
"<Encryption><SSE-S3/></Encryption></S3BucketDestination>")))).innerXml();
|
||||
|
||||
assertEquals(required(MINIMAL_DESTINATION.replace("</S3BucketDestination>",
|
||||
"<Encryption><SSE-S3/></Encryption></S3BucketDestination>")), parsed);
|
||||
}
|
||||
|
||||
@Test
|
||||
void escapesValuesOnTheWayBackOut() {
|
||||
String parsed = S3InventoryConfiguration.parse(body(required(MINIMAL_DESTINATION)
|
||||
.replace("<Id>report1</Id>", "<Id>a&b</Id>")
|
||||
+ "<Filter><Prefix>x<y</Prefix></Filter>")).innerXml();
|
||||
|
||||
assertEquals(required(MINIMAL_DESTINATION).replace("<Id>report1</Id>", "<Id>a&b</Id>")
|
||||
.replace("<IncludedObjectVersions>", "<Filter><Prefix>x<y</Prefix></Filter><IncludedObjectVersions>"),
|
||||
parsed);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsBodiesThatDoNotMatchTheSchema() {
|
||||
// A missing required element, a stray element, wrong root, empty and non-XML bodies are
|
||||
// all MalformedXML on AWS.
|
||||
String all = required(MINIMAL_DESTINATION);
|
||||
for (String invalid : new String[]{
|
||||
body(""),
|
||||
body(all.replace("<Id>report1</Id>", "<Id> </Id>")),
|
||||
body(all.replace("<Id>report1</Id>", "")),
|
||||
body(all.replace("<IsEnabled>true</IsEnabled>", "")),
|
||||
body(all.replace("<IsEnabled>true</IsEnabled>", "<IsEnabled>yes</IsEnabled>")),
|
||||
body(all.replace("<IncludedObjectVersions>All</IncludedObjectVersions>", "")),
|
||||
body(all.replace("<IncludedObjectVersions>All</IncludedObjectVersions>",
|
||||
"<IncludedObjectVersions>Latest</IncludedObjectVersions>")),
|
||||
body(all.replace("<Schedule><Frequency>Daily</Frequency></Schedule>", "")),
|
||||
body(all.replace("Daily", "Hourly")),
|
||||
body(all.replace(MINIMAL_DESTINATION, "")),
|
||||
body(all + "<Unknown/>"),
|
||||
body(all + "<Filter><Tag><Key>k</Key><Value>v</Value></Tag></Filter>"),
|
||||
body(all + "<OptionalFields><Field>Colour</Field></OptionalFields>"),
|
||||
"<SomethingElse><Id>a</Id></SomethingElse>",
|
||||
"not xml at all",
|
||||
""}) {
|
||||
AwsException e = assertThrows(AwsException.class, () -> S3InventoryConfiguration.parse(invalid),
|
||||
() -> "expected rejection of: " + invalid);
|
||||
assertEquals("MalformedXML", e.getErrorCode());
|
||||
assertEquals(400, e.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsDestinationsOutsideTheSchema() {
|
||||
String unknownFormat = DESTINATION.replace("CSV", "JSON");
|
||||
String missingFormat = DESTINATION.replace("<Format>CSV</Format>", "");
|
||||
String missingBucket = DESTINATION.replace("<Bucket>arn:aws:s3:::destination-bucket</Bucket>", "");
|
||||
String strayElement = DESTINATION.replace("<Prefix>inventory/</Prefix>",
|
||||
"<Prefix>inventory/</Prefix><Region>eu</Region>");
|
||||
String kmsWithoutKey = DESTINATION.replace("</S3BucketDestination>",
|
||||
"<Encryption><SSE-KMS/></Encryption></S3BucketDestination>");
|
||||
String twoEncryptions = DESTINATION.replace("</S3BucketDestination>",
|
||||
"<Encryption><SSE-S3/><SSE-KMS><KeyId>k</KeyId></SSE-KMS></Encryption></S3BucketDestination>");
|
||||
|
||||
for (String invalid : new String[]{
|
||||
unknownFormat, missingFormat, missingBucket, strayElement, kmsWithoutKey, twoEncryptions}) {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> S3InventoryConfiguration.parse(body(required(invalid))),
|
||||
() -> "expected rejection of: " + invalid);
|
||||
assertEquals("MalformedXML", e.getErrorCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -837,6 +837,59 @@ class S3ServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void analyticsAndInventoryConfigurationsOnABucketWithoutAnyBehaveAsEmpty() {
|
||||
// A bucket persisted before these fields existed deserializes with null maps, which is
|
||||
// the same shape a freshly created bucket has, so neither may fault.
|
||||
s3Service.createBucket("no-configs", "us-east-1");
|
||||
|
||||
assertTrue(s3Service.listBucketAnalyticsConfigurations("no-configs")
|
||||
.contains("<IsTruncated>false</IsTruncated>"));
|
||||
assertTrue(s3Service.listBucketInventoryConfigurations("no-configs")
|
||||
.contains("<IsTruncated>false</IsTruncated>"));
|
||||
assertEquals("NoSuchConfiguration", assertThrows(AwsException.class,
|
||||
() -> s3Service.getBucketAnalyticsConfiguration("no-configs", "any")).getErrorCode());
|
||||
assertEquals("NoSuchConfiguration", assertThrows(AwsException.class,
|
||||
() -> s3Service.deleteBucketInventoryConfiguration("no-configs", "any")).getErrorCode());
|
||||
|
||||
// And the first put still lands on it, in its own map.
|
||||
s3Service.putBucketAnalyticsConfiguration("no-configs", "first", "<Id>first</Id>");
|
||||
s3Service.putBucketInventoryConfiguration("no-configs", "first", "<Id>first</Id>");
|
||||
assertTrue(s3Service.getBucketAnalyticsConfiguration("no-configs", "first")
|
||||
.contains("<AnalyticsConfiguration"));
|
||||
assertTrue(s3Service.getBucketInventoryConfiguration("no-configs", "first")
|
||||
.contains("<InventoryConfiguration"));
|
||||
assertEquals("NoSuchConfiguration", assertThrows(AwsException.class,
|
||||
() -> s3Service.getBucketMetricsConfiguration("no-configs", "first")).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void analyticsAndInventoryConfigurationsDoNotOutliveTheirBucket() {
|
||||
s3Service.createBucket("recycled-configs", "us-east-1");
|
||||
s3Service.putBucketAnalyticsConfiguration("recycled-configs", "old", "<Id>old</Id>");
|
||||
s3Service.putBucketInventoryConfiguration("recycled-configs", "old", "<Id>old</Id>");
|
||||
s3Service.deleteBucket("recycled-configs");
|
||||
|
||||
s3Service.createBucket("recycled-configs", "us-east-1");
|
||||
|
||||
assertEquals("NoSuchConfiguration", assertThrows(AwsException.class,
|
||||
() -> s3Service.getBucketAnalyticsConfiguration("recycled-configs", "old")).getErrorCode());
|
||||
assertEquals("NoSuchConfiguration", assertThrows(AwsException.class,
|
||||
() -> s3Service.getBucketInventoryConfiguration("recycled-configs", "old")).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void analyticsAndInventoryConfigurationsSurviveAJacksonRoundTrip() throws Exception {
|
||||
var mapper = new com.fasterxml.jackson.databind.ObjectMapper().findAndRegisterModules();
|
||||
Bucket bucket = new Bucket("persisted-configs");
|
||||
bucket.setAnalyticsConfigurations(new java.util.LinkedHashMap<>(Map.of("a", "<Id>a</Id>")));
|
||||
bucket.setInventoryConfigurations(new java.util.LinkedHashMap<>(Map.of("i", "<Id>i</Id>")));
|
||||
|
||||
Bucket reloaded = mapper.readValue(mapper.writeValueAsString(bucket), Bucket.class);
|
||||
assertEquals("<Id>a</Id>", reloaded.getAnalyticsConfigurations().get("a"));
|
||||
assertEquals("<Id>i</Id>", reloaded.getInventoryConfigurations().get("i"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void metricsConfigurationsSurviveAJacksonRoundTrip() throws Exception {
|
||||
// Bucket records are persisted as JSON, so the configurations have to come back after a
|
||||
|
||||
Reference in new issue
Block a user