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 osis from "@distilled.cloud/aws/osis"; |
| 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 type { Providers } from "../Providers.ts"; |
| 7 | |
| 8 | export interface ResourcePolicyProps { |
| 9 | /** |
| 10 | * ARN of the OSIS resource (pipeline) the policy is attached to. A |
| 11 | * resource has at most one resource-based policy. Changing the resource |
| 12 | * replaces the policy. |
| 13 | */ |
| 14 | resourceArn: string; |
| 15 | /** |
| 16 | * The IAM resource-based policy document (JSON) that grants other |
| 17 | * accounts access to the resource. OSIS only accepts |
| 18 | * `osis:CreatePipelineEndpoint` grants whose principals are 12-digit |
| 19 | * account IDs (ARN principals are rejected as "Invalid resource policy."). |
| 20 | * Updated in place. |
| 21 | */ |
| 22 | policy: string; |
| 23 | } |
| 24 | |
| 25 | export interface ResourcePolicy extends Resource< |
| 26 | "AWS.OSIS.ResourcePolicy", |
| 27 | ResourcePolicyProps, |
| 28 | { |
| 29 | /** |
| 30 | * ARN of the resource the policy is attached to. |
| 31 | */ |
| 32 | resourceArn: string; |
| 33 | /** |
| 34 | * The attached policy document (JSON). |
| 35 | */ |
| 36 | policy: string; |
| 37 | }, |
| 38 | never, |
| 39 | Providers |
| 40 | > {} |
| 41 | |
| 42 | /** |
| 43 | * The resource-based policy of an Amazon OpenSearch Ingestion (OSIS) |
| 44 | * VPC pipeline — grants other accounts `osis:CreatePipelineEndpoint` so they |
| 45 | * can attach a pipeline endpoint from their own VPC (cross-account |
| 46 | * ingestion). Principals must be 12-digit account IDs, not ARNs. A resource |
| 47 | * has at most one. |
| 48 | * |
| 49 | * ### Creating a Resource Policy |
| 50 | * **Example:** Allow Another Account to Attach a Pipeline Endpoint |
| 51 | * ```typescript |
| 52 | * const policy = yield* OSIS.ResourcePolicy("CrossAccountEndpoint", { |
| 53 | * resourceArn: pipeline.pipelineArn, |
| 54 | * policy: Output.interpolate`{ |
| 55 | * "Version": "2012-10-17", |
| 56 | * "Statement": [ |
| 57 | * { |
| 58 | * "Effect": "Allow", |
| 59 | * "Principal": { "AWS": ["123456789012"] }, |
| 60 | * "Action": "osis:CreatePipelineEndpoint", |
| 61 | * "Resource": "${pipeline.pipelineArn}" |
| 62 | * } |
| 63 | * ] |
| 64 | * }`, |
| 65 | * }); |
| 66 | * ``` |
| 67 | * |
| 68 | * @resource |
| 69 | */ |
| 70 | export const ResourcePolicy = Resource<ResourcePolicy>("AWS.OSIS.ResourcePolicy"); |
| 71 | |
| 72 | /** Order-insensitive canonical form of a JSON policy document. */ |
| 73 | const canonicalJson = (document: string): string => { |
| 74 | const sort = (value: unknown): unknown => |
| 75 | Array.isArray(value) |
| 76 | ? value.map(sort) |
| 77 | : value !== null && typeof value === "object" |
| 78 | ? Object.fromEntries( |
| 79 | Object.entries(value as Record<string, unknown>) |
| 80 | .sort(([a], [b]) => a.localeCompare(b)) |
| 81 | .map(([k, v]) => [k, sort(v)]), |
| 82 | ) |
| 83 | : value; |
| 84 | try { |
| 85 | return JSON.stringify(sort(JSON.parse(document))); |
| 86 | } catch { |
| 87 | return document; |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | export const ResourcePolicyProvider = () => |
| 92 | Provider.effect( |
| 93 | ResourcePolicy, |
| 94 | Effect.gen(function* () { |
| 95 | /** |
| 96 | * Read the attached policy. OSIS reports "no policy" two ways: a typed |
| 97 | * `ResourceNotFoundException`, or (observed live) a success response |
| 98 | * whose `Policy` is the empty document `"{}"` — normalize both to |
| 99 | * `undefined`. |
| 100 | */ |
| 101 | const getPolicy = Effect.fn(function* (resourceArn: string) { |
| 102 | const policy = yield* osis.getResourcePolicy({ ResourceArn: resourceArn }).pipe( |
| 103 | Effect.map((response) => response.Policy), |
| 104 | Effect.catchTag("ResourceNotFoundException", () => Effect.undefined), |
| 105 | ); |
| 106 | return policy === undefined || canonicalJson(policy) === "{}" ? undefined : policy; |
| 107 | }); |
| 108 | |
| 109 | return { |
| 110 | stables: ["resourceArn"], |
| 111 | |
| 112 | diff: Effect.fn(function* ({ olds, news }) { |
| 113 | if (!isResolved(news)) return undefined; |
| 114 | if (olds?.resourceArn !== news.resourceArn) { |
| 115 | return { action: "replace" } as const; |
| 116 | } |
| 117 | }), |
| 118 | |
| 119 | read: Effect.fn(function* ({ olds, output }) { |
| 120 | const resourceArn = output?.resourceArn ?? olds?.resourceArn; |
| 121 | if (!resourceArn) return undefined; |
| 122 | const policy = yield* getPolicy(resourceArn); |
| 123 | if (policy === undefined) return undefined; |
| 124 | // Resource policies are not taggable — ownership is implied by |
| 125 | // the owned parent pipeline. |
| 126 | return { resourceArn, policy }; |
| 127 | }), |
| 128 | |
| 129 | reconcile: Effect.fn(function* ({ news, session }) { |
| 130 | const resourceArn = news!.resourceArn; |
| 131 | |
| 132 | // 1. Observe — the live policy is authoritative. |
| 133 | const observed = yield* getPolicy(resourceArn); |
| 134 | |
| 135 | // 2/3. Ensure + sync — `putResourcePolicy` is a true upsert; |
| 136 | // call it only when the canonical document drifts. |
| 137 | if (observed === undefined || canonicalJson(observed) !== canonicalJson(news!.policy)) { |
| 138 | yield* osis.putResourcePolicy({ |
| 139 | ResourceArn: resourceArn, |
| 140 | Policy: news!.policy, |
| 141 | }); |
| 142 | } |
| 143 | |
| 144 | yield* session.note(resourceArn); |
| 145 | return { resourceArn, policy: news!.policy }; |
| 146 | }), |
| 147 | |
| 148 | delete: Effect.fn(function* ({ output }) { |
| 149 | yield* osis |
| 150 | .deleteResourcePolicy({ ResourceArn: output.resourceArn }) |
| 151 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 152 | }), |
| 153 | |
| 154 | // Singleton sub-resource keyed by its parent resource ARN. |
| 155 | list: () => Effect.succeed([]), |
| 156 | }; |
| 157 | }), |
| 158 | ); |