fix(eks): accept a role ARN from another partition the way STS does (#5398)

* fix(eks): accept a role ARN from another partition the way STS does

An account keeps one IAM namespace whichever partition a request is for
(partitions.md, Known gaps). STS AssumeRole already resolved a role ARN
from another partition to the account's role, while EKS Pod Identity and
add-ons required the cluster's partition and an exact stored-ARN match.

Move the STS role-ARN matcher to IamService.roleArnMatches and use it for
the Pod Identity roleArn and the add-on role ARNs; targetRoleArn accepts
any well-formed partition. The Pod Identity session names the stored
role's ARN, as an STS session does. The EKS model sets no pattern on
roleArn or targetRoleArn.

* fix(eks): keep a role recreated under another path out of a pod identity session

The credentials endpoint finds the association's role by name only. Since
the session started naming the stored role's ARN, a role deleted and
recreated under another path gave the session its ARN and role id through
the old association. The found role now has to match the association's
role ARN the way roleArnMatches does everywhere else; one that does not is
treated as absent, as before this branch.

The association test also checks that a rejected role update leaves the
stored role ARN as it was.

* fix(eks): refuse pod identity credentials when the association's role is gone

EKS Auth assumes the association's role through STS, so a role that no
longer exists gets no credentials. The endpoint still issued them, and
since enforcement loads a session's policies by role name, a pod whose
role was recreated under another path ran with the replacement's policies.

A role that is missing, or that does not match the association's role ARN,
is now a 400 AccessDeniedException and no session is registered.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-10 14:54:12 -04:00
1 parent 29319fbfb5
commit fb202cce6a
10 files changed
+177 -64

No files matched your search

@@ -286,6 +286,32 @@ class EksPodIdentityAssociationServiceTest {
+ created.associationId(), created.associationArn());
}
@Test
void aRoleArnFromAnotherPartitionResolvesTheAccountsRoleTheWayStsDoes() {
Fixture fixture = fixture();
String chinaRole = "arn:aws-cn:iam::123456789012:role/pod-role";
String govTarget = "arn:aws-us-gov:iam::210987654321:role/target-role";
PodIdentityAssociation created = fixture.service.create(fixture.cluster, new CreatePodIdentityAssociationRequest(
fixture.cluster.getName(), NAMESPACE, SERVICE_ACCOUNT, chinaRole, null, Map.of(), govTarget,
null, null));
assertEquals(chinaRole, created.roleArn());
assertEquals(govTarget, created.targetRoleArn());
PodIdentityAssociation updated = fixture.service.update(fixture.cluster, created.associationId(),
new UpdatePodIdentityAssociationRequest(ROLE, null, null, null, null));
assertEquals(ROLE, updated.roleArn());
for (String rejected : List.of("arn:aws-cn:iam::123456789012:role/team/pod-role",
"arn:aws-cn:iam::111111111111:role/pod-role", "arn:bogus:iam::123456789012:role/pod-role")) {
AwsException ex = assertThrows(AwsException.class, () -> fixture.service.update(fixture.cluster,
created.associationId(), new UpdatePodIdentityAssociationRequest(rejected, null, null, null, null)),
rejected);
assertEquals("InvalidParameterException", ex.getErrorCode(), rejected);
assertEquals(ROLE, fixture.service.describe(fixture.cluster, created.associationId()).roleArn(), rejected);
}
}
private static Fixture fixture() {
Cluster cluster = new Cluster();
cluster.setName("test-cluster");
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.services.eks.model.ClusterStatus;
import io.github.hectorvent.floci.services.eks.model.EksPodIdentityCredentialsResponse;
import io.github.hectorvent.floci.services.eks.model.PodIdentityAssociation;
import io.github.hectorvent.floci.services.iam.IamService;
import io.github.hectorvent.floci.services.iam.model.IamRole;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -29,6 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
@@ -92,6 +94,8 @@ class EksPodIdentityCredentialsControllerTest {
);
when(associationService.findAssociation(cluster, NAMESPACE, SERVICE_ACCOUNT))
.thenReturn(Optional.of(association));
when(iamService.findRole(ACCOUNT_ID, "app-role"))
.thenReturn(Optional.of(new IamRole("AROA-app", "app-role", "/", ROLE_ARN, "{}")));
}
private static KeyPair newKeyPair() throws GeneralSecurityException {
@@ -145,6 +149,48 @@ class EksPodIdentityCredentialsControllerTest {
assertTrue(sessionNameCaptor.getValue().startsWith("eks-" + CLUSTER_NAME + "-" + SERVICE_ACCOUNT + "-"));
}
@Test
void aSessionOnARoleArnFromAnotherPartitionNamesTheStoredRoleLikeSts() throws Exception {
String chinaRoleArn = "arn:aws-cn:iam::123456789012:role/app-role";
PodIdentityAssociation association = new PodIdentityAssociation(CLUSTER_NAME, NAMESPACE, SERVICE_ACCOUNT,
chinaRoleArn, "arn:aws:eks:us-east-1:" + ACCOUNT_ID + ":podidentityassociation/" + CLUSTER_NAME
+ "/assoc-1", "assoc-1", null, 1000.0, 1000.0, null, null, false, null, null);
when(associationService.findAssociation(cluster, NAMESPACE, SERVICE_ACCOUNT))
.thenReturn(Optional.of(association));
when(iamService.findRole(ACCOUNT_ID, "app-role"))
.thenReturn(Optional.of(new IamRole("AROA-app", "app-role", "/", ROLE_ARN, "{}")));
Response response = controller.getCredentials("Bearer " + validToken());
assertEquals(200, response.getStatus());
verify(iamService).registerSession(any(), any(), any(), eq(ROLE_ARN), any(Instant.class), eq(null),
eq(ACCOUNT_ID), any(), any());
}
@Test
void aRoleRecreatedUnderAnotherPathGetsNoCredentials() throws Exception {
String replacementArn = "arn:aws:iam::" + ACCOUNT_ID + ":role/team/app-role";
when(iamService.findRole(ACCOUNT_ID, "app-role"))
.thenReturn(Optional.of(new IamRole("AROA-replacement", "app-role", "/team/", replacementArn, "{}")));
Response response = controller.getCredentials("Bearer " + validToken());
assertEquals(400, response.getStatus());
assertTrue(response.getEntity().toString().startsWith("AccessDeniedException"));
verify(iamService, never()).registerSession(any(), any(), any(), any(), any(), any(), any(), any(), any());
}
@Test
void aDeletedRoleGetsNoCredentials() throws Exception {
when(iamService.findRole(ACCOUNT_ID, "app-role")).thenReturn(Optional.empty());
Response response = controller.getCredentials("Bearer " + validToken());
assertEquals(400, response.getStatus());
assertTrue(response.getEntity().toString().startsWith("AccessDeniedException"));
verify(iamService, never()).registerSession(any(), any(), any(), any(), any(), any(), any(), any(), any());
}
@Test
void supportsRawTokenWithoutBearerPrefix() throws Exception {
Response response = controller.getCredentials(validToken());
@@ -57,10 +57,16 @@ class StsQueryHandlerTest {
}
private static StsQueryHandler newHandlerForRegion(RegionResolver regionResolver) {
return newHandler(null, regionResolver);
return newHandler((IamRole) null, regionResolver);
}
private static StsQueryHandler newHandler(IamRole role, RegionResolver regionResolver) {
IamService iamService = mock(IamService.class);
when(iamService.findRole(anyString(), anyString())).thenReturn(Optional.ofNullable(role));
return newHandler(iamService, regionResolver);
}
private static StsQueryHandler newHandler(IamService iamService, RegionResolver regionResolver) {
EmulatorConfig config = mock(EmulatorConfig.class);
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
EmulatorConfig.IamServiceConfig iam = mock(EmulatorConfig.IamServiceConfig.class);
@@ -68,9 +74,6 @@ class StsQueryHandlerTest {
when(services.iam()).thenReturn(iam);
when(iam.enforcementEnabled()).thenReturn(false);
IamService iamService = mock(IamService.class);
when(iamService.findRole(anyString(), anyString())).thenReturn(Optional.ofNullable(role));
return new StsQueryHandler(
iamService,
mock(AccountResolver.class),
@@ -178,6 +181,24 @@ class StsQueryHandlerTest {
"AssumedRoleUser Arn must use the stored role's partition");
}
@Test
void assumeRoleOnARoleArnFromAnotherPartitionRegistersTheAccountsOneRole() {
String storedArn = "arn:aws:iam::000000000000:role/TestRole";
IamService iamService = mock(IamService.class);
when(iamService.findRole("000000000000", "TestRole"))
.thenReturn(Optional.of(new IamRole("AROA-test", "TestRole", "/", storedArn, "{}")));
StsQueryHandler handler = newHandler(iamService, new RegionResolver(REGION, "000000000000"));
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
params.putSingle("RoleArn", "arn:aws-cn:iam::000000000000:role/TestRole");
params.putSingle("RoleSessionName", "test-session");
Response response = handler.handle("AssumeRole", params);
assertEquals(200, response.getStatus(), (String) response.getEntity());
verify(iamService).registerSession(anyString(), anyString(), anyString(), eq(storedArn), any(),
any(), eq("000000000000"), eq("test-session"), eq("AROA-test:test-session"));
}
@Test
void roleArnMatchesValidatesPartitionsAndPaths() {
IamRole role = new IamRole();
@@ -185,16 +206,16 @@ class StsQueryHandlerTest {
role.setPath("/team/");
role.setArn("arn:aws:iam::000000000000:role/team/TestRole");
assertTrue(StsQueryHandler.roleArnMatches("arn:aws:iam::000000000000:role/team/TestRole", role));
assertTrue(StsQueryHandler.roleArnMatches("arn:aws-cn:iam::000000000000:role/team/TestRole", role));
assertTrue(StsQueryHandler.roleArnMatches("arn:aws-us-gov:iam::000000000000:role/team/TestRole", role));
assertTrue(IamService.roleArnMatches("arn:aws:iam::000000000000:role/team/TestRole", role));
assertTrue(IamService.roleArnMatches("arn:aws-cn:iam::000000000000:role/team/TestRole", role));
assertTrue(IamService.roleArnMatches("arn:aws-us-gov:iam::000000000000:role/team/TestRole", role));
assertFalse(StsQueryHandler.roleArnMatches("arn:aws:iam::000000000000:role/other/TestRole", role));
assertFalse(StsQueryHandler.roleArnMatches("arn:aws:iam::111111111111:role/team/TestRole", role));
assertFalse(StsQueryHandler.roleArnMatches("arn:aws:iam:us-east-1:000000000000:role/team/TestRole", role));
assertFalse(StsQueryHandler.roleArnMatches("arn:aws:s3:::bucket", role));
assertFalse(StsQueryHandler.roleArnMatches("not-an-arn", role));
assertFalse(StsQueryHandler.roleArnMatches(null, role));
assertFalse(IamService.roleArnMatches("arn:aws:iam::000000000000:role/other/TestRole", role));
assertFalse(IamService.roleArnMatches("arn:aws:iam::111111111111:role/team/TestRole", role));
assertFalse(IamService.roleArnMatches("arn:aws:iam:us-east-1:000000000000:role/team/TestRole", role));
assertFalse(IamService.roleArnMatches("arn:aws:s3:::bucket", role));
assertFalse(IamService.roleArnMatches("not-an-arn", role));
assertFalse(IamService.roleArnMatches(null, role));
}
@ParameterizedTest