The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as securityhub from "@distilled.cloud/aws/securityhub"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 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 { AWSEnvironment } from "../Environment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | |
| 11 | export interface ActionTargetProps { |
| 12 | /** |
| 13 | * Display name of the custom action (shown in the Security Hub console's |
| 14 | * action menu, max 20 characters). Updatable in place. |
| 15 | */ |
| 16 | name: string; |
| 17 | |
| 18 | /** |
| 19 | * Description of the custom action. Updatable in place. |
| 20 | */ |
| 21 | description: string; |
| 22 | |
| 23 | /** |
| 24 | * ID of the custom action (alphanumeric, max 20 characters). It becomes the |
| 25 | * final segment of the action target ARN. If omitted, a unique id is |
| 26 | * generated. Changing this replaces the action target. |
| 27 | */ |
| 28 | id?: string; |
| 29 | } |
| 30 | |
| 31 | /** @resource */ |
| 32 | export interface ActionTarget extends Resource< |
| 33 | "AWS.SecurityHub.ActionTarget", |
| 34 | ActionTargetProps, |
| 35 | { |
| 36 | /** ARN of the action target (`…:action/custom/{id}`). */ |
| 37 | actionTargetArn: string; |
| 38 | /** ID of the custom action (final ARN segment). */ |
| 39 | id: string; |
| 40 | /** Display name of the custom action. */ |
| 41 | name: string; |
| 42 | /** Description of the custom action. */ |
| 43 | description: string; |
| 44 | }, |
| 45 | never, |
| 46 | Providers |
| 47 | > {} |
| 48 | |
| 49 | /** |
| 50 | * A Security Hub custom action target. Selecting the custom action on |
| 51 | * findings or insights in the console publishes a |
| 52 | * `Security Hub Findings - Custom Action` event to EventBridge, which a |
| 53 | * Function can consume via {@link consumeCustomActions}. |
| 54 | * |
| 55 | * ### Creating a Custom Action |
| 56 | * **Example:** Send Findings to a Triage Function |
| 57 | * ```typescript |
| 58 | * const action = yield* AWS.SecurityHub.ActionTarget("Escalate", { |
| 59 | * name: "Escalate", |
| 60 | * description: "Escalate the selected findings to on-call", |
| 61 | * }); |
| 62 | * ``` |
| 63 | * |
| 64 | * **Example:** Consume Custom Action Events |
| 65 | * ```typescript |
| 66 | * yield* AWS.SecurityHub.consumeCustomActions( |
| 67 | * { actionArns: [action.actionTargetArn] }, |
| 68 | * (events) => |
| 69 | * Stream.runForEach(events, (event) => |
| 70 | * Effect.log(event.detail.findings), |
| 71 | * ), |
| 72 | * ); |
| 73 | * ``` |
| 74 | */ |
| 75 | const ActionTargetResource = Resource<ActionTarget>("AWS.SecurityHub.ActionTarget"); |
| 76 | |
| 77 | export { ActionTargetResource as ActionTarget }; |
| 78 | |
| 79 | const actionTargetArn = (region: string, accountId: string, id: string) => |
| 80 | `arn:aws:securityhub:${region}:${accountId}:action/custom/${id}`; |
| 81 | |
| 82 | export const ActionTargetProvider = () => |
| 83 | Provider.effect( |
| 84 | ActionTargetResource, |
| 85 | Effect.gen(function* () { |
| 86 | // Custom action ids are strictly alphanumeric, max 20 characters — |
| 87 | // strip the physical name's delimiters and keep its trailing (random, |
| 88 | // instance-stable) suffix for uniqueness. |
| 89 | const toId = (id: string, props: { id?: string }) => |
| 90 | props.id |
| 91 | ? Effect.succeed(props.id) |
| 92 | : createPhysicalName({ id, maxLength: 64 }).pipe( |
| 93 | Effect.map((name) => name.replace(/[^a-zA-Z0-9]/g, "").slice(-20)), |
| 94 | ); |
| 95 | |
| 96 | const getActionTarget = (arn: string) => |
| 97 | securityhub.describeActionTargets({ ActionTargetArns: [arn] }).pipe( |
| 98 | Effect.map((r) => r.ActionTargets?.[0]), |
| 99 | Effect.catchTags({ |
| 100 | ResourceNotFoundException: () => Effect.undefined, |
| 101 | // The whole hub may be disabled — the action target is gone too. |
| 102 | InvalidAccessException: () => Effect.undefined, |
| 103 | }), |
| 104 | ); |
| 105 | |
| 106 | const buildAttrs = (arn: string, t: securityhub.ActionTarget) => ({ |
| 107 | actionTargetArn: arn, |
| 108 | id: arn.split("/").at(-1)!, |
| 109 | name: t.Name ?? "", |
| 110 | description: t.Description ?? "", |
| 111 | }); |
| 112 | |
| 113 | return { |
| 114 | stables: ["actionTargetArn", "id"], |
| 115 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 116 | if (!isResolved(news)) return; |
| 117 | if (olds === undefined) return; |
| 118 | if ((yield* toId(id, olds)) !== (yield* toId(id, news))) { |
| 119 | return { action: "replace" } as const; |
| 120 | } |
| 121 | }), |
| 122 | read: Effect.fn(function* ({ id, olds, output }) { |
| 123 | const actionId = output?.id ?? (yield* toId(id, olds ?? {})); |
| 124 | const { accountId, region } = yield* AWSEnvironment.current; |
| 125 | const arn = actionTargetArn(region, accountId, actionId); |
| 126 | const live = yield* getActionTarget(arn); |
| 127 | if (!live) return undefined; |
| 128 | // Action targets cannot be tagged; the id embeds the app/stage |
| 129 | // physical-name hash, so a match is ours. |
| 130 | return buildAttrs(arn, live); |
| 131 | }), |
| 132 | list: () => |
| 133 | Effect.gen(function* () { |
| 134 | const pages = yield* securityhub.describeActionTargets |
| 135 | .pages({}) |
| 136 | .pipe(Stream.runCollect); |
| 137 | const out: ActionTarget["Attributes"][] = []; |
| 138 | for (const page of pages) { |
| 139 | for (const t of page.ActionTargets ?? []) { |
| 140 | if (t.ActionTargetArn) { |
| 141 | out.push(buildAttrs(t.ActionTargetArn, t)); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | return out; |
| 146 | }).pipe( |
| 147 | // No hub — no action targets. |
| 148 | Effect.catchTag("InvalidAccessException", () => |
| 149 | Effect.succeed([] as ActionTarget["Attributes"][]), |
| 150 | ), |
| 151 | ), |
| 152 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 153 | const actionId = output?.id ?? (yield* toId(id, news)); |
| 154 | const { accountId, region } = yield* AWSEnvironment.current; |
| 155 | const arn = actionTargetArn(region, accountId, actionId); |
| 156 | |
| 157 | // 1. OBSERVE — cloud state is authoritative. |
| 158 | const live = yield* getActionTarget(arn); |
| 159 | |
| 160 | if (!live) { |
| 161 | // 2. ENSURE — tolerate the create race. |
| 162 | yield* securityhub |
| 163 | .createActionTarget({ |
| 164 | Name: news.name, |
| 165 | Description: news.description, |
| 166 | Id: actionId, |
| 167 | }) |
| 168 | .pipe(Effect.catchTag("ResourceConflictException", () => Effect.void)); |
| 169 | } else if (live.Name !== news.name || live.Description !== news.description) { |
| 170 | // 3. SYNC — observed ↔ desired. |
| 171 | yield* securityhub.updateActionTarget({ |
| 172 | ActionTargetArn: arn, |
| 173 | Name: news.name, |
| 174 | Description: news.description, |
| 175 | }); |
| 176 | } |
| 177 | |
| 178 | // 4. RETURN fresh attributes. |
| 179 | const final = yield* getActionTarget(arn); |
| 180 | yield* session.note(arn); |
| 181 | return buildAttrs(arn, final ?? { Name: news.name, Description: news.description }); |
| 182 | }), |
| 183 | delete: Effect.fn(function* ({ output }) { |
| 184 | // Idempotent — the action target (or the whole hub) may be gone. |
| 185 | yield* securityhub.deleteActionTarget({ ActionTargetArn: output.actionTargetArn }).pipe( |
| 186 | Effect.catchTags({ |
| 187 | ResourceNotFoundException: () => Effect.void, |
| 188 | InvalidAccessException: () => Effect.void, |
| 189 | }), |
| 190 | ); |
| 191 | }), |
| 192 | }; |
| 193 | }), |
| 194 | ); |