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 aoss from "@distilled.cloud/aws/opensearchserverless"; |
| 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 Effect from "effect/Effect"; |
| 7 | import * as Stream from "effect/Stream"; |
| 8 | import type { Providers } from "../Providers.ts"; |
| 9 | import { canonicalizePolicy, retryWhileConflict, stringifyPolicy } from "./internal.ts"; |
| 10 | |
| 11 | /** The kind of security policy — encryption at rest or network access. */ |
| 12 | export type SecurityPolicyType = "encryption" | "network"; |
| 13 | |
| 14 | /** A security policy document — a JSON object/array or a pre-serialized string. */ |
| 15 | export type SecurityPolicyDocument = string | Record<string, unknown> | readonly unknown[]; |
| 16 | |
| 17 | export interface SecurityPolicyProps { |
| 18 | /** |
| 19 | * Name of the security policy (3-32 characters, lowercase). Must be unique |
| 20 | * within its {@link SecurityPolicyProps.type | type}. Changing the name |
| 21 | * replaces the policy. |
| 22 | * @default a generated physical name |
| 23 | */ |
| 24 | policyName?: string; |
| 25 | /** |
| 26 | * The policy kind: |
| 27 | * - `encryption` — encryption-at-rest policy (a single JSON object with |
| 28 | * `Rules` and either `AWSOwnedKey` or `KmsARN`). Every collection MUST be |
| 29 | * matched by exactly one encryption policy before it can be created. |
| 30 | * - `network` — network-access policy (a JSON array of rules controlling |
| 31 | * public/VPC access to the collection and Dashboards endpoints). |
| 32 | * |
| 33 | * Changing the type replaces the policy. |
| 34 | */ |
| 35 | type: SecurityPolicyType; |
| 36 | /** |
| 37 | * The policy document. Supply a JSON object (encryption) or array (network), |
| 38 | * or a pre-serialized string when you need to interpolate an `Output`-derived |
| 39 | * collection name into it. |
| 40 | */ |
| 41 | policy: SecurityPolicyDocument; |
| 42 | /** |
| 43 | * A human-readable description of the policy. |
| 44 | */ |
| 45 | description?: string; |
| 46 | } |
| 47 | |
| 48 | export interface SecurityPolicy extends Resource< |
| 49 | "AWS.OpenSearchServerless.SecurityPolicy", |
| 50 | SecurityPolicyProps, |
| 51 | { |
| 52 | /** |
| 53 | * Name of the security policy. |
| 54 | */ |
| 55 | policyName: string; |
| 56 | /** |
| 57 | * Policy type (`encryption` or `network`). |
| 58 | */ |
| 59 | type: string; |
| 60 | /** |
| 61 | * Version of the policy, used for optimistic-concurrency updates. |
| 62 | */ |
| 63 | policyVersion: string; |
| 64 | /** |
| 65 | * Description of the security policy. |
| 66 | */ |
| 67 | description?: string; |
| 68 | }, |
| 69 | {}, |
| 70 | Providers |
| 71 | > {} |
| 72 | |
| 73 | /** |
| 74 | * An Amazon OpenSearch Serverless security policy. Security policies govern |
| 75 | * encryption at rest (`encryption`) and network access (`network`) for one or |
| 76 | * more collections, matched by a resource pattern such as |
| 77 | * `collection/my-collection`. |
| 78 | * |
| 79 | * An **encryption** policy is a prerequisite for every collection — a |
| 80 | * collection whose name is not covered by an encryption policy fails to |
| 81 | * create. A **network** policy controls whether the collection's data and |
| 82 | * OpenSearch Dashboards endpoints are reachable from public networks or only |
| 83 | * from specific VPC endpoints. |
| 84 | * |
| 85 | * ### Encryption Policies |
| 86 | * **Example:** AWS-Owned-Key Encryption Policy |
| 87 | * ```typescript |
| 88 | * import * as AWS from "@alchemy.run/aws"; |
| 89 | * |
| 90 | * const encryption = yield* AWS.OpenSearchServerless.SecurityPolicy("Encryption", { |
| 91 | * policyName: "my-collection-enc", |
| 92 | * type: "encryption", |
| 93 | * policy: { |
| 94 | * Rules: [{ ResourceType: "collection", Resource: ["collection/my-collection"] }], |
| 95 | * AWSOwnedKey: true, |
| 96 | * }, |
| 97 | * }); |
| 98 | * ``` |
| 99 | * |
| 100 | * ### Network Policies |
| 101 | * **Example:** Public Network Access Policy |
| 102 | * ```typescript |
| 103 | * const network = yield* AWS.OpenSearchServerless.SecurityPolicy("Network", { |
| 104 | * policyName: "my-collection-net", |
| 105 | * type: "network", |
| 106 | * policy: [ |
| 107 | * { |
| 108 | * Rules: [ |
| 109 | * { ResourceType: "collection", Resource: ["collection/my-collection"] }, |
| 110 | * { ResourceType: "dashboard", Resource: ["collection/my-collection"] }, |
| 111 | * ], |
| 112 | * AllowFromPublic: true, |
| 113 | * }, |
| 114 | * ], |
| 115 | * }); |
| 116 | * ``` |
| 117 | * |
| 118 | * @resource |
| 119 | */ |
| 120 | export const SecurityPolicy = Resource<SecurityPolicy>("AWS.OpenSearchServerless.SecurityPolicy"); |
| 121 | |
| 122 | export const SecurityPolicyProvider = () => |
| 123 | Provider.effect( |
| 124 | SecurityPolicy, |
| 125 | Effect.gen(function* () { |
| 126 | const createName = Effect.fn(function* ( |
| 127 | id: string, |
| 128 | props: { policyName?: string | undefined }, |
| 129 | ) { |
| 130 | return ( |
| 131 | props.policyName ?? (yield* createPhysicalName({ id, maxLength: 32, lowercase: true })) |
| 132 | ); |
| 133 | }); |
| 134 | |
| 135 | const toAttributes = (detail: aoss.SecurityPolicyDetail) => ({ |
| 136 | policyName: detail.name!, |
| 137 | type: detail.type!, |
| 138 | policyVersion: detail.policyVersion!, |
| 139 | description: detail.description, |
| 140 | }); |
| 141 | |
| 142 | return SecurityPolicy.Provider.of({ |
| 143 | stables: ["policyName", "type"], |
| 144 | |
| 145 | list: () => |
| 146 | Effect.gen(function* () { |
| 147 | const types: SecurityPolicyType[] = ["encryption", "network"]; |
| 148 | const results: { |
| 149 | policyName: string; |
| 150 | type: string; |
| 151 | policyVersion: string; |
| 152 | description?: string; |
| 153 | }[] = []; |
| 154 | for (const type of types) { |
| 155 | const pages = yield* aoss.listSecurityPolicies |
| 156 | .pages({ type }) |
| 157 | .pipe(Stream.runCollect); |
| 158 | for (const page of pages) { |
| 159 | for (const s of page.securityPolicySummaries ?? []) { |
| 160 | if ( |
| 161 | s.name !== undefined && |
| 162 | s.type !== undefined && |
| 163 | s.policyVersion !== undefined |
| 164 | ) { |
| 165 | results.push({ |
| 166 | policyName: s.name, |
| 167 | type: s.type, |
| 168 | policyVersion: s.policyVersion, |
| 169 | description: s.description, |
| 170 | }); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | return results; |
| 176 | }), |
| 177 | |
| 178 | read: Effect.fn(function* ({ id, olds, output }) { |
| 179 | const type = output?.type ?? olds?.type; |
| 180 | if (type === undefined) { |
| 181 | return undefined; |
| 182 | } |
| 183 | const name = output?.policyName ?? (yield* createName(id, olds ?? {})); |
| 184 | const found = yield* aoss |
| 185 | .getSecurityPolicy({ type, name }) |
| 186 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 187 | if (found?.securityPolicyDetail === undefined) { |
| 188 | return undefined; |
| 189 | } |
| 190 | // Security policies carry no tags, so ownership cannot be gated — |
| 191 | // an existing same-name policy is adopted. |
| 192 | return toAttributes(found.securityPolicyDetail); |
| 193 | }), |
| 194 | |
| 195 | diff: Effect.fn(function* ({ id, news, olds }) { |
| 196 | if (!isResolved(news)) return undefined; |
| 197 | if (olds.type !== news.type) { |
| 198 | return { action: "replace" } as const; |
| 199 | } |
| 200 | const oldName = yield* createName(id, olds); |
| 201 | const newName = yield* createName(id, news); |
| 202 | if (oldName !== newName) { |
| 203 | return { action: "replace" } as const; |
| 204 | } |
| 205 | // description/policy fall through to the default update path |
| 206 | }), |
| 207 | |
| 208 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 209 | const type = news.type; |
| 210 | const name = output?.policyName ?? (yield* createName(id, news)); |
| 211 | const policy = stringifyPolicy(news.policy); |
| 212 | |
| 213 | // 1. OBSERVE |
| 214 | let detail = yield* aoss.getSecurityPolicy({ type, name }).pipe( |
| 215 | Effect.map((r) => r.securityPolicyDetail), |
| 216 | Effect.catchTag("ResourceNotFoundException", () => Effect.undefined), |
| 217 | ); |
| 218 | |
| 219 | // 2. ENSURE — create if missing; tolerate a concurrent create race |
| 220 | if (detail === undefined) { |
| 221 | detail = yield* aoss |
| 222 | .createSecurityPolicy({ |
| 223 | type, |
| 224 | name, |
| 225 | policy, |
| 226 | description: news.description, |
| 227 | }) |
| 228 | .pipe( |
| 229 | Effect.map((r) => r.securityPolicyDetail), |
| 230 | Effect.catchTag("ConflictException", () => |
| 231 | aoss |
| 232 | .getSecurityPolicy({ type, name }) |
| 233 | .pipe(Effect.map((r) => r.securityPolicyDetail)), |
| 234 | ), |
| 235 | ); |
| 236 | } else { |
| 237 | // 3. SYNC — update policy/description when observed drifts from desired |
| 238 | const policyDrift = canonicalizePolicy(detail.policy) !== canonicalizePolicy(policy); |
| 239 | const descriptionDrift = |
| 240 | news.description !== undefined && news.description !== detail.description; |
| 241 | if (policyDrift || descriptionDrift) { |
| 242 | detail = yield* aoss |
| 243 | .updateSecurityPolicy({ |
| 244 | type, |
| 245 | name, |
| 246 | policyVersion: detail.policyVersion!, |
| 247 | policy: policyDrift ? policy : undefined, |
| 248 | description: descriptionDrift ? news.description : undefined, |
| 249 | }) |
| 250 | .pipe(Effect.map((r) => r.securityPolicyDetail)); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | if (detail?.name === undefined) { |
| 255 | return yield* aoss.ResourceNotFoundException.make({ |
| 256 | message: `security policy ${type}/${name} not visible after reconcile`, |
| 257 | }); |
| 258 | } |
| 259 | yield* session.note(`${type}/${name}`); |
| 260 | return toAttributes(detail); |
| 261 | }), |
| 262 | |
| 263 | delete: Effect.fn(function* ({ output }) { |
| 264 | yield* retryWhileConflict( |
| 265 | aoss.deleteSecurityPolicy({ |
| 266 | type: output.type, |
| 267 | name: output.policyName, |
| 268 | }), |
| 269 | ).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 270 | }), |
| 271 | }); |
| 272 | }), |
| 273 | ); |