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. 
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- 
4023import  java .io .UnsupportedEncodingException ;
41- import  java .net .InetSocketAddress ;
4224import  java .net .URLEncoder ;
4325import  java .nio .ByteBuffer ;
4426import  java .nio .charset .StandardCharsets ;
4527import  java .security .MessageDigest ;
4628import  java .security .NoSuchAlgorithmException ;
4729import  java .time .Instant ;
48- import  java .time .ZoneId ;
4930import  java .time .format .DateTimeFormatter ;
5031import  java .time .format .DateTimeFormatterBuilder ;
5132import  java .util .Arrays ;
5233import  java .util .Collections ;
5334import  java .util .List ;
5435import  java .util .concurrent .CompletableFuture ;
5536import  java .util .concurrent .CompletionStage ;
56- 
5737import  javax .crypto .Mac ;
5838import  javax .crypto .spec .SecretKeySpec ;
5939import  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 ,
0 commit comments