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 { KEY_RING_ID, kmsTestId } from "./common.ts"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: GCP.providers() }); |
| 11 | |
| 12 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 13 | |
| 14 | // Cloud KMS KeyRings cannot be deleted. Reuse the standing test ring. |
| 15 | // Encrypt/decrypt needs a version; versions cannot be deleted for ≥24h, so |
| 16 | // this key is reused across runs (names cannot be reused after delete). |
| 17 | const ENCRYPT_KEY_ID = kmsTestId("cryptokey"); |
| 18 | |
| 19 | test.provider( |
| 20 | "getProjectsLocationsKeyRingsCryptoKeys on a missing key fails with NotFound", |
| 21 | () => |
| 22 | Effect.gen(function* () { |
| 23 | const { project } = yield* GcpEnvironment.current; |
| 24 | const error = yield* Effect.flip( |
| 25 | kms.getProjectsLocationsKeyRingsCryptoKeys({ |
| 26 | name: `projects/${project}/locations/us-central1/keyRings/${KEY_RING_ID}/cryptoKeys/alchemy-cryptokey-does-not-exist`, |
| 27 | }), |
| 28 | ); |
| 29 | expect(error._tag).toBe("NotFound"); |
| 30 | }).pipe(logLevel), |
| 31 | { tags: ["provider:gcp", "provider:gcp:kms", "live"] }, |
| 32 | ); |
| 33 | |
| 34 | test.provider( |
| 35 | "create, update, and release a crypto key", |
| 36 | (stack) => |
| 37 | Effect.gen(function* () { |
| 38 | const { project } = yield* GcpEnvironment.current; |
| 39 | yield* stack.destroy(); |
| 40 | |
| 41 | const created = yield* stack.deploy( |
| 42 | Effect.gen(function* () { |
| 43 | const ring = yield* GCP.KMS.KeyRing("Keys", { |
| 44 | keyRingId: KEY_RING_ID, |
| 45 | location: "us-central1", |
| 46 | }); |
| 47 | return yield* GCP.KMS.CryptoKey("Data", { |
| 48 | keyRing: ring.name, |
| 49 | skipInitialVersionCreation: true, |
| 50 | labels: { env: "test" }, |
| 51 | }); |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | expect(created.cryptoKeyId).toEqual(expect.any(String)); |
| 56 | expect(created.location).toEqual("us-central1"); |
| 57 | expect(created.project).toEqual(project); |
| 58 | expect(created.purpose).toEqual("ENCRYPT_DECRYPT"); |
| 59 | expect(created.keyRing).toContain(`/keyRings/${KEY_RING_ID}`); |
| 60 | expect(created.name).toContain("/cryptoKeys/"); |
| 61 | expect(created.labels).toMatchObject({ env: "test" }); |
| 62 | expect(created.primaryVersion).toBeUndefined(); |
| 63 | |
| 64 | const fetched = yield* kms.getProjectsLocationsKeyRingsCryptoKeys({ |
| 65 | name: created.name, |
| 66 | }); |
| 67 | expect(fetched.name).toEqual(created.name); |
| 68 | expect(fetched.labels?.env).toEqual("test"); |
| 69 | expect(fetched.purpose).toEqual("ENCRYPT_DECRYPT"); |
| 70 | |
| 71 | const updated = yield* stack.deploy( |
| 72 | Effect.gen(function* () { |
| 73 | const ring = yield* GCP.KMS.KeyRing("Keys", { |
| 74 | keyRingId: KEY_RING_ID, |
| 75 | location: "us-central1", |
| 76 | }); |
| 77 | return yield* GCP.KMS.CryptoKey("Data", { |
| 78 | keyRing: ring.name, |
| 79 | cryptoKeyId: created.cryptoKeyId, |
| 80 | skipInitialVersionCreation: true, |
| 81 | labels: { env: "prod", role: "cmek" }, |
| 82 | }); |
| 83 | }), |
| 84 | ); |
| 85 | |
| 86 | expect(updated.name).toEqual(created.name); |
| 87 | expect(updated.labels).toMatchObject({ env: "prod", role: "cmek" }); |
| 88 | |
| 89 | const fetchedUpdate = yield* kms.getProjectsLocationsKeyRingsCryptoKeys({ |
| 90 | name: created.name, |
| 91 | }); |
| 92 | expect(fetchedUpdate.labels?.env).toEqual("prod"); |
| 93 | expect(fetchedUpdate.labels?.role).toEqual("cmek"); |
| 94 | |
| 95 | yield* stack.destroy(); |
| 96 | |
| 97 | // Deleting would retire the name forever; destroy releases instead. |
| 98 | const released = yield* kms.getProjectsLocationsKeyRingsCryptoKeys({ |
| 99 | name: created.name, |
| 100 | }); |
| 101 | expect(released.labels?.["alchemy-released"]).toEqual("true"); |
| 102 | expect(released.labels?.["alchemy-id"]).toBeUndefined(); |
| 103 | }).pipe(logLevel), |
| 104 | { tags: ["provider:gcp", "provider:gcp:kms", "live"], timeout: 90_000 }, |
| 105 | ); |
| 106 | |
| 107 | const roundTrip = (name: string, text: string) => |
| 108 | Effect.gen(function* () { |
| 109 | const plaintext = yield* Effect.sync(() => Buffer.from(text, "utf8").toString("base64")); |
| 110 | const { ciphertext } = yield* kms.encryptProjectsLocationsKeyRingsCryptoKeys({ |
| 111 | name, |
| 112 | body: { plaintext }, |
| 113 | }); |
| 114 | const decrypted = yield* kms.decryptProjectsLocationsKeyRingsCryptoKeys({ |
| 115 | name, |
| 116 | body: { ciphertext }, |
| 117 | }); |
| 118 | return decrypted.plaintext === plaintext; |
| 119 | }); |
| 120 | |
| 121 | test.provider( |
| 122 | "destroy releases a fixed-id key and the next deploy reclaims it", |
| 123 | (stack) => |
| 124 | Effect.gen(function* () { |
| 125 | yield* stack.destroy(); |
| 126 | |
| 127 | const deploy = stack.deploy( |
| 128 | Effect.gen(function* () { |
| 129 | const ring = yield* GCP.KMS.KeyRing("Keys", { |
| 130 | keyRingId: KEY_RING_ID, |
| 131 | location: "us-central1", |
| 132 | }); |
| 133 | return yield* GCP.KMS.CryptoKey("Cipher", { |
| 134 | keyRing: ring.name, |
| 135 | cryptoKeyId: ENCRYPT_KEY_ID, |
| 136 | labels: { env: "test" }, |
| 137 | }); |
| 138 | }), |
| 139 | ); |
| 140 | |
| 141 | const key = yield* deploy; |
| 142 | expect(key.name).toContain(`/cryptoKeys/${ENCRYPT_KEY_ID}`); |
| 143 | expect(yield* roundTrip(key.name, "first")).toEqual(true); |
| 144 | const firstPrimary = (yield* kms.getProjectsLocationsKeyRingsCryptoKeys({ |
| 145 | name: key.name, |
| 146 | })).primary?.name; |
| 147 | |
| 148 | yield* stack.destroy(); |
| 149 | |
| 150 | // KMS keeps the key; Alchemy releases it: every version scheduled for |
| 151 | // destruction, ownership labels swapped for the released marker. |
| 152 | const released = yield* kms.getProjectsLocationsKeyRingsCryptoKeys({ |
| 153 | name: key.name, |
| 154 | }); |
| 155 | expect(released.labels).toEqual({ |
| 156 | env: "test", |
| 157 | "alchemy-released": "true", |
| 158 | }); |
| 159 | expect(released.primary?.state).toEqual("DESTROY_SCHEDULED"); |
| 160 | |
| 161 | // Redeploying the same id reclaims it with fresh key material. |
| 162 | const reclaimed = yield* deploy; |
| 163 | expect(reclaimed.name).toEqual(key.name); |
| 164 | const live = yield* kms.getProjectsLocationsKeyRingsCryptoKeys({ |
| 165 | name: key.name, |
| 166 | }); |
| 167 | expect(live.labels?.["alchemy-released"]).toBeUndefined(); |
| 168 | expect(live.labels?.["alchemy-id"]).toEqual("cipher"); |
| 169 | expect(live.primary?.state).toEqual("ENABLED"); |
| 170 | expect(live.primary?.name).not.toEqual(firstPrimary); |
| 171 | expect(yield* roundTrip(key.name, "second")).toEqual(true); |
| 172 | |
| 173 | yield* stack.destroy(); |
| 174 | }).pipe(logLevel), |
| 175 | { tags: ["provider:gcp", "provider:gcp:kms", "live"], timeout: 120_000 }, |
| 176 | ); |
| 177 | |
| 178 | test.provider( |
| 179 | "redeploy after destroy reuses the default ring and key ids", |
| 180 | (stack) => |
| 181 | Effect.gen(function* () { |
| 182 | const { project } = yield* GcpEnvironment.current; |
| 183 | yield* stack.destroy(); |
| 184 | |
| 185 | const deploy = stack.deploy( |
| 186 | Effect.gen(function* () { |
| 187 | const ring = yield* GCP.KMS.KeyRing("RedeployKeys", { |
| 188 | location: "us-central1", |
| 189 | }); |
| 190 | const key = yield* GCP.KMS.CryptoKey("RedeployData", { |
| 191 | keyRing: ring.name, |
| 192 | }); |
| 193 | return { ring, key }; |
| 194 | }), |
| 195 | ); |
| 196 | |
| 197 | // Only this test creates rings with generated ids in this file. |
| 198 | const generatedRings = kms |
| 199 | .listProjectsLocationsKeyRings({ |
| 200 | parent: `projects/${project}/locations/us-central1`, |
| 201 | pageSize: 1000, |
| 202 | }) |
| 203 | .pipe( |
| 204 | Effect.map((response) => |
| 205 | (response.keyRings ?? []) |
| 206 | .map((ring) => ring.name ?? "") |
| 207 | .filter((name) => name.includes("/keyRings/gcp-kms-cryptokey-")), |
| 208 | ), |
| 209 | ); |
| 210 | const before = new Set(yield* generatedRings); |
| 211 | |
| 212 | const first = yield* deploy; |
| 213 | expect(yield* roundTrip(first.key.name, "first")).toEqual(true); |
| 214 | |
| 215 | yield* stack.destroy(); |
| 216 | |
| 217 | const second = yield* deploy; |
| 218 | expect(second.ring.name).toEqual(first.ring.name); |
| 219 | expect(second.key.name).toEqual(first.key.name); |
| 220 | expect(yield* roundTrip(second.key.name, "second")).toEqual(true); |
| 221 | |
| 222 | // At most the one deterministic ring appeared — no leak per cycle. |
| 223 | const created = (yield* generatedRings).filter((name) => !before.has(name)); |
| 224 | expect(created.filter((name) => name !== first.ring.name)).toEqual([]); |
| 225 | |
| 226 | yield* stack.destroy(); |
| 227 | |
| 228 | const released = yield* kms.getProjectsLocationsKeyRingsCryptoKeys({ |
| 229 | name: first.key.name, |
| 230 | }); |
| 231 | expect(released.labels?.["alchemy-released"]).toEqual("true"); |
| 232 | }).pipe(logLevel), |
| 233 | { tags: ["provider:gcp", "provider:gcp:kms", "live"], timeout: 180_000 }, |
| 234 | ); |