The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as lf from "@distilled.cloud/aws/lakeformation"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Schedule from "effect/Schedule"; |
| 4 | import * as Stream from "effect/Stream"; |
| 5 | |
| 6 | /** |
| 7 | * Bounded retry through `ConcurrentModificationException` — Lake Formation |
| 8 | * serializes grant/revoke/settings mutations and rejects concurrent writers. |
| 9 | * Explicitly typed so declaration emit does not widen the provider layer (see |
| 10 | * PATTERNS §7 on inlined `Effect.retry`). |
| 11 | */ |
| 12 | export const retryWhileConcurrentModification = <A, E extends { _tag: string }, R>( |
| 13 | self: Effect.Effect<A, E, R>, |
| 14 | ): Effect.Effect<A, E, R> => |
| 15 | Effect.retry(self, { |
| 16 | while: (e) => e._tag === "ConcurrentModificationException", |
| 17 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]), |
| 18 | }); |
| 19 | |
| 20 | /** |
| 21 | * Bounded retry through `InvalidLakeFormationPrincipal` — a freshly-created |
| 22 | * IAM role/user takes ~10s to propagate before Lake Formation accepts it as a |
| 23 | * principal ("Invalid principal, arn: ..." surfaced as a typed synthetic |
| 24 | * error via the distilled patch). Explicitly typed for the same |
| 25 | * declaration-emit reason as above. |
| 26 | */ |
| 27 | export const retryWhileInvalidPrincipal = <A, E extends { _tag: string }, R>( |
| 28 | self: Effect.Effect<A, E, R>, |
| 29 | ): Effect.Effect<A, E, R> => |
| 30 | Effect.retry(self, { |
| 31 | while: (e) => e._tag === "InvalidLakeFormationPrincipal", |
| 32 | schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(10)]), |
| 33 | }); |
| 34 | |
| 35 | /** |
| 36 | * Observe the permissions a specific principal holds on a resource. |
| 37 | * |
| 38 | * IMPORTANT: this deliberately lists by `Resource` only and filters entries |
| 39 | * client-side by exact principal identifier. Listing with a `Principal` |
| 40 | * filter makes Lake Formation fold the `IAM_ALLOWED_PRINCIPALS` group grant |
| 41 | * (typically `ALL`) into the principal's effective permissions, which would |
| 42 | * make the reconciler believe `ALL` was granted directly (verified live). |
| 43 | */ |
| 44 | export const observePrincipalPermissions = Effect.fn( |
| 45 | "AWS.LakeFormation.observePrincipalPermissions", |
| 46 | )(function* (principal: string, resource: lf.Resource, catalogId: string | undefined) { |
| 47 | const pages = yield* lf.listPermissions |
| 48 | .pages({ CatalogId: catalogId, Resource: resource }) |
| 49 | .pipe(Stream.runCollect); |
| 50 | const entries = Array.from(pages) |
| 51 | .flatMap((page) => page.PrincipalResourcePermissions ?? []) |
| 52 | .filter((e) => e.Principal?.DataLakePrincipalIdentifier === principal); |
| 53 | const permissions = [...new Set(entries.flatMap((e) => e.Permissions ?? []))].sort(); |
| 54 | const permissionsWithGrantOption = [ |
| 55 | ...new Set(entries.flatMap((e) => e.PermissionsWithGrantOption ?? [])), |
| 56 | ].sort(); |
| 57 | return { permissions, permissionsWithGrantOption }; |
| 58 | }); |