Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: anonymous credential provider logic #26

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions src/main/java/org/janelia/saalfeldlab/n5/s3/AmazonS3Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ private AmazonS3Utils() {
}

public static String getS3Bucket(final String uri) {

try {
return getS3Bucket(new URI(uri));
} catch (final URISyntaxException e) {
}
return null;
}

public static String getS3Bucket(final URI uri) {

try {
Expand All @@ -48,12 +50,14 @@ public static String getS3Bucket(final URI uri) {
}

public static String getS3Key(final String uri) {

try {
return getS3Key(new URI(uri));
} catch (final URISyntaxException e) {
}
return "";
}

public static String getS3Key(final URI uri) {

try {
Expand All @@ -71,7 +75,7 @@ public static boolean areAnonymous(final AWSCredentialsProvider credsProvider) {

final AWSCredentials creds = credsProvider.getCredentials();
// AnonymousAWSCredentials do not have an equals method
if (creds.getClass().equals(AnonymousAWSCredentials.class))
if (creds instanceof AnonymousAWSCredentials)
return true;

return creds.getAWSAccessKeyId() == null && creds.getAWSSecretKey() == null;
Expand All @@ -80,36 +84,25 @@ public static boolean areAnonymous(final AWSCredentialsProvider credsProvider) {
public static Regions getS3Region(final AmazonS3URI uri, @Nullable final String region) {

final Regions regionFromUri = parseRegion(uri.getRegion());
return regionFromUri != null ? regionFromUri : parseRegion(region);
return regionFromUri != null ? regionFromUri : parseRegion(region);
}

private static Regions parseRegion(String stringRegionFromUri) {

return stringRegionFromUri != null ? Regions.fromName(stringRegionFromUri) : null;
}

public static AWSStaticCredentialsProvider getS3Credentials(final AWSCredentials s3Credentials, final boolean s3Anonymous) {
public static AWSCredentialsProvider getS3Credentials(final AWSCredentials s3Credentials, final boolean s3Anonymous) {

AWSCredentials credentials = null;
final AWSStaticCredentialsProvider credentialsProvider;
if (s3Credentials != null) {
credentials = s3Credentials;
credentialsProvider = new AWSStaticCredentialsProvider(credentials);
return new AWSStaticCredentialsProvider(s3Credentials);
} else {
// if not anonymous, try finding credentials
if (!s3Anonymous) {
try {
credentials = new DefaultAWSCredentialsProviderChain().getCredentials();
} catch (final Exception e) {
System.out.println("Could not load AWS credentials, falling back to anonymous.");
}
credentialsProvider = new AWSStaticCredentialsProvider(
credentials == null ? new AnonymousAWSCredentials() : credentials);
} else
credentialsProvider = new AWSStaticCredentialsProvider(new AnonymousAWSCredentials());
if (!s3Anonymous)
return new DefaultAWSCredentialsProviderChain();
else
return new AWSStaticCredentialsProvider(new AnonymousAWSCredentials());
}

return credentialsProvider;
}

public static AmazonS3 createS3(final String uri) {
Expand Down Expand Up @@ -186,10 +179,11 @@ else if (region != null)

// I initially tried checking whether the bucket exists, but
// that, apparently, returns even when the client does not have access
if (!canListBucket(s3, bucketName)) {
if (!s3.doesBucketExistV2(bucketName) || !canListBucket(s3, bucketName)) {
// bucket not detected with anonymous credentials, try detecting credentials
// and return it even if it can't detect the bucket, since there's nothing else to do
s3 = createS3(null, new DefaultAWSCredentialsProviderChain(), endpointConfiguration, region);
builder.withCredentials(new DefaultAWSCredentialsProviderChain());
return builder.build();
}
}
return s3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public static class SkipErroneousNoSuchBucketFailure extends TestWatcher {

private void assumeFailIfNoSuchBucket(Throwable exception) {

if (exception.getCause() instanceof AmazonServiceException)
if (exception instanceof AmazonServiceException)
assumeFailIfNoSuchBucket(((AmazonServiceException)exception));
else if (exception.getCause() instanceof AmazonServiceException)
assumeFailIfNoSuchBucket(((AmazonServiceException)exception.getCause()));
}

Expand Down
Loading