mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(eks): make real-mode clusters reachable and authenticable from the host (#1167)
In real mode `aws eks create-cluster` brought up a k3s container that was unreachable from the host and rejected every request with 401. This makes the native AWS workflow work end to end (`aws eks update-kubeconfig` + `kubectl`). - describe-cluster returns a host-reachable endpoint (https://localhost:<hostPort>), configurable via endpoint-mode (host|network), default host - token-authentication webhook (POST /_floci/eks/token-webhook) validates the `aws eks get-token` bearer token and maps it to system:masters; pins the apiserver to the v1 TokenReview API and echoes the request apiVersion - webhook kubeconfig is injected into k3s via the Docker API (create -> copy -> start), so it works in native and Docker-in-Docker with no host-path config - k3s network falls back to the global docker-network, then auto-detected Closes #1118
This commit is contained in:
1 parent
3989064a1c
commit
7128758a63
8 files changed
+405
-24
No files matched your search
@@ -0,0 +1,53 @@
|
||||
package io.github.hectorvent.floci.services.eks;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class EksClusterManagerTest {
|
||||
|
||||
@Test
|
||||
void webhookKubeconfigEmbedsServerUrl() {
|
||||
String url = "http://host.docker.internal:4566/_floci/eks/token-webhook";
|
||||
String yaml = EksClusterManager.buildWebhookKubeconfig(url);
|
||||
|
||||
assertTrue(yaml.contains("kind: Config"), "should be a kubeconfig");
|
||||
assertTrue(yaml.contains("server: " + url), "should point the webhook at Floci");
|
||||
assertTrue(yaml.contains("current-context: floci-token-webhook"),
|
||||
"should select the webhook context");
|
||||
}
|
||||
|
||||
@Test
|
||||
void webhookKubeconfigUsesContainerNetworkAddress() {
|
||||
String url = "http://172.18.0.5:4566/_floci/eks/token-webhook";
|
||||
String yaml = EksClusterManager.buildWebhookKubeconfig(url);
|
||||
assertTrue(yaml.contains("server: " + url));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hostModeAlwaysReturnsHostReachableEndpoint() {
|
||||
assertEquals("https://localhost:6500",
|
||||
EksClusterManager.resolvePublicEndpoint(true, "host", "floci-eks-demo", 6500));
|
||||
assertEquals("https://localhost:6500",
|
||||
EksClusterManager.resolvePublicEndpoint(false, "host", "floci-eks-demo", 6500));
|
||||
}
|
||||
|
||||
@Test
|
||||
void networkModeReturnsContainerDnsOnlyInContainer() {
|
||||
assertEquals("https://floci-eks-demo:6443",
|
||||
EksClusterManager.resolvePublicEndpoint(true, "network", "floci-eks-demo", 6500));
|
||||
// Native mode has no usable container DNS name — falls back to the host endpoint.
|
||||
assertEquals("https://localhost:6500",
|
||||
EksClusterManager.resolvePublicEndpoint(false, "network", "floci-eks-demo", 6500));
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointModeIsCaseInsensitiveAndDefaultsToHost() {
|
||||
assertEquals("https://floci-eks-demo:6443",
|
||||
EksClusterManager.resolvePublicEndpoint(true, "NETWORK", "floci-eks-demo", 6500));
|
||||
// Unknown / unset modes behave as host.
|
||||
assertEquals("https://localhost:6500",
|
||||
EksClusterManager.resolvePublicEndpoint(true, "bogus", "floci-eks-demo", 6500));
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package io.github.hectorvent.floci.services.eks;
|
||||
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class EksTokenWebhookControllerTest {
|
||||
|
||||
private final EksTokenWebhookController controller = new EksTokenWebhookController();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<String, Object> status(Response response) {
|
||||
Map<String, Object> body = (Map<String, Object>) response.getEntity();
|
||||
return (Map<String, Object>) body.get("status");
|
||||
}
|
||||
|
||||
private Map<String, Object> tokenReview(String token) {
|
||||
return Map.of(
|
||||
"apiVersion", "authentication.k8s.io/v1",
|
||||
"kind", "TokenReview",
|
||||
"spec", Map.of("token", token));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void awsIamTokenAuthenticatesAsClusterAdmin() {
|
||||
Response response = controller.review(tokenReview("k8s-aws-v1.aHR0cHM6Ly9zdHM..."));
|
||||
|
||||
Map<String, Object> status = status(response);
|
||||
assertEquals(Boolean.TRUE, status.get("authenticated"));
|
||||
|
||||
Map<String, Object> user = (Map<String, Object>) status.get("user");
|
||||
assertEquals(List.of("system:masters"), user.get("groups"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unrecognisedTokenIsRejected() {
|
||||
Response response = controller.review(tokenReview("some-random-bearer-token"));
|
||||
assertEquals(Boolean.FALSE, status(response).get("authenticated"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyOrMalformedReviewIsRejected() {
|
||||
assertFalse((Boolean) status(controller.review(Map.of())).get("authenticated"));
|
||||
assertFalse((Boolean) status(controller.review(
|
||||
Map.of("spec", Map.of()))).get("authenticated"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void responseIsAlwaysAWellFormedTokenReview() {
|
||||
Response response = controller.review(tokenReview("k8s-aws-v1.abc"));
|
||||
Map<?, ?> body = (Map<?, ?>) response.getEntity();
|
||||
assertEquals("authentication.k8s.io/v1", body.get("apiVersion"));
|
||||
assertEquals("TokenReview", body.get("kind"));
|
||||
assertTrue(body.containsKey("status"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void responseEchoesRequestApiVersion() {
|
||||
// The kube-apiserver defaults to the v1beta1 webhook API and cannot convert a v1
|
||||
// response back to v1beta1 — the response apiVersion MUST match the request's.
|
||||
Map<String, Object> v1beta1Review = Map.of(
|
||||
"apiVersion", "authentication.k8s.io/v1beta1",
|
||||
"kind", "TokenReview",
|
||||
"spec", Map.of("token", "k8s-aws-v1.abc"));
|
||||
|
||||
Response response = controller.review(v1beta1Review);
|
||||
Map<?, ?> body = (Map<?, ?>) response.getEntity();
|
||||
assertEquals("authentication.k8s.io/v1beta1", body.get("apiVersion"));
|
||||
assertEquals(Boolean.TRUE, status(response).get("authenticated"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user