@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 178 lines 6.72 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Core from "@alchemy.run/test/Core";
5 import * as acmpca from "@distilled.cloud/aws/acm-pca";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Schedule from "effect/Schedule";
10 import ACMPCATestFunctionLive, { ACMPCATestFunction } from "./fixtures/handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "ACMPCABindings");
15
16 // Lambda function URL cold-start (DNS, IAM propagation, init) can take
17 // well over 60s on a fresh deploy under parallel-suite load.
18 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
19
20 let baseUrl: string;
21 let caArn: string;
22 let caCsr: string;
23 let caCertificateArn: string;
24
25 const send = (request: HttpClientRequest.HttpClientRequest) =>
26 HttpClient.execute(request).pipe(
27 Effect.flatMap((response) =>
28 response.status === 200
29 ? response.json
30 : response.text.pipe(
31 Effect.flatMap((body) =>
32 Effect.fail(new Error(`Fixture answered ${response.status}: ${body}`)),
33 ),
34 ),
35 ),
36 );
37
38 // A private CA bills a monthly fee (prorated) for as long as it exists, and
39 // this suite ACTIVATES its CA (self-signed root, short-lived certificate
40 // mode — the cheaper tier), so the whole suite is gated behind
41 // AWS_TEST_ACMPCA=1.
42 //
43 // vitest runs the tests of a file CONCURRENTLY (sequence.concurrent), so all
44 // state-ordered steps live in `beforeAll`: the CSR is captured while the CA
45 // is still PENDING_CERTIFICATE (GetCertificateAuthorityCsr only answers in
46 // that state), then the CA is activated. Every test below is
47 // order-independent against the ACTIVE CA; the issue→revoke chain runs
48 // inside a single test.
49 describe.skipIf(!process.env.AWS_TEST_ACMPCA)(
50 "ACMPCA Bindings",
51 {
52 tags: ["provider:aws", "provider:aws:acmpca", "provider:aws:lambda", "provider:aws:s3", "live"],
53 },
54 () => {
55 beforeAll(
56 Effect.gen(function* () {
57 yield* sharedStack.destroy();
58 const { functionUrl } = yield* sharedStack.deploy(
59 Effect.gen(function* () {
60 return yield* ACMPCATestFunction;
61 }).pipe(Effect.provide(ACMPCATestFunctionLive)),
62 );
63
64 expect(functionUrl).toBeTruthy();
65 baseUrl = functionUrl!.replace(/\/+$/, "");
66
67 // /csr works while the CA is still PENDING_CERTIFICATE, so it doubles
68 // as the readiness probe. It also reports the CA's ARN for the
69 // out-of-band assertions below.
70 const ready = yield* HttpClient.get(`${baseUrl}/csr`).pipe(
71 Effect.flatMap((response) =>
72 response.status === 200
73 ? response.json
74 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
75 ),
76 Effect.retry({ schedule: readinessPolicy }),
77 );
78 caArn = (ready as { caArn: string }).caArn;
79 caCsr = (ready as { csr: string }).csr;
80 expect(caArn).toContain(":certificate-authority/");
81
82 // Activate the CA: self-sign its CSR with the root template and
83 // install the result (GetCertificateAuthorityCsr + IssueCertificate +
84 // GetCertificate + ImportCertificateAuthorityCertificate).
85 const activated = (yield* send(HttpClientRequest.post(`${baseUrl}/activate`)).pipe(
86 Effect.retry({ schedule: Schedule.spaced("3 seconds"), times: 3 }),
87 )) as { certificateArn: string };
88 caCertificateArn = activated.certificateArn;
89 }),
90 { timeout: 240_000 },
91 );
92
93 afterAll(sharedStack.destroy(), { timeout: 120_000 });
94
95 describe("GetCertificateAuthorityCsr", () => {
96 test.provider(
97 "returned the CA's PEM CSR while it was pending",
98 () =>
99 Effect.gen(function* () {
100 expect(caCsr).toContain("BEGIN CERTIFICATE REQUEST");
101 }),
102 { timeout: 60_000 },
103 );
104 });
105
106 describe("IssueCertificate + GetCertificate + ImportCertificateAuthorityCertificate", () => {
107 test.provider(
108 "activated the CA by self-signing its CSR with the root template",
109 () =>
110 Effect.gen(function* () {
111 expect(caCertificateArn).toContain(caArn);
112
113 // Out-of-band verification via distilled: the CA is now ACTIVE.
114 const described = yield* acmpca.describeCertificateAuthority({
115 CertificateAuthorityArn: caArn,
116 });
117 expect(described.CertificateAuthority?.Status).toBe("ACTIVE");
118 }),
119 { timeout: 120_000 },
120 );
121 });
122
123 describe("GetCertificateAuthorityCertificate", () => {
124 test.provider(
125 "returns the installed CA certificate",
126 () =>
127 Effect.gen(function* () {
128 const body = (yield* send(HttpClientRequest.get(`${baseUrl}/ca-certificate`))) as {
129 certificate: string;
130 };
131 expect(body.certificate).toContain("BEGIN CERTIFICATE");
132 }),
133 { timeout: 60_000 },
134 );
135 });
136
137 describe("IssueCertificate + RevokeCertificate", () => {
138 test.provider(
139 "issues a short-lived end-entity certificate, then revokes it by serial",
140 () =>
141 Effect.gen(function* () {
142 const issued = (yield* send(HttpClientRequest.post(`${baseUrl}/issue`))) as {
143 certificateArn: string;
144 certificate: string;
145 };
146 expect(issued.certificateArn).toContain("/certificate/");
147 expect(issued.certificate).toContain("BEGIN CERTIFICATE");
148
149 const revoked = (yield* send(
150 HttpClientRequest.bodyJsonUnsafe(HttpClientRequest.post(`${baseUrl}/revoke`), {
151 certificateArn: issued.certificateArn,
152 }),
153 )) as { revoked: boolean; serial: string };
154 expect(revoked.revoked).toBe(true);
155 expect(revoked.serial).toBeTruthy();
156 }),
157 { timeout: 120_000 },
158 );
159 });
160
161 describe("CreateCertificateAuthorityAuditReport + DescribeCertificateAuthorityAuditReport", () => {
162 test.provider(
163 "generates an audit report into S3 and polls it",
164 () =>
165 Effect.gen(function* () {
166 const body = (yield* send(HttpClientRequest.post(`${baseUrl}/audit`))) as {
167 auditReportId: string;
168 s3Key: string;
169 status: string;
170 };
171 expect(body.auditReportId).toBeTruthy();
172 expect(body.status).toBe("SUCCESS");
173 }),
174 { timeout: 120_000 },
175 );
176 });
177 },
178 );