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
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import { GcpEnvironment } from "@alchemy.run/gcp/Environment"; |
| 3 | import { describe, expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as Core from "@alchemy.run/test/Core"; |
| 6 | import * as resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 7 | import * as kafka from "@distilled.cloud/gcp/managedkafka_v1"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { expectProbe } from "../bindingHost.ts"; |
| 10 | import KafkaBindingsHost, { Brokers, Connect, Events, Schemas } from "./fixtures/bindings-host.ts"; |
| 11 | import { releaseKafkaClusterSlot, takeKafkaClusterSlot } from "./quota.ts"; |
| 12 | |
| 13 | const testOptions = { providers: GCP.providers() }; |
| 14 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 15 | const sharedStack = Core.scratchStack(testOptions, "ManagedKafkaBindings"); |
| 16 | |
| 17 | // Kafka and Connect clusters take ~30 minutes to provision. |
| 18 | const runLifecycle = !!process.env.GCP_TEST_SLOW && !process.env.FAST; |
| 19 | |
| 20 | let baseUrl: string; |
| 21 | let hostAccount: string; |
| 22 | let clusterName: string; |
| 23 | let topicName: string; |
| 24 | let connectName: string; |
| 25 | let registryName: string; |
| 26 | |
| 27 | /** |
| 28 | * Managed Kafka has no per-resource IAM policy: bindings grant on the |
| 29 | * project under an IAM Condition matching the bound resource. |
| 30 | */ |
| 31 | const hostProjectGrants = Effect.gen(function* () { |
| 32 | const { project } = yield* GcpEnvironment.current; |
| 33 | const policy = yield* resourcemanager.getIamPolicyProjects({ |
| 34 | resource: `projects/${project}`, |
| 35 | body: { options: { requestedPolicyVersion: 3 } }, |
| 36 | }); |
| 37 | return (policy.bindings ?? []) |
| 38 | .filter((binding) => (binding.members ?? []).includes(`serviceAccount:${hostAccount}`)) |
| 39 | .map((binding) => ({ |
| 40 | role: binding.role, |
| 41 | condition: binding.condition?.expression, |
| 42 | })); |
| 43 | }); |
| 44 | |
| 45 | /** Granted on the project, unconditioned (see GetSchemaRegistryHttp). */ |
| 46 | const SCHEMA_REGISTRY_ROLE = "roles/managedkafka.schemaRegistryViewer"; |
| 47 | |
| 48 | const scopedTo = (name: string) => |
| 49 | `resource.name == "${name}" || resource.name.startsWith("${name}/")`; |
| 50 | |
| 51 | /** The host holds `role` only under the condition scoped to `name`. */ |
| 52 | const expectScopedGrant = (role: string, name: string) => |
| 53 | Effect.gen(function* () { |
| 54 | const grants = yield* hostProjectGrants; |
| 55 | expect(grants).toContainEqual({ role, condition: scopedTo(name) }); |
| 56 | expect(grants).toEqual( |
| 57 | expect.arrayContaining([{ role: SCHEMA_REGISTRY_ROLE, condition: undefined }]), |
| 58 | ); |
| 59 | // Every grant except the schema-registry viewer is condition-scoped. |
| 60 | expect( |
| 61 | grants |
| 62 | .filter((grant) => grant.role !== SCHEMA_REGISTRY_ROLE) |
| 63 | .every((grant) => grant.condition !== undefined), |
| 64 | ).toBe(true); |
| 65 | expect(grants).toHaveLength(4); |
| 66 | }); |
| 67 | |
| 68 | describe.skipIf(!runLifecycle)( |
| 69 | "ManagedKafka Bindings", |
| 70 | { |
| 71 | tags: ["provider:gcp", "provider:gcp:managedkafka", "provider:gcp:run", "live"], |
| 72 | }, |
| 73 | () => { |
| 74 | beforeAll( |
| 75 | Effect.gen(function* () { |
| 76 | yield* takeKafkaClusterSlot; |
| 77 | yield* sharedStack.destroy(); |
| 78 | const out = yield* sharedStack.deploy( |
| 79 | Effect.gen(function* () { |
| 80 | const host = yield* KafkaBindingsHost; |
| 81 | const cluster = yield* Brokers; |
| 82 | const topic = yield* Events; |
| 83 | const connect = yield* Connect; |
| 84 | const registry = yield* Schemas; |
| 85 | return { |
| 86 | uri: host.uri, |
| 87 | serviceAccount: host.serviceAccount, |
| 88 | cluster: cluster.name, |
| 89 | topic: topic.name, |
| 90 | connect: connect.name, |
| 91 | registry: registry.name, |
| 92 | }; |
| 93 | }), |
| 94 | ); |
| 95 | baseUrl = out.uri!; |
| 96 | hostAccount = out.serviceAccount!; |
| 97 | clusterName = out.cluster; |
| 98 | topicName = out.topic; |
| 99 | connectName = out.connect; |
| 100 | registryName = out.registry; |
| 101 | }), |
| 102 | { timeout: 10_800_000 }, |
| 103 | ); |
| 104 | |
| 105 | afterAll(sharedStack.destroy().pipe(Effect.ensuring(releaseKafkaClusterSlot)), { |
| 106 | timeout: 3_600_000, |
| 107 | }); |
| 108 | |
| 109 | describe("GetCluster", () => { |
| 110 | test.provider( |
| 111 | "reads the cluster as the host's service account", |
| 112 | (_stack) => |
| 113 | Effect.gen(function* () { |
| 114 | const out = yield* expectProbe<kafka.Cluster>(baseUrl, "getCluster"); |
| 115 | const direct = yield* kafka.getProjectsLocationsClusters({ |
| 116 | name: clusterName, |
| 117 | }); |
| 118 | expect(out.name).toEqual(clusterName); |
| 119 | expect(out.state).toEqual(direct.state); |
| 120 | yield* expectScopedGrant("roles/managedkafka.viewer", clusterName); |
| 121 | }), |
| 122 | { |
| 123 | tags: ["provider:gcp", "provider:gcp:managedkafka", "live"], |
| 124 | timeout: 600_000, |
| 125 | }, |
| 126 | ); |
| 127 | }); |
| 128 | |
| 129 | describe("GetTopic", () => { |
| 130 | test.provider( |
| 131 | "reads the topic as the host's service account", |
| 132 | (_stack) => |
| 133 | Effect.gen(function* () { |
| 134 | const out = yield* expectProbe<kafka.Topic>(baseUrl, "getTopic"); |
| 135 | const direct = yield* kafka.getProjectsLocationsClustersTopics({ |
| 136 | name: topicName, |
| 137 | }); |
| 138 | expect(out.name).toEqual(topicName); |
| 139 | expect(out.partitionCount).toEqual(direct.partitionCount); |
| 140 | yield* expectScopedGrant("roles/managedkafka.viewer", topicName); |
| 141 | }), |
| 142 | { |
| 143 | tags: ["provider:gcp", "provider:gcp:managedkafka", "live"], |
| 144 | timeout: 600_000, |
| 145 | }, |
| 146 | ); |
| 147 | }); |
| 148 | |
| 149 | describe("GetConnectCluster", () => { |
| 150 | test.provider( |
| 151 | "reads the Connect cluster as the host's service account", |
| 152 | (_stack) => |
| 153 | Effect.gen(function* () { |
| 154 | const out = yield* expectProbe<kafka.ConnectCluster>(baseUrl, "getConnectCluster"); |
| 155 | const direct = yield* kafka.getProjectsLocationsConnectClusters({ |
| 156 | name: connectName, |
| 157 | }); |
| 158 | expect(out.name).toEqual(connectName); |
| 159 | expect(out.kafkaCluster).toEqual(direct.kafkaCluster); |
| 160 | yield* expectScopedGrant("roles/managedkafka.viewer", connectName); |
| 161 | }), |
| 162 | { |
| 163 | tags: ["provider:gcp", "provider:gcp:managedkafka", "live"], |
| 164 | timeout: 600_000, |
| 165 | }, |
| 166 | ); |
| 167 | }); |
| 168 | |
| 169 | describe("GetSchemaRegistry", () => { |
| 170 | test.provider( |
| 171 | "reads the schema registry as the host's service account", |
| 172 | (_stack) => |
| 173 | Effect.gen(function* () { |
| 174 | const out = yield* expectProbe<kafka.SchemaRegistry>(baseUrl, "getSchemaRegistry"); |
| 175 | const direct = yield* kafka.getProjectsLocationsSchemaRegistries({ |
| 176 | name: registryName, |
| 177 | }); |
| 178 | expect(out.name).toEqual(registryName); |
| 179 | expect(out.contexts).toEqual(direct.contexts); |
| 180 | const grants = yield* hostProjectGrants; |
| 181 | expect(grants).toContainEqual({ |
| 182 | role: SCHEMA_REGISTRY_ROLE, |
| 183 | condition: undefined, |
| 184 | }); |
| 185 | expect(grants).toHaveLength(4); |
| 186 | }), |
| 187 | { |
| 188 | tags: ["provider:gcp", "provider:gcp:managedkafka", "live"], |
| 189 | timeout: 600_000, |
| 190 | }, |
| 191 | ); |
| 192 | }); |
| 193 | }, |
| 194 | ); |