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

main
ts 105 lines 3.95 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as RDS from "@alchemy.run/aws/RDS";
3 import * as Drizzle from "@alchemy.run/drizzle/Postgres";
4 import { sql } from "drizzle-orm";
5 import * as Cause from "effect/Cause";
6 import * as Duration from "effect/Duration";
7 import * as Effect from "effect/Effect";
8 import { HttpServerRequest } from "effect/http/HttpServerRequest";
9 import * as HttpServerResponse from "effect/http/HttpServerResponse";
10 import path from "pathe";
11 import { RdsConnectInfra } from "./connect-infra.ts";
12
13 const main = path.resolve(import.meta.dirname, "connect-handler.ts");
14
15 export class RdsConnectFunction extends Lambda.Function<Lambda.Function>()("RdsConnectFunction") {}
16
17 /**
18 * `RDS.Connect` bound to a standalone `DBInstance`, both ways:
19 *
20 * - no `secret` — reads the instance's RDS-managed master user secret
21 * (through the Secrets Manager interface endpoint) and connects as the
22 * master user.
23 * - `auth: "iam"` — grants `rds-db:connect` on the instance's
24 * `dbuser:{DbiResourceId}/app_iam` and presigns a token as the password.
25 */
26 export default RdsConnectFunction.make(
27 {
28 main,
29 functionUrl: true,
30 // First query per execution builds the pool + TLS handshake.
31 timeout: Duration.seconds(60),
32 },
33 Effect.gen(function* () {
34 const { network, lambdaSecurityGroup, instance } = yield* RdsConnectInfra;
35 const vpc = {
36 subnetIds: network.privateSubnetIds,
37 securityGroupIds: [lambdaSecurityGroup.groupId],
38 };
39
40 const connect = yield* RDS.Connect(instance, { database: "postgres", ...vpc });
41 const connectIam = yield* RDS.Connect(instance, {
42 auth: "iam",
43 username: "app_iam",
44 database: "postgres",
45 ...vpc,
46 });
47
48 const db = yield* Drizzle.Postgres(Effect.map(connect, (info) => info.url));
49 const iamDb = yield* Drizzle.Postgres(Effect.map(connectIam, (info) => info.url));
50
51 return {
52 fetch: Effect.gen(function* () {
53 const request = yield* HttpServerRequest;
54 const pathname = new URL(request.originalUrl).pathname;
55
56 // Connection settings from the managed secret. The password is
57 // never echoed — only its presence.
58 if (request.method === "GET" && pathname === "/connect-info") {
59 const info = yield* connect;
60 return yield* HttpServerResponse.json({
61 host: info.host,
62 port: info.port,
63 database: info.database,
64 username: info.username,
65 hasPassword: typeof info.password === "string" && info.password.length > 0,
66 ssl: info.ssl,
67 });
68 }
69
70 // Socket round-trip as the master user, which also bootstraps the
71 // IAM-auth database user (idempotent).
72 if (request.method === "POST" && pathname === "/setup-iam-user") {
73 yield* db.execute(
74 sql`DO $$ BEGIN CREATE USER app_iam WITH LOGIN; EXCEPTION WHEN duplicate_object THEN NULL; END $$`,
75 );
76 yield* db.execute(sql`GRANT rds_iam TO app_iam`);
77 const rows = yield* db.execute<{ user: string }>(
78 sql`select current_user as "user"`,
79 "objects",
80 );
81 return yield* HttpServerResponse.json({ rows });
82 }
83
84 // Socket round-trip authenticated with a presigned IAM token.
85 if (request.method === "GET" && pathname === "/iam-user") {
86 const rows = yield* iamDb.execute<{ user: string }>(
87 sql`select current_user as "user"`,
88 "objects",
89 );
90 return yield* HttpServerResponse.json({ rows });
91 }
92
93 return yield* HttpServerResponse.json(
94 { error: "Not found", method: request.method, pathname },
95 { status: 404 },
96 );
97 }).pipe(
98 // Surface the cause so a failing request explains itself in the test.
99 Effect.catchCause((cause) =>
100 HttpServerResponse.json({ error: Cause.pretty(cause) }, { status: 500 }),
101 ),
102 ),
103 };
104 }).pipe(Effect.provide(RDS.ConnectHttp)),
105 );