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 lf from "@distilled.cloud/aws/lakeformation"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Stream from "effect/Stream"; |
| 7 | import type { Providers } from "../Providers.ts"; |
| 8 | import { retryWhileInvalidPrincipal } from "./internal.ts"; |
| 9 | import { type LakeFormationResourceSpec, toWireResource } from "./ResourceSpec.ts"; |
| 10 | |
| 11 | export interface OptInProps { |
| 12 | /** |
| 13 | * The principal (IAM user/role ARN or `IAM_ALLOWED_PRINCIPALS`) whose |
| 14 | * access to the resource is opted into Lake Formation enforcement. |
| 15 | * Changing it replaces the opt-in. |
| 16 | */ |
| 17 | principal: string; |
| 18 | /** |
| 19 | * The Data Catalog resource (database, table, …) to enforce Lake |
| 20 | * Formation permissions on for the principal. Changing it replaces the |
| 21 | * opt-in. |
| 22 | */ |
| 23 | resource: LakeFormationResourceSpec; |
| 24 | } |
| 25 | |
| 26 | export interface OptIn extends Resource< |
| 27 | "AWS.LakeFormation.OptIn", |
| 28 | OptInProps, |
| 29 | { |
| 30 | principal: string; |
| 31 | resource: lf.Resource; |
| 32 | }, |
| 33 | {}, |
| 34 | Providers |
| 35 | > {} |
| 36 | |
| 37 | /** |
| 38 | * A Lake Formation opt-in — enforces Lake Formation permissions for one |
| 39 | * principal on one Data Catalog resource while the account is in hybrid |
| 40 | * access mode (where IAM/S3 policies otherwise govern access). |
| 41 | * |
| 42 | * ### Opting Into Lake Formation Enforcement |
| 43 | * **Example:** Enforce Lake Formation for a Role on a Database |
| 44 | * ```typescript |
| 45 | * import * as AWS from "@alchemy.run/aws"; |
| 46 | * |
| 47 | * const optIn = yield* AWS.LakeFormation.OptIn("AnalystOptIn", { |
| 48 | * principal: analystRole.roleArn, |
| 49 | * resource: { database: { name: database.databaseName } }, |
| 50 | * }); |
| 51 | * ``` |
| 52 | * |
| 53 | * @resource |
| 54 | */ |
| 55 | export const OptIn = Resource<OptIn>("AWS.LakeFormation.OptIn"); |
| 56 | |
| 57 | export const OptInProvider = () => |
| 58 | Provider.effect( |
| 59 | OptIn, |
| 60 | Effect.gen(function* () { |
| 61 | const observe = Effect.fn(function* (principal: string, resource: lf.Resource) { |
| 62 | const pages = yield* lf.listLakeFormationOptIns |
| 63 | .pages({ |
| 64 | Principal: { DataLakePrincipalIdentifier: principal }, |
| 65 | Resource: resource, |
| 66 | }) |
| 67 | .pipe( |
| 68 | Stream.runCollect, |
| 69 | // a deleted resource (e.g. the Glue database is already gone) |
| 70 | // means the opt-in no longer exists either; a deleted/not-yet- |
| 71 | // propagated IAM principal likewise has no visible opt-ins |
| 72 | Effect.catchTag(["EntityNotFoundException", "InvalidLakeFormationPrincipal"], () => |
| 73 | Effect.succeed([]), |
| 74 | ), |
| 75 | ); |
| 76 | return Array.from(pages) |
| 77 | .flatMap((page) => page.LakeFormationOptInsInfoList ?? []) |
| 78 | .find((info) => info.Principal?.DataLakePrincipalIdentifier === principal); |
| 79 | }); |
| 80 | |
| 81 | return OptIn.Provider.of({ |
| 82 | stables: ["principal", "resource"], |
| 83 | |
| 84 | list: () => |
| 85 | Effect.gen(function* () { |
| 86 | const pages = yield* lf.listLakeFormationOptIns.pages({}).pipe(Stream.runCollect); |
| 87 | return Array.from(pages) |
| 88 | .flatMap((page) => page.LakeFormationOptInsInfoList ?? []) |
| 89 | .filter( |
| 90 | (info) => |
| 91 | info.Principal?.DataLakePrincipalIdentifier !== undefined && |
| 92 | info.Resource !== undefined, |
| 93 | ) |
| 94 | .map((info) => ({ |
| 95 | principal: info.Principal!.DataLakePrincipalIdentifier!, |
| 96 | resource: info.Resource!, |
| 97 | })); |
| 98 | }), |
| 99 | |
| 100 | read: Effect.fn(function* ({ olds, output }) { |
| 101 | const principal = output?.principal ?? olds?.principal; |
| 102 | const resource = |
| 103 | output?.resource ?? (olds !== undefined ? toWireResource(olds.resource) : undefined); |
| 104 | if (principal === undefined || resource === undefined) { |
| 105 | return undefined; |
| 106 | } |
| 107 | const found = yield* observe(principal, resource); |
| 108 | if (found === undefined) return undefined; |
| 109 | // Opt-ins are not taggable — ownership cannot be verified. |
| 110 | return { principal, resource }; |
| 111 | }), |
| 112 | |
| 113 | diff: Effect.fn(function* ({ news, olds }) { |
| 114 | if (!isResolved(news)) return undefined; |
| 115 | if ( |
| 116 | news.principal !== olds.principal || |
| 117 | JSON.stringify(toWireResource(news.resource)) !== |
| 118 | JSON.stringify(toWireResource(olds.resource)) |
| 119 | ) { |
| 120 | // an opt-in has no mutable body — any change replaces it |
| 121 | return { action: "replace" } as const; |
| 122 | } |
| 123 | }), |
| 124 | |
| 125 | reconcile: Effect.fn(function* ({ news, session }) { |
| 126 | const resource = toWireResource(news.resource); |
| 127 | |
| 128 | // 1. OBSERVE |
| 129 | const found = yield* observe(news.principal, resource); |
| 130 | |
| 131 | // 2. ENSURE — existence-only resource, nothing to sync. A freshly |
| 132 | // created IAM principal takes ~10s to propagate before Lake |
| 133 | // Formation accepts it, so retry through the typed rejection. |
| 134 | if (found === undefined) { |
| 135 | yield* lf |
| 136 | .createLakeFormationOptIn({ |
| 137 | Principal: { DataLakePrincipalIdentifier: news.principal }, |
| 138 | Resource: resource, |
| 139 | }) |
| 140 | .pipe(retryWhileInvalidPrincipal); |
| 141 | } |
| 142 | |
| 143 | yield* session.note(news.principal); |
| 144 | return { principal: news.principal, resource }; |
| 145 | }), |
| 146 | |
| 147 | delete: Effect.fn(function* ({ output }) { |
| 148 | yield* lf |
| 149 | .deleteLakeFormationOptIn({ |
| 150 | Principal: { |
| 151 | DataLakePrincipalIdentifier: output.principal, |
| 152 | }, |
| 153 | Resource: output.resource, |
| 154 | }) |
| 155 | .pipe( |
| 156 | // the opt-in (or its principal) is already gone |
| 157 | Effect.catchTag( |
| 158 | ["EntityNotFoundException", "InvalidLakeFormationPrincipal"], |
| 159 | () => Effect.void, |
| 160 | ), |
| 161 | ); |
| 162 | }), |
| 163 | }); |
| 164 | }), |
| 165 | ); |