Skip to content

Commit b435f9c

Browse files
committed
Add Support for AWS Common Runtime Http Client
1 parent 393687d commit b435f9c

16 files changed

+1921
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"category": "AWS Common Runtime Http Client",
3+
"type": "feature",
4+
"description": "Add initial support for the AWS Common Runtime (CRT) Http Client."
5+
}

http-clients/aws-crt-client/pom.xml

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License").
6+
~ You may not use this file except in compliance with the License.
7+
~ A copy of the License is located at
8+
~
9+
~ http://aws.amazon.com/apache2.0
10+
~
11+
~ or in the "license" file accompanying this file. This file is distributed
12+
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
~ express or implied. See the License for the specific language governing
14+
~ permissions and limitations under the License.
15+
-->
16+
17+
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<parent>
22+
<artifactId>http-clients</artifactId>
23+
<groupId>software.amazon.awssdk</groupId>
24+
<version>2.7.10-SNAPSHOT</version>
25+
</parent>
26+
<modelVersion>4.0.0</modelVersion>
27+
28+
<artifactId>aws-crt-client</artifactId>
29+
<name>AWS Java SDK :: HTTP Clients :: AWS Common Runtime Client</name>
30+
31+
<dependencies>
32+
<!--AWS Common Runtime-->
33+
<dependency>
34+
<groupId>software.amazon.awssdk.crt</groupId>
35+
<artifactId>aws-crt</artifactId>
36+
<version>0.3.12</version>
37+
</dependency>
38+
39+
<!--SDK dependencies-->
40+
<dependency>
41+
<groupId>software.amazon.awssdk</groupId>
42+
<artifactId>annotations</artifactId>
43+
<version>${awsjavasdk.version}</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>software.amazon.awssdk</groupId>
47+
<artifactId>http-client-spi</artifactId>
48+
<version>${awsjavasdk.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>software.amazon.awssdk</groupId>
52+
<artifactId>utils</artifactId>
53+
<version>${awsjavasdk.version}</version>
54+
</dependency>
55+
56+
<!--Test Dependencies-->
57+
<dependency>
58+
<groupId>com.github.tomakehurst</groupId>
59+
<artifactId>wiremock</artifactId>
60+
<scope>test</scope>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.apache.commons</groupId>
64+
<artifactId>commons-lang3</artifactId>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>junit</groupId>
69+
<artifactId>junit</artifactId>
70+
<scope>test</scope>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.mockito</groupId>
74+
<artifactId>mockito-core</artifactId>
75+
<scope>test</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.assertj</groupId>
79+
<artifactId>assertj-core</artifactId>
80+
<scope>test</scope>
81+
</dependency>
82+
<dependency>
83+
<groupId>org.reactivestreams</groupId>
84+
<artifactId>reactive-streams-tck</artifactId>
85+
<scope>test</scope>
86+
</dependency>
87+
<dependency>
88+
<groupId>org.slf4j</groupId>
89+
<artifactId>slf4j-log4j12</artifactId>
90+
<scope>test</scope>
91+
</dependency>
92+
<dependency>
93+
<groupId>log4j</groupId>
94+
<artifactId>log4j</artifactId>
95+
<scope>test</scope>
96+
</dependency>
97+
<dependency>
98+
<groupId>software.amazon.awssdk</groupId>
99+
<artifactId>http-client-tests</artifactId>
100+
<version>${awsjavasdk.version}</version>
101+
<scope>test</scope>
102+
</dependency>
103+
<dependency>
104+
<groupId>software.amazon.awssdk</groupId>
105+
<artifactId>sdk-core</artifactId>
106+
<version>${awsjavasdk.version}</version>
107+
<scope>test</scope>
108+
</dependency>
109+
<dependency>
110+
<groupId>software.amazon.awssdk</groupId>
111+
<artifactId>regions</artifactId>
112+
<version>${awsjavasdk.version}</version>
113+
<scope>test</scope>
114+
</dependency>
115+
<dependency>
116+
<groupId>software.amazon.awssdk</groupId>
117+
<artifactId>s3</artifactId>
118+
<version>${awsjavasdk.version}</version>
119+
<scope>test</scope>
120+
</dependency>
121+
<dependency>
122+
<groupId>software.amazon.awssdk</groupId>
123+
<artifactId>kms</artifactId>
124+
<version>${awsjavasdk.version}</version>
125+
<scope>test</scope>
126+
</dependency>
127+
<dependency>
128+
<groupId>software.amazon.awssdk</groupId>
129+
<artifactId>auth</artifactId>
130+
<version>${awsjavasdk.version}</version>
131+
<scope>test</scope>
132+
</dependency>
133+
<dependency>
134+
<artifactId>service-test-utils</artifactId>
135+
<groupId>software.amazon.awssdk</groupId>
136+
<version>${awsjavasdk.version}</version>
137+
<scope>test</scope>
138+
</dependency>
139+
<dependency>
140+
<groupId>commons-codec</groupId>
141+
<artifactId>commons-codec</artifactId>
142+
<version>${commons-codec.verion}</version>
143+
<scope>test</scope>
144+
</dependency>
145+
</dependencies>
146+
147+
<build>
148+
<plugins>
149+
<!-- The Reactive Streams TCK tests are based on TestNG. See http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html#Running_TestNG_and_JUnit_Tests -->
150+
<plugin>
151+
<groupId>org.apache.maven.plugins</groupId>
152+
<artifactId>maven-surefire-plugin</artifactId>
153+
<version>${maven.surefire.version}</version>
154+
<configuration>
155+
<properties>
156+
<property>
157+
<name>junit</name>
158+
<value>false</value>
159+
</property>
160+
</properties>
161+
<threadCount>1</threadCount>
162+
</configuration>
163+
<dependencies>
164+
<dependency>
165+
<groupId>org.apache.maven.surefire</groupId>
166+
<artifactId>surefire-junit47</artifactId>
167+
<version>${maven.surefire.version}</version>
168+
</dependency>
169+
<dependency>
170+
<groupId>org.apache.maven.surefire</groupId>
171+
<artifactId>surefire-testng</artifactId>
172+
<version>${maven.surefire.version}</version>
173+
</dependency>
174+
</dependencies>
175+
</plugin>
176+
<plugin>
177+
<groupId>org.apache.maven.plugins</groupId>
178+
<artifactId>maven-jar-plugin</artifactId>
179+
<configuration>
180+
<archive>
181+
<manifestEntries>
182+
<Automatic-Module-Name>software.amazon.awssdk.http.crt</Automatic-Module-Name>
183+
</manifestEntries>
184+
</archive>
185+
</configuration>
186+
</plugin>
187+
</plugins>
188+
</build>
189+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package software.amazon.awssdk.http.crt;
2+
3+
import static software.amazon.awssdk.testutils.service.AwsTestBase.CREDENTIALS_PROVIDER_CHAIN;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.UUID;
8+
import org.junit.After;
9+
import org.junit.Assert;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
import software.amazon.awssdk.core.SdkBytes;
13+
import software.amazon.awssdk.crt.CrtResource;
14+
import software.amazon.awssdk.crt.io.ClientBootstrap;
15+
import software.amazon.awssdk.crt.io.SocketOptions;
16+
import software.amazon.awssdk.crt.io.TlsContext;
17+
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
18+
import software.amazon.awssdk.regions.Region;
19+
import software.amazon.awssdk.services.kms.KmsAsyncClient;
20+
import software.amazon.awssdk.services.kms.model.CreateAliasRequest;
21+
import software.amazon.awssdk.services.kms.model.CreateAliasResponse;
22+
import software.amazon.awssdk.services.kms.model.CreateKeyRequest;
23+
import software.amazon.awssdk.services.kms.model.CreateKeyResponse;
24+
import software.amazon.awssdk.services.kms.model.DecryptRequest;
25+
import software.amazon.awssdk.services.kms.model.DecryptResponse;
26+
import software.amazon.awssdk.services.kms.model.DescribeKeyRequest;
27+
import software.amazon.awssdk.services.kms.model.DescribeKeyResponse;
28+
import software.amazon.awssdk.services.kms.model.EncryptRequest;
29+
import software.amazon.awssdk.services.kms.model.EncryptResponse;
30+
31+
32+
public class AwsCrtClientKmsIntegrationTest {
33+
private static String KEY_ALIAS = "alias/aws-sdk-java-v2-integ-test";
34+
private static Region REGION = Region.US_EAST_1;
35+
private static SdkAsyncHttpClient client;
36+
private static KmsAsyncClient kms;
37+
38+
List<CrtResource> crtResources = new ArrayList<>();
39+
40+
private void addResource(CrtResource resource) {
41+
crtResources.add(resource);
42+
}
43+
44+
@Before
45+
public void setup() {
46+
Assert.assertEquals("Expected Zero allocated AwsCrtResources", 0, CrtResource.getAllocatedNativeResourceCount());
47+
48+
ClientBootstrap bootstrap = new ClientBootstrap(1);
49+
SocketOptions socketOptions = new SocketOptions();
50+
TlsContext tlsContext = new TlsContext();
51+
52+
addResource(bootstrap);
53+
addResource(socketOptions);
54+
addResource(tlsContext);
55+
56+
client = AwsCrtAsyncHttpClient.builder()
57+
.bootstrap(bootstrap)
58+
.socketOptions(socketOptions)
59+
.tlsContext(tlsContext)
60+
.build();
61+
62+
kms = KmsAsyncClient.builder()
63+
.region(REGION)
64+
.httpClient(client)
65+
.credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
66+
.build();
67+
}
68+
69+
@After
70+
public void tearDown() {
71+
kms.close();
72+
client.close();
73+
74+
for (CrtResource r: crtResources) {
75+
r.close();
76+
}
77+
78+
Assert.assertEquals("Expected Zero allocated AwsCrtResources", 0, CrtResource.getAllocatedNativeResourceCount());
79+
}
80+
81+
private boolean doesKeyExist(String keyAlias) {
82+
try {
83+
DescribeKeyRequest req = DescribeKeyRequest.builder().keyId(keyAlias).build();
84+
DescribeKeyResponse resp = kms.describeKey(req).get();
85+
Assert.assertEquals(200, resp.sdkHttpResponse().statusCode());
86+
return resp.sdkHttpResponse().isSuccessful();
87+
} catch (Exception e) {
88+
return false;
89+
}
90+
}
91+
92+
private void createKeyAlias(String keyId, String keyAlias) throws Exception {
93+
CreateAliasRequest req = CreateAliasRequest.builder().aliasName(keyAlias).targetKeyId(keyId).build();
94+
CreateAliasResponse resp = kms.createAlias(req).get();
95+
Assert.assertEquals(200, resp.sdkHttpResponse().statusCode());
96+
}
97+
98+
private String createKey() throws Exception {
99+
CreateKeyRequest req = CreateKeyRequest.builder().build();
100+
CreateKeyResponse resp = kms.createKey(req).get();
101+
Assert.assertEquals(200, resp.sdkHttpResponse().statusCode());
102+
return resp.keyMetadata().keyId();
103+
}
104+
105+
private void createKeyIfNotExists(String keyAlias) throws Exception {
106+
if (!doesKeyExist(keyAlias)) {
107+
String keyId = createKey();
108+
createKeyAlias(keyId, KEY_ALIAS);
109+
}
110+
}
111+
112+
private SdkBytes encrypt(String keyId, String plaintext) throws Exception {
113+
SdkBytes bytes = SdkBytes.fromUtf8String(plaintext);
114+
EncryptRequest req = EncryptRequest.builder().keyId(keyId).plaintext(bytes).build();
115+
EncryptResponse resp = kms.encrypt(req).get();
116+
Assert.assertEquals(200, resp.sdkHttpResponse().statusCode());
117+
return resp.ciphertextBlob();
118+
}
119+
120+
private String decrypt(SdkBytes ciphertext) throws Exception {
121+
DecryptRequest req = DecryptRequest.builder().ciphertextBlob(ciphertext).build();
122+
DecryptResponse resp = kms.decrypt(req).get();
123+
Assert.assertEquals(200, resp.sdkHttpResponse().statusCode());
124+
return resp.plaintext().asUtf8String();
125+
}
126+
127+
@Test
128+
public void testEncryptDecryptWithKms() throws Exception {
129+
createKeyIfNotExists(KEY_ALIAS);
130+
Assert.assertTrue(doesKeyExist(KEY_ALIAS));
131+
Assert.assertFalse(doesKeyExist("alias/does-not-exist-" + UUID.randomUUID()));
132+
133+
String secret = UUID.randomUUID().toString();
134+
SdkBytes cipherText = encrypt(KEY_ALIAS, secret);
135+
String plainText = decrypt(cipherText);
136+
137+
Assert.assertEquals(plainText, secret);
138+
}
139+
}

0 commit comments

Comments
 (0)