The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as cloudfront from "@distilled.cloud/aws/cloudfront"; |
| 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 type { Providers } from "../Providers.ts"; |
| 9 | |
| 10 | export class ResponseHeadersPolicyConflict extends Data.TaggedError( |
| 11 | "ResponseHeadersPolicyConflict", |
| 12 | )<{ |
| 13 | readonly message: string; |
| 14 | readonly cause?: unknown; |
| 15 | }> {} |
| 16 | |
| 17 | export class ResponseHeadersPolicyUnexpectedResponse extends Data.TaggedError( |
| 18 | "ResponseHeadersPolicyUnexpectedResponse", |
| 19 | )<{ |
| 20 | readonly message: string; |
| 21 | readonly cause?: unknown; |
| 22 | }> {} |
| 23 | |
| 24 | export interface ResponseHeadersPolicyProps { |
| 25 | /** |
| 26 | * Name of the response headers policy. If omitted, a deterministic name is |
| 27 | * generated. Names must be unique per AWS account. Changing the name |
| 28 | * triggers a replacement. |
| 29 | */ |
| 30 | name?: string; |
| 31 | /** |
| 32 | * Optional comment describing the policy. |
| 33 | */ |
| 34 | comment?: string; |
| 35 | /** |
| 36 | * CORS configuration applied to viewer responses. |
| 37 | */ |
| 38 | corsConfig?: cloudfront.ResponseHeadersPolicyCorsConfig; |
| 39 | /** |
| 40 | * Standard security headers (HSTS, X-Frame-Options, etc.) added to |
| 41 | * viewer responses. |
| 42 | */ |
| 43 | securityHeadersConfig?: cloudfront.ResponseHeadersPolicySecurityHeadersConfig; |
| 44 | /** |
| 45 | * Server-Timing header configuration for measuring CloudFront performance. |
| 46 | */ |
| 47 | serverTimingHeadersConfig?: cloudfront.ResponseHeadersPolicyServerTimingHeadersConfig; |
| 48 | /** |
| 49 | * Custom headers to add to viewer responses. |
| 50 | */ |
| 51 | customHeadersConfig?: cloudfront.ResponseHeadersPolicyCustomHeadersConfig; |
| 52 | /** |
| 53 | * Headers to remove from viewer responses. |
| 54 | */ |
| 55 | removeHeadersConfig?: cloudfront.ResponseHeadersPolicyRemoveHeadersConfig; |
| 56 | } |
| 57 | |
| 58 | export interface ResponseHeadersPolicy extends Resource< |
| 59 | "AWS.CloudFront.ResponseHeadersPolicy", |
| 60 | ResponseHeadersPolicyProps, |
| 61 | { |
| 62 | /** |
| 63 | * CloudFront-assigned response headers policy identifier. |
| 64 | */ |
| 65 | responseHeadersPolicyId: string; |
| 66 | /** |
| 67 | * Name of the response headers policy. |
| 68 | */ |
| 69 | name: string; |
| 70 | /** |
| 71 | * Most recent entity tag for update/delete operations. |
| 72 | */ |
| 73 | etag: string | undefined; |
| 74 | /** |
| 75 | * Current comment on the policy. |
| 76 | */ |
| 77 | comment: string | undefined; |
| 78 | /** |
| 79 | * Current CORS configuration. |
| 80 | */ |
| 81 | corsConfig: cloudfront.ResponseHeadersPolicyCorsConfig | undefined; |
| 82 | /** |
| 83 | * Current security headers configuration. |
| 84 | */ |
| 85 | securityHeadersConfig: cloudfront.ResponseHeadersPolicySecurityHeadersConfig | undefined; |
| 86 | /** |
| 87 | * Current Server-Timing configuration. |
| 88 | */ |
| 89 | serverTimingHeadersConfig: |
| 90 | | cloudfront.ResponseHeadersPolicyServerTimingHeadersConfig |
| 91 | | undefined; |
| 92 | /** |
| 93 | * Current custom headers configuration. |
| 94 | */ |
| 95 | customHeadersConfig: cloudfront.ResponseHeadersPolicyCustomHeadersConfig | undefined; |
| 96 | /** |
| 97 | * Current remove-headers configuration. |
| 98 | */ |
| 99 | removeHeadersConfig: cloudfront.ResponseHeadersPolicyRemoveHeadersConfig | undefined; |
| 100 | }, |
| 101 | never, |
| 102 | Providers |
| 103 | > {} |
| 104 | |
| 105 | /** |
| 106 | * A CloudFront response headers policy. |
| 107 | * |
| 108 | * Response headers policies add or remove headers in viewer responses, |
| 109 | * including CORS, standard security headers (HSTS, X-Frame-Options, |
| 110 | * X-Content-Type-Options, Referrer-Policy, etc.), Server-Timing, custom |
| 111 | * headers and explicit header removal. They are referenced by ID on a |
| 112 | * Distribution's default behavior or per-path cache behaviors. |
| 113 | * ### Creating Response Headers Policies |
| 114 | * **Example:** Standard security + CORS |
| 115 | * ```typescript |
| 116 | * const responseHeadersPolicy = yield* ResponseHeadersPolicy("AppResponseHeaders", { |
| 117 | * comment: "Default app security + CORS", |
| 118 | * corsConfig: { |
| 119 | * AccessControlAllowOrigins: { Quantity: 1, Items: ["https://app.example.com"] }, |
| 120 | * AccessControlAllowMethods: { Quantity: 2, Items: ["GET", "OPTIONS"] }, |
| 121 | * AccessControlAllowHeaders: { Quantity: 1, Items: ["Authorization"] }, |
| 122 | * AccessControlAllowCredentials: false, |
| 123 | * OriginOverride: true, |
| 124 | * }, |
| 125 | * securityHeadersConfig: { |
| 126 | * StrictTransportSecurity: { |
| 127 | * AccessControlMaxAgeSec: 31536000, |
| 128 | * IncludeSubdomains: true, |
| 129 | * Preload: true, |
| 130 | * Override: true, |
| 131 | * }, |
| 132 | * ContentTypeOptions: { Override: true }, |
| 133 | * FrameOptions: { FrameOption: "DENY", Override: true }, |
| 134 | * ReferrerPolicy: { ReferrerPolicy: "no-referrer", Override: true }, |
| 135 | * }, |
| 136 | * }); |
| 137 | * ``` |
| 138 | * |
| 139 | * @resource |
| 140 | */ |
| 141 | export const ResponseHeadersPolicy = Resource<ResponseHeadersPolicy>( |
| 142 | "AWS.CloudFront.ResponseHeadersPolicy", |
| 143 | ); |
| 144 | |
| 145 | export const ResponseHeadersPolicyProvider = () => |
| 146 | Provider.effect( |
| 147 | ResponseHeadersPolicy, |
| 148 | Effect.gen(function* () { |
| 149 | const getById = Effect.fn(function* (id: string) { |
| 150 | const config = yield* cloudfront |
| 151 | .getResponseHeadersPolicyConfig({ Id: id }) |
| 152 | .pipe(Effect.catchTag("NoSuchResponseHeadersPolicy", () => Effect.undefined)); |
| 153 | if (!config?.ResponseHeadersPolicyConfig) return undefined; |
| 154 | return { |
| 155 | config: config.ResponseHeadersPolicyConfig, |
| 156 | etag: config.ETag, |
| 157 | }; |
| 158 | }); |
| 159 | |
| 160 | const getByName = Effect.fn(function* (name: string) { |
| 161 | const listed = yield* cloudfront.listResponseHeadersPolicies({ |
| 162 | Type: "custom", |
| 163 | }); |
| 164 | const summary = listed.ResponseHeadersPolicyList?.Items?.find( |
| 165 | (item) => item.ResponseHeadersPolicy?.ResponseHeadersPolicyConfig?.Name === name, |
| 166 | ); |
| 167 | if (!summary?.ResponseHeadersPolicy?.Id) return undefined; |
| 168 | return yield* getById(summary.ResponseHeadersPolicy.Id).pipe( |
| 169 | Effect.map((found) => |
| 170 | found ? { id: summary.ResponseHeadersPolicy.Id, ...found } : undefined, |
| 171 | ), |
| 172 | ); |
| 173 | }); |
| 174 | |
| 175 | const buildConfig = ( |
| 176 | name: string, |
| 177 | props: ResponseHeadersPolicyProps, |
| 178 | ): cloudfront.ResponseHeadersPolicyConfig => ({ |
| 179 | Name: name, |
| 180 | Comment: props.comment, |
| 181 | CorsConfig: props.corsConfig, |
| 182 | SecurityHeadersConfig: props.securityHeadersConfig, |
| 183 | ServerTimingHeadersConfig: props.serverTimingHeadersConfig, |
| 184 | CustomHeadersConfig: props.customHeadersConfig, |
| 185 | RemoveHeadersConfig: props.removeHeadersConfig, |
| 186 | }); |
| 187 | |
| 188 | const toAttrs = ( |
| 189 | id: string, |
| 190 | config: cloudfront.ResponseHeadersPolicyConfig, |
| 191 | etag: string | undefined, |
| 192 | ) => ({ |
| 193 | responseHeadersPolicyId: id, |
| 194 | name: config.Name, |
| 195 | etag, |
| 196 | comment: config.Comment, |
| 197 | corsConfig: config.CorsConfig, |
| 198 | securityHeadersConfig: config.SecurityHeadersConfig, |
| 199 | serverTimingHeadersConfig: config.ServerTimingHeadersConfig, |
| 200 | customHeadersConfig: config.CustomHeadersConfig, |
| 201 | removeHeadersConfig: config.RemoveHeadersConfig, |
| 202 | }); |
| 203 | |
| 204 | return { |
| 205 | stables: ["responseHeadersPolicyId"], |
| 206 | diff: Effect.fn(function* ({ id, news, olds }) { |
| 207 | if (!isResolved(news)) return undefined; |
| 208 | if ((yield* createName(id, olds ?? {})) !== (yield* createName(id, news))) { |
| 209 | return { action: "replace" } as const; |
| 210 | } |
| 211 | }), |
| 212 | read: Effect.fn(function* ({ id, olds, output }) { |
| 213 | if (output?.responseHeadersPolicyId) { |
| 214 | const found = yield* getById(output.responseHeadersPolicyId); |
| 215 | if (found) { |
| 216 | return toAttrs(output.responseHeadersPolicyId, found.config, found.etag); |
| 217 | } |
| 218 | } |
| 219 | const name = yield* createName(id, olds ?? {}); |
| 220 | const found = yield* getByName(name); |
| 221 | if (!found) return undefined; |
| 222 | return toAttrs(found.id, found.config, found.etag); |
| 223 | }), |
| 224 | list: () => |
| 225 | Effect.gen(function* () { |
| 226 | // CloudFront is global; paginate via Marker/NextMarker. Only |
| 227 | // `Type: "custom"` policies are ours — AWS-managed ones are |
| 228 | // shared and not deletable, so they are skipped. |
| 229 | const items: ReturnType<typeof toAttrs>[] = []; |
| 230 | let marker: string | undefined = undefined; |
| 231 | do { |
| 232 | const listed: cloudfront.ListResponseHeadersPoliciesResult = |
| 233 | yield* cloudfront.listResponseHeadersPolicies({ |
| 234 | Type: "custom", |
| 235 | Marker: marker, |
| 236 | }); |
| 237 | for (const summary of listed.ResponseHeadersPolicyList?.Items ?? []) { |
| 238 | if (summary.Type !== "custom") continue; |
| 239 | const id = summary.ResponseHeadersPolicy?.Id; |
| 240 | const config = summary.ResponseHeadersPolicy?.ResponseHeadersPolicyConfig; |
| 241 | if (!id || !config) continue; |
| 242 | const found = yield* getById(id); |
| 243 | items.push(toAttrs(id, found?.config ?? config, found?.etag)); |
| 244 | } |
| 245 | marker = listed.ResponseHeadersPolicyList?.NextMarker; |
| 246 | } while (marker); |
| 247 | return items; |
| 248 | }), |
| 249 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 250 | const name = yield* createName(id, news); |
| 251 | |
| 252 | // Observe — locate the policy by id (cached on `output`) or |
| 253 | // by name. Trust observed cloud state, not stale `olds`. |
| 254 | let observed = output?.responseHeadersPolicyId |
| 255 | ? yield* getById(output.responseHeadersPolicyId).pipe( |
| 256 | Effect.map((found) => |
| 257 | found ? { id: output.responseHeadersPolicyId, ...found } : undefined, |
| 258 | ), |
| 259 | ) |
| 260 | : undefined; |
| 261 | if (!observed) { |
| 262 | observed = yield* getByName(name); |
| 263 | } |
| 264 | |
| 265 | // Ensure — create the policy if it's missing. Tolerate |
| 266 | // `ResponseHeadersPolicyAlreadyExists` (race with a peer |
| 267 | // reconciler). |
| 268 | if (!observed) { |
| 269 | const created = yield* cloudfront |
| 270 | .createResponseHeadersPolicy({ |
| 271 | ResponseHeadersPolicyConfig: buildConfig(name, news), |
| 272 | }) |
| 273 | .pipe( |
| 274 | Effect.catchTag("ResponseHeadersPolicyAlreadyExists", () => |
| 275 | getByName(name).pipe( |
| 276 | Effect.flatMap((existing) => |
| 277 | existing |
| 278 | ? Effect.succeed({ |
| 279 | ResponseHeadersPolicy: { |
| 280 | Id: existing.id, |
| 281 | LastModifiedTime: new Date(), |
| 282 | ResponseHeadersPolicyConfig: existing.config, |
| 283 | }, |
| 284 | ETag: existing.etag, |
| 285 | Location: undefined, |
| 286 | }) |
| 287 | : Effect.fail( |
| 288 | new ResponseHeadersPolicyConflict({ |
| 289 | message: `Response headers policy '${name}' already exists but could not be recovered`, |
| 290 | }), |
| 291 | ), |
| 292 | ), |
| 293 | ), |
| 294 | ), |
| 295 | ); |
| 296 | if (!created.ResponseHeadersPolicy?.Id) { |
| 297 | return yield* new ResponseHeadersPolicyUnexpectedResponse({ |
| 298 | message: "createResponseHeadersPolicy returned no identifier", |
| 299 | }); |
| 300 | } |
| 301 | yield* session.note(created.ResponseHeadersPolicy.Id); |
| 302 | return toAttrs( |
| 303 | created.ResponseHeadersPolicy.Id, |
| 304 | created.ResponseHeadersPolicy.ResponseHeadersPolicyConfig, |
| 305 | created.ETag, |
| 306 | ); |
| 307 | } |
| 308 | |
| 309 | // Sync — patch the observed config to the desired state. The |
| 310 | // freshly observed ETag handles optimistic concurrency. |
| 311 | const updated = yield* cloudfront.updateResponseHeadersPolicy({ |
| 312 | Id: observed.id, |
| 313 | IfMatch: observed.etag, |
| 314 | ResponseHeadersPolicyConfig: buildConfig(observed.config.Name, news), |
| 315 | }); |
| 316 | if (!updated.ResponseHeadersPolicy?.Id) { |
| 317 | return yield* new ResponseHeadersPolicyUnexpectedResponse({ |
| 318 | message: "updateResponseHeadersPolicy returned no identifier", |
| 319 | }); |
| 320 | } |
| 321 | yield* session.note(observed.id); |
| 322 | return toAttrs( |
| 323 | updated.ResponseHeadersPolicy.Id, |
| 324 | updated.ResponseHeadersPolicy.ResponseHeadersPolicyConfig, |
| 325 | updated.ETag, |
| 326 | ); |
| 327 | }), |
| 328 | delete: Effect.fn(function* ({ output }) { |
| 329 | const current = yield* getById(output.responseHeadersPolicyId); |
| 330 | if (!current) return; |
| 331 | yield* cloudfront |
| 332 | .deleteResponseHeadersPolicy({ |
| 333 | Id: output.responseHeadersPolicyId, |
| 334 | IfMatch: current.etag, |
| 335 | }) |
| 336 | .pipe(Effect.catchTag("NoSuchResponseHeadersPolicy", () => Effect.void)); |
| 337 | }), |
| 338 | }; |
| 339 | }), |
| 340 | ); |
| 341 | |
| 342 | const createName = (id: string, props: ResponseHeadersPolicyProps) => |
| 343 | props.name |
| 344 | ? Effect.succeed(props.name) |
| 345 | : createPhysicalName({ id, maxLength: 128, lowercase: true }); |