Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -66,12 +66,14 @@ public static SdkRequest overrideSignerIfNotOverridden(SdkRequest request,
* @deprecated No longer used by modern clients after migration to reference architecture
*/
@Deprecated
// TODO(sra-identity-and-auth): These used to be used by EndpointsAuthSchemeInterceptor, which has now been removed, but
// this method is still used from AwsExecutionContextBuilder. Should @Deprecated be removed?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if we're still using it, let's not deprecate it.

public static boolean isSignerOverridden(SdkRequest request, ExecutionAttributes executionAttributes) {
Optional<Boolean> isClientSignerOverridden = Optional.ofNullable(
executionAttributes.getAttribute(SdkExecutionAttribute.SIGNER_OVERRIDDEN));
boolean isClientSignerOverridden =
Boolean.TRUE.equals(executionAttributes.getAttribute(SdkExecutionAttribute.SIGNER_OVERRIDDEN));
Optional<Signer> requestSigner = request.overrideConfiguration()
.flatMap(RequestOverrideConfiguration::signer);
return isClientSignerOverridden.isPresent() || requestSigner.isPresent();
return isClientSignerOverridden || requestSigner.isPresent();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.services;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import java.util.concurrent.CompletableFuture;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity;
import software.amazon.awssdk.identity.spi.ResolveIdentityRequest;
import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient;

@RunWith(MockitoJUnitRunner.class)
public class SraIdentityResolutionTest {

@Mock
private AwsCredentialsProvider credsProvider;

@Test
public void testIdentityPropertyBasedResolutionIsUsedAndNotAnotherIdentityResolution() {
when(credsProvider.identityType()).thenReturn(AwsCredentialsIdentity.class);
when(credsProvider.resolveIdentity(any(ResolveIdentityRequest.class)))
.thenReturn(CompletableFuture.completedFuture(AwsBasicCredentials.create("akid1", "skid2")));
ProtocolRestJsonClient syncClient = ProtocolRestJsonClient
.builder()
.credentialsProvider(credsProvider)
// Below is necessary to create the test case where, addCredentialsToExecutionAttributes was getting called before
.overrideConfiguration(ClientOverrideConfiguration.builder().build())
.build();

try {
syncClient.allTypes();
} catch (Exception expected) {
}

verify(credsProvider, times(2)).identityType();

// This asserts that the identity used is the one from resolveIdentity() called by SRA AuthSchemeInterceptor and not from
// from another call like from AwsCredentialsAuthorizationStrategy.addCredentialsToExecutionAttributes, asserted by
// combination of times(1) and verifyNoMoreInteractions.
verify(credsProvider, times(1)).resolveIdentity(any(ResolveIdentityRequest.class));
verifyNoMoreInteractions(credsProvider);
}
}