@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, 04:45 UTC

main
ts 112 lines 4.28 KB
Raw
1 import * as secretmanager from "@distilled.cloud/gcp/secretmanager_v1";
2 import * as Effect from "effect/Effect";
3 import * as GCPBindings from "../GCPBindings.ts";
4 import { grantFor, type BindingIam } from "../HttpBinding.ts";
5 import type { SecretBindingTarget } from "./BindingHttp.ts";
6 import type { ReadSecretClient } from "./ReadSecret.ts";
7 import type { WriteSecretClient } from "./WriteSecret.ts";
8
9 /**
10 * Shared HTTP scaffolding for the Secret Manager Read/Write/ReadWrite
11 * bindings. NOT exported from `index.ts`.
12 */
13
14 export const readSecretGrants: BindingIam[] = [
15 { role: "roles/secretmanager.secretAccessor", on: "secretmanager.secret" },
16 ];
17
18 export const writeSecretGrants: BindingIam[] = [
19 {
20 role: "roles/secretmanager.secretVersionManager",
21 on: "secretmanager.secret",
22 },
23 ];
24
25 // No predefined role covers access + version management short of
26 // secretmanager.admin, so ReadWrite grants both narrow roles.
27 export const readWriteSecretGrants: BindingIam[] = [...readSecretGrants, ...writeSecretGrants];
28
29 const versionName = (secretName: string, version: string) =>
30 version.includes("/versions/") ? version : `${secretName}/versions/${version}`;
31
32 export const makeSecretHelpers = Effect.gen(function* () {
33 const access = yield* GCPBindings.withCredentials(secretmanager.accessProjectsSecretsVersions);
34 const addVersion = yield* GCPBindings.withCredentials(secretmanager.addVersionProjectsSecrets);
35 const disable = yield* GCPBindings.withCredentials(secretmanager.disableProjectsSecretsVersions);
36 const destroy = yield* GCPBindings.withCredentials(secretmanager.destroyProjectsSecretsVersions);
37
38 const makeRead = (secretName: Effect.Effect<string>): ReadSecretClient => {
39 const accessBytes = (version?: string) =>
40 Effect.gen(function* () {
41 const response = yield* access({
42 name: versionName(yield* secretName, version ?? "latest"),
43 });
44 const data = response.payload?.data ?? "";
45 return yield* Effect.sync(() => new Uint8Array(Buffer.from(data, "base64")));
46 }).pipe(
47 // Missing, disabled, and destroyed versions have no readable payload.
48 Effect.catchTag(["NotFound", "SecretVersionNotEnabled"], () => Effect.undefined),
49 );
50 return {
51 accessBytes,
52 access: (version) =>
53 accessBytes(version).pipe(
54 Effect.map((bytes) =>
55 bytes === undefined ? undefined : new TextDecoder().decode(bytes),
56 ),
57 ),
58 };
59 };
60
61 const makeWrite = (secretName: Effect.Effect<string>): WriteSecretClient => ({
62 addVersion: (value) =>
63 Effect.gen(function* () {
64 const data = yield* Effect.sync(() =>
65 (typeof value === "string"
66 ? Buffer.from(value, "utf8")
67 : Buffer.from(value.buffer, value.byteOffset, value.byteLength)
68 ).toString("base64"),
69 );
70 const version = yield* addVersion({
71 parent: yield* secretName,
72 body: { payload: { data } },
73 });
74 return version.name ?? "";
75 }),
76 disableVersion: (version) =>
77 Effect.gen(function* () {
78 yield* disable({ name: versionName(yield* secretName, version) });
79 }),
80 destroyVersion: (version) =>
81 Effect.gen(function* () {
82 yield* destroy({ name: versionName(yield* secretName, version) });
83 }).pipe(Effect.catchTag("NotFound", () => Effect.void)),
84 });
85
86 return { makeRead, makeWrite };
87 });
88
89 /** Build a secret binding that grants `grants` and returns `makeClient`'s client. */
90 export const makeSecretAccessBinding = <Client>(options: {
91 tag: string;
92 grants: BindingIam[];
93 makeClient: (
94 helpers: Effect.Success<typeof makeSecretHelpers>,
95 secretName: Effect.Effect<string>,
96 ) => Client;
97 }) =>
98 Effect.gen(function* () {
99 const bind = yield* GCPBindings.Bind;
100 const helpers = yield* makeSecretHelpers;
101 return Effect.fn(function* (secret: SecretBindingTarget) {
102 // `name` is `projects/p/secrets/s` or, for regional secrets,
103 // `projects/p/locations/l/secrets/s`; both are secret IAM targets.
104 if (!globalThis.__ALCHEMY_RUNTIME__) {
105 yield* bind`${options.tag}(${secret})`({
106 iam: options.grants.map((grant) => grantFor(grant, secret.name)),
107 });
108 }
109 const secretName = yield* secret.name;
110 return options.makeClient(helpers, secretName);
111 });
112 });