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

main
ts 553 lines 18.2 KB
Raw
1 import * as secretmanager from "@distilled.cloud/gcp/secretmanager_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import { tagRecord } from "alchemy/Tags";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as Schedule from "effect/Schedule";
11 import * as Stream from "effect/Stream";
12 import { GcpEnvironment } from "../Environment.ts";
13 import { DeleteNotConfirmed } from "../Errors.ts";
14 import {
15 createInternalLabels,
16 diffLabels,
17 hasAlchemyLabels,
18 stripInternalLabels,
19 toLabels,
20 } from "../Labels.ts";
21 import type { Providers } from "../Providers.ts";
22
23 export type CustomerManagedEncryption = {
24 /**
25 * Cloud KMS CryptoKey used to encrypt secret payloads
26 * (`projects/{project}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKey}`).
27 */
28 kmsKeyName?: string;
29 };
30
31 export type Replica = {
32 /**
33 * Replica location (e.g. `"us-central1"`).
34 */
35 location?: string;
36 /**
37 * Optional customer-managed encryption for this replica.
38 */
39 customerManagedEncryption?: CustomerManagedEncryption;
40 };
41
42 export type Replication = {
43 /**
44 * Replicate the secret without location restrictions.
45 */
46 automatic?: {
47 customerManagedEncryption?: CustomerManagedEncryption;
48 };
49 /**
50 * Replicate the secret only into the listed locations.
51 */
52 userManaged?: {
53 replicas?: Replica[];
54 };
55 };
56
57 export type Rotation = {
58 /**
59 * Timestamp in UTC when the next rotation notification should fire.
60 */
61 nextRotationTime?: string;
62 /**
63 * Period between rotation notifications (e.g. `"3600s"`).
64 */
65 rotationPeriod?: string;
66 };
67
68 export type SecretTopic = {
69 /**
70 * Pub/Sub topic resource name (`projects/{project}/topics/{topic}`).
71 */
72 name?: string;
73 };
74
75 export type SecretProps = {
76 /**
77 * Secret id (the `{secret}` segment of
78 * `projects/{project}/secrets/{secret}`). If omitted, a unique name is
79 * generated from the stack, stage, and logical id. Max 255 characters;
80 * letters, numbers, hyphens, and underscores. Changing it replaces the
81 * secret.
82 */
83 secretId?: string;
84 /**
85 * User labels. Alchemy ownership labels are merged in automatically.
86 */
87 labels?: Record<string, string>;
88 /**
89 * Custom metadata. Distinct from labels; not used for ownership.
90 */
91 annotations?: Record<string, string>;
92 /**
93 * Immutable replication policy. Defaults to automatic replication.
94 * Changing it replaces the secret.
95 * @default { automatic: {} }
96 */
97 replication?: Replication;
98 /**
99 * Timestamp in UTC when the secret expires.
100 */
101 expireTime?: string;
102 /**
103 * Input-only TTL used to compute `expireTime` (e.g. `"86400s"`). Ignored
104 * when `expireTime` is set.
105 */
106 ttl?: string;
107 /**
108 * Pub/Sub topics notified on control-plane events. Required when
109 * `rotation` is set.
110 */
111 topics?: SecretTopic[];
112 /**
113 * Rotation policy. Requires `topics`.
114 */
115 rotation?: Rotation;
116 /**
117 * Mapping from version alias to version id.
118 */
119 versionAliases?: Record<string, string>;
120 /**
121 * Delay before a destroyed version is permanently deleted (e.g.
122 * `"86400s"`).
123 */
124 versionDestroyTtl?: string;
125 };
126
127 export type Secret = Resource<
128 "GCP.SecretManager.Secret",
129 SecretProps,
130 {
131 /** Full resource name `projects/{project}/secrets/{secret}`. */
132 name: string;
133 /** Secret id (last path segment). */
134 secretId: string;
135 /** Project id. */
136 project: string;
137 /** User labels (Alchemy ownership labels stripped). */
138 labels: Record<string, string>;
139 /** Custom annotations. */
140 annotations: Record<string, string>;
141 /** Replication policy. Immutable after create. */
142 replication: Replication | undefined;
143 /** Expiration timestamp, if set. */
144 expireTime: string | undefined;
145 /** RFC3339 creation timestamp. */
146 createTime: string | undefined;
147 /** Server etag. */
148 etag: string | undefined;
149 /** Pub/Sub notification topics. */
150 topics: SecretTopic[];
151 /** Rotation policy, if set. */
152 rotation: Rotation | undefined;
153 /** Version alias map. */
154 versionAliases: Record<string, string>;
155 /** Delayed-destroy TTL, if set. */
156 versionDestroyTtl: string | undefined;
157 },
158 never,
159 Providers
160 >;
161
162 /**
163 * A Google Cloud Secret Manager secret (metadata and replication). Secret
164 * payloads live on versions — use `AddSecretVersion` and
165 * `AccessSecretVersion` to write and read them.
166 *
167 * Changing `secretId` or `replication` replaces the secret.
168 *
169 * ### Creating a Secret
170 * **Example:** Generated name
171 * ```typescript
172 * const secret = yield* GCP.SecretManager.Secret("ApiKey", {});
173 * ```
174 *
175 * **Example:** Explicit id, labels, and annotations
176 * ```typescript
177 * const secret = yield* GCP.SecretManager.Secret("ApiKey", {
178 * secretId: "order-api-key",
179 * labels: { env: "prod" },
180 * annotations: { owner: "payments" },
181 * });
182 * ```
183 *
184 * ### Replication
185 * **Example:** User-managed replicas
186 * ```typescript
187 * const secret = yield* GCP.SecretManager.Secret("RegionalKey", {
188 * replication: {
189 * userManaged: {
190 * replicas: [{ location: "us-central1" }],
191 * },
192 * },
193 * });
194 * ```
195 *
196 * ### Secret Versions
197 * **Example:** Add and access a version
198 * ```typescript
199 * const addVersion = yield* GCP.SecretManager.AddSecretVersion(secret);
200 * yield* addVersion({ payload: { data: btoa("hello") } });
201 * const access = yield* GCP.SecretManager.AccessSecretVersion(secret);
202 * const { payload } = yield* access();
203 * ```
204 *
205 * @resource
206 * @category SecretManager
207 */
208 export const Secret = Resource<Secret>("GCP.SecretManager.Secret");
209
210 export class SecretNotResolved extends Data.TaggedError("GCP.SecretManager.SecretNotResolved")<{
211 resourceName: string;
212 }> {}
213
214 const DEFAULT_REPLICATION: Replication = { automatic: {} };
215
216 const secretIdOf = (name: string) => name.split("/").pop() ?? name;
217
218 const resourceName = (project: string, secretId: string) =>
219 `projects/${project}/secrets/${secretId}`;
220
221 const userLabels = (
222 labels: Record<string, string | undefined> | null | undefined,
223 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
224
225 const userAnnotations = (
226 annotations: Record<string, string | undefined> | null | undefined,
227 ): Record<string, string> => tagRecord(annotations);
228
229 const userAliases = (
230 aliases: Record<string, string | undefined> | null | undefined,
231 ): Record<string, string> => tagRecord(aliases);
232
233 const toId = (id: string, secretId: string | undefined, existing?: string) =>
234 Effect.gen(function* () {
235 return (
236 secretId ?? existing ?? (yield* createPhysicalName({ id, maxLength: 255, lowercase: true }))
237 );
238 });
239
240 const toTopics = (topics: secretmanager.TopicList | null | undefined) =>
241 (topics ?? [])
242 .filter((topic): topic is { name: string } => typeof topic.name === "string")
243 .map((topic) => ({ name: topic.name }));
244
245 const toRotation = (rotation: secretmanager.Rotation | undefined): Rotation | undefined => {
246 if (rotation === undefined) return undefined;
247 if (rotation.nextRotationTime === undefined && rotation.rotationPeriod === undefined) {
248 return undefined;
249 }
250 return {
251 nextRotationTime: rotation.nextRotationTime,
252 rotationPeriod: rotation.rotationPeriod,
253 };
254 };
255
256 const toReplication = (
257 replication: secretmanager.Replication | undefined,
258 ): Replication | undefined => {
259 if (replication === undefined) return undefined;
260 if (replication.userManaged !== undefined) {
261 return {
262 userManaged: {
263 replicas: (replication.userManaged.replicas ?? []).map((replica) => ({
264 location: replica.location,
265 customerManagedEncryption: replica.customerManagedEncryption
266 ? {
267 kmsKeyName: replica.customerManagedEncryption.kmsKeyName,
268 }
269 : undefined,
270 })),
271 },
272 };
273 }
274 if (replication.automatic !== undefined) {
275 return {
276 automatic: {
277 customerManagedEncryption: replication.automatic.customerManagedEncryption
278 ? {
279 kmsKeyName: replication.automatic.customerManagedEncryption.kmsKeyName,
280 }
281 : undefined,
282 },
283 };
284 }
285 return undefined;
286 };
287
288 const desiredReplication = (replication: Replication | undefined): Replication =>
289 replication ?? DEFAULT_REPLICATION;
290
291 const replicationFingerprint = (replication: Replication | undefined): string => {
292 const value = desiredReplication(replication);
293 if (value.userManaged !== undefined) {
294 const replicas = [...(value.userManaged.replicas ?? [])]
295 .map((replica) => ({
296 location: replica.location ?? "",
297 kms: replica.customerManagedEncryption?.kmsKeyName ?? "",
298 }))
299 .sort((a, b) => a.location.localeCompare(b.location));
300 return JSON.stringify({ userManaged: replicas });
301 }
302 return JSON.stringify({
303 automatic: {
304 kms: value.automatic?.customerManagedEncryption?.kmsKeyName ?? "",
305 },
306 });
307 };
308
309 const recordsEqual = (left: Record<string, string>, right: Record<string, string>) => {
310 const leftKeys = Object.keys(left).sort();
311 const rightKeys = Object.keys(right).sort();
312 return (
313 leftKeys.length === rightKeys.length &&
314 leftKeys.every((key, index) => key === rightKeys[index] && left[key] === right[key])
315 );
316 };
317
318 const topicsEqual = (left: SecretTopic[], right: SecretTopic[]) => {
319 const a = left.map((topic) => topic.name ?? "").sort();
320 const b = right.map((topic) => topic.name ?? "").sort();
321 return a.length === b.length && a.every((name, index) => name === b[index]);
322 };
323
324 const rotationEqual = (left: Rotation | undefined, right: Rotation | undefined) =>
325 (left?.nextRotationTime ?? "") === (right?.nextRotationTime ?? "") &&
326 (left?.rotationPeriod ?? "") === (right?.rotationPeriod ?? "");
327
328 const toAttrs = (secret: secretmanager.Secret, project: string) => {
329 const name = secret.name ?? "";
330 return {
331 name,
332 secretId: secretIdOf(name),
333 project,
334 labels: userLabels(secret.labels),
335 annotations: userAnnotations(secret.annotations),
336 replication: toReplication(secret.replication),
337 expireTime: secret.expireTime,
338 createTime: secret.createTime,
339 etag: secret.etag,
340 topics: toTopics(secret.topics),
341 rotation: toRotation(secret.rotation),
342 versionAliases: userAliases(secret.versionAliases),
343 versionDestroyTtl: secret.versionDestroyTtl,
344 };
345 };
346
347 const getByName = (name: string) =>
348 secretmanager
349 .getProjectsSecrets({ name })
350 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
351
352 const waitUntilMissing = (name: string) =>
353 getByName(name).pipe(
354 Effect.repeat({
355 schedule: Schedule.spaced("1 second"),
356 until: (secret): boolean => secret === undefined,
357 times: 30,
358 }),
359 Effect.flatMap((secret): Effect.Effect<void, DeleteNotConfirmed> =>
360 secret === undefined ? Effect.void : Effect.fail(new DeleteNotConfirmed({ resource: name })),
361 ),
362 );
363
364 const toCreateBody = (news: SecretProps, labels: Record<string, string>): secretmanager.Secret => ({
365 replication: desiredReplication(news.replication),
366 labels,
367 annotations: news.annotations,
368 expireTime: news.expireTime,
369 ttl: news.expireTime === undefined ? news.ttl : undefined,
370 topics: news.topics,
371 rotation: news.rotation,
372 versionAliases: news.versionAliases,
373 versionDestroyTtl: news.versionDestroyTtl,
374 });
375
376 export const SecretProvider = () =>
377 Provider.succeed(Secret, {
378 stables: ["name", "secretId", "project", "createTime", "replication"],
379
380 diff: Effect.fn(function* ({ news, olds, output }) {
381 if (!isResolved(news)) return undefined;
382 const previousId = olds?.secretId ?? output?.secretId;
383 const nextId = news.secretId ?? previousId;
384 const idChanged =
385 previousId !== undefined && news.secretId !== undefined && previousId !== news.secretId;
386 const replicationChanged =
387 replicationFingerprint(olds?.replication ?? output?.replication) !==
388 replicationFingerprint(news.replication);
389
390 if (!idChanged && !replicationChanged) return undefined;
391 return {
392 action: "replace" as const,
393 deleteFirst:
394 replicationChanged && !idChanged && previousId !== undefined && nextId === previousId,
395 };
396 }),
397
398 read: Effect.fn(function* ({ id, olds, output }) {
399 const env = yield* GcpEnvironment.current;
400 const secretId = yield* toId(id, olds?.secretId, output?.secretId);
401 const name = output?.name ?? resourceName(env.project, secretId);
402 const existing = yield* getByName(name);
403 if (existing === undefined) return undefined;
404 const attrs = toAttrs(existing, env.project);
405 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
406 }),
407
408 list: () =>
409 Effect.gen(function* () {
410 const env = yield* GcpEnvironment.current;
411 const pages = yield* secretmanager.listProjectsSecrets
412 .pages({
413 parent: `projects/${env.project}`,
414 pageSize: 1000,
415 filter: "labels.alchemy-id:*",
416 })
417 .pipe(Stream.runCollect);
418 return Array.from(pages).flatMap((page) =>
419 (page.secrets ?? [])
420 .filter((secret) =>
421 Object.keys(secret.labels ?? {}).some((key) => key.startsWith("alchemy-")),
422 )
423 .map((secret) => toAttrs(secret, env.project)),
424 );
425 }),
426
427 reconcile: Effect.fn(function* ({ id, news, output }) {
428 const env = yield* GcpEnvironment.current;
429 const secretId = yield* toId(id, news.secretId, output?.secretId);
430 const name = resourceName(env.project, secretId);
431 const desiredLabels = {
432 ...toLabels(news.labels),
433 ...(yield* createInternalLabels(id)),
434 };
435 const desiredAnnotations = news.annotations ?? {};
436 const desiredAliases = news.versionAliases ?? {};
437 const desiredTopics = news.topics ?? [];
438 const desiredRotation = news.rotation;
439 const desiredDestroyTtl = news.versionDestroyTtl;
440
441 let current = yield* getByName(name);
442 const desiredReplicationFp = replicationFingerprint(news.replication);
443
444 // Replication is immutable. If the observed secret has a different
445 // policy (replacement or adoption), delete and recreate.
446 if (
447 current !== undefined &&
448 replicationFingerprint(toReplication(current.replication)) !== desiredReplicationFp
449 ) {
450 yield* secretmanager
451 .deleteProjectsSecrets({ name: current.name ?? name })
452 .pipe(Effect.catchTag("NotFound", () => Effect.void));
453 yield* waitUntilMissing(name);
454 current = undefined;
455 }
456
457 if (current === undefined) {
458 const created = yield* secretmanager
459 .createProjectsSecrets({
460 parent: `projects/${env.project}`,
461 secretId,
462 body: toCreateBody(news, desiredLabels),
463 })
464 .pipe(
465 // A just-deleted secret (replacement) keeps its id reserved for
466 // a few seconds after reads report it missing.
467 Effect.retry({
468 while: (error) => error._tag === "Conflict",
469 schedule: Schedule.spaced("1 second"),
470 times: 10,
471 }),
472 Effect.catchTag("Conflict", () => getByName(name)),
473 );
474 current = created ?? undefined;
475 }
476
477 if (current === undefined) {
478 return yield* new SecretNotResolved({ resourceName: name });
479 }
480
481 if (replicationFingerprint(toReplication(current.replication)) !== desiredReplicationFp) {
482 return yield* new SecretNotResolved({ resourceName: name });
483 }
484
485 const observedLabels = tagRecord(current.labels);
486 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
487 const labelsChanged = upsert.length > 0 || removed.length > 0;
488 const annotationsChanged = !recordsEqual(
489 userAnnotations(current.annotations),
490 desiredAnnotations,
491 );
492 const aliasesChanged = !recordsEqual(userAliases(current.versionAliases), desiredAliases);
493 const topicsChanged = !topicsEqual(toTopics(current.topics), desiredTopics);
494 const rotationChanged = !rotationEqual(toRotation(current.rotation), desiredRotation);
495 const destroyTtlChanged = (current.versionDestroyTtl ?? "") !== (desiredDestroyTtl ?? "");
496 const expireTimeChanged =
497 news.expireTime !== undefined && (current.expireTime ?? "") !== news.expireTime;
498 // `ttl` is input-only and recomputes expireTime from now. Apply it only
499 // when the observed secret has no expiration, otherwise every reconcile
500 // would reset the clock.
501 const ttlChanged =
502 news.expireTime === undefined &&
503 news.ttl !== undefined &&
504 (current.expireTime === undefined || current.expireTime === "");
505
506 if (
507 labelsChanged ||
508 annotationsChanged ||
509 aliasesChanged ||
510 topicsChanged ||
511 rotationChanged ||
512 destroyTtlChanged ||
513 expireTimeChanged ||
514 ttlChanged
515 ) {
516 current = yield* secretmanager.patchProjectsSecrets({
517 name,
518 updateMask: [
519 labelsChanged ? "labels" : undefined,
520 annotationsChanged ? "annotations" : undefined,
521 aliasesChanged ? "versionAliases" : undefined,
522 topicsChanged ? "topics" : undefined,
523 rotationChanged ? "rotation" : undefined,
524 destroyTtlChanged ? "versionDestroyTtl" : undefined,
525 expireTimeChanged ? "expireTime" : undefined,
526 ttlChanged ? "ttl" : undefined,
527 ]
528 .filter((field): field is string => field !== undefined)
529 .join(","),
530 body: {
531 name,
532 labels: desiredLabels,
533 annotations: desiredAnnotations,
534 versionAliases: desiredAliases,
535 topics: desiredTopics,
536 rotation: desiredRotation,
537 versionDestroyTtl: desiredDestroyTtl,
538 expireTime: news.expireTime,
539 ttl: ttlChanged ? news.ttl : undefined,
540 },
541 });
542 }
543
544 return toAttrs(current, env.project);
545 }),
546
547 delete: Effect.fn(function* ({ output }) {
548 yield* secretmanager
549 .deleteProjectsSecrets({ name: output.name })
550 .pipe(Effect.catchTag("NotFound", () => Effect.void));
551 yield* waitUntilMissing(output.name);
552 }),
553 });