Skip to content

Commit

Permalink
Include the root causes in the exception message of getCredentials to…
Browse files Browse the repository at this point in the history
… ease troubleshooting.
  • Loading branch information
skwslide committed Mar 13, 2018
1 parent 2d1d720 commit 05d9bcf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ public void setReuseLastProvider(boolean b) {
this.reuseLastProvider = b;
}

@Override
public AWSCredentials getCredentials() {
if (reuseLastProvider && lastUsedProvider != null) {
return lastUsedProvider.getCredentials();
}

List<String> exceptionMessages = new LinkedList<String>();
for (AWSCredentialsProvider provider : credentialsProviders) {
try {
AWSCredentials credentials = provider.getCredentials();
Expand All @@ -123,14 +125,17 @@ public AWSCredentials getCredentials() {
}
} catch (Exception e) {
// Ignore any exceptions and move onto the next provider
log.debug("Unable to load credentials from " + provider.toString() +
": " + e.getMessage());
String message = provider + ": " + e.getMessage();
log.debug("Unable to load credentials from " + message);
exceptionMessages.add(message);
}
}

throw new SdkClientException("Unable to load AWS credentials from any provider in the chain");
throw new SdkClientException("Unable to load AWS credentials from any provider in the chain: "
+ exceptionMessages);
}

@Override
public void refresh() {
for (AWSCredentialsProvider provider : credentialsProviders) {
provider.refresh();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@

import java.util.Arrays;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import com.amazonaws.SdkClientException;
import com.amazonaws.internal.StaticCredentialsProvider;

public class AWSCredentialsProviderChainTest {

@Rule
public ExpectedException thrown = ExpectedException.none();

/**
* Tests that, by default, the chain remembers which provider was able to
* provide credentials, and only calls that provider for any additional
Expand Down Expand Up @@ -81,20 +87,43 @@ public void testDisableReusingLastProvider() throws Exception {
assertEquals(2, provider2.getCredentialsCallCount);
}

/**
* Tests that getCredentials throws an thrown if all providers in the
* chain fail to provide credentials.
*/
@Test
public void testGetCredentialsException() {
MockCredentialsProvider provider1 = new MockCredentialsProvider("Failed!");
MockCredentialsProvider provider2 = new MockCredentialsProvider("Bad!");
AWSCredentialsProviderChain chain = new AWSCredentialsProviderChain(provider1, provider2);

thrown.expect(SdkClientException.class);
thrown.expectMessage(provider1.exceptionMessage);
thrown.expectMessage(provider2.exceptionMessage);

chain.getCredentials();
}

private static final class MockCredentialsProvider extends StaticCredentialsProvider {
public int getCredentialsCallCount = 0;
public boolean throwException = false;
public String exceptionMessage = "No credentials";

public MockCredentialsProvider() {
super(new BasicAWSCredentials("accessKey", "secretKey"));
}

public MockCredentialsProvider(String exceptionMessage) {
this();
this.exceptionMessage = exceptionMessage;
this.throwException = true;
}

@Override
public AWSCredentials getCredentials() {
getCredentialsCallCount++;

if (throwException) throw new RuntimeException("No credentials");
if (throwException) throw new RuntimeException(exceptionMessage);
else return super.getCredentials();
}
}
Expand Down

0 comments on commit 05d9bcf

Please sign in to comment.