From 09a5db364bd5e7cc280e76642b3d4f51581cb173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Fern=C3=A1ndez=20Casta=C3=B1o?= Date: Mon, 2 Aug 2021 11:38:37 +0200 Subject: [PATCH] Include full key in Azure read operations exceptions (#75920) Closes #75916 --- .../azure/AzureBlobStoreRepositoryTests.java | 10 ++++++++++ .../repositories/azure/AzureBlobContainer.java | 9 +++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/plugins/repository-azure/src/internalClusterTest/java/org/elasticsearch/repositories/azure/AzureBlobStoreRepositoryTests.java b/plugins/repository-azure/src/internalClusterTest/java/org/elasticsearch/repositories/azure/AzureBlobStoreRepositoryTests.java index 4ecf0012e77ff..025459a9fa6f0 100644 --- a/plugins/repository-azure/src/internalClusterTest/java/org/elasticsearch/repositories/azure/AzureBlobStoreRepositoryTests.java +++ b/plugins/repository-azure/src/internalClusterTest/java/org/elasticsearch/repositories/azure/AzureBlobStoreRepositoryTests.java @@ -30,6 +30,7 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.nio.file.NoSuchFileException; import java.util.ArrayList; import java.util.Base64; import java.util.Collection; @@ -40,6 +41,7 @@ import java.util.regex.Pattern; import static org.hamcrest.Matchers.anEmptyMap; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; @SuppressForbidden(reason = "this test uses a HttpServer to emulate an Azure endpoint") @@ -243,4 +245,12 @@ public void testDeleteBlobsIgnoringIfNotExists() throws Exception { assertThat(container.listBlobs(), is(anEmptyMap())); } } + + public void testNotFoundErrorMessageContainsFullKey() throws Exception { + try (BlobStore store = newBlobStore()) { + BlobContainer container = store.blobContainer(BlobPath.EMPTY.add("nested").add("dir")); + NoSuchFileException exception = expectThrows(NoSuchFileException.class, () -> container.readBlob("blob")); + assertThat(exception.getMessage(), equalTo("Blob [nested/dir/blob] not found")); + } + } } diff --git a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobContainer.java b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobContainer.java index 005c1027c9310..7bd91c2daef84 100644 --- a/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobContainer.java +++ b/plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobContainer.java @@ -47,6 +47,7 @@ public boolean blobExists(String blobName) throws IOException { } private InputStream openInputStream(String blobName, long position, @Nullable Long length) throws IOException { + String blobKey = buildKey(blobName); logger.trace("readBlob({}) from position [{}] with length [{}]", blobName, position, length != null ? length : "unlimited"); if (blobStore.getLocationMode() == LocationMode.SECONDARY_ONLY && blobExists(blobName) == false) { // On Azure, if the location path is a secondary location, and the blob does not @@ -55,18 +56,18 @@ private InputStream openInputStream(String blobName, long position, @Nullable Lo // before throwing a storage exception. This can cause long delays in retrieving // snapshots, so we first check if the blob exists before trying to open an input // stream to it. - throw new NoSuchFileException("Blob [" + blobName + "] does not exist"); + throw new NoSuchFileException("Blob [" + blobKey + "] not found"); } try { - return blobStore.getInputStream(buildKey(blobName), position, length); + return blobStore.getInputStream(blobKey, position, length); } catch (Exception e) { Throwable rootCause = Throwables.getRootCause(e); if (rootCause instanceof BlobStorageException) { if (((BlobStorageException) rootCause).getStatusCode() == 404) { - throw new NoSuchFileException("Blob [" + blobName + "] not found"); + throw new NoSuchFileException("Blob [" + blobKey + "] not found"); } } - throw new IOException("Unable to get input stream for blob [" + blobName + "]", e); + throw new IOException("Unable to get input stream for blob [" + blobKey + "]", e); } }