The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as scc from "@distilled.cloud/gcp/securitycenter_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | collectPages, |
| 14 | encodeOwnership, |
| 15 | findOwned, |
| 16 | hasOwnershipMarker, |
| 17 | jsonEqual, |
| 18 | lastSegment, |
| 19 | organizationIdOf, |
| 20 | organizationParent, |
| 21 | ownedByAlchemy, |
| 22 | parseOwnership, |
| 23 | replaceOn, |
| 24 | resolveOrganization, |
| 25 | sameText, |
| 26 | shaDisplayNameOf, |
| 27 | shaSettingsParent, |
| 28 | tryResolveOrganization, |
| 29 | updateMaskOf, |
| 30 | } from "./internal.ts"; |
| 31 | import type { |
| 32 | CustomConfig, |
| 33 | SecurityHealthAnalyticsCloudProvider, |
| 34 | SecurityHealthAnalyticsEnablementState, |
| 35 | SecurityHealthAnalyticsSeverity, |
| 36 | } from "./SecurityHealthAnalyticsSettingsCustomModule.ts"; |
| 37 | |
| 38 | export type OrganizationsSecurityHealthAnalyticsSettingsCustomModuleProps = { |
| 39 | /** |
| 40 | * Module id (the `{customModule}` segment of |
| 41 | * `organizations/{organization}/securityHealthAnalyticsSettings/customModules/{customModule}`). |
| 42 | * Server-assigned on create. Supply it to target an existing module. |
| 43 | * Immutable — changing it replaces the module. |
| 44 | */ |
| 45 | moduleId?: string; |
| 46 | /** |
| 47 | * Parent organization (`organizations/{organization}` or the numeric id). |
| 48 | * Defaults to the project's Resource Manager |
| 49 | * ancestor. Immutable — changing it replaces the module. |
| 50 | */ |
| 51 | organization?: string; |
| 52 | /** |
| 53 | * Display name. Becomes the finding category. Must start with an |
| 54 | * uppercase letter, contain only letters, digits, or underscores, and |
| 55 | * be at most 128 characters. Immutable — changing it replaces the |
| 56 | * module. |
| 57 | */ |
| 58 | displayName?: string; |
| 59 | /** |
| 60 | * Enablement state at this parent. |
| 61 | * @default "ENABLED" |
| 62 | */ |
| 63 | enablementState?: SecurityHealthAnalyticsEnablementState; |
| 64 | /** |
| 65 | * Detection configuration. Required on create. |
| 66 | */ |
| 67 | customConfig: CustomConfig; |
| 68 | /** |
| 69 | * Cloud provider this module applies to. |
| 70 | */ |
| 71 | cloudProvider?: SecurityHealthAnalyticsCloudProvider; |
| 72 | }; |
| 73 | |
| 74 | export type OrganizationsSecurityHealthAnalyticsSettingsCustomModule = Resource< |
| 75 | "GCP.SecurityCenter.OrganizationsSecurityHealthAnalyticsSettingsCustomModule", |
| 76 | OrganizationsSecurityHealthAnalyticsSettingsCustomModuleProps, |
| 77 | { |
| 78 | /** Full resource name `organizations/{organization}/securityHealthAnalyticsSettings/customModules/{customModule}`. */ |
| 79 | name: string; |
| 80 | /** Module id (last path segment). */ |
| 81 | moduleId: string; |
| 82 | /** Organization resource name. */ |
| 83 | organization: string; |
| 84 | /** Organization id. */ |
| 85 | organizationId: string; |
| 86 | /** Project id of the deploying stack. */ |
| 87 | project: string; |
| 88 | /** Display name. */ |
| 89 | displayName: string | undefined; |
| 90 | /** Enablement state. */ |
| 91 | enablementState: string | undefined; |
| 92 | /** Detection configuration with the Alchemy ownership prefix stripped. */ |
| 93 | customConfig: CustomConfig | undefined; |
| 94 | /** Cloud provider. */ |
| 95 | cloudProvider: string | undefined; |
| 96 | /** Ancestor module this one inherits from, if any. */ |
| 97 | ancestorModule: string | undefined; |
| 98 | /** RFC3339 last-update timestamp. */ |
| 99 | updateTime: string | undefined; |
| 100 | /** Last editor email. */ |
| 101 | lastEditor: string | undefined; |
| 102 | }, |
| 103 | never, |
| 104 | Providers |
| 105 | >; |
| 106 | |
| 107 | /** |
| 108 | * An organization-scoped Security Health Analytics custom module. |
| 109 | * |
| 110 | * Custom modules have no labels field — Alchemy stamps ownership into |
| 111 | * `customConfig.description` so `list` / nuke can find them. Module id, |
| 112 | * organization, and display name are identity (the id is server-assigned). |
| 113 | * Enablement and custom config update in place. |
| 114 | * |
| 115 | * ### Creating a Custom Module |
| 116 | * **Example:** Always-true compute instance check |
| 117 | * ```typescript |
| 118 | * const module = yield* GCP.SecurityCenter.OrganizationsSecurityHealthAnalyticsSettingsCustomModule( |
| 119 | * "AlwaysTrue", |
| 120 | * { |
| 121 | * displayName: "AlchemyAlwaysTrue", |
| 122 | * customConfig: { |
| 123 | * predicate: { expression: 'resource.name == "alchemy-nonexistent"' }, |
| 124 | * resourceSelector: { |
| 125 | * resourceTypes: ["compute.googleapis.com/Instance"], |
| 126 | * }, |
| 127 | * severity: "LOW", |
| 128 | * description: "always true", |
| 129 | * recommendation: "n/a", |
| 130 | * }, |
| 131 | * }, |
| 132 | * ); |
| 133 | * ``` |
| 134 | * |
| 135 | * **Example:** Named module on an explicit organization |
| 136 | * ```typescript |
| 137 | * const module = yield* GCP.SecurityCenter.OrganizationsSecurityHealthAnalyticsSettingsCustomModule( |
| 138 | * "AlwaysTrue", |
| 139 | * { |
| 140 | * organization: "organizations/123456789", |
| 141 | * displayName: "AlchemyAlwaysTrue", |
| 142 | * customConfig: { |
| 143 | * predicate: { expression: 'resource.name == "alchemy-nonexistent"' }, |
| 144 | * resourceSelector: { |
| 145 | * resourceTypes: ["compute.googleapis.com/Instance"], |
| 146 | * }, |
| 147 | * severity: "LOW", |
| 148 | * }, |
| 149 | * }, |
| 150 | * ); |
| 151 | * ``` |
| 152 | * |
| 153 | * @resource |
| 154 | * @category SecurityCenter |
| 155 | */ |
| 156 | export const OrganizationsSecurityHealthAnalyticsSettingsCustomModule = |
| 157 | Resource<OrganizationsSecurityHealthAnalyticsSettingsCustomModule>( |
| 158 | "GCP.SecurityCenter.OrganizationsSecurityHealthAnalyticsSettingsCustomModule", |
| 159 | ); |
| 160 | |
| 161 | export class OrganizationsSecurityHealthAnalyticsSettingsCustomModuleNotResolved extends Data.TaggedError( |
| 162 | "GCP.SecurityCenter.OrganizationsSecurityHealthAnalyticsSettingsCustomModuleNotResolved", |
| 163 | )<{ |
| 164 | resourceName: string; |
| 165 | }> {} |
| 166 | |
| 167 | const resourceName = (organization: string, moduleId: string) => |
| 168 | `${organization}/securityHealthAnalyticsSettings/customModules/${moduleId}`; |
| 169 | |
| 170 | const customConfigOf = ( |
| 171 | config: scc.GoogleCloudSecuritycenterV1CustomConfig | undefined, |
| 172 | ): CustomConfig | undefined => { |
| 173 | if (config === undefined) return undefined; |
| 174 | return { |
| 175 | predicate: config.predicate, |
| 176 | resourceSelector: config.resourceSelector, |
| 177 | severity: config.severity as SecurityHealthAnalyticsSeverity | undefined, |
| 178 | description: parseOwnership(config.description).text, |
| 179 | recommendation: config.recommendation, |
| 180 | customOutput: config.customOutput, |
| 181 | }; |
| 182 | }; |
| 183 | |
| 184 | const desiredCustomConfig = ( |
| 185 | config: CustomConfig, |
| 186 | ownership: Record<string, string>, |
| 187 | ): scc.GoogleCloudSecuritycenterV1CustomConfig => ({ |
| 188 | ...config, |
| 189 | description: encodeOwnership(ownership, config.description), |
| 190 | }); |
| 191 | |
| 192 | const toAttrs = ( |
| 193 | module: scc.GoogleCloudSecuritycenterV1SecurityHealthAnalyticsCustomModule, |
| 194 | organization: string, |
| 195 | project: string, |
| 196 | ) => { |
| 197 | const name = module.name ?? ""; |
| 198 | return { |
| 199 | name, |
| 200 | moduleId: lastSegment(name), |
| 201 | organization, |
| 202 | organizationId: organizationIdOf(organization), |
| 203 | project, |
| 204 | displayName: module.displayName, |
| 205 | enablementState: module.enablementState, |
| 206 | customConfig: customConfigOf(module.customConfig), |
| 207 | cloudProvider: module.cloudProvider, |
| 208 | ancestorModule: module.ancestorModule, |
| 209 | updateTime: module.updateTime, |
| 210 | lastEditor: module.lastEditor, |
| 211 | }; |
| 212 | }; |
| 213 | |
| 214 | const getByName = (name: string) => |
| 215 | name.length === 0 |
| 216 | ? Effect.undefined |
| 217 | : scc |
| 218 | .getOrganizationsSecurityHealthAnalyticsSettingsCustomModules({ |
| 219 | name, |
| 220 | }) |
| 221 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 222 | |
| 223 | const listModules = (organization: string) => |
| 224 | collectPages( |
| 225 | scc.listOrganizationsSecurityHealthAnalyticsSettingsCustomModules.pages({ |
| 226 | parent: shaSettingsParent(organization), |
| 227 | pageSize: 100, |
| 228 | }), |
| 229 | (page) => page.securityHealthAnalyticsCustomModules, |
| 230 | ).pipe( |
| 231 | Effect.catchTag("NotFound", () => |
| 232 | Effect.succeed([] as scc.GoogleCloudSecuritycenterV1SecurityHealthAnalyticsCustomModule[]), |
| 233 | ), |
| 234 | ); |
| 235 | |
| 236 | const observe = (organization: string, id: string, name: string) => |
| 237 | Effect.gen(function* () { |
| 238 | const existing = yield* getByName(name); |
| 239 | if (existing !== undefined) return existing; |
| 240 | const items = yield* listModules(organization); |
| 241 | return yield* findOwned(items, (item) => item.customConfig?.description, id); |
| 242 | }); |
| 243 | |
| 244 | export const OrganizationsSecurityHealthAnalyticsSettingsCustomModuleProvider = () => |
| 245 | Provider.succeed(OrganizationsSecurityHealthAnalyticsSettingsCustomModule, { |
| 246 | stables: [ |
| 247 | "name", |
| 248 | "moduleId", |
| 249 | "organization", |
| 250 | "organizationId", |
| 251 | "project", |
| 252 | "displayName", |
| 253 | "ancestorModule", |
| 254 | ], |
| 255 | |
| 256 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 257 | if (!isResolved(news)) return undefined; |
| 258 | return ( |
| 259 | replaceOn(olds?.moduleId ?? output?.moduleId, news.moduleId) ?? |
| 260 | replaceOn( |
| 261 | olds?.organization ?? output?.organization, |
| 262 | news.organization === undefined ? undefined : organizationParent(news.organization), |
| 263 | ) ?? |
| 264 | replaceOn(olds?.displayName ?? output?.displayName, news.displayName) |
| 265 | ); |
| 266 | }), |
| 267 | |
| 268 | read: Effect.fn(function* ({ id, olds, output }) { |
| 269 | const env = yield* GcpEnvironment.current; |
| 270 | const organization = yield* resolveOrganization( |
| 271 | olds?.organization ?? output?.organization, |
| 272 | output?.organization, |
| 273 | ); |
| 274 | const moduleId = olds?.moduleId ?? output?.moduleId; |
| 275 | const name = output?.name ?? (moduleId ? resourceName(organization, moduleId) : ""); |
| 276 | const existing = yield* observe(organization, id, name); |
| 277 | if (existing === undefined) return undefined; |
| 278 | const attrs = toAttrs(existing, organization, env.project); |
| 279 | return (yield* ownedByAlchemy(id, existing.customConfig?.description)) |
| 280 | ? attrs |
| 281 | : Unowned(attrs); |
| 282 | }), |
| 283 | |
| 284 | list: () => |
| 285 | Effect.gen(function* () { |
| 286 | const env = yield* GcpEnvironment.current; |
| 287 | const organization = yield* tryResolveOrganization(); |
| 288 | if (organization === undefined) return []; |
| 289 | const items = yield* listModules(organization); |
| 290 | return items |
| 291 | .filter((module) => hasOwnershipMarker(module.customConfig?.description)) |
| 292 | .map((module) => toAttrs(module, organization, env.project)); |
| 293 | }), |
| 294 | |
| 295 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 296 | const env = yield* GcpEnvironment.current; |
| 297 | const organization = yield* resolveOrganization(news.organization, output?.organization); |
| 298 | const parent = shaSettingsParent(organization); |
| 299 | const moduleId = news.moduleId ?? output?.moduleId; |
| 300 | const name = output?.name ?? (moduleId ? resourceName(organization, moduleId) : ""); |
| 301 | const ownership = yield* createInternalLabels(id); |
| 302 | const customConfig = desiredCustomConfig(news.customConfig, ownership); |
| 303 | const enablementState = news.enablementState ?? "ENABLED"; |
| 304 | const displayName = |
| 305 | news.displayName ?? |
| 306 | shaDisplayNameOf( |
| 307 | yield* createPhysicalName({ |
| 308 | id, |
| 309 | maxLength: 63, |
| 310 | lowercase: true, |
| 311 | }), |
| 312 | ); |
| 313 | const body: scc.GoogleCloudSecuritycenterV1SecurityHealthAnalyticsCustomModule = { |
| 314 | displayName, |
| 315 | enablementState, |
| 316 | customConfig, |
| 317 | cloudProvider: news.cloudProvider, |
| 318 | }; |
| 319 | |
| 320 | let current = yield* observe(organization, id, name); |
| 321 | |
| 322 | if (current === undefined) { |
| 323 | current = yield* scc.createOrganizationsSecurityHealthAnalyticsSettingsCustomModules({ |
| 324 | parent, |
| 325 | body, |
| 326 | }); |
| 327 | } |
| 328 | |
| 329 | if (current === undefined) { |
| 330 | return yield* new OrganizationsSecurityHealthAnalyticsSettingsCustomModuleNotResolved({ |
| 331 | resourceName: name || parent, |
| 332 | }); |
| 333 | } |
| 334 | |
| 335 | const currentName = current.name ?? name; |
| 336 | const enablementChanged = !sameText(current.enablementState, enablementState); |
| 337 | const configChanged = !jsonEqual(current.customConfig, customConfig); |
| 338 | const updateMask = updateMaskOf( |
| 339 | enablementChanged ? "enablement_state" : undefined, |
| 340 | configChanged ? "custom_config" : undefined, |
| 341 | ); |
| 342 | |
| 343 | if (updateMask.length > 0) { |
| 344 | current = yield* scc.patchOrganizationsSecurityHealthAnalyticsSettingsCustomModules({ |
| 345 | name: currentName, |
| 346 | updateMask, |
| 347 | body: { |
| 348 | enablementState, |
| 349 | customConfig, |
| 350 | }, |
| 351 | }); |
| 352 | } |
| 353 | |
| 354 | return toAttrs(current, organization, env.project); |
| 355 | }), |
| 356 | |
| 357 | delete: Effect.fn(function* ({ output }) { |
| 358 | yield* scc |
| 359 | .deleteOrganizationsSecurityHealthAnalyticsSettingsCustomModules({ |
| 360 | name: output.name, |
| 361 | }) |
| 362 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 363 | }), |
| 364 | }); |