Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(elasticache,rds): extract shared SigV4 request validation into a common utility (#3581)
Priamo Germosen8d3fdbdd569e
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.javanew file mode 100644index 0000000000..e8c01d38a5--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/core/common/auth/SigV4RequestValidator.java@@ -0,0 +1,214 @@+package io.github.hectorvent.floci.core.common.auth;++import io.github.hectorvent.floci.services.iam.IamService;+import org.jboss.logging.Logger;++import javax.crypto.Mac;+import javax.crypto.spec.SecretKeySpec;+import java.net.URLDecoder;+import java.nio.charset.StandardCharsets;+import java.security.MessageDigest;+import java.time.Instant;+import java.time.ZoneOffset;+import java.time.format.DateTimeFormatter;+import java.util.Arrays;+import java.util.Optional;+import java.util.stream.Collectors;++/**+ * Verifies AWS SigV4 presigned-URL auth tokens: a GET request against the target's own+ * hostname, signed with SigV4 query-parameter signing and an empty body, per AWS's+ * presigned-URL scheme. This is the IAM-auth mechanism shared by the ElastiCache and RDS+ * (and, via {@code RdsSigV4Validator}, Redshift) TCP proxies.+ *+ * <p>Extracted from {@code SigV4Validator} (ElastiCache) and {@code RdsSigV4Validator}+ * (RDS), whose only real differences are the identity query parameter name+ * ({@code User} vs {@code DBUser}), whether that parameter must be present at all, and+ * the exact string each signs as the canonical {@code host} header. Callers resolve the+ * token's host/authority themselves (ElastiCache signs only the cluster hostname, RDS+ * signs {@code host:port}) and pass the result in here.+ */+public final class SigV4RequestValidator {++ private static final Logger LOG = Logger.getLogger(SigV4RequestValidator.class);+ private static final DateTimeFormatter DATETIME_FMT =+ DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);++ /**+ * Well-known local-dev credential pair used pervasively by default AWS SDK clients+ * (AwsBasicCredentials.create("test", "test")) against this emulator, mirrored from the+ * identical fallback in S3Service/PreSignedUrlFilter. Deliberately not a fallback for any+ * other unregistered access key, only this exact, already-public pair is honored.+ */+ private static final String LEGACY_ACCESS_KEY_ID = "test";+ private static final String LEGACY_SECRET_KEY = "test";++ private final IamService iamService;++ public SigV4RequestValidator(IamService iamService) {+ this.iamService = iamService;+ }++ /**+ * Verifies a SigV4 presigned-URL token's required parameters, expiry, identity, and+ * signature.+ *+ * @param rawQuery the token's raw (still URL-encoded) query string+ * @param canonicalHostHeaderValue the exact string the caller signed as the canonical+ * {@code host} header, e.g. the cluster ID for+ * ElastiCache or {@code host:port} for RDS+ * @param identityParamName the query parameter carrying the caller's identity, e.g.+ * {@code User} or {@code DBUser}+ * @param identityRequired whether the token must include {@code identityParamName},+ * independent of whether an expected value was supplied+ * @param expectedIdentityValue the identity value to require a match against, or null+ * to skip the identity check entirely+ * @param logLabel short label prefixed to this validator's debug log lines, e.g.+ * {@code "IAM token"} or {@code "RDS IAM token"}+ * @return true if the token is well-formed, not expired, identity matches (when+ * checked), and the signature is valid+ */+ public boolean validate(String rawQuery, String canonicalHostHeaderValue, String identityParamName,+ boolean identityRequired, String expectedIdentityValue, String logLabel) {+ try {+ String[] rawPairs = rawQuery.split("&");+ String action = findRawParam(rawPairs, "Action");+ String identity = findRawParam(rawPairs, identityParamName);+ String dateTime = findRawParam(rawPairs, "X-Amz-Date");+ String expires = findRawParam(rawPairs, "X-Amz-Expires");+ String credential = findRawParam(rawPairs, "X-Amz-Credential");+ String signedHeaders = findRawParam(rawPairs, "X-Amz-SignedHeaders");+ String signature = findRawParam(rawPairs, "X-Amz-Signature");++ if (!"connect".equals(action) || (identityRequired && identity == null) || dateTime == null+ || expires == null || credential == null || signedHeaders == null || signature == null) {+ LOG.debugv("{0} missing required SigV4 parameters", logLabel);+ return false;+ }++ if (expectedIdentityValue != null && !expectedIdentityValue.equals(identity)) {+ LOG.debugv("{0} {1} mismatch: expected={2}, got={3}",+ logLabel, identityParamName, expectedIdentityValue, identity);+ return false;+ }++ Instant tokenTime = Instant.from(DATETIME_FMT.parse(dateTime));+ int expirySeconds = Integer.parseInt(expires);+ if (Instant.now().isAfter(tokenTime.plusSeconds(expirySeconds))) {+ LOG.debugv("{0} expired", logLabel);+ return false;+ }++ String decodedCredential = urlDecode(credential);+ String[] credParts = decodedCredential.split("/");+ if (credParts.length < 5) {+ return false;+ }+ String accessKeyId = credParts[0];+ String date = credParts[1];+ String region = credParts[2];+ String service = credParts[3];+ String credentialScope = date + "/" + region + "/" + service + "/aws4_request";++ String secretKey;+ if (LEGACY_ACCESS_KEY_ID.equals(accessKeyId)) {+ secretKey = LEGACY_SECRET_KEY;+ } else {+ String sessionToken = findRawParam(rawPairs, "X-Amz-Security-Token");+ Optional<String> registeredSecretKey = iamService.findSecretKey(+ accessKeyId, sessionToken == null ? null : urlDecode(sessionToken));+ if (registeredSecretKey.isEmpty()) {+ LOG.debugv("{0} references unregistered access key={1}", logLabel, sanitizeForLog(accessKeyId));+ return false;+ }+ secretKey = registeredSecretKey.get();+ }++ String canonicalQueryString = Arrays.stream(rawPairs)+ .filter(p -> !rawParamName(p).equals("X-Amz-Signature"))+ .sorted((a, b) -> rawParamName(a).compareTo(rawParamName(b)))+ .collect(Collectors.joining("&"));++ String canonicalRequest = "GET\n/\n"+ + canonicalQueryString + "\n"+ + "host:" + canonicalHostHeaderValue + "\n\n"+ + "host\n"+ + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";++ String stringToSign = "AWS4-HMAC-SHA256\n"+ + dateTime + "\n"+ + credentialScope + "\n"+ + sha256Hex(canonicalRequest);++ byte[] signingKey = deriveSigningKey(secretKey, date, region, service);+ String expectedSignature = hexEncode(hmacSha256(signingKey, stringToSign));++ boolean valid = MessageDigest.isEqual(+ expectedSignature.getBytes(StandardCharsets.UTF_8),+ signature.getBytes(StandardCharsets.UTF_8));+ if (!valid) {+ LOG.debugv("{0} signature mismatch for accessKey={1}", logLabel, sanitizeForLog(accessKeyId));+ }+ return valid;++ } catch (Exception e) {+ LOG.debugv("{0} validation error: {1}", logLabel, e.getMessage());+ return false;+ }+ }++ private static String rawParamName(String rawPair) {+ int eq = rawPair.indexOf('=');+ return eq >= 0 ? rawPair.substring(0, eq) : rawPair;+ }++ private static String findRawParam(String[] rawPairs, String name) {+ for (String pair : rawPairs) {+ int eq = pair.indexOf('=');+ if (eq >= 0 && name.equals(pair.substring(0, eq))) {+ return urlDecode(pair.substring(eq + 1));+ }+ }+ return null;+ }++ private static String urlDecode(String value) {+ return URLDecoder.decode(value, StandardCharsets.UTF_8);+ }++ /**+ * Strips control characters (CR, LF, etc.) from an attacker-controlled value before it is+ * interpolated into a log line, preventing log injection / forged multi-line log entries.+ */+ private static String sanitizeForLog(String value) {+ return value == null ? null : value.replaceAll("\\p{Cntrl}", "");+ }++ private 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");+ }++ private 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 {+ MessageDigest digest = MessageDigest.getInstance("SHA-256");+ return hexEncode(digest.digest(input.getBytes(StandardCharsets.UTF_8)));+ }++ private 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();+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/SigV4Validator.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/SigV4Validator.javaindex f48fe06159..07378c82e9 100644--- a/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/SigV4Validator.java+++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/SigV4Validator.java@@ -1,48 +1,30 @@package io.github.hectorvent.floci.services.elasticache.proxy;+import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;import io.github.hectorvent.floci.services.iam.IamService;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;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;-import java.security.MessageDigest;-import java.time.Instant;-import java.time.ZoneOffset;-import java.time.format.DateTimeFormatter;-import java.util.Arrays;-import java.util.Optional;-import java.util.stream.Collectors;/*** Validates ElastiCache IAM auth tokens (SigV4 presigned URLs).* Extracted from ElastiCacheQueryHandler so it can be shared with the TCP auth proxy.+ * The SigV4 signature verification itself lives in {@link SigV4RequestValidator}, shared+ * with {@code RdsSigV4Validator}; this class only handles the ElastiCache-specific token+ * shape (cluster ID as the signed host, {@code User} for identity).*/@ApplicationScopedpublic class SigV4Validator {private static final Logger LOG = Logger.getLogger(SigV4Validator.class);- private static final DateTimeFormatter DATETIME_FMT =- DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);- /**- * Well-known local-dev credential pair used pervasively by default AWS SDK clients- * (AwsBasicCredentials.create("test", "test")) against this emulator, mirrored from the- * identical fallback in S3Service/PreSignedUrlFilter. Deliberately not a fallback for any- * other unregistered access key -- only this exact, already-public pair is honored.- */- private static final String LEGACY_ACCESS_KEY_ID = "test";- private static final String LEGACY_SECRET_KEY = "test";-- private final IamService iamService;+ private final SigV4RequestValidator requestValidator;@Injectpublic SigV4Validator(IamService iamService) {- this.iamService = iamService;+ this.requestValidator = new SigV4RequestValidator(iamService);}/**@@ -72,144 +54,19 @@ public class SigV4Validator {return false;}- String[] rawPairs = rawQuery.split("&");- String action = findRawParam(rawPairs, "Action");- String user = findRawParam(rawPairs, "User");- String dateTime = findRawParam(rawPairs, "X-Amz-Date");- String expires = findRawParam(rawPairs, "X-Amz-Expires");- String credential = findRawParam(rawPairs, "X-Amz-Credential");- String signedHeaders = findRawParam(rawPairs, "X-Amz-SignedHeaders");- String signature = findRawParam(rawPairs, "X-Amz-Signature");-- if (!"connect".equals(action) || dateTime == null || expires == null- || credential == null || signedHeaders == null || signature == null) {- LOG.debugv("IAM token missing required SigV4 parameters");- return false;- }-- if (expectedUsername != null && !expectedUsername.equals(user)) {- LOG.debugv("IAM token user mismatch: expected={0}, got={1}",- expectedUsername, user);- return false;- }-- Instant tokenTime = Instant.from(DATETIME_FMT.parse(dateTime));- int expirySeconds = Integer.parseInt(expires);- if (Instant.now().isAfter(tokenTime.plusSeconds(expirySeconds))) {- LOG.debugv("IAM token expired");- return false;- }-- String decodedCredential = urlDecode(credential);- String[] credParts = decodedCredential.split("/");- if (credParts.length < 5) {- return false;- }- String accessKeyId = credParts[0];- String date = credParts[1];- String region = credParts[2];- String service = credParts[3];- String credentialScope = date + "/" + region + "/" + service + "/aws4_request";-- String secretKey;- if (LEGACY_ACCESS_KEY_ID.equals(accessKeyId)) {- secretKey = LEGACY_SECRET_KEY;- } else {- String sessionToken = findRawParam(rawPairs, "X-Amz-Security-Token");- Optional<String> registeredSecretKey = iamService.findSecretKey(- accessKeyId, sessionToken == null ? null : urlDecode(sessionToken));- if (registeredSecretKey.isEmpty()) {- LOG.debugv("IAM token references unregistered access key={0}", sanitizeForLog(accessKeyId));- return false;- }- secretKey = registeredSecretKey.get();- }-- String canonicalQueryString = Arrays.stream(rawPairs)- .filter(p -> !rawParamName(p).equals("X-Amz-Signature"))- .sorted((a, b) -> rawParamName(a).compareTo(rawParamName(b)))- .collect(Collectors.joining("&"));-- String canonicalRequest = "GET\n/\n"- + canonicalQueryString + "\n"- + "host:" + clusterId + "\n\n"- + "host\n"- + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";-- String stringToSign = "AWS4-HMAC-SHA256\n"- + dateTime + "\n"- + credentialScope + "\n"- + sha256Hex(canonicalRequest);-- byte[] signingKey = deriveSigningKey(secretKey, date, region, service);- String expectedSignature = hexEncode(hmacSha256(signingKey, stringToSign));-- boolean valid = MessageDigest.isEqual(- expectedSignature.getBytes(StandardCharsets.UTF_8),- signature.getBytes(StandardCharsets.UTF_8));- if (!valid) {- LOG.debugv("IAM token signature mismatch for accessKey={0}", sanitizeForLog(accessKeyId));- }- return valid;-+ return requestValidator.validate(rawQuery, clusterId, "User", false, expectedUsername, "IAM token");} catch (Exception e) {LOG.debugv("IAM token validation error: {0}", e.getMessage());return false;}}- private static String rawParamName(String rawPair) {- int eq = rawPair.indexOf('=');- return eq >= 0 ? rawPair.substring(0, eq) : rawPair;- }-- private static String findRawParam(String[] rawPairs, String name) {- for (String pair : rawPairs) {- int eq = pair.indexOf('=');- if (eq >= 0 && name.equals(pair.substring(0, eq))) {- return urlDecode(pair.substring(eq + 1));- }- }- return null;- }-- private static String urlDecode(String value) {- return URLDecoder.decode(value, StandardCharsets.UTF_8);- }-/**- * Strips control characters (CR, LF, etc.) from an attacker-controlled value before it is- * interpolated into a log line, preventing log injection / forged multi-line log entries.+ * Kept here, not only in SigV4RequestValidator, because SigV4ValidatorTest reflects on+ * this exact declared method to verify log-injection protection at this class's own+ * entry point.*/private static String sanitizeForLog(String value) {return value == null ? null : value.replaceAll("\\p{Cntrl}", "");}-- private 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");- }-- private 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 {- MessageDigest digest = MessageDigest.getInstance("SHA-256");- return hexEncode(digest.digest(input.getBytes(StandardCharsets.UTF_8)));- }-- private 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();- }}diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4Validator.java b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4Validator.javaindex 7b0b532b20..43b8d092ed 100644--- a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4Validator.java+++ b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4Validator.java@@ -1,50 +1,32 @@package io.github.hectorvent.floci.services.rds.proxy;+import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;import io.github.hectorvent.floci.services.iam.IamService;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;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;-import java.security.MessageDigest;-import java.time.Instant;-import java.time.ZoneOffset;-import java.time.format.DateTimeFormatter;-import java.util.Arrays;-import java.util.Optional;-import java.util.stream.Collectors;/*** Validates RDS IAM auth tokens (SigV4 presigned URLs).* RDS tokens sign {@code host:port} in the canonical host header, unlike ElastiCache* which signs only the cluster hostname. The token format is:* {@code hostname:port/?Action=connect&DBUser=user&X-Amz-*=...}+ * The SigV4 signature verification itself lives in {@link SigV4RequestValidator}, shared+ * with {@code SigV4Validator} (ElastiCache); this class only handles the RDS-specific+ * token shape. Reused as-is by Redshift, whose IAM auth works identically.*/@ApplicationScopedpublic class RdsSigV4Validator {private static final Logger LOG = Logger.getLogger(RdsSigV4Validator.class);- private static final DateTimeFormatter DATETIME_FMT =- DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);- /**- * Well-known local-dev credential pair used pervasively by default AWS SDK clients- * (AwsBasicCredentials.create("test", "test")) against this emulator, mirrored from the- * identical fallback in S3Service/PreSignedUrlFilter. Deliberately not a fallback for any- * other unregistered access key -- only this exact, already-public pair is honored.- */- private static final String LEGACY_ACCESS_KEY_ID = "test";- private static final String LEGACY_SECRET_KEY = "test";-- private final IamService iamService;+ private final SigV4RequestValidator requestValidator;@Injectpublic RdsSigV4Validator(IamService iamService) {- this.iamService = iamService;+ this.requestValidator = new SigV4RequestValidator(iamService);}/**@@ -72,146 +54,19 @@ public class RdsSigV4Validator {// RDS tokens sign host:port in the canonical host headerString authority = (port > 0) ? host + ":" + port : host;- String[] rawPairs = rawQuery.split("&");- String action = findRawParam(rawPairs, "Action");- String dbUser = findRawParam(rawPairs, "DBUser");- String dateTime = findRawParam(rawPairs, "X-Amz-Date");- String expires = findRawParam(rawPairs, "X-Amz-Expires");- String credential = findRawParam(rawPairs, "X-Amz-Credential");- String signedHeaders = findRawParam(rawPairs, "X-Amz-SignedHeaders");- String signature = findRawParam(rawPairs, "X-Amz-Signature");-- if (!"connect".equals(action) || dbUser == null || dateTime == null || expires == null- || credential == null || signedHeaders == null || signature == null) {- LOG.debugv("RDS IAM token missing required SigV4 parameters");- return false;- }-- if (clientUsername != null && !clientUsername.equals(dbUser)) {- LOG.debugv("RDS IAM token DBUser mismatch: client={0}, token={1}",- clientUsername, dbUser);- return false;- }-- Instant tokenTime = Instant.from(DATETIME_FMT.parse(dateTime));- int expirySeconds = Integer.parseInt(expires);- if (Instant.now().isAfter(tokenTime.plusSeconds(expirySeconds))) {- LOG.debugv("RDS IAM token expired");- return false;- }-- String decodedCredential = urlDecode(credential);- String[] credParts = decodedCredential.split("/");- if (credParts.length < 5) {- return false;- }- String accessKeyId = credParts[0];- String date = credParts[1];- String region = credParts[2];- String service = credParts[3];- String credentialScope = date + "/" + region + "/" + service + "/aws4_request";-- String secretKey;- if (LEGACY_ACCESS_KEY_ID.equals(accessKeyId)) {- secretKey = LEGACY_SECRET_KEY;- } else {- String sessionToken = findRawParam(rawPairs, "X-Amz-Security-Token");- Optional<String> registeredSecretKey = iamService.findSecretKey(- accessKeyId, sessionToken == null ? null : urlDecode(sessionToken));- if (registeredSecretKey.isEmpty()) {- LOG.debugv("RDS IAM token references unregistered access key={0}", sanitizeForLog(accessKeyId));- return false;- }- secretKey = registeredSecretKey.get();- }-- // Canonical query string: sorted pairs, excluding X-Amz-Signature- String canonicalQueryString = Arrays.stream(rawPairs)- .filter(p -> !rawParamName(p).equals("X-Amz-Signature"))- .sorted((a, b) -> rawParamName(a).compareTo(rawParamName(b)))- .collect(Collectors.joining("&"));-- // Canonical request: RDS uses host:port as the host header value- String canonicalRequest = "GET\n/\n"- + canonicalQueryString + "\n"- + "host:" + authority + "\n\n"- + "host\n"- + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; // sha256("")-- String stringToSign = "AWS4-HMAC-SHA256\n"- + dateTime + "\n"- + credentialScope + "\n"- + sha256Hex(canonicalRequest);-- byte[] signingKey = deriveSigningKey(secretKey, date, region, service);- String expectedSignature = hexEncode(hmacSha256(signingKey, stringToSign));-- boolean valid = MessageDigest.isEqual(- expectedSignature.getBytes(StandardCharsets.UTF_8),- signature.getBytes(StandardCharsets.UTF_8));- if (!valid) {- LOG.debugv("RDS IAM token signature mismatch for accessKey={0}", sanitizeForLog(accessKeyId));- }- return valid;-+ return requestValidator.validate(rawQuery, authority, "DBUser", true, clientUsername, "RDS IAM token");} catch (Exception e) {LOG.debugv("RDS IAM token validation error: {0}", e.getMessage());return false;}}- private static String rawParamName(String rawPair) {- int eq = rawPair.indexOf('=');- return eq >= 0 ? rawPair.substring(0, eq) : rawPair;- }-- private static String findRawParam(String[] rawPairs, String name) {- for (String pair : rawPairs) {- int eq = pair.indexOf('=');- if (eq >= 0 && name.equals(pair.substring(0, eq))) {- return urlDecode(pair.substring(eq + 1));- }- }- return null;- }-- private static String urlDecode(String value) {- return URLDecoder.decode(value, StandardCharsets.UTF_8);- }-/**- * Strips control characters (CR, LF, etc.) from an attacker-controlled value before it is- * interpolated into a log line, preventing log injection / forged multi-line log entries.+ * Kept here, not only in SigV4RequestValidator, because RdsSigV4ValidatorTest reflects+ * on this exact declared method to verify log-injection protection at this class's own+ * entry point.*/private static String sanitizeForLog(String value) {return value == null ? null : value.replaceAll("\\p{Cntrl}", "");}-- private 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");- }-- private 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 {- MessageDigest digest = MessageDigest.getInstance("SHA-256");- return hexEncode(digest.digest(input.getBytes(StandardCharsets.UTF_8)));- }-- private 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();- }}