Skip to content

Support ClientContextParams for S3CrtAsyncClient #3880

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 2 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AmazonS3-71a2dda.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "Amazon S3",
"contributor": "",
"description": "Add support for the following in the CRT S3 client:\n\n - Enabling/disabling accelerate endpoints\n - Using pathstyle addressing"
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ default S3CrtAsyncClientBuilder httpConfiguration(Consumer<S3CrtHttpConfiguratio
.build());
}

// S3 client context params, copied from S3BaseClientBuilder. Note we only have accelerate and path style because they're
// the only ones we can support in the CRT client (does not affect signing).
/**
* Enables this client to use S3 Transfer Acceleration endpoints.
*/
S3CrtAsyncClientBuilder accelerate(Boolean accelerate);

/**
* Forces this client to use path-style addressing for buckets.
*/
S3CrtAsyncClientBuilder forcePathStyle(Boolean forcePathStyle);

@Override
S3AsyncClient build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
import static software.amazon.awssdk.services.s3.internal.crt.S3NativeClientConfiguration.DEFAULT_PART_SIZE_IN_BYTES;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.annotations.SdkTestInternalApi;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.awscore.AwsRequest;
import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration;
import software.amazon.awssdk.core.SdkRequest;
import software.amazon.awssdk.core.checksums.ChecksumValidation;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption;
import software.amazon.awssdk.core.interceptor.Context;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
Expand Down Expand Up @@ -69,22 +73,31 @@ public CompletableFuture<CopyObjectResponse> copyObject(CopyObjectRequest copyOb
}

private static S3AsyncClient initializeS3AsyncClient(DefaultS3CrtClientBuilder builder) {
ClientOverrideConfiguration.Builder overrideConfigurationBuilder =
ClientOverrideConfiguration.builder()
// Disable checksum, retry policy and signer because they are handled in crt
.putAdvancedOption(SdkAdvancedClientOption.SIGNER, new NoOpSigner())
.putExecutionAttribute(SdkExecutionAttribute.HTTP_RESPONSE_CHECKSUM_VALIDATION,
ChecksumValidation.FORCE_SKIP)
.retryPolicy(RetryPolicy.none())
.addExecutionInterceptor(new ValidateRequestInterceptor())
.addExecutionInterceptor(new AttachHttpAttributesExecutionInterceptor());

if (builder.executionInterceptors != null) {
builder.executionInterceptors.forEach(overrideConfigurationBuilder::addExecutionInterceptor);
}

return S3AsyncClient.builder()
// Disable checksum, retry policy and signer because they are handled in crt
// Disable checksum, it is handled in CRT
.serviceConfiguration(S3Configuration.builder()
.checksumValidationEnabled(false)
.build())
.region(builder.region)
.endpointOverride(builder.endpointOverride)
.credentialsProvider(builder.credentialsProvider)
.overrideConfiguration(o -> o.putAdvancedOption(SdkAdvancedClientOption.SIGNER,
new NoOpSigner())
.putExecutionAttribute(
SdkExecutionAttribute.HTTP_RESPONSE_CHECKSUM_VALIDATION,
ChecksumValidation.FORCE_SKIP)
.retryPolicy(RetryPolicy.none())
.addExecutionInterceptor(new ValidateRequestInterceptor())
.addExecutionInterceptor(new AttachHttpAttributesExecutionInterceptor()))
.overrideConfiguration(overrideConfigurationBuilder.build())
.accelerate(builder.accelerate)
.forcePathStyle(builder.forcePathStyle)
.httpClientBuilder(initializeS3CrtAsyncHttpClient(builder))
.build();
}
Expand Down Expand Up @@ -123,6 +136,10 @@ public static final class DefaultS3CrtClientBuilder implements S3CrtAsyncClientB
private URI endpointOverride;
private Boolean checksumValidationEnabled;
private S3CrtHttpConfiguration httpConfiguration;
private Boolean accelerate;
private Boolean forcePathStyle;

private List<ExecutionInterceptor> executionInterceptors;

public AwsCredentialsProvider credentialsProvider() {
return credentialsProvider;
Expand Down Expand Up @@ -206,6 +223,27 @@ public S3CrtAsyncClientBuilder httpConfiguration(S3CrtHttpConfiguration configur
return this;
}

@Override
public S3CrtAsyncClientBuilder accelerate(Boolean accelerate) {
this.accelerate = accelerate;
return this;
}

@Override
public S3CrtAsyncClientBuilder forcePathStyle(Boolean forcePathStyle) {
this.forcePathStyle = forcePathStyle;
return this;
}

@SdkTestInternalApi
S3CrtAsyncClientBuilder addExecutionInterceptor(ExecutionInterceptor executionInterceptor) {
if (executionInterceptors == null) {
this.executionInterceptors = new ArrayList<>();
}
executionInterceptors.add(executionInterceptor);
return this;
}

@Override
public S3CrtAsyncClient build() {
return new DefaultS3CrtAsyncClient(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@

package software.amazon.awssdk.services.s3.internal.crt;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import software.amazon.awssdk.auth.signer.AwsS3V4Signer;
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
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.interceptor.SdkInternalExecutionAttribute;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.endpoints.S3ClientContextParams;
import software.amazon.awssdk.utils.AttributeMap;

class DefaultS3CrtAsyncClientTest {

Expand All @@ -40,6 +48,37 @@ void requestSignerOverrideProvided_shouldThrowException() {
}
}

@Test
void clientContextParamsSetOnBuilder_propagatedToInterceptors() {
AtomicReference<AttributeMap> clientContexParams = new AtomicReference<>();

ExecutionInterceptor paramsCaptor = new ExecutionInterceptor() {
@Override
public void beforeExecution(Context.BeforeExecution context, ExecutionAttributes executionAttributes) {
clientContexParams.set(executionAttributes.getAttribute(SdkInternalExecutionAttribute.CLIENT_CONTEXT_PARAMS));
throw new RuntimeException("BOOM");
}
};

DefaultS3CrtAsyncClient.DefaultS3CrtClientBuilder builder =
(DefaultS3CrtAsyncClient.DefaultS3CrtClientBuilder) S3CrtAsyncClient.builder();

builder.addExecutionInterceptor(paramsCaptor);

try (S3AsyncClient s3AsyncClient = builder.accelerate(false)
.forcePathStyle(true)
.build()) {

assertThatThrownBy(s3AsyncClient.listBuckets()::join).hasMessageContaining("BOOM");
AttributeMap attributeMap = clientContexParams.get();

assertThat(attributeMap.get(S3ClientContextParams.ACCELERATE)).isFalse();
assertThat(attributeMap.get(S3ClientContextParams.FORCE_PATH_STYLE)).isTrue();
assertThat(attributeMap.get(S3ClientContextParams.USE_ARN_REGION)).isFalse();
assertThat(attributeMap.get(S3ClientContextParams.DISABLE_MULTI_REGION_ACCESS_POINTS)).isFalse();
}
}

@ParameterizedTest
@ValueSource(longs = {0, -1L})
void invalidConfig_shouldThrowException(long value) {
Expand Down