From 15fb050d91c0c0fa517a8794c4c4f5091afef2cf Mon Sep 17 00:00:00 2001 From: XuJian <265865031+xujiantop-crypto@users.noreply.github.com> Date: Thu, 15 Oct 2026 21:39:54 +0800 Subject: [PATCH] fix(eks): scope pod identity to the cluster account (#5696) * fix(eks): scope pod identity to the cluster account * test(eks): remove invalid cross-account pod identity association --- .../EksPodIdentityCredentialsController.java | 16 +- .../services/eks/EksPodIdentityWebhook.java | 14 +- .../EksPodIdentityAccountIntegrationTest.java | 151 ++++++++++++++++++ 3 files changed, 169 insertions(+), 12 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/eks/EksPodIdentityAccountIntegrationTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityCredentialsController.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityCredentialsController.java index 11199e1713..6875099ebc 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityCredentialsController.java +++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityCredentialsController.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.eks; import io.github.hectorvent.floci.core.common.AwsArnUtils; +import io.github.hectorvent.floci.core.common.RequestScopes; import io.github.hectorvent.floci.core.common.WebIdentityToken; import io.github.hectorvent.floci.core.common.WebIdentityTokenVerifier; import io.github.hectorvent.floci.core.common.WebIdentityTokenVerifier.ExpiredTokenException; @@ -117,8 +118,9 @@ public class EksPodIdentityCredentialsController { String namespace = rest.substring(0, colon); String serviceAccount = rest.substring(colon + 1); - Optional maybeAssociation = associationService.findAssociation( - cluster, namespace, serviceAccount); + String callerAccountId = AwsArnUtils.accountOrDefault(cluster.getArn(), cluster.getAccountId()); + Optional maybeAssociation = RequestScopes.callAs(callerAccountId, + () -> associationService.findAssociation(cluster, namespace, serviceAccount)); if (maybeAssociation.isEmpty()) { return error(404, "ResourceNotFoundException: No association found for service account: " + serviceAccount + "\n"); @@ -126,7 +128,6 @@ public class EksPodIdentityCredentialsController { PodIdentityAssociation association = maybeAssociation.get(); String roleArn = association.roleArn(); - String callerAccountId = cluster.getAccountId(); String roleAccountId = AwsArnUtils.accountOrDefault(roleArn, callerAccountId); String accessKeyId = "ASIA" + randomId(16); @@ -150,11 +151,12 @@ public class EksPodIdentityCredentialsController { .map(id -> id + ":" + sessionName) .orElse(null); - // 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. + // Like STS AssumeRole, store the session in the minting account while its role ARN names + // the assumed role, including the role's AWS partition when that differs. String sessionRoleArn = role.map(IamRole::getArn).filter(AwsArnUtils::isArn).orElse(roleArn); - iamService.registerSession(accessKeyId, secretAccessKey, sessionToken, sessionRoleArn, - expiration, association.policy(), callerAccountId, sessionName, assumedRoleId); + RequestScopes.runAs(callerAccountId, () -> iamService.registerSession( + accessKeyId, secretAccessKey, sessionToken, sessionRoleArn, + expiration, association.policy(), callerAccountId, sessionName, assumedRoleId)); EksPodIdentityCredentialsResponse response = new EksPodIdentityCredentialsResponse( accessKeyId, diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityWebhook.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityWebhook.java index 3db085d3e3..1501457f8d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityWebhook.java +++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityWebhook.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.eks; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RequestScopes; import io.github.hectorvent.floci.services.eks.model.Cluster; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; @@ -100,13 +101,16 @@ public class EksPodIdentityWebhook { return List.of(); } Optional cluster = eks.findAuthenticationCluster(accountId, clusterName); - if (cluster.isEmpty() || !podIdentityAgentInstalled(cluster.get())) { + if (cluster.isEmpty()) { return List.of(); } - if (!hasAssociation(cluster.get(), namespace, serviceAccountOf(pod))) { - return List.of(); - } - return buildPatch(pod); + return RequestScopes.callAs(accountId, () -> { + if (!podIdentityAgentInstalled(cluster.get()) + || !hasAssociation(cluster.get(), namespace, serviceAccountOf(pod))) { + return List.of(); + } + return buildPatch(pod); + }); } private boolean podIdentityAgentInstalled(Cluster cluster) { diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksPodIdentityAccountIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksPodIdentityAccountIntegrationTest.java new file mode 100644 index 0000000000..10748f0ff2 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksPodIdentityAccountIntegrationTest.java @@ -0,0 +1,151 @@ +package io.github.hectorvent.floci.services.eks; + +import io.github.hectorvent.floci.core.common.RequestScopes; +import io.github.hectorvent.floci.services.eks.model.Cluster; +import io.github.hectorvent.floci.services.eks.model.ClusterIdentity; +import io.github.hectorvent.floci.services.eks.model.ClusterStatus; +import io.github.hectorvent.floci.services.eks.model.CreateAddonRequest; +import io.github.hectorvent.floci.services.eks.model.CreatePodIdentityAssociationRequest; +import io.github.hectorvent.floci.services.eks.model.OidcIdentity; +import io.github.hectorvent.floci.services.iam.IamService; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import org.junit.jupiter.api.Test; + +import java.time.Instant; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +class EksPodIdentityAccountIntegrationTest { + + private static final String DEFAULT_ACCOUNT = "000000000000"; + private static final String OTHER_ACCOUNT = "123456789012"; + private static final String NAMESPACE = "payments"; + private static final String SERVICE_ACCOUNT = "checkout"; + + @Inject + EksService eksService; + + @Inject + EksOidcService oidcService; + + @Inject + EksAddonService addonService; + + @Inject + EksPodIdentityAssociationService associationService; + + @Inject + IamService iamService; + + @Test + void anonymousPodIdentityCallsUseTheClustersAccount() { + String name = "pi-account-" + UUID.randomUUID().toString().substring(0, 8); + String roleName = "role-" + name; + try { + Cluster defaultCluster = createCluster(DEFAULT_ACCOUNT, name); + Cluster otherCluster = createCluster(OTHER_ACCOUNT, name); + createPodIdentityResources(DEFAULT_ACCOUNT, defaultCluster, roleName); + + // A same-named cluster in another account must not see the default account's resources. + review(name, OTHER_ACCOUNT, false); + String otherToken = mintToken(OTHER_ACCOUNT, otherCluster); + given().header("Authorization", otherToken) + .get("/v1/credentials") + .then().statusCode(404); + + createPodIdentityResources(OTHER_ACCOUNT, otherCluster, roleName); + review(name, DEFAULT_ACCOUNT, true); + review(name, OTHER_ACCOUNT, true); + assertCredentialsBelongTo(DEFAULT_ACCOUNT, defaultCluster); + assertCredentialsBelongTo(OTHER_ACCOUNT, otherCluster); + } finally { + cleanUp(DEFAULT_ACCOUNT, name, roleName); + cleanUp(OTHER_ACCOUNT, name, roleName); + } + } + + private Cluster createCluster(String account, String name) { + Cluster cluster = new Cluster(); + cluster.setName(name); + cluster.setAccountId(account); + cluster.setArn("arn:aws:eks:us-east-1:" + account + ":cluster/" + name); + cluster.setCreatedAt(Instant.now()); + cluster.setVersion("1.30"); + cluster.setStatus(ClusterStatus.ACTIVE); + String issuer = oidcService.newIssuerUrl("us-east-1"); + cluster.setIdentity(new ClusterIdentity(new OidcIdentity(issuer))); + oidcService.ensureKeyForAccount(account, name, issuer); + eksService.putClusterForAccount(account, cluster); + // Persisted clusters lose this @JsonIgnore field; the ARN still names the owner. + cluster.setAccountId(null); + return cluster; + } + + private void createPodIdentityResources(String account, Cluster cluster, String roleName) { + String roleArn = RequestScopes.callAs(account, + () -> iamService.createRole(roleName, "/", "{}", null, 3600, null).getArn()); + createPodIdentityResourcesForRole(account, cluster, roleArn); + } + + private void createPodIdentityResourcesForRole(String account, Cluster cluster, String roleArn) { + RequestScopes.runAs(account, () -> { + addonService.create(cluster, new CreateAddonRequest("eks-pod-identity-agent", + null, null, null, null, null, null, null)); + associationService.create(cluster, new CreatePodIdentityAssociationRequest( + cluster.getName(), NAMESPACE, SERVICE_ACCOUNT, roleArn, null, null, null, null, null)); + }); + } + + private String mintToken(String account, Cluster cluster) { + return RequestScopes.callAs(account, () -> oidcService.mintServiceAccountToken( + cluster.getName(), cluster.getIdentity().getOidc().getIssuer(), + NAMESPACE, SERVICE_ACCOUNT, "pods.eks.amazonaws.com", 3600)); + } + + private void review(String name, String account, boolean patched) { + Map pod = Map.of("kind", "Pod", "spec", Map.of( + "serviceAccountName", SERVICE_ACCOUNT, "containers", List.of(Map.of("name", "app")))); + Map admissionReview = Map.of("apiVersion", "admission.k8s.io/v1", + "kind", "AdmissionReview", "request", Map.of( + "uid", "request-1", "namespace", NAMESPACE, "object", pod)); + given().contentType("application/json").body(admissionReview) + .post("/_floci/eks/clusters/" + name + "/pod-identity-webhook/scope/" + account) + .then().statusCode(200) + .body("response.allowed", equalTo(true)) + .body("response.patch", patched ? notNullValue() : nullValue()); + } + + private void assertCredentialsBelongTo(String account, Cluster cluster) { + String accessKeyId = given().header("Authorization", mintToken(account, cluster)) + .get("/v1/credentials") + .then().statusCode(200) + .body("AccountId", equalTo(account)) + .extract().path("AccessKeyId"); + IamService.EksSessionIdentity identity = iamService.findEksSessionIdentity(accessKeyId).orElseThrow(); + assertEquals(account, identity.accountId()); + assertEquals("arn:aws:iam::" + account + ":role/role-" + cluster.getName(), identity.roleArn()); + iamService.unregisterSession(account, accessKeyId); + assertTrue(iamService.findEksSessionIdentity(accessKeyId).isEmpty()); + } + + private void cleanUp(String account, String name, String roleName) { + eksService.findAuthenticationCluster(account, name).ifPresent(cluster -> + RequestScopes.runAs(account, () -> { + associationService.deleteClusterAssociations(cluster); + addonService.deleteClusterAddons(cluster); + oidcService.deleteKey(name); + iamService.findRole(account, roleName).ifPresent(role -> iamService.deleteRole(roleName)); + })); + eksService.deleteClusterForAccount(account, name); + } +}