refactor(dsql): read a token's expiry from the validator (#6246)

* refactor(dsql): read a token's expiry from the validator

DSQL's endpoint worked out a token's expiry for its "Signature expired" hint,
and SigV4RequestValidator worked it out again to judge the token, each with
its own parsing (a long against an int, its own date formatter). The
validator now exposes the expiry it judges, and the hint reports that one,
so the two cannot disagree. A date or an expiry that does not parse, or one
past what an Instant holds, gives no expiry, and the token is refused as
before.

* fix(dsql): read a token's expiry seconds as an int, as validate did

The shared expiry parsed X-Amz-Expires as a long, so a signed token whose
expiry no int holds, which validate used to refuse, was taken: a refactor
that widened what is accepted. It reads the seconds as an int again, and
the DSQL token helper signs a long so a test can sign one past an int.
This commit is contained in:
Alessandro Lattao authored and GitHub committed 2026-10-22 12:42:30 -04:00
1 parent b0a4d5064b
commit f08ce36298
4 files changed
+84 -22

No files matched your search

@@ -12,6 +12,7 @@ import org.junit.jupiter.params.provider.ValueSource;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -170,6 +171,48 @@ class SigV4RequestValidatorTest {
false, null, "DSQL IAM token", null, Duration.ofSeconds(5)));
}
@Test
void expiryIsTheTokensDatePlusItsExpiresSeconds() throws Exception {
Instant signedAt = Instant.parse("2026-10-22T10:15:30Z");
String token = SigV4TokenTestHelper.createDsqlToken("abc.dsql.eu-west-1.localhost.floci.io", "eu-west-1",
"DbConnectAdmin", "AKIDDSQL", "secret-dsql", signedAt, 900, null);
assertEquals(Optional.of(signedAt.plusSeconds(900)),
SigV4RequestValidator.expiry(token.substring(token.indexOf('?') + 1)));
}
/** A token without a date and an expiry that both parse has no expiry. */
@ParameterizedTest
@ValueSource(strings = {
"X-Amz-Date=20261022T101530Z",
"X-Amz-Expires=900",
"X-Amz-Date=20261022T101530Z&X-Amz-Expires=soon",
"X-Amz-Date=yesterday&X-Amz-Expires=900",
"X-Amz-Date=20261022T101530Z&X-Amz-Expires=2147483648",
"X-Amz-Date=20261022T101530Z&X-Amz-Expires=9223372036854775807"})
void expiryIsEmptyWithoutADateAndAnExpiryThatParse(String rawQuery) {
assertEquals(Optional.empty(), SigV4RequestValidator.expiry(rawQuery));
}
/**
* A signed token whose expiry is more seconds than an int holds is refused, as it always was,
* while the same token one second shorter is taken.
*/
@Test
void validateRefusesASignedTokenWhoseExpiryIsPastAnInt() throws Exception {
IamService iamService = IamServiceTestHelper.iamServiceWithAccessKey("AKIDDSQL", "secret-dsql");
SigV4RequestValidator validator = new SigV4RequestValidator(iamService);
String host = "abc.dsql.eu-west-1.localhost.floci.io";
for (long expires : new long[] {Integer.MAX_VALUE, Integer.MAX_VALUE + 1L}) {
String token = SigV4TokenTestHelper.createDsqlToken(host, "eu-west-1", "DbConnectAdmin", "AKIDDSQL",
"secret-dsql", Instant.now(), expires, null);
String rawQuery = token.substring(token.indexOf('?') + 1);
assertEquals(expires == Integer.MAX_VALUE, validator.validate(rawQuery, host, "DbConnectAdmin", "DBUser",
false, null, "DSQL IAM token", null, Duration.ofSeconds(5)), "X-Amz-Expires=" + expires);
}
}
private static boolean validate(IamService iamService, String token) {
String rawQuery = token.substring(token.indexOf('?') + 1);
return new SigV4RequestValidator(iamService).validate(
@@ -127,7 +127,7 @@ public final class SigV4TokenTestHelper {
String accessKeyId,
String secretKey,
Instant timestamp,
int expiresSeconds,
long expiresSeconds,
String sessionToken
) throws Exception {
Map<String, String> params = new LinkedHashMap<>();
@@ -231,7 +231,7 @@ public final class SigV4TokenTestHelper {
String region,
String service,
Instant timestamp,
int expiresSeconds,
long expiresSeconds,
Map<String, String> params,
Map<String, String> signedHeaders
) throws Exception {
@@ -243,7 +243,7 @@ public final class SigV4TokenTestHelper {
queryParams.put("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
queryParams.put("X-Amz-Credential", accessKeyId + "/" + credentialScope);
queryParams.put("X-Amz-Date", dateTime);
queryParams.put("X-Amz-Expires", Integer.toString(expiresSeconds));
queryParams.put("X-Amz-Expires", Long.toString(expiresSeconds));
String signedHeaderNames = signedHeaders.keySet().stream()
.sorted()
.reduce((left, right) -> left + ";" + right)