@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 256 lines 9.85 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import {
3 CachePolicy,
4 Distribution,
5 KeyGroup,
6 OriginAccessControl,
7 OriginRequestPolicy,
8 PublicKey,
9 ResponseHeadersPolicy,
10 } from "@alchemy.run/aws/CloudFront";
11 import type { PolicyStatement } from "@alchemy.run/aws/IAM/Policy";
12 import { Bucket } from "@alchemy.run/aws/S3";
13 import { describe, expect } from "@alchemy.run/test";
14 import * as Test from "@alchemy.run/test/Alchemy";
15 import * as cloudfront from "@distilled.cloud/aws/cloudfront";
16 import * as Output from "alchemy/Output";
17 import * as Effect from "effect/Effect";
18 import * as Schedule from "effect/Schedule";
19
20 const { test } = Test.make({ providers: AWS.providers() });
21
22 const runLive = !process.env.FAST;
23
24 const SIGNING_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
25 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvTkfqkMHU8HMmIRKJaMl
26 IoD691g60aS15QlaP/DVkpuoeEp8JA8YDs5vQFu6HSIYCTQ7WwFx9oRvN08i7yXB
27 EHt3x7uZVpdkp6JBbjR9BGNsAVri6DZ0TJQ11zWeN3keqhnUdFhQjPwT+u4r6oKk
28 kNvkl7eU2nFK+UIaPlD+rA+AlYT0m7gSVcd9KaLf/UzBrtSy1dbXYDT4dHChMUVy
29 4gDsQ6D4u6lRAHY9jcKxlgEIM+O8ODKyzlbergv2EwhANG4P27DBeDhA/off3upM
30 TTVTGKZeoABtqM0ZiYq0cDgf8KUn9NPxSdnJ4+cbigLjJBPS93VYWzWX0HXlZpQ3
31 HQIDAQAB
32 -----END PUBLIC KEY-----
33 `;
34
35 describe(
36 "AWS.CloudFront smoke",
37 {
38 tags: [
39 "provider:aws",
40 "provider:aws:cloudfront",
41 "provider:aws:iam",
42 "provider:aws:s3",
43 "live",
44 ],
45 },
46 () => {
47 test.provider.skipIf(!runLive)(
48 "deploy distribution wired to cache, origin request, and response headers policies alongside a key group",
49 (stack) =>
50 Effect.gen(function* () {
51 yield* stack.destroy();
52
53 const deployed = yield* stack.deploy(
54 Effect.gen(function* () {
55 const bucket = yield* Bucket("SmokeBucket", { forceDestroy: true });
56 const oac = yield* OriginAccessControl("SmokeOac", { originType: "s3" });
57
58 const cachePolicy = yield* CachePolicy("SmokeCachePolicy", {
59 comment: "smoke",
60 minTTL: 0,
61 defaultTTL: "60 seconds",
62 maxTTL: "1 day",
63 parametersInCacheKeyAndForwardedToOrigin: {
64 EnableAcceptEncodingGzip: true,
65 EnableAcceptEncodingBrotli: true,
66 HeadersConfig: { HeaderBehavior: "none" },
67 CookiesConfig: { CookieBehavior: "none" },
68 QueryStringsConfig: { QueryStringBehavior: "none" },
69 },
70 });
71
72 const originRequestPolicy = yield* OriginRequestPolicy("SmokeOriginRequestPolicy", {
73 comment: "smoke",
74 headersConfig: { HeaderBehavior: "none" },
75 cookiesConfig: { CookieBehavior: "none" },
76 queryStringsConfig: { QueryStringBehavior: "all" },
77 });
78
79 const responseHeadersPolicy = yield* ResponseHeadersPolicy(
80 "SmokeResponseHeadersPolicy",
81 {
82 comment: "smoke",
83 securityHeadersConfig: {
84 StrictTransportSecurity: {
85 AccessControlMaxAgeSec: 31536000,
86 IncludeSubdomains: true,
87 Preload: true,
88 Override: true,
89 },
90 ContentTypeOptions: { Override: true },
91 FrameOptions: { FrameOption: "DENY", Override: true },
92 },
93 },
94 );
95
96 const publicKey = yield* PublicKey("SmokeSigningKey", {
97 encodedKey: SIGNING_PUBLIC_KEY,
98 comment: "smoke signed-url key",
99 });
100
101 const keyGroup = yield* KeyGroup("SmokeSigningKeys", {
102 comment: "smoke signed-url group",
103 items: [publicKey.publicKeyId],
104 });
105
106 const distribution = yield* Distribution("SmokeDistribution", {
107 origins: [
108 {
109 id: "site",
110 domainName: bucket.bucketRegionalDomainName,
111 s3Origin: true,
112 originAccessControlId: oac.originAccessControlId,
113 },
114 ],
115 defaultRootObject: "index.html",
116 defaultCacheBehavior: {
117 targetOriginId: "site",
118 viewerProtocolPolicy: "redirect-to-https",
119 compress: true,
120 allowedMethods: ["GET", "HEAD"],
121 cachedMethods: ["GET", "HEAD"],
122 cachePolicyId: cachePolicy.cachePolicyId,
123 originRequestPolicyId: originRequestPolicy.originRequestPolicyId,
124 responseHeadersPolicyId: responseHeadersPolicy.responseHeadersPolicyId,
125 },
126 });
127
128 const statement: PolicyStatement = {
129 Effect: "Allow",
130 Principal: { Service: "cloudfront.amazonaws.com" },
131 Action: ["s3:GetObject"],
132 Resource: [Output.interpolate`${bucket.bucketArn}/*` as any],
133 Condition: {
134 StringEquals: { "AWS:SourceArn": distribution.distributionArn as any },
135 },
136 };
137
138 yield* bucket.bind`Allow(${distribution}, CloudFront.Read(${bucket}))`({
139 policyStatements: [statement],
140 });
141
142 return {
143 bucket,
144 oac,
145 cachePolicy,
146 originRequestPolicy,
147 responseHeadersPolicy,
148 publicKey,
149 keyGroup,
150 distribution,
151 };
152 }),
153 );
154
155 const dist = yield* cloudfront.getDistribution({
156 Id: deployed.distribution.distributionId,
157 });
158 expect(dist.Distribution?.Status).toEqual("Deployed");
159
160 const defaultBehavior = dist.Distribution?.DistributionConfig?.DefaultCacheBehavior;
161 expect(defaultBehavior?.CachePolicyId).toEqual(deployed.cachePolicy.cachePolicyId);
162 expect(defaultBehavior?.OriginRequestPolicyId).toEqual(
163 deployed.originRequestPolicy.originRequestPolicyId,
164 );
165 expect(defaultBehavior?.ResponseHeadersPolicyId).toEqual(
166 deployed.responseHeadersPolicy.responseHeadersPolicyId,
167 );
168
169 const cachePolicy = yield* cloudfront.getCachePolicy({
170 Id: deployed.cachePolicy.cachePolicyId,
171 });
172 expect(cachePolicy.CachePolicy?.Id).toEqual(deployed.cachePolicy.cachePolicyId);
173
174 const originRequestPolicy = yield* cloudfront.getOriginRequestPolicy({
175 Id: deployed.originRequestPolicy.originRequestPolicyId,
176 });
177 expect(originRequestPolicy.OriginRequestPolicy?.Id).toEqual(
178 deployed.originRequestPolicy.originRequestPolicyId,
179 );
180
181 const responseHeadersPolicy = yield* cloudfront.getResponseHeadersPolicy({
182 Id: deployed.responseHeadersPolicy.responseHeadersPolicyId,
183 });
184 expect(responseHeadersPolicy.ResponseHeadersPolicy?.Id).toEqual(
185 deployed.responseHeadersPolicy.responseHeadersPolicyId,
186 );
187
188 const keyGroup = yield* cloudfront.getKeyGroup({ Id: deployed.keyGroup.keyGroupId });
189 expect(keyGroup.KeyGroup?.KeyGroupConfig?.Items).toEqual([
190 deployed.publicKey.publicKeyId,
191 ]);
192
193 yield* stack.destroy();
194 yield* assertDistributionDeleted(deployed.distribution.distributionId);
195 yield* assertCachePolicyDeleted(deployed.cachePolicy.cachePolicyId);
196 yield* assertOriginRequestPolicyDeleted(
197 deployed.originRequestPolicy.originRequestPolicyId,
198 );
199 yield* assertResponseHeadersPolicyDeleted(
200 deployed.responseHeadersPolicy.responseHeadersPolicyId,
201 );
202 yield* assertKeyGroupDeleted(deployed.keyGroup.keyGroupId);
203 yield* assertPublicKeyDeleted(deployed.publicKey.publicKeyId);
204 }),
205 900_000,
206 );
207 },
208 );
209
210 const retrying = (label: string) =>
211 Effect.retry({
212 while: (error: unknown) => error instanceof Error && error.message === label,
213 schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(60)]),
214 });
215
216 const assertDistributionDeleted = (id: string) =>
217 cloudfront.getDistribution({ Id: id }).pipe(
218 Effect.flatMap(() => Effect.fail(new Error("DistributionStillExists"))),
219 Effect.catchTag("NoSuchDistribution", () => Effect.void),
220 retrying("DistributionStillExists"),
221 );
222
223 const assertCachePolicyDeleted = (id: string) =>
224 cloudfront.getCachePolicy({ Id: id }).pipe(
225 Effect.flatMap(() => Effect.fail(new Error("CachePolicyStillExists"))),
226 Effect.catchTag("NoSuchCachePolicy", () => Effect.void),
227 retrying("CachePolicyStillExists"),
228 );
229
230 const assertOriginRequestPolicyDeleted = (id: string) =>
231 cloudfront.getOriginRequestPolicy({ Id: id }).pipe(
232 Effect.flatMap(() => Effect.fail(new Error("OriginRequestPolicyStillExists"))),
233 Effect.catchTag("NoSuchOriginRequestPolicy", () => Effect.void),
234 retrying("OriginRequestPolicyStillExists"),
235 );
236
237 const assertResponseHeadersPolicyDeleted = (id: string) =>
238 cloudfront.getResponseHeadersPolicy({ Id: id }).pipe(
239 Effect.flatMap(() => Effect.fail(new Error("ResponseHeadersPolicyStillExists"))),
240 Effect.catchTag("NoSuchResponseHeadersPolicy", () => Effect.void),
241 retrying("ResponseHeadersPolicyStillExists"),
242 );
243
244 const assertKeyGroupDeleted = (id: string) =>
245 cloudfront.getKeyGroup({ Id: id }).pipe(
246 Effect.flatMap(() => Effect.fail(new Error("KeyGroupStillExists"))),
247 Effect.catchTag("NoSuchResource", () => Effect.void),
248 retrying("KeyGroupStillExists"),
249 );
250
251 const assertPublicKeyDeleted = (id: string) =>
252 cloudfront.getPublicKey({ Id: id }).pipe(
253 Effect.flatMap(() => Effect.fail(new Error("PublicKeyStillExists"))),
254 Effect.catchTag("NoSuchPublicKey", () => Effect.void),
255 retrying("PublicKeyStillExists"),
256 );