Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit 31b81b5

Browse files
author
sbeimin
committed
Restrict use of AmazonS3.listBuckets() #101
1 parent 4fe1a64 commit 31b81b5

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

src/main/java/com/upplication/s3fs/S3FileChannel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ else if (!exists && !this.options.contains(StandardOpenOption.CREATE_NEW) &&
4444
if (exists) {
4545
try (S3Object object = path.getFileSystem()
4646
.getClient()
47-
.getObject(path.getFileStore().getBucket().getName(), key)) {
47+
.getObject(path.getFileStore().name(), key)) {
4848
Files.copy(object.getObjectContent(), tempFile, StandardCopyOption.REPLACE_EXISTING);
4949
}
5050
}

src/main/java/com/upplication/s3fs/S3FileStore.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import java.nio.file.FileStore;
55
import java.nio.file.attribute.FileAttributeView;
66
import java.nio.file.attribute.FileStoreAttributeView;
7+
import java.util.Date;
78

89
import com.amazonaws.services.s3.AmazonS3;
10+
import com.amazonaws.services.s3.model.AccessControlList;
11+
import com.amazonaws.services.s3.model.AmazonS3Exception;
912
import com.amazonaws.services.s3.model.Bucket;
1013
import com.amazonaws.services.s3.model.Owner;
11-
import com.google.common.collect.ImmutableList;
1214

1315
public class S3FileStore extends FileStore implements Comparable<S3FileStore> {
1416

@@ -65,9 +67,18 @@ public boolean supportsFileAttributeView(String attributeViewName) {
6567
public <V extends FileStoreAttributeView> V getFileStoreAttributeView(Class<V> type) {
6668
if (type != S3FileStoreAttributeView.class)
6769
throw new IllegalArgumentException("FileStoreAttributeView of type '" + type.getName() + "' is not supported.");
68-
Bucket buck = getBucket();
69-
Owner owner = buck.getOwner();
70-
return (V) new S3FileStoreAttributeView(buck.getCreationDate(), buck.getName(), owner.getId(), owner.getDisplayName());
70+
Date creationDate = null;
71+
String bucketName = name;
72+
try {
73+
Bucket bucket = getBucket();
74+
creationDate = bucket.getCreationDate();
75+
bucketName = bucket.getName();
76+
} catch (@SuppressWarnings("unused") AmazonS3Exception e) {
77+
// This is probably caused by not being authorized to call ListAll Buckets.
78+
// Lets return what we can get our hands on instead of crashing at this point.
79+
}
80+
Owner owner = getOwner();
81+
return (V) new S3FileStoreAttributeView(creationDate, bucketName, owner.getId(), owner.getDisplayName());
7182
}
7283

7384
@Override
@@ -83,6 +94,10 @@ public Bucket getBucket() {
8394
return getBucket(name);
8495
}
8596

97+
public boolean doesBucketExist(String bucketName) {
98+
return getClient().doesBucketExistV2(bucketName);
99+
}
100+
86101
private Bucket getBucket(String bucketName) {
87102
for (Bucket buck : getClient().listBuckets())
88103
if (buck.getName().equals(bucketName))
@@ -99,10 +114,14 @@ private AmazonS3 getClient() {
99114
}
100115

101116
public Owner getOwner() {
102-
Bucket buck = getBucket();
103-
if (buck != null)
104-
return buck.getOwner();
105-
return fileSystem.getClient().getS3AccountOwner();
117+
try {
118+
AccessControlList acl = getClient().getBucketAcl(name);
119+
return acl.getOwner();
120+
} catch (@SuppressWarnings("unused") AmazonS3Exception e) {
121+
// Client might not be authorized to request this info?
122+
// User S3AccountOwner as fallback.
123+
}
124+
return fileSystem.getClient().getS3AccountOwner();
106125
}
107126

108127
@Override

src/main/java/com/upplication/s3fs/S3FileSystemProvider.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.amazonaws.services.s3.AmazonS3;
44
import com.amazonaws.services.s3.internal.Constants;
55
import com.amazonaws.services.s3.model.AmazonS3Exception;
6-
import com.amazonaws.services.s3.model.Bucket;
76
import com.amazonaws.services.s3.model.ObjectMetadata;
87
import com.amazonaws.services.s3.model.S3Object;
98
import com.google.common.base.Preconditions;
@@ -364,9 +363,9 @@ public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOExcept
364363
if (exists(s3Path))
365364
throw new FileAlreadyExistsException(format("target already exists: %s", s3Path));
366365
// create bucket if necesary
367-
Bucket bucket = s3Path.getFileStore().getBucket();
368-
String bucketName = s3Path.getFileStore().name();
369-
if (bucket == null) {
366+
S3FileStore fileStore = s3Path.getFileStore();
367+
String bucketName = fileStore.name();
368+
if (!fileStore.doesBucketExist(bucketName)) {
370369
s3Path.getFileSystem().getClient().createBucket(bucketName);
371370
}
372371
// create the object as directory

src/main/java/com/upplication/s3fs/S3SeekableByteChannel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ else if (!exists && !this.options.contains(StandardOpenOption.CREATE_NEW) &&
4949
if (exists) {
5050
try (S3Object object = path.getFileSystem()
5151
.getClient()
52-
.getObject(path.getFileStore().getBucket().getName(), key)) {
52+
.getObject(path.getFileStore().name(), key)) {
5353
Files.copy(object.getObjectContent(), tempFile, StandardCopyOption.REPLACE_EXISTING);
5454
}
5555
}

0 commit comments

Comments
 (0)