mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(eks): scope clusters to the request Region (#7936)
EKS clusters are stored per account by name, and ListClusters returned every cluster of the account whatever Region the request named. DescribeCluster and every other cluster-scoped call also reached a cluster created in another Region. On AWS cluster names are Regional: ListClusters only lists the requesting Region's clusters, and the others answer ResourceNotFoundException. ListClusters now filters on the cluster's Region (from its ARN), and the cluster-scoped handlers answer ResourceNotFoundException for a cluster in another Region. The ListClusters pagination token is bound to the Region too. Floci's own restore and node-join paths still act on any Region. requireClusterInRequestRegion now returns the cluster it checked, so handlers that looked the cluster up again with describeCluster use it directly. DeleteCluster checks the Region on the cluster it reads under its capacity lock, so the check and the delete act on the same cluster. IAM resolved an EKS route's resource from the cluster EKS holds under that name in any Region. Clusters are Regional, so a request signed for another Region is now checked against that Region's cluster ARN, as a create there would mint it, and EKS then answers ResourceNotFoundException. IamRestRouteEnforcementIntegrationTest and ResourceArnBuilderTest relied on the cross-Region lookup and now check both sides.
This commit is contained in:
1 parent
fe24f7736a
commit
00d2ab3207
6 files changed
+177
-44
No files matched your search
@@ -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";
|
||||
}
|
||||
}
|
||||
+12
-6
@@ -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. */
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in new issue
Block a user