Skip to content

Commit 0102460

Browse files
committed
Add support for adapting the sdk configurations to crt configurations.
1 parent 9a8d918 commit 0102460

File tree

4 files changed

+131
-13
lines changed

4 files changed

+131
-13
lines changed

services/s3/src/main/java/software/amazon/awssdk/services/s3/internal/DefaultS3CrtAsyncClient.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,29 @@
1616
package software.amazon.awssdk.services.s3.internal;
1717

1818

19+
import static software.amazon.awssdk.services.s3.internal.S3CrtUtils.createCrtCredentialsProvider;
20+
1921
import com.amazonaws.s3.S3NativeClient;
2022
import software.amazon.awssdk.annotations.SdkInternalApi;
23+
import software.amazon.awssdk.crt.auth.credentials.CredentialsProvider;
2124
import software.amazon.awssdk.services.s3.S3CrtAsyncClient;
2225

2326
@SdkInternalApi
2427
public final class DefaultS3CrtAsyncClient implements S3CrtAsyncClient {
2528
private final S3NativeClient s3NativeClient;
2629
private final S3NativeClientConfiguration configuration;
2730

31+
private final CredentialsProvider credentialsProvider;
32+
33+
2834
public DefaultS3CrtAsyncClient(DefaultS3CrtClientBuilder builder) {
35+
this.credentialsProvider = createCrtCredentialsProvider(builder.credentialsProvider());
2936

30-
// TODO: adapt SDK Configurations to CRT configurations
3137
this.configuration = S3NativeClientConfiguration.builder()
38+
.credentialsProvider(credentialsProvider)
3239
.signingRegion(builder.region().id())
40+
.partSizeBytes(builder.partSizeBytes())
41+
.maxThroughputGbps(builder.maxThroughputGbps())
3342
.build();
3443

3544
this.s3NativeClient = new S3NativeClient(configuration.signingRegion(),
@@ -39,6 +48,8 @@ public DefaultS3CrtAsyncClient(DefaultS3CrtClientBuilder builder) {
3948
configuration.maxThroughputGbps());
4049
}
4150

51+
52+
4253
@Override
4354
public String serviceName() {
4455
return "s3";

services/s3/src/main/java/software/amazon/awssdk/services/s3/internal/DefaultS3CrtClientBuilder.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
public final class DefaultS3CrtClientBuilder implements S3CrtAsyncClientBuilder {
2929
private AwsCredentialsProvider credentialsProvider;
3030
private Region region;
31-
private ClientOverrideConfiguration clientOverrideConfiguration;
32-
private URI endpointOverride;
31+
private long partSizeBytes;
32+
private double maxThroughputGbps;
3333

3434
public AwsCredentialsProvider credentialsProvider() {
3535
return credentialsProvider;
@@ -39,12 +39,12 @@ public Region region() {
3939
return region;
4040
}
4141

42-
public ClientOverrideConfiguration clientOverrideConfiguration() {
43-
return clientOverrideConfiguration;
42+
public long partSizeBytes() {
43+
return partSizeBytes;
4444
}
4545

46-
public URI endpointOverride() {
47-
return endpointOverride;
46+
public double maxThroughputGbps() {
47+
return maxThroughputGbps;
4848
}
4949

5050
@Override
@@ -53,27 +53,37 @@ public S3CrtAsyncClientBuilder credentialsProvider(AwsCredentialsProvider creden
5353
return this;
5454
}
5555

56-
@Override
56+
5757
public S3CrtAsyncClientBuilder region(Region region) {
5858
this.region = region;
5959
return this;
6060
}
6161

62+
// TODO: Add support for this configuration
6263
@Override
6364
public S3CrtAsyncClientBuilder overrideConfiguration(ClientOverrideConfiguration overrideConfiguration) {
64-
this.clientOverrideConfiguration = overrideConfiguration;
65-
return this;
65+
throw new UnsupportedOperationException();
6666
}
6767

68+
// TODO: Add support for this configuration
6869
@Override
6970
public S3CrtAsyncClientBuilder overrideConfiguration(Consumer<ClientOverrideConfiguration.Builder> overrideConfiguration) {
70-
this.clientOverrideConfiguration = ClientOverrideConfiguration.builder().applyMutation(overrideConfiguration).build();
71-
return this;
71+
throw new UnsupportedOperationException();
7272
}
7373

74+
// TODO: Add support for this configuration
7475
@Override
7576
public S3CrtAsyncClientBuilder endpointOverride(URI endpointOverride) {
76-
this.endpointOverride = endpointOverride;
77+
throw new UnsupportedOperationException();
78+
}
79+
80+
public S3CrtAsyncClientBuilder partSizeBytes(long partSizeBytes) {
81+
this.partSizeBytes = partSizeBytes;
82+
return this;
83+
}
84+
85+
public S3CrtAsyncClientBuilder maxThroughputGbps(double maxThroughputGbps) {
86+
this.maxThroughputGbps = maxThroughputGbps;
7787
return this;
7888
}
7989

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.s3.internal;
17+
18+
import software.amazon.awssdk.annotations.SdkInternalApi;
19+
import software.amazon.awssdk.auth.credentials.AwsCredentials;
20+
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
21+
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
22+
import software.amazon.awssdk.crt.auth.credentials.CredentialsProvider;
23+
import software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider;
24+
25+
@SdkInternalApi
26+
public final class S3CrtUtils {
27+
28+
private S3CrtUtils() {
29+
}
30+
31+
// TODO: Add more adapters if there are any new crt credentials providers.
32+
/**
33+
* Adapter between the sdk credentials provider and the crt credentials provider.
34+
*/
35+
public static CredentialsProvider createCrtCredentialsProvider(AwsCredentialsProvider awsCredentialsProvider) {
36+
AwsCredentials sdkCredentials = awsCredentialsProvider.resolveCredentials();
37+
StaticCredentialsProvider.StaticCredentialsProviderBuilder builder =
38+
new StaticCredentialsProvider.StaticCredentialsProviderBuilder();
39+
40+
if (sdkCredentials instanceof AwsSessionCredentials) {
41+
builder.withSessionToken(((AwsSessionCredentials) sdkCredentials).sessionToken().getBytes());
42+
}
43+
44+
return builder.withAccessKeyId(sdkCredentials.accessKeyId().getBytes())
45+
.withSecretAccessKey(sdkCredentials.secretAccessKey().getBytes())
46+
.build();
47+
}
48+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.s3.internal;
17+
18+
import static org.hamcrest.Matchers.equalTo;
19+
import static org.junit.Assert.assertThat;
20+
21+
import java.nio.charset.StandardCharsets;
22+
import java.util.concurrent.ExecutionException;
23+
import org.junit.Test;
24+
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
25+
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
26+
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
27+
import software.amazon.awssdk.crt.auth.credentials.Credentials;
28+
import software.amazon.awssdk.crt.auth.credentials.CredentialsProvider;
29+
30+
public class S3CrtUtilsTest {
31+
32+
private final String ACCESS_KEY = "accessKey";
33+
private final String SECRET_ACCESS_KEY = "secretAccessKey";
34+
private final String SESSION_TOKEN = "sessionToken";
35+
36+
@Test
37+
public void createCrtCredentialsProviderTest() throws ExecutionException, InterruptedException {
38+
AwsCredentialsProvider awsCredentialsProvider = StaticCredentialsProvider
39+
.create(AwsSessionCredentials.create(ACCESS_KEY, SECRET_ACCESS_KEY, SESSION_TOKEN));
40+
CredentialsProvider crtCredentialsProvider = S3CrtUtils.createCrtCredentialsProvider(awsCredentialsProvider);
41+
42+
Credentials credentials = crtCredentialsProvider.getCredentials().get();
43+
44+
assertThat(ACCESS_KEY, equalTo(new String(credentials.getAccessKeyId(), StandardCharsets.UTF_8)));
45+
assertThat(SECRET_ACCESS_KEY, equalTo(new String(credentials.getSecretAccessKey(), StandardCharsets.UTF_8)));
46+
assertThat(SESSION_TOKEN, equalTo(new String(credentials.getSessionToken(), StandardCharsets.UTF_8)));
47+
}
48+
49+
}

0 commit comments

Comments
 (0)