|
| 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 software.amazon.awssdk.core.useragent.BusinessMetricCollection.METRIC_SEARCH_PATTERN; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 25 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 26 | +import software.amazon.awssdk.auth.signer.Aws4Signer; |
| 27 | +import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; |
| 28 | +import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption; |
| 29 | +import software.amazon.awssdk.http.AbortableInputStream; |
| 30 | +import software.amazon.awssdk.http.HttpExecuteResponse; |
| 31 | +import software.amazon.awssdk.http.SdkHttpRequest; |
| 32 | +import software.amazon.awssdk.http.SdkHttpResponse; |
| 33 | +import software.amazon.awssdk.regions.Region; |
| 34 | +import software.amazon.awssdk.services.multiauth.MultiauthClient; |
| 35 | +import software.amazon.awssdk.services.multiauth.auth.scheme.MultiauthAuthSchemeProvider; |
| 36 | +import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient; |
| 37 | +import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient; |
| 38 | +import software.amazon.awssdk.services.sigv4aauth.Sigv4AauthAsyncClient; |
| 39 | +import software.amazon.awssdk.services.sigv4aauth.Sigv4AauthClient; |
| 40 | +import software.amazon.awssdk.testutils.service.http.MockAsyncHttpClient; |
| 41 | +import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient; |
| 42 | +import software.amazon.awssdk.utils.StringInputStream; |
| 43 | +import java.util.Arrays; |
| 44 | + |
| 45 | +/** |
| 46 | + * Test class to verify that SIGV4A_SIGNING business metric is correctly included |
| 47 | + * in the User-Agent header when operation called using Sigv4A signing. |
| 48 | + */ |
| 49 | +class Sigv4aBusinessMetricUserAgentTest { |
| 50 | + private static final String USER_AGENT_HEADER_NAME = "User-Agent"; |
| 51 | + private static final StaticCredentialsProvider CREDENTIALS_PROVIDER = |
| 52 | + StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid")); |
| 53 | + |
| 54 | + private MockSyncHttpClient mockHttpClient; |
| 55 | + private MockAsyncHttpClient mockAsyncHttpClient; |
| 56 | + |
| 57 | + @BeforeEach |
| 58 | + public void setup() { |
| 59 | + mockHttpClient = new MockSyncHttpClient(); |
| 60 | + mockHttpClient.stubNextResponse(mockResponse()); |
| 61 | + |
| 62 | + mockAsyncHttpClient = new MockAsyncHttpClient(); |
| 63 | + mockAsyncHttpClient.stubNextResponse(mockResponse()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void when_sigv4aServiceIsUsed_correctMetricIsAdded() { |
| 68 | + Sigv4AauthClient client = Sigv4AauthClient.builder() |
| 69 | + .region(Region.US_WEST_2) |
| 70 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 71 | + .httpClient(mockHttpClient) |
| 72 | + .build(); |
| 73 | + |
| 74 | + client.simpleOperationWithNoEndpointParams(r -> r.stringMember("test")); |
| 75 | + |
| 76 | + String userAgent = getUserAgentFromLastRequest(); |
| 77 | + assertThat(userAgent).matches(METRIC_SEARCH_PATTERN.apply("S")); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void when_sigv4aServiceIsUsedAsync_correctMetricIsAdded() { |
| 82 | + Sigv4AauthAsyncClient asyncClient = Sigv4AauthAsyncClient.builder() |
| 83 | + .region(Region.US_WEST_2) |
| 84 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 85 | + .httpClient(mockAsyncHttpClient) |
| 86 | + .build(); |
| 87 | + |
| 88 | + asyncClient.simpleOperationWithNoEndpointParams(r -> r.stringMember("test")).join(); |
| 89 | + |
| 90 | + String userAgent = getUserAgentFromLastAsyncRequest(); |
| 91 | + assertThat(userAgent).matches(METRIC_SEARCH_PATTERN.apply("S")); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void when_regularServiceIsUsed_sigv4aMetricIsNotAdded() { |
| 96 | + ProtocolRestJsonClient client = ProtocolRestJsonClient.builder() |
| 97 | + .region(Region.US_WEST_2) |
| 98 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 99 | + .httpClient(mockHttpClient) |
| 100 | + .build(); |
| 101 | + |
| 102 | + client.allTypes(r -> {}); |
| 103 | + |
| 104 | + String userAgent = getUserAgentFromLastRequest(); |
| 105 | + assertThat(userAgent).doesNotMatch(METRIC_SEARCH_PATTERN.apply("S")); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void when_regularServiceIsUsedAsync_sigv4aMetricIsNotAdded() { |
| 110 | + ProtocolRestJsonAsyncClient asyncClient = ProtocolRestJsonAsyncClient.builder() |
| 111 | + .region(Region.US_WEST_2) |
| 112 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 113 | + .httpClient(mockAsyncHttpClient) |
| 114 | + .build(); |
| 115 | + |
| 116 | + asyncClient.allTypes(r -> {}).join(); |
| 117 | + |
| 118 | + String userAgent = getUserAgentFromLastAsyncRequest(); |
| 119 | + assertThat(userAgent).doesNotMatch(METRIC_SEARCH_PATTERN.apply("S")); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void when_signerIsOverridden_sigv4aMetricIsNotAdded() { |
| 124 | + MultiauthClient client = MultiauthClient.builder() |
| 125 | + .region(Region.US_WEST_2) |
| 126 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 127 | + .overrideConfiguration(ClientOverrideConfiguration.builder() |
| 128 | + .putAdvancedOption(SdkAdvancedClientOption.SIGNER, |
| 129 | + Aws4Signer.create()) |
| 130 | + .build()) |
| 131 | + .httpClient(mockHttpClient) |
| 132 | + .build(); |
| 133 | + |
| 134 | + client.multiAuthWithOnlySigv4aAndSigv4(r -> r.stringMember("test")); |
| 135 | + String userAgent = getUserAgentFromLastRequest(); |
| 136 | + |
| 137 | + assertThat(userAgent).doesNotMatch(METRIC_SEARCH_PATTERN.apply("S")); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void when_authSchemeProviderOverridesSigv4aOrder_sigv4IsSelected() { |
| 142 | + MultiauthClient client = MultiauthClient.builder() |
| 143 | + .region(Region.US_WEST_2) |
| 144 | + .credentialsProvider(CREDENTIALS_PROVIDER) |
| 145 | + .authSchemeProvider(MultiauthAuthSchemeProvider. |
| 146 | + defaultProvider( |
| 147 | + Arrays.asList("sigv4","sigv4a"))) |
| 148 | + .httpClient(mockHttpClient) |
| 149 | + .build(); |
| 150 | + |
| 151 | + client.multiAuthWithOnlySigv4aAndSigv4(r -> r.stringMember("test")); |
| 152 | + String userAgent = getUserAgentFromLastRequest(); |
| 153 | + |
| 154 | + assertThat(userAgent).doesNotMatch(METRIC_SEARCH_PATTERN.apply("S")); |
| 155 | + } |
| 156 | + |
| 157 | + private String getUserAgentFromLastRequest() { |
| 158 | + SdkHttpRequest lastRequest = mockHttpClient.getLastRequest(); |
| 159 | + assertThat(lastRequest).isNotNull(); |
| 160 | + |
| 161 | + List<String> userAgentHeaders = lastRequest.headers().get(USER_AGENT_HEADER_NAME); |
| 162 | + assertThat(userAgentHeaders).isNotNull().hasSize(1); |
| 163 | + return userAgentHeaders.get(0); |
| 164 | + } |
| 165 | + |
| 166 | + private String getUserAgentFromLastAsyncRequest() { |
| 167 | + SdkHttpRequest lastRequest = mockAsyncHttpClient.getLastRequest(); |
| 168 | + assertThat(lastRequest).isNotNull(); |
| 169 | + |
| 170 | + List<String> userAgentHeaders = lastRequest.headers().get(USER_AGENT_HEADER_NAME); |
| 171 | + assertThat(userAgentHeaders).isNotNull().hasSize(1); |
| 172 | + return userAgentHeaders.get(0); |
| 173 | + } |
| 174 | + |
| 175 | + private static HttpExecuteResponse mockResponse() { |
| 176 | + return HttpExecuteResponse.builder() |
| 177 | + .response(SdkHttpResponse.builder().statusCode(200).build()) |
| 178 | + .responseBody(AbortableInputStream.create(new StringInputStream("{}"))) |
| 179 | + .build(); |
| 180 | + } |
| 181 | +} |
0 commit comments