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 resourcemanager from "@distilled.cloud/gcp/cloudresourcemanager_v3"; |
| 2 | import * as iam from "@distilled.cloud/gcp/unstable/iam_v1"; |
| 3 | import type { ResourceBinding } from "alchemy/Resource"; |
| 4 | import * as Config from "effect/Config"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as FileSystem from "effect/FileSystem"; |
| 8 | import * as Option from "effect/Option"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import { |
| 11 | hasIamMembership, |
| 12 | projectRolesOf, |
| 13 | revokeIamMembership, |
| 14 | updateIamMembership, |
| 15 | type GcpIamResourceKind, |
| 16 | type IamCondition, |
| 17 | } from "./IamPolicy.ts"; |
| 18 | import type { BoundContainer } from "./Run/Container.ts"; |
| 19 | import { parseServiceAccountKey } from "./Token.ts"; |
| 20 | |
| 21 | export type { GcpIamResourceKind } from "./IamPolicy.ts"; |
| 22 | |
| 23 | /** |
| 24 | * IAM grant a binding attaches to a GCP runtime host (Cloud Run |
| 25 | * Service/Job/WorkerPool, Cloud Function, or a `Kubernetes.Deployment` / |
| 26 | * `Kubernetes.Job` on GKE) — the GCP analog of an AWS binding's |
| 27 | * `policyStatements`. The role is granted to the host's runtime identity: |
| 28 | * the runtime service account, or on GKE the Kubernetes ServiceAccount's |
| 29 | * Workload Identity Federation principal. |
| 30 | */ |
| 31 | export type GcpIamGrant = { |
| 32 | /** |
| 33 | * Predefined or custom role, e.g. `roles/pubsub.publisher`. Pick the |
| 34 | * narrowest role that covers the binding's operation. |
| 35 | */ |
| 36 | role: string; |
| 37 | /** |
| 38 | * Grant on this one resource's IAM policy (the analog of an IAM |
| 39 | * statement's `Resource: [arn]`). Omit only for services with no |
| 40 | * resource-level IAM, in which case the role is granted on the project. |
| 41 | */ |
| 42 | resource?: { |
| 43 | kind: Exclude<GcpIamResourceKind, "project">; |
| 44 | /** |
| 45 | * Full resource name, e.g. `projects/p/topics/t` (a bucket name for |
| 46 | * `storage.bucket`), or {@link HOST_SERVICE_ACCOUNT} with kind |
| 47 | * `iam.serviceAccount` for the host's own runtime service account. |
| 48 | */ |
| 49 | name: string; |
| 50 | }; |
| 51 | /** |
| 52 | * For a project-level grant on a service with no per-resource IAM |
| 53 | * policy: an IAM Condition narrowing the role to one resource, e.g. |
| 54 | * `resource.name == "projects/p/databases/d"`. |
| 55 | */ |
| 56 | condition?: IamCondition; |
| 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * Binding contract for GCP effectful hosts. Capability implementations |
| 61 | * call `host.bind` with env (packed into the container/function) and IAM |
| 62 | * grants (applied to the runtime service account). |
| 63 | */ |
| 64 | export type GcpHostBinding = { |
| 65 | env?: Record<string, any>; |
| 66 | iam?: GcpIamGrant[]; |
| 67 | /** |
| 68 | * Cloud SQL connection names (`project:region:instance`) to mount as |
| 69 | * Unix sockets under `/cloudsql` (Cloud Run services only). |
| 70 | */ |
| 71 | cloudSqlInstances?: string[]; |
| 72 | /** |
| 73 | * Sidecar containers to add to the host's revision (Cloud Run services |
| 74 | * and containers only); see `GCP.Run.bindContainer`. |
| 75 | */ |
| 76 | containers?: BoundContainer[]; |
| 77 | }; |
| 78 | |
| 79 | /** |
| 80 | * One role granted to a host's runtime service account on one resource, |
| 81 | * recorded on the host's attributes so a later deploy can revoke it once |
| 82 | * the binding that asked for it is gone. |
| 83 | */ |
| 84 | export type AppliedIamGrant = { |
| 85 | kind: GcpIamResourceKind; |
| 86 | /** Full resource name, or the project id for `project`. */ |
| 87 | name: string; |
| 88 | role: string; |
| 89 | /** IAM Condition the role is bound under, if any. */ |
| 90 | condition?: IamCondition; |
| 91 | }; |
| 92 | |
| 93 | /** Merge the env and IAM grants of every binding attached to a host. */ |
| 94 | export const collectHostBindings = ( |
| 95 | bindings: readonly ResourceBinding<GcpHostBinding>[], |
| 96 | ): { env: Record<string, any>; iam: GcpIamGrant[] } => ({ |
| 97 | env: bindings |
| 98 | .map((binding) => binding.data?.env) |
| 99 | .reduce<Record<string, any>>((acc, next) => ({ ...acc, ...next }), {}), |
| 100 | iam: bindings.flatMap((binding) => binding.data?.iam ?? []), |
| 101 | }); |
| 102 | |
| 103 | const lastSegment = (value: string) => { |
| 104 | const trimmed = value.replace(/\/+$/, ""); |
| 105 | const parts = trimmed.split("/"); |
| 106 | return parts[parts.length - 1] || trimmed; |
| 107 | }; |
| 108 | |
| 109 | /** |
| 110 | * Project number for the default Compute Engine service account |
| 111 | * (`{number}-compute@developer.gserviceaccount.com`). |
| 112 | */ |
| 113 | export const projectNumber = (project: string) => |
| 114 | resourcemanager |
| 115 | .getProjects({ name: `projects/${project}` }) |
| 116 | .pipe(Effect.map((resource) => lastSegment(resource.name ?? ""))); |
| 117 | |
| 118 | export const defaultComputeServiceAccount = (project: string) => |
| 119 | projectNumber(project).pipe( |
| 120 | Effect.map((number) => `${number}-compute@developer.gserviceaccount.com`), |
| 121 | ); |
| 122 | |
| 123 | /** 64-bit FNV-1a, hex. Deterministic and dependency-free. */ |
| 124 | const fnv1a64 = (input: string): string => { |
| 125 | let hash = 0xcbf29ce484222325n; |
| 126 | for (let index = 0; index < input.length; index++) { |
| 127 | hash ^= BigInt(input.charCodeAt(index)); |
| 128 | hash = (hash * 0x100000001b3n) & 0xffffffffffffffffn; |
| 129 | } |
| 130 | return hash.toString(16).padStart(16, "0"); |
| 131 | }; |
| 132 | |
| 133 | /** |
| 134 | * RFC1035 account id (6–30 chars) for the runtime service account Alchemy |
| 135 | * mints for one physical host. The hash covers the host type and full |
| 136 | * resource name, so two hosts never share an account — not across |
| 137 | * stages, not a Service and a Job with the same id, and not the two |
| 138 | * generations of a create-before-delete replacement. |
| 139 | */ |
| 140 | export const hostServiceAccountId = (hostType: string, resourceName: string): string => { |
| 141 | const slug = lastSegment(resourceName) |
| 142 | .toLowerCase() |
| 143 | .replace(/[^a-z0-9-]/g, "-") |
| 144 | .replace(/-+/g, "-") |
| 145 | .replace(/^-+|-+$/g, "") |
| 146 | .slice(0, 12) |
| 147 | .replace(/-+$/g, ""); |
| 148 | const hash = fnv1a64(`${hostType}:${resourceName}`).slice(0, 10); |
| 149 | return /^[a-z]/.test(slug) ? `alch-${slug}-${hash}` : `alch-${hash}`; |
| 150 | }; |
| 151 | |
| 152 | export const hostServiceAccountEmail = (project: string, accountId: string) => |
| 153 | `${accountId}@${project}.iam.gserviceaccount.com`; |
| 154 | |
| 155 | /** Display name stamped on Alchemy-minted host SAs so nuke can list them. */ |
| 156 | export const ALCHEMY_HOST_SA_DISPLAY_NAME = "alchemy-host"; |
| 157 | |
| 158 | const serviceAccountName = (project: string, email: string) => |
| 159 | `projects/${project}/serviceAccounts/${email}`; |
| 160 | |
| 161 | /** |
| 162 | * The numeric unique ID of a host's runtime service account: the `sub` of |
| 163 | * the Google ID tokens it fetches, which AWS roles trust. A just-created |
| 164 | * account can read as missing for a few seconds. |
| 165 | */ |
| 166 | export const serviceAccountUniqueId = (email: string) => |
| 167 | iam.getProjectsServiceAccounts({ name: `projects/-/serviceAccounts/${email}` }).pipe( |
| 168 | Effect.retry({ |
| 169 | while: (error) => error._tag === "NotFound", |
| 170 | times: 8, |
| 171 | schedule: Schedule.spaced("2 seconds"), |
| 172 | }), |
| 173 | Effect.map((account) => account.uniqueId), |
| 174 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 175 | ); |
| 176 | |
| 177 | /** |
| 178 | * Let the deploying principal and the serverless robots `actAs` the host |
| 179 | * service account so Cloud Run / Functions accept it as the runtime |
| 180 | * identity. |
| 181 | */ |
| 182 | const grantActAs = (project: string, email: string) => |
| 183 | Effect.gen(function* () { |
| 184 | const number = yield* projectNumber(project); |
| 185 | const members = [ |
| 186 | `serviceAccount:service-${number}@serverless-robot-prod.iam.gserviceaccount.com`, |
| 187 | `serviceAccount:service-${number}@gcf-admin-robot.iam.gserviceaccount.com`, |
| 188 | ]; |
| 189 | const keyFile = yield* Config.option(Config.String("GOOGLE_APPLICATION_CREDENTIALS")); |
| 190 | if (Option.isSome(keyFile)) { |
| 191 | const fs = yield* FileSystem.FileSystem; |
| 192 | const raw = yield* fs |
| 193 | .readFileString(keyFile.value) |
| 194 | .pipe(Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(""))); |
| 195 | if (raw.length > 0) { |
| 196 | const parsed = yield* parseServiceAccountKey(raw).pipe( |
| 197 | Effect.catchTag("AuthError", () => Effect.undefined), |
| 198 | ); |
| 199 | if (parsed?.client_email) { |
| 200 | members.push(`serviceAccount:${parsed.client_email}`); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | for (const member of members) { |
| 205 | yield* updateIamMembership({ |
| 206 | kind: "iam.serviceAccount", |
| 207 | name: serviceAccountName(project, email), |
| 208 | member, |
| 209 | add: ["roles/iam.serviceAccountUser"], |
| 210 | }); |
| 211 | } |
| 212 | }); |
| 213 | |
| 214 | /** |
| 215 | * Create (or adopt) the runtime service account for one host and let |
| 216 | * Cloud Run / Functions `actAs` it. `created` reports whether this call |
| 217 | * minted it, so a failed first deploy can clean up only what it made. |
| 218 | */ |
| 219 | export const ensureHostServiceAccount = (options: { |
| 220 | project: string; |
| 221 | hostType: string; |
| 222 | resourceName: string; |
| 223 | /** Let Cloud Run / Functions run as the account. @default true */ |
| 224 | actAs?: boolean; |
| 225 | }) => { |
| 226 | const accountId = hostServiceAccountId(options.hostType, options.resourceName); |
| 227 | const email = hostServiceAccountEmail(options.project, accountId); |
| 228 | return Effect.gen(function* () { |
| 229 | const existing = yield* iam |
| 230 | .getProjectsServiceAccounts({ |
| 231 | name: serviceAccountName(options.project, email), |
| 232 | }) |
| 233 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 234 | let created = false; |
| 235 | if (existing?.email === undefined || existing.email.length === 0) { |
| 236 | created = yield* iam |
| 237 | .createProjectsServiceAccounts({ |
| 238 | name: `projects/${options.project}`, |
| 239 | body: { |
| 240 | accountId, |
| 241 | serviceAccount: { displayName: ALCHEMY_HOST_SA_DISPLAY_NAME }, |
| 242 | }, |
| 243 | }) |
| 244 | .pipe( |
| 245 | Effect.as(true), |
| 246 | Effect.retry({ |
| 247 | while: (error) => error._tag === "ServiceAccountQuotaExceeded", |
| 248 | times: 6, |
| 249 | schedule: Schedule.exponential("2 seconds"), |
| 250 | }), |
| 251 | Effect.catchTag("Conflict", () => Effect.succeed(false)), |
| 252 | ); |
| 253 | } |
| 254 | if (options.actAs !== false) { |
| 255 | yield* grantActAs(options.project, email); |
| 256 | } |
| 257 | return { email, created }; |
| 258 | }); |
| 259 | }; |
| 260 | |
| 261 | /** Retry Cloud Run / Functions create while IAM `actAs` is propagating. */ |
| 262 | export const retryActAs = <A, E extends { _tag: string }, R>(effect: Effect.Effect<A, E, R>) => |
| 263 | effect.pipe( |
| 264 | Effect.retry({ |
| 265 | while: (error) => error._tag === "Forbidden", |
| 266 | times: 8, |
| 267 | schedule: Schedule.spaced("2 seconds"), |
| 268 | }), |
| 269 | ); |
| 270 | |
| 271 | const conditionKey = (condition: IamCondition | undefined) => |
| 272 | condition === undefined ? "" : `${condition.title ?? ""}\u0001${condition.expression ?? ""}`; |
| 273 | |
| 274 | const grantKey = (grant: AppliedIamGrant) => |
| 275 | `${grant.kind}\u0000${grant.name}\u0000${grant.role}\u0000${conditionKey(grant.condition)}`; |
| 276 | |
| 277 | const groupByTarget = (grants: readonly AppliedIamGrant[]) => { |
| 278 | const groups = new Map< |
| 279 | string, |
| 280 | { |
| 281 | kind: GcpIamResourceKind; |
| 282 | name: string; |
| 283 | condition: IamCondition | undefined; |
| 284 | roles: Set<string>; |
| 285 | } |
| 286 | >(); |
| 287 | for (const grant of grants) { |
| 288 | const key = `${grant.kind}\u0000${grant.name}\u0000${conditionKey(grant.condition)}`; |
| 289 | const group = groups.get(key) ?? { |
| 290 | kind: grant.kind, |
| 291 | name: grant.name, |
| 292 | condition: grant.condition, |
| 293 | roles: new Set<string>(), |
| 294 | }; |
| 295 | group.roles.add(grant.role); |
| 296 | groups.set(key, group); |
| 297 | } |
| 298 | return [...groups.values()]; |
| 299 | }; |
| 300 | |
| 301 | /** |
| 302 | * Placeholder resource name (kind `iam.serviceAccount`) for the host's |
| 303 | * own runtime service account, e.g. to let it sign blobs as itself. |
| 304 | * Resolved when the host syncs IAM; dropped for hosts whose identity is |
| 305 | * not a service account (GKE Workload Identity principals). |
| 306 | */ |
| 307 | export const HOST_SERVICE_ACCOUNT = "@host"; |
| 308 | |
| 309 | /** Desired grants for a host, deduplicated and resolved to concrete targets. */ |
| 310 | export const desiredHostGrants = ( |
| 311 | project: string, |
| 312 | grants: readonly GcpIamGrant[], |
| 313 | serviceAccount?: string, |
| 314 | ): AppliedIamGrant[] => { |
| 315 | const ownAccount = |
| 316 | serviceAccount !== undefined && !serviceAccount.includes(":") |
| 317 | ? `projects/-/serviceAccounts/${serviceAccount}` |
| 318 | : undefined; |
| 319 | const unique = new Map<string, AppliedIamGrant>(); |
| 320 | for (const grant of grants) { |
| 321 | if (grant.role.length === 0) continue; |
| 322 | const self = grant.resource?.name === HOST_SERVICE_ACCOUNT; |
| 323 | if (self && ownAccount === undefined) continue; |
| 324 | const applied: AppliedIamGrant = |
| 325 | grant.resource === undefined |
| 326 | ? { |
| 327 | kind: "project", |
| 328 | name: project, |
| 329 | role: grant.role, |
| 330 | ...(grant.condition ? { condition: grant.condition } : {}), |
| 331 | } |
| 332 | : { |
| 333 | kind: grant.resource.kind, |
| 334 | name: self ? ownAccount! : grant.resource.name, |
| 335 | role: grant.role, |
| 336 | }; |
| 337 | unique.set(grantKey(applied), applied); |
| 338 | } |
| 339 | return [...unique.values()].sort((left, right) => grantKey(left).localeCompare(grantKey(right))); |
| 340 | }; |
| 341 | |
| 342 | /** |
| 343 | * Converge the runtime service account's IAM onto the host's bindings. |
| 344 | * |
| 345 | * Every desired grant is added on its target resource. Grants recorded |
| 346 | * on a previous deploy that no binding asks for any more are revoked. |
| 347 | * For an Alchemy-minted account, the host project policy is additionally |
| 348 | * synced against the observed policy, so roles granted out of band (or |
| 349 | * lost from state) are revoked too; a user-supplied account is only ever |
| 350 | * stripped of grants Alchemy itself recorded. |
| 351 | */ |
| 352 | export const syncHostIam = Effect.fn(function* (options: { |
| 353 | project: string; |
| 354 | serviceAccount: string; |
| 355 | managed: boolean; |
| 356 | bindings: readonly ResourceBinding<GcpHostBinding>[]; |
| 357 | previous: readonly AppliedIamGrant[] | undefined; |
| 358 | }) { |
| 359 | const collected = collectHostBindings(options.bindings); |
| 360 | const desired = desiredHostGrants(options.project, collected.iam, options.serviceAccount); |
| 361 | const desiredKeys = new Set(desired.map(grantKey)); |
| 362 | const stale = (options.previous ?? []).filter((grant) => !desiredKeys.has(grantKey(grant))); |
| 363 | |
| 364 | // A principal Alchemy doesn't own (user-supplied service account, GKE |
| 365 | // Workload Identity principal) may already hold a role for other |
| 366 | // reasons. Such a grant is used but never recorded as ours, so a later |
| 367 | // deploy or destroy never revokes it. |
| 368 | const previousKeys = new Set((options.previous ?? []).map(grantKey)); |
| 369 | const preexisting = new Set<string>(); |
| 370 | if (!options.managed) { |
| 371 | for (const grant of desired) { |
| 372 | if (previousKeys.has(grantKey(grant)) || grant.condition !== undefined) { |
| 373 | continue; |
| 374 | } |
| 375 | const held = yield* hasIamMembership({ |
| 376 | kind: grant.kind, |
| 377 | name: grant.name, |
| 378 | member: options.serviceAccount, |
| 379 | role: grant.role, |
| 380 | }); |
| 381 | if (held) preexisting.add(grantKey(grant)); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | for (const group of groupByTarget(desired)) { |
| 386 | yield* updateIamMembership({ |
| 387 | kind: group.kind, |
| 388 | name: group.name, |
| 389 | member: options.serviceAccount, |
| 390 | add: group.roles, |
| 391 | condition: group.condition, |
| 392 | }); |
| 393 | } |
| 394 | |
| 395 | // Unconditional project grants of a minted account are synced against |
| 396 | // the observed policy below; conditional ones only via the record. |
| 397 | const isHostProject = (grant: { kind: string; name: string; condition?: IamCondition }) => |
| 398 | grant.kind === "project" && grant.name === options.project && grant.condition === undefined; |
| 399 | |
| 400 | if (options.managed) { |
| 401 | const wanted = new Set(desired.filter(isHostProject).map((grant) => grant.role)); |
| 402 | const held = yield* projectRolesOf(options.project, options.serviceAccount); |
| 403 | const extra = held.filter((role) => !wanted.has(role)); |
| 404 | if (extra.length > 0) { |
| 405 | yield* revokeIamMembership({ |
| 406 | kind: "project", |
| 407 | name: options.project, |
| 408 | member: options.serviceAccount, |
| 409 | roles: extra, |
| 410 | }); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | for (const group of groupByTarget(stale)) { |
| 415 | if (options.managed && isHostProject(group)) continue; |
| 416 | yield* revokeIamMembership({ |
| 417 | kind: group.kind, |
| 418 | name: group.name, |
| 419 | member: options.serviceAccount, |
| 420 | roles: group.roles, |
| 421 | condition: group.condition, |
| 422 | }); |
| 423 | } |
| 424 | |
| 425 | return { |
| 426 | env: collected.env, |
| 427 | grants: desired.filter((grant) => !preexisting.has(grantKey(grant))), |
| 428 | }; |
| 429 | }); |
| 430 | |
| 431 | /** Revoke every recorded grant from the host's runtime service account. */ |
| 432 | export const revokeHostIam = Effect.fn(function* (options: { |
| 433 | serviceAccount: string; |
| 434 | grants: readonly AppliedIamGrant[]; |
| 435 | }) { |
| 436 | for (const group of groupByTarget(options.grants)) { |
| 437 | yield* revokeIamMembership({ |
| 438 | kind: group.kind, |
| 439 | name: group.name, |
| 440 | member: options.serviceAccount, |
| 441 | roles: group.roles, |
| 442 | condition: group.condition, |
| 443 | }); |
| 444 | } |
| 445 | }); |
| 446 | |
| 447 | /** |
| 448 | * Delete an Alchemy-minted host service account (no-op if it is already |
| 449 | * gone) after revoking its project roles and every recorded grant, so no |
| 450 | * `deleted:serviceAccount:` members linger on other resources. |
| 451 | */ |
| 452 | export const deleteHostServiceAccount = Effect.fn(function* (options: { |
| 453 | project: string; |
| 454 | email: string; |
| 455 | grants?: readonly AppliedIamGrant[]; |
| 456 | }) { |
| 457 | yield* revokeHostIam({ |
| 458 | serviceAccount: options.email, |
| 459 | grants: options.grants ?? [], |
| 460 | }); |
| 461 | const held = yield* projectRolesOf(options.project, options.email); |
| 462 | if (held.length > 0) { |
| 463 | yield* revokeIamMembership({ |
| 464 | kind: "project", |
| 465 | name: options.project, |
| 466 | member: options.email, |
| 467 | roles: held, |
| 468 | }); |
| 469 | } |
| 470 | const name = serviceAccountName(options.project, options.email); |
| 471 | yield* iam |
| 472 | .deleteProjectsServiceAccounts({ name }) |
| 473 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 474 | // Block until IAM stops serving the account. |
| 475 | yield* iam.getProjectsServiceAccounts({ name }).pipe( |
| 476 | Effect.flatMap(() => Effect.fail(new HostServiceAccountStillExists({ email: options.email }))), |
| 477 | Effect.catchTag("NotFound", () => Effect.void), |
| 478 | Effect.retry({ |
| 479 | while: (error) => error._tag === "GCP.HostServiceAccountStillExists", |
| 480 | times: 30, |
| 481 | schedule: Schedule.spaced("2 seconds"), |
| 482 | }), |
| 483 | ); |
| 484 | }); |
| 485 | |
| 486 | export class HostServiceAccountStillExists extends Data.TaggedError( |
| 487 | "GCP.HostServiceAccountStillExists", |
| 488 | )<{ email: string }> {} |