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 Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | DEFAULT_GLOBAL, |
| 14 | canonicalizeLink, |
| 15 | changedFields, |
| 16 | collectPages, |
| 17 | hasAlchemyLabelKeys, |
| 18 | normalizeLocation, |
| 19 | parentOf, |
| 20 | parseName, |
| 21 | rfc1035, |
| 22 | sameStringList, |
| 23 | toPhysicalId, |
| 24 | userLabels, |
| 25 | waitUntilGone, |
| 26 | waitUntilPresent, |
| 27 | NetworksecurityNotResolved, |
| 28 | } from "./internal.ts"; |
| 29 | |
| 30 | const COLLECTION = "dnsThreatDetectors"; |
| 31 | const DEFAULT_PROVIDER = "INFOBLOX"; |
| 32 | |
| 33 | export type DnsThreatDetectorProvider = |
| 34 | | networksecurity.DnsThreatDetectorProviderEnum |
| 35 | | (string & {}); |
| 36 | |
| 37 | export type DnsThreatDetectorProps = { |
| 38 | /** |
| 39 | * DnsThreatDetector id (the `{dnsThreatDetector}` segment of |
| 40 | * `projects/{project}/locations/global/dnsThreatDetectors/{dnsThreatDetector}`). |
| 41 | * If omitted, a unique name is generated from the stack, stage, and |
| 42 | * logical id. Immutable — changing it replaces the detector. A project |
| 43 | * may have only one DNS threat detector. |
| 44 | */ |
| 45 | dnsThreatDetectorId?: string; |
| 46 | /** |
| 47 | * Location. The only supported value is `global`. Immutable — |
| 48 | * changing it replaces the detector. |
| 49 | * @default "global" |
| 50 | */ |
| 51 | location?: string; |
| 52 | /** |
| 53 | * DNS threat analysis provider. Currently only `INFOBLOX` is |
| 54 | * supported. Immutable — changing it replaces the detector. |
| 55 | * @default "INFOBLOX" |
| 56 | */ |
| 57 | provider?: DnsThreatDetectorProvider; |
| 58 | /** |
| 59 | * VPC network resource names that this detector should not monitor, |
| 60 | * e.g. `projects/PROJECT_ID/global/networks/NETWORK_NAME`. |
| 61 | */ |
| 62 | excludedNetworks?: string[]; |
| 63 | /** |
| 64 | * User labels. Alchemy ownership labels are merged in automatically. |
| 65 | */ |
| 66 | labels?: Record<string, string>; |
| 67 | }; |
| 68 | |
| 69 | export type DnsThreatDetector = Resource< |
| 70 | "GCP.NetworkSecurity.DnsThreatDetector", |
| 71 | DnsThreatDetectorProps, |
| 72 | { |
| 73 | /** Full resource name `projects/{project}/locations/global/dnsThreatDetectors/{dnsThreatDetector}`. */ |
| 74 | name: string; |
| 75 | /** DnsThreatDetector id (last path segment). */ |
| 76 | dnsThreatDetectorId: string; |
| 77 | /** Project id. */ |
| 78 | project: string; |
| 79 | /** Location id — always `"global"`. */ |
| 80 | location: string; |
| 81 | /** Threat analysis provider (`INFOBLOX`). */ |
| 82 | provider: string | undefined; |
| 83 | /** VPC networks excluded from monitoring. */ |
| 84 | excludedNetworks: string[]; |
| 85 | /** User labels (Alchemy ownership labels stripped). */ |
| 86 | labels: Record<string, string>; |
| 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 DNS threat detector that sends VPC DNS query logs to Infoblox for |
| 98 | * analysis. By default every VPC in the project is monitored; pass |
| 99 | * `excludedNetworks` to skip specific networks. |
| 100 | * |
| 101 | * A project may have only one detector. Changing `dnsThreatDetectorId`, |
| 102 | * `location`, or `provider` replaces the resource. Labels and |
| 103 | * `excludedNetworks` update in place. |
| 104 | * |
| 105 | * ### Creating a DnsThreatDetector |
| 106 | * **Example:** Monitor every VPC |
| 107 | * ```typescript |
| 108 | * const detector = yield* GCP.NetworkSecurity.DnsThreatDetector("Armor", { |
| 109 | * provider: "INFOBLOX", |
| 110 | * }); |
| 111 | * ``` |
| 112 | * |
| 113 | * **Example:** Exclude a network |
| 114 | * ```typescript |
| 115 | * const detector = yield* GCP.NetworkSecurity.DnsThreatDetector("Armor", { |
| 116 | * provider: "INFOBLOX", |
| 117 | * excludedNetworks: [ |
| 118 | * `projects/${project}/global/networks/${vpc.networkName}`, |
| 119 | * ], |
| 120 | * labels: { env: "prod" }, |
| 121 | * }); |
| 122 | * ``` |
| 123 | * |
| 124 | * @resource |
| 125 | * @category NetworkSecurity |
| 126 | */ |
| 127 | export const DnsThreatDetector = Resource<DnsThreatDetector>( |
| 128 | "GCP.NetworkSecurity.DnsThreatDetector", |
| 129 | ); |
| 130 | |
| 131 | const resourceName = (project: string, location: string, dnsThreatDetectorId: string) => |
| 132 | `projects/${project}/locations/${location}/dnsThreatDetectors/${dnsThreatDetectorId}`; |
| 133 | |
| 134 | const desiredProvider = (news: DnsThreatDetectorProps) => |
| 135 | (news.provider ?? DEFAULT_PROVIDER).toUpperCase(); |
| 136 | |
| 137 | const toExcluded = (networks: readonly string[] | undefined) => |
| 138 | (networks ?? []).map((network) => canonicalizeLink(network)).filter(Boolean); |
| 139 | |
| 140 | const toAttrs = (detector: networksecurity.DnsThreatDetector, project: string) => { |
| 141 | const name = detector.name ?? ""; |
| 142 | const parsed = parseName(name, COLLECTION, DEFAULT_GLOBAL); |
| 143 | return { |
| 144 | name, |
| 145 | dnsThreatDetectorId: parsed.id, |
| 146 | project: parsed.project || project, |
| 147 | location: parsed.location || DEFAULT_GLOBAL, |
| 148 | provider: detector.provider, |
| 149 | excludedNetworks: toExcluded(detector.excludedNetworks), |
| 150 | labels: userLabels(detector.labels), |
| 151 | createTime: detector.createTime, |
| 152 | updateTime: detector.updateTime, |
| 153 | }; |
| 154 | }; |
| 155 | |
| 156 | const getByName = (name: string) => |
| 157 | networksecurity |
| 158 | .getProjectsLocationsDnsThreatDetectors({ name }) |
| 159 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 160 | |
| 161 | export const DnsThreatDetectorProvider = () => |
| 162 | Provider.succeed(DnsThreatDetector, { |
| 163 | stables: ["name", "dnsThreatDetectorId", "project", "location", "provider", "createTime"], |
| 164 | |
| 165 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 166 | if (!isResolved(news)) return undefined; |
| 167 | const previousId = olds?.dnsThreatDetectorId ?? output?.dnsThreatDetectorId; |
| 168 | const nextId = news.dnsThreatDetectorId |
| 169 | ? rfc1035(news.dnsThreatDetectorId, "dns-threat-detector") |
| 170 | : previousId; |
| 171 | const previousLocation = normalizeLocation( |
| 172 | olds?.location ?? output?.location, |
| 173 | DEFAULT_GLOBAL, |
| 174 | ); |
| 175 | const nextLocation = normalizeLocation( |
| 176 | news.location ?? olds?.location ?? output?.location, |
| 177 | DEFAULT_GLOBAL, |
| 178 | ); |
| 179 | const previousProvider = ( |
| 180 | olds?.provider ?? |
| 181 | output?.provider ?? |
| 182 | DEFAULT_PROVIDER |
| 183 | ).toUpperCase(); |
| 184 | const nextProvider = desiredProvider(news); |
| 185 | if ( |
| 186 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 187 | previousLocation !== nextLocation || |
| 188 | previousProvider !== nextProvider |
| 189 | ) { |
| 190 | return { action: "replace" as const }; |
| 191 | } |
| 192 | return undefined; |
| 193 | }), |
| 194 | |
| 195 | read: Effect.fn(function* ({ id, olds, output }) { |
| 196 | const env = yield* GcpEnvironment.current; |
| 197 | const dnsThreatDetectorId = yield* toPhysicalId( |
| 198 | id, |
| 199 | olds?.dnsThreatDetectorId, |
| 200 | output?.dnsThreatDetectorId, |
| 201 | "dns-threat-detector", |
| 202 | ); |
| 203 | const location = normalizeLocation(olds?.location ?? output?.location, DEFAULT_GLOBAL); |
| 204 | const name = output?.name ?? resourceName(env.project, location, dnsThreatDetectorId); |
| 205 | const existing = yield* getByName(name); |
| 206 | if (existing === undefined) return undefined; |
| 207 | const attrs = toAttrs(existing, env.project); |
| 208 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 209 | }), |
| 210 | |
| 211 | list: () => |
| 212 | Effect.gen(function* () { |
| 213 | const env = yield* GcpEnvironment.current; |
| 214 | const items = yield* collectPages( |
| 215 | networksecurity.listProjectsLocationsDnsThreatDetectors.pages({ |
| 216 | parent: parentOf(env.project, "-"), |
| 217 | pageSize: 1000, |
| 218 | }), |
| 219 | (page) => page.dnsThreatDetectors, |
| 220 | ); |
| 221 | return items |
| 222 | .filter((item) => hasAlchemyLabelKeys(item.labels)) |
| 223 | .map((item) => toAttrs(item, env.project)); |
| 224 | }), |
| 225 | |
| 226 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 227 | const env = yield* GcpEnvironment.current; |
| 228 | const dnsThreatDetectorId = yield* toPhysicalId( |
| 229 | id, |
| 230 | news.dnsThreatDetectorId, |
| 231 | output?.dnsThreatDetectorId, |
| 232 | "dns-threat-detector", |
| 233 | ); |
| 234 | const location = normalizeLocation(news.location ?? output?.location, DEFAULT_GLOBAL); |
| 235 | const name = resourceName(env.project, location, dnsThreatDetectorId); |
| 236 | const desiredLabels = { |
| 237 | ...toLabels(news.labels), |
| 238 | ...(yield* createInternalLabels(id)), |
| 239 | }; |
| 240 | const provider = desiredProvider(news); |
| 241 | const excludedNetworks = toExcluded(news.excludedNetworks); |
| 242 | |
| 243 | let current = yield* getByName(name); |
| 244 | |
| 245 | if (current === undefined) { |
| 246 | yield* networksecurity |
| 247 | .createProjectsLocationsDnsThreatDetectors({ |
| 248 | parent: parentOf(env.project, location), |
| 249 | dnsThreatDetectorId, |
| 250 | body: { |
| 251 | provider, |
| 252 | labels: desiredLabels, |
| 253 | excludedNetworks, |
| 254 | }, |
| 255 | }) |
| 256 | .pipe( |
| 257 | Effect.retry({ |
| 258 | while: (error) => error._tag === "Conflict", |
| 259 | times: 5, |
| 260 | schedule: Schedule.spaced("2 seconds"), |
| 261 | }), |
| 262 | Effect.catchTag("Conflict", () => Effect.void), |
| 263 | ); |
| 264 | current = yield* waitUntilPresent(getByName(name), name); |
| 265 | } |
| 266 | |
| 267 | if (current === undefined) { |
| 268 | return yield* new NetworksecurityNotResolved({ resourceName: name }); |
| 269 | } |
| 270 | |
| 271 | const observedLabels = tagRecord(current.labels); |
| 272 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 273 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 274 | const excludedChanged = !sameStringList(current.excludedNetworks, excludedNetworks); |
| 275 | const updateMask = changedFields([ |
| 276 | ["labels", labelsChanged], |
| 277 | ["excludedNetworks", excludedChanged], |
| 278 | ]); |
| 279 | |
| 280 | if (updateMask.length > 0) { |
| 281 | yield* networksecurity.patchProjectsLocationsDnsThreatDetectors({ |
| 282 | name: current.name ?? name, |
| 283 | updateMask: updateMask.join(","), |
| 284 | body: { |
| 285 | name: current.name ?? name, |
| 286 | labels: desiredLabels, |
| 287 | excludedNetworks, |
| 288 | }, |
| 289 | }); |
| 290 | current = yield* waitUntilPresent(getByName(current.name ?? name), current.name ?? name); |
| 291 | } |
| 292 | |
| 293 | return toAttrs(current, env.project); |
| 294 | }), |
| 295 | |
| 296 | delete: Effect.fn(function* ({ output }) { |
| 297 | yield* networksecurity.deleteProjectsLocationsDnsThreatDetectors({ name: output.name }).pipe( |
| 298 | Effect.retry({ |
| 299 | while: (error) => error._tag === "Conflict", |
| 300 | times: 8, |
| 301 | schedule: Schedule.spaced("2 seconds"), |
| 302 | }), |
| 303 | Effect.catchTag("NotFound", () => Effect.void), |
| 304 | ); |
| 305 | yield* waitUntilGone(getByName(output.name), output.name); |
| 306 | }), |
| 307 | }); |