mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
972ff03f00
commit
3cdc9ef22d
6 files changed
+222
-11
No files matched your search
@@ -0,0 +1,114 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamRole;
|
||||
import io.github.hectorvent.floci.services.iam.model.ServerCertificate;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** Direct Query handler tests with a mocked service, without a Quarkus application or HTTP server. */
|
||||
class IamQueryHandlerTest {
|
||||
|
||||
private static final String ROLE_NAME = "TimestampRole";
|
||||
private static final String CERTIFICATE_NAME = "TimestampCertificate";
|
||||
|
||||
/**
|
||||
* Smithy's date-time format carries at most millisecond precision and asks serializers to truncate
|
||||
* anything finer; ISO_INSTANT then prints the fraction in groups of three, or not at all.
|
||||
*/
|
||||
static Stream<Arguments> timestampCases() {
|
||||
return Stream.of(
|
||||
Arguments.of("2026-10-05T05:47:29.777826302Z", "2026-10-05T05:47:29.777Z"),
|
||||
Arguments.of("2026-10-05T05:47:29.785090420Z", "2026-10-05T05:47:29.785Z"),
|
||||
Arguments.of("2026-10-05T05:47:29Z", "2026-10-05T05:47:29Z"),
|
||||
Arguments.of("2026-10-05T05:47:29.000000001Z", "2026-10-05T05:47:29Z"),
|
||||
Arguments.of("2026-10-05T05:47:29.000999999Z", "2026-10-05T05:47:29Z"),
|
||||
Arguments.of("2026-10-05T05:47:29.001000000Z", "2026-10-05T05:47:29.001Z"),
|
||||
Arguments.of("2026-10-05T05:47:29.010000000Z", "2026-10-05T05:47:29.010Z"),
|
||||
Arguments.of("2026-12-31T23:59:59.999999999Z", "2026-12-31T23:59:59.999Z"),
|
||||
Arguments.of("1969-12-31T23:59:59.999999999Z", "1969-12-31T23:59:59.999Z"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("timestampCases")
|
||||
void serverCertificateResponsesWriteMillisecondTimestamps(String stored, String expected) throws Exception {
|
||||
Instant timestamp = Instant.parse(stored);
|
||||
ServerCertificate certificate = new ServerCertificate();
|
||||
certificate.setServerCertificateName(CERTIFICATE_NAME);
|
||||
certificate.setServerCertificateId("certificate-id");
|
||||
certificate.setArn("arn:aws:iam::000000000000:server-certificate/TimestampCertificate");
|
||||
certificate.setCertificateBody("fabricated-certificate");
|
||||
certificate.setUploadDate(timestamp);
|
||||
certificate.setExpiration(timestamp);
|
||||
|
||||
IamService service = mock(IamService.class);
|
||||
when(service.uploadServerCertificate(CERTIFICATE_NAME, "/", "fabricated-certificate",
|
||||
"fabricated-key", null, Map.of())).thenReturn(certificate);
|
||||
when(service.getServerCertificate(CERTIFICATE_NAME)).thenReturn(certificate);
|
||||
when(service.listServerCertificates(null)).thenReturn(List.of(certificate));
|
||||
IamQueryHandler handler = new IamQueryHandler(service, null, null, null, null, null, null);
|
||||
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("ServerCertificateName", CERTIFICATE_NAME);
|
||||
params.putSingle("Path", "/");
|
||||
params.putSingle("CertificateBody", "fabricated-certificate");
|
||||
params.putSingle("PrivateKey", "fabricated-key");
|
||||
|
||||
for (String action : List.of("UploadServerCertificate", "GetServerCertificate", "ListServerCertificates")) {
|
||||
try (Response response = handler.handle(action, params, null)) {
|
||||
assertXmlMember(response, "UploadDate", expected);
|
||||
assertXmlMember(response, "Expiration", expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("timestampCases")
|
||||
void roleResponsesWriteMillisecondTimestamps(String stored, String expected) throws Exception {
|
||||
IamRole role = new IamRole();
|
||||
role.setRoleName(ROLE_NAME);
|
||||
role.setRoleId("role-id");
|
||||
role.setPath("/");
|
||||
role.setArn("arn:aws:iam::000000000000:role/TimestampRole");
|
||||
role.setAssumeRolePolicyDocument("{}");
|
||||
role.setCreateDate(Instant.parse(stored));
|
||||
IamService service = mock(IamService.class);
|
||||
when(service.createRole(ROLE_NAME, "/", "{}", null, 3600, Map.of(), null)).thenReturn(role);
|
||||
when(service.getRole(ROLE_NAME)).thenReturn(role);
|
||||
IamQueryHandler handler = new IamQueryHandler(service, null, null, null, null, null, null);
|
||||
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("RoleName", ROLE_NAME);
|
||||
params.putSingle("Path", "/");
|
||||
params.putSingle("AssumeRolePolicyDocument", "{}");
|
||||
|
||||
for (String action : List.of("CreateRole", "GetRole")) {
|
||||
try (Response response = handler.handle(action, params, null)) {
|
||||
assertXmlMember(response, "CreateDate", expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertXmlMember(Response response, String member, String expected) throws Exception {
|
||||
assertEquals(200, response.getStatus());
|
||||
Document document = XmlParser.parseDocument((String) response.getEntity());
|
||||
NodeList matches = document.getElementsByTagNameNS("*", member);
|
||||
assertEquals(1, matches.getLength(), member);
|
||||
assertEquals(expected, matches.item(0).getTextContent(), member);
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,8 @@ import org.mockito.Mockito;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -2683,6 +2685,33 @@ class IamServiceTest {
|
||||
assertTrue(csv.contains("<root_account>,arn:aws:iam::"), "expected a root account row, got: " + csv);
|
||||
}
|
||||
|
||||
/**
|
||||
* The User Guide's example report writes every timestamp as 2014-10-15T16:31:25+00:00: whole
|
||||
* seconds and a numeric offset, never a nanosecond fraction or a trailing Z.
|
||||
*/
|
||||
@Test
|
||||
void credentialReportWritesTimestampsInTheDocumentedForm() {
|
||||
IamUser user = iamService.createUser("cred-report-time-user", "/");
|
||||
iamService.createLoginProfile("cred-report-time-user", "Sup3r-Secret!", false);
|
||||
AccessKey key = iamService.createAccessKey("cred-report-time-user");
|
||||
Instant passwordChanged = iamService.getLoginProfile("cred-report-time-user").getPasswordLastChanged();
|
||||
iamService.generateCredentialReport();
|
||||
|
||||
String csv = new String(Base64.getDecoder().decode(iamService.getCredentialReport().base64Content()));
|
||||
String[] fields = csv.lines().filter(line -> line.startsWith("cred-report-time-user,")).findFirst()
|
||||
.orElseThrow(() -> new AssertionError("no row for cred-report-time-user in: " + csv))
|
||||
.split(",", -1);
|
||||
|
||||
assertReportTime(user.getCreateDate(), fields[2], "user_creation_time");
|
||||
assertReportTime(passwordChanged, fields[5], "password_last_changed");
|
||||
assertReportTime(key.getCreateDate(), fields[9], "access_key_1_last_rotated");
|
||||
}
|
||||
|
||||
private static void assertReportTime(Instant stored, String field, String column) {
|
||||
assertTrue(field.matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\+00:00"), column + ": " + field);
|
||||
assertEquals(stored.truncatedTo(ChronoUnit.SECONDS), OffsetDateTime.parse(field).toInstant(), column);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCredentialReportIncludesEveryUserWithPasswordAndAccessKeyState() {
|
||||
iamService.createUser("cred-report-user", "/");
|
||||
|
||||
@@ -14,10 +14,15 @@ import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
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.MethodSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -31,6 +36,7 @@ import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyMap;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -44,6 +50,7 @@ class StsQueryHandlerTest {
|
||||
Pattern.compile("<SessionToken>([^<]+)</SessionToken>");
|
||||
|
||||
private static final Pattern ARN = Pattern.compile("<Arn>([^<]+)</Arn>");
|
||||
private static final Pattern EXPIRATION = Pattern.compile("<Expiration>([^<]+)</Expiration>");
|
||||
|
||||
private static StsQueryHandler newHandler() {
|
||||
IamRole role = new IamRole();
|
||||
@@ -67,6 +74,10 @@ class StsQueryHandlerTest {
|
||||
}
|
||||
|
||||
private static StsQueryHandler newHandler(IamService iamService, RegionResolver regionResolver) {
|
||||
return newHandler(iamService, regionResolver, Clock.systemUTC());
|
||||
}
|
||||
|
||||
private static StsQueryHandler newHandler(IamService iamService, RegionResolver regionResolver, Clock clock) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
EmulatorConfig.IamServiceConfig iam = mock(EmulatorConfig.IamServiceConfig.class);
|
||||
@@ -84,7 +95,8 @@ class StsQueryHandlerTest {
|
||||
mock(WebIdentityTokenVerifier.class),
|
||||
mock(OidcIssuerKeyLookup.class),
|
||||
mock(SAMLProviderService.class),
|
||||
mock(SAMLTrustPolicyEvaluator.class));
|
||||
mock(SAMLTrustPolicyEvaluator.class),
|
||||
clock);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -541,6 +553,42 @@ class StsQueryHandlerTest {
|
||||
assertTrue(body.contains("<AssumedRoleId>AROAKX9OBMP2TSQJZ48H:my-session</AssumedRoleId>"), body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Smithy's date-time format carries at most millisecond precision and asks serializers to truncate
|
||||
* anything finer, so the XML drops the sub-millisecond digits while the session keeps the full instant.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"2026-10-05T05:47:29.777826302Z, 2026-10-05T05:47:29.777Z",
|
||||
"2026-10-05T05:47:29.010000000Z, 2026-10-05T05:47:29.010Z",
|
||||
"2026-10-05T05:47:29.000999999Z, 2026-10-05T05:47:29Z",
|
||||
"2026-10-05T05:47:29Z, 2026-10-05T05:47:29Z"
|
||||
})
|
||||
void assumeRoleWritesExpirationAtMillisecondPrecision(String expiresAt, String expected) {
|
||||
IamRole role = new IamRole();
|
||||
role.setRoleName("TestRole");
|
||||
role.setArn("arn:aws:iam::000000000000:role/TestRole");
|
||||
IamService iamService = mock(IamService.class);
|
||||
when(iamService.findRole(anyString(), anyString())).thenReturn(Optional.of(role));
|
||||
Instant expiration = Instant.parse(expiresAt);
|
||||
Clock clock = Clock.fixed(expiration.minusSeconds(900), ZoneOffset.UTC);
|
||||
StsQueryHandler handler = newHandler(iamService, new RegionResolver(REGION, "000000000000"), clock);
|
||||
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("RoleArn", role.getArn());
|
||||
params.putSingle("RoleSessionName", "s");
|
||||
params.putSingle("DurationSeconds", "900");
|
||||
|
||||
Response response = handler.handle("AssumeRole", params);
|
||||
|
||||
assertEquals(200, response.getStatus(), (String) response.getEntity());
|
||||
assertEquals(expected, extract(EXPIRATION, (String) response.getEntity()));
|
||||
ArgumentCaptor<Instant> sessionExpiration = ArgumentCaptor.forClass(Instant.class);
|
||||
verify(iamService).registerSession(anyString(), anyString(), anyString(), eq(role.getArn()),
|
||||
sessionExpiration.capture(), isNull(), eq("000000000000"), eq("s"), anyString());
|
||||
assertEquals(expiration, sessionExpiration.getValue());
|
||||
}
|
||||
|
||||
private static String extract(Pattern pattern, String body) {
|
||||
Matcher matcher = pattern.matcher(body);
|
||||
assertTrue(matcher.find(), "expected " + pattern + " in " + body);
|
||||
|
||||
Reference in new issue
Block a user