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 macie2 from "@distilled.cloud/aws/macie2"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import { createInternalTags, diffTags, hasAlchemyTags, tagRecord } from "alchemy/Tags"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import { retryThroughEnablement } from "./common.ts"; |
| 11 | |
| 12 | export interface AllowListProps { |
| 13 | /** |
| 14 | * Custom name for the allow list (1-128 characters). Must be unique per |
| 15 | * account. If omitted, a unique name is generated from the app/stage/logical |
| 16 | * ID. Updatable in place. |
| 17 | */ |
| 18 | name?: string; |
| 19 | |
| 20 | /** |
| 21 | * Custom description of the allow list (up to 512 characters). Updatable in |
| 22 | * place. |
| 23 | */ |
| 24 | description?: string; |
| 25 | |
| 26 | /** |
| 27 | * The criteria that specify the text to ignore — either a `regex` (a regular |
| 28 | * expression that defines the text pattern) or an `s3WordsList` (an S3 |
| 29 | * object listing predefined words, one per line). Updatable in place. |
| 30 | */ |
| 31 | criteria: macie2.AllowListCriteria; |
| 32 | |
| 33 | /** |
| 34 | * Tags applied to the allow list. Alchemy ownership tags are merged in |
| 35 | * automatically. |
| 36 | */ |
| 37 | tags?: Record<string, string>; |
| 38 | } |
| 39 | |
| 40 | /** @resource */ |
| 41 | export interface AllowList extends Resource< |
| 42 | "AWS.Macie2.AllowList", |
| 43 | AllowListProps, |
| 44 | { |
| 45 | /** Generated allow list ID. */ |
| 46 | id: string; |
| 47 | /** ARN of the allow list. */ |
| 48 | arn: string; |
| 49 | /** The resolved allow list name. */ |
| 50 | name: string; |
| 51 | /** Current status code (`OK` / `S3_OBJECT_NOT_FOUND` / ...). */ |
| 52 | status: string | undefined; |
| 53 | }, |
| 54 | never, |
| 55 | Providers |
| 56 | > {} |
| 57 | |
| 58 | /** |
| 59 | * An Amazon Macie allow list — text patterns or predefined words that Macie |
| 60 | * ignores when it inspects S3 objects for sensitive data. Requires Macie to be |
| 61 | * enabled for the account (see `Macie2.Session`). Name, description, and |
| 62 | * criteria are all updatable in place; destroy deletes the list even if |
| 63 | * classification jobs reference it. |
| 64 | * |
| 65 | * ### Creating an allow list |
| 66 | * **Example:** Regex allow list |
| 67 | * ```typescript |
| 68 | * const allowList = yield* Macie2.AllowList("InternalIds", { |
| 69 | * description: "Internal ticket ids are not sensitive", |
| 70 | * criteria: { regex: "TICKET-[0-9]{6}" }, |
| 71 | * }); |
| 72 | * ``` |
| 73 | * |
| 74 | * **Example:** Predefined words from S3 |
| 75 | * ```typescript |
| 76 | * const allowList = yield* Macie2.AllowList("KnownTestData", { |
| 77 | * criteria: { |
| 78 | * s3WordsList: { bucketName: bucket.bucketName, objectKey: "words.txt" }, |
| 79 | * }, |
| 80 | * }); |
| 81 | * ``` |
| 82 | */ |
| 83 | const AllowListResource = Resource<AllowList>("AWS.Macie2.AllowList"); |
| 84 | |
| 85 | export { AllowListResource as AllowList }; |
| 86 | |
| 87 | const createName = (id: string, props: Partial<AllowListProps>) => |
| 88 | props.name ? Effect.succeed(props.name) : createPhysicalName({ id, maxLength: 128 }); |
| 89 | |
| 90 | const buildAllowListAttrs = (id: string, live: macie2.GetAllowListResponse) => ({ |
| 91 | id, |
| 92 | arn: live.arn!, |
| 93 | name: live.name!, |
| 94 | status: live.status?.code, |
| 95 | }); |
| 96 | |
| 97 | export const AllowListProvider = () => |
| 98 | Provider.effect( |
| 99 | AllowListResource, |
| 100 | Effect.gen(function* () { |
| 101 | const getAllowList = (id: string) => |
| 102 | macie2.getAllowList({ id }).pipe( |
| 103 | Effect.catchTags({ |
| 104 | ResourceNotFoundException: () => Effect.undefined, |
| 105 | // Macie disabled ⇒ the list is unreachable (and disabling deletes |
| 106 | // all Macie configuration), so report it as gone. |
| 107 | AccessDeniedException: () => Effect.undefined, |
| 108 | }), |
| 109 | ); |
| 110 | |
| 111 | return { |
| 112 | stables: ["id", "arn"], |
| 113 | |
| 114 | read: Effect.fn(function* ({ id, output }) { |
| 115 | if (!output?.id) return undefined; |
| 116 | const live = yield* getAllowList(output.id); |
| 117 | if (!live) return undefined; |
| 118 | const attrs = buildAllowListAttrs(output.id, live); |
| 119 | return (yield* hasAlchemyTags(id, live.tags)) ? attrs : Unowned(attrs); |
| 120 | }), |
| 121 | |
| 122 | list: () => |
| 123 | Effect.gen(function* () { |
| 124 | const pages = yield* macie2.listAllowLists.pages({}).pipe( |
| 125 | Stream.runCollect, |
| 126 | Effect.catchTag("AccessDeniedException", () => Effect.succeed([])), |
| 127 | ); |
| 128 | const out: AllowList["Attributes"][] = []; |
| 129 | for (const page of pages) { |
| 130 | for (const summary of page.allowLists ?? []) { |
| 131 | const live = yield* getAllowList(summary.id!); |
| 132 | if (live) out.push(buildAllowListAttrs(summary.id!, live)); |
| 133 | } |
| 134 | } |
| 135 | return out; |
| 136 | }), |
| 137 | |
| 138 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 139 | const internalTags = yield* createInternalTags(id); |
| 140 | const desiredTags = { ...news.tags, ...internalTags }; |
| 141 | const name = news.name ?? (yield* createName(id, news)); |
| 142 | |
| 143 | // 1. OBSERVE — cloud state is authoritative; output caches the id. |
| 144 | let allowListId = output?.id; |
| 145 | let live = allowListId ? yield* getAllowList(allowListId) : undefined; |
| 146 | |
| 147 | if (!live || allowListId === undefined) { |
| 148 | // 2. ENSURE — create the allow list (retry through enablement lag). |
| 149 | const created = yield* retryThroughEnablement( |
| 150 | macie2.createAllowList({ |
| 151 | name, |
| 152 | description: news.description, |
| 153 | criteria: news.criteria, |
| 154 | tags: desiredTags, |
| 155 | }), |
| 156 | ); |
| 157 | allowListId = created.id!; |
| 158 | } else { |
| 159 | // 3. SYNC settings — observed ↔ desired (name is required by the |
| 160 | // update API, so it is always sent when anything drifts). |
| 161 | const drift = |
| 162 | live.name !== name || |
| 163 | (news.description !== undefined && live.description !== news.description) || |
| 164 | JSON.stringify(live.criteria) !== JSON.stringify(news.criteria); |
| 165 | if (drift) { |
| 166 | yield* macie2.updateAllowList({ |
| 167 | id: allowListId, |
| 168 | name, |
| 169 | description: news.description, |
| 170 | criteria: news.criteria, |
| 171 | }); |
| 172 | } |
| 173 | |
| 174 | // 3b. SYNC tags — diff against OBSERVED cloud tags. |
| 175 | const { upsert, removed } = diffTags(tagRecord(live.tags), desiredTags); |
| 176 | if (upsert.length > 0) { |
| 177 | yield* macie2.tagResource({ |
| 178 | resourceArn: live.arn!, |
| 179 | tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])), |
| 180 | }); |
| 181 | } |
| 182 | if (removed.length > 0) { |
| 183 | yield* macie2.untagResource({ |
| 184 | resourceArn: live.arn!, |
| 185 | tagKeys: removed, |
| 186 | }); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // 4. RETURN fresh attributes. |
| 191 | const final = yield* macie2.getAllowList({ id: allowListId }); |
| 192 | yield* session.note(allowListId); |
| 193 | return buildAllowListAttrs(allowListId, final); |
| 194 | }), |
| 195 | |
| 196 | delete: Effect.fn(function* ({ output }) { |
| 197 | // Idempotent — the list may already be gone, or Macie may already be |
| 198 | // disabled for the account (which removes all Macie configuration). |
| 199 | yield* macie2.deleteAllowList({ id: output.id, ignoreJobChecks: "true" }).pipe( |
| 200 | Effect.catchTags({ |
| 201 | ResourceNotFoundException: () => Effect.void, |
| 202 | AccessDeniedException: () => Effect.void, |
| 203 | }), |
| 204 | ); |
| 205 | }), |
| 206 | }; |
| 207 | }), |
| 208 | ); |