mirror of
https://github.com/floci-io/floci.git
synced 2026-12-03 02:07:58 -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
+54
@@ -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}));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user