rich7420 commented on code in PR #11061:
URL: https://github.com/apache/ozone/pull/11061#discussion_r3871419641
##########
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/EndpointBase.java:
##########
@@ -779,6 +782,46 @@ protected S3ChunkInputStreamInfo getS3ChunkInputStreamInfo(
return new S3ChunkInputStreamInfo(multiDigestInputStream, effectiveLength);
}
+ /**
+ * Whether a signed multi-chunk upload should ask OM to piggyback the derived
+ * signing key so the chunk signatures can be verified (HDDS-15140/15141).
+ */
+ protected boolean wantsChunkSignatureVerification(S3ChunkInputStreamInfo
info) {
+ return info.getMultiDigestInputStream().getWrappedStream() instanceof
SignedChunksInputStream;
+ }
Review Comment:
Good catch. Verification now opts in only for exact
`STREAMING_AWS4_HMAC_SHA256_PAYLOAD`; the other variants are covered as
non-opt-in cases.
##########
hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/signature/TestChunksValidator.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * 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.signature;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.Arrays;
+import org.apache.hadoop.ozone.s3.exception.OS3Exception;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Verifies {@link ChunksValidator} against the canonical AWS SigV4 streaming
+ * example (secret {@code wJalr...}, region us-east-1, service s3, date
+ * 20130524, a 66560-byte payload of 'a' in chunks of 65536 + 1024 + 0).
+ *
+ * @see <a
href="https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-streaming.html">
+ * Signature Calculation: Transfer Payload in Multiple Chunks</a>
+ */
+class TestChunksValidator {
Review Comment:
Added endpoint tests with a real derived key and valid/tampered signed
bodies, covering regular PUT, datastream, and MPU.
##########
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/signature/ChunksValidator.java:
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.signature;
+
+import static
org.apache.hadoop.ozone.s3.exception.S3ErrorTable.SIGNATURE_DOES_NOT_MATCH;
+import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.newError;
+
+import java.nio.charset.StandardCharsets;
+import java.security.InvalidKeyException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+import org.apache.hadoop.ozone.s3.exception.OS3Exception;
+import org.apache.kerby.util.Hex;
+
+/**
+ * Verifies the per-chunk signatures of a SigV4 chunked upload
+ * ({@code STREAMING-AWS4-HMAC-SHA256-PAYLOAD}).
+ * <p>
+ * Each chunk signature is {@code hex(HMAC-SHA256(signingKey, stringToSign))},
+ * where the string-to-sign is:
+ * <pre>
+ * AWS4-HMAC-SHA256-PAYLOAD\n
+ * <date-time>\n
+ * <credential-scope>\n
+ * <previous-signature>\n
+ * <SHA-256("")>\n
+ * <SHA-256(chunk-payload)>
+ * </pre>
+ * The signatures are chained: the first chunk uses the request (seed)
signature
+ * as the previous signature, and each subsequent chunk uses the previous
+ * chunk's computed signature. The signing key is the SigV4 signing key derived
+ * from the caller's secret; it is provided by the caller so that the S3
Gateway
+ * does not have to handle the secret directly (see HDDS-15140).
+ *
+ * @see <a
href="https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-streaming.html">
+ * Signature Calculation: Transfer Payload in Multiple Chunks</a>
+ */
+public class ChunksValidator {
+
+ private static final String CHUNK_STRING_TO_SIGN_ALGORITHM =
+ "AWS4-HMAC-SHA256-PAYLOAD";
+ private static final String HMAC_SHA256 = "HmacSHA256";
+ private static final String SHA_256 = "SHA-256";
+ private static final String NEWLINE = "\n";
+
+ /** SHA-256 hex of the empty string (the hashed empty headers slot). */
+ private static final String EMPTY_STRING_SHA256 =
+ "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
+
+ private static final ThreadLocal<Mac> HMAC = ThreadLocal.withInitial(() -> {
+ try {
+ return Mac.getInstance(HMAC_SHA256);
+ } catch (NoSuchAlgorithmException e) {
+ throw new IllegalStateException(HMAC_SHA256 + " not available", e);
+ }
+ });
+
+ private static final ThreadLocal<MessageDigest> SHA256 =
ThreadLocal.withInitial(() -> {
+ try {
+ return MessageDigest.getInstance(SHA_256);
+ } catch (NoSuchAlgorithmException e) {
+ throw new IllegalStateException(SHA_256 + " not available", e);
+ }
+ });
+
+ private final byte[] signingKey;
+ private final String dateTime;
+ private final String credentialScope;
+ private String previousSignature;
+
+ public ChunksValidator(byte[] signingKey, String dateTime,
+ String credentialScope, String seedSignature) {
+ this.signingKey = signingKey.clone();
+ this.dateTime = dateTime;
+ this.credentialScope = credentialScope;
+ this.previousSignature = seedSignature;
+ }
+
+ /**
+ * Verify one chunk and advance the signature chain.
+ *
+ * @param chunkSignature the signature parsed from the chunk header line
+ * @param payloadSha256Hex hex SHA-256 of the chunk payload
+ * @throws OS3Exception if the computed signature does not match
+ */
+ public void validateChunk(String chunkSignature, String payloadSha256Hex)
Review Comment:
Good point. The signature is now normalized with `Locale.ROOT`, with
uppercase coverage.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]