Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): persist inline x-amz-tagging header on PutObject (#987)
opcode-sec8fb07d265451
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/S3Test.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/S3Test.javaindex f4cf335217..a977e667d7 100644--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/S3Test.java+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/S3Test.java@@ -427,4 +427,104 @@ class S3Test {} catch (Exception ignored) {}}}++ @Test+ @Order(27)+ void putObjectPersistsInlineTaggingHeader() {+ String bucket = TestFixtures.uniqueName("inline-tag-bucket");+ String key = "inline-tagged.txt";+ s3.createBucket(CreateBucketRequest.builder().bucket(bucket).build());+ try {+ s3.putObject(PutObjectRequest.builder()+ .bucket(bucket).key(key)+ .tagging(Tagging.builder()+ .tagSet(+ software.amazon.awssdk.services.s3.model.Tag.builder().key("env").value("test").build(),+ software.amazon.awssdk.services.s3.model.Tag.builder().key("project").value("floci").build()+ )+ .build())+ .build(),+ RequestBody.fromString("inline-tagged content"));++ GetObjectTaggingResponse response = s3.getObjectTagging(+ GetObjectTaggingRequest.builder().bucket(bucket).key(key).build());++ assertThat(response.tagSet()).hasSize(2);+ assertThat(response.tagSet())+ .anyMatch(t -> "env".equals(t.key()) && "test".equals(t.value()))+ .anyMatch(t -> "project".equals(t.key()) && "floci".equals(t.value()));+ } finally {+ try {+ s3.deleteObject(DeleteObjectRequest.builder().bucket(bucket).key(key).build());+ } catch (Exception ignored) {}+ try {+ s3.deleteBucket(DeleteBucketRequest.builder().bucket(bucket).build());+ } catch (Exception ignored) {}+ }+ }++ @Test+ @Order(28)+ void putObjectInlineTaggingRejectsMalformedPair() {+ assertInlineTaggingHeaderRejected("nokeyhasvalue", 400, "InvalidArgument");+ }++ @Test+ @Order(29)+ void putObjectInlineTaggingRejectsEmptyKey() {+ assertInlineTaggingHeaderRejected("=value", 400, "InvalidArgument");+ }++ @Test+ @Order(30)+ void putObjectInlineTaggingRejectsDuplicateKey() {+ assertInlineTaggingHeaderRejected("env=a&env=b", 400, "InvalidArgument");+ }++ @Test+ @Order(31)+ void putObjectInlineTaggingRejectsTooManyTags() {+ StringBuilder header = new StringBuilder();+ for (int i = 0; i < 11; i++) {+ if (i > 0) header.append('&');+ header.append("k").append(i).append("=v").append(i);+ }+ assertInlineTaggingHeaderRejected(header.toString(), 400, "BadRequest");+ }++ @Test+ @Order(32)+ void putObjectInlineTaggingRejectsOversizedHeader() {+ // The size check runs before pair parsing, so a single huge value trips it+ // without first being rejected by the >10-tag rule.+ String huge = "k=" + "v".repeat(8 * 1024 + 1);+ assertInlineTaggingHeaderRejected(huge, 400, "InvalidArgument");+ }++ private void assertInlineTaggingHeaderRejected(String rawTaggingHeader, int expectedStatus, String expectedErrorCode) {+ String bucket = TestFixtures.uniqueName("inline-tag-reject");+ String key = "rejected.txt";+ s3.createBucket(CreateBucketRequest.builder().bucket(bucket).build());+ try {+ assertThatThrownBy(() -> s3.putObject(+ PutObjectRequest.builder()+ .bucket(bucket).key(key)+ .overrideConfiguration(o -> o.putHeader("x-amz-tagging", rawTaggingHeader))+ .build(),+ RequestBody.fromString("content")))+ .isInstanceOf(S3Exception.class)+ .satisfies(e -> {+ S3Exception s3e = (S3Exception) e;+ assertThat(s3e.statusCode()).isEqualTo(expectedStatus);+ assertThat(s3e.awsErrorDetails().errorCode()).isEqualTo(expectedErrorCode);+ });+ } finally {+ try {+ s3.deleteObject(DeleteObjectRequest.builder().bucket(bucket).key(key).build());+ } catch (Exception ignored) {}+ try {+ s3.deleteBucket(DeleteBucketRequest.builder().bucket(bucket).build());+ } catch (Exception ignored) {}+ }+ }}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.javaindex 0b6d6ccabf..0adec0de25 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@@ -437,6 +437,7 @@ public class S3Controller {@HeaderParam("Content-Encoding") String contentEncoding,@HeaderParam("x-amz-content-sha256") String contentSha256,@HeaderParam("x-amz-copy-source") String copySource,+ @HeaderParam("x-amz-tagging") String tagging,@HeaderParam("If-Match") String ifMatch,@HeaderParam("If-None-Match") String ifNoneMatch,@QueryParam("uploadId") String uploadId,@@ -483,6 +484,8 @@ public class S3Controller {return preconditionResponse;}+ Map<String, String> inlineTags = parseInlineTaggingHeader(tagging);+String lockMode = httpHeaders.getHeaderString("x-amz-object-lock-mode");String retainUntilStr = httpHeaders.getHeaderString("x-amz-object-lock-retain-until-date");String legalHold = httpHeaders.getHeaderString("x-amz-object-lock-legal-hold");@@ -508,7 +511,8 @@ public class S3Controller {.withServerSideEncryption(serverSideEncryption).withAcl(cannedAcl).withChecksumAlgorithm(checksumAlgorithm)- .withClientChecksum(extractChecksumFromHeaders(httpHeaders)));+ .withClientChecksum(extractChecksumFromHeaders(httpHeaders))+ .withTagging(inlineTags));var resp = Response.ok().header("ETag", obj.getETag());if (obj.getVersionId() != null) {resp.header("x-amz-version-id", obj.getVersionId());@@ -2271,6 +2275,62 @@ public class S3Controller {}}+ private boolean hasQueryParam(UriInfo uriInfo, String param) {+ if (uriInfo.getQueryParameters().containsKey(param)) return true;+ String query = uriInfo.getRequestUri().getQuery();+ if (query == null) return false;+ return query.equals(param) || query.contains(param + "&") || query.contains("&" + param);+ }++ private static final int MAX_INLINE_TAGS = 10;+ private static final int MAX_INLINE_TAGGING_HEADER_BYTES = 8 * 1024;++ /**+ * Parses an {@code x-amz-tagging} request-header value (URL-encoded+ * {@code k=v&k=v}) into a tag map. Returns an empty map for null or blank input.+ *+ * <p>Note: the error codes thrown here ({@code InvalidArgument} for malformed input,+ * {@code BadRequest} for exceeding the 10-tag limit) match real-AWS S3 behavior+ * observed in practice but are not in the S3 Smithy service model.+ */+ private static Map<String, String> parseInlineTaggingHeader(String header) {+ if (header == null || header.isEmpty()) {+ return Map.of();+ }+ if (header.getBytes(StandardCharsets.UTF_8).length > MAX_INLINE_TAGGING_HEADER_BYTES) {+ throw new AwsException("InvalidArgument",+ "The x-amz-tagging header exceeds the 8 KB limit.", 400);+ }+ Map<String, String> tags = new LinkedHashMap<>();+ for (String pair : header.split("&")) {+ if (pair.isEmpty()) {+ continue;+ }+ int eq = pair.indexOf('=');+ if (eq < 0) {+ throw new AwsException("InvalidArgument",+ "The x-amz-tagging header is malformed: missing '=' in pair.", 400);+ }+ String rawKey = pair.substring(0, eq);+ String rawValue = pair.substring(eq + 1);+ if (rawKey.isEmpty()) {+ throw new AwsException("InvalidArgument",+ "The x-amz-tagging header is malformed: empty tag key.", 400);+ }+ String tagKey = URLDecoder.decode(rawKey, StandardCharsets.UTF_8);+ String tagValue = URLDecoder.decode(rawValue, StandardCharsets.UTF_8);+ if (tags.put(tagKey, tagValue) != null) {+ throw new AwsException("InvalidArgument",+ "The x-amz-tagging header contains duplicate tag key: " + tagKey, 400);+ }+ }+ if (tags.size() > MAX_INLINE_TAGS) {+ throw new AwsException("BadRequest",+ "Object tags cannot be greater than " + MAX_INLINE_TAGS, 400);+ }+ return tags;+ }+/*** Extracts the object key from the raw Vert.x request URI, preserving leading slashes* that JAX-RS path normalization would otherwise strip.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.javaindex 95ba239495..27d22a29fa 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@@ -297,6 +297,9 @@ public class S3Service {object.setCacheControl(effectiveOptions.getCacheControl());object.setServerSideEncryption(normalizedServerSideEncryption);object.setAcl(cannedObjectAclXml(effectiveOptions.getAcl()));+ if (effectiveOptions.getTagging() != null && !effectiveOptions.getTagging().isEmpty()) {+ object.setTags(new HashMap<>(effectiveOptions.getTagging()));+ }if (bucket.isVersioningEnabled()) {String versionId = UUID.randomUUID().toString();diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/model/PutObjectOptions.java b/src/main/java/io/github/hectorvent/floci/services/s3/model/PutObjectOptions.javaindex 41e1de1623..83d612ba4f 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/model/PutObjectOptions.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/model/PutObjectOptions.java@@ -1,6 +1,7 @@package io.github.hectorvent.floci.services.s3.model;import java.time.Instant;+import java.util.Map;public class PutObjectOptions {private String storageClass;@@ -13,6 +14,7 @@ public class PutObjectOptions {private String serverSideEncryption;private String acl;private String checksumAlgorithm;+ private Map<String, String> tagging;public String getStorageClass() { return storageClass; }public PutObjectOptions withStorageClass(String storageClass) { this.storageClass = storageClass; return this; }@@ -44,6 +46,9 @@ public class PutObjectOptions {public String getChecksumAlgorithm() { return checksumAlgorithm; }public PutObjectOptions withChecksumAlgorithm(String checksumAlgorithm) { this.checksumAlgorithm = checksumAlgorithm; return this; }+ public Map<String, String> getTagging() { return tagging; }+ public PutObjectOptions withTagging(Map<String, String> tagging) { this.tagging = tagging; return this; }+private S3Checksum clientChecksum;public S3Checksum getClientChecksum() { return clientChecksum; }public PutObjectOptions withClientChecksum(S3Checksum clientChecksum) { this.clientChecksum = clientChecksum; return this; }