The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as avp from "@distilled.cloud/aws/verifiedpermissions"; |
| 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 * as Stream from "effect/Stream"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | |
| 11 | export class PolicyStoreAliasUnexpectedResponse extends Data.TaggedError( |
| 12 | "PolicyStoreAliasUnexpectedResponse", |
| 13 | )<{ |
| 14 | readonly message: string; |
| 15 | readonly cause?: unknown; |
| 16 | }> {} |
| 17 | |
| 18 | export interface PolicyStoreAliasProps { |
| 19 | /** |
| 20 | * The ID of the policy store the alias points at. There is no update API |
| 21 | * for aliases — changing the store replaces the alias. |
| 22 | */ |
| 23 | policyStoreId: string; |
| 24 | /** |
| 25 | * Name of the alias. AWS requires every alias name to start with |
| 26 | * `policy-store-alias/`; the prefix is added automatically when omitted, |
| 27 | * so `"photo-app-prod"` and `"policy-store-alias/photo-app-prod"` name the |
| 28 | * same alias. The full name may be at most 150 characters of letters, |
| 29 | * digits, `-`, `_`, and `/`, and must be unique per account and region. |
| 30 | * If omitted, a unique name is generated from the app, stage, and logical |
| 31 | * ID. Changing the name replaces the alias. |
| 32 | */ |
| 33 | aliasName?: string; |
| 34 | /** |
| 35 | * How the alias is deleted when the resource is destroyed. `SoftDelete` |
| 36 | * keeps the alias name reserved in `PendingDeletion` state for a recovery |
| 37 | * window; `HardDelete` frees the name immediately. |
| 38 | * @default "HardDelete" |
| 39 | */ |
| 40 | deletionMode?: "SoftDelete" | "HardDelete"; |
| 41 | } |
| 42 | |
| 43 | export interface PolicyStoreAlias extends Resource< |
| 44 | "AWS.VerifiedPermissions.PolicyStoreAlias", |
| 45 | PolicyStoreAliasProps, |
| 46 | { |
| 47 | /** |
| 48 | * Full name of the alias, including the `policy-store-alias/` prefix — |
| 49 | * usable in place of a policy store ID in authorization requests. |
| 50 | */ |
| 51 | aliasName: string; |
| 52 | /** |
| 53 | * ID of the policy store the alias points at. |
| 54 | */ |
| 55 | policyStoreId: string; |
| 56 | /** |
| 57 | * ARN of the alias. |
| 58 | */ |
| 59 | aliasArn: string; |
| 60 | }, |
| 61 | {}, |
| 62 | Providers |
| 63 | > {} |
| 64 | |
| 65 | /** |
| 66 | * A named alias for a Verified Permissions policy store. Aliases let callers |
| 67 | * reference a policy store by a stable name (e.g. in `IsAuthorized` |
| 68 | * requests) so the underlying store can be swapped without reconfiguring |
| 69 | * clients. Alias names always carry the `policy-store-alias/` prefix AWS |
| 70 | * requires; it is added automatically to generated and user-supplied names. |
| 71 | * |
| 72 | * ### Creating an Alias |
| 73 | * **Example:** Alias with a Generated Name |
| 74 | * ```typescript |
| 75 | * import * as AWS from "@alchemy.run/aws"; |
| 76 | * |
| 77 | * const store = yield* AWS.VerifiedPermissions.PolicyStore("Store", {}); |
| 78 | * |
| 79 | * const alias = yield* AWS.VerifiedPermissions.PolicyStoreAlias("Alias", { |
| 80 | * policyStoreId: store.policyStoreId, |
| 81 | * }); |
| 82 | * ``` |
| 83 | * |
| 84 | * **Example:** Named Alias with Hard Delete |
| 85 | * ```typescript |
| 86 | * yield* AWS.VerifiedPermissions.PolicyStoreAlias("Alias", { |
| 87 | * policyStoreId: store.policyStoreId, |
| 88 | * // created as "policy-store-alias/photo-app-prod" |
| 89 | * aliasName: "photo-app-prod", |
| 90 | * deletionMode: "HardDelete", |
| 91 | * }); |
| 92 | * ``` |
| 93 | * |
| 94 | * @resource |
| 95 | */ |
| 96 | export const PolicyStoreAlias = Resource<PolicyStoreAlias>( |
| 97 | "AWS.VerifiedPermissions.PolicyStoreAlias", |
| 98 | ); |
| 99 | |
| 100 | /** |
| 101 | * Every alias name must carry this prefix — CreatePolicyStoreAlias, |
| 102 | * GetPolicyStoreAlias, and DeletePolicyStoreAlias reject bare names with a |
| 103 | * ValidationException. |
| 104 | */ |
| 105 | const ALIAS_PREFIX = "policy-store-alias/"; |
| 106 | |
| 107 | /** Smithy `Alias` shape: max length of the full (prefixed) name. */ |
| 108 | const ALIAS_MAX_LENGTH = 150; |
| 109 | |
| 110 | const withAliasPrefix = (aliasName: string) => |
| 111 | aliasName.startsWith(ALIAS_PREFIX) ? aliasName : `${ALIAS_PREFIX}${aliasName}`; |
| 112 | |
| 113 | const toAliasName = (id: string, props: { aliasName?: string } = {}) => |
| 114 | props.aliasName |
| 115 | ? Effect.succeed(withAliasPrefix(props.aliasName)) |
| 116 | : createPhysicalName({ |
| 117 | id, |
| 118 | maxLength: ALIAS_MAX_LENGTH - ALIAS_PREFIX.length, |
| 119 | lowercase: true, |
| 120 | }).pipe(Effect.map((name) => `${ALIAS_PREFIX}${name}`)); |
| 121 | |
| 122 | export const PolicyStoreAliasProvider = () => |
| 123 | Provider.effect( |
| 124 | PolicyStoreAlias, |
| 125 | Effect.gen(function* () { |
| 126 | const observe = Effect.fn(function* (aliasName: string) { |
| 127 | return yield* avp |
| 128 | .getPolicyStoreAlias({ aliasName }) |
| 129 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 130 | }); |
| 131 | |
| 132 | return PolicyStoreAlias.Provider.of({ |
| 133 | stables: ["aliasName", "policyStoreId", "aliasArn"], |
| 134 | |
| 135 | list: () => |
| 136 | Effect.gen(function* () { |
| 137 | const pages = yield* avp.listPolicyStoreAliases.pages({}).pipe(Stream.runCollect); |
| 138 | const items = Array.from(pages).flatMap((page) => page.policyStoreAliases ?? []); |
| 139 | return items.map((item) => ({ |
| 140 | aliasName: item.aliasName, |
| 141 | policyStoreId: item.policyStoreId, |
| 142 | aliasArn: item.aliasArn, |
| 143 | })); |
| 144 | }), |
| 145 | |
| 146 | read: Effect.fn(function* ({ id, olds, output }) { |
| 147 | const aliasName = |
| 148 | output?.aliasName ?? (olds !== undefined ? yield* toAliasName(id, olds) : undefined); |
| 149 | if (aliasName === undefined) return undefined; |
| 150 | const alias = yield* observe(aliasName); |
| 151 | if (alias === undefined || alias.state === "PendingDeletion") { |
| 152 | return undefined; |
| 153 | } |
| 154 | return { |
| 155 | aliasName: alias.aliasName, |
| 156 | policyStoreId: alias.policyStoreId, |
| 157 | aliasArn: alias.aliasArn, |
| 158 | }; |
| 159 | }), |
| 160 | |
| 161 | diff: Effect.fn(function* ({ news, olds }) { |
| 162 | if (!isResolved(news)) return undefined; |
| 163 | // no UpdatePolicyStoreAlias API — name and target changes replace; |
| 164 | // deletionMode only affects delete-time behavior |
| 165 | if ( |
| 166 | olds.policyStoreId !== news.policyStoreId || |
| 167 | (news.aliasName !== undefined && |
| 168 | (olds.aliasName === undefined || |
| 169 | withAliasPrefix(olds.aliasName) !== withAliasPrefix(news.aliasName))) |
| 170 | ) { |
| 171 | return { action: "replace" } as const; |
| 172 | } |
| 173 | }), |
| 174 | |
| 175 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 176 | const aliasName = output?.aliasName ?? (yield* toAliasName(id, news)); |
| 177 | |
| 178 | // 1. OBSERVE — cloud state is authoritative |
| 179 | const existing = yield* observe(aliasName); |
| 180 | |
| 181 | // 2. ENSURE — tolerate the AlreadyExists race |
| 182 | if (existing === undefined || existing.state === "PendingDeletion") { |
| 183 | const created = yield* avp |
| 184 | .createPolicyStoreAlias({ |
| 185 | aliasName, |
| 186 | policyStoreId: news.policyStoreId, |
| 187 | }) |
| 188 | .pipe(Effect.catchTag("ConflictException", () => Effect.undefined)); |
| 189 | if (created !== undefined) { |
| 190 | yield* session.note(created.aliasName); |
| 191 | return { |
| 192 | aliasName: created.aliasName, |
| 193 | policyStoreId: created.policyStoreId, |
| 194 | aliasArn: created.aliasArn, |
| 195 | }; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | const alias = existing ?? (yield* observe(aliasName)); |
| 200 | if (alias === undefined) { |
| 201 | return yield* new PolicyStoreAliasUnexpectedResponse({ |
| 202 | message: `policy store alias '${aliasName}' not found after create`, |
| 203 | }); |
| 204 | } |
| 205 | yield* session.note(alias.aliasName); |
| 206 | return { |
| 207 | aliasName: alias.aliasName, |
| 208 | policyStoreId: alias.policyStoreId, |
| 209 | aliasArn: alias.aliasArn, |
| 210 | }; |
| 211 | }), |
| 212 | |
| 213 | delete: Effect.fn(function* ({ olds, output }) { |
| 214 | yield* avp |
| 215 | .deletePolicyStoreAlias({ |
| 216 | aliasName: output.aliasName, |
| 217 | deletionMode: olds.deletionMode ?? "HardDelete", |
| 218 | }) |
| 219 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 220 | }), |
| 221 | }); |
| 222 | }), |
| 223 | ); |