The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as config from "@distilled.cloud/aws/config-service"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import { createInternalTags, createTagsList, diffTags, hasAlchemyTags } 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 | |
| 11 | export interface AggregationAuthorizationProps { |
| 12 | /** |
| 13 | * The 12-digit account ID of the aggregator account that is authorized to |
| 14 | * collect AWS Config data from this account. Changing it replaces the |
| 15 | * authorization. |
| 16 | */ |
| 17 | authorizedAccountId: string; |
| 18 | /** |
| 19 | * The region of the aggregator account that is authorized to collect AWS |
| 20 | * Config data from this account. Changing it replaces the authorization. |
| 21 | */ |
| 22 | authorizedAwsRegion: string; |
| 23 | /** |
| 24 | * Tags to apply to the authorization. Merged with internal Alchemy tags. |
| 25 | */ |
| 26 | tags?: Record<string, string>; |
| 27 | } |
| 28 | |
| 29 | export interface AggregationAuthorization extends Resource< |
| 30 | "AWS.Config.AggregationAuthorization", |
| 31 | AggregationAuthorizationProps, |
| 32 | { |
| 33 | /** ARN of the aggregation authorization. */ |
| 34 | aggregationAuthorizationArn: string; |
| 35 | /** The aggregator account authorized to collect data. */ |
| 36 | authorizedAccountId: string; |
| 37 | /** The aggregator region authorized to collect data. */ |
| 38 | authorizedAwsRegion: string; |
| 39 | }, |
| 40 | never, |
| 41 | Providers |
| 42 | > {} |
| 43 | |
| 44 | /** |
| 45 | * An AWS Config aggregation authorization that grants an aggregator account |
| 46 | * in a specific region permission to collect AWS Config configuration and |
| 47 | * compliance data from this account. |
| 48 | * |
| 49 | * The authorization's identity is the `(account, region)` pair — changing |
| 50 | * either replaces it. |
| 51 | * ### Authorizing an Aggregator |
| 52 | * **Example:** Authorize an aggregator account |
| 53 | * ```typescript |
| 54 | * import * as Config from "@alchemy.run/aws/Config"; |
| 55 | * |
| 56 | * const authorization = yield* Config.AggregationAuthorization( |
| 57 | * "OrgAggregator", |
| 58 | * { |
| 59 | * authorizedAccountId: "123456789012", |
| 60 | * authorizedAwsRegion: "us-east-1", |
| 61 | * }, |
| 62 | * ); |
| 63 | * ``` |
| 64 | * |
| 65 | * @resource |
| 66 | */ |
| 67 | export const AggregationAuthorization = Resource<AggregationAuthorization>( |
| 68 | "AWS.Config.AggregationAuthorization", |
| 69 | ); |
| 70 | |
| 71 | export const AggregationAuthorizationProvider = () => |
| 72 | Provider.effect( |
| 73 | AggregationAuthorization, |
| 74 | Effect.gen(function* () { |
| 75 | // There is no Get API — enumerate and match on the (account, region) |
| 76 | // identity pair. |
| 77 | const observeAuthorization = Effect.fn(function* (accountId: string, region: string) { |
| 78 | return yield* config.describeAggregationAuthorizations.items({}).pipe( |
| 79 | Stream.filter( |
| 80 | (auth) => auth.AuthorizedAccountId === accountId && auth.AuthorizedAwsRegion === region, |
| 81 | ), |
| 82 | Stream.runHead, |
| 83 | Effect.map((head) => (head._tag === "Some" ? head.value : undefined)), |
| 84 | ); |
| 85 | }); |
| 86 | |
| 87 | const observedTags = (arn: string) => |
| 88 | config.listTagsForResource({ ResourceArn: arn }).pipe( |
| 89 | Effect.map((r) => |
| 90 | Object.fromEntries( |
| 91 | (r.Tags ?? []).flatMap((t) => (t.Key !== undefined ? [[t.Key, t.Value ?? ""]] : [])), |
| 92 | ), |
| 93 | ), |
| 94 | Effect.catchTag("ResourceNotFoundException", () => |
| 95 | Effect.succeed({} as Record<string, string>), |
| 96 | ), |
| 97 | ); |
| 98 | |
| 99 | const toAttrs = (auth: config.AggregationAuthorization) => ({ |
| 100 | aggregationAuthorizationArn: auth.AggregationAuthorizationArn!, |
| 101 | authorizedAccountId: auth.AuthorizedAccountId!, |
| 102 | authorizedAwsRegion: auth.AuthorizedAwsRegion!, |
| 103 | }); |
| 104 | |
| 105 | return AggregationAuthorization.Provider.of({ |
| 106 | stables: ["aggregationAuthorizationArn", "authorizedAccountId", "authorizedAwsRegion"], |
| 107 | list: () => |
| 108 | config.describeAggregationAuthorizations.items({}).pipe( |
| 109 | Stream.runCollect, |
| 110 | Effect.map((auths) => |
| 111 | Array.from(auths).flatMap((auth) => |
| 112 | auth.AggregationAuthorizationArn && |
| 113 | auth.AuthorizedAccountId && |
| 114 | auth.AuthorizedAwsRegion |
| 115 | ? [toAttrs(auth)] |
| 116 | : [], |
| 117 | ), |
| 118 | ), |
| 119 | ), |
| 120 | read: Effect.fn(function* ({ id, olds, output }) { |
| 121 | const accountId = output?.authorizedAccountId ?? olds?.authorizedAccountId; |
| 122 | const region = output?.authorizedAwsRegion ?? olds?.authorizedAwsRegion; |
| 123 | if (accountId === undefined || region === undefined) { |
| 124 | return undefined; |
| 125 | } |
| 126 | const auth = yield* observeAuthorization(accountId, region); |
| 127 | if (auth?.AggregationAuthorizationArn === undefined) { |
| 128 | return undefined; |
| 129 | } |
| 130 | const attrs = toAttrs(auth); |
| 131 | const tags = yield* observedTags(auth.AggregationAuthorizationArn); |
| 132 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 133 | }), |
| 134 | // The (account, region) pair IS the resource identity — any change |
| 135 | // replaces it. Tags remain updatable in place. |
| 136 | diff: Effect.fn(function* ({ news, olds }) { |
| 137 | if (!isResolved(news)) return undefined; |
| 138 | if ( |
| 139 | olds.authorizedAccountId !== news.authorizedAccountId || |
| 140 | olds.authorizedAwsRegion !== news.authorizedAwsRegion |
| 141 | ) { |
| 142 | return { action: "replace" } as const; |
| 143 | } |
| 144 | }), |
| 145 | reconcile: Effect.fn(function* ({ id, news, session }) { |
| 146 | const internalTags = yield* createInternalTags(id); |
| 147 | const desiredTags = { ...news.tags, ...internalTags }; |
| 148 | |
| 149 | // 1. OBSERVE — cloud state is authoritative. |
| 150 | let live = yield* observeAuthorization( |
| 151 | news.authorizedAccountId, |
| 152 | news.authorizedAwsRegion, |
| 153 | ); |
| 154 | |
| 155 | // 2. ENSURE — PutAggregationAuthorization is idempotent: it |
| 156 | // creates when missing and is a no-op when present (create-time |
| 157 | // tags on an existing authorization are ignored, so tags are |
| 158 | // synced separately below). |
| 159 | if (live === undefined) { |
| 160 | live = yield* config |
| 161 | .putAggregationAuthorization({ |
| 162 | AuthorizedAccountId: news.authorizedAccountId, |
| 163 | AuthorizedAwsRegion: news.authorizedAwsRegion, |
| 164 | Tags: createTagsList(desiredTags), |
| 165 | }) |
| 166 | .pipe(Effect.map((r) => r.AggregationAuthorization)); |
| 167 | } |
| 168 | |
| 169 | const arn = live?.AggregationAuthorizationArn; |
| 170 | if (arn === undefined) { |
| 171 | // Extremely unlikely (Put returns the authorization) — fall back |
| 172 | // to a fresh observation so the attrs are never fabricated. |
| 173 | live = yield* observeAuthorization(news.authorizedAccountId, news.authorizedAwsRegion); |
| 174 | } |
| 175 | |
| 176 | // 3. SYNC TAGS — diff against OBSERVED cloud tags so adoption |
| 177 | // converges. |
| 178 | const finalArn = live?.AggregationAuthorizationArn; |
| 179 | if (finalArn !== undefined) { |
| 180 | const currentTags = yield* observedTags(finalArn); |
| 181 | const { upsert, removed } = diffTags(currentTags, desiredTags); |
| 182 | if (upsert.length > 0) { |
| 183 | yield* config.tagResource({ |
| 184 | ResourceArn: finalArn, |
| 185 | Tags: upsert, |
| 186 | }); |
| 187 | } |
| 188 | if (removed.length > 0) { |
| 189 | yield* config.untagResource({ |
| 190 | ResourceArn: finalArn, |
| 191 | TagKeys: removed, |
| 192 | }); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | yield* session.note(`${news.authorizedAccountId}/${news.authorizedAwsRegion}`); |
| 197 | return { |
| 198 | aggregationAuthorizationArn: finalArn!, |
| 199 | authorizedAccountId: news.authorizedAccountId, |
| 200 | authorizedAwsRegion: news.authorizedAwsRegion, |
| 201 | }; |
| 202 | }), |
| 203 | // DeleteAggregationAuthorization succeeds whether or not the |
| 204 | // authorization still exists — idempotent by design. |
| 205 | delete: Effect.fn(function* ({ output }) { |
| 206 | yield* config.deleteAggregationAuthorization({ |
| 207 | AuthorizedAccountId: output.authorizedAccountId, |
| 208 | AuthorizedAwsRegion: output.authorizedAwsRegion, |
| 209 | }); |
| 210 | }), |
| 211 | }); |
| 212 | }), |
| 213 | ); |