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 kms from "@distilled.cloud/gcp/cloudkms_v1"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { KEY_RING_ID, kmsTestId } from "./common.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | // Cloud KMS KeyRings cannot be deleted. Reuse the standing test ring. |
| 16 | // Versions enter DESTROY_SCHEDULED for ≥24h, so reuse a standing key. |
| 17 | const CRYPTO_KEY_ID = kmsTestId("version"); |
| 18 | |
| 19 | const waitUntilGone = (name: string) => |
| 20 | kms.getProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersions({ name }).pipe( |
| 21 | Effect.as("found" as const), |
| 22 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 23 | Effect.repeat({ |
| 24 | schedule: Schedule.spaced("1 second"), |
| 25 | until: (status) => status === "gone", |
| 26 | times: 10, |
| 27 | }), |
| 28 | ); |
| 29 | |
| 30 | const waitVersionReady = (name: string) => |
| 31 | kms.getProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersions({ name }).pipe( |
| 32 | Effect.repeat({ |
| 33 | schedule: Schedule.spaced("500 millis"), |
| 34 | until: (version) => version.state === "ENABLED" || version.state === "DISABLED", |
| 35 | times: 10, |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | test.provider( |
| 40 | "getProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersions on a missing version fails with NotFound", |
| 41 | () => |
| 42 | Effect.gen(function* () { |
| 43 | const { project } = yield* GcpEnvironment.current; |
| 44 | const error = yield* Effect.flip( |
| 45 | kms.getProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersions({ |
| 46 | name: `projects/${project}/locations/us-central1/keyRings/${KEY_RING_ID}/cryptoKeys/${CRYPTO_KEY_ID}/cryptoKeyVersions/999999`, |
| 47 | }), |
| 48 | ); |
| 49 | expect(error._tag).toBe("NotFound"); |
| 50 | }).pipe(logLevel), |
| 51 | { tags: ["provider:gcp", "provider:gcp:kms", "live"] }, |
| 52 | ); |
| 53 | |
| 54 | test.provider( |
| 55 | "create, update, and destroy a crypto key version", |
| 56 | (stack) => |
| 57 | Effect.gen(function* () { |
| 58 | const { project } = yield* GcpEnvironment.current; |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const created = yield* stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | const ring = yield* GCP.KMS.KeyRing("Keys", { |
| 64 | keyRingId: KEY_RING_ID, |
| 65 | location: "us-central1", |
| 66 | }); |
| 67 | const key = yield* GCP.KMS.CryptoKey("Data", { |
| 68 | keyRing: ring.name, |
| 69 | cryptoKeyId: CRYPTO_KEY_ID, |
| 70 | skipInitialVersionCreation: true, |
| 71 | labels: { env: "test" }, |
| 72 | }); |
| 73 | return yield* GCP.KMS.CryptoKeyVersion("V1", { |
| 74 | cryptoKey: key.name, |
| 75 | }); |
| 76 | }), |
| 77 | ); |
| 78 | |
| 79 | expect(created.cryptoKeyVersionId).toEqual(expect.any(String)); |
| 80 | expect(created.location).toEqual("us-central1"); |
| 81 | expect(created.project).toEqual(project); |
| 82 | expect(created.cryptoKey).toContain(`/cryptoKeys/${CRYPTO_KEY_ID}`); |
| 83 | expect(created.name).toContain("/cryptoKeyVersions/"); |
| 84 | expect(["ENABLED", "PENDING_GENERATION"]).toContain(created.state); |
| 85 | |
| 86 | yield* waitVersionReady(created.name); |
| 87 | |
| 88 | const fetched = yield* kms.getProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersions({ |
| 89 | name: created.name, |
| 90 | }); |
| 91 | expect(fetched.name).toEqual(created.name); |
| 92 | expect(fetched.state).toEqual("ENABLED"); |
| 93 | |
| 94 | const disabled = yield* stack.deploy( |
| 95 | Effect.gen(function* () { |
| 96 | const ring = yield* GCP.KMS.KeyRing("Keys", { |
| 97 | keyRingId: KEY_RING_ID, |
| 98 | location: "us-central1", |
| 99 | }); |
| 100 | const key = yield* GCP.KMS.CryptoKey("Data", { |
| 101 | keyRing: ring.name, |
| 102 | cryptoKeyId: CRYPTO_KEY_ID, |
| 103 | skipInitialVersionCreation: true, |
| 104 | labels: { env: "test" }, |
| 105 | }); |
| 106 | return yield* GCP.KMS.CryptoKeyVersion("V1", { |
| 107 | cryptoKey: key.name, |
| 108 | cryptoKeyVersionId: created.cryptoKeyVersionId, |
| 109 | state: "DISABLED", |
| 110 | }); |
| 111 | }), |
| 112 | ); |
| 113 | expect(disabled.name).toEqual(created.name); |
| 114 | expect(disabled.state).toEqual("DISABLED"); |
| 115 | |
| 116 | const fetchedDisabled = yield* kms.getProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersions({ |
| 117 | name: created.name, |
| 118 | }); |
| 119 | expect(fetchedDisabled.state).toEqual("DISABLED"); |
| 120 | |
| 121 | const enabled = yield* stack.deploy( |
| 122 | Effect.gen(function* () { |
| 123 | const ring = yield* GCP.KMS.KeyRing("Keys", { |
| 124 | keyRingId: KEY_RING_ID, |
| 125 | location: "us-central1", |
| 126 | }); |
| 127 | const key = yield* GCP.KMS.CryptoKey("Data", { |
| 128 | keyRing: ring.name, |
| 129 | cryptoKeyId: CRYPTO_KEY_ID, |
| 130 | skipInitialVersionCreation: true, |
| 131 | labels: { env: "test" }, |
| 132 | }); |
| 133 | return yield* GCP.KMS.CryptoKeyVersion("V1", { |
| 134 | cryptoKey: key.name, |
| 135 | cryptoKeyVersionId: created.cryptoKeyVersionId, |
| 136 | state: "ENABLED", |
| 137 | }); |
| 138 | }), |
| 139 | ); |
| 140 | expect(enabled.name).toEqual(created.name); |
| 141 | expect(enabled.state).toEqual("ENABLED"); |
| 142 | |
| 143 | yield* stack.destroy(); |
| 144 | |
| 145 | const afterDestroy = yield* kms |
| 146 | .getProjectsLocationsKeyRingsCryptoKeysCryptoKeyVersions({ |
| 147 | name: created.name, |
| 148 | }) |
| 149 | .pipe( |
| 150 | Effect.map((version) => version.state ?? "found"), |
| 151 | Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)), |
| 152 | ); |
| 153 | expect(["DESTROY_SCHEDULED", "DESTROYED", "gone"]).toContain(afterDestroy); |
| 154 | |
| 155 | const gone = yield* waitUntilGone( |
| 156 | `projects/${project}/locations/us-central1/keyRings/${KEY_RING_ID}/cryptoKeys/${CRYPTO_KEY_ID}/cryptoKeyVersions/999999`, |
| 157 | ); |
| 158 | expect(gone).toEqual("gone"); |
| 159 | }).pipe(logLevel), |
| 160 | { tags: ["provider:gcp", "provider:gcp:kms", "live"], timeout: 90_000 }, |
| 161 | ); |