mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(rds,elasticache): decode IAM auth token parameters once (#5039)
* fix(rds,elasticache): decode IAM auth token parameters once SigV4RequestValidator, which verifies the IAM auth tokens the RDS, Redshift, ElastiCache and MemoryDB proxies receive, URL-decoded every query parameter as it looked it up, then decoded X-Amz-Credential and X-Amz-Security-Token a second time. Session tokens are drawn from an alphabet that includes '+', '/' and '='. A presigner writes '+' as %2B; the first decode restored it and the second turned it into a space, so the session no longer matched and the token was refused with "password authentication failed". Every token signed with a Lambda function's execution-role credentials failed this way, from the AWS CLI, botocore and the Go SDK's feature/rds/auth alike, while AssumeRole credentials passed only because their tokens are alphanumeric. The second decode also let a token carrying %252B stand in for a session whose token holds a literal '+'. SigV4 defines the canonical query string as each parameter name and value URI-encoded (only A-Z a-z 0-9 - . _ ~ left as they are, uppercase hex, a space as %20, '/' encoded), sorted by encoded name and then by value. The validator now decodes each parameter exactly once, keeping a literal '+' as a plus as the API Gateway and EKS verifiers already do, looks the session up with that value, and rebuilds the canonical query string from the decoded parameters rather than hashing the bytes on the wire, so the token is checked against the string its signer hashed whichever valid percent-encoding it travelled in. Using the raw pairs as before would have fixed the session lookup alone, but would still reject a token whose credential travels with literal slashes or lowercase hex. The test token helper encoded values with URLEncoder, whose form encoding writes a space as '+' and leaves '*' unescaped, which no SDK presigner does; it now applies SigV4's UriEncode. S3 presigned URLs do not go through this validator and read X-Amz-Security-Token once already: a URL presigned with a Lambda session's credentials is served, and one whose token reads a space for the '+' is refused. * fix(rds): authorize the key the token's signature was verified with The signature check reads a token's parameters with their names percent-decoded, and takes the first X-Amz-Credential it finds. The rds-db:connect check read the credential again from the raw query, matching the name byte for byte. A token that spelled its signer's credential as X-Amz%2DCredential and added a plain X-Amz-Credential of a key IAM does not know was verified with the signer's key and authorized on the unknown one, which IAM enforcement waves through: the connection was allowed whatever the signer's policy said. SigV4RequestValidator.queryParameter reads a parameter exactly as the signature check does, and RdsSigV4Validator takes the credential scope from there, so the key authorized is the key that signed. validateAuthorizesTheKeyTheSignatureWasVerifiedWith signs such a token as SigV4 signs a query and expects it refused; it was accepted before. RdsIamAuthLambdaSessionIntegrationTest now creates its database inside the try whose finally deletes it, so a create that fails an assertion leaves no container behind.
This commit is contained in:
1 parent
38ce99ba54
commit
3041d6bd90
8 files changed
+453
-43
No files matched your search
@@ -655,6 +655,8 @@ The RDS auth proxy validates the master username and password at the proxy layer
|
||||
|
||||
IAM database authentication is also supported. Set `--enable-iam-database-authentication` at instance creation time and use `aws rds generate-db-auth-token` to obtain a token.
|
||||
|
||||
A token can be signed with an access key or with temporary credentials: an assumed role's, or the execution role credentials Floci hands a Lambda function. A token signed with temporary credentials carries their session token in `X-Amz-Security-Token`, and it is accepted while that session is valid and only with the session token issued alongside the access key.
|
||||
|
||||
On RDS, a token is only good for the endpoint it was generated for: the hostname, port and region passed to `generate-db-auth-token` are the ones the instance (or cluster, or RDS Proxy) publishes. MySQL and MariaDB endpoints in Floci always refuse a token generated for another endpoint. PostgreSQL endpoints do the same by default; set `FLOCI_SERVICES_RDS_IAM_TOKEN_ENDPOINT_BINDING=false` to accept a token generated for another name, for clients that generate tokens for a container name or DNS alias the endpoint does not publish. Either way, the username must match the `DBUser` in the token exactly. Because clients on the host reach the same proxy over the loopback interface, a token generated for `localhost` or `127.0.0.1` on the published port is accepted as well. Clients on a Docker network connect by Floci's container name, so set `FLOCI_SERVICES_RDS_ENDPOINT_HOST` to that name (see [Docker Compose](#docker-compose)) so it is what the endpoint publishes and tokens are generated for. With [IAM enforcement](iam.md#iam-enforcement-mode) turned on, the token's principal must also be allowed `rds-db:connect` on the database user, scoped the way AWS scopes it:
|
||||
|
||||
```
|
||||
|
||||
+90
-28
@@ -11,7 +11,9 @@ import java.security.MessageDigest;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Arrays;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -71,14 +73,14 @@ public final class SigV4RequestValidator {
|
||||
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");
|
||||
List<QueryParameter> parameters = decodeQuery(rawQuery);
|
||||
String action = findParam(parameters, "Action");
|
||||
String identity = findParam(parameters, identityParamName);
|
||||
String dateTime = findParam(parameters, "X-Amz-Date");
|
||||
String expires = findParam(parameters, "X-Amz-Expires");
|
||||
String credential = findParam(parameters, "X-Amz-Credential");
|
||||
String signedHeaders = findParam(parameters, "X-Amz-SignedHeaders");
|
||||
String signature = findParam(parameters, "X-Amz-Signature");
|
||||
|
||||
if (!"connect".equals(action) || (identityRequired && identity == null) || dateTime == null
|
||||
|| expires == null || credential == null || signedHeaders == null || signature == null) {
|
||||
@@ -99,8 +101,7 @@ public final class SigV4RequestValidator {
|
||||
return false;
|
||||
}
|
||||
|
||||
String decodedCredential = urlDecode(credential);
|
||||
String[] credParts = decodedCredential.split("/");
|
||||
String[] credParts = credential.split("/");
|
||||
if (credParts.length < 5) {
|
||||
return false;
|
||||
}
|
||||
@@ -114,9 +115,8 @@ public final class SigV4RequestValidator {
|
||||
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));
|
||||
accessKeyId, findParam(parameters, "X-Amz-Security-Token"));
|
||||
if (registeredSecretKey.isEmpty()) {
|
||||
LOG.debugv("{0} references unregistered access key={1}", logLabel, sanitizeForLog(accessKeyId));
|
||||
return false;
|
||||
@@ -124,13 +124,8 @@ public final class SigV4RequestValidator {
|
||||
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"
|
||||
+ canonicalQueryString(parameters) + "\n"
|
||||
+ "host:" + canonicalHostHeaderValue + "\n\n"
|
||||
+ "host\n"
|
||||
+ "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
|
||||
@@ -157,23 +152,90 @@ public final class SigV4RequestValidator {
|
||||
}
|
||||
}
|
||||
|
||||
private static String rawParamName(String rawPair) {
|
||||
int eq = rawPair.indexOf('=');
|
||||
return eq >= 0 ? rawPair.substring(0, eq) : rawPair;
|
||||
private record QueryParameter(String name, String value) {}
|
||||
|
||||
/**
|
||||
* The value of {@code name} in a token's query string, read as {@link #validate} reads it: the
|
||||
* first parameter whose percent-decoded name matches, its value decoded once. A caller that acts
|
||||
* on a parameter after validating the token reads it here, so it acts on what was verified: a
|
||||
* name spelled with a percent-encoded character, or a parameter given twice, cannot make it read
|
||||
* a different credential than the one the signature was checked with.
|
||||
*/
|
||||
public static String queryParameter(String rawQuery, String name) {
|
||||
return findParam(decodeQuery(rawQuery), name);
|
||||
}
|
||||
|
||||
private static String findRawParam(String[] rawPairs, String name) {
|
||||
for (String pair : rawPairs) {
|
||||
/**
|
||||
* Every parameter of the token's query string, its name and value percent-decoded exactly once.
|
||||
* A session token is drawn from an alphabet that includes {@code +}, {@code /} and {@code =}, and
|
||||
* the presigner writes it percent-encoded once; decoding it again would turn every {@code +}
|
||||
* into a space, and a literal {@code %2B} into a {@code +}.
|
||||
*/
|
||||
private static List<QueryParameter> decodeQuery(String rawQuery) {
|
||||
List<QueryParameter> parameters = new ArrayList<>();
|
||||
for (String pair : rawQuery.split("&")) {
|
||||
if (pair.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
int eq = pair.indexOf('=');
|
||||
if (eq >= 0 && name.equals(pair.substring(0, eq))) {
|
||||
return urlDecode(pair.substring(eq + 1));
|
||||
parameters.add(new QueryParameter(
|
||||
decodeQueryComponent(eq >= 0 ? pair.substring(0, eq) : pair),
|
||||
decodeQueryComponent(eq >= 0 ? pair.substring(eq + 1) : "")));
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
|
||||
private static String findParam(List<QueryParameter> parameters, String name) {
|
||||
for (QueryParameter parameter : parameters) {
|
||||
if (parameter.name().equals(name)) {
|
||||
return parameter.value();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String urlDecode(String value) {
|
||||
return URLDecoder.decode(value, StandardCharsets.UTF_8);
|
||||
/**
|
||||
* SigV4's canonical query string: every parameter but {@code X-Amz-Signature}, its name and
|
||||
* value each URI-encoded, sorted by encoded name and then by encoded value. Built from the
|
||||
* decoded parameters rather than the bytes on the wire, so it is the string the signer hashed
|
||||
* whichever valid percent-encoding the token travelled in.
|
||||
*/
|
||||
private static String canonicalQueryString(List<QueryParameter> parameters) {
|
||||
return parameters.stream()
|
||||
.filter(parameter -> !"X-Amz-Signature".equals(parameter.name()))
|
||||
.map(parameter -> new QueryParameter(uriEncode(parameter.name()), uriEncode(parameter.value())))
|
||||
.sorted(Comparator.comparing(QueryParameter::name).thenComparing(QueryParameter::value))
|
||||
.map(parameter -> parameter.name() + "=" + parameter.value())
|
||||
.collect(Collectors.joining("&"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Percent-decodes a query-string component without {@code URLDecoder}'s form-encoding rule that
|
||||
* a literal {@code +} means a space: SigV4 signers escape a space as {@code %20}, so a raw
|
||||
* {@code +} on the wire is a plus sign.
|
||||
*/
|
||||
private static String decodeQueryComponent(String value) {
|
||||
return URLDecoder.decode(value.replace("+", "%2B"), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
/**
|
||||
* SigV4's UriEncode: every byte of the UTF-8 form except {@code A-Z a-z 0-9 - . _ ~} becomes
|
||||
* {@code %XY} with uppercase hex, a space included ({@code %20}, never {@code +}) and {@code /}
|
||||
* included.
|
||||
*/
|
||||
private static String uriEncode(String value) {
|
||||
StringBuilder encoded = new StringBuilder(value.length());
|
||||
for (byte raw : value.getBytes(StandardCharsets.UTF_8)) {
|
||||
int unsigned = Byte.toUnsignedInt(raw);
|
||||
if ((unsigned >= 'A' && unsigned <= 'Z') || (unsigned >= 'a' && unsigned <= 'z')
|
||||
|| (unsigned >= '0' && unsigned <= '9') || unsigned == '-' || unsigned == '.'
|
||||
|| unsigned == '_' || unsigned == '~') {
|
||||
encoded.append((char) unsigned);
|
||||
} else {
|
||||
encoded.append('%').append(String.format("%02X", unsigned));
|
||||
}
|
||||
}
|
||||
return encoded.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,8 +12,6 @@ import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
@@ -137,17 +135,15 @@ public class RdsSigV4Validator {
|
||||
/**
|
||||
* The {@code X-Amz-Credential} scope split into its parts
|
||||
* ({@code accessKeyId/date/region/service/aws4_request}), or {@code null} if absent or malformed.
|
||||
* Read as the signature check reads it, so the key authorized is the key that signed.
|
||||
*/
|
||||
private static String[] credentialScope(String rawQuery) {
|
||||
for (String pair : rawQuery.split("&")) {
|
||||
int eq = pair.indexOf('=');
|
||||
if (eq >= 0 && "X-Amz-Credential".equals(pair.substring(0, eq))) {
|
||||
String[] parts = URLDecoder.decode(pair.substring(eq + 1), StandardCharsets.UTF_8)
|
||||
.split("/");
|
||||
return parts.length >= 5 ? parts : null;
|
||||
}
|
||||
String credential = SigV4RequestValidator.queryParameter(rawQuery, "X-Amz-Credential");
|
||||
if (credential == null) {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
String[] parts = credential.split("/");
|
||||
return parts.length >= 5 ? parts : null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+90
-1
@@ -1,8 +1,15 @@
|
||||
package io.github.hectorvent.floci.core.common.auth;
|
||||
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.testutil.IamServiceTestHelper;
|
||||
import io.github.hectorvent.floci.testutil.SigV4TokenTestHelper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
@@ -12,10 +19,15 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
* 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.
|
||||
* request flows; this targets the primitives themselves at their one shared source, and how
|
||||
* {@code validate} reads a presigned token's query string: each parameter decoded once, and the
|
||||
* canonical query string rebuilt from the decoded values.
|
||||
*/
|
||||
class SigV4RequestValidatorTest {
|
||||
|
||||
private static final String SESSION_ACCESS_KEY_ID = "ASIAEXAMPLESESSION01";
|
||||
private static final String SESSION_SECRET = "session-secret-key";
|
||||
|
||||
/**
|
||||
* Signing-key derivation for AWS's well-known example secret key
|
||||
* ("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLE", date 20150830, us-east-1, iam), the same
|
||||
@@ -68,4 +80,81 @@ class SigV4RequestValidatorTest {
|
||||
assertFalse(SigV4RequestValidator.isSha256Hex("UNSIGNED-PAYLOAD"));
|
||||
assertFalse(SigV4RequestValidator.isSha256Hex("e3b0c442"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Session tokens are drawn from an alphabet that includes {@code +}, {@code /} and {@code =}
|
||||
* (Lambda's execution-role tokens always are), and a presigner writes each of them
|
||||
* percent-encoded once. The token has to be decoded exactly once: a second pass turns every
|
||||
* {@code +} into a space, and a literal {@code %2B} into a {@code +}, so the issued session no
|
||||
* longer matches what the client signed with.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"IQoJb3JpZ2luX2VjEJr+abc/def+ghi/jkl==",
|
||||
"a+b",
|
||||
"a/b",
|
||||
"a=b",
|
||||
"a%2Bb",
|
||||
"+/=%2B"
|
||||
})
|
||||
void validateAcceptsASessionTokenThatTheSignerPercentEncoded(String sessionToken) throws Exception {
|
||||
IamService iamService = IamServiceTestHelper.iamServiceWithSessionCredential(
|
||||
SESSION_ACCESS_KEY_ID, SESSION_SECRET, sessionToken, Instant.now().plusSeconds(3600));
|
||||
String token = SigV4TokenTestHelper.createRdsToken("db.example.local", 5432, "app_user",
|
||||
SESSION_ACCESS_KEY_ID, SESSION_SECRET, Instant.now().minusSeconds(60), 900, sessionToken);
|
||||
|
||||
assertTrue(validate(iamService, token));
|
||||
}
|
||||
|
||||
/**
|
||||
* Decoding once also means a token only names the session it carries: one that decodes to the
|
||||
* issued session token only after a second pass, or that differs from it by a space where the
|
||||
* issued one has a {@code +}, is not that session's token.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"a+b, a b",
|
||||
"a+b, a%2Bb",
|
||||
"a%2Bb, a+b"
|
||||
})
|
||||
void validateRefusesASessionTokenOtherThanTheIssuedOne(String issued, String presented) throws Exception {
|
||||
IamService iamService = IamServiceTestHelper.iamServiceWithSessionCredential(
|
||||
SESSION_ACCESS_KEY_ID, SESSION_SECRET, issued, Instant.now().plusSeconds(3600));
|
||||
String token = SigV4TokenTestHelper.createRdsToken("db.example.local", 5432, "app_user",
|
||||
SESSION_ACCESS_KEY_ID, SESSION_SECRET, Instant.now().minusSeconds(60), 900, presented);
|
||||
|
||||
assertFalse(validate(iamService, token));
|
||||
}
|
||||
|
||||
/**
|
||||
* The canonical query string is built from the decoded parameters, each name and value
|
||||
* URI-encoded again as SigV4 defines it, not from the bytes on the wire: a credential that
|
||||
* travels with literal slashes, or with lowercase hex, is still the credential that was signed.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"/", "%2f"})
|
||||
void validateBuildsTheCanonicalQueryFromTheDecodedParameters(String slashOnTheWire) throws Exception {
|
||||
IamService iamService = IamServiceTestHelper.iamServiceWithAccessKey("AKIDRDS", "secret-rds");
|
||||
String token = SigV4TokenTestHelper.createRdsToken("db.example.local", 5432, "app_user",
|
||||
"AKIDRDS", "secret-rds", Instant.now().minusSeconds(60), 900);
|
||||
String credential = token.replaceFirst("(?s).*(X-Amz-Credential=[^&]*).*", "$1");
|
||||
String rewritten = token.replace(credential, credential.replace("%2F", slashOnTheWire));
|
||||
|
||||
assertTrue(validate(iamService, rewritten));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateRefusesATokenWhoseParameterValueChangedAfterSigning() throws Exception {
|
||||
IamService iamService = IamServiceTestHelper.iamServiceWithAccessKey("AKIDRDS", "secret-rds");
|
||||
String token = SigV4TokenTestHelper.createRdsToken("db.example.local", 5432, "app user",
|
||||
"AKIDRDS", "secret-rds", Instant.now().minusSeconds(60), 900);
|
||||
|
||||
assertFalse(validate(iamService, token.replace("DBUser=app%20user", "DBUser=app%2Buser")));
|
||||
}
|
||||
|
||||
private static boolean validate(IamService iamService, String token) {
|
||||
String rawQuery = token.substring(token.indexOf('?') + 1);
|
||||
return new SigV4RequestValidator(iamService).validate(
|
||||
rawQuery, "db.example.local:5432", "DBUser", true, null, "RDS IAM token");
|
||||
}
|
||||
}
|
||||
+19
@@ -247,6 +247,25 @@ class SigV4ValidatorTest {
|
||||
assertTrue(validator.validate(token, "cache-cluster-01", "default"));
|
||||
}
|
||||
|
||||
/** A Lambda execution role's session token carries {@code +}, {@code /} and {@code =}. */
|
||||
@Test
|
||||
void validateAcceptsTokenSignedWithLambdaSessionCredentials() throws Exception {
|
||||
String accessKeyId = "ASIAIOSFODNN7EXAMPLE";
|
||||
String secretAccessKey = "lambda+session/secret";
|
||||
String sessionToken = "IQoJb3JpZ2luX2VjEJr+abc/def+ghi/jkl==";
|
||||
IamService iamService = IamServiceTestHelper.iamServiceWithSessionCredential(
|
||||
accessKeyId, secretAccessKey, sessionToken, Instant.now().plusSeconds(3600));
|
||||
SigV4Validator validator = new SigV4Validator(iamService);
|
||||
|
||||
String token = SigV4TokenTestHelper.createElastiCacheToken(
|
||||
"cache-cluster-01", "default", accessKeyId, secretAccessKey,
|
||||
Instant.now().minusSeconds(60), 900, sessionToken);
|
||||
|
||||
assertTrue(validator.validate(token, "cache-cluster-01", "default"));
|
||||
assertFalse(validator.validate(token.replace("%2B", "%20"), "cache-cluster-01", "default"),
|
||||
"a token whose session token reads a space where the issued one has a + is another token");
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateRejectsStsCredentialWithoutIssuedSessionToken() throws Exception {
|
||||
String accessKeyId = "ASIAIOSFODNN7EXAMPLE";
|
||||
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
package io.github.hectorvent.floci.services.rds;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.testutil.SigV4TokenTestHelper;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Properties;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static io.restassured.http.ContentType.URLENC;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
/**
|
||||
* IAM database authentication from a Lambda function, against a real PostgreSQL container behind
|
||||
* the RDS proxy. A function signs its token with its execution role's session credentials, and
|
||||
* that session token carries {@code +}, {@code /} and {@code =}, which the presigner writes
|
||||
* percent-encoded once. The proxy has to read the token back as it was signed for the session to
|
||||
* be found and the signature to match.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@Tag("docker")
|
||||
class RdsIamAuthLambdaSessionIntegrationTest {
|
||||
|
||||
private static final String RDS_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20261002/us-east-1/rds/aws4_request, "
|
||||
+ "SignedHeaders=content-type;host, Signature=test";
|
||||
private static final String ACCOUNT_ID = "000000000000";
|
||||
private static final String MASTER_USER = "master";
|
||||
private static final String MASTER_PASSWORD = "Master123!";
|
||||
private static final String DATABASE = "appdb";
|
||||
private static final String SESSION_SECRET = "lambda+session/secret=key";
|
||||
private static final String SESSION_TOKEN = "IQoJb3JpZ2luX2VjEJr+abc/def+ghi/jkl%2Bmno==";
|
||||
|
||||
@Inject
|
||||
DockerClient dockerClient;
|
||||
|
||||
@Inject
|
||||
IamService iamService;
|
||||
|
||||
@BeforeEach
|
||||
void checkDocker() {
|
||||
try {
|
||||
dockerClient.pingCmd().exec();
|
||||
} catch (Exception e) {
|
||||
assumeTrue(false, "Docker is not available: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void tokenSignedWithLambdaSessionCredentialsConnects() throws Exception {
|
||||
String instanceId = "iam-lambda-" + Long.toString(System.nanoTime(), 36);
|
||||
String accessKeyId = "ASIA" + Long.toString(System.nanoTime(), 36).toUpperCase();
|
||||
try {
|
||||
// Inside the try, so a create that provisions the database but fails an assertion is
|
||||
// still deleted.
|
||||
int port = rds("CreateDBInstance")
|
||||
.formParam("DBInstanceIdentifier", instanceId)
|
||||
.formParam("DBInstanceClass", "db.t3.micro")
|
||||
.formParam("Engine", "postgres")
|
||||
.formParam("MasterUsername", MASTER_USER)
|
||||
.formParam("MasterUserPassword", MASTER_PASSWORD)
|
||||
.formParam("DBName", DATABASE)
|
||||
.formParam("AllocatedStorage", "20")
|
||||
.formParam("EnableIAMDatabaseAuthentication", "true")
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<IAMDatabaseAuthenticationEnabled>true</IAMDatabaseAuthenticationEnabled>"))
|
||||
.extract().xmlPath()
|
||||
.getInt("CreateDBInstanceResponse.CreateDBInstanceResult.DBInstance.Endpoint.Port");
|
||||
iamService.registerLambdaExecutionRoleSession(ACCOUNT_ID, accessKeyId, SESSION_SECRET, SESSION_TOKEN,
|
||||
"arn:aws:iam::" + ACCOUNT_ID + ":role/connect-with-iam");
|
||||
await().atMost(Duration.ofSeconds(60)).ignoreExceptions().until(() -> {
|
||||
try (Connection connection = connect(port, MASTER_PASSWORD)) {
|
||||
return connection.isValid(5);
|
||||
}
|
||||
});
|
||||
|
||||
String token = SigV4TokenTestHelper.createRdsToken("127.0.0.1", port, MASTER_USER,
|
||||
accessKeyId, SESSION_SECRET, Instant.now().minusSeconds(60), 900, SESSION_TOKEN);
|
||||
try (Connection connection = connect(port, token);
|
||||
Statement statement = connection.createStatement();
|
||||
ResultSet rows = statement.executeQuery("SELECT current_user")) {
|
||||
assertTrue(rows.next());
|
||||
assertEquals(MASTER_USER, rows.getString(1));
|
||||
}
|
||||
|
||||
String anotherSession = SigV4TokenTestHelper.createRdsToken("127.0.0.1", port, MASTER_USER,
|
||||
accessKeyId, SESSION_SECRET, Instant.now().minusSeconds(60), 900,
|
||||
SESSION_TOKEN.replace('+', ' '));
|
||||
SQLException refused = assertThrows(SQLException.class, () -> connect(port, anotherSession).close());
|
||||
assertTrue(refused.getMessage().contains("password authentication failed"), refused.getMessage());
|
||||
} finally {
|
||||
iamService.unregisterSession(ACCOUNT_ID, accessKeyId);
|
||||
rds("DeleteDBInstance")
|
||||
.formParam("DBInstanceIdentifier", instanceId)
|
||||
.formParam("SkipFinalSnapshot", "true")
|
||||
.when().post("/");
|
||||
}
|
||||
}
|
||||
|
||||
private static Connection connect(int port, String password) throws SQLException {
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("user", MASTER_USER);
|
||||
properties.setProperty("password", password);
|
||||
properties.setProperty("sslmode", "disable");
|
||||
properties.setProperty("connectTimeout", "5");
|
||||
return DriverManager.getConnection("jdbc:postgresql://127.0.0.1:" + port + "/" + DATABASE, properties);
|
||||
}
|
||||
|
||||
private static RequestSpecification rds(String action) {
|
||||
return given().header("Authorization", RDS_AUTH)
|
||||
.contentType(URLENC)
|
||||
.formParam("Action", action)
|
||||
.formParam("Version", "2014-10-31");
|
||||
}
|
||||
}
|
||||
+41
@@ -64,6 +64,24 @@ class RdsSigV4ValidatorTest {
|
||||
"a valid token from a principal that is not allowed rds-db:connect must be rejected");
|
||||
}
|
||||
|
||||
/**
|
||||
* The key IAM authorizes must be the key the signature was verified with. A token that spells
|
||||
* its signer's credential with a percent-encoded name and adds a second credential, of a key
|
||||
* IAM does not know, must not be authorized on that second key.
|
||||
*/
|
||||
@Test
|
||||
void validateAuthorizesTheKeyTheSignatureWasVerifiedWith() throws Exception {
|
||||
IamService iamService = IamServiceTestHelper.iamServiceWithUserPolicy(
|
||||
"AKIDRDS", "secret-rds", "jane", S3_ONLY_POLICY);
|
||||
RdsSigV4Validator validator = new RdsSigV4Validator(iamService, () -> true);
|
||||
String token = SigV4TokenTestHelper.createRdsTokenWithASecondCredential(
|
||||
"db.example.local", 3307, "jane_doe", "AKIDRDS", "secret-rds", "AKIDUNKNOWN",
|
||||
Instant.now().minusSeconds(60), 900);
|
||||
|
||||
assertFalse(validator.validate(token, "jane_doe", exampleBinding()),
|
||||
"the signer is not allowed rds-db:connect, whatever other credential the token names");
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateAcceptsCallerAllowedRdsDbConnectOnTheBoundDbUser() throws Exception {
|
||||
// The example policy from the AWS "IAM database authentication" guide.
|
||||
@@ -515,6 +533,29 @@ class RdsSigV4ValidatorTest {
|
||||
"Validator must accept RDS IAM tokens signed with STS session credentials (ASIA… keys)");
|
||||
}
|
||||
|
||||
/**
|
||||
* What every Lambda that connects with IAM authentication sends: a token presigned with the
|
||||
* function's execution-role credentials, whose session token carries {@code +}, {@code /} and
|
||||
* {@code =}.
|
||||
*/
|
||||
@Test
|
||||
void validateAcceptsTokenSignedWithLambdaSessionCredentials() throws Exception {
|
||||
String accessKeyId = "ASIAIOSFODNN7EXAMPLE";
|
||||
String secretAccessKey = "lambda+session/secret";
|
||||
String sessionToken = "IQoJb3JpZ2luX2VjEJr+abc/def+ghi/jkl==";
|
||||
IamService iamService = IamServiceTestHelper.iamServiceWithSessionCredential(
|
||||
accessKeyId, secretAccessKey, sessionToken, Instant.now().plusSeconds(3600));
|
||||
RdsSigV4Validator validator = new RdsSigV4Validator(iamService);
|
||||
|
||||
String token = SigV4TokenTestHelper.createRdsToken(
|
||||
"db.example.local", 3307, "admin", accessKeyId, secretAccessKey,
|
||||
Instant.now().minusSeconds(60), 900, sessionToken);
|
||||
|
||||
assertTrue(validator.validate(token, "admin", exampleBinding()));
|
||||
assertFalse(validator.validate(token.replace("%2B", "%20"), "admin", exampleBinding()),
|
||||
"a token whose session token reads a space where the issued one has a + is another token");
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateRejectsStsCredentialWithoutIssuedSessionToken() throws Exception {
|
||||
String accessKeyId = "ASIAIOSFODNN7EXAMPLE";
|
||||
|
||||
@@ -2,7 +2,6 @@ package io.github.hectorvent.floci.testutil;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.time.Instant;
|
||||
@@ -116,6 +115,59 @@ public final class SigV4TokenTestHelper {
|
||||
"rds-db", timestamp, expiresSeconds, params, Map.of("host", host + ":" + port));
|
||||
}
|
||||
|
||||
/**
|
||||
* An RDS token that carries the signer's credential under a percent-encoded parameter name
|
||||
* ({@code X-Amz%2DCredential}) and, after it, a second {@code X-Amz-Credential} naming
|
||||
* {@code otherAccessKeyId}: a token that tries to have one key's signature verified and another
|
||||
* key authorized. Signed as SigV4 signs a query, over every parameter decoded, so its signature
|
||||
* is valid for the signer's key.
|
||||
*/
|
||||
public static String createRdsTokenWithASecondCredential(
|
||||
String host,
|
||||
int port,
|
||||
String dbUser,
|
||||
String accessKeyId,
|
||||
String secretKey,
|
||||
String otherAccessKeyId,
|
||||
Instant timestamp,
|
||||
int expiresSeconds
|
||||
) throws Exception {
|
||||
String date = DateTimeFormatter.ofPattern("yyyyMMdd").withZone(ZoneOffset.UTC).format(timestamp);
|
||||
String dateTime = DATETIME_FMT.format(timestamp);
|
||||
String credentialScope = date + "/us-east-1/rds-db/aws4_request";
|
||||
List<String[]> params = List.of(
|
||||
new String[] {"Action", "connect"},
|
||||
new String[] {"DBUser", dbUser},
|
||||
new String[] {"X-Amz-Algorithm", "AWS4-HMAC-SHA256"},
|
||||
new String[] {"X-Amz-Credential", accessKeyId + "/" + credentialScope},
|
||||
new String[] {"X-Amz-Credential", otherAccessKeyId + "/" + credentialScope},
|
||||
new String[] {"X-Amz-Date", dateTime},
|
||||
new String[] {"X-Amz-Expires", Integer.toString(expiresSeconds)},
|
||||
new String[] {"X-Amz-SignedHeaders", "host"});
|
||||
|
||||
String canonicalQuery = params.stream()
|
||||
.map(param -> uriEncode(param[0]) + "=" + uriEncode(param[1]))
|
||||
.sorted()
|
||||
.reduce((a, b) -> a + "&" + b)
|
||||
.orElseThrow();
|
||||
String canonicalRequest = "GET\n/\n"
|
||||
+ canonicalQuery + "\n"
|
||||
+ "host:" + host + ":" + port + "\n\n"
|
||||
+ "host\n"
|
||||
+ "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
|
||||
String stringToSign = "AWS4-HMAC-SHA256\n" + dateTime + "\n" + credentialScope + "\n"
|
||||
+ sha256Hex(canonicalRequest);
|
||||
String signature = hexEncode(hmacSha256(
|
||||
deriveSigningKey(secretKey, date, "us-east-1", "rds-db"), stringToSign));
|
||||
|
||||
List<String> wire = new ArrayList<>();
|
||||
for (String[] param : params) {
|
||||
String name = param[1].startsWith(accessKeyId + "/") ? "X-Amz%2DCredential" : param[0];
|
||||
wire.add(name + "=" + uriEncode(param[1]));
|
||||
}
|
||||
return host + ":" + port + "/?" + String.join("&", wire) + "&X-Amz-Signature=" + signature;
|
||||
}
|
||||
|
||||
public static String createEksToken(
|
||||
String clusterName,
|
||||
String accessKeyId,
|
||||
@@ -177,7 +229,7 @@ public final class SigV4TokenTestHelper {
|
||||
|
||||
List<String> encodedPairs = new ArrayList<>();
|
||||
for (Map.Entry<String, String> entry : queryParams.entrySet()) {
|
||||
encodedPairs.add(entry.getKey() + "=" + urlEncode(entry.getValue()));
|
||||
encodedPairs.add(entry.getKey() + "=" + uriEncode(entry.getValue()));
|
||||
}
|
||||
|
||||
String canonicalQuery = encodedPairs.stream()
|
||||
@@ -212,8 +264,24 @@ public final class SigV4TokenTestHelper {
|
||||
return eq >= 0 ? rawPair.substring(0, eq) : rawPair;
|
||||
}
|
||||
|
||||
private static String urlEncode(String value) {
|
||||
return URLEncoder.encode(value, StandardCharsets.UTF_8);
|
||||
/**
|
||||
* SigV4's UriEncode, as the AWS SDK presigners apply it to every query value: RFC 3986, with
|
||||
* only {@code A-Z a-z 0-9 - . _ ~} left as they are and uppercase hex. Not {@code URLEncoder},
|
||||
* whose form encoding writes a space as {@code +} and leaves {@code *} unescaped.
|
||||
*/
|
||||
private static String uriEncode(String value) {
|
||||
StringBuilder encoded = new StringBuilder(value.length());
|
||||
for (byte raw : value.getBytes(StandardCharsets.UTF_8)) {
|
||||
int unsigned = Byte.toUnsignedInt(raw);
|
||||
if ((unsigned >= 'A' && unsigned <= 'Z') || (unsigned >= 'a' && unsigned <= 'z')
|
||||
|| (unsigned >= '0' && unsigned <= '9') || unsigned == '-' || unsigned == '.'
|
||||
|| unsigned == '_' || unsigned == '~') {
|
||||
encoded.append((char) unsigned);
|
||||
} else {
|
||||
encoded.append('%').append(String.format("%02X", unsigned));
|
||||
}
|
||||
}
|
||||
return encoded.toString();
|
||||
}
|
||||
|
||||
private static byte[] deriveSigningKey(String secretKey, String date, String region,
|
||||
|
||||
Reference in new issue
Block a user