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

main
ts 162 lines 6.24 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { TrustStore } from "@alchemy.run/aws/ELBv2";
3 import { Bucket } from "@alchemy.run/aws/S3";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as elbv2 from "@distilled.cloud/aws/elastic-load-balancing-v2";
7 import * as s3 from "@distilled.cloud/aws/s3";
8 import * as Effect from "effect/Effect";
9 import { MinimumLogLevel } from "effect/References";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // A self-signed CA certificate generated once and checked in (never created at
16 // test time, per the fixture convention). X.509 v3 with basicConstraints
17 // CA:TRUE + keyCertSign — ELBv2 trust stores reject v1 certs
18 // ("The certificate version is not supported").
19 const CA_BUNDLE_PEM = `-----BEGIN CERTIFICATE-----
20 MIIC2jCCAcKgAwIBAgIJAJyM/Dvd55qtMA0GCSqGSIb3DQEBCwUAMBoxGDAWBgNV
21 BAMMD2FsY2hlbXktdGVzdC1jYTAeFw0yNjA2MTcwNjA5MDlaFw0zNjA2MTQwNjA5
22 MDlaMBoxGDAWBgNVBAMMD2FsY2hlbXktdGVzdC1jYTCCASIwDQYJKoZIhvcNAQEB
23 BQADggEPADCCAQoCggEBANgo7XPCQMpyecXg2SCj6Tn6R1snlmhSA1vKGQnHoQBS
24 QA11DMpv+iFRT9s1d3izaGA4GEcxfrXOsmUBkzYIHJIYakCWdr6qcUXs6lS2uhnZ
25 qcyR0CamDtHTqAxRKEK+QPaISoxyD3BIwQqE0I8yNzV3/6osIE513e+7tp9E+J04
26 dBhyG5goSwR3ueqs53gQioYVp/fgLKo4MqFcsA3p7anEE9hyeq1Q/lGAXxQwZmXT
27 3kQli/JjMoF8OfccpA3aBx9Y2aDTCU8HXTscVYmPSHbnTGkTARGBwnag+Jwq5Uni
28 YvM2OeDUPwvszgpi3JgiblZQhZQAy4/MeNhmE8qgIa8CAwEAAaMjMCEwDwYDVR0T
29 AQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMCAQYwDQYJKoZIhvcNAQELBQADggEBAJnp
30 el0xBbL/eQY87evhy0o+ZTHMVCdI9Uc+kDK0XPMi4hc5OfjWNIy8u5/s33kPkNYS
31 Y5Jhm5KtGtMb9kXioCWjSi0aREA8zijGrXn1jC+0rksMQmJka63bKsJ4TjFaHMcc
32 m/xt25xX1Ssp/gWr9YX3MzbPhcn57Uu9OTtzf13F6CMv1XtRS1RKFYtkLZrhvzBR
33 WPdos3xvn3D0Fjd5H5AgVKTkeb2YPhINfN4jyzn3J09teKZpNN/qHTAQewIh2FnO
34 MeplcuT3eQVUZNTBelvUE7VKHe11AUc8TkvVMS/XOFeN6OHAJtq08EegbTcjwz9Z
35 lyGGetkNMmdhGRV6AlY=
36 -----END CERTIFICATE-----
37 `;
38
39 // Idempotent out-of-band delete for the probe trust store below. The probe
40 // create is EXPECTED to fail, but if it ever unexpectedly succeeded the store
41 // would live outside the stack — so cleanup must be guaranteed. Not-found is
42 // success; any other error is a defect, so the error channel is `never` and
43 // this is a valid `Effect.ensuring` finalizer.
44 const deleteTrustStoreByNameIdempotent = (name: string) =>
45 elbv2.describeTrustStores({ Names: [name] }).pipe(
46 Effect.flatMap((r) =>
47 Effect.forEach(
48 (r.TrustStores ?? []).flatMap((t) => (t.TrustStoreArn ? [t.TrustStoreArn] : [])),
49 (arn) => elbv2.deleteTrustStore({ TrustStoreArn: arn }),
50 ),
51 ),
52 Effect.catchTag("TrustStoreNotFoundException", () => Effect.void),
53 Effect.orDie,
54 Effect.asVoid,
55 );
56
57 // Fast unconditional probe: createTrustStore against a non-existent bundle
58 // must surface a typed error (not an untyped catch-all). Proves both the
59 // resource wiring and the distilled typed-error path.
60 test.provider(
61 "trust store create with missing bundle returns a typed error",
62 (stack) =>
63 Effect.gen(function* () {
64 yield* stack.destroy();
65
66 const probeName = `alchemy-mtls-probe-${stack.name.replace(/[^a-zA-Z0-9]/g, "")}`.slice(
67 0,
68 32,
69 );
70
71 // Pre-clean: reclaim a probe trust store leaked by a prior crashed run
72 // (a leftover would turn the expected error into DuplicateTrustStoreName).
73 yield* deleteTrustStoreByNameIdempotent(probeName);
74
75 yield* Effect.gen(function* () {
76 const result = yield* elbv2
77 .createTrustStore({
78 Name: probeName,
79 CaCertificatesBundleS3Bucket: "alchemy-no-such-bucket-elbv2-probe",
80 CaCertificatesBundleS3Key: "missing.pem",
81 })
82 .pipe(Effect.flip);
83
84 // AWS rejects a missing/inaccessible bundle with one of these typed tags.
85 expect(
86 [
87 "CaCertificatesBundleNotFoundException",
88 "InvalidCaCertificatesBundleException",
89 ].includes(result._tag),
90 ).toBe(true);
91 }).pipe(
92 // Guaranteed cleanup if the create ever unexpectedly succeeds.
93 Effect.ensuring(deleteTrustStoreByNameIdempotent(probeName)),
94 );
95
96 yield* stack.destroy();
97 }).pipe(logLevel),
98 { tags: ["provider:aws", "provider:aws:elbv2", "live"], timeout: 120_000 },
99 );
100
101 // Full mTLS-verify lifecycle: upload a CA bundle to a stack-owned bucket,
102 // create an ACTIVE trust store, then destroy. Runs ungated — verified live on
103 // the standard testing account (completes in a few seconds).
104 test.provider(
105 "trust store full lifecycle from an uploaded CA bundle",
106 (stack) =>
107 Effect.gen(function* () {
108 yield* stack.destroy();
109
110 const deployed = yield* stack.deploy(
111 Effect.gen(function* () {
112 const bucket = yield* Bucket("TsBucket", { forceDestroy: true });
113 return { bucketName: bucket.bucketName };
114 }),
115 );
116
117 const key = "ca-bundle.pem";
118 yield* s3.putObject({
119 Bucket: deployed.bucketName,
120 Key: key,
121 Body: CA_BUNDLE_PEM,
122 ContentType: "application/x-pem-file",
123 });
124
125 const ts = yield* stack.deploy(
126 Effect.gen(function* () {
127 const bucket = yield* Bucket("TsBucket", { forceDestroy: true });
128 const trustStore = yield* TrustStore("TsStore", {
129 caCertificatesBundleS3Bucket: bucket.bucketName,
130 caCertificatesBundleS3Key: key,
131 });
132 return { trustStore };
133 }),
134 );
135
136 expect(ts.trustStore.status).toBe("ACTIVE");
137 expect(ts.trustStore.numberOfCaCertificates).toBeGreaterThanOrEqual(1);
138
139 const observed = yield* elbv2
140 .describeTrustStores({
141 TrustStoreArns: [ts.trustStore.trustStoreArn],
142 })
143 .pipe(Effect.map((r) => r.TrustStores?.[0]));
144 expect(observed?.Status).toBe("ACTIVE");
145
146 yield* stack.destroy();
147
148 const after = yield* elbv2
149 .describeTrustStores({
150 TrustStoreArns: [ts.trustStore.trustStoreArn],
151 })
152 .pipe(
153 Effect.map((r) => r.TrustStores?.length ?? 0),
154 Effect.catchTag("TrustStoreNotFoundException", () => Effect.succeed(0)),
155 );
156 expect(after).toBe(0);
157 }).pipe(logLevel),
158 {
159 tags: ["provider:aws", "provider:aws:elbv2", "provider:aws:s3", "live"],
160 timeout: 600_000,
161 },
162 );