adoroszlai commented on code in PR #10711:
URL: https://github.com/apache/ozone/pull/10711#discussion_r3559009757


##########
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/ObjectEndpoint.java:
##########
@@ -121,6 +123,9 @@ public class ObjectEndpoint extends ObjectOperationHandler {
   private static final String PATH = "path";
   // Default Content-Type for objects stored without one, matching S3.
   private static final String DEFAULT_CONTENT_TYPE = "binary/octet-stream";
+  // x-amz-copy-source-range must be a single numeric byte range: 
bytes=<start>-<end>
+  private static final Pattern COPY_SOURCE_RANGE_PATTERN =
+      Pattern.compile("^bytes=(\\d+)-(\\d+)$");

Review Comment:
   Duplicate?
   
   
https://github.com/apache/ozone/blob/cd8ba56782f7165fa94597e26b3e962d5060150d/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3Consts.java#L68-L69



##########
hadoop-ozone/integration-test-s3/src/test/java/org/apache/hadoop/ozone/s3/awssdk/v2/AbstractS3SDKV2Tests.java:
##########
@@ -1404,6 +1404,57 @@ public void testCopyObjectWithDestinationIfMatchFail() {
     assertEquals("PreconditionFailed", 
exception.awsErrorDetails().errorCode());
   }
 
+  @Test
+  public void testUploadPartCopyInvalidRange() {
+    final String sourceBucketName = getBucketName("source");
+    final String destBucketName = getBucketName("dest");
+    final String sourceKey = getKeyName("source");
+    final String destKey = getKeyName("dest");
+    s3Client.createBucket(b -> b.bucket(sourceBucketName));
+    s3Client.createBucket(b -> b.bucket(destBucketName));
+
+    // Source object is exactly 5 bytes.
+    s3Client.putObject(b -> b.bucket(sourceBucketName).key(sourceKey), 
RequestBody.fromString("hello"));
+
+    CreateMultipartUploadResponse createResponse = 
s3Client.createMultipartUpload(b -> b
+        .bucket(destBucketName)
+        .key(destKey));
+    String uploadId = createResponse.uploadId();
+
+    // Case 1: range beyond the source object length -> InvalidRange.
+    UploadPartCopyRequest outOfRangeRequest = UploadPartCopyRequest.builder()
+        .sourceBucket(sourceBucketName)
+        .sourceKey(sourceKey)
+        .destinationBucket(destBucketName)
+        .destinationKey(destKey)
+        .uploadId(uploadId)
+        .partNumber(1)

Review Comment:
   Can we store `Builder` instance and use it to build all requests, where only 
`copySourceRange` is different?



##########
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/ObjectEndpoint.java:
##########
@@ -891,11 +896,20 @@ private Response createMultipartKey(OzoneVolume volume, 
OzoneBucket ozoneBucket,
             getHeaders().getHeaderString(COPY_SOURCE_HEADER_RANGE);
         RangeHeader rangeHeader = null;
         if (range != null) {
-          rangeHeader = RangeHeaderParserUtil.parseRangeHeader(range, 0);
+          Matcher matcher = COPY_SOURCE_RANGE_PATTERN.matcher(range);
+          if (!matcher.matches()) {
+            throw newError(S3ErrorTable.INVALID_ARGUMENT, range);
+          }
+          long startOffset = Long.parseLong(matcher.group(1));
+          long endOffset = Long.parseLong(matcher.group(2));
+          long sourceSize = sourceKeyDetails.getDataSize();
+          if (startOffset > endOffset || endOffset >= sourceSize) {
+            throw newError(S3ErrorTable.INVALID_RANGE, range);
+          }
+          rangeHeader = new RangeHeader(startOffset, endOffset, false, false);

Review Comment:
   Can `RangeHeaderParserUtil` can be tweaked to make the range mandatory in 
this case?



##########
hadoop-ozone/integration-test-s3/src/test/java/org/apache/hadoop/ozone/s3/awssdk/v2/AbstractS3SDKV2Tests.java:
##########
@@ -1404,6 +1404,57 @@ public void testCopyObjectWithDestinationIfMatchFail() {
     assertEquals("PreconditionFailed", 
exception.awsErrorDetails().errorCode());
   }
 
+  @Test
+  public void testUploadPartCopyInvalidRange() {
+    final String sourceBucketName = getBucketName("source");
+    final String destBucketName = getBucketName("dest");
+    final String sourceKey = getKeyName("source");
+    final String destKey = getKeyName("dest");
+    s3Client.createBucket(b -> b.bucket(sourceBucketName));
+    s3Client.createBucket(b -> b.bucket(destBucketName));
+
+    // Source object is exactly 5 bytes.
+    s3Client.putObject(b -> b.bucket(sourceBucketName).key(sourceKey), 
RequestBody.fromString("hello"));
+
+    CreateMultipartUploadResponse createResponse = 
s3Client.createMultipartUpload(b -> b
+        .bucket(destBucketName)
+        .key(destKey));
+    String uploadId = createResponse.uploadId();
+
+    // Case 1: range beyond the source object length -> InvalidRange.
+    UploadPartCopyRequest outOfRangeRequest = UploadPartCopyRequest.builder()
+        .sourceBucket(sourceBucketName)
+        .sourceKey(sourceKey)
+        .destinationBucket(destBucketName)
+        .destinationKey(destKey)
+        .uploadId(uploadId)
+        .partNumber(1)
+        .copySourceRange("bytes=0-21")
+        .build();
+    S3Exception outOfRange = assertThrows(S3Exception.class, () -> 
s3Client.uploadPartCopy(outOfRangeRequest));
+    // InvalidRange maps to HTTP 416; AWS also permits 400 for this case.
+    assertTrue(outOfRange.statusCode() == 400 || outOfRange.statusCode() == 
416,

Review Comment:
   nit: please use `assertThat` instead of `assertTrue` (HDDS-9951), and use 
`HttpStatus` constants.
   
   ```java
       assertThat(outOfRange.statusCode()).isIn(SC_BAD_REQUEST, 
SC_REQUESTED_RANGE_NOT_SATISFIABLE);
   ```



-- 
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]

Reply via email to