Skip to content

Commit

Permalink
Restrict use of AmazonS3.listBuckets() #101
Browse files Browse the repository at this point in the history
  • Loading branch information
sbeimin committed Feb 26, 2018
1 parent a024ac7 commit d8fcad8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/upplication/s3fs/S3FileChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ else if (!exists && !this.options.contains(StandardOpenOption.CREATE_NEW) &&
if (exists) {
try (S3Object object = path.getFileSystem()
.getClient()
.getObject(path.getFileStore().getBucket().getName(), key)) {
.getObject(path.getFileStore().name(), key)) {
Files.copy(object.getObjectContent(), tempFile, StandardCopyOption.REPLACE_EXISTING);
}
}
Expand Down
35 changes: 27 additions & 8 deletions src/main/java/com/upplication/s3fs/S3FileStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import java.nio.file.FileStore;
import java.nio.file.attribute.FileAttributeView;
import java.nio.file.attribute.FileStoreAttributeView;
import java.util.Date;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.AccessControlList;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.Owner;
import com.google.common.collect.ImmutableList;

public class S3FileStore extends FileStore implements Comparable<S3FileStore> {

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

@Override
Expand All @@ -83,6 +94,10 @@ public Bucket getBucket() {
return getBucket(name);
}

public boolean doesBucketExist(String bucketName) {
return getClient().doesBucketExistV2(bucketName);
}

private Bucket getBucket(String bucketName) {
for (Bucket buck : getClient().listBuckets())
if (buck.getName().equals(bucketName))
Expand All @@ -99,10 +114,14 @@ private AmazonS3 getClient() {
}

public Owner getOwner() {
Bucket buck = getBucket();
if (buck != null)
return buck.getOwner();
return fileSystem.getClient().getS3AccountOwner();
try {
AccessControlList acl = getClient().getBucketAcl(name);
return acl.getOwner();
} catch (@SuppressWarnings("unused") AmazonS3Exception e) {
// Client might not be authorized to request this info?
// User S3AccountOwner as fallback.
}
return fileSystem.getClient().getS3AccountOwner();
}

@Override
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/upplication/s3fs/S3FileSystemProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.internal.Constants;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3Object;
import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -364,9 +363,9 @@ public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOExcept
if (exists(s3Path))
throw new FileAlreadyExistsException(format("target already exists: %s", s3Path));
// create bucket if necesary
Bucket bucket = s3Path.getFileStore().getBucket();
String bucketName = s3Path.getFileStore().name();
if (bucket == null) {
S3FileStore fileStore = s3Path.getFileStore();
String bucketName = fileStore.name();
if (!fileStore.doesBucketExist(bucketName)) {
s3Path.getFileSystem().getClient().createBucket(bucketName);
}
// create the object as directory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ else if (!exists && !this.options.contains(StandardOpenOption.CREATE_NEW) &&
if (exists) {
try (S3Object object = path.getFileSystem()
.getClient()
.getObject(path.getFileStore().getBucket().getName(), key)) {
.getObject(path.getFileStore().name(), key)) {
Files.copy(object.getObjectContent(), tempFile, StandardCopyOption.REPLACE_EXISTING);
}
}
Expand Down

0 comments on commit d8fcad8

Please sign in to comment.