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

@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.eks;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.Pagination;
@@ -120,7 +121,7 @@ public class EksAddonService {
}
if (request.serviceAccountRoleArn() != null && !request.serviceAccountRoleArn().isBlank()) {
validateRoleArn(cluster, request.serviceAccountRoleArn(), "serviceAccountRoleArn");
validateRoleArn(request.serviceAccountRoleArn(), "serviceAccountRoleArn");
}
if (request.resolveConflicts() != null && !request.resolveConflicts().isBlank()) {
@@ -221,7 +222,7 @@ public class EksAddonService {
if (request.serviceAccountRoleArn() != null) {
if (!request.serviceAccountRoleArn().isBlank()) {
validateRoleArn(cluster, request.serviceAccountRoleArn(), "serviceAccountRoleArn");
validateRoleArn(request.serviceAccountRoleArn(), "serviceAccountRoleArn");
}
params.add(new UpdateParam("ServiceAccountRoleArn", request.serviceAccountRoleArn()));
}
@@ -383,7 +384,7 @@ public class EksAddonService {
throw new AwsException("InvalidParameterException",
"roleArn and serviceAccount are required for podIdentityAssociations", 400);
}
validateRoleArn(cluster, assoc.roleArn(), "roleArn");
validateRoleArn(assoc.roleArn(), "roleArn");
}
}
@@ -469,13 +470,12 @@ public class EksAddonService {
}
}
private void validateRoleArn(Cluster cluster, String roleArn, String fieldName) {
private void validateRoleArn(String roleArn, String fieldName) {
if (roleArn == null || roleArn.isBlank()) {
throw new AwsException("InvalidParameterException", fieldName + " is required", 400);
}
String[] arn = roleArn.split(":", 6);
String[] clusterArn = cluster.getArn().split(":", 6);
if (arn.length != 6 || !"arn".equals(arn[0]) || !arn[1].equals(clusterArn[1])
if (arn.length != 6 || !"arn".equals(arn[0]) || !arn[1].matches(AwsArnUtils.PARTITION_REGEX)
|| !"iam".equals(arn[2]) || !arn[3].isEmpty() || !arn[4].matches("[0-9]{12}")
|| !arn[5].startsWith("role/") || arn[5].startsWith("role/aws-service-role/")
|| roleArn.endsWith("/")) {
@@ -484,7 +484,7 @@ public class EksAddonService {
if (iam != null) {
String roleName = roleArn.substring(roleArn.lastIndexOf('/') + 1);
iam.findRole(arn[4], roleName)
.filter(role -> roleArn.equals(role.getArn()))
.filter(role -> IamService.roleArnMatches(roleArn, role))
.orElseThrow(() -> new AwsException("InvalidParameterException", "Role not found: " + roleArn, 400));
}
}
@@ -84,10 +84,10 @@ public class EksPodIdentityAssociationService {
if (roleArn == null || roleArn.isBlank()) {
throw invalid("roleArn is required");
}
validateRoleArn(cluster, roleArn, "roleArn");
validateRoleArn(roleArn, "roleArn");
if (request.targetRoleArn() != null && !request.targetRoleArn().isBlank()) {
validateTargetRoleArn(cluster, request.targetRoleArn());
validateTargetRoleArn(request.targetRoleArn());
}
Map<String, String> tags = request.tags() == null ? Map.of() : request.tags();
@@ -194,12 +194,12 @@ public class EksPodIdentityAssociationService {
String newRoleArn = current.roleArn();
if (request.roleArn() != null) {
validateRoleArn(cluster, request.roleArn(), "roleArn");
validateRoleArn(request.roleArn(), "roleArn");
newRoleArn = request.roleArn();
}
String newTargetRoleArn = current.targetRoleArn();
if (request.targetRoleArn() != null) {
validateTargetRoleArn(cluster, request.targetRoleArn());
validateTargetRoleArn(request.targetRoleArn());
newTargetRoleArn = request.targetRoleArn();
}
Boolean newDisableSessionTags = request.disableSessionTags() != null
@@ -313,16 +313,14 @@ public class EksPodIdentityAssociationService {
}
}
/** The partition role ARNs must share with the cluster: its own, or the deployment's without an ARN. */
private String clusterPartition(Cluster cluster) {
return AwsArnUtils.partitionOrDefault(cluster != null ? cluster.getArn() : null,
regionResolver.getDefaultPartition());
}
private void validateRoleArn(Cluster cluster, String roleArn, String fieldName) {
/**
* The role must exist in the account the ARN names. Its partition only has to be well formed:
* an account keeps one IAM namespace whichever partition a request is for, and STS AssumeRole
* accepts the same role ARNs through {@link IamService#roleArnMatches}.
*/
private void validateRoleArn(String roleArn, String fieldName) {
String[] arn = roleArn.split(":", 6);
String expectedPartition = clusterPartition(cluster);
if (arn.length != 6 || !"arn".equals(arn[0]) || !arn[1].equals(expectedPartition)
if (arn.length != 6 || !"arn".equals(arn[0]) || !arn[1].matches(AwsArnUtils.PARTITION_REGEX)
|| !"iam".equals(arn[2]) || !arn[3].isEmpty() || !arn[4].matches("[0-9]{12}")
|| !arn[5].startsWith("role/") || arn[5].startsWith("role/aws-service-role/")
|| roleArn.endsWith("/")) {
@@ -330,14 +328,13 @@ public class EksPodIdentityAssociationService {
}
String roleName = roleArn.substring(roleArn.lastIndexOf('/') + 1);
iam.findRole(arn[4], roleName)
.filter(role -> roleArn.equals(role.getArn()))
.filter(role -> IamService.roleArnMatches(roleArn, role))
.orElseThrow(() -> invalid("IAM role does not exist"));
}
private void validateTargetRoleArn(Cluster cluster, String targetRoleArn) {
private static void validateTargetRoleArn(String targetRoleArn) {
String[] arn = targetRoleArn.split(":", 6);
String expectedPartition = clusterPartition(cluster);
if (arn.length != 6 || !"arn".equals(arn[0]) || !arn[1].equals(expectedPartition)
if (arn.length != 6 || !"arn".equals(arn[0]) || !arn[1].matches(AwsArnUtils.PARTITION_REGEX)
|| !"iam".equals(arn[2]) || !arn[3].isEmpty() || !arn[4].matches("[0-9]{12}")
|| !arn[5].startsWith("role/") || targetRoleArn.endsWith("/")) {
throw invalid("targetRoleArn must identify an IAM role");
@@ -136,12 +136,24 @@ public class EksPodIdentityCredentialsController {
String sessionName = podIdentitySessionName(cluster.getName(), association.serviceAccount());
String roleName = roleArn.contains("/") ? roleArn.substring(roleArn.lastIndexOf('/') + 1) : roleArn;
Optional<IamRole> role = iamService.findRole(roleAccountId, roleName);
// EKS Auth assumes the association's role through STS, so a role that is gone gets no
// credentials. The lookup is by name only, and enforcement loads a session's policies the
// same way, so a role recreated under another path is refused too: it is not the role the
// association names.
Optional<IamRole> role = iamService.findRole(roleAccountId, roleName)
.filter(found -> IamService.roleArnMatches(roleArn, found));
if (role.isEmpty()) {
return error(400, "AccessDeniedException: The role of the association cannot be assumed: "
+ roleArn + "\n");
}
String assumedRoleId = role.map(IamRole::getRoleId).filter(id -> !id.isBlank())
.map(id -> id + ":" + sessionName)
.orElse(null);
iamService.registerSession(accessKeyId, secretAccessKey, sessionToken, roleArn,
// Like STS AssumeRole, the session names the stored role, so it lives in the role's
// partition even when the association was made with the role's ARN in another partition.
String sessionRoleArn = role.map(IamRole::getArn).filter(AwsArnUtils::isArn).orElse(roleArn);
iamService.registerSession(accessKeyId, secretAccessKey, sessionToken, sessionRoleArn,
expiration, association.policy(), callerAccountId, sessionName, assumedRoleId);
EksPodIdentityCredentialsResponse response = new EksPodIdentityCredentialsResponse(
@@ -1078,6 +1078,34 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
+ " at 'roleName' failed to satisfy constraint: " + constraint, 400);
}
/**
* Whether {@code requestedRoleArn} names {@code role}: same account, same path and name, no region.
*
* <p>The ARN's partition only has to be well formed. One process serves several partitions but
* keeps one IAM namespace per account, so a role ARN from another partition resolves to the
* account's one role (see the partitions guide). STS AssumeRole and EKS Pod Identity both match
* role ARNs through here so they accept the same ARNs.
*/
public static boolean roleArnMatches(String requestedRoleArn, IamRole role) {
if (requestedRoleArn == null || role == null || !AwsArnUtils.isArn(requestedRoleArn)) {
return false;
}
AwsArnUtils.Arn requested = AwsArnUtils.parse(requestedRoleArn);
if (!"iam".equals(requested.service())
|| !requested.partition().matches(AwsArnUtils.PARTITION_REGEX)) {
return false;
}
String storedArn = role.getArn();
if (storedArn != null && AwsArnUtils.isArn(storedArn)) {
AwsArnUtils.Arn stored = AwsArnUtils.parse(storedArn);
return requested.accountId().equals(stored.accountId())
&& requested.region().equals(stored.region())
&& requested.resource().equals(stored.resource());
}
String expectedResource = "role" + normalizePath(role.getPath()) + role.getRoleName();
return requested.region().isEmpty() && requested.resource().equals(expectedResource);
}
/**
* AWS publishes UnmodifiableEntity on twelve role actions, and its message names the linked
* service the caller has to go through instead. This guards the eleven of them the emulator
@@ -177,7 +177,7 @@ public class StsQueryHandler {
.orElse(regionResolver.buildGlobalArn("iam", callerAccount, "root"));
String principalArn = callerArns.map(IamService.CallerArns::principalArn).orElse(callerArn);
if (role == null || !roleArnMatches(roleArn, role)) {
if (role == null || !IamService.roleArnMatches(roleArn, role)) {
return AwsQueryResponse.error("AccessDenied",
"User: " + callerArn + " is not authorized to perform: sts:AssumeRole on resource: " + roleArn,
AwsNamespaces.STS, 403);
@@ -210,26 +210,6 @@ public class StsQueryHandler {
"role" + IamService.normalizePath(role.getPath()) + roleName);
}
static boolean roleArnMatches(String requestedRoleArn, IamRole role) {
if (requestedRoleArn == null || role == null || !AwsArnUtils.isArn(requestedRoleArn)) {
return false;
}
AwsArnUtils.Arn requested = AwsArnUtils.parse(requestedRoleArn);
if (!"iam".equals(requested.service())
|| !requested.partition().matches(AwsArnUtils.PARTITION_REGEX)) {
return false;
}
String storedArn = role.getArn();
if (storedArn != null && AwsArnUtils.isArn(storedArn)) {
AwsArnUtils.Arn stored = AwsArnUtils.parse(storedArn);
return requested.accountId().equals(stored.accountId())
&& requested.region().equals(stored.region())
&& requested.resource().equals(stored.resource());
}
String expectedResource = "role" + IamService.normalizePath(role.getPath()) + role.getRoleName();
return requested.region().isEmpty() && requested.resource().equals(expectedResource);
}
private Response handleGetCallerIdentity(MultivaluedMap<String, String> params) {
String accountId = regionResolver.getAccountId();
String authorization = headers == null ? null : headers.getHeaderString("Authorization");
@@ -298,7 +278,7 @@ public class StsQueryHandler {
IamRole role = outcome.role();
if (role == null) {
role = iamService.findRole(accountId, roleName).orElse(null);
if (role == null || !roleArnMatches(roleArn, role)) {
if (role == null || !IamService.roleArnMatches(roleArn, role)) {
return accessDenied(roleArn);
}
}
@@ -406,7 +386,7 @@ public class StsQueryHandler {
}
Optional<IamRole> role = iamService.findRole(roleAccountId, roleName);
if (role.isEmpty() || !roleArnMatches(roleArn, role.get())) {
if (role.isEmpty() || !IamService.roleArnMatches(roleArn, role.get())) {
return WebIdentityOutcome.deny(accessDenied(roleArn));
}