From 512c926b6e67cd202b23185da5074bc084d0d810 Mon Sep 17 00:00:00 2001 From: Timur Alperovich Date: Mon, 25 Oct 2021 18:27:05 -0700 Subject: [PATCH] Fix anonymous bucket listing bug If the bucket locator is used with anonymous access, listing a container results in a null-pointer exception, as there are no blobstores in the locator. In that case, we should use the globLocator structure and return the first available blobstore. --- .../gaul/s3proxy/GlobBlobStoreLocator.java | 21 ++++++++++++------ .../s3proxy/GlobBlobStoreLocatorTest.java | 22 +++++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/gaul/s3proxy/GlobBlobStoreLocator.java b/src/main/java/org/gaul/s3proxy/GlobBlobStoreLocator.java index 7c53f58..f5db1d6 100644 --- a/src/main/java/org/gaul/s3proxy/GlobBlobStoreLocator.java +++ b/src/main/java/org/gaul/s3proxy/GlobBlobStoreLocator.java @@ -41,17 +41,24 @@ public final class GlobBlobStoreLocator implements BlobStoreLocator { Map.Entry locatorEntry = locator.get(identity); Map.Entry globEntry = null; - for (Map.Entry> - entry : globLocator.entrySet()) { - if (entry.getKey().matches(FileSystems.getDefault() - .getPath(container))) { - globEntry = entry.getValue(); + if (container != null) { + for (Map.Entry> + entry : globLocator.entrySet()) { + if (entry.getKey().matches(FileSystems.getDefault() + .getPath(container))) { + globEntry = entry.getValue(); + } } } if (globEntry == null) { if (identity == null) { - return locator.entrySet().iterator().next() - .getValue(); + if (!locator.isEmpty()) { + return locator.entrySet().iterator().next() + .getValue(); + } + return Maps.immutableEntry(null, + globLocator.entrySet().iterator().next().getValue() + .getValue()); } return locatorEntry; } diff --git a/src/test/java/org/gaul/s3proxy/GlobBlobStoreLocatorTest.java b/src/test/java/org/gaul/s3proxy/GlobBlobStoreLocatorTest.java index 012bedc..3a11f28 100644 --- a/src/test/java/org/gaul/s3proxy/GlobBlobStoreLocatorTest.java +++ b/src/test/java/org/gaul/s3proxy/GlobBlobStoreLocatorTest.java @@ -128,4 +128,26 @@ public final class GlobBlobStoreLocatorTest { assertThat(locator.locateBlobStore("id2", "cont5X.extra", null) .getValue()).isSameAs(blobStoreTwo); } + + @Test + public void testGlobLocatorAnonymous() { + ImmutableMap> globMap = + ImmutableMap.of( + FileSystems.getDefault().getPathMatcher( + "glob:one"), + Maps.immutableEntry(null, blobStoreOne), + FileSystems.getDefault().getPathMatcher( + "glob:two"), + Maps.immutableEntry(null, blobStoreTwo) + ); + GlobBlobStoreLocator locator = new GlobBlobStoreLocator( + ImmutableMap.of(), globMap); + + assertThat(locator.locateBlobStore(null, null, null) + .getValue()).isSameAs(blobStoreOne); + assertThat(locator.locateBlobStore(null, "one", null) + .getValue()).isSameAs(blobStoreOne); + assertThat(locator.locateBlobStore(null, "two", null) + .getValue()).isSameAs(blobStoreTwo); + } }