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 aa from "@distilled.cloud/aws/accessanalyzer"; |
| 2 | import { unownedUnlessPrevious } 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 { toWireDays } from "alchemy/Util/Duration"; |
| 9 | import * as Data from "effect/Data"; |
| 10 | import type * as Duration from "effect/Duration"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import * as Stream from "effect/Stream"; |
| 14 | import type { Providers } from "../Providers.ts"; |
| 15 | |
| 16 | /** |
| 17 | * The zone of trust and access type an analyzer monitors. |
| 18 | * |
| 19 | * - `ACCOUNT` / `ORGANIZATION` — external-access analysis (the free tier): |
| 20 | * surfaces resource policies that grant access to principals outside the |
| 21 | * account or organization. |
| 22 | * - `ACCOUNT_UNUSED_ACCESS` / `ORGANIZATION_UNUSED_ACCESS` — unused-access |
| 23 | * analysis (paid): surfaces unused IAM roles, users, and permissions. |
| 24 | */ |
| 25 | export type AnalyzerType = |
| 26 | | "ACCOUNT" |
| 27 | | "ORGANIZATION" |
| 28 | | "ACCOUNT_UNUSED_ACCESS" |
| 29 | | "ORGANIZATION_UNUSED_ACCESS"; |
| 30 | |
| 31 | export interface AnalyzerProps { |
| 32 | /** |
| 33 | * Name of the analyzer. Must be unique within the account/Region and start |
| 34 | * with a letter (`[A-Za-z][A-Za-z0-9_.-]*`, up to 255 characters). Changing |
| 35 | * the name replaces the analyzer. |
| 36 | * @default a generated physical name derived from the logical ID |
| 37 | */ |
| 38 | analyzerName?: string; |
| 39 | /** |
| 40 | * The analyzer's zone of trust and access type. Create-only — changing the |
| 41 | * type replaces the analyzer. |
| 42 | * @default "ACCOUNT" |
| 43 | */ |
| 44 | type?: AnalyzerType; |
| 45 | /** |
| 46 | * How long access must go unused before the analyzer reports it as an |
| 47 | * unused-access finding. Accepts any duration input (e.g. `"180 days"`); |
| 48 | * converted to whole days on the wire (valid range 1–365 days). Only valid |
| 49 | * for `ACCOUNT_UNUSED_ACCESS` / `ORGANIZATION_UNUSED_ACCESS` analyzers. |
| 50 | * Create-only — the API rejects in-place tracking-period updates, so |
| 51 | * changing this replaces the analyzer (delete-first, since accounts are |
| 52 | * limited to one unused-access analyzer per Region). When omitted, the |
| 53 | * analyzer's existing tracking period is left unmanaged. |
| 54 | * @default "90 days" (the AWS default) |
| 55 | */ |
| 56 | unusedAccessAge?: Duration.Input; |
| 57 | /** |
| 58 | * Tags to apply to the analyzer. Merged with internal Alchemy tags. |
| 59 | */ |
| 60 | tags?: Record<string, string>; |
| 61 | } |
| 62 | |
| 63 | export interface Analyzer extends Resource< |
| 64 | "AWS.AccessAnalyzer.Analyzer", |
| 65 | AnalyzerProps, |
| 66 | { |
| 67 | analyzerName: string; |
| 68 | analyzerArn: string; |
| 69 | type: string; |
| 70 | status: string; |
| 71 | }, |
| 72 | {}, |
| 73 | Providers |
| 74 | > {} |
| 75 | |
| 76 | class AnalyzerStillExists extends Data.TaggedError("AnalyzerStillExists")<{ |
| 77 | readonly analyzerName: string; |
| 78 | }> {} |
| 79 | |
| 80 | /** |
| 81 | * An AWS IAM Access Analyzer — continuously monitors resource policies to |
| 82 | * identify resources shared with an external entity (external-access |
| 83 | * analyzers) or unused IAM access (unused-access analyzers). |
| 84 | * |
| 85 | * The `ACCOUNT` external-access analyzer is free and is the common case: |
| 86 | * create one per account per Region to have Access Analyzer surface public |
| 87 | * and cross-account grants as findings. |
| 88 | * ### Creating an Analyzer |
| 89 | * **Example:** Account External-Access Analyzer |
| 90 | * ```typescript |
| 91 | * import * as AWS from "@alchemy.run/aws"; |
| 92 | * |
| 93 | * const analyzer = yield* AWS.AccessAnalyzer.Analyzer("AccountAnalyzer", { |
| 94 | * type: "ACCOUNT", |
| 95 | * }); |
| 96 | * ``` |
| 97 | * |
| 98 | * **Example:** Analyzer with Tags |
| 99 | * ```typescript |
| 100 | * const analyzer = yield* AWS.AccessAnalyzer.Analyzer("AccountAnalyzer", { |
| 101 | * analyzerName: "prod-external-access", |
| 102 | * type: "ACCOUNT", |
| 103 | * tags: { Environment: "prod" }, |
| 104 | * }); |
| 105 | * ``` |
| 106 | * |
| 107 | * **Example:** Unused-Access Analyzer with a Custom Tracking Period |
| 108 | * ```typescript |
| 109 | * const analyzer = yield* AWS.AccessAnalyzer.Analyzer("UnusedAccess", { |
| 110 | * type: "ACCOUNT_UNUSED_ACCESS", |
| 111 | * unusedAccessAge: "180 days", |
| 112 | * }); |
| 113 | * ``` |
| 114 | * |
| 115 | * ### Archiving Findings |
| 116 | * **Example:** Auto-archive Findings from a Trusted Account |
| 117 | * ```typescript |
| 118 | * const analyzer = yield* AWS.AccessAnalyzer.Analyzer("AccountAnalyzer", {}); |
| 119 | * |
| 120 | * yield* AWS.AccessAnalyzer.ArchiveRule("TrustedAccount", { |
| 121 | * analyzerName: analyzer.analyzerName, |
| 122 | * ruleName: "trusted-account", |
| 123 | * filter: { |
| 124 | * "principal.AWS": { eq: ["123456789012"] }, |
| 125 | * }, |
| 126 | * }); |
| 127 | * ``` |
| 128 | * |
| 129 | * @resource |
| 130 | */ |
| 131 | export const Analyzer = Resource<Analyzer>("AWS.AccessAnalyzer.Analyzer"); |
| 132 | |
| 133 | export const AnalyzerProvider = () => |
| 134 | Provider.effect( |
| 135 | Analyzer, |
| 136 | Effect.gen(function* () { |
| 137 | const createName = Effect.fn(function* ( |
| 138 | id: string, |
| 139 | props: { analyzerName?: string | undefined }, |
| 140 | ) { |
| 141 | return props.analyzerName ?? (yield* createPhysicalName({ id, maxLength: 255 })); |
| 142 | }); |
| 143 | |
| 144 | const observe = Effect.fn(function* (name: string) { |
| 145 | return yield* aa.getAnalyzer({ analyzerName: name }).pipe( |
| 146 | Effect.map((r) => r.analyzer), |
| 147 | Effect.catchTag("ResourceNotFoundException", () => Effect.undefined), |
| 148 | ); |
| 149 | }); |
| 150 | |
| 151 | return Analyzer.Provider.of({ |
| 152 | stables: ["analyzerName", "analyzerArn", "type"], |
| 153 | |
| 154 | list: () => |
| 155 | Effect.gen(function* () { |
| 156 | const pages = yield* aa.listAnalyzers.pages({}).pipe(Stream.runCollect); |
| 157 | const summaries = Array.from(pages).flatMap((page) => page.analyzers ?? []); |
| 158 | return summaries.map((summary) => ({ |
| 159 | analyzerName: summary.name, |
| 160 | analyzerArn: summary.arn, |
| 161 | type: summary.type, |
| 162 | status: summary.status, |
| 163 | })); |
| 164 | }), |
| 165 | |
| 166 | read: Effect.fn(function* ({ id, olds, output, previous }) { |
| 167 | const name = output?.analyzerName ?? (yield* createName(id, olds ?? {})); |
| 168 | const analyzer = yield* observe(name); |
| 169 | if (analyzer === undefined) return undefined; |
| 170 | const attrs = { |
| 171 | analyzerName: analyzer.name, |
| 172 | analyzerArn: analyzer.arn, |
| 173 | type: analyzer.type, |
| 174 | status: analyzer.status, |
| 175 | }; |
| 176 | const tags = analyzer.tags ?? {}; |
| 177 | return (yield* hasAlchemyTags(id, tags)) |
| 178 | ? attrs |
| 179 | : unownedUnlessPrevious(attrs, previous, "analyzerName"); |
| 180 | }), |
| 181 | |
| 182 | diff: Effect.fn(function* ({ id, news, olds }) { |
| 183 | if (!isResolved(news)) return undefined; |
| 184 | const oldName = yield* createName(id, olds); |
| 185 | const newName = yield* createName(id, news); |
| 186 | if (oldName !== newName) { |
| 187 | return { action: "replace" } as const; |
| 188 | } |
| 189 | // type is create-only for external-access analyzers |
| 190 | if ((olds.type ?? "ACCOUNT") !== (news.type ?? "ACCOUNT")) { |
| 191 | return { action: "replace" } as const; |
| 192 | } |
| 193 | // unusedAccessAge is create-only (the API rejects in-place |
| 194 | // tracking-period updates with "Cannot update unused access age"). |
| 195 | // Delete first: accounts are limited to one unused-access analyzer |
| 196 | // per Region, so create-before-delete would hit the quota (and the |
| 197 | // physical name is stable across the replacement). |
| 198 | if ( |
| 199 | news.unusedAccessAge !== undefined && |
| 200 | toWireDays(olds.unusedAccessAge) !== toWireDays(news.unusedAccessAge) |
| 201 | ) { |
| 202 | return { action: "replace", deleteFirst: true } as const; |
| 203 | } |
| 204 | // only tags are mutable → default update path |
| 205 | }), |
| 206 | |
| 207 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 208 | const name = output?.analyzerName ?? (yield* createName(id, news)); |
| 209 | const type = news.type ?? "ACCOUNT"; |
| 210 | const internalTags = yield* createInternalTags(id); |
| 211 | const desiredTags = { ...news.tags, ...internalTags }; |
| 212 | const desiredUnusedAccessAge = toWireDays(news.unusedAccessAge); |
| 213 | |
| 214 | // 1. OBSERVE — cloud state is authoritative |
| 215 | let analyzer = yield* observe(name); |
| 216 | |
| 217 | // 2. ENSURE — createAnalyzer, tolerating a concurrent create race |
| 218 | if (analyzer === undefined) { |
| 219 | yield* aa |
| 220 | .createAnalyzer({ |
| 221 | analyzerName: name, |
| 222 | type, |
| 223 | tags: desiredTags, |
| 224 | configuration: |
| 225 | desiredUnusedAccessAge !== undefined |
| 226 | ? { |
| 227 | unusedAccess: { |
| 228 | unusedAccessAge: desiredUnusedAccessAge, |
| 229 | }, |
| 230 | } |
| 231 | : undefined, |
| 232 | }) |
| 233 | .pipe(Effect.catchTag("ConflictException", () => Effect.void)); |
| 234 | analyzer = yield* observe(name); |
| 235 | } |
| 236 | if (analyzer === undefined) { |
| 237 | return yield* aa.ResourceNotFoundException.make({ |
| 238 | message: `analyzer ${name} not visible after create`, |
| 239 | resourceId: name, |
| 240 | resourceType: "AWS::AccessAnalyzer::Analyzer", |
| 241 | }); |
| 242 | } |
| 243 | |
| 244 | // 3. SYNC TAGS — diff against OBSERVED cloud tags |
| 245 | // (unusedAccessAge is create-only — diff replaces on change, so |
| 246 | // there is no configuration sync step.) |
| 247 | const observedTags = tagRecord(analyzer.tags); |
| 248 | const { upsert, removed } = diffTags(observedTags, desiredTags); |
| 249 | if (upsert.length > 0) { |
| 250 | yield* aa.tagResource({ |
| 251 | resourceArn: analyzer.arn, |
| 252 | tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])), |
| 253 | }); |
| 254 | } |
| 255 | if (removed.length > 0) { |
| 256 | yield* aa.untagResource({ |
| 257 | resourceArn: analyzer.arn, |
| 258 | tagKeys: removed, |
| 259 | }); |
| 260 | } |
| 261 | |
| 262 | yield* session.note(analyzer.arn); |
| 263 | return { |
| 264 | analyzerName: analyzer.name, |
| 265 | analyzerArn: analyzer.arn, |
| 266 | type: analyzer.type, |
| 267 | status: analyzer.status, |
| 268 | }; |
| 269 | }), |
| 270 | |
| 271 | delete: Effect.fn(function* ({ output }) { |
| 272 | yield* aa |
| 273 | .deleteAnalyzer({ analyzerName: output.analyzerName }) |
| 274 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 275 | |
| 276 | // Unused-access analyzers are account/Region singletons. The delete |
| 277 | // API returns before the quota slot is necessarily reusable, so a |
| 278 | // delete-first replacement (or a subsequent clean test run) can hit |
| 279 | // ServiceQuotaExceededException unless we observe deletion here. |
| 280 | yield* observe(output.analyzerName).pipe( |
| 281 | Effect.flatMap((analyzer) => |
| 282 | analyzer === undefined |
| 283 | ? Effect.void |
| 284 | : Effect.fail( |
| 285 | new AnalyzerStillExists({ |
| 286 | analyzerName: output.analyzerName, |
| 287 | }), |
| 288 | ), |
| 289 | ), |
| 290 | Effect.retry({ |
| 291 | while: (error) => error._tag === "AnalyzerStillExists", |
| 292 | schedule: Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(15)]), |
| 293 | }), |
| 294 | ); |
| 295 | }), |
| 296 | }); |
| 297 | }), |
| 298 | ); |