The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as networksecurity from "@distilled.cloud/gcp/networksecurity_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 { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import { GcpEnvironment } from "../Environment.ts"; |
| 12 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 13 | import type { Providers } from "../Providers.ts"; |
| 14 | import { |
| 15 | DEFAULT_LOCATION, |
| 16 | normalizeLocation, |
| 17 | parentOf, |
| 18 | parseResourceName, |
| 19 | resourceName, |
| 20 | toId, |
| 21 | userLabels, |
| 22 | } from "./names.ts"; |
| 23 | import { waitForOperation } from "./operations.ts"; |
| 24 | |
| 25 | const COLLECTION = "sacRealms"; |
| 26 | const DEFAULT_SECURITY_SERVICE = |
| 27 | "PALO_ALTO_PRISMA_ACCESS" satisfies networksecurity.SACRealmSecurityServiceEnum; |
| 28 | |
| 29 | export type SacRealmSecurityService = networksecurity.SACRealmSecurityServiceEnum | (string & {}); |
| 30 | |
| 31 | export type SacRealmPairingKey = { |
| 32 | /** Pairing key value shared with the SSE partner. */ |
| 33 | key: string | undefined; |
| 34 | /** RFC3339 expiry of the pairing key. */ |
| 35 | expireTime: string | undefined; |
| 36 | }; |
| 37 | |
| 38 | export type SacRealmProps = { |
| 39 | /** |
| 40 | * Realm id (the `{sacRealm}` segment of |
| 41 | * `projects/{project}/locations/global/sacRealms/{sacRealm}`). If |
| 42 | * omitted, a unique RFC1035 name is generated from the stack, stage, |
| 43 | * and logical id. Immutable — changing it replaces the realm. |
| 44 | */ |
| 45 | sacRealmId?: string; |
| 46 | /** |
| 47 | * Location of the realm. Realms are global — always `"global"`. |
| 48 | * Immutable — changing it replaces the realm. `GLOBAL` is accepted |
| 49 | * and normalized to `global`. |
| 50 | * @default "global" |
| 51 | */ |
| 52 | location?: string; |
| 53 | /** |
| 54 | * SSE service provider associated with the realm. Immutable — |
| 55 | * changing it replaces the realm. |
| 56 | * @default "PALO_ALTO_PRISMA_ACCESS" |
| 57 | */ |
| 58 | securityService?: SacRealmSecurityService; |
| 59 | /** |
| 60 | * User labels. Alchemy ownership labels are merged in automatically. |
| 61 | * The SAC realm API has no patch, so changing labels replaces the |
| 62 | * realm. |
| 63 | */ |
| 64 | labels?: Record<string, string>; |
| 65 | }; |
| 66 | |
| 67 | export type SacRealm = Resource< |
| 68 | "GCP.NetworkSecurity.SacRealm", |
| 69 | SacRealmProps, |
| 70 | { |
| 71 | /** Full resource name. */ |
| 72 | name: string; |
| 73 | /** Realm id (last path segment). */ |
| 74 | sacRealmId: string; |
| 75 | /** Project id. */ |
| 76 | project: string; |
| 77 | /** Location id. Always `"global"`. */ |
| 78 | location: string; |
| 79 | /** SSE service provider. */ |
| 80 | securityService: string | undefined; |
| 81 | /** User labels (Alchemy ownership labels stripped). */ |
| 82 | labels: Record<string, string>; |
| 83 | /** Server-reported lifecycle state. */ |
| 84 | state: string | undefined; |
| 85 | /** Pairing key used during partner handshake. */ |
| 86 | pairingKey: SacRealmPairingKey | undefined; |
| 87 | /** RFC3339 creation timestamp. */ |
| 88 | createTime: string | undefined; |
| 89 | /** RFC3339 last-update timestamp. */ |
| 90 | updateTime: string | undefined; |
| 91 | }, |
| 92 | never, |
| 93 | Providers |
| 94 | >; |
| 95 | |
| 96 | /** |
| 97 | * A Secure Access Connect (SAC) realm — the handshake between a Google |
| 98 | * Cloud project and an SSE partner such as Palo Alto Prisma Access. |
| 99 | * |
| 100 | * The create API has no patch; changing `sacRealmId`, `location`, |
| 101 | * `securityService`, or labels replaces the realm. |
| 102 | * |
| 103 | * ### Creating a Realm |
| 104 | * **Example:** Generated name |
| 105 | * ```typescript |
| 106 | * const realm = yield* GCP.NetworkSecurity.SacRealm("Prisma", {}); |
| 107 | * ``` |
| 108 | * |
| 109 | * **Example:** Named realm with labels |
| 110 | * ```typescript |
| 111 | * const realm = yield* GCP.NetworkSecurity.SacRealm("Prisma", { |
| 112 | * sacRealmId: "app-prisma", |
| 113 | * securityService: "PALO_ALTO_PRISMA_ACCESS", |
| 114 | * labels: { env: "prod" }, |
| 115 | * }); |
| 116 | * ``` |
| 117 | * |
| 118 | * @resource |
| 119 | * @category NetworkSecurity |
| 120 | */ |
| 121 | export const SacRealm = Resource<SacRealm>("GCP.NetworkSecurity.SacRealm"); |
| 122 | |
| 123 | export class SacRealmNotResolved extends Data.TaggedError( |
| 124 | "GCP.NetworkSecurity.SacRealmNotResolved", |
| 125 | )<{ |
| 126 | resourceName: string; |
| 127 | }> {} |
| 128 | |
| 129 | export class SacRealmFailed extends Data.TaggedError("GCP.NetworkSecurity.SacRealmFailed")<{ |
| 130 | resourceName: string; |
| 131 | state: string | undefined; |
| 132 | }> {} |
| 133 | |
| 134 | export class SacRealmStillExists extends Data.TaggedError( |
| 135 | "GCP.NetworkSecurity.SacRealmStillExists", |
| 136 | )<{ |
| 137 | resourceName: string; |
| 138 | }> {} |
| 139 | |
| 140 | const isPendingState = (state: string | undefined) => state === "STATE_UNSPECIFIED"; |
| 141 | |
| 142 | const toPairingKey = ( |
| 143 | key: networksecurity.SACRealmPairingKey | undefined, |
| 144 | ): SacRealmPairingKey | undefined => |
| 145 | key === undefined |
| 146 | ? undefined |
| 147 | : { |
| 148 | key: key.key, |
| 149 | expireTime: key.expireTime, |
| 150 | }; |
| 151 | |
| 152 | const toAttrs = (realm: networksecurity.SACRealm, project: string) => { |
| 153 | const name = realm.name ?? ""; |
| 154 | const parsed = parseResourceName(name, COLLECTION); |
| 155 | return { |
| 156 | name, |
| 157 | sacRealmId: parsed.id, |
| 158 | project: parsed.project || project, |
| 159 | location: parsed.location || DEFAULT_LOCATION, |
| 160 | securityService: realm.securityService, |
| 161 | labels: userLabels(realm.labels), |
| 162 | state: realm.state, |
| 163 | pairingKey: toPairingKey(realm.pairingKey), |
| 164 | createTime: realm.createTime, |
| 165 | updateTime: realm.updateTime, |
| 166 | }; |
| 167 | }; |
| 168 | |
| 169 | const getByName = (name: string) => |
| 170 | networksecurity |
| 171 | .getProjectsLocationsSacRealms({ name }) |
| 172 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 173 | |
| 174 | const waitUntilReady = (name: string) => |
| 175 | getByName(name).pipe( |
| 176 | Effect.filterOrFail( |
| 177 | (realm): realm is networksecurity.SACRealm => realm !== undefined, |
| 178 | () => new SacRealmNotResolved({ resourceName: name }), |
| 179 | ), |
| 180 | Effect.filterOrFail( |
| 181 | (realm) => realm.state !== "KEY_EXPIRED", |
| 182 | (realm) => new SacRealmFailed({ resourceName: name, state: realm.state }), |
| 183 | ), |
| 184 | Effect.filterOrFail( |
| 185 | (realm) => !isPendingState(realm.state), |
| 186 | () => new SacRealmNotResolved({ resourceName: name }), |
| 187 | ), |
| 188 | Effect.retry({ |
| 189 | while: (error) => error._tag === "GCP.NetworkSecurity.SacRealmNotResolved", |
| 190 | times: 10, |
| 191 | schedule: Schedule.spaced("2 seconds"), |
| 192 | }), |
| 193 | ); |
| 194 | |
| 195 | const waitUntilGone = (name: string) => |
| 196 | getByName(name).pipe( |
| 197 | Effect.flatMap((realm) => |
| 198 | realm === undefined |
| 199 | ? Effect.void |
| 200 | : Effect.fail(new SacRealmStillExists({ resourceName: name })), |
| 201 | ), |
| 202 | Effect.retry({ |
| 203 | while: (error) => error._tag === "GCP.NetworkSecurity.SacRealmStillExists", |
| 204 | times: 10, |
| 205 | schedule: Schedule.spaced("2 seconds"), |
| 206 | }), |
| 207 | ); |
| 208 | |
| 209 | const listOwned = (project: string) => |
| 210 | networksecurity.listProjectsLocationsSacRealms |
| 211 | .pages({ |
| 212 | parent: parentOf(project, DEFAULT_LOCATION), |
| 213 | pageSize: 1000, |
| 214 | }) |
| 215 | .pipe( |
| 216 | Stream.flatMap((page) => Stream.fromIterable(page.sacRealms ?? [])), |
| 217 | Stream.filter((realm) => |
| 218 | Object.keys(realm.labels ?? {}).some((key) => key.startsWith("alchemy-")), |
| 219 | ), |
| 220 | Stream.map((realm) => toAttrs(realm, project)), |
| 221 | Stream.runCollect, |
| 222 | Effect.map((chunk) => Array.from(chunk)), |
| 223 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 224 | ); |
| 225 | |
| 226 | export const SacRealmProvider = () => |
| 227 | Provider.succeed(SacRealm, { |
| 228 | stables: ["name", "sacRealmId", "project", "location", "createTime"], |
| 229 | |
| 230 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 231 | if (!isResolved(news)) return undefined; |
| 232 | const previousId = olds?.sacRealmId ?? output?.sacRealmId; |
| 233 | const nextId = news.sacRealmId ?? previousId; |
| 234 | const previousLocation = normalizeLocation(olds?.location ?? output?.location); |
| 235 | const nextLocation = normalizeLocation(news.location ?? olds?.location ?? output?.location); |
| 236 | const previousService = ( |
| 237 | olds?.securityService ?? |
| 238 | output?.securityService ?? |
| 239 | DEFAULT_SECURITY_SERVICE |
| 240 | ).toUpperCase(); |
| 241 | const nextService = (news.securityService ?? previousService).toUpperCase(); |
| 242 | const previousLabels = { |
| 243 | ...toLabels(olds?.labels), |
| 244 | }; |
| 245 | const nextLabels = { |
| 246 | ...toLabels(news.labels), |
| 247 | }; |
| 248 | const { upsert, removed } = diffLabels(previousLabels, nextLabels); |
| 249 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 250 | const replace = |
| 251 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 252 | previousLocation !== nextLocation || |
| 253 | previousService !== nextService || |
| 254 | labelsChanged; |
| 255 | if (!replace) return undefined; |
| 256 | return { action: "replace" as const }; |
| 257 | }), |
| 258 | |
| 259 | read: Effect.fn(function* ({ id, olds, output }) { |
| 260 | const env = yield* GcpEnvironment.current; |
| 261 | const sacRealmId = yield* toId(id, olds?.sacRealmId, output?.sacRealmId, "sacr"); |
| 262 | const location = normalizeLocation(olds?.location ?? output?.location); |
| 263 | const name = output?.name ?? resourceName(env.project, location, COLLECTION, sacRealmId); |
| 264 | const existing = yield* getByName(name); |
| 265 | if (existing === undefined) return undefined; |
| 266 | const attrs = toAttrs(existing, env.project); |
| 267 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 268 | }), |
| 269 | |
| 270 | list: () => |
| 271 | Effect.gen(function* () { |
| 272 | const env = yield* GcpEnvironment.current; |
| 273 | return yield* listOwned(env.project); |
| 274 | }), |
| 275 | |
| 276 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 277 | const env = yield* GcpEnvironment.current; |
| 278 | const sacRealmId = yield* toId(id, news.sacRealmId, output?.sacRealmId, "sacr"); |
| 279 | const location = normalizeLocation(news.location ?? output?.location); |
| 280 | const name = resourceName(env.project, location, COLLECTION, sacRealmId); |
| 281 | const securityService = news.securityService ?? DEFAULT_SECURITY_SERVICE; |
| 282 | const desiredLabels = { |
| 283 | ...toLabels(news.labels), |
| 284 | ...(yield* createInternalLabels(id)), |
| 285 | }; |
| 286 | |
| 287 | let current = yield* getByName(name); |
| 288 | |
| 289 | if (current === undefined) { |
| 290 | const created = yield* networksecurity |
| 291 | .createProjectsLocationsSacRealms({ |
| 292 | parent: parentOf(env.project, location), |
| 293 | sacRealmId, |
| 294 | body: { |
| 295 | securityService, |
| 296 | labels: desiredLabels, |
| 297 | }, |
| 298 | }) |
| 299 | .pipe( |
| 300 | Effect.retry({ |
| 301 | while: (error) => error._tag === "Conflict", |
| 302 | times: 5, |
| 303 | schedule: Schedule.spaced("2 seconds"), |
| 304 | }), |
| 305 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 306 | ); |
| 307 | if (created !== undefined) { |
| 308 | yield* waitForOperation(created); |
| 309 | } |
| 310 | current = yield* waitUntilReady(name); |
| 311 | } |
| 312 | |
| 313 | if (current === undefined) { |
| 314 | return yield* new SacRealmNotResolved({ resourceName: name }); |
| 315 | } |
| 316 | |
| 317 | return toAttrs(current, env.project); |
| 318 | }), |
| 319 | |
| 320 | delete: Effect.fn(function* ({ output }) { |
| 321 | const operation = yield* networksecurity |
| 322 | .deleteProjectsLocationsSacRealms({ name: output.name }) |
| 323 | .pipe( |
| 324 | Effect.retry({ |
| 325 | while: (error) => error._tag === "Conflict", |
| 326 | times: 8, |
| 327 | schedule: Schedule.spaced("2 seconds"), |
| 328 | }), |
| 329 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 330 | ); |
| 331 | if (operation !== undefined) { |
| 332 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 333 | } |
| 334 | yield* waitUntilGone(output.name); |
| 335 | }), |
| 336 | }); |