- 
                Notifications
    You must be signed in to change notification settings 
- Fork 949
Add business metric support for Sigv4A auth scheme #6489
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
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            16 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      e0c2f8b
              
                Add business metric support for Sigv4A auth scheme
              
              
                S-Saranya1 d577caa
              
                Merge branch 'master' into somepal/sigv4a-auth-scheme-featureID
              
              
                S-Saranya1 558ac66
              
                Fixing failing tests
              
              
                S-Saranya1 823f0bd
              
                Address PR feedback
              
              
                S-Saranya1 cd2136b
              
                Additional changes
              
              
                S-Saranya1 3d7dab8
              
                Fixing test failures
              
              
                S-Saranya1 bd7a7a6
              
                Fixing test failures
              
              
                S-Saranya1 f9ded2e
              
                Fix test failure
              
              
                S-Saranya1 db8c797
              
                Additional changes in test:
              
              
                S-Saranya1 57016a0
              
                Address review comments
              
              
                S-Saranya1 7577da9
              
                Removing unused import
              
              
                S-Saranya1 025cb48
              
                Merge branch 'master' into somepal/sigv4a-auth-scheme-featureID
              
              
                S-Saranya1 ed69ac0
              
                Move sigv4a business metric logic to auth interceptor to record at au…
              
              
                S-Saranya1 b69ebe7
              
                Remove changes from ApplyUserAgentStage
              
              
                S-Saranya1 059c869
              
                Adding missed file
              
              
                S-Saranya1 ca6352b
              
                Address review comments
              
              
                S-Saranya1 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "type": "feature", | ||
