diff --git a/src/main/java/io/floci/aws/services/eks/EksController.java b/src/main/java/io/floci/aws/services/eks/EksController.java index 73a1ad0d06..37502335c1 100644 --- a/src/main/java/io/floci/aws/services/eks/EksController.java +++ b/src/main/java/io/floci/aws/services/eks/EksController.java @@ -90,14 +90,14 @@ public class EksController { @GET @Path("/clusters/{name}") public Response describeCluster(@PathParam("name") String name) { - Cluster cluster = eksService.describeCluster(name); + Cluster cluster = eksService.requireClusterInRequestRegion(name); return Response.ok(Map.of("cluster", toClusterResponse(cluster))).build(); } @DELETE @Path("/clusters/{name}") public Response deleteCluster(@PathParam("name") String name) { - Cluster cluster = eksService.deleteCluster(name); + Cluster cluster = eksService.deleteClusterInRequestRegion(name); return Response.ok(Map.of("cluster", toClusterResponse(cluster))).build(); } @@ -127,6 +127,7 @@ public class EksController { @POST @Path("/clusters/{name}/node-groups") public Response createNodeGroup(@PathParam("name") String name, CreateNodeGroupRequest request) { + eksService.requireClusterInRequestRegion(name); Nodegroup nodeGroup = eksService.createNodeGroup(name, request); return Response.ok(Map.of("nodegroup", nodeGroup)).build(); } @@ -134,6 +135,7 @@ public class EksController { @GET @Path("/clusters/{name}/node-groups") public Response listNodeGroups(@PathParam("name") String name) { + eksService.requireClusterInRequestRegion(name); List nodeGroupNames = eksService.listNodeGroups(name); return Response.ok(Map.of("nodegroups", nodeGroupNames)).build(); } @@ -142,6 +144,7 @@ public class EksController { @Path("/clusters/{name}/node-groups/{nodegroupName}") public Response describeNodeGroup(@PathParam("name") String name, @PathParam("nodegroupName") String nodegroupName) { + eksService.requireClusterInRequestRegion(name); Nodegroup nodeGroup = eksService.describeNodeGroup(name, nodegroupName); return Response.ok(Map.of("nodegroup", nodeGroup)).build(); } @@ -150,6 +153,7 @@ public class EksController { @Path("/clusters/{name}/node-groups/{nodegroupName}") public Response deleteNodeGroup(@PathParam("name") String name, @PathParam("nodegroupName") String nodegroupName) { + eksService.requireClusterInRequestRegion(name); Nodegroup nodeGroup = eksService.deleteNodeGroup(name, nodegroupName); return Response.ok(Map.of("nodegroup", nodeGroup)).build(); } @@ -157,6 +161,7 @@ public class EksController { @POST @Path("/clusters/{name}/fargate-profiles") public Response createFargateProfile(@PathParam("name") String name, CreateFargateProfileRequest request) { + eksService.requireClusterInRequestRegion(name); FargateProfile profile = eksService.createFargateProfile(name, request); return Response.ok(Map.of("fargateProfile", profile)).build(); } @@ -164,6 +169,7 @@ public class EksController { @GET @Path("/clusters/{name}/fargate-profiles") public Response listFargateProfiles(@PathParam("name") String name) { + eksService.requireClusterInRequestRegion(name); List profileNames = eksService.listFargateProfiles(name); return Response.ok(Map.of("fargateProfileNames", profileNames)).build(); } @@ -172,6 +178,7 @@ public class EksController { @Path("/clusters/{name}/fargate-profiles/{fargateProfileName}") public Response describeFargateProfile(@PathParam("name") String name, @PathParam("fargateProfileName") String fargateProfileName) { + eksService.requireClusterInRequestRegion(name); FargateProfile profile = eksService.describeFargateProfile(name, fargateProfileName); return Response.ok(Map.of("fargateProfile", profile)).build(); } @@ -180,6 +187,7 @@ public class EksController { @Path("/clusters/{name}/fargate-profiles/{fargateProfileName}") public Response deleteFargateProfile(@PathParam("name") String name, @PathParam("fargateProfileName") String fargateProfileName) { + eksService.requireClusterInRequestRegion(name); FargateProfile profile = eksService.deleteFargateProfile(name, fargateProfileName); return Response.ok(Map.of("fargateProfile", profile)).build(); } @@ -192,7 +200,7 @@ public class EksController { @POST @Path("/clusters/{name}/access-entries") public Response createAccessEntry(@PathParam("name") String name, CreateAccessEntryRequest request) { - return Response.ok(Map.of("accessEntry", accessEntries.create(eksService.describeCluster(name), request))).build(); + return Response.ok(Map.of("accessEntry", accessEntries.create(eksService.requireClusterInRequestRegion(name), request))).build(); } @GET @@ -201,7 +209,7 @@ public class EksController { @QueryParam("maxResults") String maxResults, @QueryParam("nextToken") String nextToken, @QueryParam("associatedPolicyArn") String associatedPolicyArn) { - EksAccessEntryService.Page page = accessEntries.list(eksService.describeCluster(name), + EksAccessEntryService.Page page = accessEntries.list(eksService.requireClusterInRequestRegion(name), Pagination.parseMaxResults(maxResults, "InvalidParameterException"), nextToken, associatedPolicyArn); return Response.ok(page.nextToken() == null ? Map.of("accessEntries", page.accessEntries()) : Map.of("accessEntries", page.accessEntries(), "nextToken", page.nextToken())).build(); @@ -210,13 +218,13 @@ public class EksController { @GET @Path("/clusters/{name}/access-entries/{principalArn: .+}") public Response describeAccessEntry(@PathParam("name") String name, @PathParam("principalArn") String principalArn) { - return Response.ok(Map.of("accessEntry", accessEntries.describe(eksService.describeCluster(name), principalArn))).build(); + return Response.ok(Map.of("accessEntry", accessEntries.describe(eksService.requireClusterInRequestRegion(name), principalArn))).build(); } @DELETE @Path("/clusters/{name}/access-entries/{principalArn: .+}") public Response deleteAccessEntry(@PathParam("name") String name, @PathParam("principalArn") String principalArn) { - accessEntries.delete(eksService.describeCluster(name), principalArn); + accessEntries.delete(eksService.requireClusterInRequestRegion(name), principalArn); return Response.ok(Map.of()).build(); } @@ -224,7 +232,7 @@ public class EksController { @Path("/clusters/{name}/access-entries/{principalArn: .+}/access-policies") public Response associateAccessPolicy(@PathParam("name") String name, @PathParam("principalArn") String principalArn, AssociateAccessPolicyRequest request) { - AssociatedAccessPolicy policy = accessEntries.associate(eksService.describeCluster(name), principalArn, request); + AssociatedAccessPolicy policy = accessEntries.associate(eksService.requireClusterInRequestRegion(name), principalArn, request); return Response.ok(Map.of("clusterName", name, "principalArn", principalArn, "associatedAccessPolicy", policy)).build(); } @@ -235,7 +243,7 @@ public class EksController { @PathParam("principalArn") String principalArn, @QueryParam("maxResults") String maxResults, @QueryParam("nextToken") String nextToken) { - EksAccessEntryService.PolicyPage page = accessEntries.listAccessPolicies(eksService.describeCluster(name), + EksAccessEntryService.PolicyPage page = accessEntries.listAccessPolicies(eksService.requireClusterInRequestRegion(name), principalArn, Pagination.parseMaxResults(maxResults, "InvalidParameterException"), nextToken); Map body = new LinkedHashMap<>(); body.put("clusterName", name); @@ -252,14 +260,14 @@ public class EksController { public Response disassociateAccessPolicy(@PathParam("name") String name, @PathParam("principalArn") String principalArn, @PathParam("policyArn") String policyArn) { - accessEntries.disassociate(eksService.describeCluster(name), principalArn, policyArn); + accessEntries.disassociate(eksService.requireClusterInRequestRegion(name), principalArn, policyArn); return Response.ok(Map.of()).build(); } @POST @Path("/clusters/{name}/addons") public Response createAddon(@PathParam("name") String name, CreateAddonRequest request) { - Cluster cluster = eksService.describeCluster(name); + Cluster cluster = eksService.requireClusterInRequestRegion(name); Addon addon = addons.create(cluster, request); return Response.ok(Map.of("addon", addon)).build(); } @@ -269,7 +277,7 @@ public class EksController { public Response listAddons(@PathParam("name") String name, @QueryParam("maxResults") String maxResults, @QueryParam("nextToken") String nextToken) { - Cluster cluster = eksService.describeCluster(name); + Cluster cluster = eksService.requireClusterInRequestRegion(name); EksAddonService.AddonNamesPage page = addons.list(cluster, Pagination.parseMaxResults(maxResults, "InvalidParameterException"), nextToken); return Response.ok(page.nextToken() == null @@ -281,7 +289,7 @@ public class EksController { @Path("/clusters/{name}/addons/{addonName}") public Response describeAddon(@PathParam("name") String name, @PathParam("addonName") String addonName) { - Cluster cluster = eksService.describeCluster(name); + Cluster cluster = eksService.requireClusterInRequestRegion(name); Addon addon = addons.describe(cluster, addonName); return Response.ok(Map.of("addon", addon)).build(); } @@ -291,7 +299,7 @@ public class EksController { public Response updateAddon(@PathParam("name") String name, @PathParam("addonName") String addonName, UpdateAddonRequest request) { - Cluster cluster = eksService.describeCluster(name); + Cluster cluster = eksService.requireClusterInRequestRegion(name); Update update = addons.update(cluster, addonName, request); return Response.ok(Map.of("update", update)).build(); } @@ -302,7 +310,7 @@ public class EksController { @PathParam("updateId") String updateId, @QueryParam("addonName") String addonName, @QueryParam("nodegroupName") String nodegroupName) { - Cluster cluster = eksService.describeCluster(name); + Cluster cluster = eksService.requireClusterInRequestRegion(name); Update update = addons.describeUpdate(cluster, updateId, addonName); return Response.ok(Map.of("update", update)).build(); } @@ -312,7 +320,7 @@ public class EksController { public Response deleteAddon(@PathParam("name") String name, @PathParam("addonName") String addonName, @QueryParam("preserve") Boolean preserve) { - Cluster cluster = eksService.describeCluster(name); + Cluster cluster = eksService.requireClusterInRequestRegion(name); Addon addon = addons.delete(cluster, addonName, Boolean.TRUE.equals(preserve)); return Response.ok(Map.of("addon", addon)).build(); } @@ -343,7 +351,7 @@ public class EksController { @GET @Path("/clusters/{name}/identity-provider-configs") public Response listIdentityProviderConfigs(@PathParam("name") String name) { - eksService.describeCluster(name); + eksService.requireClusterInRequestRegion(name); return Response.ok(Map.of("identityProviderConfigs", List.of())).build(); } @@ -351,7 +359,7 @@ public class EksController { @Path("/clusters/{name}/pod-identity-associations") public Response createPodIdentityAssociation(@PathParam("name") String name, CreatePodIdentityAssociationRequest request) { - Cluster cluster = eksService.describeCluster(name); + Cluster cluster = eksService.requireClusterInRequestRegion(name); PodIdentityAssociation association = podIdentityAssociations.create(cluster, request); return Response.ok(Map.of("association", association)).build(); } @@ -363,7 +371,7 @@ public class EksController { @QueryParam("serviceAccount") String serviceAccount, @QueryParam("maxResults") String maxResults, @QueryParam("nextToken") String nextToken) { - Cluster cluster = eksService.describeCluster(name); + Cluster cluster = eksService.requireClusterInRequestRegion(name); EksPodIdentityAssociationService.Page page = podIdentityAssociations.list(cluster, namespace, serviceAccount, Pagination.parseMaxResults(maxResults, "InvalidParameterException"), nextToken); return Response.ok(page.nextToken() == null @@ -375,7 +383,7 @@ public class EksController { @Path("/clusters/{name}/pod-identity-associations/{associationId}") public Response describePodIdentityAssociation(@PathParam("name") String name, @PathParam("associationId") String associationId) { - Cluster cluster = eksService.describeCluster(name); + Cluster cluster = eksService.requireClusterInRequestRegion(name); PodIdentityAssociation association = podIdentityAssociations.describe(cluster, associationId); return Response.ok(Map.of("association", association)).build(); } @@ -385,7 +393,7 @@ public class EksController { public Response updatePodIdentityAssociation(@PathParam("name") String name, @PathParam("associationId") String associationId, UpdatePodIdentityAssociationRequest request) { - Cluster cluster = eksService.describeCluster(name); + Cluster cluster = eksService.requireClusterInRequestRegion(name); PodIdentityAssociation association = podIdentityAssociations.update(cluster, associationId, request); return Response.ok(Map.of("association", association)).build(); } @@ -394,7 +402,7 @@ public class EksController { @Path("/clusters/{name}/pod-identity-associations/{associationId}") public Response deletePodIdentityAssociation(@PathParam("name") String name, @PathParam("associationId") String associationId) { - Cluster cluster = eksService.describeCluster(name); + Cluster cluster = eksService.requireClusterInRequestRegion(name); PodIdentityAssociation association = podIdentityAssociations.delete(cluster, associationId); return Response.ok(Map.of("association", association)).build(); } diff --git a/src/main/java/io/floci/aws/services/eks/EksService.java b/src/main/java/io/floci/aws/services/eks/EksService.java index c01a561e09..01a323eee2 100644 --- a/src/main/java/io/floci/aws/services/eks/EksService.java +++ b/src/main/java/io/floci/aws/services/eks/EksService.java @@ -856,7 +856,10 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun } public List listClusters() { + // Clusters are stored per account; like AWS, a request only sees its own Region's clusters. + String region = regionResolver.getRegion(); return storage.scan(k -> true).stream() + .filter(cluster -> region.equals(resolveClusterRegion(cluster))) .map(Cluster::getName) .collect(Collectors.toList()); } @@ -864,11 +867,41 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun public PaginatedResult listClusters(Integer maxResults, String nextToken) { int limit = EksAccessEntryService.pageSize(maxResults); List names = listClusters().stream().sorted().toList(); - return Pagination.paginate(names, name -> name, limit, nextToken, "clusters:" + regionResolver.getAccountId(), + return Pagination.paginate(names, name -> name, limit, nextToken, "clusters:" + regionResolver.getAccountId() + ":" + regionResolver.getRegion(), token -> new AwsException("InvalidParameterException", "Invalid nextToken", 400)); } + /** + * Returns the cluster {@code name} when it is in the request's Region, and answers + * ResourceNotFoundException for a cluster in another Region, as AWS does: cluster names + * are Regional, so a request must not reach a cluster created in a different Region of the account. + * Applied by the API handlers only; Floci's own restore and node-join paths act on any Region. + */ + public Cluster requireClusterInRequestRegion(String name) { + return requireInRequestRegion(describeCluster(name), name); + } + + private Cluster requireInRequestRegion(Cluster cluster, String name) { + if (!regionResolver.getRegion().equals(resolveClusterRegion(cluster))) { + throw new AwsException("ResourceNotFoundException", "No cluster found for name: " + name, 404); + } + return cluster; + } + public Cluster deleteCluster(String name) { + return deleteCluster(name, false); + } + + /** + * Deletes the cluster {@code name} only when it is in the request's Region, as the DeleteCluster + * API does. The Region is checked on the cluster the delete reads under its lock, so a cluster + * deleted and created again under the same name in another Region meanwhile is not removed. + */ + public Cluster deleteClusterInRequestRegion(String name) { + return deleteCluster(name, true); + } + + private Cluster deleteCluster(String name, boolean inRequestRegion) { // A first node group replacing the node holds this lock while the swap runs; deleting // meanwhile would find no container to stop and have its cluster written back. Read the // cluster only once the lock is held: a delete that waited may find it gone. @@ -876,6 +909,9 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun Cluster cluster = storage.get(name) .orElseThrow(() -> new AwsException("ResourceNotFoundException", "No cluster found for name: " + name, 404)); + if (inRequestRegion) { + requireInRequestRegion(cluster, name); + } if (cluster.getAccountId() == null) { cluster.setAccountId(regionResolver.getAccountId()); } diff --git a/src/main/java/io/floci/aws/services/iam/ResourceArnBuilder.java b/src/main/java/io/floci/aws/services/iam/ResourceArnBuilder.java index c35d7891ea..385bb78868 100644 --- a/src/main/java/io/floci/aws/services/iam/ResourceArnBuilder.java +++ b/src/main/java/io/floci/aws/services/iam/ResourceArnBuilder.java @@ -1293,8 +1293,9 @@ public class ResourceArnBuilder { * looks everything up. A cluster is the ARN EKS holds it under, or for one it holds none of, the ARN a * create mints in the request's Region; {@code CreateCluster} is checked against the cluster its body * names, as the EKS user guide's example policy for creating a cluster grants it. A node group, Fargate - * profile, add-on, access entry or Pod Identity association is the ARN EKS holds it under, which ends - * in an id the request does not carry, or {@code *} when EKS holds none to act on; listing or creating + * profile, add-on, access entry or Pod Identity association is the ARN EKS holds it under in the request's + * Region (clusters are Regional), which ends in an id the request does not carry, or {@code *} when EKS + * holds none to act on; listing or creating * them, and every other route under a cluster, act on the cluster. The path is split as the router * splits it, before decoding, so an encoded {@code /} stays inside a name, and an access entry's * principal is the rest of the path, as EKS takes it. {@code DescribeUpdate} acts on the add-on its @@ -1341,14 +1342,15 @@ public class ResourceArnBuilder { String collection = segments[2]; return switch (collection) { case "node-groups", "fargate-profiles", "addons", "pod-identity-associations" -> - eksHeldArn(cluster, collection, pathSegment(segments[3])); + eksHeldArn(cluster, collection, pathSegment(segments[3]), region); case "access-entries" -> eksHeldArn(cluster, collection, - pathSegment(String.join("/", List.of(segments).subList(3, accessEntryEnd(segments, method))))); + pathSegment(String.join("/", List.of(segments).subList(3, accessEntryEnd(segments, method)))), + region); case "updates" -> { // The updates Floci keeps are its add-ons', and EKS returns one whether or not the request names // its add-on, so the update is checked against the add-on it was made to. - Optional addon = segments.length == 4 && eksService != null - ? eksService.get().findResourceArn(cluster, collection, pathSegment(segments[3])) + Optional addon = segments.length == 4 + ? eksHeld(cluster, collection, pathSegment(segments[3]), region) : Optional.empty(); yield addon.orElseGet(() -> eksClusterArn(cluster, region, accountId)); } @@ -1383,14 +1385,25 @@ public class ResourceArnBuilder { /** The ARN EKS holds the cluster {@code name} under, or else the one a create mints in {@code region}. */ private String eksClusterArn(String name, String region, String accountId) { - Optional held = eksService == null ? Optional.empty() - : eksService.get().findResourceArn(name, null, null); - return held.orElseGet(() -> AwsArnUtils.Arn.of("eks", region, accountId, "cluster/" + name).toString()); + return eksHeld(name, null, null, region) + .orElseGet(() -> AwsArnUtils.Arn.of("eks", region, accountId, "cluster/" + name).toString()); } /** The ARN EKS holds {@code name} of {@code collection} in the cluster under, or {@code *} when it holds none. */ - private String eksHeldArn(String cluster, String collection, String name) { - return eksService == null ? "*" : eksService.get().findResourceArn(cluster, collection, name).orElse("*"); + private String eksHeldArn(String cluster, String collection, String name, String region) { + return eksHeld(cluster, collection, name, region).orElse("*"); + } + + /** + * The ARN EKS holds the resource under, when the cluster is in {@code region}. EKS clusters are Regional, so a + * request signed for another Region does not reach them, and is checked as acting on a resource of its own Region. + */ + private Optional eksHeld(String cluster, String collection, String name, String region) { + if (eksService == null) { + return Optional.empty(); + } + return eksService.get().findResourceArn(cluster, collection, name) + .filter(arn -> region.equals(AwsArnUtils.regionOrDefault(arn, null))); } /** The ARN a shared tagging route names, or {@code *} for any other route. */ diff --git a/src/test/java/io/floci/aws/services/eks/EksRegionScopingIntegrationTest.java b/src/test/java/io/floci/aws/services/eks/EksRegionScopingIntegrationTest.java new file mode 100644 index 0000000000..7d83e9d902 --- /dev/null +++ b/src/test/java/io/floci/aws/services/eks/EksRegionScopingIntegrationTest.java @@ -0,0 +1,54 @@ +package io.floci.aws.services.eks; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; + +/** + * Cluster names are Regional on AWS: a cluster created in one Region is not listed, described or + * otherwise reachable from another Region of the same account. + */ +@QuarkusTest +class EksRegionScopingIntegrationTest { + + private static final String ACCOUNT = "135792468024"; + private static final String CLUSTER = "region-scoped"; + + @Test + void clusterIsOnlyVisibleInTheRegionItWasCreatedIn() { + given().header("Authorization", auth("eu-west-1")).contentType("application/json") + .body(Map.of("name", CLUSTER, "roleArn", "arn:aws:iam::" + ACCOUNT + ":role/cluster")) + .post("/clusters").then().statusCode(200) + .body("cluster.arn", equalTo("arn:aws:eks:eu-west-1:" + ACCOUNT + ":cluster/" + CLUSTER)); + + try { + given().header("Authorization", auth("eu-west-1")).get("/clusters") + .then().statusCode(200).body("clusters", hasItem(CLUSTER)); + given().header("Authorization", auth("eu-west-1")).get("/clusters/" + CLUSTER) + .then().statusCode(200).body("cluster.name", equalTo(CLUSTER)); + + given().header("Authorization", auth("eu-central-1")).get("/clusters") + .then().statusCode(200).body("clusters", not(hasItem(CLUSTER))); + given().header("Authorization", auth("eu-central-1")).get("/clusters/" + CLUSTER) + .then().statusCode(404); + given().header("Authorization", auth("eu-central-1")).get("/clusters/" + CLUSTER + "/node-groups") + .then().statusCode(404); + given().header("Authorization", auth("eu-central-1")).delete("/clusters/" + CLUSTER) + .then().statusCode(404); + } finally { + given().header("Authorization", auth("eu-west-1")).delete("/clusters/" + CLUSTER) + .then().statusCode(200); + } + } + + private static String auth(String region) { + return "AWS4-HMAC-SHA256 Credential=" + ACCOUNT + "/20261110/" + region + + "/eks/aws4_request, SignedHeaders=host, Signature=abc"; + } +} diff --git a/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java index af4fade56d..a7223b90e6 100644 --- a/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamRestRouteEnforcementIntegrationTest.java @@ -190,20 +190,22 @@ class IamRestRouteEnforcementIntegrationTest { @Test void anEksClusterRouteIsCheckedAgainstTheClusterEksActsOn() { - // EKS finds a cluster by its name alone, whatever Region a request is signed for, so a request signed - // for another Region is checked against the cluster it acts on. + // EKS clusters are Regional: a request signed for the cluster's Region is checked against the cluster it + // acts on, and one signed for another Region acts on that Region's cluster of the name, which is not there. String user = user("route-eks-region"); String name = "route-west-" + suffix(); String cluster = given().header("Authorization", auth(ADMIN, "us-west-2", "eks")) .contentType("application/json") .body("{\"name\":\"" + name + "\",\"roleArn\":\"arn:aws:iam::" + ACCOUNT + ":role/route-cluster\"}") .post("/clusters").then().statusCode(200).extract().path("cluster.arn"); - cleanup.register(() -> eks().delete("/clusters/" + name)); + cleanup.register(() -> given().header("Authorization", auth(ADMIN, "us-west-2", "eks")) + .delete("/clusters/" + name)); policy(user, allow("eks:DescribeCluster", "*") + "," + deny("eks:DescribeCluster", cluster)); - refused(as(user, "eks").get("/clusters/" + name).then(), "eks:DescribeCluster"); + refused(as(user, "us-west-2", "eks").get("/clusters/" + name).then(), "eks:DescribeCluster"); + as(user, "eks").get("/clusters/" + name).then().statusCode(404); policy(user, allow("eks:DescribeCluster", cluster)); - as(user, "eks").get("/clusters/" + name).then().statusCode(200); + as(user, "us-west-2", "eks").get("/clusters/" + name).then().statusCode(200); } @Test @@ -651,7 +653,11 @@ class IamRestRouteEnforcementIntegrationTest { } private RequestSpecification as(String user, String service) { - return given().header("Authorization", auth(keys.get(user), service)).contentType("application/json"); + return as(user, "us-east-1", service); + } + + private RequestSpecification as(String user, String region, String service) { + return given().header("Authorization", auth(keys.get(user), region, service)).contentType("application/json"); } /** A Lambda request as {@code user}, with an empty JSON body on a write. */ diff --git a/src/test/java/io/floci/aws/services/iam/ResourceArnBuilderTest.java b/src/test/java/io/floci/aws/services/iam/ResourceArnBuilderTest.java index 417feb567e..38a68f0aaf 100644 --- a/src/test/java/io/floci/aws/services/iam/ResourceArnBuilderTest.java +++ b/src/test/java/io/floci/aws/services/iam/ResourceArnBuilderTest.java @@ -2457,8 +2457,8 @@ class ResourceArnBuilderTest { // ── EKS ────────────────────────────────────────────────────────────────────── - /** The cluster {@code held}, which EKS keeps in us-west-2 and finds by its name alone, and what it holds. */ - private static final String HELD_CLUSTER = "arn:aws:eks:us-west-2:000000000000:cluster/held"; + /** The cluster {@code held}, which EKS keeps in the request's Region (us-east-1), and what it holds. */ + private static final String HELD_CLUSTER = "arn:aws:eks:us-east-1:000000000000:cluster/held"; private static final String WORKER = "arn:aws:iam::000000000000:role/team/worker"; private static final String RUNNER = "arn:aws:iam::000000000000:role/ci/access-policies/runner"; @@ -2476,7 +2476,7 @@ class ResourceArnBuilderTest { {"access-entries", RUNNER, "access-entry/held/role/000000000000/runner/7"}, }) { when(eks.findResourceArn("held", held[0], held[1])) - .thenReturn(Optional.of("arn:aws:eks:us-west-2:000000000000:" + held[2])); + .thenReturn(Optional.of("arn:aws:eks:us-east-1:000000000000:" + held[2])); } return backedBy(eks); } @@ -2509,7 +2509,7 @@ class ResourceArnBuilderTest { @Test void anEksRouteOnWhatAClusterHoldsActsOnTheArnEksHoldsItUnder() { ResourceArnBuilder eks = eksHolding(); - String held = "arn:aws:eks:us-west-2:000000000000:"; + String held = "arn:aws:eks:us-east-1:000000000000:"; assertEquals(held + "nodegroup/held/ng/1", eksResource(eks, "GET", "/clusters/held/node-groups/ng")); assertEquals(held + "nodegroup/held/ng/1", eksResource(eks, "POST", "/clusters/held/node-groups/ng/update-version")); @@ -2552,7 +2552,7 @@ class ResourceArnBuilderTest { void anEksUpdateActsOnItsAddonAndATagOnTheCallersClusterOfThatName() { ResourceArnBuilder eks = eksHolding(); // EKS returns an add-on's update whatever add-on the request names, if any, so it acts on its own add-on. - String addon = "arn:aws:eks:us-west-2:000000000000:addon/held/vpc-cni/4"; + String addon = "arn:aws:eks:us-east-1:000000000000:addon/held/vpc-cni/4"; assertEquals(addon, eksResource(eks, "GET", "/clusters/held/updates/u-1")); queryParams.putSingle("addonName", "coredns"); assertEquals(addon, eksResource(eks, "GET", "/clusters/held/updates/u-1")); @@ -2575,6 +2575,22 @@ class ResourceArnBuilderTest { } } + @Test + void anEksRouteOnAClusterHeldInAnotherRegionActsOnTheRequestsRegion() { + // EKS clusters are Regional: a request signed for us-east-1 does not reach a cluster EKS keeps in us-west-2, + // so it acts on the cluster of that name a create would mint in us-east-1, and on nothing the other holds. + EksService eks = mock(EksService.class); + when(eks.findResourceArn(any(), any(), any())).thenReturn(Optional.empty()); + when(eks.findResourceArn("west", null, null)) + .thenReturn(Optional.of("arn:aws:eks:us-west-2:000000000000:cluster/west")); + when(eks.findResourceArn("west", "node-groups", "ng")) + .thenReturn(Optional.of("arn:aws:eks:us-west-2:000000000000:nodegroup/west/ng/1")); + ResourceArnBuilder builder = backedBy(eks); + + assertEquals("arn:aws:eks:us-east-1:000000000000:cluster/west", eksResource(builder, "GET", "/clusters/west")); + assertEquals("*", eksResource(builder, "GET", "/clusters/west/node-groups/ng")); + } + /** Stubs the request path as sent and, decoded, as {@link UriInfo#getPath()} gives it. */ private void request(String sent) { when(uriInfo.getRequestUri()).thenReturn(URI.create("http://localhost:4566" + sent));