feat(eks): implement access-entry management (#3745)

* feat(eks): implement access-entry management

* test(eks): wait for cluster activation in access-entry compatibility test

* fix(eks): validate access-entry maxResults with shared parser
This commit is contained in:
luiseterc authored and GitHub committed 2026-09-16 18:50:39 -04:00
1 parent 2b091608af
commit 98c47c4347
14 files changed
+687 -21

No files matched your search

@@ -0,0 +1,117 @@
package io.github.hectorvent.floci.services.eks;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
@QuarkusTest
class EksAccessEntryIntegrationTest {
@Test
void nodeEntryRoundTripUsesJsonAndEncodedPrincipalPaths() {
String account = "135791357913";
String name = "entries-" + UUID.randomUUID().toString().substring(0, 8);
String role = "worker-" + name;
String principal = "arn:aws:iam::" + account + ":role/path/" + role;
String path = "/clusters/" + name + "/access-entries";
String entryPath = path + "/" + URLEncoder.encode(principal, StandardCharsets.UTF_8);
given().header("Authorization", auth(account, "iam")).contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateRole").formParam("RoleName", role).formParam("Path", "/path/")
.formParam("AssumeRolePolicyDocument", "{}").post("/").then().statusCode(200);
createCluster(account, name, "API");
try {
Map<String, Object> request = Map.of("principalArn", principal, "type", "EC2_LINUX",
"tags", Map.of("team", "platform"), "clientRequestToken", "retry-token");
String arn = given().header("Authorization", auth(account, "eks")).contentType("application/json")
.body(request).post(path).then().statusCode(200)
.body("accessEntry.username", equalTo("system:node:{{EC2PrivateDNSName}}"))
.body("accessEntry.createdAt", instanceOf(Number.class))
.body("accessEntry.tags.team", equalTo("platform"))
.body("accessEntry.principalId", nullValue())
.extract().path("accessEntry.accessEntryArn");
given().header("Authorization", auth(account, "eks")).contentType("application/json")
.body(request).post(path).then().statusCode(200)
.body("accessEntry.accessEntryArn", equalTo(arn));
given().header("Authorization", auth(account, "eks")).urlEncodingEnabled(false)
.get(entryPath).then().statusCode(200).contentType(containsString("application/json"))
.body("accessEntry.principalArn", equalTo(principal));
given().header("Authorization", auth(account, "eks")).get(path).then().statusCode(200)
.body("accessEntries", contains(principal)).body("nextToken", nullValue());
given().header("Authorization", auth("246802468024", "eks")).get(path).then().statusCode(404);
given().header("Authorization", auth(account, "eks")).urlEncodingEnabled(false)
.delete(entryPath).then().statusCode(200).body(equalTo("{}"));
given().header("Authorization", auth(account, "eks")).urlEncodingEnabled(false)
.get(entryPath).then().statusCode(404).body("__type", equalTo("ResourceNotFoundException"));
given().header("Authorization", auth(account, "eks")).contentType("application/json")
.body(request).post(path).then().statusCode(200);
given().header("Authorization", auth(account, "eks")).delete("/clusters/" + name)
.then().statusCode(200);
createCluster(account, name, "API");
given().header("Authorization", auth(account, "eks")).get(path).then().statusCode(200)
.body("accessEntries", empty());
} finally {
given().header("Authorization", auth(account, "eks")).delete("/clusters/" + name)
.then().statusCode(200);
given().header("Authorization", auth(account, "iam")).contentType("application/x-www-form-urlencoded")
.formParam("Action", "DeleteRole").formParam("RoleName", role).post("/").then().statusCode(200);
}
}
@ParameterizedTest
@ValueSource(strings = {"abc", "1.5", "2147483648", "0", "-1", "101"})
void invalidMaxResultsReturnsAwsJsonError(String maxResults) {
String account = "135791357913";
String name = "pagination-" + UUID.randomUUID().toString().substring(0, 8);
createCluster(account, name, "API");
try {
given().header("Authorization", auth(account, "eks")).queryParam("maxResults", maxResults)
.get("/clusters/" + name + "/access-entries").then().statusCode(400)
.contentType(containsString("application/json"))
.body("__type", equalTo("InvalidParameterException"))
.body("message", not(emptyOrNullString()));
} finally {
given().header("Authorization", auth(account, "eks")).delete("/clusters/" + name)
.then().statusCode(200);
}
}
@Test
void configMapModeRejectsEntriesAndUnknownModeIsRejectedAtCreate() {
String name = "config-" + UUID.randomUUID().toString().substring(0, 8);
String account = "135791357913";
createCluster(account, name, "CONFIG_MAP");
try {
given().header("Authorization", auth(account, "eks"))
.get("/clusters/" + name + "/access-entries").then().statusCode(400)
.body("__type", equalTo("InvalidRequestException"));
given().header("Authorization", auth(account, "eks")).contentType("application/json")
.body(Map.of("name", name + "-invalid", "accessConfig", Map.of("authenticationMode", "invalid")))
.post("/clusters").then().statusCode(400).body("__type", equalTo("InvalidParameterException"));
} finally {
given().header("Authorization", auth(account, "eks")).delete("/clusters/" + name)
.then().statusCode(200);
}
}
private static void createCluster(String account, String name, String mode) {
given().header("Authorization", auth(account, "eks")).contentType("application/json")
.body(Map.of("name", name, "roleArn", "arn:aws:iam::" + account + ":role/cluster",
"accessConfig", Map.of("authenticationMode", mode, "bootstrapClusterCreatorAdminPermissions", false)))
.post("/clusters").then().statusCode(200)
.body("cluster.accessConfig.authenticationMode", equalTo(mode))
.body("cluster.accessConfig.bootstrapClusterCreatorAdminPermissions", equalTo(false));
}
private static String auth(String account, String service) {
return "AWS4-HMAC-SHA256 Credential=" + account
+ "/20260916/us-east-1/" + service + "/aws4_request, SignedHeaders=host, Signature=abc";
}
}
@@ -0,0 +1,162 @@
package io.github.hectorvent.floci.services.eks;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.eks.model.AccessConfig;
import io.github.hectorvent.floci.services.eks.model.AccessEntry;
import io.github.hectorvent.floci.services.eks.model.Cluster;
import io.github.hectorvent.floci.services.eks.model.ClusterStatus;
import io.github.hectorvent.floci.services.eks.model.CreateAccessEntryRequest;
import io.github.hectorvent.floci.services.iam.IamService;
import io.github.hectorvent.floci.services.iam.model.IamRole;
import io.github.hectorvent.floci.services.iam.model.IamUser;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
class EksAccessEntryServiceTest {
private static final String PRINCIPAL = "arn:aws:iam::123456789012:role/path/worker";
@Test
void nodeEntryUsesGeneratedIdentityAndCapturesStablePrincipalId() throws Exception {
Fixture fixture = fixture();
AccessEntry entry = fixture.service.create(fixture.cluster, request(PRINCIPAL, "EC2_LINUX", "retry"));
assertEquals("system:node:{{EC2PrivateDNSName}}", entry.username());
assertEquals(List.of("system:nodes"), entry.kubernetesGroups());
assertEquals(Map.of("team", "platform"), entry.tags());
assertTrue(entry.createdAt() > 0);
EksAccessEntryService.StoredEntry stored = fixture.storage.scan(key -> true).getFirst();
assertEquals("AROA-worker", stored.principalId());
ObjectMapper mapper = new ObjectMapper();
EksAccessEntryService.StoredEntry restored = mapper.readValue(mapper.writeValueAsBytes(stored),
EksAccessEntryService.StoredEntry.class);
assertEquals(stored, restored);
assertFalse(mapper.writeValueAsString(entry).contains("principalId"));
fixture.role.setRoleId("AROA-recreated");
assertEquals("AROA-worker", fixture.storage.scan(key -> true).getFirst().principalId());
assertEquals(entry, fixture.service.describe(fixture.cluster, PRINCIPAL));
}
@Test
void retriesAreIdempotentButConflictingRequestsAndDuplicatesFail() {
Fixture fixture = fixture();
CreateAccessEntryRequest request = request(PRINCIPAL, "EC2_LINUX", "retry");
AccessEntry first = fixture.service.create(fixture.cluster, request);
assertEquals(first, fixture.service.create(fixture.cluster, request));
assertEquals("InvalidParameterException", assertThrows(AwsException.class, () -> fixture.service.create(
fixture.cluster, request(PRINCIPAL, "STANDARD", "retry"))).getErrorCode());
assertEquals(409, assertThrows(AwsException.class, () -> fixture.service.create(
fixture.cluster, request(PRINCIPAL, "EC2_LINUX", "other"))).getHttpStatus());
}
@Test
void standardRoleDefaultsStripTheIamPath() {
Fixture fixture = fixture();
AccessEntry entry = fixture.service.create(fixture.cluster, request(PRINCIPAL, null, null));
assertEquals("STANDARD", entry.type());
assertEquals("arn:aws:sts::123456789012:assumed-role/worker/{{SessionName}}", entry.username());
}
@Test
void standardUserCanBelongToAnotherAccount() {
Fixture fixture = fixture();
String principal = "arn:aws:iam::999999999999:user/team/reader";
when(fixture.iam.findUser("999999999999", "reader")).thenReturn(Optional.of(
new IamUser("AIDA-reader", "reader", "/team/", principal)));
AccessEntry entry = fixture.service.create(fixture.cluster,
new CreateAccessEntryRequest(principal, null, null, List.of("readers"), null, null));
assertEquals(principal, entry.username());
assertEquals(List.of("readers"), entry.kubernetesGroups());
assertEquals("AIDA-reader", fixture.storage.scan(key -> true).getFirst().principalId());
}
@Test
void nodeOverridesForeignAccountsAndMissingPrincipalsAreRejected() {
Fixture fixture = fixture();
assertThrows(AwsException.class, () -> fixture.service.create(fixture.cluster,
new CreateAccessEntryRequest(PRINCIPAL, "EC2_LINUX", "custom", null, null, null)));
assertThrows(AwsException.class, () -> fixture.service.create(fixture.cluster,
new CreateAccessEntryRequest(PRINCIPAL, "EC2_LINUX", null, List.of(), null, null)));
assertThrows(AwsException.class, () -> fixture.service.create(fixture.cluster,
request(PRINCIPAL.replace("123456789012", "999999999999"), "EC2_LINUX", null)));
when(fixture.iam.findRole("123456789012", "worker")).thenReturn(Optional.empty());
assertThrows(AwsException.class, () -> fixture.service.create(fixture.cluster,
request(PRINCIPAL, "STANDARD", null)));
}
@Test
void paginationTokensCannotCrossClusterOrAccountBoundaries() {
Fixture fixture = fixture();
fixture.service.create(fixture.cluster, request(PRINCIPAL, "STANDARD", null));
String second = PRINCIPAL.replace("worker", "worker2");
when(fixture.iam.findRole("123456789012", "worker2")).thenReturn(Optional.of(
new IamRole("AROA-second", "worker2", "/path/", second, "{}")));
fixture.service.create(fixture.cluster, request(second, "STANDARD", null));
EksAccessEntryService.Page first = fixture.service.list(fixture.cluster, 1, null);
assertEquals(List.of(PRINCIPAL), first.accessEntries());
assertNotNull(first.nextToken());
EksAccessEntryService.Page next = fixture.service.list(fixture.cluster, 1, first.nextToken());
assertEquals(List.of(second), next.accessEntries());
assertNull(next.nextToken());
assertThrows(AwsException.class, () -> fixture.service.list(fixture.cluster, 0, null));
assertThrows(AwsException.class, () -> fixture.service.list(fixture.cluster, 101, null));
assertThrows(AwsException.class, () -> fixture.service.list(fixture.cluster, 1, "invalid"));
fixture.cluster.setArn(fixture.cluster.getArn().replace("123456789012", "999999999999"));
assertTrue(fixture.service.list(fixture.cluster, 100, null).accessEntries().isEmpty());
assertThrows(AwsException.class, () -> fixture.service.list(fixture.cluster, 1, first.nextToken()));
}
@Test
void clusterDeletionCleansEntriesAndRecreationCannotInheritThem() {
Fixture fixture = fixture();
fixture.service.create(fixture.cluster, request(PRINCIPAL, "EC2_LINUX", null));
fixture.service.deleteClusterEntries(fixture.cluster);
assertTrue(fixture.storage.scan(key -> true).isEmpty());
fixture.service.create(fixture.cluster, request(PRINCIPAL, "EC2_LINUX", null));
fixture.cluster.setCreatedAt(fixture.cluster.getCreatedAt().plusSeconds(1));
assertTrue(fixture.service.list(fixture.cluster, null, null).accessEntries().isEmpty());
assertEquals(404, assertThrows(AwsException.class,
() -> fixture.service.describe(fixture.cluster, PRINCIPAL)).getHttpStatus());
}
@Test
void configMapAndInactiveClustersRejectAccessEntryOperations() {
Fixture fixture = fixture();
fixture.cluster.setAccessConfig(new AccessConfig("CONFIG_MAP", true));
assertEquals("InvalidRequestException", assertThrows(AwsException.class,
() -> fixture.service.list(fixture.cluster, null, null)).getErrorCode());
fixture.cluster.setAccessConfig(new AccessConfig("API", false));
fixture.cluster.setStatus(ClusterStatus.CREATING);
assertThrows(AwsException.class, () -> fixture.service.create(fixture.cluster,
request(PRINCIPAL, "EC2_LINUX", null)));
}
private static CreateAccessEntryRequest request(String principal, String type, String token) {
return new CreateAccessEntryRequest(principal, type, null, null, Map.of("team", "platform"), token);
}
private static Fixture fixture() {
Cluster cluster = new Cluster();
cluster.setName("nodes");
cluster.setArn("arn:aws:eks:us-east-1:123456789012:cluster/nodes");
cluster.setCreatedAt(Instant.parse("2026-09-16T10:00:00Z"));
cluster.setStatus(ClusterStatus.ACTIVE);
cluster.setAccessConfig(new AccessConfig("API", false));
IamService iam = mock(IamService.class);
IamRole role = new IamRole("AROA-worker", "worker", "/path/", PRINCIPAL, "{}");
when(iam.findRole("123456789012", "worker")).thenReturn(Optional.of(role));
InMemoryStorage<String, EksAccessEntryService.StoredEntry> storage = new InMemoryStorage<>();
return new Fixture(cluster, iam, role, storage, new EksAccessEntryService(storage, iam));
}
private record Fixture(Cluster cluster, IamService iam, IamRole role,
InMemoryStorage<String, EksAccessEntryService.StoredEntry> storage,
EksAccessEntryService service) {}
}
@@ -67,7 +67,7 @@ class EksServiceTest {
Ec2Service ec2Service = null;
RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");
eksService = new EksService(storageFactory, config, regionResolver, clusterManager, ec2Service,
new EksOidcService(storageFactory, new ObjectMapper()));
new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class));
}
private EmulatorConfig testConfig() {
@@ -227,7 +227,7 @@ class EksServiceTest {
EksOidcService oidcService = new EksOidcService(
fixedStorageFactory(keyStore), new ObjectMapper());
EksService restarted = new EksService(fixedStorageFactory(clusterStore), testConfig(),
new RegionResolver("us-east-1", "000000000000"), null, null, oidcService);
new RegionResolver("us-east-1", "000000000000"), null, null, oidcService, mock(EksAccessEntryService.class));
restarted.init();
Cluster migrated = restarted.describeCluster("legacy-cluster");
@@ -251,7 +251,7 @@ class EksServiceTest {
EksOidcService oidcService = new EksOidcService(
fixedStorageFactory(keyStore), new ObjectMapper());
EksService restarted = new EksService(fixedStorageFactory(clusterStore), testConfig(),
new RegionResolver("us-east-1", "000000000000"), null, null, oidcService);
new RegionResolver("us-east-1", "000000000000"), null, null, oidcService, mock(EksAccessEntryService.class));
restarted.init();
// The issuer a trust policy was written against must survive a restart, and its key must
@@ -282,7 +282,7 @@ class EksServiceTest {
EksOidcService oidcService = new EksOidcService(
fixedStorageFactory(keyStore), new ObjectMapper());
new EksService(fixedStorageFactory(clusterStore), testConfig(),
new RegionResolver("us-east-1", "000000000000"), null, null, oidcService).init();
new RegionResolver("us-east-1", "000000000000"), null, null, oidcService, mock(EksAccessEntryService.class)).init();
Cluster migrated = clusterStore.getForAccount(otherAccount, "legacy-cluster").orElseThrow();
String issuer = migrated.getIdentity().getOidc().getIssuer();
@@ -316,7 +316,7 @@ class EksServiceTest {
EksService restarted = new EksService(fixedStorageFactory(clusterStore), testConfig(false),
new RegionResolver("us-east-1", "000000000000"), clusterManager, null,
new EksOidcService(fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>()),
new ObjectMapper()));
new ObjectMapper()), mock(EksAccessEntryService.class));
try {
restarted.init();
@@ -356,7 +356,7 @@ class EksServiceTest {
EksService restarted = new EksService(fixedStorageFactory(clusterStore), testConfig(false),
new RegionResolver("us-east-1", "000000000000"), clusterManager, null,
new EksOidcService(fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>()),
new ObjectMapper()));
new ObjectMapper()), mock(EksAccessEntryService.class));
try {
restarted.init();
@@ -390,7 +390,7 @@ class EksServiceTest {
EksService restarted = new EksService(fixedStorageFactory(clusterStore), testConfig(false),
new RegionResolver("us-east-1", "000000000000"), clusterManager, null,
new EksOidcService(fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>()),
new ObjectMapper()));
new ObjectMapper()), mock(EksAccessEntryService.class));
try {
restarted.init();
@@ -419,7 +419,7 @@ class EksServiceTest {
EksService restarted = new EksService(fixedStorageFactory(clusterStore), testConfig(false),
new RegionResolver("us-east-1", "000000000000"), clusterManager, null,
new EksOidcService(fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>()),
new ObjectMapper()));
new ObjectMapper()), mock(EksAccessEntryService.class));
try {
restarted.init();
@@ -446,7 +446,7 @@ class EksServiceTest {
EksService restarted = new EksService(fixedStorageFactory(clusterStore), testConfig(false),
new RegionResolver("us-east-1", "000000000000"), clusterManager, null,
new EksOidcService(fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>()),
new ObjectMapper()));
new ObjectMapper()), mock(EksAccessEntryService.class));
try {
restarted.init();
@@ -521,7 +521,7 @@ class EksServiceTest {
};
RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");
EksService service = new EksService(storageFactory, testConfig(), regionResolver, null, realEc2Service(),
new EksOidcService(storageFactory, new ObjectMapper()));
new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class));
ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig();
vpcConfig.setSubnetIds(List.of("subnet-1", "subnet-2"));
@@ -548,7 +548,7 @@ class EksServiceTest {
};
RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");
EksService service = new EksService(storageFactory, testConfig(), regionResolver, null, realEc2Service(),
new EksOidcService(storageFactory, new ObjectMapper()));
new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class));
ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig();
vpcConfig.setSubnetIds(List.of(Ec2Service.defaultSubnetId("us-east-1", "a"),
@@ -583,7 +583,7 @@ class EksServiceTest {
};
RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");
EksService service = new EksService(storageFactory, testConfig(), regionResolver, null,
realEc2Service(), new EksOidcService(storageFactory, new ObjectMapper()));
realEc2Service(), new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class));
ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig();
vpcConfig.setSubnetIds(List.of(Ec2Service.defaultSubnetId("us-east-1", "a"),
@@ -613,7 +613,7 @@ class EksServiceTest {
};
RegionResolver regionResolver = new RegionResolver("eu-west-2", "000000000000");
EksService service = new EksService(storageFactory, testConfig(), regionResolver, null,
realEc2Service(), new EksOidcService(storageFactory, new ObjectMapper()));
realEc2Service(), new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class));
ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig();
vpcConfig.setSubnetIds(List.of(Ec2Service.defaultSubnetId("eu-west-2", "a")));
@@ -660,7 +660,7 @@ class EksServiceTest {
// The request is for eu-west-2, where that subnet does not exist.
RegionResolver regionResolver = new RegionResolver("eu-west-2", "000000000000");
EksService service = new EksService(storageFactory, testConfig(), regionResolver, null,
ec2Service, new EksOidcService(storageFactory, new ObjectMapper()));
ec2Service, new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class));
ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig();
vpcConfig.setSubnetIds(List.of(usEastOnlySubnet));
@@ -991,7 +991,7 @@ class EksServiceTest {
};
return new EksService(storageFactory, testConfig(mock),
new RegionResolver("us-east-1", "000000000000"), clusterManager, null,
new EksOidcService(storageFactory, new ObjectMapper()));
new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class));
}
@Test
@@ -51,7 +51,7 @@ class EksSubResourceListsIntegrationTest {
void createCluster() {
given().contentType(JSON)
.body("{\"name\":\"" + CLUSTER + "\",\"roleArn\":\"arn:aws:iam::000000000000:role/eks-role\","
+ "\"version\":\"1.29\"}")
+ "\"version\":\"1.29\",\"accessConfig\":{\"authenticationMode\":\"API\",\"bootstrapClusterCreatorAdminPermissions\":false}}")
.when().post("/clusters")
.then().statusCode(200)
.body("cluster.name", equalTo(CLUSTER));