-
Notifications
You must be signed in to change notification settings - Fork 936
Implement feature IDs for S3 Features #6402
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
Closed
S-Saranya1
wants to merge
3
commits into
feature/master/feature-ids-implementation
from
somepal/s3-featureID-implementation
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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": "Implemented business metrics tracking for S3_Express_Bucket (feature ID \"J\") and S3_Access_Grants plugin (feature ID \"K\") through User-Agent header." | ||
} |
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
240 changes: 240 additions & 0 deletions
240
...rc/it/java/software/amazon/awssdk/services/s3/S3AccessGrantsUserAgentIntegrationTest.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,240 @@ | ||
/* | ||
* 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.s3; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
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.core.interceptor.Context; | ||
import software.amazon.awssdk.core.interceptor.ExecutionAttributes; | ||
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; | ||
import software.amazon.awssdk.core.sync.RequestBody; | ||
import software.amazon.awssdk.core.sync.ResponseTransformer; | ||
import software.amazon.awssdk.core.useragent.BusinessMetricFeatureId; | ||
import software.amazon.awssdk.http.SdkHttpRequest; | ||
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; | ||
import software.amazon.awssdk.identity.spi.IdentityProvider; | ||
import software.amazon.awssdk.identity.spi.IdentityProviders; | ||
import software.amazon.awssdk.identity.spi.ResolveIdentityRequest; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.s3.model.GetObjectRequest; | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
|
||
/** | ||
* Integration test to verify that S3 Access Grants operations include the correct business metric feature ID | ||
* in the User-Agent header for tracking purposes. | ||
*/ | ||
public class S3AccessGrantsUserAgentIntegrationTest extends S3IntegrationTestBase { | ||
private static final String KEY = "test-s3-access-grants-feature-id.txt"; | ||
private static final String CONTENTS = "test content for S3 Access Grants feature id validation"; | ||
private static final UserAgentCapturingInterceptor userAgentInterceptor = new UserAgentCapturingInterceptor(); | ||
|
||
private static S3Client s3WithAccessGrants; | ||
private static S3Client regularS3; | ||
private static String testBucket; | ||
|
||
@BeforeAll | ||
static void setup() { | ||
testBucket = temporaryBucketName(S3AccessGrantsUserAgentIntegrationTest.class); | ||
|
||
s3WithAccessGrants = S3Client.builder() | ||
.region(Region.US_EAST_1) | ||
.credentialsProvider(StaticCredentialsProvider.create( | ||
AwsBasicCredentials.create("test", "test"))) | ||
.overrideConfiguration(o -> o.addExecutionInterceptor(userAgentInterceptor) | ||
.addExecutionInterceptor(new S3AccessGrantsSimulatorInterceptor())) | ||
.build(); | ||
|
||
regularS3 = S3Client.builder() | ||
.region(Region.US_EAST_1) | ||
.credentialsProvider(StaticCredentialsProvider.create( | ||
AwsBasicCredentials.create("test", "test"))) | ||
.overrideConfiguration(o -> o.addExecutionInterceptor(userAgentInterceptor)) | ||
.build(); | ||
|
||
regularS3.createBucket(b -> b.bucket(testBucket)); | ||
regularS3.waiter().waitUntilBucketExists(r -> r.bucket(testBucket)); | ||
} | ||
|
||
@AfterAll | ||
static void teardown() { | ||
try { | ||
deleteBucketAndAllContents(testBucket); | ||
} catch (Exception e) { } | ||
|
||
if (s3WithAccessGrants != null) { | ||
s3WithAccessGrants.close(); | ||
} | ||
if (regularS3 != null) { | ||
regularS3.close(); | ||
} | ||
} | ||
|
||
@BeforeEach | ||
void reset() { | ||
userAgentInterceptor.reset(); | ||
} | ||
|
||
@Test | ||
void putObject_whenS3AccessGrantsPluginActive_shouldIncludeS3AccessGrantsFeatureIdInUserAgent() { | ||
PutObjectRequest putRequest = PutObjectRequest.builder() | ||
.bucket(testBucket) | ||
.key(KEY) | ||
.build(); | ||
|
||
s3WithAccessGrants.putObject(putRequest, RequestBody.fromString(CONTENTS)); | ||
|
||
List<String> capturedUserAgents = userAgentInterceptor.getCapturedUserAgents(); | ||
assertThat(capturedUserAgents).hasSize(1); | ||
|
||
String userAgent = capturedUserAgents.get(0); | ||
assertThat(userAgent).isNotNull(); | ||
|
||
// Verify the User-Agent contains the S3 Access Grants business metric feature ID | ||
String expectedFeatureId = BusinessMetricFeatureId.S3_ACCESS_GRANTS.value(); | ||
assertThat(userAgent).containsPattern("m/([A-Za-z0-9+\\-]+,)*" + expectedFeatureId + "(,[A-Za-z0-9+\\-]+)*"); | ||
|
||
userAgentInterceptor.reset(); | ||
|
||
GetObjectRequest getRequest = GetObjectRequest.builder() | ||
.bucket(testBucket) | ||
.key(KEY) | ||
.build(); | ||
|
||
s3WithAccessGrants.getObject(getRequest, ResponseTransformer.toBytes()); | ||
|
||
capturedUserAgents = userAgentInterceptor.getCapturedUserAgents(); | ||
assertThat(capturedUserAgents).hasSize(1); | ||
|
||
userAgent = capturedUserAgents.get(0); | ||
assertThat(userAgent).isNotNull(); | ||
assertThat(userAgent).containsPattern("m/([A-Za-z0-9+\\-]+,)*" + expectedFeatureId + "(,[A-Za-z0-9+\\-]+)*"); | ||
} | ||
|
||
@Test | ||
void putObject_whenS3AccessGrantsPluginNotActive_shouldNotIncludeS3AccessGrantsFeatureIdInUserAgent() { | ||
PutObjectRequest putRequest = PutObjectRequest.builder() | ||
.bucket(testBucket) | ||
.key(KEY + "-regular") | ||
.build(); | ||
|
||
regularS3.putObject(putRequest, RequestBody.fromString(CONTENTS)); | ||
|
||
List<String> capturedUserAgents = userAgentInterceptor.getCapturedUserAgents(); | ||
assertThat(capturedUserAgents).hasSize(1); | ||
|
||
String userAgent = capturedUserAgents.get(0); | ||
assertThat(userAgent).isNotNull(); | ||
|
||
// Verify the User-Agent does not contain the S3 Access Grants business metric feature ID | ||
String s3AccessGrantsFeatureId = BusinessMetricFeatureId.S3_ACCESS_GRANTS.value(); | ||
assertThat(userAgent).doesNotMatch(".*m/([A-Za-z0-9+\\-]+,)*" + s3AccessGrantsFeatureId + "(,[A-Za-z0-9+\\-]+)*.*"); | ||
|
||
userAgentInterceptor.reset(); | ||
|
||
GetObjectRequest getRequest = GetObjectRequest.builder() | ||
.bucket(testBucket) | ||
.key(KEY + "-regular") | ||
.build(); | ||
|
||
regularS3.getObject(getRequest, ResponseTransformer.toBytes()); | ||
|
||
capturedUserAgents = userAgentInterceptor.getCapturedUserAgents(); | ||
assertThat(capturedUserAgents).hasSize(1); | ||
|
||
userAgent = capturedUserAgents.get(0); | ||
assertThat(userAgent).isNotNull(); | ||
assertThat(userAgent).doesNotMatch(".*m/([A-Za-z0-9+\\-]+,)*" + s3AccessGrantsFeatureId + "(,[A-Za-z0-9+\\-]+)*.*"); | ||
} | ||
|
||
/** | ||
* Interceptor to capture User-Agent headers from HTTP requests | ||
*/ | ||
private static class UserAgentCapturingInterceptor implements ExecutionInterceptor { | ||
private final List<String> capturedUserAgents = new ArrayList<>(); | ||
private final AtomicReference<String> lastUserAgent = new AtomicReference<>(); | ||
|
||
@Override | ||
public void beforeTransmission(Context.BeforeTransmission context, ExecutionAttributes executionAttributes) { | ||
SdkHttpRequest httpRequest = context.httpRequest(); | ||
List<String> userAgentHeaders = httpRequest.headers().get("User-Agent"); | ||
|
||
if (userAgentHeaders != null && !userAgentHeaders.isEmpty()) { | ||
String userAgent = userAgentHeaders.get(0); | ||
capturedUserAgents.add(userAgent); | ||
lastUserAgent.set(userAgent); | ||
} | ||
} | ||
|
||
public List<String> getCapturedUserAgents() { | ||
return new ArrayList<>(capturedUserAgents); | ||
} | ||
|
||
public String getLastUserAgent() { | ||
return lastUserAgent.get(); | ||
} | ||
|
||
public void reset() { | ||
capturedUserAgents.clear(); | ||
lastUserAgent.set(null); | ||
} | ||
} | ||
|
||
/** | ||
* Interceptor that simulates the presence of S3 Access Grants plugin by injecting | ||
* a mock identity provider with the expected class name pattern. | ||
*/ | ||
private static class S3AccessGrantsSimulatorInterceptor implements ExecutionInterceptor { | ||
@Override | ||
public void beforeExecution(Context.BeforeExecution context, ExecutionAttributes executionAttributes) { | ||
IdentityProvider<AwsCredentialsIdentity> mockS3AccessGrantsProvider = new MockS3AccessGrantsIdentityProvider(); | ||
|
||
IdentityProviders identityProviders = IdentityProviders.builder() | ||
.putIdentityProvider(mockS3AccessGrantsProvider) | ||
.build(); | ||
|
||
executionAttributes.putAttribute( | ||
software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute.IDENTITY_PROVIDERS, | ||
identityProviders | ||
); | ||
} | ||
} | ||
|
||
private static class MockS3AccessGrantsIdentityProvider implements IdentityProvider<AwsCredentialsIdentity> { | ||
@Override | ||
public Class<AwsCredentialsIdentity> identityType() { | ||
return AwsCredentialsIdentity.class; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<AwsCredentialsIdentity> resolveIdentity(ResolveIdentityRequest request) { | ||
// Return basic credentials for testing | ||
return CompletableFuture.completedFuture( | ||
AwsCredentialsIdentity.create("test-access-key", "test-secret-key") | ||
); | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add Junit and use mock/stubs and verify the request is created with required header ?