feat(iam): add ECS task-role session registration and orphan sweep (#4143)

Adds SessionCredential.ecsTaskArn and IamService.registerEcsTaskRoleSession/
sweepOrphanedEcsTaskRoleSessions, mirroring the EC2 instance-session pattern
(registerEc2InstanceSession/sweepOrphanedEc2InstanceSessions): a task-role
session lives in the same session store as any other temporary credential,
tagged by the exact task ARN it was minted for, and none of it survives a
Floci restart since no ECS task does either.

Nothing calls these yet. First piece of ECS task-role credential vending,
split out on its own since the rest (the credential issuer, its endpoint,
and how tasks reach it) is substantial enough to review in separate pieces.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-22 07:38:51 -05:00
1 parent 2f5aa99701
commit eca5a1bb14
3 files changed
+70

No files matched your search

@@ -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()) {
@@ -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; }
}
@@ -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
// =========================================================================