Skip to content

Fix UploadPartCopy source handling #127442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions muted-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,6 @@ tests:
- class: org.elasticsearch.xpack.esql.qa.single_node.PushQueriesIT
method: testPushCaseInsensitiveEqualityOnDefaults
issue: https://github.com/elastic/elasticsearch/issues/127431
- class: org.elasticsearch.repositories.blobstore.testkit.analyze.S3RepositoryAnalysisRestIT
method: testRepositoryAnalysis
issue: https://github.com/elastic/elasticsearch/issues/127436

# Examples:
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ public void handle(final HttpExchange exchange) throws IOException {
exchange.sendResponseHeaders(RestStatus.NOT_FOUND.getStatus(), -1);
} else {
// CopyPart is UploadPart with an x-amz-copy-source header
final var sourceBlobName = exchange.getRequestHeaders().get("X-amz-copy-source");
if (sourceBlobName != null) {
var sourceBlob = blobs.get(sourceBlobName.getFirst());
final var copySource = copySourceName(exchange);
if (copySource != null) {
var sourceBlob = blobs.get(copySource);
if (sourceBlob == null) {
exchange.sendResponseHeaders(RestStatus.NOT_FOUND.getStatus(), -1);
} else {
Expand Down Expand Up @@ -230,12 +230,10 @@ public void handle(final HttpExchange exchange) throws IOException {
exchange.sendResponseHeaders((upload == null ? RestStatus.NOT_FOUND : RestStatus.NO_CONTENT).getStatus(), -1);

} else if (request.isPutObjectRequest()) {
// a copy request is a put request with a copy source header
final var copySources = exchange.getRequestHeaders().get("X-amz-copy-source");
if (copySources != null) {
final var copySource = copySources.getFirst();
final var prefix = copySource.length() > 0 && copySource.charAt(0) == '/' ? "" : "/";
var sourceBlob = blobs.get(prefix + copySource);
// a copy request is a put request with an X-amz-copy-source header
final var copySource = copySourceName(exchange);
if (copySource != null) {
var sourceBlob = blobs.get(copySource);
if (sourceBlob == null) {
exchange.sendResponseHeaders(RestStatus.NOT_FOUND.getStatus(), -1);
} else {
Expand Down Expand Up @@ -516,6 +514,21 @@ static List<String> extractPartEtags(BytesReference completeMultipartUploadBody)
}
}

@Nullable // if no X-amz-copy-source header present
private static String copySourceName(final HttpExchange exchange) {
final var copySources = exchange.getRequestHeaders().get("X-amz-copy-source");
if (copySources != null) {
if (copySources.size() != 1) {
throw new AssertionError("multiple X-amz-copy-source headers found: " + copySources);
}
final var copySource = copySources.get(0);
// SDKv1 uses format /bucket/path/blob whereas SDKv2 omits the leading / so we must add it back in
return copySource.length() > 0 && copySource.charAt(0) == '/' ? copySource : ("/" + copySource);
} else {
return null;
}
}

private static HttpHeaderParser.Range parsePartRange(final HttpExchange exchange) {
final var sourceRangeHeaders = exchange.getRequestHeaders().get("X-amz-copy-source-range");
if (sourceRangeHeaders == null) {
Expand Down