The best way to control Claude Code, Codex and more from any computer or phone
| 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 privateca from "@distilled.cloud/gcp/privateca_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 waitUntilGone = (name: string) => |
| 15 | privateca.getProjectsLocationsCertificateTemplates({ name }).pipe( |
| 16 | Effect.as("found" as const), |
| 17 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 18 | Effect.repeat({ |
| 19 | schedule: Schedule.spaced("1 second"), |
| 20 | until: (status) => status === "gone", |
| 21 | times: 10, |
| 22 | }), |
| 23 | ); |
| 24 | |
| 25 | test.provider( |
| 26 | "getProjectsLocationsCertificateTemplates on a missing template fails with NotFound", |
| 27 | (stack) => |
| 28 | Effect.gen(function* () { |
| 29 | const { project } = yield* GcpEnvironment.current; |
| 30 | |
| 31 | yield* stack.destroy(); |
| 32 | |
| 33 | const error = yield* Effect.flip( |
| 34 | privateca.getProjectsLocationsCertificateTemplates({ |
| 35 | name: `projects/${project}/locations/us-central1/certificateTemplates/alchemy-template-missing`, |
| 36 | }), |
| 37 | ); |
| 38 | expect(error._tag).toBe("NotFound"); |
| 39 | |
| 40 | const page = yield* privateca.listProjectsLocationsCertificateTemplates({ |
| 41 | parent: `projects/${project}/locations/-`, |
| 42 | pageSize: 10, |
| 43 | }); |
| 44 | expect((page.certificateTemplates ?? []).map((template) => template.name)).not.toContain( |
| 45 | `projects/${project}/locations/us-central1/certificateTemplates/alchemy-template-missing`, |
| 46 | ); |
| 47 | |
| 48 | yield* stack.destroy(); |
| 49 | }).pipe(logLevel), |
| 50 | { tags: ["provider:gcp", "provider:gcp:privateca", "live"], timeout: 90_000 }, |
| 51 | ); |
| 52 | |
| 53 | test.provider( |
| 54 | "create, update, replace, and delete a certificate template", |
| 55 | (stack) => |
| 56 | Effect.gen(function* () { |
| 57 | yield* stack.destroy(); |
| 58 | |
| 59 | const created = yield* stack.deploy( |
| 60 | Effect.gen(function* () { |
| 61 | return yield* GCP.PrivateCA.CertificateTemplate("LeafTls", { |
| 62 | location: "us-central1", |
| 63 | description: "leaf tls a", |
| 64 | maximumLifetime: "86400s", |
| 65 | labels: { env: "test" }, |
| 66 | identityConstraints: { |
| 67 | allowSubjectPassthrough: true, |
| 68 | allowSubjectAltNamesPassthrough: true, |
| 69 | }, |
| 70 | passthroughExtensions: { |
| 71 | knownExtensions: ["EXTENDED_KEY_USAGE"], |
| 72 | }, |
| 73 | predefinedValues: { |
| 74 | caOptions: { isCa: false }, |
| 75 | keyUsage: { |
| 76 | baseKeyUsage: { |
| 77 | digitalSignature: true, |
| 78 | keyEncipherment: true, |
| 79 | }, |
| 80 | extendedKeyUsage: { serverAuth: true }, |
| 81 | }, |
| 82 | }, |
| 83 | }); |
| 84 | }), |
| 85 | ); |
| 86 | |
| 87 | expect(created.name).toContain("/certificateTemplates/"); |
| 88 | expect(created.certificateTemplateId).toEqual(expect.any(String)); |
| 89 | expect(created.location).toEqual("us-central1"); |
| 90 | expect(created.description).toEqual("leaf tls a"); |
| 91 | expect(created.maximumLifetime).toEqual("86400s"); |
| 92 | expect(created.labels).toMatchObject({ env: "test" }); |
| 93 | expect(created.identityConstraints?.allowSubjectPassthrough).toEqual(true); |
| 94 | expect(created.identityConstraints?.allowSubjectAltNamesPassthrough).toEqual(true); |
| 95 | expect(created.passthroughExtensions?.knownExtensions).toContain("EXTENDED_KEY_USAGE"); |
| 96 | expect(created.predefinedValues?.caOptions?.isCa).toEqual(false); |
| 97 | expect(created.createTime).toEqual(expect.any(String)); |
| 98 | |
| 99 | const fetched = yield* privateca.getProjectsLocationsCertificateTemplates({ |
| 100 | name: created.name, |
| 101 | }); |
| 102 | expect(fetched.name).toEqual(created.name); |
| 103 | expect(fetched.labels?.env).toEqual("test"); |
| 104 | expect(fetched.description).toEqual("leaf tls a"); |
| 105 | expect(fetched.maximumLifetime).toEqual("86400s"); |
| 106 | expect(Object.keys(fetched.labels ?? {}).some((key) => key.startsWith("alchemy-"))).toEqual( |
| 107 | true, |
| 108 | ); |
| 109 | |
| 110 | const updated = yield* stack.deploy( |
| 111 | Effect.gen(function* () { |
| 112 | return yield* GCP.PrivateCA.CertificateTemplate("LeafTls", { |
| 113 | certificateTemplateId: created.certificateTemplateId, |
| 114 | location: "us-central1", |
| 115 | description: "leaf tls b", |
| 116 | maximumLifetime: "172800s", |
| 117 | labels: { env: "prod", role: "tls" }, |
| 118 | identityConstraints: { |
| 119 | allowSubjectPassthrough: true, |
| 120 | allowSubjectAltNamesPassthrough: false, |
| 121 | }, |
| 122 | passthroughExtensions: { |
| 123 | knownExtensions: ["BASE_KEY_USAGE", "EXTENDED_KEY_USAGE"], |
| 124 | }, |
| 125 | predefinedValues: { |
| 126 | caOptions: { isCa: false }, |
| 127 | keyUsage: { |
| 128 | baseKeyUsage: { |
| 129 | digitalSignature: true, |
| 130 | keyEncipherment: true, |
| 131 | }, |
| 132 | extendedKeyUsage: { serverAuth: true, clientAuth: true }, |
| 133 | }, |
| 134 | }, |
| 135 | }); |
| 136 | }), |
| 137 | ); |
| 138 | |
| 139 | expect(updated.name).toEqual(created.name); |
| 140 | expect(updated.description).toEqual("leaf tls b"); |
| 141 | expect(updated.maximumLifetime).toEqual("172800s"); |
| 142 | expect(updated.labels).toMatchObject({ env: "prod", role: "tls" }); |
| 143 | expect(updated.identityConstraints?.allowSubjectAltNamesPassthrough).toEqual(false); |
| 144 | expect(updated.passthroughExtensions?.knownExtensions).toEqual( |
| 145 | expect.arrayContaining(["BASE_KEY_USAGE", "EXTENDED_KEY_USAGE"]), |
| 146 | ); |
| 147 | expect(updated.predefinedValues?.keyUsage?.extendedKeyUsage?.clientAuth).toEqual(true); |
| 148 | |
| 149 | const refetched = yield* privateca.getProjectsLocationsCertificateTemplates({ |
| 150 | name: created.name, |
| 151 | }); |
| 152 | expect(refetched.description).toEqual("leaf tls b"); |
| 153 | expect(refetched.labels?.env).toEqual("prod"); |
| 154 | expect(refetched.labels?.role).toEqual("tls"); |
| 155 | expect(refetched.maximumLifetime).toEqual("172800s"); |
| 156 | |
| 157 | const replaced = yield* stack.deploy( |
| 158 | Effect.gen(function* () { |
| 159 | return yield* GCP.PrivateCA.CertificateTemplate("LeafTls", { |
| 160 | certificateTemplateId: created.certificateTemplateId, |
| 161 | location: "us-east1", |
| 162 | description: "leaf tls east", |
| 163 | maximumLifetime: "86400s", |
| 164 | labels: { env: "prod" }, |
| 165 | identityConstraints: { |
| 166 | allowSubjectPassthrough: true, |
| 167 | allowSubjectAltNamesPassthrough: true, |
| 168 | }, |
| 169 | }); |
| 170 | }), |
| 171 | ); |
| 172 | |
| 173 | expect(replaced.location).toEqual("us-east1"); |
| 174 | expect(replaced.certificateTemplateId).toEqual(created.certificateTemplateId); |
| 175 | expect(replaced.name).not.toEqual(created.name); |
| 176 | expect(replaced.name).toContain("/locations/us-east1/"); |
| 177 | |
| 178 | const fetchedReplacement = yield* privateca.getProjectsLocationsCertificateTemplates({ |
| 179 | name: replaced.name, |
| 180 | }); |
| 181 | expect(fetchedReplacement.name).toEqual(replaced.name); |
| 182 | expect(fetchedReplacement.labels?.env).toEqual("prod"); |
| 183 | |
| 184 | const previousGone = yield* waitUntilGone(created.name); |
| 185 | expect(previousGone).toEqual("gone"); |
| 186 | |
| 187 | yield* stack.destroy(); |
| 188 | |
| 189 | const gone = yield* waitUntilGone(replaced.name); |
| 190 | expect(gone).toEqual("gone"); |
| 191 | }).pipe(logLevel), |
| 192 | { |
| 193 | tags: ["provider:gcp", "provider:gcp:privateca", "live"], |
| 194 | timeout: 120_000, |
| 195 | }, |
| 196 | ); |