|
| 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.auth.source; |
| 17 | + |
| 18 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.get; |
| 20 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; |
| 21 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | + |
| 24 | +import com.github.tomakehurst.wiremock.junit5.WireMockExtension; |
| 25 | +import java.time.Duration; |
| 26 | +import java.time.Instant; |
| 27 | +import java.util.List; |
| 28 | +import java.util.stream.Stream; |
| 29 | +import org.junit.jupiter.api.AfterAll; |
| 30 | +import org.junit.jupiter.api.BeforeEach; |
| 31 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 32 | +import org.junit.jupiter.params.ParameterizedTest; |
| 33 | +import org.junit.jupiter.params.provider.Arguments; |
| 34 | +import org.junit.jupiter.params.provider.MethodSource; |
| 35 | +import software.amazon.awssdk.auth.credentials.ContainerCredentialsProvider; |
| 36 | +import software.amazon.awssdk.core.SdkSystemSetting; |
| 37 | +import software.amazon.awssdk.http.AbortableInputStream; |
| 38 | +import software.amazon.awssdk.http.HttpExecuteResponse; |
| 39 | +import software.amazon.awssdk.http.SdkHttpClient; |
| 40 | +import software.amazon.awssdk.http.SdkHttpRequest; |
| 41 | +import software.amazon.awssdk.http.SdkHttpResponse; |
| 42 | +import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; |
| 43 | +import software.amazon.awssdk.identity.spi.IdentityProvider; |
| 44 | +import software.amazon.awssdk.services.sts.StsClient; |
| 45 | +import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient; |
| 46 | +import software.amazon.awssdk.utils.DateUtils; |
| 47 | +import software.amazon.awssdk.utils.StringInputStream; |
| 48 | + |
| 49 | +/** |
| 50 | + * Test class to verify that ContainerCredentialsProvider correctly includes |
| 51 | + * business metrics in the User-Agent header. This test focuses specifically on the |
| 52 | + * CREDENTIALS_HTTP ("z") business metric feature ID. |
| 53 | + */ |
| 54 | +class ContainerCredentialsProviderUserAgentTest { |
| 55 | + private static final String CONTAINER_CREDENTIALS_PATH = "/v2/credentials/test-role-arn"; |
| 56 | + private static final String CONTAINER_SERVICE_ENDPOINT = "http://localhost:"; |
| 57 | + |
| 58 | + private MockSyncHttpClient mockHttpClient; |
| 59 | + |
| 60 | + @RegisterExtension |
| 61 | + static WireMockExtension wireMockServer = WireMockExtension.newInstance() |
| 62 | + .options(wireMockConfig().dynamicPort()) |
| 63 | + .configureStaticDsl(true) |
| 64 | + .build(); |
| 65 | + |
| 66 | + @BeforeEach |
| 67 | + public void setup() { |
| 68 | + |
| 69 | + System.setProperty(SdkSystemSetting.AWS_CONTAINER_SERVICE_ENDPOINT.property(), |
| 70 | + CONTAINER_SERVICE_ENDPOINT + wireMockServer.getPort()); |
| 71 | + System.setProperty(SdkSystemSetting.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI.property(), |
| 72 | + CONTAINER_CREDENTIALS_PATH); |
| 73 | + |
| 74 | + mockHttpClient = new MockSyncHttpClient(); |
| 75 | + mockHttpClient.stubNextResponse(mockStsResponse()); |
| 76 | + |
| 77 | + stubContainerCredentialsResponses(); |
| 78 | + } |
| 79 | + |
| 80 | + @AfterAll |
| 81 | + public static void teardown() { |
| 82 | + System.clearProperty(SdkSystemSetting.AWS_CONTAINER_SERVICE_ENDPOINT.property()); |
| 83 | + System.clearProperty(SdkSystemSetting.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI.property()); |
| 84 | + System.clearProperty(SdkSystemSetting.AWS_CONTAINER_AUTHORIZATION_TOKEN.property()); |
| 85 | + } |
| 86 | + |
| 87 | + private static HttpExecuteResponse mockStsResponse() { |
| 88 | + return HttpExecuteResponse.builder() |
| 89 | + .response(SdkHttpResponse.builder().statusCode(200).build()) |
| 90 | + .responseBody(AbortableInputStream.create(new StringInputStream(""))) |
| 91 | + .build(); |
| 92 | + } |
| 93 | + |
| 94 | + private void stubContainerCredentialsResponses() { |
| 95 | + String credentialsResponse = createCredentialsResponse("ACCESS_KEY_ID", "SECRET_ACCESS_KEY", null); |
| 96 | + wireMockServer.stubFor(get(urlPathEqualTo(CONTAINER_CREDENTIALS_PATH)) |
| 97 | + .willReturn(aResponse().withBody(credentialsResponse))); |
| 98 | + } |
| 99 | + |
| 100 | + private void stubContainerCredentialsResponsesWithSessionToken() { |
| 101 | + String credentialsResponse = createCredentialsResponse("ACCESS_KEY_ID", "SECRET_ACCESS_KEY", "SESSION_TOKEN"); |
| 102 | + wireMockServer.stubFor(get(urlPathEqualTo(CONTAINER_CREDENTIALS_PATH)) |
| 103 | + .willReturn(aResponse().withBody(credentialsResponse))); |
| 104 | + } |
| 105 | + |
| 106 | + private void stubContainerCredentialsResponsesWithAuthToken() { |
| 107 | + System.setProperty(SdkSystemSetting.AWS_CONTAINER_AUTHORIZATION_TOKEN.property(), "test-auth-token"); |
| 108 | + |
| 109 | + String credentialsResponse = createCredentialsResponse("ACCESS_KEY_ID", "SECRET_ACCESS_KEY", null); |
| 110 | + wireMockServer.stubFor(get(urlPathEqualTo(CONTAINER_CREDENTIALS_PATH)) |
| 111 | + .willReturn(aResponse().withBody(credentialsResponse))); |
| 112 | + } |
| 113 | + |
| 114 | + private String createCredentialsResponse(String accessKeyId, String secretAccessKey, String sessionToken) { |
| 115 | + StringBuilder response = new StringBuilder(); |
| 116 | + response.append("{"); |
| 117 | + response.append("\"AccessKeyId\":\"").append(accessKeyId).append("\","); |
| 118 | + response.append("\"SecretAccessKey\":\"").append(secretAccessKey).append("\","); |
| 119 | + if (sessionToken != null) { |
| 120 | + response.append("\"Token\":\"").append(sessionToken).append("\","); |
| 121 | + } |
| 122 | + response.append("\"Expiration\":\"").append(DateUtils.formatIso8601Date(Instant.now().plus(Duration.ofHours(1)))).append("\""); |
| 123 | + response.append("}"); |
| 124 | + return response.toString(); |
| 125 | + } |
| 126 | + |
| 127 | + @ParameterizedTest |
| 128 | + @MethodSource("containerCredentialProviders") |
| 129 | + void userAgentString_containsContainerBusinessMetric_WhenUsingContainerCredentials( |
| 130 | + IdentityProvider<? extends AwsCredentialsIdentity> provider, String expected) throws Exception { |
| 131 | + |
| 132 | + stsClient(provider, mockHttpClient).getCallerIdentity(); |
| 133 | + |
| 134 | + SdkHttpRequest lastRequest = mockHttpClient.getLastRequest(); |
| 135 | + assertThat(lastRequest).isNotNull(); |
| 136 | + |
| 137 | + List<String> userAgentHeaders = lastRequest.headers().get("User-Agent"); |
| 138 | + assertThat(userAgentHeaders).isNotNull().hasSize(1); |
| 139 | + assertThat(userAgentHeaders.get(0)).contains(expected); |
| 140 | + } |
| 141 | + |
| 142 | + private static Stream<Arguments> containerCredentialProviders() { |
| 143 | + return Stream.of( |
| 144 | + Arguments.of(ContainerCredentialsProvider.create(), "m/D,z"), |
| 145 | + |
| 146 | + Arguments.of(ContainerCredentialsProvider.builder() |
| 147 | + .endpoint(CONTAINER_SERVICE_ENDPOINT + wireMockServer.getPort()) |
| 148 | + .build(), "m/D,z") |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + @ParameterizedTest |
| 153 | + @MethodSource("containerCredentialProvidersWithSessionToken") |
| 154 | + void userAgentString_containsContainerBusinessMetric_WhenUsingContainerCredentialsWithSessionToken( |
| 155 | + IdentityProvider<? extends AwsCredentialsIdentity> provider, String expected) throws Exception { |
| 156 | + |
| 157 | + stubContainerCredentialsResponsesWithSessionToken(); |
| 158 | + |
| 159 | + stsClient(provider, mockHttpClient).getCallerIdentity(); |
| 160 | + |
| 161 | + SdkHttpRequest lastRequest = mockHttpClient.getLastRequest(); |
| 162 | + assertThat(lastRequest).isNotNull(); |
| 163 | + |
| 164 | + List<String> userAgentHeaders = lastRequest.headers().get("User-Agent"); |
| 165 | + assertThat(userAgentHeaders).isNotNull().hasSize(1); |
| 166 | + assertThat(userAgentHeaders.get(0)).contains(expected); |
| 167 | + } |
| 168 | + |
| 169 | + private static Stream<Arguments> containerCredentialProvidersWithSessionToken() { |
| 170 | + return Stream.of( |
| 171 | + Arguments.of(ContainerCredentialsProvider.create(), "m/D,z") |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | + @ParameterizedTest |
| 176 | + @MethodSource("containerCredentialProvidersWithAuthToken") |
| 177 | + void userAgentString_containsContainerBusinessMetric_WhenUsingContainerCredentialsWithAuthToken( |
| 178 | + IdentityProvider<? extends AwsCredentialsIdentity> provider, String expected) throws Exception { |
| 179 | + |
| 180 | + stubContainerCredentialsResponsesWithAuthToken(); |
| 181 | + |
| 182 | + stsClient(provider, mockHttpClient).getCallerIdentity(); |
| 183 | + |
| 184 | + SdkHttpRequest lastRequest = mockHttpClient.getLastRequest(); |
| 185 | + assertThat(lastRequest).isNotNull(); |
| 186 | + |
| 187 | + List<String> userAgentHeaders = lastRequest.headers().get("User-Agent"); |
| 188 | + assertThat(userAgentHeaders).isNotNull().hasSize(1); |
| 189 | + assertThat(userAgentHeaders.get(0)).contains(expected); |
| 190 | + } |
| 191 | + |
| 192 | + private static Stream<Arguments> containerCredentialProvidersWithAuthToken() { |
| 193 | + return Stream.of( |
| 194 | + Arguments.of(ContainerCredentialsProvider.create(), "m/D,z") |
| 195 | + ); |
| 196 | + } |
| 197 | + |
| 198 | + private static StsClient stsClient(IdentityProvider<? extends AwsCredentialsIdentity> provider, SdkHttpClient httpClient) { |
| 199 | + return StsClient.builder() |
| 200 | + .credentialsProvider(provider) |
| 201 | + .httpClient(httpClient) |
| 202 | + .build(); |
| 203 | + } |
| 204 | +} |
0 commit comments