@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 210 lines 7.34 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as certificatemanager from "@distilled.cloud/gcp/certificatemanager_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 const runLifecycle = !process.env.FAST;
15
16 const location = "us-central1";
17
18 const waitUntilGone = (name: string) =>
19 certificatemanager.getProjectsLocationsCertificateIssuanceConfigs({ name }).pipe(
20 Effect.as("found" as const),
21 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
22 Effect.repeat({
23 schedule: Schedule.spaced("1 second"),
24 until: (status) => status === "gone",
25 times: 10,
26 }),
27 );
28
29 test.provider(
30 "getProjectsLocationsCertificateIssuanceConfigs on a missing config fails with NotFound",
31 (stack) =>
32 Effect.gen(function* () {
33 const { project } = yield* GcpEnvironment.current;
34 yield* stack.destroy();
35
36 const error = yield* Effect.flip(
37 certificatemanager.getProjectsLocationsCertificateIssuanceConfigs({
38 name: `projects/${project}/locations/${location}/certificateIssuanceConfigs/alchemy-issuance-missing`,
39 }),
40 );
41 expect(error._tag).toBe("NotFound");
42
43 yield* stack.destroy();
44 }).pipe(logLevel),
45 {
46 tags: ["provider:gcp", "provider:gcp:certificatemanager", "live"],
47 timeout: 90_000,
48 },
49 );
50
51 test.provider.skipIf(!runLifecycle)(
52 "create, update, and delete a certificate issuance config",
53 (stack) =>
54 Effect.gen(function* () {
55 yield* stack.destroy();
56
57 const created = yield* stack.deploy(
58 Effect.gen(function* () {
59 const pool = yield* GCP.PrivateCA.CaPool("IssuancePool", {
60 location,
61 tier: "DEVOPS",
62 labels: { env: "test" },
63 });
64 const ca = yield* GCP.PrivateCA.CertificateAuthority("IssuanceCa", {
65 caPool: pool.name,
66 location,
67 type: "SELF_SIGNED",
68 desiredState: "ENABLED",
69 lifetime: "315360000s",
70 keySpec: { algorithm: "EC_P256_SHA256" },
71 config: {
72 subjectConfig: {
73 subject: {
74 organization: "Alchemy",
75 commonName: "Alchemy Issuance Test Root",
76 },
77 },
78 x509Config: {
79 caOptions: { isCa: true },
80 keyUsage: {
81 baseKeyUsage: {
82 certSign: true,
83 crlSign: true,
84 },
85 },
86 },
87 },
88 });
89 return yield* GCP.CertificateManager.CertificateIssuanceConfig("WorkloadTls", {
90 location,
91 description: "issuance a",
92 labels: { env: "test", ca: ca.certificateAuthorityId },
93 lifetime: "2592000s",
94 rotationWindowPercentage: 66,
95 keyAlgorithm: "ECDSA_P256",
96 certificateAuthorityConfig: {
97 certificateAuthorityServiceConfig: {
98 caPool: pool.name,
99 },
100 },
101 });
102 }),
103 );
104
105 expect(created.name).toContain("/certificateIssuanceConfigs/");
106 expect(created.certificateIssuanceConfigId).toEqual(expect.any(String));
107 expect(created.location).toEqual(location);
108 expect(created.description).toEqual("issuance a");
109 expect(created.labels).toMatchObject({ env: "test" });
110 expect(created.lifetime).toEqual("2592000s");
111 expect(created.rotationWindowPercentage).toEqual(66);
112 expect(created.keyAlgorithm).toEqual("ECDSA_P256");
113 expect(created.caPool).toContain("/caPools/");
114
115 const fetched = yield* certificatemanager.getProjectsLocationsCertificateIssuanceConfigs({
116 name: created.name,
117 });
118 expect(fetched.name).toEqual(created.name);
119 expect(fetched.description).toEqual("issuance a");
120 expect(fetched.labels?.env).toEqual("test");
121 expect(fetched.lifetime).toEqual("2592000s");
122 expect(fetched.rotationWindowPercentage).toEqual(66);
123 expect(fetched.keyAlgorithm).toEqual("ECDSA_P256");
124 expect(fetched.certificateAuthorityConfig?.certificateAuthorityServiceConfig?.caPool).toEqual(
125 created.caPool,
126 );
127 expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual(
128 true,
129 );
130
131 const updated = yield* stack.deploy(
132 Effect.gen(function* () {
133 const pool = yield* GCP.PrivateCA.CaPool("IssuancePool", {
134 location,
135 caPoolId: created.caPool.split("/").pop(),
136 tier: "DEVOPS",
137 labels: { env: "test" },
138 });
139 const ca = yield* GCP.PrivateCA.CertificateAuthority("IssuanceCa", {
140 caPool: pool.name,
141 location,
142 type: "SELF_SIGNED",
143 desiredState: "ENABLED",
144 lifetime: "315360000s",
145 keySpec: { algorithm: "EC_P256_SHA256" },
146 config: {
147 subjectConfig: {
148 subject: {
149 organization: "Alchemy",
150 commonName: "Alchemy Issuance Test Root",
151 },
152 },
153 x509Config: {
154 caOptions: { isCa: true },
155 keyUsage: {
156 baseKeyUsage: {
157 certSign: true,
158 crlSign: true,
159 },
160 },
161 },
162 },
163 });
164 return yield* GCP.CertificateManager.CertificateIssuanceConfig("WorkloadTls", {
165 certificateIssuanceConfigId: created.certificateIssuanceConfigId,
166 location,
167 description: "issuance b",
168 labels: {
169 env: "prod",
170 role: "tls",
171 ca: ca.certificateAuthorityId,
172 },
173 lifetime: "2592000s",
174 rotationWindowPercentage: 66,
175 keyAlgorithm: "ECDSA_P256",
176 certificateAuthorityConfig: {
177 certificateAuthorityServiceConfig: {
178 caPool: pool.name,
179 },
180 },
181 });
182 }),
183 );
184
185 expect(updated.name).toEqual(created.name);
186 expect(updated.description).toEqual("issuance b");
187 expect(updated.labels).toMatchObject({ env: "prod", role: "tls" });
188 expect(updated.lifetime).toEqual("2592000s");
189 expect(updated.keyAlgorithm).toEqual("ECDSA_P256");
190 expect(updated.caPool).toEqual(created.caPool);
191
192 const refetched = yield* certificatemanager.getProjectsLocationsCertificateIssuanceConfigs({
193 name: created.name,
194 });
195 expect(refetched.description).toEqual("issuance b");
196 expect(refetched.labels?.env).toEqual("prod");
197 expect(refetched.labels?.role).toEqual("tls");
198 expect(refetched.lifetime).toEqual("2592000s");
199 expect(refetched.keyAlgorithm).toEqual("ECDSA_P256");
200
201 yield* stack.destroy();
202
203 const gone = yield* waitUntilGone(created.name);
204 expect(gone).toEqual("gone");
205 }).pipe(logLevel),
206 {
207 tags: ["provider:gcp", "provider:gcp:certificatemanager", "live"],
208 timeout: 120_000,
209 },
210 );