@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 116 lines 4.5 KB
Raw
1 import * as Binding from "alchemy/Binding";
2 import type { Input } from "alchemy/Input";
3 import * as Output from "alchemy/Output";
4 import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
6 import * as Redacted from "effect/Redacted";
7 import * as GCPBindings from "../GCPBindings.ts";
8 import type { GcpHostBinding } from "../Host.ts";
9 import { CLOUD_SQL_MOUNT_PATH } from "../HostRuntime.ts";
10 import { grantFor } from "../HttpBinding.ts";
11 import { ReadSecret } from "../SecretManager/ReadSecret.ts";
12 import { ReadSecretHttp } from "../SecretManager/ReadSecretHttp.ts";
13 import { Connect, PasswordMissing, type ConnectionInfo, type ConnectOptions } from "./Connect.ts";
14 import type { Instance } from "./Instance.ts";
15
16 const socketUrl = (options: {
17 databaseVersion: string | undefined;
18 socketPath: string;
19 database: string;
20 username: string;
21 password: string;
22 }) => {
23 const mysql = (options.databaseVersion ?? "").toUpperCase().startsWith("MYSQL_");
24 // The socket travels as a query parameter; `localhost` only satisfies
25 // URL parsers that reject credentials with an empty host.
26 const url = new URL(
27 `${mysql ? "mysql" : "postgresql"}://localhost/${encodeURIComponent(options.database)}`,
28 );
29 url.username = encodeURIComponent(options.username);
30 url.password = encodeURIComponent(options.password);
31 url.searchParams.set(mysql ? "socketPath" : "host", options.socketPath);
32 return url.toString();
33 };
34
35 /**
36 * HTTP implementation of {@link Connect}: reads the password through
37 * `GCP.SecretManager.ReadSecret` and connects over the Cloud Run
38 * `cloudsql` socket volume.
39 *
40 * @layer
41 * @provides GCP.SQL.Connect
42 * @category SQL
43 */
44 export const ConnectHttp = Layer.effect(
45 Connect,
46 Effect.gen(function* () {
47 const bind = yield* GCPBindings.Bind;
48 const readSecret = yield* ReadSecret;
49 return Effect.fn(function* (instance: Instance, options: ConnectOptions) {
50 if (!globalThis.__ALCHEMY_RUNTIME__) {
51 // Connects over the socket volume Cloud Run mounts for the host.
52 yield* GCPBindings.requireServiceAccountHost("GCP.SQL.Connect");
53 yield* bind`GCP.SQL.Connect(${instance})`({
54 // Cloud SQL has no instance-level IAM policy; an IAM Condition
55 // scopes the project grant to this instance.
56 iam: [
57 grantFor(
58 { role: "roles/cloudsql.client", scopeByCondition: true },
59 Output.interpolate`projects/${instance.project}/instances/${instance.instanceName}`,
60 ),
61 ],
62 });
63 const host = yield* Binding.Host;
64 if (host !== undefined) {
65 const bindHost = (host as unknown as { bind: unknown }).bind as (
66 label: TemplateStringsArray,
67 ...args: unknown[]
68 ) => (data: {
69 cloudSqlInstances: Input<GcpHostBinding["cloudSqlInstances"]>;
70 }) => Effect.Effect<void>;
71 yield* bindHost`GCP.SQL.Socket(${instance})`({
72 cloudSqlInstances: [
73 Output.interpolate`${instance.project}:${instance.region}:${instance.instanceName}`,
74 ],
75 });
76 }
77 }
78 const password = yield* readSecret(options.passwordSecret);
79 const project = yield* instance.project;
80 const region = yield* instance.region;
81 const instanceName = yield* instance.instanceName;
82 const databaseVersion = yield* instance.databaseVersion;
83 const database = yield* options.database.databaseName;
84 const username = yield* options.user.userName;
85 const secretName = yield* options.passwordSecret.name;
86
87 return Effect.gen(function* () {
88 const value = yield* password.access();
89 if (value === undefined) {
90 return yield* new PasswordMissing({ secret: yield* secretName });
91 }
92 const connectionName = `${yield* project}:${yield* region}:${yield* instanceName}`;
93 const socketPath = `${CLOUD_SQL_MOUNT_PATH}/${connectionName}`;
94 const databaseName = yield* database;
95 const userName = yield* username;
96 const info: ConnectionInfo = {
97 connectionName,
98 socketPath,
99 database: databaseName,
100 username: userName,
101 password: Redacted.make(value),
102 url: Redacted.make(
103 socketUrl({
104 databaseVersion: yield* databaseVersion,
105 socketPath,
106 database: databaseName,
107 username: userName,
108 password: value,
109 }),
110 ),
111 };
112 return info;
113 });
114 });
115 }),
116 ).pipe(Layer.provide(ReadSecretHttp));