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 * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import { GcpEnvironment } from "../Environment.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | normalizeLocation, |
| 14 | parentOf, |
| 15 | parseResourceName, |
| 16 | resourceName, |
| 17 | toId, |
| 18 | toResourcePath, |
| 19 | } from "./names.ts"; |
| 20 | import { waitForOperation } from "./operations.ts"; |
| 21 | import { |
| 22 | createInternalLabels, |
| 23 | encodeDescription, |
| 24 | hasAlchemyLabels, |
| 25 | hasOwnershipMarker, |
| 26 | parseDescription, |
| 27 | sameStringList, |
| 28 | } from "./ownership.ts"; |
| 29 | |
| 30 | const COLLECTION = "tlsInspectionPolicies"; |
| 31 | |
| 32 | export type TlsFeatureProfile = |
| 33 | | "PROFILE_COMPATIBLE" |
| 34 | | "PROFILE_MODERN" |
| 35 | | "PROFILE_RESTRICTED" |
| 36 | | "PROFILE_CUSTOM" |
| 37 | | (string & {}); |
| 38 | |
| 39 | export type MinTlsVersion = "TLS_1_0" | "TLS_1_1" | "TLS_1_2" | "TLS_1_3" | (string & {}); |
| 40 | |
| 41 | export type TlsInspectionPolicyProps = { |
| 42 | /** |
| 43 | * TlsInspectionPolicy id (the `{tlsInspectionPolicy}` segment of |
| 44 | * `projects/{project}/locations/{location}/tlsInspectionPolicies/{tlsInspectionPolicy}`). |
| 45 | * If omitted, a unique RFC1035 name is generated from the stack, stage, |
| 46 | * and logical id. Must match `^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$`. |
| 47 | * Immutable — changing it replaces the policy. |
| 48 | */ |
| 49 | tlsInspectionPolicyId?: string; |
| 50 | /** |
| 51 | * Location (`us-central1`, `us-east1`, …). Immutable — changing it |
| 52 | * replaces the policy. `US-CENTRAL1` is accepted and normalized to |
| 53 | * `us-central1`. |
| 54 | * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 55 | */ |
| 56 | location?: string; |
| 57 | /** |
| 58 | * CA pool used to issue interception certificates, as |
| 59 | * `projects/{project}/locations/{location}/caPools/{caPool}` or a bare |
| 60 | * pool id (combined with `location`). |
| 61 | */ |
| 62 | caPool: string; |
| 63 | /** |
| 64 | * Human-readable description. TlsInspectionPolicy has no labels field, |
| 65 | * so Alchemy ownership is stored in a `[alchemy …]` prefix and stripped |
| 66 | * from attributes. |
| 67 | */ |
| 68 | description?: string; |
| 69 | /** |
| 70 | * TLS feature profile. Defaults to `PROFILE_COMPATIBLE` (broadest |
| 71 | * client/server set). Secure Web Proxy does not yet honor this field. |
| 72 | */ |
| 73 | tlsFeatureProfile?: TlsFeatureProfile; |
| 74 | /** |
| 75 | * Minimum TLS version negotiated with clients and servers. Secure Web |
| 76 | * Proxy does not yet honor this field. |
| 77 | */ |
| 78 | minTlsVersion?: MinTlsVersion; |
| 79 | /** |
| 80 | * Custom TLS cipher suites. Valid only when `tlsFeatureProfile` is |
| 81 | * `PROFILE_CUSTOM`. Secure Web Proxy does not yet honor this field. |
| 82 | */ |
| 83 | customTlsFeatures?: string[]; |
| 84 | /** |
| 85 | * When true, do not trust the default public CA set — only CAs in |
| 86 | * `trustConfig` are accepted. Defaults to false. Secure Web Proxy does |
| 87 | * not yet honor this field. |
| 88 | * @default false |
| 89 | */ |
| 90 | excludePublicCaSet?: boolean; |
| 91 | /** |
| 92 | * Certificate Manager TrustConfig used when connecting to the TLS |
| 93 | * server (`projects/{project}/locations/{location}/trustConfigs/{trustConfig}`). |
| 94 | * Needed for private or self-signed server certificates. Secure Web |
| 95 | * Proxy does not yet honor this field. |
| 96 | */ |
| 97 | trustConfig?: string; |
| 98 | }; |
| 99 | |
| 100 | export type TlsInspectionPolicy = Resource< |
| 101 | "GCP.NetworkSecurity.TlsInspectionPolicy", |
| 102 | TlsInspectionPolicyProps, |
| 103 | { |
| 104 | /** Full resource name `projects/{project}/locations/{location}/tlsInspectionPolicies/{tlsInspectionPolicy}`. */ |
| 105 | name: string; |
| 106 | /** TlsInspectionPolicy id (last path segment). */ |
| 107 | tlsInspectionPolicyId: string; |
| 108 | /** Project id. */ |
| 109 | project: string; |
| 110 | /** Location id (`us-central1`, …). */ |
| 111 | location: string; |
| 112 | /** CA pool used to issue interception certificates. */ |
| 113 | caPool: string | undefined; |
| 114 | /** User description with the Alchemy ownership prefix stripped. */ |
| 115 | description: string | undefined; |
| 116 | /** TLS feature profile currently configured. */ |
| 117 | tlsFeatureProfile: string | undefined; |
| 118 | /** Minimum TLS version currently configured. */ |
| 119 | minTlsVersion: string | undefined; |
| 120 | /** Custom TLS cipher suites, if `tlsFeatureProfile` is `PROFILE_CUSTOM`. */ |
| 121 | customTlsFeatures: string[]; |
| 122 | /** Whether the default public CA set is excluded. */ |
| 123 | excludePublicCaSet: boolean; |
| 124 | /** TrustConfig used when connecting to TLS servers, if set. */ |
| 125 | trustConfig: string | undefined; |
| 126 | /** RFC3339 creation timestamp. */ |
| 127 | createTime: string | undefined; |
| 128 | /** RFC3339 last-update timestamp. */ |
| 129 | updateTime: string | undefined; |
| 130 | }, |
| 131 | never, |
| 132 | Providers |
| 133 | >; |
| 134 | |
| 135 | /** |
| 136 | * A Network Security TlsInspectionPolicy — CA pool and TLS settings used |
| 137 | * to intercept TLS for Secure Web Proxy and Cloud NGFW. |
| 138 | * |
| 139 | * TlsInspectionPolicy has no labels field, so Alchemy stamps ownership |
| 140 | * into the description for `list` / nuke. Changing `tlsInspectionPolicyId` |
| 141 | * or `location` replaces the policy. `caPool`, description, TLS feature |
| 142 | * profile, min version, custom features, `excludePublicCaSet`, and |
| 143 | * `trustConfig` update in place. |
| 144 | * |
| 145 | * ### Creating a TlsInspectionPolicy |
| 146 | * **Example:** Intercept with a CaPool |
| 147 | * ```typescript |
| 148 | * const pool = yield* GCP.PrivateCA.CaPool("Intercept", { |
| 149 | * location: "us-central1", |
| 150 | * tier: "DEVOPS", |
| 151 | * }); |
| 152 | * const policy = yield* GCP.NetworkSecurity.TlsInspectionPolicy("Inspect", { |
| 153 | * caPool: pool.name, |
| 154 | * }); |
| 155 | * ``` |
| 156 | * |
| 157 | * **Example:** Named policy with TLS constraints |
| 158 | * ```typescript |
| 159 | * const policy = yield* GCP.NetworkSecurity.TlsInspectionPolicy("Inspect", { |
| 160 | * tlsInspectionPolicyId: "app-inspect", |
| 161 | * location: "us-central1", |
| 162 | * caPool: pool.name, |
| 163 | * description: "prod intercept", |
| 164 | * excludePublicCaSet: true, |
| 165 | * minTlsVersion: "TLS_1_2", |
| 166 | * tlsFeatureProfile: "PROFILE_MODERN", |
| 167 | * }); |
| 168 | * ``` |
| 169 | * |
| 170 | * ### Updating a TlsInspectionPolicy |
| 171 | * **Example:** Tighten TLS |
| 172 | * ```typescript |
| 173 | * const policy = yield* GCP.NetworkSecurity.TlsInspectionPolicy("Inspect", { |
| 174 | * tlsInspectionPolicyId: "app-inspect", |
| 175 | * location: "us-central1", |
| 176 | * caPool: pool.name, |
| 177 | * description: "prod intercept v2", |
| 178 | * excludePublicCaSet: true, |
| 179 | * minTlsVersion: "TLS_1_2", |
| 180 | * }); |
| 181 | * ``` |
| 182 | * |
| 183 | * @resource |
| 184 | * @category NetworkSecurity |
| 185 | */ |
| 186 | export const TlsInspectionPolicy = Resource<TlsInspectionPolicy>( |
| 187 | "GCP.NetworkSecurity.TlsInspectionPolicy", |
| 188 | ); |
| 189 | |
| 190 | export class TlsInspectionPolicyNotResolved extends Data.TaggedError( |
| 191 | "GCP.NetworkSecurity.TlsInspectionPolicyNotResolved", |
| 192 | )<{ |
| 193 | resourceName: string; |
| 194 | }> {} |
| 195 | |
| 196 | export class TlsInspectionPolicyStillExists extends Data.TaggedError( |
| 197 | "GCP.NetworkSecurity.TlsInspectionPolicyStillExists", |
| 198 | )<{ |
| 199 | resourceName: string; |
| 200 | }> {} |
| 201 | |
| 202 | const caPoolName = (project: string, location: string, caPool: string) => { |
| 203 | const trimmed = toResourcePath(caPool); |
| 204 | if (trimmed.includes("/")) return trimmed; |
| 205 | return `projects/${project}/locations/${location}/caPools/${trimmed}`; |
| 206 | }; |
| 207 | |
| 208 | const trustConfigName = (project: string, location: string, trustConfig: string | undefined) => { |
| 209 | if (trustConfig === undefined || trustConfig.length === 0) return undefined; |
| 210 | const trimmed = toResourcePath(trustConfig); |
| 211 | if (trimmed.includes("/")) return trimmed; |
| 212 | return `projects/${project}/locations/${location}/trustConfigs/${trimmed}`; |
| 213 | }; |
| 214 | |
| 215 | const toAttrs = (policy: networksecurity.TlsInspectionPolicy, project: string, region: string) => { |
| 216 | const name = policy.name ?? ""; |
| 217 | const parsed = parseResourceName(name, COLLECTION); |
| 218 | const owned = parseDescription(policy.description); |
| 219 | return { |
| 220 | name, |
| 221 | tlsInspectionPolicyId: parsed.id, |
| 222 | project: parsed.project || project, |
| 223 | location: parsed.location || region, |
| 224 | caPool: policy.caPool, |
| 225 | description: owned.description, |
| 226 | tlsFeatureProfile: policy.tlsFeatureProfile, |
| 227 | minTlsVersion: policy.minTlsVersion, |
| 228 | customTlsFeatures: policy.customTlsFeatures ?? [], |
| 229 | excludePublicCaSet: policy.excludePublicCaSet === true, |
| 230 | trustConfig: policy.trustConfig, |
| 231 | createTime: policy.createTime, |
| 232 | updateTime: policy.updateTime, |
| 233 | }; |
| 234 | }; |
| 235 | |
| 236 | const getByName = (name: string) => |
| 237 | networksecurity |
| 238 | .getProjectsLocationsTlsInspectionPolicies({ name }) |
| 239 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 240 | |
| 241 | const waitUntilExists = (name: string) => |
| 242 | getByName(name).pipe( |
| 243 | Effect.flatMap((policy) => |
| 244 | policy |
| 245 | ? Effect.succeed(policy) |
| 246 | : Effect.fail(new TlsInspectionPolicyNotResolved({ resourceName: name })), |
| 247 | ), |
| 248 | Effect.retry({ |
| 249 | while: (error) => error._tag === "GCP.NetworkSecurity.TlsInspectionPolicyNotResolved", |
| 250 | times: 8, |
| 251 | schedule: Schedule.spaced("1 second"), |
| 252 | }), |
| 253 | ); |
| 254 | |
| 255 | const waitUntilGone = (name: string) => |
| 256 | getByName(name).pipe( |
| 257 | Effect.flatMap((policy) => |
| 258 | policy === undefined |
| 259 | ? Effect.void |
| 260 | : Effect.fail(new TlsInspectionPolicyStillExists({ resourceName: name })), |
| 261 | ), |
| 262 | Effect.retry({ |
| 263 | while: (error) => error._tag === "GCP.NetworkSecurity.TlsInspectionPolicyStillExists", |
| 264 | times: 10, |
| 265 | schedule: Schedule.spaced("2 seconds"), |
| 266 | }), |
| 267 | ); |
| 268 | |
| 269 | const listOwned = (project: string, region: string) => |
| 270 | networksecurity.listProjectsLocationsTlsInspectionPolicies |
| 271 | .pages({ |
| 272 | parent: parentOf(project, "-"), |
| 273 | pageSize: 1000, |
| 274 | }) |
| 275 | .pipe( |
| 276 | Stream.flatMap((page) => Stream.fromIterable(page.tlsInspectionPolicies ?? [])), |
| 277 | Stream.filter((policy) => hasOwnershipMarker(policy.description)), |
| 278 | Stream.map((policy) => toAttrs(policy, project, region)), |
| 279 | Stream.runCollect, |
| 280 | Effect.map((chunk) => Array.from(chunk)), |
| 281 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 282 | ); |
| 283 | |
| 284 | export const TlsInspectionPolicyProvider = () => |
| 285 | Provider.succeed(TlsInspectionPolicy, { |
| 286 | stables: ["name", "tlsInspectionPolicyId", "project", "location", "createTime"], |
| 287 | |
| 288 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 289 | if (!isResolved(news)) return undefined; |
| 290 | const env = yield* GcpEnvironment.current; |
| 291 | const previousId = olds?.tlsInspectionPolicyId ?? output?.tlsInspectionPolicyId; |
| 292 | const nextId = news.tlsInspectionPolicyId ?? previousId; |
| 293 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 294 | const nextLocation = normalizeLocation( |
| 295 | news.location ?? olds?.location ?? output?.location, |
| 296 | env.region, |
| 297 | ); |
| 298 | if ( |
| 299 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 300 | previousLocation !== nextLocation |
| 301 | ) { |
| 302 | return { action: "replace" as const }; |
| 303 | } |
| 304 | return undefined; |
| 305 | }), |
| 306 | |
| 307 | read: Effect.fn(function* ({ id, olds, output }) { |
| 308 | const env = yield* GcpEnvironment.current; |
| 309 | const tlsInspectionPolicyId = yield* toId( |
| 310 | id, |
| 311 | olds?.tlsInspectionPolicyId, |
| 312 | output?.tlsInspectionPolicyId, |
| 313 | "tlsinsp", |
| 314 | ); |
| 315 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 316 | const name = |
| 317 | output?.name ?? resourceName(env.project, location, COLLECTION, tlsInspectionPolicyId); |
| 318 | const existing = yield* getByName(name); |
| 319 | if (existing === undefined) return undefined; |
| 320 | const attrs = toAttrs(existing, env.project, env.region); |
| 321 | const { labels } = parseDescription(existing.description); |
| 322 | return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs); |
| 323 | }), |
| 324 | |
| 325 | list: () => |
| 326 | Effect.gen(function* () { |
| 327 | const env = yield* GcpEnvironment.current; |
| 328 | return yield* listOwned(env.project, env.region); |
| 329 | }), |
| 330 | |
| 331 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 332 | const env = yield* GcpEnvironment.current; |
| 333 | const tlsInspectionPolicyId = yield* toId( |
| 334 | id, |
| 335 | news.tlsInspectionPolicyId, |
| 336 | output?.tlsInspectionPolicyId, |
| 337 | "tlsinsp", |
| 338 | ); |
| 339 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 340 | const name = resourceName(env.project, location, COLLECTION, tlsInspectionPolicyId); |
| 341 | const ownership = yield* createInternalLabels(id); |
| 342 | const desiredDescription = encodeDescription(ownership, news.description); |
| 343 | const caPool = caPoolName(env.project, location, news.caPool); |
| 344 | const trustConfig = trustConfigName(env.project, location, news.trustConfig); |
| 345 | const excludePublicCaSet = news.excludePublicCaSet === true; |
| 346 | const customTlsFeatures = news.customTlsFeatures ?? []; |
| 347 | |
| 348 | let current = yield* getByName(output?.name ?? name); |
| 349 | |
| 350 | if (current === undefined) { |
| 351 | const created = yield* networksecurity |
| 352 | .createProjectsLocationsTlsInspectionPolicies({ |
| 353 | parent: parentOf(env.project, location), |
| 354 | tlsInspectionPolicyId, |
| 355 | body: { |
| 356 | description: desiredDescription, |
| 357 | caPool, |
| 358 | trustConfig, |
| 359 | excludePublicCaSet, |
| 360 | tlsFeatureProfile: news.tlsFeatureProfile, |
| 361 | minTlsVersion: news.minTlsVersion, |
| 362 | customTlsFeatures: customTlsFeatures.length > 0 ? customTlsFeatures : undefined, |
| 363 | }, |
| 364 | }) |
| 365 | .pipe( |
| 366 | Effect.retry({ |
| 367 | while: (error) => error._tag === "Conflict", |
| 368 | times: 5, |
| 369 | schedule: Schedule.spaced("2 seconds"), |
| 370 | }), |
| 371 | Effect.catchTag("Conflict", () => Effect.undefined), |
| 372 | ); |
| 373 | if (created !== undefined) { |
| 374 | yield* waitForOperation(created); |
| 375 | } |
| 376 | current = yield* waitUntilExists(name); |
| 377 | } |
| 378 | |
| 379 | if (current === undefined) { |
| 380 | return yield* new TlsInspectionPolicyNotResolved({ resourceName: name }); |
| 381 | } |
| 382 | |
| 383 | const descriptionChanged = (current.description ?? "") !== desiredDescription; |
| 384 | const caPoolChanged = (current.caPool ?? "") !== caPool; |
| 385 | const trustChanged = (current.trustConfig ?? "") !== (trustConfig ?? ""); |
| 386 | const excludeChanged = (current.excludePublicCaSet === true) !== excludePublicCaSet; |
| 387 | const profileChanged = (current.tlsFeatureProfile ?? "") !== (news.tlsFeatureProfile ?? ""); |
| 388 | const minTlsChanged = (current.minTlsVersion ?? "") !== (news.minTlsVersion ?? ""); |
| 389 | const featuresChanged = |
| 390 | !sameStringList( |
| 391 | current.customTlsFeatures, |
| 392 | customTlsFeatures.length > 0 ? customTlsFeatures : current.customTlsFeatures, |
| 393 | ) && news.customTlsFeatures !== undefined; |
| 394 | |
| 395 | if ( |
| 396 | descriptionChanged || |
| 397 | caPoolChanged || |
| 398 | trustChanged || |
| 399 | excludeChanged || |
| 400 | profileChanged || |
| 401 | minTlsChanged || |
| 402 | featuresChanged |
| 403 | ) { |
| 404 | const updateMask = [ |
| 405 | descriptionChanged ? "description" : undefined, |
| 406 | caPoolChanged ? "caPool" : undefined, |
| 407 | trustChanged ? "trustConfig" : undefined, |
| 408 | excludeChanged ? "excludePublicCaSet" : undefined, |
| 409 | profileChanged ? "tlsFeatureProfile" : undefined, |
| 410 | minTlsChanged ? "minTlsVersion" : undefined, |
| 411 | featuresChanged ? "customTlsFeatures" : undefined, |
| 412 | ].filter((field): field is string => field !== undefined); |
| 413 | const operation = yield* networksecurity.patchProjectsLocationsTlsInspectionPolicies({ |
| 414 | name: current.name ?? name, |
| 415 | updateMask: updateMask.join(","), |
| 416 | body: { |
| 417 | name: current.name ?? name, |
| 418 | description: desiredDescription, |
| 419 | caPool, |
| 420 | trustConfig, |
| 421 | excludePublicCaSet, |
| 422 | tlsFeatureProfile: news.tlsFeatureProfile, |
| 423 | minTlsVersion: news.minTlsVersion, |
| 424 | customTlsFeatures: |
| 425 | news.customTlsFeatures !== undefined ? customTlsFeatures : current.customTlsFeatures, |
| 426 | }, |
| 427 | }); |
| 428 | yield* waitForOperation(operation); |
| 429 | current = yield* waitUntilExists(current.name ?? name); |
| 430 | } |
| 431 | |
| 432 | return toAttrs(current, env.project, env.region); |
| 433 | }), |
| 434 | |
| 435 | delete: Effect.fn(function* ({ output }) { |
| 436 | const operation = yield* networksecurity |
| 437 | .deleteProjectsLocationsTlsInspectionPolicies({ |
| 438 | name: output.name, |
| 439 | force: true, |
| 440 | }) |
| 441 | .pipe( |
| 442 | Effect.retry({ |
| 443 | while: (error) => error._tag === "Conflict", |
| 444 | times: 8, |
| 445 | schedule: Schedule.spaced("2 seconds"), |
| 446 | }), |
| 447 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 448 | ); |
| 449 | if (operation !== undefined) { |
| 450 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 451 | } |
| 452 | yield* waitUntilGone(output.name); |
| 453 | }), |
| 454 | }); |