|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.services; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.mockito.ArgumentMatchers.any; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.spy; |
| 22 | +import static org.mockito.Mockito.verify; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | + |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import org.junit.jupiter.api.BeforeEach; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider; |
| 32 | +import software.amazon.awssdk.core.SelectedAuthScheme; |
| 33 | +import software.amazon.awssdk.core.interceptor.Context; |
| 34 | +import software.amazon.awssdk.core.interceptor.ExecutionAttributes; |
| 35 | +import software.amazon.awssdk.core.interceptor.SdkExecutionAttribute; |
| 36 | +import software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute; |
| 37 | +import software.amazon.awssdk.http.auth.aws.scheme.AwsV4AuthScheme; |
| 38 | +import software.amazon.awssdk.http.auth.spi.scheme.AuthScheme; |
| 39 | +import software.amazon.awssdk.http.auth.spi.scheme.AuthSchemeOption; |
| 40 | +import software.amazon.awssdk.http.auth.spi.signer.HttpSigner; |
| 41 | +import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; |
| 42 | +import software.amazon.awssdk.identity.spi.IdentityProvider; |
| 43 | +import software.amazon.awssdk.identity.spi.IdentityProviders; |
| 44 | +import software.amazon.awssdk.services.protocolrestjson.auth.scheme.ProtocolRestJsonAuthSchemeParams; |
| 45 | +import software.amazon.awssdk.services.protocolrestjson.auth.scheme.ProtocolRestJsonAuthSchemeProvider; |
| 46 | +import software.amazon.awssdk.services.protocolrestjson.auth.scheme.internal.ProtocolRestJsonAuthSchemeInterceptor; |
| 47 | + |
| 48 | +public class AuthSchemeInterceptorTest { |
| 49 | + private static final ProtocolRestJsonAuthSchemeInterceptor INTERCEPTOR = new ProtocolRestJsonAuthSchemeInterceptor(); |
| 50 | + |
| 51 | + private Context.BeforeExecution mockContext; |
| 52 | + |
| 53 | + @BeforeEach |
| 54 | + public void setup() { |
| 55 | + mockContext = mock(Context.BeforeExecution.class); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void resolveAuthScheme_authSchemeSignerThrows_continuesToNextAuthScheme() { |
| 60 | + ProtocolRestJsonAuthSchemeProvider mockAuthSchemeProvider = mock(ProtocolRestJsonAuthSchemeProvider.class); |
| 61 | + List<AuthSchemeOption> authSchemeOptions = Arrays.asList( |
| 62 | + AuthSchemeOption.builder().schemeId(TestAuthScheme.SCHEME_ID).build(), |
| 63 | + AuthSchemeOption.builder().schemeId(AwsV4AuthScheme.SCHEME_ID).build() |
| 64 | + ); |
| 65 | + when(mockAuthSchemeProvider.resolveAuthScheme(any(ProtocolRestJsonAuthSchemeParams.class))).thenReturn(authSchemeOptions); |
| 66 | + |
| 67 | + IdentityProviders mockIdentityProviders = mock(IdentityProviders.class); |
| 68 | + when(mockIdentityProviders.identityProvider(any(Class.class))).thenReturn(AnonymousCredentialsProvider.create()); |
| 69 | + |
| 70 | + Map<String, AuthScheme<?>> authSchemes = new HashMap<>(); |
| 71 | + authSchemes.put(AwsV4AuthScheme.SCHEME_ID, AwsV4AuthScheme.create()); |
| 72 | + |
| 73 | + TestAuthScheme notProvidedAuthScheme = spy(new TestAuthScheme()); |
| 74 | + authSchemes.put(TestAuthScheme.SCHEME_ID, notProvidedAuthScheme); |
| 75 | + |
| 76 | + ExecutionAttributes attributes = new ExecutionAttributes(); |
| 77 | + attributes.putAttribute(SdkExecutionAttribute.OPERATION_NAME, "GetFoo"); |
| 78 | + attributes.putAttribute(SdkInternalExecutionAttribute.AUTH_SCHEME_RESOLVER, mockAuthSchemeProvider); |
| 79 | + attributes.putAttribute(SdkInternalExecutionAttribute.IDENTITY_PROVIDERS, mockIdentityProviders); |
| 80 | + attributes.putAttribute(SdkInternalExecutionAttribute.AUTH_SCHEMES, authSchemes); |
| 81 | + |
| 82 | + INTERCEPTOR.beforeExecution(mockContext, attributes); |
| 83 | + |
| 84 | + SelectedAuthScheme<?> selectedAuthScheme = attributes.getAttribute(SdkInternalExecutionAttribute.SELECTED_AUTH_SCHEME); |
| 85 | + |
| 86 | + verify(notProvidedAuthScheme).signer(); |
| 87 | + assertThat(selectedAuthScheme.authSchemeOption().schemeId()).isEqualTo(AwsV4AuthScheme.SCHEME_ID); |
| 88 | + } |
| 89 | + |
| 90 | + private static class TestAuthScheme implements AuthScheme<AwsCredentialsIdentity> { |
| 91 | + public static final String SCHEME_ID = "codegen-test-scheme"; |
| 92 | + |
| 93 | + @Override |
| 94 | + public String schemeId() { |
| 95 | + return SCHEME_ID; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public IdentityProvider<AwsCredentialsIdentity> identityProvider(IdentityProviders providers) { |
| 100 | + return providers.identityProvider(AwsCredentialsIdentity.class); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public HttpSigner<AwsCredentialsIdentity> signer() { |
| 105 | + throw new RuntimeException("Not on classpath"); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments