Skip to content

Commit

Permalink
Merge pull request #26 from saalfeldlab/fix/credentialProvider
Browse files Browse the repository at this point in the history
fix: anonymous credential provider logic
  • Loading branch information
cmhulbert authored Mar 5, 2024
2 parents 0662c67 + 3e3ebf7 commit 69e1e12
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
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

0 comments on commit 69e1e12

Please sign in to comment.