The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as scc from "@distilled.cloud/gcp/securitycenter_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 Effect from "effect/Effect"; |
| 7 | import * as Stream from "effect/Stream"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import { createInternalLabels } from "../Labels.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | DEFAULT_CLOUD_PROVIDER, |
| 13 | DEFAULT_ENABLEMENT, |
| 14 | DEFAULT_ETD_TYPE, |
| 15 | defaultEtdConfig, |
| 16 | encodeDescription, |
| 17 | etdSettingsParent, |
| 18 | fingerprint, |
| 19 | folderIdOf, |
| 20 | folderParent, |
| 21 | hasOwnershipMarker, |
| 22 | lastSegment, |
| 23 | ownedByAlchemy, |
| 24 | parseName, |
| 25 | parseOwnership, |
| 26 | replaceOn, |
| 27 | resolveFolder, |
| 28 | sameText, |
| 29 | SecuritycenterNotResolved, |
| 30 | tryResolveFolder, |
| 31 | updateMaskOf, |
| 32 | } from "./internal.ts"; |
| 33 | |
| 34 | export type FolderEventThreatDetectionSettingsCustomModuleProps = { |
| 35 | /** |
| 36 | * Parent folder (`folders/{folder}` or the numeric id). Defaults to |
| 37 | * the project's Resource Manager folder ancestor. |
| 38 | * Immutable — changing it replaces the module. |
| 39 | */ |
| 40 | folder?: string; |
| 41 | /** |
| 42 | * Custom module type (for example `CONFIGURABLE_BAD_IP`). Immutable — |
| 43 | * changing it replaces the module. |
| 44 | * @default "CONFIGURABLE_BAD_IP" |
| 45 | */ |
| 46 | type?: string; |
| 47 | /** |
| 48 | * Module configuration matching `type`. |
| 49 | */ |
| 50 | config?: scc.DocumentMap; |
| 51 | /** |
| 52 | * Enablement state. |
| 53 | * @default "ENABLED" |
| 54 | */ |
| 55 | enablementState?: scc.EventThreatDetectionCustomModuleEnablementStateEnum | (string & {}); |
| 56 | /** |
| 57 | * Cloud provider this module applies to. |
| 58 | * @default "GOOGLE_CLOUD_PLATFORM" |
| 59 | */ |
| 60 | cloudProvider?: scc.EventThreatDetectionCustomModuleCloudProviderEnum | (string & {}); |
| 61 | /** |
| 62 | * Human-readable display name. |
| 63 | */ |
| 64 | displayName?: string; |
| 65 | /** |
| 66 | * Human-readable description. Event Threat Detection custom modules |
| 67 | * have no labels field, so Alchemy ownership is stored in a |
| 68 | * `[alchemy …]` prefix and stripped from attributes. |
| 69 | */ |
| 70 | description?: string; |
| 71 | }; |
| 72 | |
| 73 | export type FolderEventThreatDetectionSettingsCustomModule = Resource< |
| 74 | "GCP.SecurityCenter.FolderEventThreatDetectionSettingsCustomModule", |
| 75 | FolderEventThreatDetectionSettingsCustomModuleProps, |
| 76 | { |
| 77 | /** Full resource name `{folder}/eventThreatDetectionSettings/customModules/{module}`. */ |
| 78 | name: string; |
| 79 | /** Module id (last path segment; server assigned). */ |
| 80 | moduleId: string; |
| 81 | /** Folder resource name. */ |
| 82 | folder: string; |
| 83 | /** Folder id. */ |
| 84 | folderId: string; |
| 85 | /** Project id of the deploying stack. */ |
| 86 | project: string; |
| 87 | /** Custom module type. */ |
| 88 | type: string | undefined; |
| 89 | /** Module configuration. */ |
| 90 | config: scc.DocumentMap | undefined; |
| 91 | /** Enablement state. */ |
| 92 | enablementState: string | undefined; |
| 93 | /** Cloud provider. */ |
| 94 | cloudProvider: string | undefined; |
| 95 | /** Display name. */ |
| 96 | displayName: string | undefined; |
| 97 | /** User description with the Alchemy ownership prefix stripped. */ |
| 98 | description: string | undefined; |
| 99 | /** Ancestor module this one inherits from, if any. */ |
| 100 | ancestorModule: string | undefined; |
| 101 | /** Last editor of the module. */ |
| 102 | lastEditor: string | undefined; |
| 103 | /** RFC3339 last-update timestamp. */ |
| 104 | updateTime: string | undefined; |
| 105 | }, |
| 106 | never, |
| 107 | Providers |
| 108 | >; |
| 109 | |
| 110 | /** |
| 111 | * A folder-scoped Event Threat Detection custom module. |
| 112 | * |
| 113 | * Custom modules have no labels field — Alchemy stamps ownership into the |
| 114 | * description so `list` / nuke can find them. The module id is assigned by |
| 115 | * the API. Folder and type are identity. Config, enablement, display name, |
| 116 | * and description update in place. |
| 117 | * |
| 118 | * ### Creating a Custom Module |
| 119 | * **Example:** Flag a test IP |
| 120 | * ```typescript |
| 121 | * const module = yield* GCP.SecurityCenter.FolderEventThreatDetectionSettingsCustomModule( |
| 122 | * "BadIp", |
| 123 | * { |
| 124 | * type: "CONFIGURABLE_BAD_IP", |
| 125 | * config: { |
| 126 | * metadata: { severity: "LOW" }, |
| 127 | * ips: ["192.0.2.1"], |
| 128 | * }, |
| 129 | * description: "test bad ip", |
| 130 | * }, |
| 131 | * ); |
| 132 | * ``` |
| 133 | * |
| 134 | * @resource |
| 135 | * @category SecurityCenter |
| 136 | */ |
| 137 | export const FolderEventThreatDetectionSettingsCustomModule = |
| 138 | Resource<FolderEventThreatDetectionSettingsCustomModule>( |
| 139 | "GCP.SecurityCenter.FolderEventThreatDetectionSettingsCustomModule", |
| 140 | ); |
| 141 | |
| 142 | const toAttrs = (module: scc.EventThreatDetectionCustomModule, folder: string, project: string) => { |
| 143 | const name = module.name ?? ""; |
| 144 | const parsed = parseName(name, "customModules"); |
| 145 | const ownership = parseOwnership(module.description); |
| 146 | return { |
| 147 | name, |
| 148 | moduleId: parsed.id || lastSegment(name), |
| 149 | folder, |
| 150 | folderId: folderIdOf(folder), |
| 151 | project, |
| 152 | type: module.type, |
| 153 | config: module.config, |
| 154 | enablementState: module.enablementState, |
| 155 | cloudProvider: module.cloudProvider, |
| 156 | displayName: module.displayName, |
| 157 | description: ownership.text, |
| 158 | ancestorModule: module.ancestorModule, |
| 159 | lastEditor: module.lastEditor, |
| 160 | updateTime: module.updateTime, |
| 161 | }; |
| 162 | }; |
| 163 | |
| 164 | const getByName = (name: string) => |
| 165 | name.length === 0 |
| 166 | ? Effect.undefined |
| 167 | : scc |
| 168 | .getFoldersEventThreatDetectionSettingsCustomModules({ name }) |
| 169 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 170 | |
| 171 | const listAt = (parent: string) => |
| 172 | scc.listFoldersEventThreatDetectionSettingsCustomModules.pages({ parent, pageSize: 100 }).pipe( |
| 173 | Stream.flatMap((page) => Stream.fromIterable(page.eventThreatDetectionCustomModules ?? [])), |
| 174 | Stream.runCollect, |
| 175 | Effect.map((chunk) => Array.from(chunk)), |
| 176 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 177 | ); |
| 178 | |
| 179 | const findOwned = (parent: string, id: string) => |
| 180 | Effect.gen(function* () { |
| 181 | const modules = yield* listAt(parent); |
| 182 | for (const module of modules) { |
| 183 | if (yield* ownedByAlchemy(id, module.description)) return module; |
| 184 | } |
| 185 | return undefined; |
| 186 | }); |
| 187 | |
| 188 | export const FolderEventThreatDetectionSettingsCustomModuleProvider = () => |
| 189 | Provider.succeed(FolderEventThreatDetectionSettingsCustomModule, { |
| 190 | stables: ["name", "moduleId", "folder", "folderId", "project", "type"], |
| 191 | |
| 192 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 193 | if (!isResolved(news)) return undefined; |
| 194 | return ( |
| 195 | replaceOn( |
| 196 | olds?.folder ?? output?.folder, |
| 197 | news.folder !== undefined ? folderParent(news.folder) : undefined, |
| 198 | ) ?? replaceOn(olds?.type ?? output?.type, news.type) |
| 199 | ); |
| 200 | }), |
| 201 | |
| 202 | read: Effect.fn(function* ({ id, olds, output }) { |
| 203 | const env = yield* GcpEnvironment.current; |
| 204 | const folder = yield* resolveFolder(olds?.folder ?? output?.folder, output?.folder); |
| 205 | let existing = yield* getByName(output?.name ?? ""); |
| 206 | if (existing === undefined) { |
| 207 | existing = yield* findOwned(etdSettingsParent(folder), id); |
| 208 | } |
| 209 | if (existing === undefined) return undefined; |
| 210 | const attrs = toAttrs(existing, folder, env.project); |
| 211 | return (yield* ownedByAlchemy(id, existing.description)) ? attrs : Unowned(attrs); |
| 212 | }), |
| 213 | |
| 214 | list: () => |
| 215 | Effect.gen(function* () { |
| 216 | const env = yield* GcpEnvironment.current; |
| 217 | const folder = yield* tryResolveFolder(); |
| 218 | if (folder === undefined) return []; |
| 219 | const modules = yield* listAt(etdSettingsParent(folder)); |
| 220 | return modules |
| 221 | .filter((module) => hasOwnershipMarker(module.description)) |
| 222 | .map((module) => toAttrs(module, folder, env.project)); |
| 223 | }), |
| 224 | |
| 225 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 226 | const env = yield* GcpEnvironment.current; |
| 227 | const folder = yield* resolveFolder(news.folder, output?.folder); |
| 228 | const parent = etdSettingsParent(folder); |
| 229 | const ownership = yield* createInternalLabels(id); |
| 230 | const description = encodeDescription(ownership, news.description); |
| 231 | const type = news.type ?? DEFAULT_ETD_TYPE; |
| 232 | const config = news.config ?? defaultEtdConfig; |
| 233 | const enablementState = news.enablementState ?? DEFAULT_ENABLEMENT; |
| 234 | const cloudProvider = news.cloudProvider ?? DEFAULT_CLOUD_PROVIDER; |
| 235 | const displayName = news.displayName; |
| 236 | |
| 237 | let current = yield* getByName(output?.name ?? ""); |
| 238 | if (current === undefined) { |
| 239 | current = yield* findOwned(parent, id); |
| 240 | } |
| 241 | |
| 242 | if (current === undefined) { |
| 243 | const created = yield* scc |
| 244 | .createFoldersEventThreatDetectionSettingsCustomModules({ |
| 245 | parent, |
| 246 | body: { |
| 247 | type, |
| 248 | config, |
| 249 | enablementState, |
| 250 | cloudProvider, |
| 251 | displayName, |
| 252 | description, |
| 253 | }, |
| 254 | }) |
| 255 | .pipe(Effect.catchTag("Conflict", () => findOwned(parent, id))); |
| 256 | current = created ?? undefined; |
| 257 | } |
| 258 | |
| 259 | if (current === undefined) { |
| 260 | return yield* new SecuritycenterNotResolved({ |
| 261 | resourceName: `${parent}/customModules`, |
| 262 | }); |
| 263 | } |
| 264 | |
| 265 | const currentName = current.name ?? ""; |
| 266 | const updateMask = updateMaskOf( |
| 267 | fingerprint(current.config) !== fingerprint(config) ? "config" : undefined, |
| 268 | !sameText(current.enablementState, enablementState) ? "enablementState" : undefined, |
| 269 | !sameText(current.displayName, displayName) ? "displayName" : undefined, |
| 270 | !sameText(current.description, description) ? "description" : undefined, |
| 271 | ); |
| 272 | |
| 273 | if (updateMask.length > 0) { |
| 274 | current = yield* scc.patchFoldersEventThreatDetectionSettingsCustomModules({ |
| 275 | name: currentName, |
| 276 | updateMask, |
| 277 | body: { config, enablementState, displayName, description }, |
| 278 | }); |
| 279 | } |
| 280 | |
| 281 | return toAttrs(current, folder, env.project); |
| 282 | }), |
| 283 | |
| 284 | delete: Effect.fn(function* ({ output }) { |
| 285 | yield* scc |
| 286 | .deleteFoldersEventThreatDetectionSettingsCustomModules({ |
| 287 | name: output.name, |
| 288 | }) |
| 289 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 290 | }), |
| 291 | }); |