@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 122 lines 4.29 KB
Raw
1 import * as secretsmanager from "@distilled.cloud/aws/secrets-manager";
2 import * as Output from "alchemy/Output";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as Redacted from "effect/Redacted";
6 import * as AWSBindings from "../AWSBindings.ts";
7 import {
8 Connect,
9 connectEnvPrefix,
10 type ConnectOptions,
11 formatMongoConnectionUrl,
12 } from "./Connect.ts";
13 import type { DBCluster } from "./DBCluster.ts";
14
15 export const ConnectHttp = Layer.effect(
16 Connect,
17 Effect.gen(function* () {
18 const bind = yield* AWSBindings.Bind;
19 const getSecretValue = yield* AWSBindings.withCredentials(secretsmanager.getSecretValue);
20
21 return Effect.fn(function* (cluster: DBCluster, options?: ConnectOptions) {
22 const Host = yield* cluster.endpoint;
23 const Port = yield* cluster.port;
24 // Default credential source: the cluster's managed master user secret
25 // (`manageMasterUserPassword: true`). An explicit Secrets Manager
26 // secret overrides it.
27 const SecretArn = options?.secret
28 ? yield* options.secret.secretArn
29 : yield* cluster.masterUserSecretArn;
30
31 if (!globalThis.__ALCHEMY_RUNTIME__) {
32 // DocumentDB is VPC-only: attach the host to the cluster's VPC.
33 const vpc =
34 options?.subnetIds !== undefined || options?.securityGroupIds !== undefined
35 ? {
36 subnetIds: options?.subnetIds ?? [],
37 securityGroupIds: options?.securityGroupIds ?? [],
38 }
39 : undefined;
40 const secretResource = options?.secret
41 ? options.secret.secretArn
42 : Output.map(cluster.masterUserSecretArn, (arn) => {
43 if (arn === undefined) {
44 throw new Error(
45 `AWS.DocDB.Connect(${cluster.LogicalId}): the cluster has no managed master user secret — ` +
46 `set manageMasterUserPassword: true on the cluster or pass options.secret`,
47 );
48 }
49 return arn;
50 });
51 const prefix = connectEnvPrefix(cluster.LogicalId);
52 yield* bind`AWS.DocDB.Connect(${cluster})`({
53 env: {
54 [`${prefix}_HOST`]: cluster.endpoint,
55 // Lambda environment variables are strings — stringify the port.
56 [`${prefix}_PORT`]: Output.interpolate`${cluster.port}`,
57 },
58 policyStatements: [
59 {
60 Effect: "Allow",
61 Action: ["secretsmanager:GetSecretValue", "secretsmanager:DescribeSecret"],
62 Resource: [secretResource],
63 },
64 ],
65 });
66 if (vpc !== undefined) {
67 yield* AWSBindings.bindVpc`AWS.DocDB.Connect(${cluster})`(vpc);
68 }
69 }
70
71 return Effect.gen(function* () {
72 const host = yield* Host;
73 const port = yield* Port;
74 const secretArn = yield* SecretArn;
75
76 if (!host) {
77 return yield* Effect.die(
78 `DocumentDB endpoint for '${cluster.LogicalId}' is not available yet`,
79 );
80 }
81 if (!secretArn) {
82 return yield* Effect.die(
83 `AWS.DocDB.Connect(${cluster.LogicalId}): the cluster has no managed master user secret — ` +
84 `set manageMasterUserPassword: true on the cluster or pass options.secret`,
85 );
86 }
87
88 const value = yield* getSecretValue({ SecretId: secretArn });
89 const secretString = value.SecretString
90 ? typeof value.SecretString === "string"
91 ? value.SecretString
92 : Redacted.value(value.SecretString)
93 : "{}";
94 const secret = JSON.parse(secretString) as {
95 username?: string;
96 password?: string;
97 };
98
99 const resolvedPort = options?.port ?? port ?? 27017;
100 const tls = options?.tls ?? true;
101 const password = secret.password !== undefined ? Redacted.make(secret.password) : undefined;
102
103 return {
104 host,
105 port: resolvedPort,
106 database: options?.database,
107 username: secret.username,
108 password,
109 tls,
110 url: formatMongoConnectionUrl({
111 host,
112 port: resolvedPort,
113 database: options?.database,
114 username: secret.username,
115 password,
116 tls,
117 }),
118 };
119 });
120 });
121 }),
122 );