Skip to content
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

Adjust encoding of Azure block IDs #68957

Merged
merged 5 commits into from
Feb 15, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import java.nio.file.FileAlreadyExistsException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -447,11 +448,13 @@ private void executeMultipartUpload(String blobName, InputStream inputStream, lo
assert blobSize == (((nbParts - 1) * partSize) + lastPartSize) : "blobSize does not match multipart sizes";

final List<String> blockIds = new ArrayList<>(nbParts);
final Base64.Encoder base64Encoder = Base64.getEncoder().withoutPadding();
final Base64.Decoder base64UrlDecoder = Base64.getUrlDecoder();
for (int i = 0; i < nbParts; i++) {
final long length = i < nbParts - 1 ? partSize : lastPartSize;
Flux<ByteBuffer> byteBufferFlux = convertStreamToByteBuffer(inputStream, length, DEFAULT_UPLOAD_BUFFERS_SIZE);

final String blockId = UUIDs.base64UUID();
final String blockId = base64Encoder.encodeToString(base64UrlDecoder.decode(UUIDs.base64UUID()));
blockBlobAsyncClient.stageBlock(blockId, byteBufferFlux, length).block();
blockIds.add(blockId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,11 @@ public void testWriteLargeBlob() throws Exception {

if ("PUT".equals(exchange.getRequestMethod())) {
final Map<String, String> params = new HashMap<>();
RestUtils.decodeQueryString(exchange.getRequestURI().getQuery(), 0, params);
RestUtils.decodeQueryString(exchange.getRequestURI().getRawQuery(), 0, params);

final String blockId = params.get("blockid");
assert Strings.hasText(blockId) == false || AzureFixtureHelper.assertValidBlockId(blockId);

if (Strings.hasText(blockId) && (countDownUploads.decrementAndGet() % 2 == 0)) {
blocks.put(blockId, Streams.readFully(exchange.getRequestBody()));
exchange.sendResponseHeaders(RestStatus.CREATED.getStatus(), -1);
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/azure-fixture/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ tasks.named("test").configure { enabled = false }

dependencies {
api project(':server')
api project(':test:framework')
}

tasks.named("preProcessFixture").configure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,20 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static org.elasticsearch.repositories.azure.AzureFixtureHelper.assertValidBlockId;

/**
* Minimal HTTP handler that acts as an Azure compliant server
*/
Expand All @@ -64,9 +62,10 @@ public void handle(final HttpExchange exchange) throws IOException {
if (Regex.simpleMatch("PUT /" + account + "/" + container + "/*blockid=*", request)) {
// Put Block (https://docs.microsoft.com/en-us/rest/api/storageservices/put-block)
final Map<String, String> params = new HashMap<>();
RestUtils.decodeQueryString(exchange.getRequestURI().getQuery(), 0, params);
RestUtils.decodeQueryString(exchange.getRequestURI().getRawQuery(), 0, params);

final String blockId = params.get("blockid");
assert assertValidBlockId(blockId);
blobs.put(blockId, Streams.readFully(exchange.getRequestBody()));
exchange.sendResponseHeaders(RestStatus.CREATED.getStatus(), -1);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.repositories.azure;

import org.elasticsearch.common.Strings;

import java.util.Base64;

public class AzureFixtureHelper {
private AzureFixtureHelper() {
}

public static boolean assertValidBlockId(String blockId) {
assert Strings.hasText(blockId) : "blockId missing";
try {
final byte[] decode = Base64.getDecoder().decode(blockId);
// all block IDs for a blob must be the same length and <64 bytes prior to decoding.
// Elasticsearch generates them all to be 15 bytes long so we can just assert that:
assert decode.length == 15 : "blockid [" + blockId + "] decodes to [" + decode.length + "] bytes";
} catch (Exception e) {
assert false : new AssertionError("blockid [" + blockId + "] is not in base64", e);
}
return true;
}
}