This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit e514bf5d5bbf61a3b79f1de2bbf5ba72edebeb06 Author: qbialota <[email protected]> AuthorDate: Fri Nov 13 11:53:04 2026 +0100 fix(S3): fix browsing the root of a bucket and directory names * `FileService.checkAccess(…)` reported the root of a bucket as non-existent when the path was built from an URI (e.g. "s3://host:port/bucket"): the `Bucket` metadata created from the name only has no creation date. The bucket existence is now asked to the server with a `HeadBucket` request. * `PathIterator` failed with a `NullPointerException` when listing the root of a bucket, because the directory key is null in that case. * `KeyPath.toString()` on a relative directory path, for example the file name of a directory, contained the server part ("S3://accessKey@host:port/name") instead of the name only. --- .../org/apache/sis/cloud/aws/s3/FileService.java | 20 +++- .../main/org/apache/sis/cloud/aws/s3/KeyPath.java | 4 +- .../org/apache/sis/cloud/aws/s3/PathIterator.java | 3 +- .../org/apache/sis/cloud/aws/s3/KeyPathTest.java | 17 ++++ .../apache/sis/cloud/aws/s3/PathIteratorTest.java | 103 +++++++++++++++++++++ .../org/apache/sis/cloud/aws/s3/S3ClientMock.java | 100 ++++++++++++++++++++ netbeans-project/ivy.xml | 3 +- netbeans-project/nbproject/project.properties | 1 + settings.gradle.kts | 2 +- 9 files changed, 247 insertions(+), 6 deletions(-) diff --git a/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/FileService.java b/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/FileService.java index 27baabdd41..e65409d053 100644 --- a/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/FileService.java +++ b/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/FileService.java @@ -54,6 +54,7 @@ import software.amazon.awssdk.services.s3.model.S3Object; import software.amazon.awssdk.services.s3.model.GetObjectRequest; import software.amazon.awssdk.services.s3.model.GetObjectResponse; import software.amazon.awssdk.services.s3.model.NoSuchBucketException; +import software.amazon.awssdk.services.s3.model.HeadBucketRequest; import org.apache.sis.util.CharSequences; import org.apache.sis.util.ArgumentChecks; import org.apache.sis.util.resources.Errors; @@ -673,7 +674,8 @@ public class FileService extends FileSystemProvider { final boolean exists; if (kp.isDirectory) { final Bucket bucket = kp.bucket(); - exists = (bucket != null) && (bucket.creationDate() != null); + // A bucket built from its name only (e.g. from an URI) has no creation date: ask the server. + exists = (bucket != null) && (bucket.creationDate() != null || bucketExists(kp)); } else { final S3Object metadata = kp.metadata(); exists = (metadata != null) && (metadata.size() != null); @@ -694,6 +696,22 @@ public class FileService extends FileSystemProvider { } } + /** + * Returns whether the bucket of the given path exists on the server. + * + * @param kp an absolute path. + * @return {@code false} if the server reports that the bucket does not exist. + * @throws SdkException if the request failed for another reason (e.g. access denied). + */ + private static boolean bucketExists(final KeyPath kp) { + try { + kp.fs.client().headBucket(HeadBucketRequest.builder().bucket(kp.bucket).build()); + return true; + } catch (NoSuchBucketException e) { + return false; + } + } + /** * Returns a read-only or updatable view of a set of file attributes. * diff --git a/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/KeyPath.java b/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/KeyPath.java index 5ce7070213..0306932650 100644 --- a/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/KeyPath.java +++ b/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/KeyPath.java @@ -744,8 +744,8 @@ search: if (key != null) { */ @Override public String toString() { - if (bucket == null && !isDirectory) { - return key; + if (bucket == null && (key != null || !isDirectory)) { + return key; // Relative path (e.g. a file name), possibly a directory: no "server part". } final var sb = fs.server.toString(bucket); if (key != null) { diff --git a/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/PathIterator.java b/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/PathIterator.java index f1b8c15d32..6828cfdf7f 100644 --- a/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/PathIterator.java +++ b/endorsed/src/org.apache.sis.cloud.aws/main/org/apache/sis/cloud/aws/s3/PathIterator.java @@ -187,7 +187,8 @@ verify: do if (directories == null || !nextDirectory()) { int last = path.length(); do if ((last -= separatorLength) < 0) return false; while (path.startsWith(separator, last)); - if (path.regionMatches(0, directory.key, 0, last + separatorLength)) { + // `directory.key` is null when listing the root of a bucket (no object can have the name of the bucket). + if (directory.key != null && path.regionMatches(0, directory.key, 0, last + separatorLength)) { return false; } } diff --git a/endorsed/src/org.apache.sis.cloud.aws/test/org/apache/sis/cloud/aws/s3/KeyPathTest.java b/endorsed/src/org.apache.sis.cloud.aws/test/org/apache/sis/cloud/aws/s3/KeyPathTest.java index d7b719d8fa..4b5c6f7a17 100644 --- a/endorsed/src/org.apache.sis.cloud.aws/test/org/apache/sis/cloud/aws/s3/KeyPathTest.java +++ b/endorsed/src/org.apache.sis.cloud.aws/test/org/apache/sis/cloud/aws/s3/KeyPathTest.java @@ -128,6 +128,23 @@ public final class KeyPathTest extends TestCase { assertNull(p.key); } + /** + * Tests {@link KeyPath#toString()} on the name of a directory. + * It used to contain the server part, for example {@code "S3://testhost:8581/second"}. + * + * @throws URISyntaxException if an error occurred while creating the <abbr>URI</abbr> for the self-hosted S3. + */ + @Test + public void testDirectoryFileName() throws URISyntaxException { + for (final boolean selfHosted : new boolean[] {false, true}) { + createPaths(selfHosted); + assertNotNull(absolute.getRoot()); + final var directory = new KeyPath((KeyPath) absolute.getRoot(), "first/second", true); + assertEquals("second", directory.getFileName().toString()); + assertEquals("second", new KeyPath(fs, "second", true).toString()); + } + } + /** * Tests {@link KeyPath#getParent()}. * diff --git a/endorsed/src/org.apache.sis.cloud.aws/test/org/apache/sis/cloud/aws/s3/PathIteratorTest.java b/endorsed/src/org.apache.sis.cloud.aws/test/org/apache/sis/cloud/aws/s3/PathIteratorTest.java new file mode 100644 index 0000000000..dca31547f1 --- /dev/null +++ b/endorsed/src/org.apache.sis.cloud.aws/test/org/apache/sis/cloud/aws/s3/PathIteratorTest.java @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sis.cloud.aws.s3; + +import java.lang.reflect.Field; +import java.net.URISyntaxException; +import java.nio.file.AccessMode; +import java.nio.file.DirectoryStream; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; +import java.util.ArrayList; +import software.amazon.awssdk.services.s3.model.Bucket; + +// Test dependencies +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import org.apache.sis.test.TestCase; +import static org.apache.sis.test.Assertions.assertMessageContains; + + +/** + * Tests {@link PathIterator} and {@link FileService#checkAccess(Path, AccessMode...)} on a bucket root, + * using a fake S3 client. + * + * @author Quentin Bialota (Geomatys) + */ +@SuppressWarnings("exports") +public final class PathIteratorTest extends TestCase { + /** + * Creates a new test case. + */ + public PathIteratorTest() { + } + + /** + * Creates a self-hosted file system backed by a fake client. + */ + private static ClientFileSystem createFileSystem(final String... buckets) + throws URISyntaxException, ReflectiveOperationException + { + final ClientFileSystem fs = ClientFileSystemTest.create(true); + final Field client = ClientFileSystem.class.getDeclaredField("client"); + client.setAccessible(true); + client.set(fs, new S3ClientMock(buckets)); + return fs; + } + + /** + * Returns the root of the given bucket as built from an URI, i.e. without bucket metadata. + */ + static KeyPath bucketRoot(final ClientFileSystem fs, final String bucket) { + return new KeyPath(fs, bucket, new String[0], true); + } + + /** + * Lists the root of a bucket. Used to fail with a {@link NullPointerException} + * because the root has no key. Also checks that directory names are only names. + * + * @throws Exception if an error occurred while creating the file system or listing the bucket. + */ + @Test + public void testListBucketRoot() throws Exception { + final ClientFileSystem fs = createFileSystem("the-bucket"); + final var names = new ArrayList<String>(); + try (DirectoryStream<Path> stream = fs.provider().newDirectoryStream(bucketRoot(fs, "the-bucket"), (p) -> true)) { + for (final Path child : stream) { + names.add(child.getFileName().toString()); + } + } + assertArrayEquals(new String[] {"common", "data.nc"}, names.toArray()); + } + + /** + * Tests {@code checkAccess} on the root of a bucket built from an URI (no creation date). + * + * @throws Exception if an error occurred while creating the file system. + */ + @Test + public void testBucketRootExists() throws Exception { + final ClientFileSystem fs = createFileSystem("the-bucket"); + final FileService service = assertInstanceOf(FileService.class, fs.provider()); + assertDoesNotThrow(() -> service.checkAccess(bucketRoot(fs, "the-bucket"))); + assertMessageContains(assertThrows(NoSuchFileException.class, () -> service.checkAccess(bucketRoot(fs, "missing-bucket"))), "missing-bucket"); + + // A bucket listed by the server has a creation date: no request needed. + final var listed = new KeyPath(fs, Bucket.builder().name("other").creationDate(java.time.Instant.EPOCH).build()); + assertDoesNotThrow(() -> service.checkAccess(listed)); + } +} diff --git a/endorsed/src/org.apache.sis.cloud.aws/test/org/apache/sis/cloud/aws/s3/S3ClientMock.java b/endorsed/src/org.apache.sis.cloud.aws/test/org/apache/sis/cloud/aws/s3/S3ClientMock.java new file mode 100644 index 0000000000..aec226348c --- /dev/null +++ b/endorsed/src/org.apache.sis.cloud.aws/test/org/apache/sis/cloud/aws/s3/S3ClientMock.java @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sis.cloud.aws.s3; + +import java.util.Set; +import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.model.CommonPrefix; +import software.amazon.awssdk.services.s3.model.HeadBucketRequest; +import software.amazon.awssdk.services.s3.model.HeadBucketResponse; +import software.amazon.awssdk.services.s3.model.ListObjectsV2Request; +import software.amazon.awssdk.services.s3.model.ListObjectsV2Response; +import software.amazon.awssdk.services.s3.model.NoSuchBucketException; +import software.amazon.awssdk.services.s3.model.S3Object; + +// Test dependencies +import static org.junit.jupiter.api.Assertions.*; + + +/** + * A fake S3 client for querying only the root of a given set of buckets. + * Each bucket contains a {@code "common/"} pseudo-directory and a {@code "data.nc"} object at its root. + * + * @author Quentin Bialota (Geomatys) + */ +final class S3ClientMock implements S3Client { + /** + * The buckets recognized by the client. + */ + private final Set<String> buckets; + + /** + * Creates a new client mock. + * + * @param buckets the buckets recognized by the client. + */ + S3ClientMock(final String[] buckets) { + this.buckets = Set.of(buckets); + } + + /** + * Returns the name of the service, which is {@code "s3}. + */ + @Override + public String serviceName() { + return "s3"; + } + + /** + * Determines if a bucket exists and if the caller has permission to access it. + * The response contains a 200 OK code if the bucket exists and is accessible. + * + * @param request a request with the bucket to access. + * @return whether is bucket is accessible. + */ + @Override + public HeadBucketResponse headBucket(final HeadBucketRequest request) { + if (!buckets.contains(request.bucket())) { + throw NoSuchBucketException.builder().message(request.bucket()).build(); + } + return HeadBucketResponse.builder().build(); + } + + /** + * Returns the objects in a bucket with each request. + * This method assumes that only the bucket root is requested. + * + * @param request a request for the bucket root. + * @return {@code "common/data.nc"}. + */ + @Override + public ListObjectsV2Response listObjectsV2(final ListObjectsV2Request request) { + assertNull(request.prefix(), "Listing the root of a bucket should not use a prefix."); + return ListObjectsV2Response.builder() + .commonPrefixes(CommonPrefix.builder().prefix("common/").build()) + .contents(S3Object.builder().key("data.nc").size(10L).build()) + .isTruncated(false) + .build(); + } + + /** + * No-operation (nothing to release). + */ + @Override + public void close() { + } +} diff --git a/netbeans-project/ivy.xml b/netbeans-project/ivy.xml index 5637b4df4f..c284db16bd 100644 --- a/netbeans-project/ivy.xml +++ b/netbeans-project/ivy.xml @@ -24,7 +24,8 @@ <dependency org="joda-time" name="joda-time" rev="2.12.7"/> <dependency org="com.google.guava" name="guava" rev="33.7.2-jre"/> <dependency org="org.libreoffice" name="libreoffice" rev="26.2.2"/> - <dependency org="software.amazon.awssdk" name="s3" rev="2.55.9"/> + <dependency org="software.amazon.awssdk" name="aws-core" rev="2.57.13"/> + <dependency org="software.amazon.awssdk" name="s3" rev="2.57.13"/> <dependency org="org.antlr" name="antlr4-maven-plugin" rev="4.13.2"/> <dependency org="javax.servlet" name="javax.servlet-api" rev="4.0.1"/> <dependency org="jakarta.servlet" name="jakarta.servlet-api" rev="6.1.0"/> diff --git a/netbeans-project/nbproject/project.properties b/netbeans-project/nbproject/project.properties index 44da6b1d5f..f5be9354b4 100644 --- a/netbeans-project/nbproject/project.properties +++ b/netbeans-project/nbproject/project.properties @@ -113,6 +113,7 @@ test.options = --add-modules jama,GeographicLib.Java,\ com.h2database,\ org.hsqldb,\ org.apache.derby.tools,\ + software.amazon.awssdk.awscore,\ software.amazon.awssdk.utils,\ org.opengis.geoapi.conformance,\ org.junit.jupiter.api,\ diff --git a/settings.gradle.kts b/settings.gradle.kts index bdaf8ba40c..233b8b782b 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -71,7 +71,7 @@ dependencyResolutionManagement { library("ucar", "edu.ucar", "cdm-core") .version {strictly("[5.0, 6.0["); prefer("5.11.0")} library("udunits", "edu.ucar", "udunits") .version {strictly("[5.0, 6.0["); prefer("5.11.0")} library("guava", "com.google.guava", "guava") .version { prefer("33.7.2-jre")} - library("aws.s3", "software.amazon.awssdk", "s3") .version {strictly("[2.0, 3.0["); prefer("2.55.9")} + library("aws.s3", "software.amazon.awssdk", "s3") .version {strictly("[2.0, 3.0["); prefer("2.57.13")} } create("tests") { library("geoapi", "org.opengis", "geoapi-conformance") .version {strictly(geoapiVersion)}
