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, 06:00 UTC
| 1 | import * as guardduty from "@distilled.cloud/aws/guardduty"; |
| 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 { createInternalTags, diffTags, hasAlchemyTags, tagRecord } from "alchemy/Tags"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import { AWSEnvironment } from "../Environment.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | |
| 13 | /** File format of the hosted threat intelligence list. */ |
| 14 | export type ThreatIntelSetFormat = |
| 15 | | "TXT" |
| 16 | | "STIX" |
| 17 | | "OTX_CSV" |
| 18 | | "ALIEN_VAULT" |
| 19 | | "PROOF_POINT" |
| 20 | | "FIRE_EYE"; |
| 21 | |
| 22 | export interface ThreatIntelSetProps { |
| 23 | /** |
| 24 | * ID of the detector the threat intel set belongs to. Changing this |
| 25 | * replaces the set. |
| 26 | */ |
| 27 | detectorId: string; |
| 28 | |
| 29 | /** |
| 30 | * Display name of the threat intel set. If omitted, a unique name is |
| 31 | * generated. Updatable in place. |
| 32 | */ |
| 33 | name?: string; |
| 34 | |
| 35 | /** |
| 36 | * Format of the file hosting the threat list. Changing this replaces the |
| 37 | * set. |
| 38 | */ |
| 39 | format: ThreatIntelSetFormat; |
| 40 | |
| 41 | /** |
| 42 | * S3 URI of the file containing known malicious IP addresses, e.g. |
| 43 | * `https://s3.amazonaws.com/my-bucket/threats.txt`. Updatable in place. |
| 44 | */ |
| 45 | location: string; |
| 46 | |
| 47 | /** |
| 48 | * Whether GuardDuty actively uses the list. Traffic to/from listed IPs |
| 49 | * generates findings while active. Updatable in place. |
| 50 | * @default true |
| 51 | */ |
| 52 | activate?: boolean; |
| 53 | |
| 54 | /** |
| 55 | * Account ID of the bucket owner hosting the list (guards against bucket |
| 56 | * squatting). |
| 57 | */ |
| 58 | expectedBucketOwner?: string; |
| 59 | |
| 60 | /** |
| 61 | * Tags applied to the threat intel set. Alchemy ownership tags are merged |
| 62 | * in automatically. |
| 63 | */ |
| 64 | tags?: Record<string, string>; |
| 65 | } |
| 66 | |
| 67 | /** @resource */ |
| 68 | export interface ThreatIntelSet extends Resource< |
| 69 | "AWS.GuardDuty.ThreatIntelSet", |
| 70 | ThreatIntelSetProps, |
| 71 | { |
| 72 | /** ID of the detector the set belongs to. */ |
| 73 | detectorId: string; |
| 74 | /** The auto-generated threat intel set ID. */ |
| 75 | threatIntelSetId: string; |
| 76 | /** ARN of the threat intel set. */ |
| 77 | threatIntelSetArn: string; |
| 78 | /** Display name of the set. */ |
| 79 | name: string; |
| 80 | /** File format of the hosted list. */ |
| 81 | format: string; |
| 82 | /** S3 URI of the hosted list. */ |
| 83 | location: string; |
| 84 | /** Current status (`ACTIVE`, `INACTIVE`, `ACTIVATING`, `ERROR`, …). */ |
| 85 | status: string; |
| 86 | }, |
| 87 | never, |
| 88 | Providers |
| 89 | > {} |
| 90 | |
| 91 | /** |
| 92 | * A GuardDuty threat intelligence set — an S3-hosted list of known malicious |
| 93 | * IP addresses that GuardDuty generates findings for. The list file must |
| 94 | * exist in S3 before activation; name, location, and activation are |
| 95 | * updatable in place, while format changes replace the set. |
| 96 | * |
| 97 | * ### Custom Threat Intelligence |
| 98 | * **Example:** Feed a custom threat list |
| 99 | * ```typescript |
| 100 | * const detector = yield* AWS.GuardDuty.Detector("Detector", {}); |
| 101 | * const threats = yield* AWS.GuardDuty.ThreatIntelSet("BadIPs", { |
| 102 | * detectorId: detector.detectorId, |
| 103 | * format: "TXT", |
| 104 | * location: "https://s3.amazonaws.com/my-security-bucket/threats.txt", |
| 105 | * }); |
| 106 | * ``` |
| 107 | */ |
| 108 | const ThreatIntelSetResource = Resource<ThreatIntelSet>("AWS.GuardDuty.ThreatIntelSet"); |
| 109 | |
| 110 | export { ThreatIntelSetResource as ThreatIntelSet }; |
| 111 | |
| 112 | const threatIntelSetArn = ( |
| 113 | region: string, |
| 114 | accountId: string, |
| 115 | detectorId: string, |
| 116 | threatIntelSetId: string, |
| 117 | ) => |
| 118 | `arn:aws:guardduty:${region}:${accountId}:detector/${detectorId}/threatintelset/${threatIntelSetId}`; |
| 119 | |
| 120 | /** Statuses that count as "the set is (becoming) active". */ |
| 121 | const ACTIVE_STATUSES = ["ACTIVE", "ACTIVATING"]; |
| 122 | |
| 123 | export const ThreatIntelSetProvider = () => |
| 124 | Provider.effect( |
| 125 | ThreatIntelSetResource, |
| 126 | Effect.gen(function* () { |
| 127 | const toName = (id: string, props: { name?: string }) => |
| 128 | props.name ? Effect.succeed(props.name) : createPhysicalName({ id, maxLength: 64 }); |
| 129 | |
| 130 | const getSet = (detectorId: string, threatIntelSetId: string) => |
| 131 | guardduty |
| 132 | .getThreatIntelSet({ |
| 133 | DetectorId: detectorId, |
| 134 | ThreatIntelSetId: threatIntelSetId, |
| 135 | }) |
| 136 | .pipe(Effect.catchTag("BadRequestException", () => Effect.undefined)); |
| 137 | |
| 138 | // Recover the set id after state loss by matching the deterministic |
| 139 | // name across the detector's threat intel sets. |
| 140 | const findByName = Effect.fn(function* (detectorId: string, name: string) { |
| 141 | const pages = yield* guardduty.listThreatIntelSets |
| 142 | .pages({ DetectorId: detectorId }) |
| 143 | .pipe(Stream.runCollect); |
| 144 | for (const threatIntelSetId of Array.from(pages).flatMap( |
| 145 | (page) => page.ThreatIntelSetIds ?? [], |
| 146 | )) { |
| 147 | const s = yield* getSet(detectorId, threatIntelSetId); |
| 148 | if (s?.Name === name && s.Status !== "DELETE_PENDING") { |
| 149 | return { threatIntelSetId, set: s }; |
| 150 | } |
| 151 | } |
| 152 | return undefined; |
| 153 | }); |
| 154 | |
| 155 | const buildAttrs = Effect.fn(function* ( |
| 156 | detectorId: string, |
| 157 | threatIntelSetId: string, |
| 158 | s: guardduty.GetThreatIntelSetResponse, |
| 159 | ) { |
| 160 | const { accountId, region } = yield* AWSEnvironment.current; |
| 161 | return { |
| 162 | detectorId, |
| 163 | threatIntelSetId, |
| 164 | threatIntelSetArn: threatIntelSetArn(region, accountId, detectorId, threatIntelSetId), |
| 165 | name: s.Name, |
| 166 | format: s.Format, |
| 167 | location: s.Location, |
| 168 | status: s.Status, |
| 169 | }; |
| 170 | }); |
| 171 | |
| 172 | return { |
| 173 | stables: ["detectorId", "threatIntelSetId", "threatIntelSetArn"], |
| 174 | diff: Effect.fn(function* ({ olds, news }) { |
| 175 | if (!isResolved(news)) return; |
| 176 | if (olds === undefined) return; |
| 177 | if (olds.detectorId !== news.detectorId || olds.format !== news.format) { |
| 178 | return { action: "replace" } as const; |
| 179 | } |
| 180 | }), |
| 181 | read: Effect.fn(function* ({ id, olds, output }) { |
| 182 | const detectorId = output?.detectorId ?? olds?.detectorId; |
| 183 | if (!detectorId) return undefined; |
| 184 | let threatIntelSetId = output?.threatIntelSetId; |
| 185 | let set = threatIntelSetId ? yield* getSet(detectorId, threatIntelSetId) : undefined; |
| 186 | if (!set) { |
| 187 | const found = yield* findByName(detectorId, yield* toName(id, olds ?? {})); |
| 188 | if (!found) return undefined; |
| 189 | ({ threatIntelSetId, set } = found); |
| 190 | } |
| 191 | const attrs = yield* buildAttrs(detectorId, threatIntelSetId!, set); |
| 192 | return (yield* hasAlchemyTags(id, set.Tags)) ? attrs : Unowned(attrs); |
| 193 | }), |
| 194 | list: () => |
| 195 | Effect.gen(function* () { |
| 196 | const { DetectorIds } = yield* guardduty.listDetectors({}); |
| 197 | const out: ThreatIntelSet["Attributes"][] = []; |
| 198 | for (const detectorId of DetectorIds ?? []) { |
| 199 | const pages = yield* guardduty.listThreatIntelSets |
| 200 | .pages({ DetectorId: detectorId }) |
| 201 | .pipe(Stream.runCollect); |
| 202 | for (const threatIntelSetId of Array.from(pages).flatMap( |
| 203 | (page) => page.ThreatIntelSetIds ?? [], |
| 204 | )) { |
| 205 | const s = yield* getSet(detectorId, threatIntelSetId); |
| 206 | if (s) { |
| 207 | out.push(yield* buildAttrs(detectorId, threatIntelSetId, s)); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | return out; |
| 212 | }), |
| 213 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 214 | const detectorId = news.detectorId; |
| 215 | const name = yield* toName(id, news); |
| 216 | const desiredActivate = news.activate ?? true; |
| 217 | const internalTags = yield* createInternalTags(id); |
| 218 | const desiredTags = { ...news.tags, ...internalTags }; |
| 219 | |
| 220 | // 1. OBSERVE — output.threatIntelSetId is only a cache; fall back |
| 221 | // to a name search so a crash-and-retry converges. |
| 222 | let threatIntelSetId = output?.threatIntelSetId; |
| 223 | let live = threatIntelSetId ? yield* getSet(detectorId, threatIntelSetId) : undefined; |
| 224 | if (!live) { |
| 225 | const found = yield* findByName(detectorId, name); |
| 226 | if (found) ({ threatIntelSetId, set: live } = found); |
| 227 | } |
| 228 | |
| 229 | if (!live || !threatIntelSetId) { |
| 230 | // 2. ENSURE — create with tags applied inline. |
| 231 | const created = yield* guardduty.createThreatIntelSet({ |
| 232 | DetectorId: detectorId, |
| 233 | Name: name, |
| 234 | Format: news.format, |
| 235 | Location: news.location, |
| 236 | Activate: desiredActivate, |
| 237 | ExpectedBucketOwner: news.expectedBucketOwner, |
| 238 | Tags: desiredTags, |
| 239 | }); |
| 240 | threatIntelSetId = created.ThreatIntelSetId; |
| 241 | } else { |
| 242 | // 3. SYNC settings — observed ↔ desired. |
| 243 | const observedActive = ACTIVE_STATUSES.includes(live.Status); |
| 244 | const drift = |
| 245 | live.Name !== name || |
| 246 | live.Location !== news.location || |
| 247 | observedActive !== desiredActivate; |
| 248 | if (drift) { |
| 249 | yield* guardduty.updateThreatIntelSet({ |
| 250 | DetectorId: detectorId, |
| 251 | ThreatIntelSetId: threatIntelSetId, |
| 252 | Name: name, |
| 253 | Location: news.location, |
| 254 | Activate: desiredActivate, |
| 255 | ExpectedBucketOwner: news.expectedBucketOwner, |
| 256 | }); |
| 257 | } |
| 258 | |
| 259 | // 3b. SYNC tags — diff against OBSERVED cloud tags. |
| 260 | const { accountId, region } = yield* AWSEnvironment.current; |
| 261 | const arn = threatIntelSetArn(region, accountId, detectorId, threatIntelSetId); |
| 262 | const { upsert, removed } = diffTags(tagRecord(live.Tags), desiredTags); |
| 263 | if (upsert.length > 0) { |
| 264 | yield* guardduty.tagResource({ |
| 265 | ResourceArn: arn, |
| 266 | Tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])), |
| 267 | }); |
| 268 | } |
| 269 | if (removed.length > 0) { |
| 270 | yield* guardduty.untagResource({ |
| 271 | ResourceArn: arn, |
| 272 | TagKeys: removed, |
| 273 | }); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // 4. RETURN fresh attributes. |
| 278 | const final = yield* guardduty.getThreatIntelSet({ |
| 279 | DetectorId: detectorId, |
| 280 | ThreatIntelSetId: threatIntelSetId, |
| 281 | }); |
| 282 | yield* session.note(`${detectorId}/${threatIntelSetId}`); |
| 283 | return yield* buildAttrs(detectorId, threatIntelSetId, final); |
| 284 | }), |
| 285 | delete: Effect.fn(function* ({ output }) { |
| 286 | // Idempotent — the set (or its whole detector) may already be |
| 287 | // gone; both surface as BadRequestException. |
| 288 | yield* guardduty |
| 289 | .deleteThreatIntelSet({ |
| 290 | DetectorId: output.detectorId, |
| 291 | ThreatIntelSetId: output.threatIntelSetId, |
| 292 | }) |
| 293 | .pipe(Effect.catchTag("BadRequestException", () => Effect.void)); |
| 294 | }), |
| 295 | }; |
| 296 | }), |
| 297 | ); |