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 { fileURLToPath } from "node:url"; |
| 2 | import * as AWS from "@alchemy.run/aws"; |
| 3 | import { |
| 4 | Distribution, |
| 5 | MANAGED_ALL_VIEWER_EXCEPT_HOST_HEADER_POLICY_ID, |
| 6 | MANAGED_CACHING_DISABLED_POLICY_ID, |
| 7 | OriginAccessControl, |
| 8 | } from "@alchemy.run/aws/CloudFront"; |
| 9 | import type { PolicyStatement } from "@alchemy.run/aws/IAM/Policy"; |
| 10 | import { Bucket } from "@alchemy.run/aws/S3"; |
| 11 | import { describe, expect } from "@alchemy.run/test"; |
| 12 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 13 | import * as cloudfront from "@distilled.cloud/aws/cloudfront"; |
| 14 | import * as LambdaWeb from "@distilled.cloud/aws/lambda-web"; |
| 15 | import * as S3 from "@distilled.cloud/aws/s3"; |
| 16 | import * as Output from "alchemy/Output"; |
| 17 | import * as Provider from "alchemy/Provider"; |
| 18 | import type * as Duration from "effect/Duration"; |
| 19 | import * as Effect from "effect/Effect"; |
| 20 | import * as HttpClient from "effect/http/HttpClient"; |
| 21 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 22 | import * as Redacted from "effect/Redacted"; |
| 23 | import * as Schedule from "effect/Schedule"; |
| 24 | |
| 25 | const { test } = Test.make({ providers: AWS.providers() }); |
| 26 | |
| 27 | const runLive = !process.env.FAST; |
| 28 | // Web Functions are live-only; the Floci sweep (`ALCHEMY_TEST_DEV=1`) points |
| 29 | // every AWS client at the emulator, which does not emulate them. |
| 30 | const runWebFunctions = runLive && process.env.ALCHEMY_TEST_DEV !== "1"; |
| 31 | |
| 32 | const webFunctionMain = fileURLToPath(new URL("./fixtures/web-function.ts", import.meta.url)); |
| 33 | |
| 34 | // CloudFront itself bounds these lifecycles: a create waits ~5 min for |
| 35 | // `Deployed`, a config update up to several more, and delete must disable |
| 36 | // the distribution and wait for `Deployed` again (~3–5 min) before the |
| 37 | // DeleteDistribution call. Under a loaded full-suite run create+update+delete |
| 38 | // measured ~570s — right at the old 600s cap, so attempts timed out |
| 39 | // mid-teardown. 15 min covers the platform floor with headroom. |
| 40 | const DEPLOY_DELETE_TIMEOUT = 900_000; |
| 41 | |
| 42 | describe( |
| 43 | "AWS.CloudFront.Distribution", |
| 44 | { tags: ["provider:aws", "provider:aws:cloudfront", "live"] }, |
| 45 | () => { |
| 46 | test.provider.skipIf(!runLive)( |
| 47 | "create and delete distribution for a private S3 origin", |
| 48 | (stack) => |
| 49 | Effect.gen(function* () { |
| 50 | yield* stack.destroy(); |
| 51 | |
| 52 | const deployed = yield* stack.deploy( |
| 53 | Effect.gen(function* () { |
| 54 | const bucket = yield* Bucket("WebsiteBucket", { forceDestroy: true }); |
| 55 | const oac = yield* OriginAccessControl("WebsiteOac", { originType: "s3" }); |
| 56 | const distribution = yield* Distribution("WebsiteDistribution", { |
| 57 | origins: [ |
| 58 | { |
| 59 | id: "site", |
| 60 | domainName: bucket.bucketRegionalDomainName, |
| 61 | s3Origin: true, |
| 62 | originAccessControlId: oac.originAccessControlId, |
| 63 | // Duration.Input → wire whole-seconds conversion under test. |
| 64 | connectionTimeout: "5 seconds", |
| 65 | }, |
| 66 | ], |
| 67 | defaultRootObject: "index.html", |
| 68 | defaultCacheBehavior: { |
| 69 | targetOriginId: "site", |
| 70 | viewerProtocolPolicy: "redirect-to-https", |
| 71 | compress: true, |
| 72 | allowedMethods: ["GET", "HEAD"], |
| 73 | cachedMethods: ["GET", "HEAD"], |
| 74 | forwardedValues: { QueryString: false, Cookies: { Forward: "none" } }, |
| 75 | // Duration.Input → wire whole-seconds conversion under test. |
| 76 | minTtl: 0, |
| 77 | defaultTtl: "5 minutes", |
| 78 | maxTtl: "1 hour", |
| 79 | }, |
| 80 | }); |
| 81 | |
| 82 | const statement: PolicyStatement = { |
| 83 | Effect: "Allow", |
| 84 | Principal: { Service: "cloudfront.amazonaws.com" }, |
| 85 | Action: ["s3:GetObject"], |
| 86 | Resource: [Output.interpolate`${bucket.bucketArn}/*` as any], |
| 87 | Condition: { |
| 88 | StringEquals: { "AWS:SourceArn": distribution.distributionArn as any }, |
| 89 | }, |
| 90 | }; |
| 91 | |
| 92 | yield* bucket.bind`Allow(${distribution}, CloudFront.Read(${bucket}))`({ |
| 93 | policyStatements: [statement], |
| 94 | }); |
| 95 | |
| 96 | return { bucket, oac, distribution }; |
| 97 | }), |
| 98 | ); |
| 99 | |
| 100 | const current = yield* cloudfront.getDistribution({ |
| 101 | Id: deployed.distribution.distributionId, |
| 102 | }); |
| 103 | expect(current.Distribution?.Status).toEqual("Deployed"); |
| 104 | expect(current.Distribution?.DomainName).toEqual(deployed.distribution.domainName); |
| 105 | // Duration.Input props reached the wire as whole seconds. |
| 106 | const config = current.Distribution?.DistributionConfig; |
| 107 | expect(config?.Origins?.Items?.[0]?.ConnectionTimeout).toEqual(5); |
| 108 | expect(config?.DefaultCacheBehavior?.MinTTL).toEqual(0); |
| 109 | expect(config?.DefaultCacheBehavior?.DefaultTTL).toEqual(300); |
| 110 | expect(config?.DefaultCacheBehavior?.MaxTTL).toEqual(3600); |
| 111 | |
| 112 | const control = yield* cloudfront.getOriginAccessControl({ |
| 113 | Id: deployed.oac.originAccessControlId, |
| 114 | }); |
| 115 | expect(control.OriginAccessControl?.Id).toEqual(deployed.oac.originAccessControlId); |
| 116 | |
| 117 | yield* S3.putObject({ |
| 118 | Bucket: deployed.bucket.bucketName, |
| 119 | Key: "index.html", |
| 120 | Body: "<html>ok</html>", |
| 121 | ContentType: "text/html; charset=utf-8", |
| 122 | }); |
| 123 | |
| 124 | yield* stack.destroy(); |
| 125 | yield* assertDistributionDeleted(deployed.distribution.distributionId); |
| 126 | }), |
| 127 | { tags: ["provider:aws:iam", "provider:aws:s3"], timeout: DEPLOY_DELETE_TIMEOUT }, |
| 128 | ); |
| 129 | |
| 130 | // Fast probe (read-only): CloudFront `list()` exhaustively paginates |
| 131 | // `listDistributions` and resolves each summary to the full `read`-shaped |
| 132 | // Attributes. This runs without deploying anything (distribution create + |
| 133 | // disable-before-delete exceeds CI budget — see the gated full test below), |
| 134 | // so it verifies the live list op cheaply. |
| 135 | test.provider( |
| 136 | "list enumerates account distributions", |
| 137 | () => |
| 138 | Effect.gen(function* () { |
| 139 | const provider = yield* Provider.findProvider(Distribution); |
| 140 | const all = yield* provider.list(); |
| 141 | |
| 142 | expect(Array.isArray(all)).toBe(true); |
| 143 | for (const item of all) { |
| 144 | expect(item.distributionId).toBeDefined(); |
| 145 | expect(item.distributionArn).toBeDefined(); |
| 146 | expect(item.domainName).toBeDefined(); |
| 147 | } |
| 148 | }), |
| 149 | // Hydrating every distribution in a busy account (3 read calls each) |
| 150 | // under CloudFront's aggressive read throttle takes a while even with |
| 151 | // the provider's bounded retry — give it ample headroom. |
| 152 | { timeout: 300_000 }, |
| 153 | ); |
| 154 | |
| 155 | // Full lifecycle: deploy a real distribution, assert it shows up in the |
| 156 | // enumerated result, then tear it down. Gated behind the same slow-test env |
| 157 | // flag as the create/delete case because distribution provisioning + |
| 158 | // disable-before-delete can take many minutes. |
| 159 | test.provider.skipIf(!runLive)( |
| 160 | "list includes a freshly deployed distribution", |
| 161 | (stack) => |
| 162 | Effect.gen(function* () { |
| 163 | yield* stack.destroy(); |
| 164 | |
| 165 | const deployed = yield* stack.deploy( |
| 166 | Effect.gen(function* () { |
| 167 | const bucket = yield* Bucket("ListWebsiteBucket", { forceDestroy: true }); |
| 168 | const oac = yield* OriginAccessControl("ListWebsiteOac", { originType: "s3" }); |
| 169 | return yield* Distribution("ListWebsiteDistribution", { |
| 170 | origins: [ |
| 171 | { |
| 172 | id: "site", |
| 173 | domainName: bucket.bucketRegionalDomainName, |
| 174 | s3Origin: true, |
| 175 | originAccessControlId: oac.originAccessControlId, |
| 176 | }, |
| 177 | ], |
| 178 | defaultCacheBehavior: { |
| 179 | targetOriginId: "site", |
| 180 | viewerProtocolPolicy: "redirect-to-https", |
| 181 | compress: true, |
| 182 | }, |
| 183 | }); |
| 184 | }), |
| 185 | ); |
| 186 | |
| 187 | const provider = yield* Provider.findProvider(Distribution); |
| 188 | const all = yield* provider.list(); |
| 189 | |
| 190 | expect(all.some((d) => d.distributionId === deployed.distributionId)).toBe(true); |
| 191 | |
| 192 | yield* stack.destroy(); |
| 193 | yield* assertDistributionDeleted(deployed.distributionId); |
| 194 | }), |
| 195 | { tags: ["provider:aws:s3"], timeout: DEPLOY_DELETE_TIMEOUT }, |
| 196 | ); |
| 197 | // Exercises the newly-exposed config gaps: geo restriction + custom error |
| 198 | // responses. Creates with a whitelist + a custom 404, updates the geo |
| 199 | // restriction to `none`, and asserts both round-trip via getDistributionConfig. |
| 200 | test.provider.skipIf(!runLive)( |
| 201 | "geo restriction and custom error responses round-trip", |
| 202 | (stack) => |
| 203 | Effect.gen(function* () { |
| 204 | yield* stack.destroy(); |
| 205 | |
| 206 | const deployed = yield* stack.deploy( |
| 207 | Effect.gen(function* () { |
| 208 | const bucket = yield* Bucket("GeoBucket", { forceDestroy: true }); |
| 209 | const oac = yield* OriginAccessControl("GeoOac", { originType: "s3" }); |
| 210 | const distribution = yield* Distribution("GeoDistribution", { |
| 211 | origins: [ |
| 212 | { |
| 213 | id: "site", |
| 214 | domainName: bucket.bucketRegionalDomainName, |
| 215 | s3Origin: true, |
| 216 | originAccessControlId: oac.originAccessControlId, |
| 217 | }, |
| 218 | ], |
| 219 | defaultCacheBehavior: { |
| 220 | targetOriginId: "site", |
| 221 | viewerProtocolPolicy: "redirect-to-https", |
| 222 | compress: true, |
| 223 | }, |
| 224 | geoRestriction: { restrictionType: "whitelist", locations: ["US", "CA"] }, |
| 225 | customErrorResponses: [ |
| 226 | { |
| 227 | ErrorCode: 404, |
| 228 | ResponseCode: "404", |
| 229 | ResponsePagePath: "/404.html", |
| 230 | ErrorCachingMinTTL: 10, |
| 231 | }, |
| 232 | ], |
| 233 | }); |
| 234 | return { distribution }; |
| 235 | }), |
| 236 | ); |
| 237 | |
| 238 | const created = yield* cloudfront.getDistributionConfig({ |
| 239 | Id: deployed.distribution.distributionId, |
| 240 | }); |
| 241 | expect(created.DistributionConfig?.Restrictions?.GeoRestriction.RestrictionType).toEqual( |
| 242 | "whitelist", |
| 243 | ); |
| 244 | expect(created.DistributionConfig?.Restrictions?.GeoRestriction.Items?.sort()).toEqual([ |
| 245 | "CA", |
| 246 | "US", |
| 247 | ]); |
| 248 | expect(created.DistributionConfig?.CustomErrorResponses?.Items?.[0].ErrorCode).toEqual( |
| 249 | 404, |
| 250 | ); |
| 251 | |
| 252 | // Update: drop the geo restriction. |
| 253 | yield* stack.deploy( |
| 254 | Effect.gen(function* () { |
| 255 | const bucket = yield* Bucket("GeoBucket", { forceDestroy: true }); |
| 256 | const oac = yield* OriginAccessControl("GeoOac", { originType: "s3" }); |
| 257 | return yield* Distribution("GeoDistribution", { |
| 258 | origins: [ |
| 259 | { |
| 260 | id: "site", |
| 261 | domainName: bucket.bucketRegionalDomainName, |
| 262 | s3Origin: true, |
| 263 | originAccessControlId: oac.originAccessControlId, |
| 264 | }, |
| 265 | ], |
| 266 | defaultCacheBehavior: { |
| 267 | targetOriginId: "site", |
| 268 | viewerProtocolPolicy: "redirect-to-https", |
| 269 | compress: true, |
| 270 | }, |
| 271 | geoRestriction: { restrictionType: "none" }, |
| 272 | }); |
| 273 | }), |
| 274 | ); |
| 275 | |
| 276 | const updated = yield* cloudfront.getDistributionConfig({ |
| 277 | Id: deployed.distribution.distributionId, |
| 278 | }); |
| 279 | expect(updated.DistributionConfig?.Restrictions?.GeoRestriction.RestrictionType).toEqual( |
| 280 | "none", |
| 281 | ); |
| 282 | |
| 283 | yield* stack.destroy(); |
| 284 | yield* assertDistributionDeleted(deployed.distribution.distributionId); |
| 285 | }), |
| 286 | { tags: ["provider:aws:s3"], timeout: DEPLOY_DELETE_TIMEOUT }, |
| 287 | ); |
| 288 | |
| 289 | // Regression for the UpdateDistribution full-config-replacement failure: |
| 290 | // members the props don't express (DefaultRootObject, per-origin |
| 291 | // CustomHeaders) are set on the live distribution out-of-band, then an |
| 292 | // alchemy update is deployed. Before `mergeWithObservedConfig`, the update |
| 293 | // request omitted those members and CloudFront rejected it with |
| 294 | // `IllegalUpdate` ("Default root object is missing for the resource", |
| 295 | // "The 'OriginCustomHeaders' field is missing"). Now they are carried over |
| 296 | // from the observed config while the declared props still win. |
| 297 | test.provider.skipIf(!runLive)( |
| 298 | "update preserves config members set out-of-band", |
| 299 | (stack) => |
| 300 | Effect.gen(function* () { |
| 301 | yield* stack.destroy(); |
| 302 | |
| 303 | // A plain custom origin keeps the fixture to a single resource — the |
| 304 | // regression under test lives entirely in the distribution's config |
| 305 | // update, so no bucket/OAC is needed. |
| 306 | const program = (defaultTtl: Duration.Input) => |
| 307 | Effect.gen(function* () { |
| 308 | return yield* Distribution("MergeDistribution", { |
| 309 | origins: [{ id: "site", domainName: "example.com" }], |
| 310 | defaultCacheBehavior: { |
| 311 | targetOriginId: "site", |
| 312 | viewerProtocolPolicy: "redirect-to-https", |
| 313 | compress: true, |
| 314 | forwardedValues: { QueryString: false, Cookies: { Forward: "none" } }, |
| 315 | minTtl: 0, |
| 316 | defaultTtl, |
| 317 | maxTtl: "1 hour", |
| 318 | }, |
| 319 | }); |
| 320 | }); |
| 321 | |
| 322 | const deployed = yield* stack.deploy(program("5 minutes")); |
| 323 | |
| 324 | // Out-of-band read-modify-write: enrich the live config with members |
| 325 | // the props above don't express. |
| 326 | const live = yield* cloudfront.getDistributionConfig({ Id: deployed.distributionId }); |
| 327 | const liveConfig = live.DistributionConfig!; |
| 328 | yield* cloudfront.updateDistribution({ |
| 329 | Id: deployed.distributionId, |
| 330 | IfMatch: live.ETag, |
| 331 | DistributionConfig: { |
| 332 | ...liveConfig, |
| 333 | DefaultRootObject: "index.html", |
| 334 | Origins: { |
| 335 | ...liveConfig.Origins, |
| 336 | Items: liveConfig.Origins.Items.map((origin) => ({ |
| 337 | ...origin, |
| 338 | CustomHeaders: { |
| 339 | Quantity: 1, |
| 340 | Items: [{ HeaderName: "x-out-of-band", HeaderValue: "kept" }], |
| 341 | }, |
| 342 | })), |
| 343 | }, |
| 344 | }, |
| 345 | }); |
| 346 | |
| 347 | // Update via alchemy with a changed prop. The props still don't |
| 348 | // express the members set above — pre-fix this deploy failed with |
| 349 | // `IllegalUpdate`. |
| 350 | yield* stack.deploy(program("10 minutes")); |
| 351 | |
| 352 | const updated = yield* cloudfront.getDistributionConfig({ Id: deployed.distributionId }); |
| 353 | const config = updated.DistributionConfig; |
| 354 | // Desired wins: the prop change went through. |
| 355 | expect(config?.DefaultCacheBehavior?.DefaultTTL).toEqual(600); |
| 356 | // Members the props don't express carried over from the observed |
| 357 | // config instead of being dropped (or rejected) by the update. |
| 358 | expect(config?.DefaultRootObject).toEqual("index.html"); |
| 359 | const headers = config?.Origins?.Items?.[0]?.CustomHeaders?.Items?.map((header) => ({ |
| 360 | name: header.HeaderName, |
| 361 | value: |
| 362 | typeof header.HeaderValue === "string" |
| 363 | ? header.HeaderValue |
| 364 | : Redacted.value(header.HeaderValue), |
| 365 | })); |
| 366 | expect(headers).toEqual([{ name: "x-out-of-band", value: "kept" }]); |
| 367 | |
| 368 | yield* stack.destroy(); |
| 369 | yield* assertDistributionDeleted(deployed.distributionId); |
| 370 | }), |
| 371 | { timeout: DEPLOY_DELETE_TIMEOUT }, |
| 372 | ); |
| 373 | |
| 374 | test.provider( |
| 375 | "webFunction origin refuses the AllViewer origin request policy", |
| 376 | (stack) => |
| 377 | Effect.gen(function* () { |
| 378 | const failure = yield* stack |
| 379 | .plan( |
| 380 | Effect.gen(function* () { |
| 381 | const api = yield* AWS.Lambda.WebFunction("HostForwarded", { |
| 382 | main: webFunctionMain, |
| 383 | authType: "IamAuth", |
| 384 | }); |
| 385 | return yield* Distribution("HostForwardedCdn", { |
| 386 | origins: [{ id: "api", webFunction: api }], |
| 387 | defaultCacheBehavior: { |
| 388 | targetOriginId: "api", |
| 389 | // Managed AllViewer: forwards the viewer's Host header. |
| 390 | originRequestPolicyId: "216adef6-5c7f-47e4-b989-5492eafa07d3", |
| 391 | }, |
| 392 | }); |
| 393 | }), |
| 394 | ) |
| 395 | .pipe(Effect.exit); |
| 396 | expect(failure._tag).toBe("Failure"); |
| 397 | expect(String(failure)).toContain("WebFunctionOriginForwardsHost"); |
| 398 | }), |
| 399 | { tags: ["provider:aws:lambda"], timeout: 60_000 }, |
| 400 | ); |
| 401 | |
| 402 | test.provider.skipIf(!runWebFunctions)( |
| 403 | "webFunction origin: CloudFront signs requests to an IamAuth Web Function", |
| 404 | (stack) => |
| 405 | Effect.gen(function* () { |
| 406 | yield* stack.destroy(); |
| 407 | |
| 408 | const { api, distribution } = yield* stack.deploy( |
| 409 | Effect.gen(function* () { |
| 410 | const api = yield* AWS.Lambda.WebFunction("CdnOrigin", { |
| 411 | main: webFunctionMain, |
| 412 | authType: "IamAuth", |
| 413 | }); |
| 414 | // No cache or origin request policy: a webFunction origin |
| 415 | // defaults to passing the whole request through uncached. |
| 416 | const distribution = yield* Distribution("WebFunctionCdn", { |
| 417 | origins: [{ id: "api", webFunction: api }], |
| 418 | defaultCacheBehavior: { targetOriginId: "api" }, |
| 419 | }); |
| 420 | return { api, distribution }; |
| 421 | }), |
| 422 | ); |
| 423 | |
| 424 | // Through CloudFront: signed by the origin access control, with the |
| 425 | // method, query string, body and the viewer's own Authorization |
| 426 | // header all reaching the server. |
| 427 | const viaCdn = yield* HttpClient.execute( |
| 428 | HttpClientRequest.post(`https://${distribution.domainName}/echo?page=2`).pipe( |
| 429 | HttpClientRequest.setHeader("authorization", "Bearer app-token"), |
| 430 | HttpClientRequest.bodyText("ping", "text/plain"), |
| 431 | ), |
| 432 | ).pipe( |
| 433 | Effect.flatMap((response) => |
| 434 | response.status === 200 |
| 435 | ? response.json |
| 436 | : Effect.fail(new Error(`CloudFront returned ${response.status}`)), |
| 437 | ), |
| 438 | Effect.retry({ |
| 439 | schedule: Schedule.max([Schedule.spaced("5 seconds"), Schedule.recurs(36)]), |
| 440 | }), |
| 441 | ); |
| 442 | expect(viaCdn).toEqual({ |
| 443 | message: "hello through cloudfront", |
| 444 | method: "POST", |
| 445 | query: "?page=2", |
| 446 | body: "ping", |
| 447 | endpoint: "default", |
| 448 | authorization: "Bearer app-token", |
| 449 | }); |
| 450 | |
| 451 | // The behavior got the API defaults. |
| 452 | const config = yield* cloudfront.getDistributionConfig({ |
| 453 | Id: distribution.distributionId, |
| 454 | }); |
| 455 | const behavior = config.DistributionConfig!.DefaultCacheBehavior; |
| 456 | expect(behavior.CachePolicyId).toBe(MANAGED_CACHING_DISABLED_POLICY_ID); |
| 457 | expect(behavior.OriginRequestPolicyId).toBe( |
| 458 | MANAGED_ALL_VIEWER_EXCEPT_HOST_HEADER_POLICY_ID, |
| 459 | ); |
| 460 | expect(behavior.AllowedMethods?.Items.sort()).toEqual([ |
| 461 | "DELETE", |
| 462 | "GET", |
| 463 | "HEAD", |
| 464 | "OPTIONS", |
| 465 | "PATCH", |
| 466 | "POST", |
| 467 | "PUT", |
| 468 | ]); |
| 469 | |
| 470 | // Directly, unsigned: refused. |
| 471 | const direct = yield* HttpClient.get(api.url).pipe(Effect.map((r) => r.status)); |
| 472 | expect(direct).toBe(403); |
| 473 | |
| 474 | // Out-of-band: the grant names CloudFront and only this distribution. |
| 475 | const policy = yield* LambdaWeb.getResourcePolicy({ resourceArn: api.functionArn }); |
| 476 | const statements = ( |
| 477 | JSON.parse(policy.policy) as { |
| 478 | Statement: Array<{ |
| 479 | Principal: { Service?: string }; |
| 480 | Resource: string; |
| 481 | Condition?: { StringEquals?: Record<string, string> }; |
| 482 | }>; |
| 483 | } |
| 484 | ).Statement; |
| 485 | expect(statements).toHaveLength(1); |
| 486 | expect(statements[0]!.Principal.Service).toBe("cloudfront.amazonaws.com"); |
| 487 | expect(statements[0]!.Resource).toBe(api.endpointArn); |
| 488 | expect(statements[0]!.Condition?.StringEquals?.["aws:SourceArn"]).toBe( |
| 489 | distribution.distributionArn, |
| 490 | ); |
| 491 | |
| 492 | yield* stack.destroy(); |
| 493 | yield* assertDistributionDeleted(distribution.distributionId); |
| 494 | }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))), |
| 495 | { tags: ["provider:aws:lambda"], timeout: DEPLOY_DELETE_TIMEOUT }, |
| 496 | ); |
| 497 | |
| 498 | test.provider.skipIf(!runWebFunctions)( |
| 499 | "webFunction origin: a multi-Region Web Function is signed with SigV4A", |
| 500 | (stack) => |
| 501 | Effect.gen(function* () { |
| 502 | yield* stack.destroy(); |
| 503 | |
| 504 | const { api, distribution } = yield* stack.deploy( |
| 505 | Effect.gen(function* () { |
| 506 | const api = yield* AWS.Lambda.WebFunction("GlobalCdnOrigin", { |
| 507 | main: webFunctionMain, |
| 508 | authType: "IamAuth", |
| 509 | regions: ["us-east-1"], |
| 510 | }); |
| 511 | const distribution = yield* Distribution("GlobalWebFunctionCdn", { |
| 512 | origins: [{ id: "api", webFunction: api }], |
| 513 | defaultCacheBehavior: { targetOriginId: "api" }, |
| 514 | }); |
| 515 | return { api, distribution }; |
| 516 | }), |
| 517 | ); |
| 518 | expect(api.endpointType).toBe("MultiRegion"); |
| 519 | expect(api.domainName).toContain(".lambda-web.global.on.aws"); |
| 520 | |
| 521 | // The global domain only accepts SigV4A: with SigV4 every request |
| 522 | // through CloudFront is refused. |
| 523 | const viaCdn = yield* HttpClient.get(`https://${distribution.domainName}/`).pipe( |
| 524 | Effect.flatMap((response) => |
| 525 | response.status === 200 |
| 526 | ? response.json |
| 527 | : Effect.fail(new Error(`CloudFront returned ${response.status}`)), |
| 528 | ), |
| 529 | Effect.retry({ |
| 530 | schedule: Schedule.max([Schedule.spaced("5 seconds"), Schedule.recurs(36)]), |
| 531 | }), |
| 532 | ); |
| 533 | expect(viaCdn).toMatchObject({ message: "hello through cloudfront", method: "GET" }); |
| 534 | |
| 535 | // Out-of-band: the origin's access control signs with SigV4A. |
| 536 | const config = yield* cloudfront.getDistributionConfig({ |
| 537 | Id: distribution.distributionId, |
| 538 | }); |
| 539 | const originAccessControlId = |
| 540 | config.DistributionConfig!.Origins.Items[0]!.OriginAccessControlId!; |
| 541 | const control = yield* cloudfront.getOriginAccessControl({ Id: originAccessControlId }); |
| 542 | expect(control.OriginAccessControl?.OriginAccessControlConfig?.SigningProtocol).toBe( |
| 543 | "sigv4a", |
| 544 | ); |
| 545 | |
| 546 | yield* stack.destroy(); |
| 547 | yield* assertDistributionDeleted(distribution.distributionId); |
| 548 | }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))), |
| 549 | { tags: ["provider:aws:lambda"], timeout: DEPLOY_DELETE_TIMEOUT }, |
| 550 | ); |
| 551 | }, |
| 552 | ); |
| 553 | |
| 554 | const assertDistributionDeleted = (distributionId: string) => |
| 555 | cloudfront.getDistribution({ Id: distributionId }).pipe( |
| 556 | Effect.flatMap(() => Effect.fail(new Error("DistributionStillExists"))), |
| 557 | Effect.catchTag("NoSuchDistribution", () => Effect.void), |
| 558 | Effect.retry({ |
| 559 | while: (error) => error instanceof Error && error.message === "DistributionStillExists", |
| 560 | schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(60)]), |
| 561 | }), |
| 562 | ); |