@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, 06:00 UTC

main
ts 123 lines 4.66 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as rds from "@distilled.cloud/aws/rds";
5 import { Region } from "@distilled.cloud/aws/Region";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as HttpClientRequest from "effect/http/HttpClientRequest";
10 import * as Schedule from "effect/Schedule";
11 import RdsConnectFunctionLive, { RdsConnectFunction } from "./fixtures/connect-handler.ts";
12 import { RdsConnectInfra } from "./fixtures/connect-infra.ts";
13
14 const { test } = Test.make({ providers: AWS.providers() });
15
16 class NotReady extends Data.TaggedError("NotReady")<{
17 readonly message: string;
18 }> {}
19
20 // A fresh function URL (DNS, IAM propagation, VPC ENI attachment) and the
21 // first TLS handshake to a just-available instance take a while to settle.
22 const send = (request: HttpClientRequest.HttpClientRequest) =>
23 HttpClient.execute(request).pipe(
24 Effect.flatMap((response) =>
25 response.status === 200
26 ? response.json
27 : Effect.flatMap(response.text, (body) =>
28 Effect.fail(
29 new NotReady({
30 message: `${request.method} ${request.url} -> ${response.status}: ${body}`,
31 }),
32 ),
33 ),
34 ),
35 Effect.tapError((error) => Effect.logWarning(error.message)),
36 Effect.retry({ schedule: Schedule.spaced("5 seconds"), times: 36 }),
37 );
38
39 // Deploy (postgres db.t3.micro + VPC Lambda) measured ~12 minutes; destroy
40 // waits up to ~25 minutes for Lambda to release its VPC ENIs.
41 test.provider.skipIf(!!process.env.FAST)(
42 "connects to a DBInstance with its managed master secret and IAM auth",
43 (stack) =>
44 Effect.gen(function* () {
45 yield* stack.destroy();
46
47 const region = yield* yield* Region;
48 const { functionUrl, dbInstanceIdentifier, endpointAddress } = yield* stack.deploy(
49 Effect.gen(function* () {
50 const { network, endpointSecurityGroup, instance } = yield* RdsConnectInfra;
51 // The Lambda sits in isolated subnets; it reaches Secrets Manager
52 // through an interface endpoint.
53 yield* AWS.EC2.VpcEndpoint("ConnectSecretsManagerEndpoint", {
54 vpcId: network.vpcId,
55 serviceName: `com.amazonaws.${region}.secretsmanager`,
56 vpcEndpointType: "Interface",
57 subnetIds: network.privateSubnetIds,
58 securityGroupIds: [endpointSecurityGroup.groupId],
59 privateDnsEnabled: true,
60 });
61 const fn = yield* RdsConnectFunction;
62 return {
63 functionUrl: fn.functionUrl,
64 dbInstanceIdentifier: instance.dbInstanceIdentifier,
65 endpointAddress: instance.endpointAddress,
66 };
67 }).pipe(Effect.provide(RdsConnectFunctionLive)),
68 );
69 expect(functionUrl).toBeTruthy();
70 const baseUrl = functionUrl!.replace(/\/+$/, "");
71
72 // Managed secret: host/port from the instance, credentials from the
73 // RDS-managed master user secret.
74 const info = (yield* send(HttpClientRequest.get(`${baseUrl}/connect-info`))) as {
75 host: string;
76 port: number;
77 database?: string;
78 username?: string;
79 hasPassword: boolean;
80 ssl: boolean;
81 };
82 expect(info).toEqual({
83 host: endpointAddress,
84 port: 5432,
85 database: "postgres",
86 username: "alchemy",
87 hasPassword: true,
88 ssl: true,
89 });
90
91 // Socket round-trip as the master user; bootstraps the IAM user.
92 const master = (yield* send(HttpClientRequest.post(`${baseUrl}/setup-iam-user`))) as {
93 rows: { user: string }[];
94 };
95 expect(master.rows).toEqual([{ user: "alchemy" }]);
96
97 // Socket round-trip authenticated with a presigned IAM token, which
98 // only works when `rds-db:connect` targets the instance's DbiResourceId.
99 const iam = (yield* send(HttpClientRequest.get(`${baseUrl}/iam-user`))) as {
100 rows: { user: string }[];
101 };
102 expect(iam.rows).toEqual([{ user: "app_iam" }]);
103
104 yield* stack.destroy();
105
106 const gone = yield* rds
107 .describeDBInstances({ DBInstanceIdentifier: dbInstanceIdentifier })
108 .pipe(
109 Effect.as(false),
110 Effect.catchTag("DBInstanceNotFoundFault", () => Effect.succeed(true)),
111 Effect.repeat({
112 schedule: Schedule.spaced("5 seconds"),
113 times: 8,
114 until: (absent) => absent,
115 }),
116 );
117 expect(gone).toBe(true);
118 }),
119 {
120 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:lambda", "provider:aws:rds", "live"],
121 timeout: 2_700_000,
122 },
123 );