fix: serialize IAM and STS Query timestamps with millisecond precision (#5110)

* fix: format IAM and STS Query timestamps at millisecond precision

Truncate only the Query XML projection, preserving stored instants and
session expiration. Cover nanosecond inputs, millisecond and second
boundaries, and timezone offsets with direct mocked handler tests.

Signed-off-by: Andre Kurait <akurait@amazon.com>

* fix(iam): write credential report times as AWS does, pin STS test time

The credential report CSV still wrote ISO_INSTANT at nanosecond
precision with a trailing Z. The IAM User Guide's example report writes
2014-10-15T16:31:25+00:00, whole seconds with a numeric offset, so the
report now uses that form.

The AssumeRole expiration test pinned time by static-mocking
Instant.now(). StsQueryHandler gains a package-private constructor that
takes a Clock (the CDI constructor keeps the system clock), and the test
moves into StsQueryHandlerTest on its existing harness. The IAM cases
move to IamQueryHandlerTest; input offsets, which only exercised JDK
parsing, are dropped.

---------

Signed-off-by: Andre Kurait <akurait@amazon.com>
Co-authored-by: Priamo <pgermosen@gmail.com>
This commit is contained in:
Andre KuraitandPriamo authored and GitHub committed 2026-10-10 16:46:17 -04:00
1 parent 972ff03f00
commit 3cdc9ef22d
6 files changed
+222 -11

No files matched your search

@@ -32,6 +32,7 @@ import org.jboss.logging.Logger;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
@@ -2936,7 +2937,7 @@ public class IamQueryHandler {
private String isoDate(Instant instant) {
if (instant == null) return "";
return DateTimeFormatter.ISO_INSTANT.format(instant);
return DateTimeFormatter.ISO_INSTANT.format(instant.truncatedTo(ChronoUnit.MILLIS));
}
/** For a member AWS types as a blob, which the Query protocol carries base64-encoded. */
@@ -236,6 +236,9 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
/** The creation half of a long-term API key's alias. */
private static final DateTimeFormatter CREDENTIAL_ALIAS_DATE =
DateTimeFormatter.ofPattern("yyyyMMdd").withZone(ZoneOffset.UTC);
/** The credential report's timestamps, as in the User Guide's example rows: 2014-10-15T16:31:25+00:00. */
private static final DateTimeFormatter CREDENTIAL_REPORT_TIME =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssxxx").withZone(ZoneOffset.UTC);
/** AWS General Reference, IAM service quotas: "SSH Public keys per user", not adjustable. */
private static final int MAX_SSH_PUBLIC_KEYS_PER_USER = 5;
/** publicKeyIdType is 20 to 128 of [\w]+, and AWS's own examples use the APKA prefix. */
@@ -5358,6 +5361,6 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
}
private String isoDate(Instant instant) {
return instant == null ? "" : DateTimeFormatter.ISO_INSTANT.format(instant);
return instant == null ? "" : CREDENTIAL_REPORT_TIME.format(instant);
}
}
@@ -26,8 +26,10 @@ import org.jboss.logging.Logger;
import java.security.SecureRandom;
import java.security.interfaces.RSAPublicKey;
import java.time.Clock;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -55,6 +57,7 @@ public class StsQueryHandler {
private final OidcIssuerKeyLookup oidcIssuerKeys;
private final SAMLProviderService samlProviderService;
private final SAMLTrustPolicyEvaluator samlTrustEvaluator;
private final Clock clock;
/** CSPRNG for session secret keys and session tokens; ordinary IDs keep using {@link ThreadLocalRandom}. */
private final SecureRandom secureRandom = new SecureRandom();
@@ -73,6 +76,18 @@ public class StsQueryHandler {
OidcIssuerKeyLookup oidcIssuerKeys,
SAMLProviderService samlProviderService,
SAMLTrustPolicyEvaluator samlTrustEvaluator) {
this(iamService, accountResolver, regionResolver, config, trustPolicyEvaluator, webIdentityTrustEvaluator,
tokenVerifier, oidcIssuerKeys, samlProviderService, samlTrustEvaluator, Clock.systemUTC());
}
StsQueryHandler(IamService iamService, AccountResolver accountResolver, RegionResolver regionResolver,
EmulatorConfig config, AssumeRolePolicyEvaluator trustPolicyEvaluator,
WebIdentityTrustPolicyEvaluator webIdentityTrustEvaluator,
WebIdentityTokenVerifier tokenVerifier,
OidcIssuerKeyLookup oidcIssuerKeys,
SAMLProviderService samlProviderService,
SAMLTrustPolicyEvaluator samlTrustEvaluator,
Clock clock) {
this.iamService = iamService;
this.accountResolver = accountResolver;
this.regionResolver = regionResolver;
@@ -83,6 +98,7 @@ public class StsQueryHandler {
this.oidcIssuerKeys = oidcIssuerKeys;
this.samlProviderService = samlProviderService;
this.samlTrustEvaluator = samlTrustEvaluator;
this.clock = clock;
}
public Response handle(String action, MultivaluedMap<String, String> params) {
@@ -117,7 +133,7 @@ public class StsQueryHandler {
String accessKeyId = "ASIA" + randomId(16);
String secretKey = randomSecret(40);
String sessionToken = randomSecret(200);
Instant expiration = Instant.now().plusSeconds(durationSeconds);
Instant expiration = clock.instant().plusSeconds(durationSeconds);
String roleName = roleArn != null && roleArn.contains("/")
? roleArn.substring(roleArn.lastIndexOf('/') + 1)
@@ -238,7 +254,7 @@ public class StsQueryHandler {
String accessKeyId = "ASIA" + randomId(16);
String secretKey = randomSecret(40);
String sessionToken = randomSecret(200);
Instant expiration = Instant.now().plusSeconds(durationSeconds);
Instant expiration = clock.instant().plusSeconds(durationSeconds);
String result = credentialsXml(accessKeyId, secretKey, sessionToken, expiration);
// No role ARN: the credentials route back to the caller's account and act as the caller.
@@ -286,7 +302,7 @@ public class StsQueryHandler {
String accessKeyId = "ASIA" + randomId(16);
String secretKey = randomSecret(40);
String sessionToken = randomSecret(200);
Instant expiration = Instant.now().plusSeconds(durationSeconds);
Instant expiration = clock.instant().plusSeconds(durationSeconds);
String sessionRoleArn = canonicalRoleArn(role, accountId, roleName);
String assumedRoleArn = assumedRoleArn(sessionRoleArn, accountId, roleName, sessionName);
@@ -454,7 +470,7 @@ public class StsQueryHandler {
new AwsException("InvalidIdentityToken", "The SAML provider is not trusted.", 400));
SAMLAssertionVerifier.Verified verified;
try {
verified = SAMLAssertionVerifier.verify(getParam(params, "SAMLAssertion"), provider, Instant.now());
verified = SAMLAssertionVerifier.verify(getParam(params, "SAMLAssertion"), provider, clock.instant());
} catch (SAMLAssertionVerifier.InvalidAssertionException e) {
throw new AwsException("InvalidIdentityToken", e.awsMessage(), 400);
}
@@ -482,8 +498,8 @@ public class StsQueryHandler {
if (sessionName.length() > 64) {
sessionName = sessionName.substring(0, 64);
}
Instant requestedExpiration = Instant.now().plusSeconds(durationSeconds);
Instant roleExpiration = Instant.now().plusSeconds(role.getMaxSessionDuration());
Instant requestedExpiration = clock.instant().plusSeconds(durationSeconds);
Instant roleExpiration = clock.instant().plusSeconds(role.getMaxSessionDuration());
Instant expiration = verified.expiration().isBefore(requestedExpiration) ? verified.expiration() : requestedExpiration;
if (roleExpiration.isBefore(expiration)) {
expiration = roleExpiration;
@@ -528,7 +544,7 @@ public class StsQueryHandler {
String accessKeyId = "ASIA" + randomId(16);
String secretKey = randomSecret(40);
String sessionToken = randomSecret(200);
Instant expiration = Instant.now().plusSeconds(durationSeconds);
Instant expiration = clock.instant().plusSeconds(durationSeconds);
String accountId = regionResolver.getAccountId();
String federatedUserId = accountId + ":" + name;
String federatedUserArn = regionResolver.buildGlobalArn("sts", accountId, "federated-user/" + name);
@@ -632,7 +648,7 @@ public class StsQueryHandler {
}
private String isoDate(Instant instant) {
return DateTimeFormatter.ISO_INSTANT.format(instant);
return DateTimeFormatter.ISO_INSTANT.format(instant.truncatedTo(ChronoUnit.MILLIS));
}
private static String randomId(int length) {