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 { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import type { Providers } from "../Providers.ts"; |
| 8 | import { |
| 9 | collectPages, |
| 10 | hasOwnershipMarker, |
| 11 | listOwnedPolicies, |
| 12 | parseName, |
| 13 | policyNameOf, |
| 14 | replaceOnIdentity, |
| 15 | resourceNameOf, |
| 16 | sameStringList, |
| 17 | toAcmId, |
| 18 | waitForOperation, |
| 19 | waitUntilExists, |
| 20 | waitUntilGone, |
| 21 | } from "./internal.ts"; |
| 22 | |
| 23 | export type AuthorizedOrgsDescDirection = acm.AuthorizedOrgsDescAuthorizationDirectionEnum; |
| 24 | export type AuthorizedOrgsDescAssetType = acm.AuthorizedOrgsDescAssetTypeEnum; |
| 25 | export type AuthorizedOrgsDescAuthorizationType = acm.AuthorizedOrgsDescAuthorizationTypeEnum; |
| 26 | |
| 27 | export type AccessPoliciesAuthorizedOrgsDescProps = { |
| 28 | /** |
| 29 | * Parent access policy (`accessPolicies/{policy}` or the policy id). |
| 30 | * Immutable — changing it replaces the descriptor. |
| 31 | */ |
| 32 | policy: string; |
| 33 | /** |
| 34 | * Authorized-orgs descriptor id (the `{authorized_orgs_desc}` |
| 35 | * segment). If omitted, a unique name is generated from the stack, |
| 36 | * stage, and logical id. Must begin with a letter, then alphanumeric |
| 37 | * or `_`. Immutable — changing it replaces the descriptor. |
| 38 | */ |
| 39 | authorizedOrgsDescId?: string; |
| 40 | /** |
| 41 | * Direction of the authorization relationship. |
| 42 | * `AUTHORIZATION_DIRECTION_FROM` lets this organization evaluate |
| 43 | * traffic in `orgs`. `AUTHORIZATION_DIRECTION_TO` lets `orgs` evaluate |
| 44 | * traffic in this organization. Immutable — changing it replaces the |
| 45 | * descriptor. |
| 46 | */ |
| 47 | authorizationDirection: AuthorizedOrgsDescDirection | (string & {}); |
| 48 | /** |
| 49 | * Asset type. Immutable — changing it replaces the descriptor. |
| 50 | */ |
| 51 | assetType: AuthorizedOrgsDescAssetType | (string & {}); |
| 52 | /** |
| 53 | * Authorization type. Currently only `AUTHORIZATION_TYPE_TRUST`. |
| 54 | * Immutable — changing it replaces the descriptor. |
| 55 | */ |
| 56 | authorizationType: AuthorizedOrgsDescAuthorizationType | (string & {}); |
| 57 | /** |
| 58 | * Organization ids in this descriptor. Format: `organizations/{id}`. |
| 59 | * This is the only mutable field. |
| 60 | */ |
| 61 | orgs?: string[]; |
| 62 | }; |
| 63 | |
| 64 | export type AccessPoliciesAuthorizedOrgsDesc = Resource< |
| 65 | "GCP.AccessContextManager.AccessPoliciesAuthorizedOrgsDesc", |
| 66 | AccessPoliciesAuthorizedOrgsDescProps, |
| 67 | { |
| 68 | /** Resource name `accessPolicies/{policy}/authorizedOrgsDescs/{id}`. */ |
| 69 | name: string; |
| 70 | /** Descriptor id (last path segment). */ |
| 71 | authorizedOrgsDescId: string; |
| 72 | /** Parent policy name `accessPolicies/{policy}`. */ |
| 73 | policy: string; |
| 74 | /** Authorization direction. */ |
| 75 | authorizationDirection: string | undefined; |
| 76 | /** Asset type. */ |
| 77 | assetType: string | undefined; |
| 78 | /** Authorization type. */ |
| 79 | authorizationType: string | undefined; |
| 80 | /** Organization ids (`organizations/{id}`). */ |
| 81 | orgs: string[]; |
| 82 | }, |
| 83 | never, |
| 84 | Providers |
| 85 | >; |
| 86 | |
| 87 | /** |
| 88 | * An Access Context Manager authorized-orgs descriptor. |
| 89 | * |
| 90 | * Authorized-orgs descriptors configure cross-organization authorization |
| 91 | * for device or credential-strength evaluation. They live under an |
| 92 | * {@link AccessPolicy}. The API has no labels or description field — |
| 93 | * Alchemy uses the generated resource id as identity, and `list` returns |
| 94 | * descriptors under Alchemy-owned access policies so `pnpm nuke:gcp` |
| 95 | * can find them. |
| 96 | * |
| 97 | * `policy`, `authorizedOrgsDescId`, `authorizationDirection`, |
| 98 | * `assetType`, and `authorizationType` are immutable. `orgs` updates in |
| 99 | * place. |
| 100 | * |
| 101 | * ### Creating an Authorized Orgs Descriptor |
| 102 | * **Example:** Trust devices from a partner org |
| 103 | * ```typescript |
| 104 | * const policy = yield* GCP.AccessContextManager.AccessPolicy("Corp", { |
| 105 | * scopes: ["projects/123456789"], |
| 106 | * }); |
| 107 | * const desc = yield* GCP.AccessContextManager.AccessPoliciesAuthorizedOrgsDesc( |
| 108 | * "Partner", |
| 109 | * { |
| 110 | * policy: policy.name, |
| 111 | * authorizationDirection: "AUTHORIZATION_DIRECTION_FROM", |
| 112 | * assetType: "ASSET_TYPE_DEVICE", |
| 113 | * authorizationType: "AUTHORIZATION_TYPE_TRUST", |
| 114 | * orgs: ["organizations/987654321"], |
| 115 | * }, |
| 116 | * ); |
| 117 | * ``` |
| 118 | * |
| 119 | * ### Updating an Authorized Orgs Descriptor |
| 120 | * **Example:** Change the trusted org list |
| 121 | * ```typescript |
| 122 | * const desc = yield* GCP.AccessContextManager.AccessPoliciesAuthorizedOrgsDesc( |
| 123 | * "Partner", |
| 124 | * { |
| 125 | * policy: policy.name, |
| 126 | * authorizationDirection: "AUTHORIZATION_DIRECTION_FROM", |
| 127 | * assetType: "ASSET_TYPE_DEVICE", |
| 128 | * authorizationType: "AUTHORIZATION_TYPE_TRUST", |
| 129 | * orgs: ["organizations/111", "organizations/222"], |
| 130 | * }, |
| 131 | * ); |
| 132 | * ``` |
| 133 | * |
| 134 | * @resource |
| 135 | * @category AccessContextManager |
| 136 | */ |
| 137 | export const AccessPoliciesAuthorizedOrgsDesc = Resource<AccessPoliciesAuthorizedOrgsDesc>( |
| 138 | "GCP.AccessContextManager.AccessPoliciesAuthorizedOrgsDesc", |
| 139 | ); |
| 140 | |
| 141 | export class AccessPoliciesAuthorizedOrgsDescNotResolved extends Data.TaggedError( |
| 142 | "GCP.AccessContextManager.AccessPoliciesAuthorizedOrgsDescNotResolved", |
| 143 | )<{ |
| 144 | resourceName: string; |
| 145 | }> {} |
| 146 | |
| 147 | const toAttrs = (desc: acm.AuthorizedOrgsDesc): AccessPoliciesAuthorizedOrgsDesc["Attributes"] => { |
| 148 | const name = desc.name ?? ""; |
| 149 | const parsed = parseName(name, "authorizedOrgsDescs"); |
| 150 | return { |
| 151 | name, |
| 152 | authorizedOrgsDescId: parsed.id, |
| 153 | policy: parsed.parent, |
| 154 | authorizationDirection: desc.authorizationDirection, |
| 155 | assetType: desc.assetType, |
| 156 | authorizationType: desc.authorizationType, |
| 157 | orgs: desc.orgs ?? [], |
| 158 | }; |
| 159 | }; |
| 160 | |
| 161 | const getByName = (name: string) => |
| 162 | acm |
| 163 | .getAccessPoliciesAuthorizedOrgsDescs({ name }) |
| 164 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 165 | |
| 166 | const listDescs = (policy: string) => |
| 167 | collectPages( |
| 168 | acm.listAccessPoliciesAuthorizedOrgsDescs.pages({ |
| 169 | parent: policyNameOf(policy), |
| 170 | pageSize: 100, |
| 171 | }), |
| 172 | (page) => page.authorizedOrgsDescs, |
| 173 | ).pipe(Effect.catchTag("NotFound", () => Effect.succeed([] as acm.AuthorizedOrgsDesc[]))); |
| 174 | |
| 175 | export const AccessPoliciesAuthorizedOrgsDescProvider = () => |
| 176 | Provider.succeed(AccessPoliciesAuthorizedOrgsDesc, { |
| 177 | stables: [ |
| 178 | "name", |
| 179 | "authorizedOrgsDescId", |
| 180 | "policy", |
| 181 | "authorizationDirection", |
| 182 | "assetType", |
| 183 | "authorizationType", |
| 184 | ], |
| 185 | |
| 186 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 187 | if (!isResolved(news)) return undefined; |
| 188 | const previousId = olds?.authorizedOrgsDescId ?? output?.authorizedOrgsDescId; |
| 189 | const idChanged = |
| 190 | previousId !== undefined && |
| 191 | news.authorizedOrgsDescId !== undefined && |
| 192 | news.authorizedOrgsDescId !== previousId; |
| 193 | const previousPolicy = olds?.policy ?? output?.policy; |
| 194 | const policyChanged = |
| 195 | previousPolicy !== undefined && policyNameOf(news.policy) !== policyNameOf(previousPolicy); |
| 196 | const directionChanged = |
| 197 | (olds?.authorizationDirection ?? output?.authorizationDirection) !== undefined && |
| 198 | news.authorizationDirection !== |
| 199 | (olds?.authorizationDirection ?? output?.authorizationDirection); |
| 200 | const assetChanged = |
| 201 | (olds?.assetType ?? output?.assetType) !== undefined && |
| 202 | news.assetType !== (olds?.assetType ?? output?.assetType); |
| 203 | const typeChanged = |
| 204 | (olds?.authorizationType ?? output?.authorizationType) !== undefined && |
| 205 | news.authorizationType !== (olds?.authorizationType ?? output?.authorizationType); |
| 206 | return replaceOnIdentity( |
| 207 | idChanged || policyChanged || directionChanged || assetChanged || typeChanged, |
| 208 | ); |
| 209 | }), |
| 210 | |
| 211 | read: Effect.fn(function* ({ id, olds, output }) { |
| 212 | const authorizedOrgsDescId = yield* toAcmId( |
| 213 | id, |
| 214 | olds?.authorizedOrgsDescId, |
| 215 | output?.authorizedOrgsDescId, |
| 216 | ); |
| 217 | const policy = olds?.policy ?? output?.policy; |
| 218 | if (policy === undefined) return undefined; |
| 219 | const name = |
| 220 | output?.name ?? resourceNameOf(policy, "authorizedOrgsDescs", authorizedOrgsDescId); |
| 221 | const existing = yield* getByName(name); |
| 222 | if (existing === undefined) return undefined; |
| 223 | return toAttrs(existing); |
| 224 | }), |
| 225 | |
| 226 | list: () => |
| 227 | Effect.gen(function* () { |
| 228 | const policies = yield* listOwnedPolicies(); |
| 229 | const descs = yield* Effect.forEach( |
| 230 | policies, |
| 231 | (policy) => |
| 232 | policy.name && hasOwnershipMarker(policy.title) |
| 233 | ? listDescs(policy.name) |
| 234 | : Effect.succeed([] as acm.AuthorizedOrgsDesc[]), |
| 235 | { concurrency: 4 }, |
| 236 | ); |
| 237 | return descs.flat().map(toAttrs); |
| 238 | }), |
| 239 | |
| 240 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 241 | const policy = policyNameOf(news.policy); |
| 242 | const authorizedOrgsDescId = yield* toAcmId( |
| 243 | id, |
| 244 | news.authorizedOrgsDescId, |
| 245 | output?.authorizedOrgsDescId, |
| 246 | ); |
| 247 | const name = resourceNameOf(policy, "authorizedOrgsDescs", authorizedOrgsDescId); |
| 248 | const desiredOrgs = news.orgs ?? []; |
| 249 | |
| 250 | let current = yield* getByName(output?.name ?? name); |
| 251 | |
| 252 | if (current === undefined) { |
| 253 | const created = yield* acm |
| 254 | .createAccessPoliciesAuthorizedOrgsDescs({ |
| 255 | parent: policy, |
| 256 | body: { |
| 257 | name, |
| 258 | authorizationDirection: news.authorizationDirection, |
| 259 | assetType: news.assetType, |
| 260 | authorizationType: news.authorizationType, |
| 261 | orgs: desiredOrgs.length > 0 ? desiredOrgs : undefined, |
| 262 | }, |
| 263 | }) |
| 264 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 265 | if (created !== undefined) { |
| 266 | // ALREADY_EXISTS (6): a concurrent create won the race. |
| 267 | yield* waitForOperation(created).pipe( |
| 268 | Effect.catchTag("GCP.OperationFailed", (error) => |
| 269 | error.code === 6 ? Effect.void : Effect.fail(error), |
| 270 | ), |
| 271 | ); |
| 272 | } |
| 273 | current = yield* waitUntilExists(getByName(name), name); |
| 274 | } |
| 275 | |
| 276 | if (current === undefined) { |
| 277 | return yield* new AccessPoliciesAuthorizedOrgsDescNotResolved({ |
| 278 | resourceName: name, |
| 279 | }); |
| 280 | } |
| 281 | |
| 282 | if (!sameStringList(current.orgs, desiredOrgs)) { |
| 283 | const operation = yield* acm.patchAccessPoliciesAuthorizedOrgsDescs({ |
| 284 | name: current.name ?? name, |
| 285 | updateMask: "orgs", |
| 286 | body: { |
| 287 | name: current.name ?? name, |
| 288 | orgs: desiredOrgs, |
| 289 | }, |
| 290 | }); |
| 291 | yield* waitForOperation(operation); |
| 292 | current = yield* waitUntilExists(getByName(current.name ?? name), current.name ?? name); |
| 293 | } |
| 294 | |
| 295 | if (current === undefined) { |
| 296 | return yield* new AccessPoliciesAuthorizedOrgsDescNotResolved({ |
| 297 | resourceName: name, |
| 298 | }); |
| 299 | } |
| 300 | |
| 301 | return toAttrs(current); |
| 302 | }), |
| 303 | |
| 304 | delete: Effect.fn(function* ({ output }) { |
| 305 | const operation = yield* acm |
| 306 | .deleteAccessPoliciesAuthorizedOrgsDescs({ name: output.name }) |
| 307 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 308 | if (operation !== undefined) { |
| 309 | // NOT_FOUND (5): already gone. |
| 310 | yield* waitForOperation(operation).pipe( |
| 311 | Effect.catchTag("GCP.OperationFailed", (error) => |
| 312 | error.code === 5 ? Effect.void : Effect.fail(error), |
| 313 | ), |
| 314 | ); |
| 315 | } |
| 316 | yield* waitUntilGone(getByName(output.name), output.name); |
| 317 | }), |
| 318 | }); |