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 Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { GcpEnvironment } from "../Environment.ts"; |
| 9 | import { createInternalLabels } from "../Labels.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | collectPages, |
| 13 | encodeOwnership, |
| 14 | hasOwnershipMarker, |
| 15 | lastSegment, |
| 16 | ownedByAlchemy, |
| 17 | parseOwnership, |
| 18 | projectOf, |
| 19 | replaceOn, |
| 20 | sameText, |
| 21 | toResourceId, |
| 22 | updateMaskOf, |
| 23 | } from "./internal.ts"; |
| 24 | |
| 25 | export type MuteConfigType = "MUTE_CONFIG_TYPE_UNSPECIFIED" | "STATIC" | "DYNAMIC"; |
| 26 | |
| 27 | export type MuteConfigProps = { |
| 28 | /** |
| 29 | * Mute config id (the `{muteConfig}` segment of |
| 30 | * `projects/{project}/muteConfigs/{muteConfig}`). If omitted, a unique |
| 31 | * id is generated. Lowercase letters, digits, and hyphens; must start |
| 32 | * with a letter; max 63 characters. Immutable — changing it replaces |
| 33 | * the config. |
| 34 | */ |
| 35 | muteConfigId?: string; |
| 36 | /** |
| 37 | * Finding filter that selects which findings are muted. |
| 38 | */ |
| 39 | filter: string; |
| 40 | /** |
| 41 | * Human-readable description (max 1024 characters). Mute configs have |
| 42 | * no labels field, so Alchemy ownership is stored in a `[alchemy …]` |
| 43 | * prefix and stripped from attributes. |
| 44 | */ |
| 45 | description?: string; |
| 46 | /** |
| 47 | * Mute config type. `STATIC` mutes matching findings immediately. |
| 48 | * `DYNAMIC` re-evaluates until `expiryTime`. Immutable — changing it |
| 49 | * replaces the config. |
| 50 | * @default "STATIC" |
| 51 | */ |
| 52 | type?: MuteConfigType; |
| 53 | /** |
| 54 | * RFC3339 expiry for `DYNAMIC` mute configs. |
| 55 | */ |
| 56 | expiryTime?: string; |
| 57 | }; |
| 58 | |
| 59 | export type MuteConfig = Resource< |
| 60 | "GCP.SecurityCenter.MuteConfig", |
| 61 | MuteConfigProps, |
| 62 | { |
| 63 | /** Full resource name `projects/{project}/muteConfigs/{muteConfig}`. */ |
| 64 | name: string; |
| 65 | /** Mute config id (last path segment). */ |
| 66 | muteConfigId: string; |
| 67 | /** Project id. */ |
| 68 | project: string; |
| 69 | /** Finding filter. */ |
| 70 | filter: string | undefined; |
| 71 | /** User description with the Alchemy ownership prefix stripped. */ |
| 72 | description: string | undefined; |
| 73 | /** Mute config type. */ |
| 74 | type: string | undefined; |
| 75 | /** RFC3339 expiry, if set. */ |
| 76 | expiryTime: string | undefined; |
| 77 | /** RFC3339 creation timestamp. */ |
| 78 | createTime: string | undefined; |
| 79 | /** RFC3339 last-update timestamp. */ |
| 80 | updateTime: string | undefined; |
| 81 | /** Last editor email. */ |
| 82 | mostRecentEditor: string | undefined; |
| 83 | }, |
| 84 | never, |
| 85 | Providers |
| 86 | >; |
| 87 | |
| 88 | /** |
| 89 | * A project-scoped Security Command Center mute config that hides matching |
| 90 | * findings from the default view. |
| 91 | * |
| 92 | * Mute configs have no labels field — Alchemy stamps ownership into the |
| 93 | * description so `list` / nuke can find them. Id and type are identity. |
| 94 | * Filter, description, and expiry update in place. |
| 95 | * |
| 96 | * ### Creating a Mute Config |
| 97 | * **Example:** Mute low-severity findings |
| 98 | * ```typescript |
| 99 | * const mute = yield* GCP.SecurityCenter.MuteConfig("Low", { |
| 100 | * filter: 'severity="LOW"', |
| 101 | * description: "mute low severity", |
| 102 | * }); |
| 103 | * ``` |
| 104 | * |
| 105 | * ### Updating a Mute Config |
| 106 | * **Example:** Mute medium severity as well |
| 107 | * ```typescript |
| 108 | * const mute = yield* GCP.SecurityCenter.MuteConfig("Low", { |
| 109 | * filter: 'severity="LOW" OR severity="MEDIUM"', |
| 110 | * description: "mute low and medium", |
| 111 | * }); |
| 112 | * ``` |
| 113 | * |
| 114 | * @resource |
| 115 | * @category SecurityCenter |
| 116 | */ |
| 117 | export const MuteConfig = Resource<MuteConfig>("GCP.SecurityCenter.MuteConfig"); |
| 118 | |
| 119 | export class MuteConfigNotResolved extends Data.TaggedError( |
| 120 | "GCP.SecurityCenter.MuteConfigNotResolved", |
| 121 | )<{ |
| 122 | resourceName: string; |
| 123 | }> {} |
| 124 | |
| 125 | const resourceName = (project: string, muteConfigId: string) => |
| 126 | `projects/${project}/muteConfigs/${muteConfigId}`; |
| 127 | |
| 128 | const toAttrs = (config: scc.GoogleCloudSecuritycenterV1MuteConfig, project: string) => { |
| 129 | const name = config.name ?? ""; |
| 130 | const parsed = parseOwnership(config.description); |
| 131 | return { |
| 132 | name, |
| 133 | muteConfigId: lastSegment(name), |
| 134 | project: projectOf(name) || project, |
| 135 | filter: config.filter, |
| 136 | description: parsed.text, |
| 137 | type: config.type, |
| 138 | expiryTime: config.expiryTime, |
| 139 | createTime: config.createTime, |
| 140 | updateTime: config.updateTime, |
| 141 | mostRecentEditor: config.mostRecentEditor, |
| 142 | }; |
| 143 | }; |
| 144 | |
| 145 | const getByName = (name: string) => |
| 146 | name.length === 0 |
| 147 | ? Effect.undefined |
| 148 | : scc |
| 149 | .getProjectsMuteConfigs({ name }) |
| 150 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 151 | |
| 152 | export const MuteConfigProvider = () => |
| 153 | Provider.succeed(MuteConfig, { |
| 154 | stables: ["name", "muteConfigId", "project", "type", "createTime"], |
| 155 | |
| 156 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 157 | if (!isResolved(news)) return undefined; |
| 158 | return ( |
| 159 | replaceOn(olds?.muteConfigId ?? output?.muteConfigId, news.muteConfigId) ?? |
| 160 | replaceOn(olds?.type ?? output?.type ?? "STATIC", news.type ?? "STATIC") |
| 161 | ); |
| 162 | }), |
| 163 | |
| 164 | read: Effect.fn(function* ({ id, olds, output }) { |
| 165 | const env = yield* GcpEnvironment.current; |
| 166 | const muteConfigId = yield* toResourceId(id, olds?.muteConfigId, output?.muteConfigId, "m"); |
| 167 | const name = output?.name ?? resourceName(env.project, muteConfigId); |
| 168 | const existing = yield* getByName(name); |
| 169 | if (existing === undefined) return undefined; |
| 170 | const attrs = toAttrs(existing, env.project); |
| 171 | return (yield* ownedByAlchemy(id, existing.description)) ? attrs : Unowned(attrs); |
| 172 | }), |
| 173 | |
| 174 | list: () => |
| 175 | Effect.gen(function* () { |
| 176 | const env = yield* GcpEnvironment.current; |
| 177 | const items = yield* collectPages( |
| 178 | scc.listProjectsMuteConfigs.pages({ |
| 179 | parent: `projects/${env.project}`, |
| 180 | pageSize: 100, |
| 181 | }), |
| 182 | (page) => page.muteConfigs, |
| 183 | ).pipe( |
| 184 | Effect.catchTag("NotFound", () => |
| 185 | Effect.succeed([] as scc.GoogleCloudSecuritycenterV1MuteConfig[]), |
| 186 | ), |
| 187 | ); |
| 188 | return items |
| 189 | .filter((config) => hasOwnershipMarker(config.description)) |
| 190 | .map((config) => toAttrs(config, env.project)); |
| 191 | }), |
| 192 | |
| 193 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 194 | const env = yield* GcpEnvironment.current; |
| 195 | const muteConfigId = yield* toResourceId(id, news.muteConfigId, output?.muteConfigId, "m"); |
| 196 | const name = resourceName(env.project, muteConfigId); |
| 197 | const ownership = yield* createInternalLabels(id); |
| 198 | const description = encodeOwnership(ownership, news.description); |
| 199 | const type = news.type ?? "STATIC"; |
| 200 | const body: scc.GoogleCloudSecuritycenterV1MuteConfig = { |
| 201 | filter: news.filter, |
| 202 | description, |
| 203 | type, |
| 204 | expiryTime: news.expiryTime, |
| 205 | }; |
| 206 | |
| 207 | let current = yield* getByName(output?.name ?? name); |
| 208 | |
| 209 | if (current === undefined) { |
| 210 | const created = yield* scc |
| 211 | .createProjectsMuteConfigs({ |
| 212 | parent: `projects/${env.project}`, |
| 213 | muteConfigId, |
| 214 | body, |
| 215 | }) |
| 216 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 217 | current = created ?? undefined; |
| 218 | } |
| 219 | |
| 220 | if (current === undefined) { |
| 221 | return yield* new MuteConfigNotResolved({ resourceName: name }); |
| 222 | } |
| 223 | |
| 224 | const currentName = current.name ?? name; |
| 225 | const filterChanged = !sameText(current.filter, news.filter); |
| 226 | const descriptionChanged = !sameText(current.description, description); |
| 227 | const expiryChanged = !sameText(current.expiryTime, news.expiryTime); |
| 228 | const updateMask = updateMaskOf( |
| 229 | filterChanged ? "filter" : undefined, |
| 230 | descriptionChanged ? "description" : undefined, |
| 231 | expiryChanged ? "expiry_time" : undefined, |
| 232 | ); |
| 233 | |
| 234 | if (updateMask.length > 0) { |
| 235 | current = yield* scc.patchProjectsMuteConfigs({ |
| 236 | name: currentName, |
| 237 | updateMask, |
| 238 | body: { |
| 239 | ...body, |
| 240 | name: currentName, |
| 241 | }, |
| 242 | }); |
| 243 | } |
| 244 | |
| 245 | return toAttrs(current, env.project); |
| 246 | }), |
| 247 | |
| 248 | delete: Effect.fn(function* ({ output }) { |
| 249 | yield* scc |
| 250 | .deleteProjectsMuteConfigs({ name: output.name }) |
| 251 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 252 | }), |
| 253 | }); |