diff --git a/src/main/java/io/floci/aws/services/iam/IamService.java b/src/main/java/io/floci/aws/services/iam/IamService.java index dad21cbeca..bc218f1f3c 100644 --- a/src/main/java/io/floci/aws/services/iam/IamService.java +++ b/src/main/java/io/floci/aws/services/iam/IamService.java @@ -2750,6 +2750,11 @@ public class IamService implements SessionAccountLookup, ResourceProvider { // profile deleted and recreated takes its history with it. checkPasswordPolicy(password, null); profile = new LoginProfile(userName, password, passwordResetRequired); + // Stamped from the service's clock, as every later password change is, so the + // credential report compares times read from one timeline. + Instant now = clock.instant(); + profile.setCreateDate(now); + profile.setPasswordLastChanged(now); loginProfiles.put(userName, profile); } LOG.infov("Created IAM login profile for user: {0}", userName); @@ -2778,7 +2783,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { checkPasswordPolicy(password, profile); recordSupersededPassword(profile); profile.setPassword(password); - profile.setPasswordLastChanged(Instant.now()); + profile.setPasswordLastChanged(clock.instant()); } if (passwordResetRequired != null) { profile.setPasswordResetRequired(passwordResetRequired); @@ -2840,7 +2845,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { checkPasswordPolicy(newPassword, profile); recordSupersededPassword(profile); profile.setPassword(newPassword); - profile.setPasswordLastChanged(Instant.now()); + profile.setPasswordLastChanged(clock.instant()); profile.setPasswordResetRequired(false); loginProfiles.put(userName, profile); } @@ -6391,7 +6396,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { * AWS's own documented example response for the no-report-exists case. */ public CredentialReportGeneration generateCredentialReport() { - Instant now = Instant.now(); + Instant now = clock.instant(); Optional existing = credentialReports.get(credentialReportKey()); if (existing.isPresent() && now.isBefore(existing.get().getGeneratedTime().plus(CREDENTIAL_REPORT_MAX_AGE))) { return new CredentialReportGeneration("COMPLETE", @@ -6415,7 +6420,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { .orElseThrow(() -> new AwsException("ReportNotPresent", "The request was rejected because the credential report does not exist. " + "To generate a credential report, use GenerateCredentialReport.", 410)); - if (Instant.now().isAfter(report.getGeneratedTime().plus(CREDENTIAL_REPORT_MAX_AGE))) { + if (clock.instant().isAfter(report.getGeneratedTime().plus(CREDENTIAL_REPORT_MAX_AGE))) { throw new AwsException("ReportExpired", "The request was rejected because the most recent credential report has expired. " + "To generate a new credential report, use GenerateCredentialReport.", 410); diff --git a/src/test/java/io/floci/aws/services/iam/IamServiceTest.java b/src/test/java/io/floci/aws/services/iam/IamServiceTest.java index e8084ac619..c8beb97d63 100644 --- a/src/test/java/io/floci/aws/services/iam/IamServiceTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamServiceTest.java @@ -24,6 +24,7 @@ import io.floci.aws.services.iam.model.OrganizationRootFeatures; import io.floci.aws.services.iam.model.PolicyVersion; import io.floci.aws.services.iam.model.SessionCredential; import io.floci.aws.services.iam.model.VirtualMfaDevice; +import io.floci.aws.testing.ManualClock; import io.floci.aws.testing.MutableClock; import jakarta.enterprise.inject.Instance; import org.junit.jupiter.api.BeforeEach; @@ -96,6 +97,12 @@ class IamServiceTest { ); } + private static IamService iamServiceOn(Clock clock) { + return new IamService(new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), + new RegionResolver("us-east-1", "000000000000"), false, clock); + } + @Test void roleChainingRequiresALiveTokenMatchedRoleSession() { MutableClock clock = new MutableClock(); @@ -3662,17 +3669,21 @@ class IamServiceTest { * changes: otherwise a changed password would still report the original creation time. */ @Test - void updateLoginProfilePasswordChangeMovesPasswordLastChanged() throws InterruptedException { - iamService.createUser("cred-report-changed-user", "/"); - iamService.createLoginProfile("cred-report-changed-user", "Original-P4ss!", false); - Instant createdAt = iamService.getLoginProfile("cred-report-changed-user").getPasswordLastChanged(); + void updateLoginProfilePasswordChangeMovesPasswordLastChanged() { + ManualClock clock = new ManualClock(Instant.parse("2026-03-01T00:00:00Z")); + IamService service = iamServiceOn(clock); + service.createUser("cred-report-changed-user", "/"); + service.createLoginProfile("cred-report-changed-user", "Original-P4ss!", false); + LoginProfile created = service.getLoginProfile("cred-report-changed-user"); + assertEquals(clock.instant(), created.getCreateDate()); + assertEquals(clock.instant(), created.getPasswordLastChanged()); - Thread.sleep(20); - iamService.updateLoginProfile("cred-report-changed-user", "Updated-P4ss!", null); - Instant changedAt = iamService.getLoginProfile("cred-report-changed-user").getPasswordLastChanged(); + clock.advance(Duration.ofDays(30)); + service.updateLoginProfile("cred-report-changed-user", "Updated-P4ss!", null); - assertTrue(changedAt.isAfter(createdAt), - "expected password_last_changed to move: created=" + createdAt + " changed=" + changedAt); + LoginProfile changed = service.getLoginProfile("cred-report-changed-user"); + assertEquals(Instant.parse("2026-03-31T00:00:00Z"), changed.getPasswordLastChanged()); + assertEquals(Instant.parse("2026-03-01T00:00:00Z"), changed.getCreateDate()); } /** Toggling only passwordResetRequired is not a password change, so the timestamp must not move. */ @@ -3688,33 +3699,60 @@ class IamServiceTest { } @Test - void getCredentialReportOnAnExpiredReportThrowsReportExpired() { - StorageBackend credentialReports = new InMemoryStorage<>(); - credentialReports.put("credential-report/aws", - new CredentialReport("dGVzdA==", Instant.now().minus(Duration.ofHours(5)))); - IamService withExpiredReport = new IamService( - new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), - new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), - new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), - new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), - new InMemoryStorage<>(), credentialReports, new InMemoryStorage<>(), - new InMemoryStorage<>(), - new InMemoryStorage<>(), - new InMemoryStorage<>(), - new InMemoryStorage<>(), - new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), - new RegionResolver("us-east-1", "000000000000"), false, null); + void getCredentialReportServesAReportUntilItIsOlderThanFourHours() { + ManualClock clock = new ManualClock(Instant.parse("2026-03-01T00:00:00Z")); + IamService service = iamServiceOn(clock); + service.generateCredentialReport(); - AwsException ex = assertThrows(AwsException.class, withExpiredReport::getCredentialReport); + clock.advance(Duration.ofHours(4)); + assertEquals(Instant.parse("2026-03-01T00:00:00Z"), service.getCredentialReport().generatedTime()); + + clock.advance(Duration.ofMillis(1)); + AwsException ex = assertThrows(AwsException.class, service::getCredentialReport); assertEquals("ReportExpired", ex.getErrorCode()); } @Test - void generateCredentialReportOnAnExpiredReportStartsANewOne() { + void generateCredentialReportReusesAReportUntilItIsFourHoursOld() { + ManualClock clock = new ManualClock(Instant.parse("2026-03-01T00:00:00Z")); + IamService service = iamServiceOn(clock); + service.generateCredentialReport(); + + clock.advance(Duration.ofHours(4).minusMillis(1)); + IamService.CredentialReportGeneration reused = service.generateCredentialReport(); + assertEquals("COMPLETE", reused.state()); + assertEquals(Instant.parse("2026-03-01T00:00:00Z"), service.getCredentialReport().generatedTime()); + + clock.advance(Duration.ofMillis(1)); + IamService.CredentialReportGeneration regenerated = service.generateCredentialReport(); + assertEquals("STARTED", regenerated.state()); + assertEquals("The previous report has expired. Starting a new report generation task", + regenerated.description()); + assertEquals(Instant.parse("2026-03-01T04:00:00Z"), service.getCredentialReport().generatedTime()); + } + + @Test + void generateCredentialReportOnAnExpiredReportMakesItReadableAgain() { + ManualClock clock = new ManualClock(Instant.parse("2026-03-01T00:00:00Z")); + IamService service = iamServiceOn(clock); + service.generateCredentialReport(); + clock.advance(Duration.ofHours(4).plusMillis(1)); + assertThrows(AwsException.class, service::getCredentialReport); + + IamService.CredentialReportGeneration generation = service.generateCredentialReport(); + + assertEquals("STARTED", generation.state()); + assertEquals(clock.instant(), service.getCredentialReport().generatedTime()); + } + + /** A report read back from storage ages on the service's clock like one it generated itself. */ + @Test + void aStoredCredentialReportExpiresOnTheServicesClock() { + Instant start = Instant.parse("2026-03-01T00:00:00Z"); StorageBackend credentialReports = new InMemoryStorage<>(); - credentialReports.put("credential-report/aws", - new CredentialReport("dGVzdA==", Instant.now().minus(Duration.ofHours(5)))); - IamService withExpiredReport = new IamService( + credentialReports.put("credential-report/aws", new CredentialReport("dGVzdA==", start)); + ManualClock clock = new ManualClock(start.plus(Duration.ofHours(4))); + IamService withStoredReport = new IamService( new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), @@ -3725,14 +3763,13 @@ class IamServiceTest { new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(), - new RegionResolver("us-east-1", "000000000000"), false, null); + new RegionResolver("us-east-1", "000000000000"), false, null, clock); - IamService.CredentialReportGeneration generation = withExpiredReport.generateCredentialReport(); + assertEquals(start, withStoredReport.getCredentialReport().generatedTime()); - assertEquals("STARTED", generation.state()); - assertEquals("The previous report has expired. Starting a new report generation task", - generation.description()); - assertDoesNotThrow(withExpiredReport::getCredentialReport); + clock.advance(Duration.ofMillis(1)); + AwsException ex = assertThrows(AwsException.class, withStoredReport::getCredentialReport); + assertEquals("ReportExpired", ex.getErrorCode()); } @Test