The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as eventbridge from "@distilled.cloud/aws/eventbridge"; |
| 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 Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import type { Providers } from "../Providers.ts"; |
| 9 | |
| 10 | export interface PermissionProps { |
| 11 | /** |
| 12 | * Event bus name. Defaults to the account default bus. |
| 13 | */ |
| 14 | eventBusName?: string; |
| 15 | /** |
| 16 | * The action that EventBridge allows for the principal. |
| 17 | * @default "events:PutEvents" |
| 18 | */ |
| 19 | action?: string; |
| 20 | /** |
| 21 | * The AWS account ID, organization ID, or `*` principal receiving access. |
| 22 | */ |
| 23 | principal: string; |
| 24 | /** |
| 25 | * Optional statement identifier. If omitted, Alchemy generates one. |
| 26 | */ |
| 27 | statementId?: string; |
| 28 | /** |
| 29 | * Optional condition limiting the allowed caller. |
| 30 | */ |
| 31 | condition?: eventbridge.Condition; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * An EventBridge event bus permission statement. |
| 36 | * |
| 37 | * `Permission` manages a single `PutPermission` / `RemovePermission` lifecycle |
| 38 | * entry on an event bus so helper surfaces can safely grant publishers access |
| 39 | * without requiring callers to hand-write raw bus policies. |
| 40 | * ### Granting Access |
| 41 | * **Example:** Allow Another Account To Publish |
| 42 | * ```typescript |
| 43 | * const permission = yield* Permission("PartnerPublish", { |
| 44 | * eventBusName: bus.eventBusName, |
| 45 | * principal: "123456789012", |
| 46 | * }); |
| 47 | * ``` |
| 48 | * |
| 49 | * @resource |
| 50 | */ |
| 51 | export interface Permission extends Resource< |
| 52 | "AWS.EventBridge.Permission", |
| 53 | PermissionProps, |
| 54 | { |
| 55 | /** The statement ID identifying the permission on the bus policy. */ |
| 56 | statementId: string; |
| 57 | /** The name of the event bus the permission is attached to. */ |
| 58 | eventBusName: string; |
| 59 | }, |
| 60 | never, |
| 61 | Providers |
| 62 | > {} |
| 63 | |
| 64 | export const Permission = Resource<Permission>("AWS.EventBridge.Permission"); |
| 65 | |
| 66 | /** An event bus resource policy is not valid JSON. */ |
| 67 | export class InvalidEventBusPolicy extends Data.TaggedError("InvalidEventBusPolicy")<{ |
| 68 | message: string; |
| 69 | cause?: unknown; |
| 70 | }> {} |
| 71 | |
| 72 | export const PermissionProvider = () => |
| 73 | Provider.effect( |
| 74 | Permission, |
| 75 | Effect.gen(function* () { |
| 76 | const toStatementId = (id: string, props: PermissionProps) => |
| 77 | props.statementId |
| 78 | ? Effect.succeed(props.statementId) |
| 79 | : createPhysicalName({ |
| 80 | id, |
| 81 | maxLength: 64, |
| 82 | }); |
| 83 | |
| 84 | type PermissionAttrs = { statementId: string; eventBusName: string }; |
| 85 | |
| 86 | return { |
| 87 | stables: ["statementId", "eventBusName"], |
| 88 | list: () => |
| 89 | Effect.gen(function* () { |
| 90 | // EventBridge has no list-permissions API. A Permission is a |
| 91 | // single statement inside an event bus resource policy, so we |
| 92 | // enumerate every bus in the ambient account/region (manual |
| 93 | // NextToken pagination — listEventBuses is not a paginated |
| 94 | // distilled op), describe each bus to read its Policy JSON, and |
| 95 | // emit one Attributes per statement Sid. |
| 96 | const busNames: string[] = []; |
| 97 | let nextToken: string | undefined; |
| 98 | do { |
| 99 | const page = yield* eventbridge.listEventBuses({ |
| 100 | NextToken: nextToken, |
| 101 | }); |
| 102 | for (const bus of page.EventBuses ?? []) { |
| 103 | if (bus.Name) busNames.push(bus.Name); |
| 104 | } |
| 105 | nextToken = page.NextToken; |
| 106 | } while (nextToken); |
| 107 | |
| 108 | const perBus = yield* Effect.forEach( |
| 109 | busNames, |
| 110 | (busName) => |
| 111 | Effect.gen(function* () { |
| 112 | const res = yield* eventbridge.describeEventBus({ |
| 113 | Name: busName, |
| 114 | }); |
| 115 | if (!res.Policy) return [] as PermissionAttrs[]; |
| 116 | const policy = yield* Effect.try({ |
| 117 | try: () => |
| 118 | JSON.parse(res.Policy!) as { |
| 119 | Statement?: { Sid?: string } | { Sid?: string }[]; |
| 120 | }, |
| 121 | catch: (cause) => |
| 122 | new InvalidEventBusPolicy({ message: "invalid policy", cause }), |
| 123 | }).pipe( |
| 124 | // A malformed/non-JSON policy yields no permissions |
| 125 | // rather than failing the whole enumeration. |
| 126 | Effect.orElseSucceed(() => ({ |
| 127 | Statement: [] as { Sid?: string }[], |
| 128 | })), |
| 129 | ); |
| 130 | const statements = Array.isArray(policy.Statement) |
| 131 | ? policy.Statement |
| 132 | : policy.Statement |
| 133 | ? [policy.Statement] |
| 134 | : []; |
| 135 | return statements |
| 136 | .filter((s): s is { Sid: string } => typeof s.Sid === "string") |
| 137 | .map((s): PermissionAttrs => ({ |
| 138 | statementId: s.Sid, |
| 139 | eventBusName: busName, |
| 140 | })); |
| 141 | }).pipe( |
| 142 | // Bus removed out of band between list and describe — skip. |
| 143 | Effect.catchTag("ResourceNotFoundException", () => |
| 144 | Effect.succeed([] as PermissionAttrs[]), |
| 145 | ), |
| 146 | ), |
| 147 | { concurrency: 10 }, |
| 148 | ); |
| 149 | return perBus.flat(); |
| 150 | }), |
| 151 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 152 | if (!isResolved(news)) return; |
| 153 | const oldStatementId = yield* toStatementId(id, olds); |
| 154 | const newStatementId = yield* toStatementId(id, news); |
| 155 | |
| 156 | if (oldStatementId !== newStatementId) { |
| 157 | return { action: "replace" } as const; |
| 158 | } |
| 159 | |
| 160 | if ((olds.eventBusName ?? "default") !== (news.eventBusName ?? "default")) { |
| 161 | return { action: "replace" } as const; |
| 162 | } |
| 163 | }), |
| 164 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 165 | const statementId = output?.statementId ?? (yield* toStatementId(id, news)); |
| 166 | const eventBusName = output?.eventBusName ?? news.eventBusName ?? "default"; |
| 167 | const eventBusParam = eventBusName !== "default" ? eventBusName : undefined; |
| 168 | |
| 169 | // Observe + Ensure for an existence-only resource: there is no |
| 170 | // server-side `describePermission` for a single statement, so we |
| 171 | // remove-then-put unconditionally. `removePermission` tolerates |
| 172 | // missing statements; `putPermission` is idempotent on identical |
| 173 | // params and overwrites on different ones — this is the official |
| 174 | // recommended update path. |
| 175 | yield* eventbridge |
| 176 | .removePermission({ |
| 177 | EventBusName: eventBusParam, |
| 178 | StatementId: statementId, |
| 179 | }) |
| 180 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 181 | |
| 182 | yield* eventbridge.putPermission({ |
| 183 | EventBusName: eventBusParam, |
| 184 | Action: news.action ?? "events:PutEvents", |
| 185 | Principal: news.principal, |
| 186 | StatementId: statementId, |
| 187 | Condition: news.condition, |
| 188 | }); |
| 189 | |
| 190 | yield* session.note(`EventBridge permission ${statementId}`); |
| 191 | |
| 192 | return { |
| 193 | statementId, |
| 194 | eventBusName, |
| 195 | }; |
| 196 | }), |
| 197 | delete: Effect.fn(function* ({ output }) { |
| 198 | yield* eventbridge |
| 199 | .removePermission({ |
| 200 | EventBusName: output.eventBusName !== "default" ? output.eventBusName : undefined, |
| 201 | StatementId: output.statementId, |
| 202 | }) |
| 203 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 204 | }), |
| 205 | }; |
| 206 | }), |
| 207 | ); |