mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
refactor(s3): point PreSignedUrlFilter at the shared SigV4RequestValidator crypto helpers (#3596)
S3's PreSignedUrlFilter carried its own private copy of deriveSigningKey/hmacSha256/ sha256Hex/hexEncode, the same SigV4 signing-key derivation block that SigV4Validator (ElastiCache) and RdsSigV4Validator (RDS) used to carry independently before #3581 consolidated those two into core.common.auth.SigV4RequestValidator. S3's copy was out of scope there and left as a third independent copy of the same crypto logic. S3's canonical-request construction is real REST SigV4 (method/path/headers/payload hash), a different shape from SigV4RequestValidator's own validate() method, which handles the simplified query-token form ElastiCache/RDS presigned URLs use. So this doesn't reuse validate() itself, only the four low-level crypto primitives, now made public on SigV4RequestValidator for exactly this kind of reuse. PreSignedUrlFilter's own copies are removed and its one call site now delegates to the shared methods. S3HeaderSignatureFilter, which reused PreSignedUrlFilter's helpers directly (they were package-private), is updated to call the shared class as well. Adds SigV4RequestValidatorTest: the shared crypto primitives were previously only exercised indirectly through each caller's own request flow. Now that they're public specifically for cross-service reuse, this pins them directly against an independently computed value (cross-checked with Python's hmac/hashlib, not derived from this code). No behavior change otherwise. Full S3 test suite (over 1000 tests across all S3 files) and the ElastiCache/RDS/Redshift SigV4 validator tests pass unchanged.
This commit is contained in:
1 parent
3c96327ac4
commit
bd13c7f9ce
4 files changed
+74
-42
No files matched your search
+10
-5
@@ -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));
|
||||
|
||||
@@ -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\"?>")
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in new issue
Block a user