Skip to content

Commit

Permalink
Changing ProfileCredentialProvider to throw an exception when no prof…
Browse files Browse the repository at this point in the history
…ile file can be found. Fixes #120
  • Loading branch information
spfink committed Aug 25, 2017
1 parent 01670ab commit 57c44ce
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.File;
import software.amazon.awssdk.AwsSystemSetting;
import software.amazon.awssdk.SdkClientException;
import software.amazon.awssdk.annotation.SdkTestInternalApi;
import software.amazon.awssdk.auth.profile.ProfilesConfigFile;
import software.amazon.awssdk.profile.path.AwsProfileFileLocationProvider;
Expand Down Expand Up @@ -70,7 +71,12 @@ public static Builder builder() {

@Override
protected AwsCredentials loadCredentials() {
return profilesConfigFile == null ? null : profilesConfigFile.getCredentials(profileName);
if (profilesConfigFile == null) {
throw new SdkClientException("Unable to find profile configuration file. If you are specifying a profile config "
+ "file please verify it exists.");
}

return profilesConfigFile.getCredentials(profileName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import software.amazon.awssdk.RequestConfig;
import software.amazon.awssdk.SdkResponse;
import software.amazon.awssdk.ServiceAdvancedConfiguration;
import software.amazon.awssdk.auth.AwsCredentials;
import software.amazon.awssdk.auth.AwsCredentialsProvider;
import software.amazon.awssdk.config.AdvancedClientOption;
import software.amazon.awssdk.config.ClientConfiguration;
Expand All @@ -34,6 +35,7 @@
import software.amazon.awssdk.metrics.RequestMetricCollector;
import software.amazon.awssdk.metrics.spi.AwsRequestMetrics;
import software.amazon.awssdk.util.AwsRequestMetricsFullSupport;
import software.amazon.awssdk.utils.Validate;

abstract class BaseClientHandler {
private final ClientConfiguration clientConfiguration;
Expand All @@ -54,11 +56,16 @@ ExecutionContext createExecutionContext(RequestConfig requestConfig) {
: clientConfiguration.credentialsProvider();

ClientOverrideConfiguration overrideConfiguration = clientConfiguration.overrideConfiguration();

AwsCredentials credentials = credentialsProvider.getCredentials();

Validate.validState(credentials != null, "Credential providers must never return null.");

ExecutionAttributes executionAttributes =
new ExecutionAttributes().putAttribute(AwsExecutionAttributes.SERVICE_ADVANCED_CONFIG,
serviceAdvancedConfiguration)
.putAttribute(AwsExecutionAttributes.AWS_CREDENTIALS,
credentialsProvider.getCredentials())
credentials)
.putAttribute(AwsExecutionAttributes.REQUEST_CONFIG, requestConfig);

return ExecutionContext.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public SdkHttpFullRequest execute(SdkHttpFullRequest request, RequestExecutionCo
* Sign the request if the signer if provided and credentials are present.
*/
private SdkHttpFullRequest signRequest(SdkHttpFullRequest request, RequestExecutionContext context) {
final AwsCredentials credentials = context.credentialsProvider().getCredentials();
final AwsCredentials credentials = context.executionAttributes().getAttribute(AwsExecutionAttributes.AWS_CREDENTIALS);
updateInterceptorContext(request, context.executionContext());
Signer signer = newSigner(request, context);
if (shouldSign(signer, credentials)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ public void testDisableReusingLastProvider() throws Exception {
assertEquals(2, provider2.getCredentialsCallCount);
}

@Test
public void testNullProfileFileUsesNextProvider() {
ProfileCredentialsProvider provider = ProfileCredentialsProvider.builder().defaultProfilesConfigFileLocator(() -> null).build();

MockCredentialsProvider provider2 = new MockCredentialsProvider();

AwsCredentialsProviderChain chain = AwsCredentialsProviderChain.builder().credentialsProviders(provider, provider2).build();

chain.getCredentials();
assertEquals(1, provider2.getCredentialsCallCount);
}


private static final class MockCredentialsProvider extends StaticCredentialsProvider {
public int getCredentialsCallCount = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.BeforeClass;
import org.junit.Test;
import software.amazon.awssdk.AwsSystemSetting;
import software.amazon.awssdk.SdkClientException;
import software.amazon.awssdk.auth.profile.ProfileResourceLoader;
import software.amazon.awssdk.auth.profile.ProfilesConfigFile;

Expand Down Expand Up @@ -50,11 +51,12 @@ public void testDefault() {
Assert.assertEquals("defaultAccessKey", credentials.secretAccessKey());
}

@Test
@Test(expected = SdkClientException.class)
public void testNoProfileFile() {
ProfileCredentialsProvider nullProvider =
ProfileCredentialsProvider.builder().defaultProfilesConfigFileLocator(() -> null).build();
Assert.assertNull(nullProvider.loadCredentials());

nullProvider.getCredentials();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ public void failedExecutionCallsErrorResponseHandler() throws Exception {
verifyNoMoreInteractions(responseHandler); // No response handler calls
}

@Test(expected = IllegalStateException.class)
public void clientHandlerThrowsExceptionWhenCredentialProviderReturnsNull() {
when(credentialsProvider.getCredentials()).thenReturn(null);
syncClientHandler.execute(clientExecutionParams());
}

private void expectRetrievalFromMocks() {
when(credentialsProvider.getCredentials()).thenReturn(awsCredentials);
when(requestConfig.getOriginalRequest()).thenReturn(request);
Expand Down

0 comments on commit 57c44ce

Please sign in to comment.