| "category": "AWS SDK for Java v2", | ||
| "contributor": "", | ||
| "description": "Add business metric support for Sigv4A auth scheme to track when an operation is called using Sigv4A signing" | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
  
    
      
          
            181 changes: 181 additions & 0 deletions
          
          181 
        
  ...test/src/test/java/software/amazon/awssdk/services/Sigv4aBusinessMetricUserAgentTest.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| /* | ||
| * 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.assertj.core.api.Assertions.assertThat; | ||
| import static software.amazon.awssdk.core.useragent.BusinessMetricCollection.METRIC_SEARCH_PATTERN; | ||
|  | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
| import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
| import software.amazon.awssdk.auth.signer.Aws4Signer; | ||
| import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; | ||
| import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption; | ||
| import software.amazon.awssdk.http.AbortableInputStream; | ||
| import software.amazon.awssdk.http.HttpExecuteResponse; | ||
| import software.amazon.awssdk.http.SdkHttpRequest; | ||
| import software.amazon.awssdk.http.SdkHttpResponse; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.services.multiauth.MultiauthClient; | ||
| import software.amazon.awssdk.services.multiauth.auth.scheme.MultiauthAuthSchemeProvider; | ||
| import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient; | ||
| import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient; | ||
| import software.amazon.awssdk.services.sigv4aauth.Sigv4AauthAsyncClient; | ||
| import software.amazon.awssdk.services.sigv4aauth.Sigv4AauthClient; | ||
| import software.amazon.awssdk.testutils.service.http.MockAsyncHttpClient; | ||
| import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient; | ||
| import software.amazon.awssdk.utils.StringInputStream; | ||
| import java.util.Arrays; | ||
|  | ||
| /** | ||
| * Test class to verify that SIGV4A_SIGNING business metric is correctly included | ||
| * in the User-Agent header when operation called using Sigv4A signing. | ||
| */ | ||
| class Sigv4aBusinessMetricUserAgentTest { | ||
| private static final String USER_AGENT_HEADER_NAME = "User-Agent"; | ||
| private static final StaticCredentialsProvider CREDENTIALS_PROVIDER = | ||
| StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid")); | ||
|  | ||
| private MockSyncHttpClient mockHttpClient; | ||
| private MockAsyncHttpClient mockAsyncHttpClient; | ||
|  | ||
| @BeforeEach | ||
| public void setup() { | ||
| mockHttpClient = new MockSyncHttpClient(); | ||
| mockHttpClient.stubNextResponse(mockResponse()); | ||
|  | ||
| mockAsyncHttpClient = new MockAsyncHttpClient(); | ||
| mockAsyncHttpClient.stubNextResponse(mockResponse()); | ||
| } | ||
|  | ||
| @Test | ||
| void when_sigv4aServiceIsUsed_correctMetricIsAdded() { | ||
| Sigv4AauthClient client = Sigv4AauthClient.builder() | ||
| .region(Region.US_WEST_2) | ||
| .credentialsProvider(CREDENTIALS_PROVIDER) | ||
| .httpClient(mockHttpClient) | ||
| .build(); | ||
|  | ||
| client.simpleOperationWithNoEndpointParams(r -> r.stringMember("test")); | ||
|  | ||
| String userAgent = getUserAgentFromLastRequest(); | ||
| assertThat(userAgent).matches(METRIC_SEARCH_PATTERN.apply("S")); | ||
| } | ||
|  | ||
| @Test | ||
| void when_sigv4aServiceIsUsedAsync_correctMetricIsAdded() { | ||
| Sigv4AauthAsyncClient asyncClient = Sigv4AauthAsyncClient.builder() | ||
| .region(Region.US_WEST_2) | ||
| .credentialsProvider(CREDENTIALS_PROVIDER) | ||
| .httpClient(mockAsyncHttpClient) | ||
| .build(); | ||
|  | ||
| asyncClient.simpleOperationWithNoEndpointParams(r -> r.stringMember("test")).join(); | ||
|  | ||
| String userAgent = getUserAgentFromLastAsyncRequest(); | ||
| assertThat(userAgent).matches(METRIC_SEARCH_PATTERN.apply("S")); | ||
| } | ||
|  | ||
| @Test | ||
| void when_regularServiceIsUsed_sigv4aMetricIsNotAdded() { | ||
| ProtocolRestJsonClient client = ProtocolRestJsonClient.builder() | ||
| .region(Region.US_WEST_2) | ||
| .credentialsProvider(CREDENTIALS_PROVIDER) | ||
| .httpClient(mockHttpClient) | ||
| .build(); | ||
|  | ||
| client.allTypes(r -> {}); | ||
|  | ||
| String userAgent = getUserAgentFromLastRequest(); | ||
| assertThat(userAgent).doesNotMatch(METRIC_SEARCH_PATTERN.apply("S")); | ||
| } | ||
|  | ||
| @Test | ||
| void when_regularServiceIsUsedAsync_sigv4aMetricIsNotAdded() { | ||
| ProtocolRestJsonAsyncClient asyncClient = ProtocolRestJsonAsyncClient.builder() | ||
| .region(Region.US_WEST_2) | ||
| .credentialsProvider(CREDENTIALS_PROVIDER) | ||
| .httpClient(mockAsyncHttpClient) | ||
| .build(); | ||
|  | ||
| asyncClient.allTypes(r -> {}).join(); | ||
|  | ||
| String userAgent = getUserAgentFromLastAsyncRequest(); | ||
| assertThat(userAgent).doesNotMatch(METRIC_SEARCH_PATTERN.apply("S")); | ||
| } | ||
|  | ||
| @Test | ||
| void when_signerIsOverridden_sigv4aMetricIsNotAdded() { | ||
| MultiauthClient client = MultiauthClient.builder() | ||
| .region(Region.US_WEST_2) | ||
| .credentialsProvider(CREDENTIALS_PROVIDER) | ||
| .overrideConfiguration(ClientOverrideConfiguration.builder() | ||
| .putAdvancedOption(SdkAdvancedClientOption.SIGNER, | ||
| Aws4Signer.create()) | ||
| .build()) | ||
| .httpClient(mockHttpClient) | ||
| .build(); | ||
|  | ||
| client.multiAuthWithOnlySigv4aAndSigv4(r -> r.stringMember("test")); | ||
| String userAgent = getUserAgentFromLastRequest(); | ||
|  | ||
| assertThat(userAgent).doesNotMatch(METRIC_SEARCH_PATTERN.apply("S")); | ||
| } | ||
|  | ||
| @Test | ||
| void when_authSchemeProviderOverridesSigv4aOrder_sigv4IsSelected() { | ||
| MultiauthClient client = MultiauthClient.builder() | ||
| .region(Region.US_WEST_2) | ||
| .credentialsProvider(CREDENTIALS_PROVIDER) | ||
| .authSchemeProvider(MultiauthAuthSchemeProvider. | ||
| defaultProvider( | ||
| Arrays.asList("sigv4","sigv4a"))) | ||
| .httpClient(mockHttpClient) | ||
| .build(); | ||
|  | ||
| client.multiAuthWithOnlySigv4aAndSigv4(r -> r.stringMember("test")); | ||
| String userAgent = getUserAgentFromLastRequest(); | ||
|  | ||
| assertThat(userAgent).doesNotMatch(METRIC_SEARCH_PATTERN.apply("S")); | ||
| } | ||
|  | ||
| private String getUserAgentFromLastRequest() { | ||
| SdkHttpRequest lastRequest = mockHttpClient.getLastRequest(); | ||
| assertThat(lastRequest).isNotNull(); | ||
|  | ||
| List<String> userAgentHeaders = lastRequest.headers().get(USER_AGENT_HEADER_NAME); | ||
| assertThat(userAgentHeaders).isNotNull().hasSize(1); | ||
| return userAgentHeaders.get(0); | ||
| } | ||
|  | ||
| private String getUserAgentFromLastAsyncRequest() { | ||
| SdkHttpRequest lastRequest = mockAsyncHttpClient.getLastRequest(); | ||
| assertThat(lastRequest).isNotNull(); | ||
|  | ||
| List<String> userAgentHeaders = lastRequest.headers().get(USER_AGENT_HEADER_NAME); | ||
| assertThat(userAgentHeaders).isNotNull().hasSize(1); | ||
| return userAgentHeaders.get(0); | ||
| } | ||
|  | ||
| private static HttpExecuteResponse mockResponse() { | ||
| return HttpExecuteResponse.builder() | ||
| .response(SdkHttpResponse.builder().statusCode(200).build()) | ||
| .responseBody(AbortableInputStream.create(new StringInputStream("{}"))) | ||
| .build(); | ||
| } | ||
| } | ||
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.