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

Add tests to AwsExecutionContextBuilderTest to show current behavior #4519

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration;
import software.amazon.awssdk.awscore.client.config.AwsClientOption;
import software.amazon.awssdk.core.SdkRequest;
Expand Down Expand Up @@ -61,12 +62,21 @@ public class AwsExecutionContextBuilderTest {
ExecutionInterceptor interceptor;

@Mock
Signer defaultSigner, clientOverrideSigner;
AwsCredentialsProvider defaultCredentialsProvider;

@Mock
Signer defaultSigner;

@Mock
Signer clientOverrideSigner;

@Before
public void setUp() throws Exception {
when(sdkRequest.overrideConfiguration()).thenReturn(Optional.empty());
when(interceptor.modifyRequest(any(), any())).thenReturn(sdkRequest);
when(defaultCredentialsProvider.resolveCredentials()).thenAnswer(
invocationOnMock -> AwsBasicCredentials.create("ak", "sk"));

}

@Test
Expand Down Expand Up @@ -101,17 +111,30 @@ public void verifyCoreExecutionAttributesTakePrecedence() {
assertThat(executionContext.executionAttributes().getAttribute(SdkExecutionAttribute.SERVICE_NAME)).isEqualTo("DoNotOverrideService");
}

// // this is post SRA case. this is asserting that AuthorizationStrategy is not used.
// @Test
// public void verify_doesNotResolveIdentity_doesNotAssignSigner() {
// ExecutionContext executionContext =
// AwsExecutionContextBuilder.invokeInterceptorsAndCreateExecutionContext(clientExecutionParams(),
// testClientConfiguration().build());
//
// assertThat(executionContext.signer()).isNull();
// verify(defaultCredentialsProvider, times(0)).resolveCredentials();
// }

// pre SRA, AuthorizationStrategy would setup the signer and resolve identity.
@Test
public void signing_ifNoOverrides_assignDefaultSigner() {
public void preSra_signing_ifNoOverrides_assignDefaultSigner_resolveIdentity() {
ExecutionContext executionContext =
AwsExecutionContextBuilder.invokeInterceptorsAndCreateExecutionContext(clientExecutionParams(),
testClientConfiguration().build());

assertThat(executionContext.signer()).isEqualTo(defaultSigner);
verify(defaultCredentialsProvider, times(1)).resolveCredentials();
}

@Test
public void signing_ifClientOverride_assignClientOverrideSigner() {
public void preSra_signing_ifClientOverride_assignClientOverrideSigner_resolveIdentity() {
Optional overrideConfiguration = Optional.of(AwsRequestOverrideConfiguration.builder()
.signer(clientOverrideSigner)
.build());
Expand All @@ -122,6 +145,72 @@ public void signing_ifClientOverride_assignClientOverrideSigner() {
testClientConfiguration().build());

assertThat(executionContext.signer()).isEqualTo(clientOverrideSigner);
verify(defaultCredentialsProvider, times(1)).resolveCredentials();
}

@Test
public void preSra_authTypeNone_doesNotAssignSigner_doesNotResolveIdentity() {
ExecutionAttributes executionAttributes =
ExecutionAttributes.builder()
// yes, our code would put false instead of true
.put(SdkInternalExecutionAttribute.IS_NONE_AUTH_TYPE_REQUEST, false)
.build();

SdkClientConfiguration clientConfig = testClientConfiguration()
.option(SdkClientOption.EXECUTION_ATTRIBUTES, executionAttributes)
.build();

ExecutionContext executionContext =
AwsExecutionContextBuilder.invokeInterceptorsAndCreateExecutionContext(clientExecutionParams(),
clientConfig);

assertThat(executionContext.signer()).isNull();
verify(defaultCredentialsProvider, times(0)).resolveCredentials();
}

@Test
public void preSra_authTypeNone_signerClientOverride_doesNotAssignSigner_doesNotResolveIdentity() {
ExecutionAttributes executionAttributes =
ExecutionAttributes.builder()
// yes, our code would put false instead of true
.put(SdkInternalExecutionAttribute.IS_NONE_AUTH_TYPE_REQUEST, false)
.put(SdkInternalExecutionAttribute.SIGNER_OVERRIDDEN, true)
.build();

SdkClientConfiguration clientConfig = testClientConfiguration()
.option(SdkClientOption.EXECUTION_ATTRIBUTES, executionAttributes)
.option(SdkAdvancedClientOption.SIGNER, this.clientOverrideSigner)
.build();

ExecutionContext executionContext =
AwsExecutionContextBuilder.invokeInterceptorsAndCreateExecutionContext(clientExecutionParams(), clientConfig);

assertThat(executionContext.signer()).isNull();
verify(defaultCredentialsProvider, times(0)).resolveCredentials();
}

@Test
public void preSra_authTypeNone_signerRequestOverride_doesNotAssignSigner_doesNotResolveIdentity() {
ExecutionAttributes executionAttributes =
ExecutionAttributes.builder()
// yes, our code would put false instead of true
.put(SdkInternalExecutionAttribute.IS_NONE_AUTH_TYPE_REQUEST, false)
.build();

SdkClientConfiguration clientConfig = testClientConfiguration()
.option(SdkClientOption.EXECUTION_ATTRIBUTES, executionAttributes)
.build();

Optional overrideConfiguration = Optional.of(AwsRequestOverrideConfiguration.builder()
.signer(clientOverrideSigner)
.build());
when(sdkRequest.overrideConfiguration()).thenReturn(overrideConfiguration);

ExecutionContext executionContext =
AwsExecutionContextBuilder.invokeInterceptorsAndCreateExecutionContext(clientExecutionParams(), clientConfig);

assertThat(executionContext.signer()).isNull();
verify(defaultCredentialsProvider, times(0)).resolveCredentials();
}

@Test
Expand Down Expand Up @@ -209,7 +298,7 @@ private SdkClientConfiguration.Builder testClientConfiguration() {
return SdkClientConfiguration.builder()
.option(SdkClientOption.EXECUTION_INTERCEPTORS, new ArrayList<>())
.option(SdkClientOption.EXECUTION_INTERCEPTORS, interceptorList)
.option(AwsClientOption.CREDENTIALS_PROVIDER, DefaultCredentialsProvider.create())
.option(AwsClientOption.CREDENTIALS_PROVIDER, defaultCredentialsProvider)
.option(SdkAdvancedClientOption.SIGNER, this.defaultSigner);
}
}
Loading