Skip to content

Commit 4d2a79c

Browse files
authored
Merge pull request #7 from dchenbec/bugfix-signature-mismatch-jdk11
Fix SigV4 timestamp formatting to match SigV4 Spec
2 parents 3bdb1e9 + bfbed56 commit 4d2a79c

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

CHANGELOG.md

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

3+
## [4.0.2] - 2020-03-31
4+
5+
Changed the plugin to use a `DateTimeFormatter` to ensure precisely 3 digits of millisecond precision for the SigV4
6+
timestamp. A change between JDK8 and JDK11 caused the output of `java.time.Instant.toString()` to change from 3 to 6
7+
digits, which results in a signature mismatch.
8+
39
## [4.0.1] - 2020-03-31
410

511
No changes to code, but we are re-publishing the 4.0.0 release compiled with Java 8 to fix [Issue

pom.xml

Lines changed: 1 addition & 1 deletion
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.1</version>
6+
<version>4.0.2</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>

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
import java.security.NoSuchAlgorithmException;
4747
import java.time.Instant;
4848
import java.time.ZoneId;
49+
import java.time.format.DateTimeFormatter;
50+
import java.time.format.DateTimeFormatterBuilder;
4951
import java.util.Arrays;
5052
import java.util.Collections;
5153
import java.util.List;
@@ -76,6 +78,11 @@ public class SigV4AuthProvider implements AuthProvider {
7678
SIGV4_INITIAL_RESPONSE = initialResponse.asReadOnlyBuffer();
7779
}
7880

81+
private static final int AWS_FRACTIONAL_TIMESTAMP_DIGITS = 3; // SigV4 expects three digits of nanoseconds for timestamps
82+
private static final DateTimeFormatter timestampFormatter =
83+
(new DateTimeFormatterBuilder()).appendInstant(AWS_FRACTIONAL_TIMESTAMP_DIGITS).toFormatter();
84+
85+
7986
private static final byte[] NONCE_KEY = "nonce=".getBytes(StandardCharsets.UTF_8);
8087
private static final int EXPECTED_NONCE_LENGTH = 32;
8188

@@ -206,7 +213,7 @@ public CompletionStage<ByteBuffer> evaluateChallenge(ByteBuffer challenge) {
206213
String.format("signature=%s,access_key=%s,amzdate=%s",
207214
signature,
208215
credentials.getAWSAccessKeyId(),
209-
requestTimestamp);
216+
timestampFormatter.format(requestTimestamp));
210217

211218
if (credentials instanceof AWSSessionCredentials) {
212219
response = response + ",session_token=" + ((AWSSessionCredentials)credentials).getSessionToken();
@@ -270,7 +277,7 @@ private String generateSignature(byte[] nonce, Instant requestTimestamp, AWSCred
270277

271278
String stringToSign = String.format("%s%n%s%n%s%n%s",
272279
SignerConstants.AWS4_SIGNING_ALGORITHM,
273-
requestTimestamp,
280+
timestampFormatter.format(requestTimestamp),
274281
signingScope,
275282
sha256Digest(canonicalRequest));
276283

@@ -297,7 +304,7 @@ private static String canonicalizeRequest(String accessKey,
297304
String.format("X-Amz-Credential=%s%%2F%s",
298305
accessKey,
299306
URLEncoder.encode(signingScope, StandardCharsets.UTF_8.name())),
300-
"X-Amz-Date=" + URLEncoder.encode(requestTimestamp.toString(), StandardCharsets.UTF_8.name()),
307+
"X-Amz-Date=" + URLEncoder.encode(timestampFormatter.format(requestTimestamp), StandardCharsets.UTF_8.name()),
301308
AMZ_EXPIRES_HEADER
302309
);
303310

0 commit comments

Comments
 (0)