Light, fluffy, and always free - The AWS Local Emulator alternative
feat(iam): add ECS task-role session registration and orphan sweep (#4143)
Priamo Germoseneca5a1bb140e
diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamService.javaindex 2c0bac45f0..9140c1a4ee 100644--- a/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java+++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java@@ -2207,6 +2207,33 @@ public class IamService implements SessionAccountLookup, ResourceProvider {return removed;}+ /** Registers an ECS task-role session, outside request scope. */+ public void registerEcsTaskRoleSession(SessionCredential session) {+ if (session.getOriginAccountId() == null || session.getOriginAccountId().isBlank()+ || session.getEcsTaskArn() == null || session.getEcsTaskArn().isBlank()) {+ throw new IllegalArgumentException("ECS task session account and task ARN must not be blank");+ }+ if (sessions instanceof AccountAwareStorageBackend<SessionCredential> aware) {+ aware.putForAccount(session.getOriginAccountId(), session.getAccessKeyId(), session);+ } else {+ sessions.put(session.getAccessKeyId(), session);+ }+ }++ /** No ECS task survives a Floci restart; discard credentials from the previous process. */+ public int sweepOrphanedEcsTaskRoleSessions() {+ List<SessionCredential> stored = sessions instanceof AccountAwareStorageBackend<SessionCredential> aware+ ? aware.scanAllAccounts() : sessions.scan(key -> true);+ int removed = 0;+ for (SessionCredential session : stored) {+ if (session.getEcsTaskArn() != null) {+ deleteSession(session.getAccessKeyId(), session);+ removed++;+ }+ }+ return removed;+ }+/** Removes a session from an explicit account namespace. */public void unregisterSession(String accountId, String sessionAccessKeyId) {if (sessionAccessKeyId == null || sessionAccessKeyId.isBlank()) {diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/model/SessionCredential.java b/src/main/java/io/github/hectorvent/floci/services/iam/model/SessionCredential.javaindex 412119ea60..fba150c783 100644--- a/src/main/java/io/github/hectorvent/floci/services/iam/model/SessionCredential.java+++ b/src/main/java/io/github/hectorvent/floci/services/iam/model/SessionCredential.java@@ -25,6 +25,8 @@ public class SessionCredential {private boolean lambdaExecutionRole;private String ec2InstanceId;private String ec2RoleId;+ /** Exact ECS task ARN this session was minted for, when this is a task-role session. */+ private String ecsTaskArn;public SessionCredential() {}@@ -99,6 +101,9 @@ public class SessionCredential {public String getEc2InstanceId() { return ec2InstanceId; }public void setEc2InstanceId(String ec2InstanceId) { this.ec2InstanceId = ec2InstanceId; }+ public String getEcsTaskArn() { return ecsTaskArn; }+ public void setEcsTaskArn(String ecsTaskArn) { this.ecsTaskArn = ecsTaskArn; }+public boolean isLambdaExecutionRole() { return lambdaExecutionRole; }public void setLambdaExecutionRole(boolean lambdaExecutionRole) { this.lambdaExecutionRole = lambdaExecutionRole; }}diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamServiceTest.javaindex ea2ac5912d..484e1d0fe8 100644--- a/src/test/java/io/github/hectorvent/floci/services/iam/IamServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamServiceTest.java@@ -102,6 +102,44 @@ class IamServiceTest {assertTrue(stored.getForAccount("123456789012", restored.getAccessKeyId()).isEmpty());}+ @Test+ void ecsTaskRoleSessionUsesTaskIdentityTokenAndOwningAccount() {+ SessionCredential session = new SessionCredential("ASIAECSSESSION", "secret", "token",+ "arn:aws:iam::123456789012:role/path/worker", Instant.now().plusSeconds(3600), null, "123456789012");+ session.setEcsTaskArn("arn:aws:ecs:us-east-1:123456789012:task/cluster/abc123");+ AccountAwareStorageBackend<SessionCredential> stored = new AccountAwareStorageBackend<>(+ new InMemoryStorage<>(), null, "000000000000");+ iamService = iamService(false, new InMemoryStorage<>(), stored);+ iamService.registerEcsTaskRoleSession(session);+ assertTrue(stored.getForAccount("000000000000", session.getAccessKeyId()).isEmpty());+ assertEquals("arn:aws:ecs:us-east-1:123456789012:task/cluster/abc123",+ stored.getForAccount("123456789012", session.getAccessKeyId()).orElseThrow().getEcsTaskArn());++ iamService.registerSession("ASIAOTHER", "other-secret", "other-token", session.getRoleArn(),+ Instant.now().plusSeconds(3600), null);+ assertEquals(1, iamService.sweepOrphanedEcsTaskRoleSessions());+ assertTrue(iamService.findSecretKey(session.getAccessKeyId(), "token").isEmpty());+ assertTrue(iamService.findSecretKey("ASIAOTHER", "other-token").isPresent());+ }++ @Test+ void ecsTaskRoleSessionMarkerSurvivesPersistenceAndExpiredCredentialsCannotAuthenticate() throws Exception {+ SessionCredential expired = new SessionCredential("ASIAEXPIREDECS", "secret", "token",+ "arn:aws:iam::123456789012:role/worker", Instant.now().minusSeconds(1), null, "123456789012");+ expired.setEcsTaskArn("arn:aws:ecs:us-east-1:123456789012:task/cluster/expired");+ ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();+ SessionCredential restored = mapper.readValue(mapper.writeValueAsBytes(expired), SessionCredential.class);+ assertEquals("arn:aws:ecs:us-east-1:123456789012:task/cluster/expired", restored.getEcsTaskArn());+ AccountAwareStorageBackend<SessionCredential> stored = new AccountAwareStorageBackend<>(+ new InMemoryStorage<>(), null, "000000000000");+ IamService service = iamService(false, new InMemoryStorage<>(), stored);+ service.registerEcsTaskRoleSession(restored);+ assertTrue(service.findSecretKey(restored.getAccessKeyId(), "token").isEmpty());+ IamService restarted = iamService(false, new InMemoryStorage<>(), stored);+ assertEquals(1, restarted.sweepOrphanedEcsTaskRoleSessions());+ assertTrue(stored.getForAccount("123456789012", restored.getAccessKeyId()).isEmpty());+ }+// =========================================================================// Users// =========================================================================