diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java index 2f39091c45..9a864b3a34 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java @@ -179,6 +179,10 @@ public class S3Controller { if (hasQueryParam(uriInfo, "website")) { return handlePutBucketWebsite(bucket, body); } + if (hasQueryParam(uriInfo, "logging")) { + s3Service.putBucketLogging(bucket, new String(body, StandardCharsets.UTF_8)); + return Response.ok().build(); + } if (hasQueryParam(uriInfo, "policy")) { s3Service.putBucketPolicy(bucket, new String(body, StandardCharsets.UTF_8)); return Response.ok().build(); @@ -330,6 +334,11 @@ public class S3Controller { if (hasQueryParam(uriInfo, "website")) { return handleGetBucketWebsite(bucket); } + if (hasQueryParam(uriInfo, "logging")) { + return Response.ok(s3Service.getBucketLogging(bucket)) + .type("application/xml") + .build(); + } if (hasQueryParam(uriInfo, "policy")) { return Response.ok(s3Service.getBucketPolicy(bucket)).build(); } 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.java index a1fe4ce53f..0ed35e2b70 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 @@ -206,6 +206,39 @@ public class S3Service { return bucketStore.scan(key -> true); } + public void putBucketLogging(String bucketName, String loggingConfigurationXml) { + Bucket bucket = bucketStore.get(bucketName) + .orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404)); + + if (loggingConfigurationXml == null || loggingConfigurationXml.isBlank()) { + bucket.setLoggingConfiguration(null); + } else { + String targetBucket = XmlParser.extractFirst(loggingConfigurationXml, "TargetBucket", null); + if (targetBucket == null) { + bucket.setLoggingConfiguration(null); + } else { + bucket.setLoggingConfiguration(loggingConfigurationXml); + } + } + + bucketStore.put(bucketName, bucket); + } + + public String getBucketLogging(String bucketName) { + Bucket bucket = bucketStore.get(bucketName) + .orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404)); + + if (bucket.getLoggingConfiguration() == null || bucket.getLoggingConfiguration().isBlank()) { + return new XmlBuilder() + .raw("") + .start("BucketLoggingStatus", AwsNamespaces.S3) + .end("BucketLoggingStatus") + .build(); + } + + return bucket.getLoggingConfiguration(); + } + public S3Object putObject(String bucketName, String key, byte[] data, String contentType, Map metadata) { return putObject(bucketName, key, data, contentType, metadata, new PutObjectOptions()); diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/model/Bucket.java b/src/main/java/io/github/hectorvent/floci/services/s3/model/Bucket.java index 69e9ff8d2a..5d168a4b29 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/model/Bucket.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/model/Bucket.java @@ -14,6 +14,7 @@ public class Bucket { private String name; private Instant creationDate; private String versioningStatus; // null (never enabled), "Enabled", "Suspended" + private String loggingConfiguration; private Map tags; private NotificationConfiguration notificationConfiguration; private boolean objectLockEnabled; @@ -49,6 +50,14 @@ public class Bucket { public String getVersioningStatus() { return versioningStatus; } public void setVersioningStatus(String versioningStatus) { this.versioningStatus = versioningStatus; } + public String getLoggingConfiguration() { + return loggingConfiguration; + } + + public void setLoggingConfiguration(String loggingConfiguration) { + this.loggingConfiguration = loggingConfiguration; + } + public Map getTags() { return tags; } public void setTags(Map tags) { this.tags = tags; } diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java index 2e13b9568c..7bebada966 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java @@ -2046,6 +2046,73 @@ class S3IntegrationTest { .body(equalTo("")); } + @Test + @Order(206) + void bucketLogging_roundTripAndDisable() { + String bucket = "bucket-logging-test"; + String targetBucket = "bucket-logging-target"; + + given() + .when().put("/" + bucket) + .then().statusCode(200); + + given() + .when().put("/" + targetBucket) + .then().statusCode(200); + + given() + .queryParam("logging", "") + .when().get("/" + bucket) + .then() + .statusCode(200) + .body(containsString("BucketLoggingStatus")) + .body(not(containsString("LoggingEnabled"))); + + String loggingXml = """ + + + %s + logs/ + + + """.formatted(targetBucket); + + given() + .queryParam("logging", "") + .body(loggingXml) + .when().put("/" + bucket) + .then() + .statusCode(200); + + given() + .queryParam("logging", "") + .when().get("/" + bucket) + .then() + .statusCode(200) + .body(containsString("LoggingEnabled")) + .body(containsString(targetBucket)) + .body(containsString("logs/")); + + String disabledXml = """ + + """; + + given() + .queryParam("logging", "") + .body(disabledXml) + .when().put("/" + bucket) + .then() + .statusCode(200); + + given() + .queryParam("logging", "") + .when().get("/" + bucket) + .then() + .statusCode(200) + .body(containsString("BucketLoggingStatus")) + .body(not(containsString("LoggingEnabled"))); + } + private static String customerKeyMd5(String customerKey) { try { byte[] md5 = MessageDigest.getInstance("MD5").digest(Base64.getDecoder().decode(customerKey));