Skip to content

Commit fdabb69

Browse files
authored
Merge pull request #19 from bhouse99/brianh-aws-sdk-2-mk1
Updated AWS SDK dependency from 1.x to SDK 2.15
2 parents 8198c03 + f897d7a commit fdabb69

File tree

6 files changed

+65
-62
lines changed

6 files changed

+65
-62
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## [4.0.4] - 2021-01-20
4+
5+
Updated to AWS 2 SDK.
6+
7+
38
## [4.0.3] - 2020-05-15
49

510
Updated the plugin to fix newlines used in the SigV4 signature on Windows. This resolves [Issue

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# IMPORTANT: Latest Version
22

3-
The current version is 4.0.3. Please see the [changelog](./CHANGELOG.md) for details on version history.
3+
The current version is 4.0.4. Please see the [changelog](./CHANGELOG.md) for details on version history.
44

55
# What
66

@@ -66,7 +66,7 @@ The authentication plugin supports version 4.x of the DataStax Java Driver for C
6666
<dependency>
6767
<groupId>software.aws.mcs</groupId>
6868
<artifactId>aws-sigv4-auth-cassandra-java-driver-plugin</artifactId>
69-
<version>4.0.2</version>
69+
<version>4.0.4</version>
7070
</dependency>
7171
```
7272

pom.xml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>software.aws.mcs</groupId>
55
<artifactId>aws-sigv4-auth-cassandra-java-driver-plugin</artifactId>
6-
<version>4.0.3</version>
6+
<version>4.0.4</version>
77
<name>AWS SigV4 Auth Java Driver 4.x Plugin</name>
88
<description>A Plugin to allow SigV4 authentication for Java Cassandra drivers with Amazon MCS</description>
99
<url>https://github.com/aws/aws-sigv4-auth-cassandra-java-driver-plugin</url>
@@ -46,9 +46,9 @@
4646

4747
<dependencies>
4848
<dependency>
49-
<groupId>com.amazonaws</groupId>
50-
<artifactId>aws-java-sdk-core</artifactId>
51-
<version>1.11.717</version>
49+
<groupId>commons-codec</groupId>
50+
<artifactId>commons-codec</artifactId>
51+
<version>1.15</version>
5252
</dependency>
5353
<dependency>
5454
<groupId>com.datastax.oss</groupId>
@@ -67,6 +67,11 @@
6767
<version>5.6.0</version>
6868
<scope>test</scope>
6969
</dependency>
70+
<dependency>
71+
<groupId>software.amazon.awssdk</groupId>
72+
<artifactId>auth</artifactId>
73+
<version>2.15.66</version>
74+
</dependency>
7075
</dependencies>
7176

7277
<build>

src/main/java/software/aws/mcs/auth/SigV4AuthProvider.java

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* #%L
55
* AWS SigV4 Auth Java Driver 4.x Plugin
66
* %%
7-
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
7+
* Copyright (C) 2020-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
88
* %%
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
@@ -20,44 +20,42 @@
2020
* #L%
2121
*/
2222

23-
import com.amazonaws.SDKGlobalConfiguration;
24-
import com.amazonaws.auth.AWSCredentials;
25-
import com.amazonaws.auth.AWSCredentialsProvider;
26-
import com.amazonaws.auth.AWSSessionCredentials;
27-
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
28-
import com.amazonaws.auth.internal.AWS4SignerUtils;
29-
import com.amazonaws.auth.internal.SignerConstants;
30-
31-
import com.datastax.oss.driver.api.core.auth.AuthenticationException;
32-
import com.datastax.oss.driver.api.core.auth.Authenticator;
33-
import com.datastax.oss.driver.api.core.auth.AuthProvider;
34-
import com.datastax.oss.driver.api.core.config.DriverOption;
35-
import com.datastax.oss.driver.api.core.context.DriverContext;
36-
import com.datastax.oss.driver.api.core.metadata.EndPoint;
37-
38-
import org.apache.commons.codec.binary.Hex;
39-
4023
import java.io.UnsupportedEncodingException;
41-
import java.net.InetSocketAddress;
4224
import java.net.URLEncoder;
4325
import java.nio.ByteBuffer;
4426
import java.nio.charset.StandardCharsets;
4527
import java.security.MessageDigest;
4628
import java.security.NoSuchAlgorithmException;
4729
import java.time.Instant;
48-
import java.time.ZoneId;
4930
import java.time.format.DateTimeFormatter;
5031
import java.time.format.DateTimeFormatterBuilder;
5132
import java.util.Arrays;
5233
import java.util.Collections;
5334
import java.util.List;
5435
import java.util.concurrent.CompletableFuture;
5536
import java.util.concurrent.CompletionStage;
56-
5737
import javax.crypto.Mac;
5838
import javax.crypto.spec.SecretKeySpec;
5939
import javax.validation.constraints.NotNull;
6040

41+
import org.apache.commons.codec.binary.Hex;
42+
43+
import com.datastax.oss.driver.api.core.auth.AuthProvider;
44+
import com.datastax.oss.driver.api.core.auth.AuthenticationException;
45+
import com.datastax.oss.driver.api.core.auth.Authenticator;
46+
import com.datastax.oss.driver.api.core.config.DriverOption;
47+
import com.datastax.oss.driver.api.core.context.DriverContext;
48+
import com.datastax.oss.driver.api.core.metadata.EndPoint;
49+
import software.amazon.awssdk.auth.credentials.AwsCredentials;
50+
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
51+
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
52+
import software.amazon.awssdk.auth.signer.internal.Aws4SignerUtils;
53+
import software.amazon.awssdk.auth.signer.internal.SignerConstant;
54+
import software.amazon.awssdk.regions.Region;
55+
import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain;
56+
57+
import static software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider.create;
58+
6159
/**
6260
* This auth provider can be used with the Amazon MCS service to
6361
* authenticate with SigV4. It uses the AWSCredentialsProvider
@@ -89,7 +87,7 @@ public class SigV4AuthProvider implements AuthProvider {
8987
// These are static values because we don't need HTTP, but SigV4 assumes some amount of HTTP metadata
9088
private static final String CANONICAL_SERVICE = "cassandra";
9189

92-
private final AWSCredentialsProvider credentialsProvider;
90+
private final AwsCredentialsProvider credentialsProvider;
9391
private final String signingRegion;
9492

9593
/**
@@ -99,7 +97,7 @@ public class SigV4AuthProvider implements AuthProvider {
9997
* environment variable or the "aws.region" system property.
10098
*/
10199
public SigV4AuthProvider() {
102-
this(DefaultAWSCredentialsProviderChain.getInstance(), null);
100+
this(create(), null);
103101
}
104102

105103
private final static DriverOption REGION_OPTION = new DriverOption() {
@@ -126,7 +124,7 @@ public String getPath() {
126124
* preference.
127125
*
128126
* For programmatic construction, use {@link #SigV4AuthProvider()}
129-
* or {@link #SigV4AuthProvider(AWSCredentialsProvider, String)}.
127+
* or {@link #SigV4AuthProvider(AwsCredentialsProvider, String)}.
130128
*
131129
* @param driverContext the driver context for instance creation.
132130
* Unused for this plugin.
@@ -142,7 +140,7 @@ public SigV4AuthProvider(DriverContext driverContext) {
142140
* variable, or the "aws.region" system property to configure it.
143141
*/
144142
public SigV4AuthProvider(final String region) {
145-
this(DefaultAWSCredentialsProviderChain.getInstance(), region);
143+
this(create(), region);
146144
}
147145

148146
/**
@@ -152,15 +150,14 @@ public SigV4AuthProvider(final String region) {
152150
* null value indicates to use the AWS_REGION environment
153151
* variable, or the "aws.region" system property to configure it.
154152
*/
155-
public SigV4AuthProvider(@NotNull AWSCredentialsProvider credentialsProvider, final String region) {
153+
public SigV4AuthProvider(@NotNull AwsCredentialsProvider credentialsProvider, final String region) {
156154
this.credentialsProvider = credentialsProvider;
157155

158156
if (region == null) {
159-
if (System.getProperty(SDKGlobalConfiguration.AWS_REGION_SYSTEM_PROPERTY) != null) {
160-
this.signingRegion = System.getProperty(SDKGlobalConfiguration.AWS_REGION_SYSTEM_PROPERTY);
161-
} else {
162-
this.signingRegion = System.getenv(SDKGlobalConfiguration.AWS_REGION_ENV_VAR);
163-
}
157+
DefaultAwsRegionProviderChain chain = new DefaultAwsRegionProviderChain();
158+
Region defaultRegion = chain.getRegion();
159+
this.signingRegion = defaultRegion.toString();
160+
164161
} else {
165162
this.signingRegion = region;
166163
}
@@ -170,7 +167,6 @@ public SigV4AuthProvider(@NotNull AWSCredentialsProvider credentialsProvider, fi
170167
"A region must be specified by constructor, AWS_REGION env variable, or aws.region system property"
171168
);
172169
}
173-
174170
}
175171

176172
@Override
@@ -204,19 +200,18 @@ public CompletionStage<ByteBuffer> evaluateChallenge(ByteBuffer challenge) {
204200
byte[] nonce = extractNonce(challenge);
205201

206202
Instant requestTimestamp = Instant.now();
207-
208-
AWSCredentials credentials = credentialsProvider.getCredentials();
203+
AwsCredentials credentials = credentialsProvider.resolveCredentials();
209204

210205
String signature = generateSignature(nonce, requestTimestamp, credentials);
211206

212207
String response =
213208
String.format("signature=%s,access_key=%s,amzdate=%s",
214209
signature,
215-
credentials.getAWSAccessKeyId(),
210+
credentials.accessKeyId(),
216211
timestampFormatter.format(requestTimestamp));
217212

218-
if (credentials instanceof AWSSessionCredentials) {
219-
response = response + ",session_token=" + ((AWSSessionCredentials)credentials).getSessionToken();
213+
if (credentials instanceof AwsSessionCredentials) {
214+
response = response + ",session_token=" + ((AwsSessionCredentials)credentials).sessionToken();
220215
}
221216

222217
return CompletableFuture.completedFuture(ByteBuffer.wrap(response.getBytes(StandardCharsets.UTF_8)));
@@ -266,22 +261,22 @@ static byte[] extractNonce(ByteBuffer challengeBuffer) {
266261
return Arrays.copyOfRange(challenge, nonceStart, nonceEnd);
267262
}
268263

269-
private String generateSignature(byte[] nonce, Instant requestTimestamp, AWSCredentials credentials) throws UnsupportedEncodingException {
270-
String credentialScopeDate = AWS4SignerUtils.formatDateStamp(requestTimestamp.toEpochMilli());
264+
private String generateSignature(byte[] nonce, Instant requestTimestamp, AwsCredentials credentials) throws UnsupportedEncodingException {
265+
String credentialScopeDate = Aws4SignerUtils.formatDateStamp(requestTimestamp.toEpochMilli());
271266

272267
String signingScope = String.format("%s/%s/%s/aws4_request", credentialScopeDate, signingRegion, CANONICAL_SERVICE);
273268

274269
String nonceHash = sha256Digest(nonce);
275270

276-
String canonicalRequest = canonicalizeRequest(credentials.getAWSAccessKeyId(), signingScope, requestTimestamp, nonceHash);
271+
String canonicalRequest = canonicalizeRequest(credentials.accessKeyId(), signingScope, requestTimestamp, nonceHash);
277272

278273
String stringToSign = String.format("%s\n%s\n%s\n%s",
279-
SignerConstants.AWS4_SIGNING_ALGORITHM,
274+
SignerConstant.AWS4_SIGNING_ALGORITHM,
280275
timestampFormatter.format(requestTimestamp),
281276
signingScope,
282277
sha256Digest(canonicalRequest));
283278

284-
byte[] signingKey = getSignatureKey(credentials.getAWSSecretKey(),
279+
byte[] signingKey = getSignatureKey(credentials.secretAccessKey(),
285280
credentialScopeDate,
286281
signingRegion,
287282
CANONICAL_SERVICE);
@@ -291,7 +286,7 @@ private String generateSignature(byte[] nonce, Instant requestTimestamp, AWSCred
291286
return Hex.encodeHexString(signature, true);
292287
}
293288

294-
private static final String AMZ_ALGO_HEADER = "X-Amz-Algorithm=" + SignerConstants.AWS4_SIGNING_ALGORITHM;
289+
private static final String AMZ_ALGO_HEADER = "X-Amz-Algorithm=" + SignerConstant.AWS4_SIGNING_ALGORITHM;
295290
private static final String AMZ_EXPIRES_HEADER = "X-Amz-Expires=900";
296291

297292
private static String canonicalizeRequest(String accessKey,

src/test/java/software/aws/mcs/auth/SigV4AuthProviderTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* #%L
55
* AWS SigV4 Auth Java Driver 4.x Plugin
66
* %%
7-
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
7+
* Copyright (C) 2020-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
88
* %%
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
@@ -20,14 +20,13 @@
2020
* #L%
2121
*/
2222

23-
import static org.junit.jupiter.api.Assertions.assertEquals;
24-
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
import java.nio.ByteBuffer;
24+
import java.nio.charset.StandardCharsets;
2525

2626
import org.junit.jupiter.api.Test;
2727

28-
import java.nio.ByteBuffer;
29-
import java.nio.charset.StandardCharsets;
30-
import java.util.Arrays;
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.assertThrows;
3130

3231
public class SigV4AuthProviderTest {
3332
@Test

src/test/java/software/aws/mcs/auth/TestSigV4.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* #%L
55
* AWS SigV4 Auth Java Driver 4.x Plugin
66
* %%
7-
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
7+
* Copyright (C) 2020-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
88
* %%
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
@@ -20,15 +20,14 @@
2020
* #L%
2121
*/
2222

23-
import com.datastax.oss.driver.api.core.CqlSession;
24-
import com.datastax.oss.driver.api.core.cql.*;
25-
26-
import software.aws.mcs.auth.SigV4AuthProvider;
27-
2823
import java.net.InetSocketAddress;
2924
import java.util.ArrayList;
3025
import javax.net.ssl.SSLContext;
3126

27+
import com.datastax.oss.driver.api.core.CqlSession;
28+
import com.datastax.oss.driver.api.core.cql.ResultSet;
29+
import com.datastax.oss.driver.api.core.cql.Row;
30+
3231
public class TestSigV4 {
3332
static String[] DEFAULT_CONTACT_POINTS = {"127.0.0.1:9042"};
3433

@@ -59,7 +58,7 @@ public static void main(String[] args) throws Exception {
5958
.addContactPoints(contactPoints)
6059
.withAuthProvider(new SigV4AuthProvider())
6160
.withSslContext(SSLContext.getDefault())
62-
.withLocalDatacenter("dc1")
61+
.withLocalDatacenter("us-west-2")
6362
.build()) {
6463

6564
// We use execute to send a query to Cassandra. This returns a ResultSet, which is essentially a collection

0 commit comments

Comments
 (0)