Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

refactor(s3): point PreSignedUrlFilter at the shared SigV4RequestValidator crypto helpers (#3596)

Priamo Germosenbd13c7f9ce29
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/auth/SigV4RequestValidator.java b/src/main/java/io/github/hectorvent/floci/core/common/auth/SigV4RequestValidator.java
index e8c01d38a5..4608551daf 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/auth/SigV4RequestValidator.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/auth/SigV4RequestValidator.java
@@ -184,8 +184,13 @@ public final class SigV4RequestValidator {
return value == null ? null : value.replaceAll("\\p{Cntrl}", "");
}
- private static byte[] deriveSigningKey(String secretKey, String date, String region,
- String service) throws Exception {
+ /**
+ * Public so other SigV4 verifiers with a different canonical-request shape (e.g. S3's real
+ * REST request signing in {@code PreSignedUrlFilter}) can reuse the crypto primitives below
+ * without their own copy, even where they can't reuse {@link #validate}'s query-token flow.
+ */
+ public static byte[] deriveSigningKey(String secretKey, String date, String region,
+ String service) throws Exception {
byte[] kSecret = ("AWS4" + secretKey).getBytes(StandardCharsets.UTF_8);
byte[] kDate = hmacSha256(kSecret, date);
byte[] kRegion = hmacSha256(kDate, region);
@@ -193,18 +198,18 @@ public final class SigV4RequestValidator {
return hmacSha256(kService, "aws4_request");
}
- private static byte[] hmacSha256(byte[] key, String data) throws Exception {
+ public static byte[] hmacSha256(byte[] key, String data) throws Exception {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(key, "HmacSHA256"));
return mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
}
- private static String sha256Hex(String input) throws Exception {
+ public static String sha256Hex(String input) throws Exception {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
return hexEncode(digest.digest(input.getBytes(StandardCharsets.UTF_8)));
}
- private static String hexEncode(byte[] bytes) {
+ public static String hexEncode(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (byte b : bytes) {
sb.append(String.format("%02x", b));
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/PreSignedUrlFilter.java b/src/main/java/io/github/hectorvent/floci/services/s3/PreSignedUrlFilter.java
index 8d509bc042..1205da5893 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/PreSignedUrlFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/PreSignedUrlFilter.java
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.core.common.XmlBuilder;
+import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
import io.github.hectorvent.floci.services.iam.IamService;
import jakarta.inject.Inject;
import jakarta.ws.rs.container.ContainerRequestContext;
@@ -11,8 +12,6 @@ import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.Provider;
import org.jboss.logging.Logger;
-import javax.crypto.Mac;
-import javax.crypto.spec.SecretKeySpec;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
@@ -200,11 +199,12 @@ public class PreSignedUrlFilter implements ContainerRequestFilter {
String stringToSign = "AWS4-HMAC-SHA256\n"
+ amzDate + "\n"
+ credentialScope + "\n"
- + sha256Hex(canonicalRequest);
+ + SigV4RequestValidator.sha256Hex(canonicalRequest);
// Derive signing key and compute expected signature
- byte[] signingKey = deriveSigningKey(secretKey, date, region, service);
- String expectedSignature = hexEncode(hmacSha256(signingKey, stringToSign));
+ byte[] signingKey = SigV4RequestValidator.deriveSigningKey(secretKey, date, region, service);
+ String expectedSignature = SigV4RequestValidator.hexEncode(
+ SigV4RequestValidator.hmacSha256(signingKey, stringToSign));
return MessageDigest.isEqual(
expectedSignature.getBytes(StandardCharsets.UTF_8),
@@ -286,34 +286,6 @@ public class PreSignedUrlFilter implements ContainerRequestFilter {
return encoded.toString();
}
- static byte[] deriveSigningKey(String secretKey, String date, String region,
- String service) throws Exception {
- byte[] kSecret = ("AWS4" + secretKey).getBytes(StandardCharsets.UTF_8);
- byte[] kDate = hmacSha256(kSecret, date);
- byte[] kRegion = hmacSha256(kDate, region);
- byte[] kService = hmacSha256(kRegion, service);
- return hmacSha256(kService, "aws4_request");
- }
-
- static byte[] hmacSha256(byte[] key, String data) throws Exception {
- Mac mac = Mac.getInstance("HmacSHA256");
- mac.init(new SecretKeySpec(key, "HmacSHA256"));
- return mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
- }
-
- static String sha256Hex(String input) throws Exception {
- MessageDigest digest = MessageDigest.getInstance("SHA-256");
- return hexEncode(digest.digest(input.getBytes(StandardCharsets.UTF_8)));
- }
-
- static String hexEncode(byte[] bytes) {
- StringBuilder sb = new StringBuilder(bytes.length * 2);
- for (byte b : bytes) {
- sb.append(String.format("%02x", b));
- }
- return sb.toString();
- }
-
static Response errorResponse(int status, String code, String message) {
String xml = new XmlBuilder()
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3HeaderSignatureFilter.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3HeaderSignatureFilter.java
index 7846273347..bfab61a7d0 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3HeaderSignatureFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3HeaderSignatureFilter.java
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.s3;
+import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
import io.github.hectorvent.floci.services.iam.IamService;
import jakarta.annotation.Priority;
import jakarta.inject.Inject;
@@ -202,9 +203,9 @@ public class S3HeaderSignatureFilter implements ContainerRequestFilter {
String stringToSign = ALGORITHM + "\n"
+ amzDate + "\n"
+ scopeDate + "/" + region + "/" + SIGNING_SERVICE + "/" + TERMINATOR + "\n"
- + PreSignedUrlFilter.sha256Hex(canonicalRequest);
- byte[] signingKey = PreSignedUrlFilter.deriveSigningKey(secretKey, scopeDate, region, SIGNING_SERVICE);
- String expected = PreSignedUrlFilter.hexEncode(PreSignedUrlFilter.hmacSha256(signingKey, stringToSign));
+ + SigV4RequestValidator.sha256Hex(canonicalRequest);
+ byte[] signingKey = SigV4RequestValidator.deriveSigningKey(secretKey, scopeDate, region, SIGNING_SERVICE);
+ String expected = SigV4RequestValidator.hexEncode(SigV4RequestValidator.hmacSha256(signingKey, stringToSign));
return MessageDigest.isEqual(
expected.getBytes(StandardCharsets.UTF_8),
signature.getBytes(StandardCharsets.UTF_8));
@@ -221,7 +222,7 @@ public class S3HeaderSignatureFilter implements ContainerRequestFilter {
byte[] body = ctx.getEntityStream() != null ? ctx.getEntityStream().readAllBytes() : new byte[0];
ctx.setEntityStream(new ByteArrayInputStream(body));
try {
- String actual = PreSignedUrlFilter.hexEncode(MessageDigest.getInstance("SHA-256").digest(body));
+ String actual = SigV4RequestValidator.hexEncode(MessageDigest.getInstance("SHA-256").digest(body));
return MessageDigest.isEqual(
actual.getBytes(StandardCharsets.UTF_8),
declaredHash.toLowerCase().getBytes(StandardCharsets.UTF_8));
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/auth/SigV4RequestValidatorTest.java b/src/test/java/io/github/hectorvent/floci/core/common/auth/SigV4RequestValidatorTest.java
new file mode 100644
index 0000000000..a19c556d60
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/core/common/auth/SigV4RequestValidatorTest.java
@@ -0,0 +1,54 @@
+package io.github.hectorvent.floci.core.common.auth;
+
+import org.junit.jupiter.api.Test;
+
+import java.nio.charset.StandardCharsets;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Pins the shared SigV4 crypto primitives against AWS's own published test vector, independent
+ * of any one caller's canonical-request shape. SigV4Validator, RdsSigV4Validator, and S3's
+ * PreSignedUrlFilter/S3HeaderSignatureFilter each exercise these indirectly through their own
+ * request flows; this targets the primitives themselves at their one shared source.
+ */
+class SigV4RequestValidatorTest {
+
+ /**
+ * Signing-key derivation for AWS's well-known example secret key
+ * ("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLE", date 20150830, us-east-1, iam), the same
+ * inputs AWS uses throughout its SigV4 documentation. Expected value cross-checked
+ * independently with Python's hmac/hashlib, not derived from this code.
+ */
+ @Test
+ void deriveSigningKeyMatchesAnIndependentlyComputedValue() throws Exception {
+ byte[] signingKey = SigV4RequestValidator.deriveSigningKey(
+ "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLE", "20150830", "us-east-1", "iam");
+
+ assertEquals("93c91b7c5da17c72120bd321a9833353b5dd75355fe396cc91abc149ad9755b5",
+ SigV4RequestValidator.hexEncode(signingKey));
+ }
+
+ @Test
+ void sha256HexOfEmptyStringIsTheWellKnownConstant() throws Exception {
+ // This exact value is also hardcoded in validate()'s canonical request for the empty
+ // body ElastiCache/RDS presigned tokens always carry; this pins it independently.
+ assertEquals("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
+ SigV4RequestValidator.sha256Hex(""));
+ }
+
+ @Test
+ void hmacSha256IsDeterministicForTheSameKeyAndData() throws Exception {
+ byte[] key = "a-secret-key".getBytes(StandardCharsets.UTF_8);
+
+ byte[] first = SigV4RequestValidator.hmacSha256(key, "some data");
+ byte[] second = SigV4RequestValidator.hmacSha256(key, "some data");
+
+ assertEquals(SigV4RequestValidator.hexEncode(first), SigV4RequestValidator.hexEncode(second));
+ }
+
+ @Test
+ void hexEncodeLowercasesAndZeroPadsEachByte() {
+ assertEquals("00ff0a", SigV4RequestValidator.hexEncode(new byte[]{0x00, (byte) 0xFF, 0x0A}));
+ }
+}