The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as ecr from "@distilled.cloud/aws/ecr"; |
| 2 | import * as Provider from "alchemy/Provider"; |
| 3 | import { Resource } from "alchemy/Resource"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { AWSEnvironment } from "../Environment.ts"; |
| 6 | import type { PolicyDocument } from "../IAM/Policy.ts"; |
| 7 | import { normalizePolicyDocument, stringifyPolicyDocument } from "../IAM/Policy.ts"; |
| 8 | import type { Providers } from "../Providers.ts"; |
| 9 | |
| 10 | export interface RegistryPolicyProps { |
| 11 | /** |
| 12 | * The registry permissions policy — grants other AWS accounts registry |
| 13 | * level permissions such as `ecr:ReplicateImage` (cross-account |
| 14 | * replication) or `ecr:CreateRepository`. Accepts either a structured IAM |
| 15 | * {@link PolicyDocument} or a raw JSON string (escape hatch / adoption of |
| 16 | * an existing policy). |
| 17 | */ |
| 18 | policy: PolicyDocument | string; |
| 19 | } |
| 20 | |
| 21 | export interface RegistryPolicy extends Resource< |
| 22 | "AWS.ECR.RegistryPolicy", |
| 23 | RegistryPolicyProps, |
| 24 | { |
| 25 | /** The AWS account ID of the registry. */ |
| 26 | registryId: string; |
| 27 | /** The JSON registry permissions policy as stored by ECR. */ |
| 28 | policy: string; |
| 29 | }, |
| 30 | never, |
| 31 | Providers |
| 32 | > {} |
| 33 | |
| 34 | /** |
| 35 | * The permissions policy for a private Amazon ECR registry — an |
| 36 | * account/region **singleton** used to grant other AWS accounts |
| 37 | * registry-level permissions (most commonly `ecr:ReplicateImage` when |
| 38 | * configuring cross-account replication). |
| 39 | * ### Managing the Registry Policy |
| 40 | * **Example:** Allow Cross-Account Replication |
| 41 | * ```typescript |
| 42 | * const policy = yield* RegistryPolicy("ReplicationPolicy", { |
| 43 | * policy: { |
| 44 | * Version: "2012-10-17", |
| 45 | * Statement: [ |
| 46 | * { |
| 47 | * Sid: "AllowReplication", |
| 48 | * Effect: "Allow", |
| 49 | * Principal: { AWS: `arn:aws:iam::${sourceAccountId}:root` }, |
| 50 | * Action: ["ecr:ReplicateImage"], |
| 51 | * Resource: `arn:aws:ecr:us-east-1:${accountId}:repository/*`, |
| 52 | * }, |
| 53 | * ], |
| 54 | * }, |
| 55 | * }); |
| 56 | * ``` |
| 57 | * |
| 58 | * @resource |
| 59 | */ |
| 60 | export const RegistryPolicy = Resource<RegistryPolicy>("AWS.ECR.RegistryPolicy"); |
| 61 | |
| 62 | export const RegistryPolicyProvider = () => |
| 63 | Provider.effect( |
| 64 | RegistryPolicy, |
| 65 | Effect.gen(function* () { |
| 66 | const readPolicy = ecr |
| 67 | .getRegistryPolicy({}) |
| 68 | .pipe(Effect.catchTag("RegistryPolicyNotFoundException", () => Effect.undefined)); |
| 69 | |
| 70 | const toPolicyText = (policy: PolicyDocument | string) => |
| 71 | typeof policy === "string" ? policy : stringifyPolicyDocument(policy); |
| 72 | |
| 73 | return { |
| 74 | stables: ["registryId"], |
| 75 | read: Effect.fn(function* () { |
| 76 | const observed = yield* readPolicy; |
| 77 | if (observed?.policyText === undefined) return undefined; |
| 78 | return { |
| 79 | registryId: observed.registryId ?? (yield* AWSEnvironment.current).accountId, |
| 80 | policy: observed.policyText, |
| 81 | }; |
| 82 | }), |
| 83 | // The registry policy is an account/region singleton: at most one |
| 84 | // exists, keyed on the caller's registry. |
| 85 | list: () => |
| 86 | Effect.gen(function* () { |
| 87 | const observed = yield* readPolicy; |
| 88 | if (observed?.policyText === undefined) return []; |
| 89 | return [ |
| 90 | { |
| 91 | registryId: observed.registryId ?? (yield* AWSEnvironment.current).accountId, |
| 92 | policy: observed.policyText, |
| 93 | }, |
| 94 | ]; |
| 95 | }), |
| 96 | reconcile: Effect.fn(function* ({ news, session }) { |
| 97 | const desired = toPolicyText(news.policy); |
| 98 | |
| 99 | // Observe — compare the live policy against the desired one via |
| 100 | // `normalizePolicyDocument` (key order / whitespace insensitive) |
| 101 | // so a re-deploy of an equivalent document is a no-op. |
| 102 | const observed = yield* readPolicy; |
| 103 | let registryId = observed?.registryId; |
| 104 | if ( |
| 105 | observed?.policyText === undefined || |
| 106 | normalizePolicyDocument(observed.policyText) !== normalizePolicyDocument(desired) |
| 107 | ) { |
| 108 | const put = yield* ecr.putRegistryPolicy({ policyText: desired }); |
| 109 | registryId = put.registryId ?? registryId; |
| 110 | } |
| 111 | registryId ??= (yield* AWSEnvironment.current).accountId; |
| 112 | |
| 113 | yield* session.note(registryId); |
| 114 | return { registryId, policy: desired }; |
| 115 | }), |
| 116 | delete: Effect.fn(function* () { |
| 117 | yield* ecr |
| 118 | .deleteRegistryPolicy({}) |
| 119 | .pipe(Effect.catchTag("RegistryPolicyNotFoundException", () => Effect.void)); |
| 120 | }), |
| 121 | }; |
| 122 | }), |
| 123 | ); |