@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 113 lines 3.72 KB
Raw
1 import * as Kubernetes from "@alchemy.run/kubernetes";
2 import { connectCluster } from "@alchemy.run/kubernetes/Client";
3 import { expect, layer } from "@alchemy.run/test";
4 import * as NodeServices from "@effect/platform-node/NodeServices";
5 import * as Effect from "effect/Effect";
6 import * as FileSystem from "effect/FileSystem";
7 import * as Layer from "effect/Layer";
8 import * as Path from "effect/Path";
9
10 // The kubeconfig adapter resolved against a REAL kubeconfig file on disk,
11 // including a REAL exec credential plugin invocation (`echo` prints the
12 // ExecCredential JSON) — the same path `aws eks get-token` / `kubelogin` /
13 // `gke-gcloud-auth-plugin` contexts take.
14 const describe = layer(Layer.provideMerge(Kubernetes.KubeConfigAdapter, NodeServices.layer));
15
16 const CA_PEM = "-----BEGIN CERTIFICATE-----\nabc\n-----END CERTIFICATE-----\n";
17
18 const execCredential = JSON.stringify({
19 apiVersion: "client.authentication.k8s.io/v1",
20 kind: "ExecCredential",
21 status: { token: "exec-minted-token" },
22 });
23
24 const kubeconfig = `
25 apiVersion: v1
26 kind: Config
27 current-context: token-ctx
28 clusters:
29 - name: token-cluster
30 cluster:
31 server: https://token.example:6443
32 certificate-authority-data: ${Buffer.from(CA_PEM).toString("base64")}
33 - name: exec-cluster
34 cluster:
35 server: https://exec.example:6443
36 insecure-skip-tls-verify: true
37 users:
38 - name: token-user
39 user:
40 token: static-test-token
41 - name: exec-user
42 user:
43 exec:
44 apiVersion: client.authentication.k8s.io/v1
45 command: echo
46 args:
47 - '${execCredential}'
48 contexts:
49 - name: token-ctx
50 context:
51 cluster: token-cluster
52 user: token-user
53 - name: exec-ctx
54 context:
55 cluster: exec-cluster
56 user: exec-user
57 `;
58
59 const writeKubeconfig = Effect.gen(function* () {
60 const fs = yield* FileSystem.FileSystem;
61 const path = yield* Path.Path;
62 const dir = yield* fs.makeTempDirectory({ prefix: "alchemy-kubeconfig-" });
63 const file = path.join(dir, "config");
64 yield* fs.writeFileString(file, kubeconfig);
65 return file;
66 });
67
68 describe("Kubernetes.KubeConfig", (it) => {
69 it.effect(
70 "resolves the current-context's cluster and static token",
71 () =>
72 Effect.gen(function* () {
73 const file = yield* writeKubeconfig;
74 const transport = yield* connectCluster(Kubernetes.KubeConfig({ path: file }));
75 expect(transport.endpoint).toBe("https://token.example:6443");
76 expect(Buffer.from(transport.certificateAuthorityData!, "base64").toString("utf8")).toBe(
77 CA_PEM,
78 );
79 const headers = yield* transport.headers;
80 expect(headers.Authorization).toBe("Bearer static-test-token");
81 }),
82 { tags: ["unit", "provider:kubernetes", "local"] },
83 );
84
85 it.effect(
86 "mints credentials through an exec plugin context",
87 () =>
88 Effect.gen(function* () {
89 const file = yield* writeKubeconfig;
90 const transport = yield* connectCluster(
91 Kubernetes.KubeConfig({ path: file, context: "exec-ctx" }),
92 );
93 expect(transport.endpoint).toBe("https://exec.example:6443");
94 expect(transport.insecureSkipTlsVerify).toBe(true);
95 const headers = yield* transport.headers;
96 expect(headers.Authorization).toBe("Bearer exec-minted-token");
97 }),
98 { tags: ["unit", "provider:kubernetes", "local"] },
99 );
100
101 it.effect(
102 "an unknown context fails with a typed KubeConfigError",
103 () =>
104 Effect.gen(function* () {
105 const file = yield* writeKubeconfig;
106 const result = yield* Effect.result(
107 connectCluster(Kubernetes.KubeConfig({ path: file, context: "missing" })),
108 );
109 expect(result._tag).toBe("Failure");
110 }),
111 { tags: ["unit", "provider:kubernetes", "local"] },
112 );
113 });