This is an automated email from the ASF dual-hosted git repository.

chihsuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new c1a9b117cbc HDDS-15139. Centralize S3 payload header classification 
(#11543)
c1a9b117cbc is described below

commit c1a9b117cbcc52dd9ffa0c6ece8b1fbfc00afa1d
Author: KUAN-HAO HUANG <[email protected]>
AuthorDate: Wed Oct 21 13:05:05 2026 +0100

    HDDS-15139. Centralize S3 payload header classification (#11543)
    
    Co-authored-by: Siyao Meng <[email protected]>
    Co-authored-by: Chi-Hsuan Huang <[email protected]>
---
 .../hadoop/ozone/s3/endpoint/EndpointBase.java     |  18 ++--
 .../s3/signature/AuthorizationV4QueryParser.java   |   3 +-
 .../hadoop/ozone/s3/util/S3ContentSha256Type.java  |  65 +++++++++++++
 .../org/apache/hadoop/ozone/s3/util/S3Utils.java   |  28 ------
 .../hadoop/ozone/s3/endpoint/TestObjectPut.java    | 104 +++++++++++++++------
 .../ozone/s3/util/TestS3ContentSha256Type.java     |  89 ++++++++++++++++++
 6 files changed, 241 insertions(+), 66 deletions(-)

diff --git 
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/EndpointBase.java
 
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/EndpointBase.java
index eb713ea336e..53c930247a3 100644
--- 
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/EndpointBase.java
+++ 
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/EndpointBase.java
@@ -47,16 +47,12 @@
 import static 
org.apache.hadoop.ozone.s3.util.S3Consts.RESERVED_USER_METADATA_KEY_PREFIX;
 import static org.apache.hadoop.ozone.s3.util.S3Consts.STORAGE_CLASS_HEADER;
 import static org.apache.hadoop.ozone.s3.util.S3Consts.STORAGE_CONFIG_HEADER;
-import static 
org.apache.hadoop.ozone.s3.util.S3Consts.STREAMING_AWS4_HMAC_SHA256_PAYLOAD;
-import static 
org.apache.hadoop.ozone.s3.util.S3Consts.STREAMING_AWS4_HMAC_SHA256_PAYLOAD_TRAILER;
 import static org.apache.hadoop.ozone.s3.util.S3Consts.TAG_HEADER;
 import static org.apache.hadoop.ozone.s3.util.S3Consts.TAG_KEY_LENGTH_LIMIT;
 import static org.apache.hadoop.ozone.s3.util.S3Consts.TAG_NUM_LIMIT;
 import static org.apache.hadoop.ozone.s3.util.S3Consts.TAG_REGEX_PATTERN;
 import static org.apache.hadoop.ozone.s3.util.S3Consts.TAG_VALUE_LENGTH_LIMIT;
 import static org.apache.hadoop.ozone.s3.util.S3Consts.X_AMZ_TRAILER;
-import static org.apache.hadoop.ozone.s3.util.S3Utils.hasMultiChunksPayload;
-import static org.apache.hadoop.ozone.s3.util.S3Utils.hasUnsignedPayload;
 import static org.apache.hadoop.ozone.s3.util.S3Utils.urlDecode;
 import static 
org.apache.hadoop.ozone.s3.util.S3Utils.validateMultiChunksUpload;
 import static org.apache.hadoop.ozone.s3.util.S3Utils.validateSignatureHeader;
@@ -129,6 +125,7 @@
 import org.apache.hadoop.ozone.s3.signature.SignatureInfo;
 import org.apache.hadoop.ozone.s3.util.AuditUtils;
 import org.apache.hadoop.ozone.s3.util.ReadConsistencyContext;
+import org.apache.hadoop.ozone.s3.util.S3ContentSha256Type;
 import org.apache.hadoop.ozone.s3.util.S3GActionIamMapper;
 import org.apache.hadoop.ozone.s3.util.S3Utils;
 import org.apache.hadoop.security.UserGroupInformation;
@@ -890,14 +887,15 @@ protected static int parsePartNumberMarker(String 
partNumberMarker) {
   protected S3ChunkInputStreamInfo getS3ChunkInputStreamInfo(
       InputStream body, long contentLength, String amzDecodedLength, String 
keyPath) throws OS3Exception {
     final String amzContentSha256Header = 
validateSignatureHeader(getHeaders(), keyPath, signatureInfo.isSignPayload());
+    final S3ContentSha256Type payloadType = 
S3ContentSha256Type.fromHeaderValue(amzContentSha256Header);
     final InputStream chunkInputStream;
     final long effectiveLength;
-    if (hasMultiChunksPayload(amzContentSha256Header)) {
+    if (payloadType.isStreaming()) {
       validateMultiChunksUpload(getHeaders(), amzDecodedLength, keyPath);
-      if (hasUnsignedPayload(amzContentSha256Header)) {
+      if (payloadType.isUnsigned()) {
         chunkInputStream = new UnsignedChunksInputStream(body);
       } else {
-        TrailerHeader trailerHeader = 
STREAMING_AWS4_HMAC_SHA256_PAYLOAD_TRAILER.equals(amzContentSha256Header)
+        TrailerHeader trailerHeader = payloadType == 
S3ContentSha256Type.STREAMING_AWS4_HMAC_SHA256_PAYLOAD_TRAILER
             ? 
TrailerHeader.fromHeader(getHeaders().getHeaderString(X_AMZ_TRAILER), keyPath) 
: TrailerHeader.NONE;
         chunkInputStream = new SignedChunksInputStream(body, keyPath, 
trailerHeader);
       }
@@ -915,16 +913,14 @@ protected S3ChunkInputStreamInfo 
getS3ChunkInputStreamInfo(
     // and Sha256Digest is used for "x-amz-content-sha256" header verification
     List<MessageDigest> digests = new ArrayList<>();
     digests.add(getMD5DigestInstance());
-    if (!hasUnsignedPayload(amzContentSha256Header) && 
!hasMultiChunksPayload(amzContentSha256Header)) {
+    if (!payloadType.isUnsigned() && !payloadType.isStreaming()) {
       digests.add(getSha256DigestInstance());
     }
     MultiDigestInputStream multiDigestInputStream =
         new MultiDigestInputStream(chunkInputStream, digests);
     // Header auth only: for a presigned (query) request the payload hash is 
not part of the signed
     // canonical request, so its seed signature cannot start the chunk 
signature chain.
-    boolean verifyChunkSignature = signatureInfo.isSignPayload()
-        && (STREAMING_AWS4_HMAC_SHA256_PAYLOAD.equals(amzContentSha256Header)
-        || 
STREAMING_AWS4_HMAC_SHA256_PAYLOAD_TRAILER.equals(amzContentSha256Header));
+    boolean verifyChunkSignature = signatureInfo.isSignPayload() && 
payloadType.isHmacSignedStreaming();
     if (verifyChunkSignature && 
OzoneSecurityUtil.isSecurityEnabled(getOzoneConfiguration())
         && 
!OzoneManagerVersion.S3_DERIVED_KEY.isSupportedBy(getClientProtocol().getOmVersion()))
 {
       OS3Exception ex = newError(S3ErrorTable.NOT_IMPLEMENTED, keyPath);
diff --git 
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/signature/AuthorizationV4QueryParser.java
 
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/signature/AuthorizationV4QueryParser.java
index 89b4b659252..e1433a93227 100644
--- 
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/signature/AuthorizationV4QueryParser.java
+++ 
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/signature/AuthorizationV4QueryParser.java
@@ -21,6 +21,7 @@
 import static org.apache.commons.lang3.StringUtils.isEmpty;
 import static 
org.apache.hadoop.ozone.s3.signature.SignatureProcessor.AWS4_SIGNING_ALGORITHM;
 import static 
org.apache.hadoop.ozone.s3.signature.SignatureProcessor.DATE_FORMATTER;
+import static org.apache.hadoop.ozone.s3.util.S3Consts.UNSIGNED_PAYLOAD;
 import static org.apache.hadoop.ozone.s3.util.S3Utils.urlDecode;
 
 import com.google.common.annotations.VisibleForTesting;
@@ -94,7 +95,7 @@ public SignatureInfo parseSignature() throws 
MalformedResourceException {
         .setAlgorithm(queryParameters.get("X-Amz-Algorithm"))
         .setService(credential.getAwsService())
         .setSignPayload(false)
-        .setPayloadHash("UNSIGNED-PAYLOAD")
+        .setPayloadHash(UNSIGNED_PAYLOAD)
         .build();
   }
 
diff --git 
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3ContentSha256Type.java
 
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3ContentSha256Type.java
new file mode 100644
index 00000000000..8da16ba00d8
--- /dev/null
+++ 
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3ContentSha256Type.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.ozone.s3.util;
+
+import java.util.Objects;
+
+/** Classifies the x-amz-content-sha256 header without changing its signed 
value. */
+public enum S3ContentSha256Type {
+  UNSIGNED_PAYLOAD,
+  STREAMING_UNSIGNED_PAYLOAD_TRAILER,
+  STREAMING_AWS4_HMAC_SHA256_PAYLOAD,
+  STREAMING_AWS4_HMAC_SHA256_PAYLOAD_TRAILER,
+  STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD,
+  STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD_TRAILER,
+  OTHER,
+  OTHER_STREAMING;
+
+  public static S3ContentSha256Type fromHeaderValue(String headerValue) {
+    Objects.requireNonNull(headerValue);
+    switch (headerValue) {
+    case S3Consts.UNSIGNED_PAYLOAD:
+      return UNSIGNED_PAYLOAD;
+    case S3Consts.STREAMING_UNSIGNED_PAYLOAD_TRAILER:
+      return STREAMING_UNSIGNED_PAYLOAD_TRAILER;
+    case S3Consts.STREAMING_AWS4_HMAC_SHA256_PAYLOAD:
+      return STREAMING_AWS4_HMAC_SHA256_PAYLOAD;
+    case S3Consts.STREAMING_AWS4_HMAC_SHA256_PAYLOAD_TRAILER:
+      return STREAMING_AWS4_HMAC_SHA256_PAYLOAD_TRAILER;
+    case S3Consts.STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD:
+      return STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD;
+    case S3Consts.STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD_TRAILER:
+      return STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD_TRAILER;
+    default:
+      // Preserve streaming detection for unrecognized algorithms; other 
values are not validated here.
+      return headerValue.startsWith(S3Consts.MULTI_CHUNKS_UPLOAD_PREFIX) ? 
OTHER_STREAMING : OTHER;
+    }
+  }
+
+  public boolean isUnsigned() {
+    return this == UNSIGNED_PAYLOAD || this == 
STREAMING_UNSIGNED_PAYLOAD_TRAILER;
+  }
+
+  public boolean isStreaming() {
+    return this != UNSIGNED_PAYLOAD && this != OTHER;
+  }
+
+  public boolean isHmacSignedStreaming() {
+    return this == STREAMING_AWS4_HMAC_SHA256_PAYLOAD || this == 
STREAMING_AWS4_HMAC_SHA256_PAYLOAD_TRAILER;
+  }
+}
diff --git 
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3Utils.java
 
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3Utils.java
index 6acd1ef9e0f..cee5123aed8 100644
--- 
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3Utils.java
+++ 
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3Utils.java
@@ -24,18 +24,14 @@
 import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.newError;
 import static org.apache.hadoop.ozone.s3.util.S3Consts.AWS_CHUNKED;
 import static 
org.apache.hadoop.ozone.s3.util.S3Consts.DECODED_CONTENT_LENGTH_HEADER;
-import static 
org.apache.hadoop.ozone.s3.util.S3Consts.MULTI_CHUNKS_UPLOAD_PREFIX;
-import static 
org.apache.hadoop.ozone.s3.util.S3Consts.STREAMING_UNSIGNED_PAYLOAD_TRAILER;
 import static org.apache.hadoop.ozone.s3.util.S3Consts.UNSIGNED_PAYLOAD;
 import static org.apache.hadoop.ozone.s3.util.S3Consts.X_AMZ_CONTENT_SHA256;
 
-import jakarta.annotation.Nonnull;
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
 import java.util.Arrays;
 import java.util.Base64;
-import java.util.Objects;
 import java.util.stream.Collectors;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.HttpHeaders;
@@ -153,30 +149,6 @@ public static WebApplicationException 
wrapOS3Exception(OS3Exception ex) {
             .build());
   }
 
-  public static boolean hasUnsignedPayload(@Nonnull String 
amzContentSha256Header) {
-    Objects.requireNonNull(amzContentSha256Header);
-    return amzContentSha256Header.equals(UNSIGNED_PAYLOAD) ||
-        amzContentSha256Header.equals(STREAMING_UNSIGNED_PAYLOAD_TRAILER);
-  }
-
-  public static boolean hasMultiChunksPayload(@Nonnull String 
amzContentSha256Header) {
-    Objects.requireNonNull(amzContentSha256Header);
-    // Multiple chunk uploads
-    // - https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-streaming.html
-    // - 
https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-streaming-trailers.html
-    // Possible values
-    // - STREAMING-UNSIGNED-PAYLOAD-TRAILER
-    // - STREAMING-AWS4-HMAC-SHA256-PAYLOAD
-    // - STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER
-    // - STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD
-    // - STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD-TRAILER
-    // Currently since all the multi chunks values have x-amz-content-sha256 
header value that starts
-    // with STREAMING, we can use this prefix to differentiates between multi 
chunks and single chunks upload.
-    // In the future if there are more multi chunks signature algorithms that 
has the same prefix,
-    // this function will be able to handle detect it.
-    return amzContentSha256Header.startsWith(MULTI_CHUNKS_UPLOAD_PREFIX);
-  }
-
   public static void validateMultiChunksUpload(HttpHeaders headers, String 
amzDecodedContentLength,
                                                String resource) throws 
OS3Exception {
     final String contentEncoding = 
headers.getHeaderString(HttpHeaders.CONTENT_ENCODING);
diff --git 
a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectPut.java
 
b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectPut.java
index e1f6bb1119b..13061989758 100644
--- 
a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectPut.java
+++ 
b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectPut.java
@@ -47,6 +47,7 @@
 import static org.apache.hadoop.ozone.s3.util.S3Consts.TAG_KEY_LENGTH_LIMIT;
 import static org.apache.hadoop.ozone.s3.util.S3Consts.TAG_NUM_LIMIT;
 import static org.apache.hadoop.ozone.s3.util.S3Consts.TAG_VALUE_LENGTH_LIMIT;
+import static org.apache.hadoop.ozone.s3.util.S3Consts.UNSIGNED_PAYLOAD;
 import static org.apache.hadoop.ozone.s3.util.S3Consts.X_AMZ_CONTENT_SHA256;
 import static org.apache.hadoop.ozone.s3.util.S3Consts.X_AMZ_TRAILER;
 import static org.apache.hadoop.ozone.s3.util.S3Utils.parseETag;
@@ -76,6 +77,7 @@
 import com.google.common.collect.ImmutableMap;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.nio.charset.StandardCharsets;
 import java.security.MessageDigest;
@@ -108,6 +110,9 @@
 import org.apache.hadoop.ozone.client.protocol.ClientProtocol;
 import org.apache.hadoop.ozone.om.helpers.BucketLayout;
 import org.apache.hadoop.ozone.s3.HeaderPreprocessor;
+import org.apache.hadoop.ozone.s3.MultiDigestInputStream;
+import org.apache.hadoop.ozone.s3.SignedChunksInputStream;
+import org.apache.hadoop.ozone.s3.UnsignedChunksInputStream;
 import org.apache.hadoop.ozone.s3.exception.OS3Exception;
 import org.apache.hadoop.ozone.s3.exception.S3ErrorTable;
 import org.apache.hadoop.ozone.s3.util.S3Consts;
@@ -468,43 +473,90 @@ void testPutObjectRejectsDecodedLengthEndingMidChunk() {
     assertKeyWasNotCommitted();
   }
 
-  static Stream<Arguments> chunkSignatureVerificationCases() {
+  static Stream<Arguments> payloadClassificationCases() {
     return Stream.of(
-        Arguments.of(STREAMING_AWS4_HMAC_SHA256_PAYLOAD, true),
-        Arguments.of(STREAMING_AWS4_HMAC_SHA256_PAYLOAD_TRAILER, true),
-        Arguments.of(STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD, false),
-        Arguments.of(STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD_TRAILER, false),
-        Arguments.of(STREAMING_UNSIGNED_PAYLOAD_TRAILER, false));
+        Arguments.of(STREAMING_AWS4_HMAC_SHA256_PAYLOAD, 
SignedChunksInputStream.class, false, true),
+        Arguments.of(STREAMING_AWS4_HMAC_SHA256_PAYLOAD_TRAILER, 
SignedChunksInputStream.class, false, true),
+        Arguments.of(STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD, 
SignedChunksInputStream.class, false, false),
+        Arguments.of(STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD_TRAILER, 
SignedChunksInputStream.class, false, false),
+        Arguments.of(STREAMING_UNSIGNED_PAYLOAD_TRAILER, 
UnsignedChunksInputStream.class, false, false),
+        Arguments.of("STREAMING-FUTURE-PAYLOAD", 
SignedChunksInputStream.class, false, false),
+        Arguments.of("STREAMING", SignedChunksInputStream.class, false, false),
+        Arguments.of("STREAMING-AWS4-HMAC-SHA256-PAYLOAD ", 
SignedChunksInputStream.class, false, false),
+        Arguments.of(UNSIGNED_PAYLOAD, ByteArrayInputStream.class, false, 
false),
+        
Arguments.of("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
+            ByteArrayInputStream.class, true, false),
+        Arguments.of("", ByteArrayInputStream.class, true, false),
+        Arguments.of("not-a-hash", ByteArrayInputStream.class, true, false),
+        Arguments.of("streaming-AWS4-HMAC-SHA256-PAYLOAD", 
ByteArrayInputStream.class, true, false),
+        Arguments.of(" STREAMING-AWS4-HMAC-SHA256-PAYLOAD", 
ByteArrayInputStream.class, true, false));
   }
 
   @ParameterizedTest
-  @MethodSource("chunkSignatureVerificationCases")
-  void testChunkSignatureVerificationSelection(String algorithm, boolean 
expected) throws Exception {
-    when(headers.getHeaderString(X_AMZ_CONTENT_SHA256)).thenReturn(algorithm);
-    if (STREAMING_AWS4_HMAC_SHA256_PAYLOAD_TRAILER.equals(algorithm)) {
+  @MethodSource("payloadClassificationCases")
+  void testPayloadClassification(String header, Class<? extends InputStream> 
streamType, boolean sha256,
+      boolean hmacSignedStreaming) throws Exception {
+    when(headers.getHeaderString(X_AMZ_CONTENT_SHA256)).thenReturn(header);
+    // ECDSA and unsigned trailer modes keep their existing behavior without a 
trailer declaration.
+    if (STREAMING_AWS4_HMAC_SHA256_PAYLOAD_TRAILER.equals(header)) {
       
when(headers.getHeaderString(X_AMZ_TRAILER)).thenReturn("x-amz-checksum-crc32c");
     }
+    for (boolean headerAuth : new boolean[]{true, false}) {
+      objectEndpoint.setSignatureInfo(signatureInfo(headerAuth));
+      try (ByteArrayInputStream body = new ByteArrayInputStream(new byte[0])) {
+        EndpointBase.S3ChunkInputStreamInfo info = 
objectEndpoint.getS3ChunkInputStreamInfo(body, 99, "37", KEY_NAME);
+        try (MultiDigestInputStream stream = info.getMultiDigestInputStream()) 
{
+          assertThat(stream.getWrappedStream()).isInstanceOf(streamType);
+          if (streamType == ByteArrayInputStream.class) {
+            assertThat(stream.getWrappedStream()).isSameAs(body);
+            assertThat(info.getEffectiveLength()).isEqualTo(99);
+          } else {
+            assertThat(info.getEffectiveLength()).isEqualTo(37);
+          }
+          
assertThat(stream.getMessageDigest(OzoneConsts.MD5_HASH)).isNotNull();
+          assertThat(stream.getMessageDigest(OzoneConsts.FILE_HASH) != 
null).isEqualTo(sha256);
+          
assertThat(info.isChunkSignatureVerificationRequired()).isEqualTo(headerAuth && 
hmacSignedStreaming);
+        }
+      }
+    }
+  }
 
-    EndpointBase.S3ChunkInputStreamInfo info = 
objectEndpoint.getS3ChunkInputStreamInfo(
-        new ByteArrayInputStream(new byte[0]), 0, "0", KEY_NAME);
+  @ParameterizedTest
+  @ValueSource(booleans = {true, false})
+  void testHmacTrailerValidationOrder(boolean headerAuth) {
+    objectEndpoint.setSignatureInfo(signatureInfo(headerAuth));
+    
when(headers.getHeaderString(X_AMZ_CONTENT_SHA256)).thenReturn(STREAMING_AWS4_HMAC_SHA256_PAYLOAD_TRAILER);
+    ByteArrayInputStream body = new ByteArrayInputStream(new byte[0]);
 
-    
assertThat(info.isChunkSignatureVerificationRequired()).isEqualTo(expected);
-  }
+    OS3Exception missingLength = assertErrorResponse(INVALID_ARGUMENT,
+        () -> objectEndpoint.getS3ChunkInputStreamInfo(body, 0, null, 
KEY_NAME));
+    
assertThat(missingLength.getErrorMessage()).contains(DECODED_CONTENT_LENGTH_HEADER);
 
-  @Test
-  void testPresignedRequestDoesNotVerifyChunkSignatures() throws Exception {
-    // A presigned (query auth) request does not sign the payload hash, so its 
seed signature cannot
-    // start the chunk signature chain and the chunks must not be verified 
against it.
-    
when(headers.getHeaderString(X_AMZ_CONTENT_SHA256)).thenReturn(STREAMING_AWS4_HMAC_SHA256_PAYLOAD);
-    ObjectEndpoint presigned = EndpointBuilder.newObjectEndpointBuilder()
-        .setHeaders(headers)
-        .setSignatureInfo(signatureInfo(false))
-        .build();
+    OS3Exception missingTrailer = assertErrorResponse(INVALID_ARGUMENT,
+        () -> objectEndpoint.getS3ChunkInputStreamInfo(body, 0, "0", 
KEY_NAME));
+    assertThat(missingTrailer.getErrorMessage()).contains(X_AMZ_TRAILER);
 
-    EndpointBase.S3ChunkInputStreamInfo info = 
presigned.getS3ChunkInputStreamInfo(
-        new ByteArrayInputStream(new byte[0]), 0, "0", KEY_NAME);
+    
when(headers.getHeaderString(X_AMZ_TRAILER)).thenReturn("unsupported-checksum");
+    assertErrorResponse(INVALID_REQUEST, () -> 
objectEndpoint.getS3ChunkInputStreamInfo(body, 0, "0", KEY_NAME));
+  }
 
-    assertThat(info.isChunkSignatureVerificationRequired()).isFalse();
+  @Test
+  void testMissingPayloadHeader() throws Exception {
+    when(headers.getHeaderString(X_AMZ_CONTENT_SHA256)).thenReturn(null);
+    try (ByteArrayInputStream body = new ByteArrayInputStream(new byte[0])) {
+      OS3Exception missingHeader = assertErrorResponse(INVALID_ARGUMENT,
+          () -> objectEndpoint.getS3ChunkInputStreamInfo(body, 99, null, 
KEY_NAME));
+      
assertThat(missingHeader.getErrorMessage()).contains(X_AMZ_CONTENT_SHA256);
+
+      objectEndpoint.setSignatureInfo(signatureInfo(false));
+      EndpointBase.S3ChunkInputStreamInfo info = 
objectEndpoint.getS3ChunkInputStreamInfo(body, 99, null, KEY_NAME);
+      try (MultiDigestInputStream stream = info.getMultiDigestInputStream()) {
+        assertThat(stream.getWrappedStream()).isSameAs(body);
+        assertThat(info.getEffectiveLength()).isEqualTo(99);
+        
assertThat(stream.getAllDigests()).containsOnlyKeys(OzoneConsts.MD5_HASH);
+        assertThat(info.isChunkSignatureVerificationRequired()).isFalse();
+      }
+    }
   }
 
   @Test
diff --git 
a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/util/TestS3ContentSha256Type.java
 
b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/util/TestS3ContentSha256Type.java
new file mode 100644
index 00000000000..1dac679a3c8
--- /dev/null
+++ 
b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/util/TestS3ContentSha256Type.java
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.ozone.s3.util;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.stream.Stream;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.MethodSource;
+
+/** Tests payload classification, including the existing fallback for unknown 
streaming algorithms. */
+class TestS3ContentSha256Type {
+
+  @ParameterizedTest
+  @CsvSource({
+      "UNSIGNED-PAYLOAD, UNSIGNED_PAYLOAD",
+      "STREAMING-UNSIGNED-PAYLOAD-TRAILER, STREAMING_UNSIGNED_PAYLOAD_TRAILER",
+      "STREAMING-AWS4-HMAC-SHA256-PAYLOAD, STREAMING_AWS4_HMAC_SHA256_PAYLOAD",
+      "STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER, 
STREAMING_AWS4_HMAC_SHA256_PAYLOAD_TRAILER",
+      "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD, 
STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD",
+      "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD-TRAILER, 
STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD_TRAILER"
+  })
+  void parsesKnownHeaders(String header, S3ContentSha256Type expected) {
+    
assertThat(S3ContentSha256Type.fromHeaderValue(header)).isEqualTo(expected);
+  }
+
+  static Stream<Arguments> unrecognizedHeaders() {
+    return Stream.of(
+        
Arguments.of("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
 S3ContentSha256Type.OTHER),
+        Arguments.of("", S3ContentSha256Type.OTHER),
+        Arguments.of("not-a-hash", S3ContentSha256Type.OTHER),
+        Arguments.of("unsigned-payload", S3ContentSha256Type.OTHER),
+        Arguments.of(" UNSIGNED-PAYLOAD", S3ContentSha256Type.OTHER),
+        Arguments.of("UNSIGNED-PAYLOAD ", S3ContentSha256Type.OTHER),
+        Arguments.of("streaming-AWS4-HMAC-SHA256-PAYLOAD", 
S3ContentSha256Type.OTHER),
+        Arguments.of(" STREAMING-AWS4-HMAC-SHA256-PAYLOAD", 
S3ContentSha256Type.OTHER),
+        Arguments.of("STREAMING", S3ContentSha256Type.OTHER_STREAMING),
+        Arguments.of("STREAMING-FUTURE-PAYLOAD", 
S3ContentSha256Type.OTHER_STREAMING),
+        Arguments.of("STREAMINGanything", S3ContentSha256Type.OTHER_STREAMING),
+        Arguments.of("STREAMING-AWS4-HMAC-SHA256-PAYLOAD ", 
S3ContentSha256Type.OTHER_STREAMING));
+  }
+
+  @ParameterizedTest
+  @MethodSource("unrecognizedHeaders")
+  void preservesUnrecognizedHeaders(String header, S3ContentSha256Type 
expected) {
+    
assertThat(S3ContentSha256Type.fromHeaderValue(header)).isEqualTo(expected);
+  }
+
+  @ParameterizedTest
+  @CsvSource({
+      "UNSIGNED_PAYLOAD, true, false, false",
+      "STREAMING_UNSIGNED_PAYLOAD_TRAILER, true, true, false",
+      "STREAMING_AWS4_HMAC_SHA256_PAYLOAD, false, true, true",
+      "STREAMING_AWS4_HMAC_SHA256_PAYLOAD_TRAILER, false, true, true",
+      "STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD, false, true, false",
+      "STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD_TRAILER, false, true, false",
+      "OTHER, false, false, false",
+      "OTHER_STREAMING, false, true, false"
+  })
+  void classifiesPayload(S3ContentSha256Type type, boolean unsigned, boolean 
streaming, boolean hmacSignedStreaming) {
+    assertThat(type.isUnsigned()).isEqualTo(unsigned);
+    assertThat(type.isStreaming()).isEqualTo(streaming);
+    assertThat(type.isHmacSignedStreaming()).isEqualTo(hmacSignedStreaming);
+  }
+
+  @Test
+  void rejectsNull() {
+    assertThrows(NullPointerException.class, () -> 
S3ContentSha256Type.fromHeaderValue(null));
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to