@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 253 lines 9.06 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { ClientVpnAuthorizationRule } from "@alchemy.run/aws/EC2/ClientVpnAuthorizationRule";
3 import { ClientVpnEndpoint } from "@alchemy.run/aws/EC2/ClientVpnEndpoint";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as ec2 from "@distilled.cloud/aws/ec2";
7 import * as Alchemy from "alchemy";
8 import * as Provider from "alchemy/Provider";
9 import * as Effect from "effect/Effect";
10 import {
11 assertClientVpnAuthorizationDeleted,
12 assertClientVpnCertificateDeleted,
13 clientVpnEndpointProps,
14 clientVpnTestTimeout,
15 importClientVpnCertificate,
16 readClientVpnAuthorizationRules,
17 waitForClientVpn,
18 } from "./fixtures/client-vpn.ts";
19
20 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
21 providers: AWS.providers(),
22 });
23 const certificate = beforeAll(importClientVpnCertificate("ClientVpnAuthorizationPrerequisites"));
24 const Stack = Alchemy.Stack(
25 "ClientVpnAuthorizationPrerequisites",
26 { providers: AWS.providers(), state: Alchemy.localState() },
27 Effect.gen(function* () {
28 const certificateArn = yield* certificate;
29 const endpoint = yield* ClientVpnEndpoint("Endpoint", clientVpnEndpointProps(certificateArn));
30 return { endpoint };
31 }),
32 );
33 const prerequisites = beforeAll(deploy(Stack), {
34 timeout: clientVpnTestTimeout,
35 });
36 afterAll(
37 destroy(Stack).pipe(
38 Effect.andThen(certificate.pipe(Effect.flatMap(assertClientVpnCertificateDeleted))),
39 ),
40 { timeout: clientVpnTestTimeout },
41 );
42
43 test.provider.skipIf(!!process.env.FAST)(
44 "creates, lists, replaces, and deletes authorization rules without a subnet association",
45 (stack) =>
46 Effect.gen(function* () {
47 yield* stack.destroy();
48 const { endpoint } = yield* prerequisites;
49 const clientVpnEndpointId = endpoint.clientVpnEndpointId;
50 const deployRule = (targetNetworkCidr: string, description?: string) =>
51 stack.deploy(
52 ClientVpnAuthorizationRule("Authorization", {
53 clientVpnEndpointId,
54 targetNetworkCidr,
55 authorizeAllGroups: true,
56 description,
57 }),
58 );
59 const created = yield* deployRule("10.171.0.0/16", "Initial authorization");
60 expect(created.clientVpnEndpointId).toBe(clientVpnEndpointId);
61 expect(created.targetNetworkCidr).toBe("10.171.0.0/16");
62 const initial = yield* waitForClientVpn(
63 readClientVpnAuthorizationRules(clientVpnEndpointId),
64 (rules) =>
65 rules.some(
66 (rule) => rule.DestinationCidr === "10.171.0.0/16" && rule.Status?.Code === "active",
67 ),
68 "active authorization",
69 );
70 expect(initial).toContainEqual(
71 expect.objectContaining({
72 DestinationCidr: "10.171.0.0/16",
73 AccessAll: true,
74 Description: "Initial authorization",
75 }),
76 );
77 const provider = yield* Provider.findProvider(ClientVpnAuthorizationRule);
78 const listed = yield* waitForClientVpn(
79 provider.list(),
80 (rules) =>
81 rules.some(
82 (rule) =>
83 rule.clientVpnEndpointId === clientVpnEndpointId &&
84 rule.targetNetworkCidr === "10.171.0.0/16",
85 ),
86 "authorization provider list",
87 );
88 expect(listed).toContainEqual(
89 expect.objectContaining({
90 clientVpnEndpointId,
91 targetNetworkCidr: "10.171.0.0/16",
92 authorizeAllGroups: true,
93 }),
94 );
95
96 yield* deployRule("10.171.0.0/16", "Initial authorization");
97 expect(
98 (yield* readClientVpnAuthorizationRules(clientVpnEndpointId)).filter(
99 (rule) => rule.DestinationCidr === "10.171.0.0/16" && rule.Status?.Code === "active",
100 ),
101 ).toHaveLength(1);
102
103 // AWS has no modify-authorization API; a description change must revoke and recreate.
104 yield* deployRule("10.171.0.0/16", "Updated authorization");
105 const updated = yield* waitForClientVpn(
106 readClientVpnAuthorizationRules(clientVpnEndpointId),
107 (rules) =>
108 rules.some(
109 (rule) =>
110 rule.DestinationCidr === "10.171.0.0/16" &&
111 rule.Description === "Updated authorization" &&
112 rule.Status?.Code === "active",
113 ),
114 "replaced authorization description",
115 );
116 expect(
117 updated.filter(
118 (rule) => rule.DestinationCidr === "10.171.0.0/16" && rule.Status?.Code === "active",
119 ),
120 ).toHaveLength(1);
121
122 const replaced = yield* deployRule("10.172.0.0/16");
123 expect(replaced.targetNetworkCidr).toBe("10.172.0.0/16");
124 const replacement = yield* waitForClientVpn(
125 readClientVpnAuthorizationRules(clientVpnEndpointId),
126 (rules) =>
127 rules.some(
128 (rule) => rule.DestinationCidr === "10.172.0.0/16" && rule.Status?.Code === "active",
129 ),
130 "replacement authorization CIDR",
131 );
132 expect(
133 replacement.find(
134 (rule) => rule.DestinationCidr === "10.172.0.0/16" && rule.Status?.Code === "active",
135 )?.Description ?? "",
136 ).toBe("");
137 yield* assertClientVpnAuthorizationDeleted(clientVpnEndpointId, "10.171.0.0/16");
138 yield* stack.destroy();
139 yield* assertClientVpnAuthorizationDeleted(clientVpnEndpointId, "10.172.0.0/16");
140 }),
141 {
142 tags: ["provider:aws", "provider:aws:acm", "provider:aws:ec2", "live"],
143 timeout: clientVpnTestTimeout,
144 },
145 );
146
147 test.provider.skipIf(!!process.env.FAST)(
148 "recreates an authorization rule revoked out of band",
149 (stack) =>
150 Effect.gen(function* () {
151 yield* stack.destroy();
152 const { endpoint } = yield* prerequisites;
153 const clientVpnEndpointId = endpoint.clientVpnEndpointId;
154 const targetNetworkCidr = "10.173.0.0/16";
155 const program = ClientVpnAuthorizationRule("Authorization", {
156 clientVpnEndpointId,
157 targetNetworkCidr,
158 authorizeAllGroups: true,
159 description: "Managed authorization",
160 });
161 yield* stack.deploy(program);
162 yield* ec2.revokeClientVpnIngress({
163 ClientVpnEndpointId: clientVpnEndpointId,
164 TargetNetworkCidr: targetNetworkCidr,
165 RevokeAllGroups: true,
166 });
167 yield* assertClientVpnAuthorizationDeleted(clientVpnEndpointId, targetNetworkCidr);
168 yield* stack.deploy(program);
169 const repaired = yield* waitForClientVpn(
170 readClientVpnAuthorizationRules(clientVpnEndpointId),
171 (rules) =>
172 rules.some(
173 (rule) => rule.DestinationCidr === targetNetworkCidr && rule.Status?.Code === "active",
174 ),
175 "recreated authorization",
176 );
177 expect(repaired).toContainEqual(
178 expect.objectContaining({
179 DestinationCidr: targetNetworkCidr,
180 AccessAll: true,
181 Description: "Managed authorization",
182 }),
183 );
184 yield* stack.destroy();
185 yield* assertClientVpnAuthorizationDeleted(clientVpnEndpointId, targetNetworkCidr);
186 }),
187 {
188 tags: ["provider:aws", "provider:aws:acm", "provider:aws:ec2", "live"],
189 timeout: clientVpnTestTimeout,
190 },
191 );
192
193 test.provider.skipIf(!!process.env.FAST)(
194 "discovers an authorization rule by natural key without prior state and deletes it",
195 (stack) =>
196 Effect.gen(function* () {
197 yield* stack.destroy();
198 const { endpoint } = yield* prerequisites;
199 const clientVpnEndpointId = endpoint.clientVpnEndpointId;
200 const targetNetworkCidr = "10.174.0.0/16";
201 const description = "Cold natural-key discovery";
202 yield* ec2.authorizeClientVpnIngress({
203 ClientVpnEndpointId: clientVpnEndpointId,
204 TargetNetworkCidr: targetNetworkCidr,
205 AuthorizeAllGroups: true,
206 Description: description,
207 });
208 const preexisting = yield* waitForClientVpn(
209 readClientVpnAuthorizationRules(clientVpnEndpointId),
210 (rules) =>
211 rules.some(
212 (rule) =>
213 rule.DestinationCidr === targetNetworkCidr &&
214 rule.AccessAll === true &&
215 rule.Status?.Code === "active",
216 ),
217 "SDK-created authorization",
218 );
219 const original = preexisting.filter(
220 (rule) => rule.DestinationCidr === targetNetworkCidr && rule.AccessAll === true,
221 );
222 expect(original).toHaveLength(1);
223
224 const managed = yield* stack.deploy(
225 ClientVpnAuthorizationRule("Authorization", {
226 clientVpnEndpointId,
227 targetNetworkCidr,
228 authorizeAllGroups: true,
229 description,
230 }),
231 );
232 expect(managed).toMatchObject({
233 clientVpnEndpointId,
234 targetNetworkCidr,
235 authorizeAllGroups: true,
236 description,
237 status: "active",
238 });
239 const observed = yield* readClientVpnAuthorizationRules(clientVpnEndpointId);
240 expect(
241 observed.filter(
242 (rule) => rule.DestinationCidr === targetNetworkCidr && rule.AccessAll === true,
243 ),
244 ).toEqual(original);
245
246 yield* stack.destroy();
247 yield* assertClientVpnAuthorizationDeleted(clientVpnEndpointId, targetNetworkCidr);
248 }),
249 {
250 tags: ["provider:aws", "provider:aws:acm", "provider:aws:ec2", "live"],
251 timeout: clientVpnTestTimeout,
252 },
253 );