The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as acm from "@distilled.cloud/gcp/accesscontextmanager_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import type { Providers } from "../Providers.ts"; |
| 9 | import { |
| 10 | collectPages, |
| 11 | jsonEqual, |
| 12 | lastSegment, |
| 13 | organizationParent, |
| 14 | replaceOnIdentity, |
| 15 | resolveOrganization, |
| 16 | resourceNameFromOperation, |
| 17 | sameStringList, |
| 18 | tryResolveOrganization, |
| 19 | waitForOperation, |
| 20 | waitUntilExists, |
| 21 | waitUntilGone, |
| 22 | } from "./internal.ts"; |
| 23 | |
| 24 | export type GcpUserAccessBindingApplication = acm.Application; |
| 25 | export type GcpUserAccessBindingSessionSettings = acm.SessionSettings; |
| 26 | export type GcpUserAccessBindingScopedAccessSettings = acm.ScopedAccessSettings; |
| 27 | |
| 28 | export type GcpUserAccessBindingProps = { |
| 29 | /** |
| 30 | * Organization parent (`organizations/{organization}` or the numeric |
| 31 | * organization id). If omitted, Alchemy uses `GOOGLE_ORGANIZATION_ID` |
| 32 | * or the project's Resource Manager parent. Immutable — changing it |
| 33 | * replaces the binding. |
| 34 | */ |
| 35 | organization?: string; |
| 36 | /** |
| 37 | * Google Group id whose users are subject to this binding. Directory |
| 38 | * API group `id`, not the group email. Immutable — changing it |
| 39 | * replaces the binding. Unique per organization. |
| 40 | */ |
| 41 | groupKey: string; |
| 42 | /** |
| 43 | * Access levels a user must satisfy. At most one entry. Example: |
| 44 | * `accessPolicies/9522/accessLevels/device_trusted`. |
| 45 | */ |
| 46 | accessLevels?: string[]; |
| 47 | /** |
| 48 | * Dry-run access levels that are evaluated but not enforced. At most |
| 49 | * one entry. |
| 50 | */ |
| 51 | dryRunAccessLevels?: string[]; |
| 52 | /** |
| 53 | * Google Cloud session length settings for the group. |
| 54 | */ |
| 55 | sessionSettings?: GcpUserAccessBindingSessionSettings; |
| 56 | /** |
| 57 | * Applications subject to this binding. Empty applies the binding to |
| 58 | * every application. Mutually exclusive with `scopedAccessSettings`. |
| 59 | */ |
| 60 | restrictedClientApplications?: GcpUserAccessBindingApplication[]; |
| 61 | /** |
| 62 | * Per-application access settings. Mutually exclusive with |
| 63 | * `restrictedClientApplications`. |
| 64 | */ |
| 65 | scopedAccessSettings?: GcpUserAccessBindingScopedAccessSettings[]; |
| 66 | }; |
| 67 | |
| 68 | export type GcpUserAccessBinding = Resource< |
| 69 | "GCP.AccessContextManager.GcpUserAccessBinding", |
| 70 | GcpUserAccessBindingProps, |
| 71 | { |
| 72 | /** Resource name `organizations/{org}/gcpUserAccessBindings/{id}`. */ |
| 73 | name: string; |
| 74 | /** Binding id (last path segment). Assigned by the API. */ |
| 75 | gcpUserAccessBindingId: string; |
| 76 | /** Organization id. */ |
| 77 | organization: string; |
| 78 | /** Google Group id. */ |
| 79 | groupKey: string | undefined; |
| 80 | /** Enforced access levels. */ |
| 81 | accessLevels: string[]; |
| 82 | /** Dry-run access levels. */ |
| 83 | dryRunAccessLevels: string[]; |
| 84 | /** Session settings, if set. */ |
| 85 | sessionSettings: GcpUserAccessBindingSessionSettings | undefined; |
| 86 | /** Restricted client applications. */ |
| 87 | restrictedClientApplications: GcpUserAccessBindingApplication[]; |
| 88 | /** Scoped access settings. */ |
| 89 | scopedAccessSettings: GcpUserAccessBindingScopedAccessSettings[]; |
| 90 | }, |
| 91 | never, |
| 92 | Providers |
| 93 | >; |
| 94 | |
| 95 | /** |
| 96 | * A Context-Aware Access binding of Cloud Console / API restrictions to |
| 97 | * a Google Group. |
| 98 | * |
| 99 | * Bindings live on an organization. The API assigns the resource name |
| 100 | * and has no labels or description field. Identity is |
| 101 | * `(organization, groupKey)` — Alchemy treats existence at that pair as |
| 102 | * ownership, and `list` returns every binding on the current |
| 103 | * organization so `pnpm nuke:gcp` can clean leaks. |
| 104 | * |
| 105 | * `organization` and `groupKey` are immutable. Access levels, dry-run |
| 106 | * levels, session settings, restricted applications, and scoped settings |
| 107 | * update in place. |
| 108 | * |
| 109 | * ### Creating a Binding |
| 110 | * **Example:** Require an access level for a group |
| 111 | * ```typescript |
| 112 | * const binding = yield* GCP.AccessContextManager.GcpUserAccessBinding( |
| 113 | * "Engineers", |
| 114 | * { |
| 115 | * groupKey: "01d520gv4vjcrht", |
| 116 | * accessLevels: ["accessPolicies/9522/accessLevels/device_trusted"], |
| 117 | * }, |
| 118 | * ); |
| 119 | * ``` |
| 120 | * |
| 121 | * ### Updating a Binding |
| 122 | * **Example:** Change the required access level |
| 123 | * ```typescript |
| 124 | * const binding = yield* GCP.AccessContextManager.GcpUserAccessBinding( |
| 125 | * "Engineers", |
| 126 | * { |
| 127 | * groupKey: "01d520gv4vjcrht", |
| 128 | * accessLevels: ["accessPolicies/9522/accessLevels/corp_network"], |
| 129 | * }, |
| 130 | * ); |
| 131 | * ``` |
| 132 | * |
| 133 | * @resource |
| 134 | * @category AccessContextManager |
| 135 | */ |
| 136 | export const GcpUserAccessBinding = Resource<GcpUserAccessBinding>( |
| 137 | "GCP.AccessContextManager.GcpUserAccessBinding", |
| 138 | ); |
| 139 | |
| 140 | export class GcpUserAccessBindingNotResolved extends Data.TaggedError( |
| 141 | "GCP.AccessContextManager.GcpUserAccessBindingNotResolved", |
| 142 | )<{ |
| 143 | resourceName: string; |
| 144 | }> {} |
| 145 | |
| 146 | const toAttrs = (binding: acm.GcpUserAccessBinding): GcpUserAccessBinding["Attributes"] => { |
| 147 | const name = binding.name ?? ""; |
| 148 | const parts = name.split("/").filter((part) => part.length > 0); |
| 149 | const orgsAt = parts.lastIndexOf("organizations"); |
| 150 | return { |
| 151 | name, |
| 152 | gcpUserAccessBindingId: lastSegment(name), |
| 153 | organization: orgsAt >= 0 && parts[orgsAt + 1] ? parts[orgsAt + 1]! : "", |
| 154 | groupKey: binding.groupKey, |
| 155 | accessLevels: binding.accessLevels ?? [], |
| 156 | dryRunAccessLevels: binding.dryRunAccessLevels ?? [], |
| 157 | sessionSettings: binding.sessionSettings, |
| 158 | restrictedClientApplications: binding.restrictedClientApplications ?? [], |
| 159 | scopedAccessSettings: binding.scopedAccessSettings ?? [], |
| 160 | }; |
| 161 | }; |
| 162 | |
| 163 | const getByName = (name: string) => |
| 164 | acm |
| 165 | .getOrganizationsGcpUserAccessBindings({ name }) |
| 166 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 167 | |
| 168 | const listBindings = (organization: string) => |
| 169 | collectPages( |
| 170 | acm.listOrganizationsGcpUserAccessBindings.pages({ |
| 171 | parent: organizationParent(organization), |
| 172 | pageSize: 100, |
| 173 | }), |
| 174 | (page) => page.gcpUserAccessBindings, |
| 175 | ).pipe(Effect.catchTag("NotFound", () => Effect.succeed([] as acm.GcpUserAccessBinding[]))); |
| 176 | |
| 177 | const findByGroupKey = (organization: string, groupKey: string) => |
| 178 | Effect.gen(function* () { |
| 179 | const bindings = yield* listBindings(organization); |
| 180 | return bindings.find((binding) => binding.groupKey === groupKey); |
| 181 | }); |
| 182 | |
| 183 | const observe = (name: string | undefined, organization: string, groupKey: string | undefined) => |
| 184 | Effect.gen(function* () { |
| 185 | if (name !== undefined && name.length > 0) { |
| 186 | const byName = yield* getByName(name); |
| 187 | if (byName !== undefined) return byName; |
| 188 | } |
| 189 | if (groupKey !== undefined && groupKey.length > 0) { |
| 190 | return yield* findByGroupKey(organization, groupKey); |
| 191 | } |
| 192 | return undefined; |
| 193 | }); |
| 194 | |
| 195 | export const GcpUserAccessBindingProvider = () => |
| 196 | Provider.succeed(GcpUserAccessBinding, { |
| 197 | stables: ["name", "gcpUserAccessBindingId", "organization", "groupKey"], |
| 198 | |
| 199 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 200 | if (!isResolved(news)) return undefined; |
| 201 | const previousOrg = olds?.organization ?? output?.organization; |
| 202 | const nextOrg = news.organization ?? previousOrg; |
| 203 | const orgChanged = |
| 204 | previousOrg !== undefined && |
| 205 | nextOrg !== undefined && |
| 206 | lastSegment(previousOrg) !== lastSegment(nextOrg); |
| 207 | const previousGroup = olds?.groupKey ?? output?.groupKey; |
| 208 | const groupChanged = previousGroup !== undefined && news.groupKey !== previousGroup; |
| 209 | return replaceOnIdentity(orgChanged || groupChanged); |
| 210 | }), |
| 211 | |
| 212 | read: Effect.fn(function* ({ olds, output }) { |
| 213 | const organization = yield* resolveOrganization(olds?.organization, output?.organization); |
| 214 | const existing = yield* observe( |
| 215 | output?.name, |
| 216 | organization, |
| 217 | olds?.groupKey ?? output?.groupKey, |
| 218 | ); |
| 219 | if (existing === undefined) return undefined; |
| 220 | const attrs = toAttrs(existing); |
| 221 | if (output?.name !== undefined && existing.name === output.name) { |
| 222 | return attrs; |
| 223 | } |
| 224 | if ( |
| 225 | (olds?.groupKey ?? output?.groupKey) !== undefined && |
| 226 | existing.groupKey === (olds?.groupKey ?? output?.groupKey) |
| 227 | ) { |
| 228 | return attrs; |
| 229 | } |
| 230 | return Unowned(attrs); |
| 231 | }), |
| 232 | |
| 233 | list: () => |
| 234 | Effect.gen(function* () { |
| 235 | const organization = yield* tryResolveOrganization(); |
| 236 | if (organization === undefined) return []; |
| 237 | const bindings = yield* listBindings(organization); |
| 238 | return bindings.map(toAttrs); |
| 239 | }), |
| 240 | |
| 241 | reconcile: Effect.fn(function* ({ news, output }) { |
| 242 | const organization = yield* resolveOrganization(news.organization, output?.organization); |
| 243 | const desiredLevels = news.accessLevels ?? []; |
| 244 | const desiredDryRun = news.dryRunAccessLevels ?? []; |
| 245 | |
| 246 | let current = yield* observe(output?.name, organization, news.groupKey); |
| 247 | |
| 248 | if (current === undefined) { |
| 249 | const created = yield* acm |
| 250 | .createOrganizationsGcpUserAccessBindings({ |
| 251 | parent: organization, |
| 252 | body: { |
| 253 | groupKey: news.groupKey, |
| 254 | accessLevels: desiredLevels.length > 0 ? desiredLevels : undefined, |
| 255 | dryRunAccessLevels: desiredDryRun.length > 0 ? desiredDryRun : undefined, |
| 256 | sessionSettings: news.sessionSettings, |
| 257 | scopedAccessSettings: news.scopedAccessSettings, |
| 258 | restrictedClientApplications: news.restrictedClientApplications, |
| 259 | }, |
| 260 | }) |
| 261 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 262 | if (created !== undefined) { |
| 263 | // ALREADY_EXISTS (6): a concurrent create won the race. |
| 264 | const settled = yield* waitForOperation(created).pipe( |
| 265 | Effect.catchTag("GCP.OperationFailed", (error) => |
| 266 | error.code === 6 ? Effect.succeed(created) : Effect.fail(error), |
| 267 | ), |
| 268 | ); |
| 269 | const createdName = |
| 270 | resourceNameFromOperation(settled) ?? |
| 271 | resourceNameFromOperation(created) ?? |
| 272 | output?.name; |
| 273 | current = |
| 274 | createdName !== undefined |
| 275 | ? yield* waitUntilExists(getByName(createdName), createdName) |
| 276 | : yield* waitUntilExists( |
| 277 | findByGroupKey(organization, news.groupKey), |
| 278 | `${organization}/gcpUserAccessBindings/${news.groupKey}`, |
| 279 | ); |
| 280 | } else { |
| 281 | current = yield* waitUntilExists( |
| 282 | findByGroupKey(organization, news.groupKey), |
| 283 | `${organization}/gcpUserAccessBindings/${news.groupKey}`, |
| 284 | ); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if (current === undefined || current.name === undefined) { |
| 289 | return yield* new GcpUserAccessBindingNotResolved({ |
| 290 | resourceName: output?.name ?? `${organization}/${news.groupKey}`, |
| 291 | }); |
| 292 | } |
| 293 | |
| 294 | const levelsChanged = !sameStringList(current.accessLevels, desiredLevels); |
| 295 | const dryRunChanged = !sameStringList(current.dryRunAccessLevels, desiredDryRun); |
| 296 | const sessionChanged = |
| 297 | news.sessionSettings !== undefined && |
| 298 | !jsonEqual(current.sessionSettings, news.sessionSettings); |
| 299 | const appsChanged = |
| 300 | news.restrictedClientApplications !== undefined && |
| 301 | !jsonEqual(current.restrictedClientApplications, news.restrictedClientApplications); |
| 302 | const scopedChanged = |
| 303 | news.scopedAccessSettings !== undefined && |
| 304 | !jsonEqual(current.scopedAccessSettings, news.scopedAccessSettings); |
| 305 | |
| 306 | const updateMask = [ |
| 307 | levelsChanged ? "access_levels" : undefined, |
| 308 | dryRunChanged ? "dry_run_access_levels" : undefined, |
| 309 | sessionChanged ? "session_settings" : undefined, |
| 310 | appsChanged ? "restricted_client_applications" : undefined, |
| 311 | scopedChanged ? "scoped_access_settings" : undefined, |
| 312 | ].filter((field): field is string => field !== undefined); |
| 313 | |
| 314 | if (updateMask.length > 0) { |
| 315 | const operation = yield* acm.patchOrganizationsGcpUserAccessBindings({ |
| 316 | name: current.name, |
| 317 | updateMask: updateMask.join(","), |
| 318 | body: { |
| 319 | name: current.name, |
| 320 | accessLevels: desiredLevels, |
| 321 | dryRunAccessLevels: desiredDryRun, |
| 322 | sessionSettings: news.sessionSettings, |
| 323 | scopedAccessSettings: news.scopedAccessSettings, |
| 324 | restrictedClientApplications: news.restrictedClientApplications, |
| 325 | }, |
| 326 | }); |
| 327 | yield* waitForOperation(operation); |
| 328 | current = yield* waitUntilExists(getByName(current.name), current.name); |
| 329 | } |
| 330 | |
| 331 | if (current === undefined) { |
| 332 | return yield* new GcpUserAccessBindingNotResolved({ |
| 333 | resourceName: output?.name ?? `${organization}/${news.groupKey}`, |
| 334 | }); |
| 335 | } |
| 336 | |
| 337 | return toAttrs(current); |
| 338 | }), |
| 339 | |
| 340 | delete: Effect.fn(function* ({ output }) { |
| 341 | const operation = yield* acm |
| 342 | .deleteOrganizationsGcpUserAccessBindings({ name: output.name }) |
| 343 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 344 | if (operation !== undefined) { |
| 345 | // NOT_FOUND (5): already gone. |
| 346 | yield* waitForOperation(operation).pipe( |
| 347 | Effect.catchTag("GCP.OperationFailed", (error) => |
| 348 | error.code === 5 ? Effect.void : Effect.fail(error), |
| 349 | ), |
| 350 | ); |
| 351 | } |
| 352 | yield* waitUntilGone(getByName(output.name), output.name); |
| 353 | }), |
| 354 | }); |