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

main
ts 202 lines 7.38 KB
Raw
1 /**
2 * The built-in {@link ClusterAdapter} implementations shipped by
3 * `Kubernetes.providers()`: `kubeconfig`, `token`, `client-cert`, and
4 * `exec`. They cover any cluster `kubectl` can reach; managed-cloud
5 * adapters (EKS's `aws-eks`) are contributed by their provider layers.
6 *
7 * These adapters are auth-only — no workload identity and no managed image
8 * registry. Workloads on such clusters bind through environment variables
9 * and run pre-built `image` references (or images pushed by the user's own
10 * pipeline).
11 */
12
13 import * as Data from "effect/Data";
14 import * as Effect from "effect/Effect";
15 import * as FileSystem from "effect/FileSystem";
16 import * as Layer from "effect/Layer";
17 import * as Path from "effect/Path";
18 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
19 import {
20 ClusterAdapter,
21 type ClusterAdapterService,
22 type ClusterTransport,
23 } from "./ClusterAdapter.ts";
24 import type { Connection } from "./Connection.ts";
25 import {
26 mintUserCredentials,
27 resolveKubeContext,
28 runExecCredential,
29 type MintedCredentials,
30 } from "./internal/kubeconfig.ts";
31
32 export class BuiltinAdaptersInvalidProps extends Data.TaggedError("BuiltinAdaptersInvalidProps")<{
33 readonly message: string;
34 readonly cause?: unknown;
35 }> {}
36
37 const toAuthHeaders = (credentials: MintedCredentials): Record<string, string> =>
38 credentials.token ? { Authorization: `Bearer ${credentials.token}` } : {};
39
40 const requireEndpoint = (connection: Connection) =>
41 connection.endpoint !== undefined
42 ? Effect.succeed(connection.endpoint)
43 : Effect.fail(
44 new BuiltinAdaptersInvalidProps({
45 message:
46 `A '${connection.auth.kind}' Kubernetes connection requires an ` +
47 "explicit `endpoint` (only kubeconfig-backed and managed-cloud " +
48 "connections can discover it)",
49 }),
50 );
51
52 /**
53 * `kubeconfig` — resolve the cluster and credentials from a kubeconfig
54 * file, honoring static tokens, client certificates, and `exec` credential
55 * plugins.
56 */
57 export const KubeConfigAdapter: Layer.Layer<
58 ClusterAdapterService,
59 never,
60 FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner
61 > = Layer.effect(
62 ClusterAdapter("kubeconfig"),
63 Effect.gen(function* () {
64 // Capture the platform services at layer build so per-request minting
65 // (exec plugins can re-run on token expiry) needs no ambient context.
66 const context = yield* Effect.context<
67 FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner
68 >();
69
70 return {
71 kind: "Kubernetes.ClusterAdapter" as const,
72 connect: Effect.fn(function* (connection: Connection) {
73 if (connection.auth.kind !== "kubeconfig") {
74 return yield* Effect.die(
75 new Error(`kubeconfig adapter received auth kind '${connection.auth.kind}'`),
76 );
77 }
78 const resolved = yield* resolveKubeContext({
79 path: connection.auth.path,
80 context: connection.auth.context,
81 }).pipe(Effect.provideContext(context));
82
83 // Mint once at connect: static tokens and client certificates are
84 // stable; exec plugins re-mint per request below (their tokens
85 // expire) but any plugin-issued client certificate is captured
86 // here.
87 const initial = yield* mintUserCredentials(resolved.user).pipe(
88 Effect.provideContext(context),
89 );
90
91 const isExec = resolved.user.exec?.command !== undefined;
92 const headers: ClusterTransport["headers"] = isExec
93 ? mintUserCredentials(resolved.user).pipe(
94 Effect.map(toAuthHeaders),
95 Effect.provideContext(context),
96 )
97 : Effect.succeed(toAuthHeaders(initial));
98
99 return {
100 endpoint: connection.endpoint ?? resolved.endpoint,
101 certificateAuthorityData:
102 connection.certificateAuthorityData ?? resolved.certificateAuthorityData,
103 insecureSkipTlsVerify: connection.insecureSkipTlsVerify ?? resolved.insecureSkipTlsVerify,
104 headers,
105 clientCert: initial.clientCert,
106 } satisfies ClusterTransport;
107 }),
108 };
109 }),
110 );
111
112 /** `token` — a static bearer token against an explicit endpoint. */
113 export const TokenAdapter: Layer.Layer<ClusterAdapterService> = Layer.succeed(
114 ClusterAdapter("token"),
115 {
116 kind: "Kubernetes.ClusterAdapter" as const,
117 connect: Effect.fn(function* (connection: Connection) {
118 if (connection.auth.kind !== "token") {
119 return yield* Effect.die(
120 new Error(`token adapter received auth kind '${connection.auth.kind}'`),
121 );
122 }
123 const token = connection.auth.token;
124 return {
125 endpoint: yield* requireEndpoint(connection),
126 certificateAuthorityData: connection.certificateAuthorityData,
127 insecureSkipTlsVerify: connection.insecureSkipTlsVerify,
128 headers: Effect.succeed({ Authorization: `Bearer ${token}` }),
129 } satisfies ClusterTransport;
130 }),
131 },
132 );
133
134 /** `client-cert` — mutual TLS against an explicit endpoint. */
135 export const ClientCertAdapter: Layer.Layer<ClusterAdapterService> = Layer.succeed(
136 ClusterAdapter("client-cert"),
137 {
138 kind: "Kubernetes.ClusterAdapter" as const,
139 connect: Effect.fn(function* (connection: Connection) {
140 if (connection.auth.kind !== "client-cert") {
141 return yield* Effect.die(
142 new Error(`client-cert adapter received auth kind '${connection.auth.kind}'`),
143 );
144 }
145 return {
146 endpoint: yield* requireEndpoint(connection),
147 certificateAuthorityData: connection.certificateAuthorityData,
148 insecureSkipTlsVerify: connection.insecureSkipTlsVerify,
149 headers: Effect.succeed({}),
150 clientCert: {
151 certificate: connection.auth.certificate,
152 key: connection.auth.key,
153 },
154 } satisfies ClusterTransport;
155 }),
156 },
157 );
158
159 /**
160 * `exec` — mint credentials with a kubeconfig-style exec credential plugin
161 * against an explicit endpoint.
162 */
163 export const ExecAdapter: Layer.Layer<
164 ClusterAdapterService,
165 never,
166 ChildProcessSpawner.ChildProcessSpawner
167 > = Layer.effect(
168 ClusterAdapter("exec"),
169 Effect.gen(function* () {
170 const context = yield* Effect.context<ChildProcessSpawner.ChildProcessSpawner>();
171
172 return {
173 kind: "Kubernetes.ClusterAdapter" as const,
174 connect: Effect.fn(function* (connection: Connection) {
175 if (connection.auth.kind !== "exec") {
176 return yield* Effect.die(
177 new Error(`exec adapter received auth kind '${connection.auth.kind}'`),
178 );
179 }
180 const exec = connection.auth;
181 const initial = yield* runExecCredential(exec).pipe(Effect.provideContext(context));
182 return {
183 endpoint: yield* requireEndpoint(connection),
184 certificateAuthorityData: connection.certificateAuthorityData,
185 insecureSkipTlsVerify: connection.insecureSkipTlsVerify,
186 headers: runExecCredential(exec).pipe(
187 Effect.map(toAuthHeaders),
188 Effect.provideContext(context),
189 ),
190 clientCert: initial.clientCert,
191 } satisfies ClusterTransport;
192 }),
193 };
194 }),
195 );
196
197 /** All built-in adapters, merged for `Kubernetes.providers()`. */
198 export const builtinAdapters = (): Layer.Layer<
199 ClusterAdapterService,
200 never,
201 FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner
202 > => Layer.mergeAll(KubeConfigAdapter, TokenAdapter, ClientCertAdapter, ExecAdapter);