The best way to control Claude Code, Codex and more from any computer or phone

main
ts 404 lines 17.7 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { SecurityGroup, Subnet, Vpc } from "@alchemy.run/aws/EC2";
3 import {
4 ClientVpnEndpoint,
5 type ClientVpnEndpointProps,
6 } from "@alchemy.run/aws/EC2/ClientVpnEndpoint";
7 import { LogGroup, LogStream } from "@alchemy.run/aws/Logs";
8 import { describe, expect } from "@alchemy.run/test";
9 import * as Test from "@alchemy.run/test/Alchemy";
10 import * as ec2 from "@distilled.cloud/aws/ec2";
11 import * as Alchemy from "alchemy";
12 import * as Provider from "alchemy/Provider";
13 import * as Effect from "effect/Effect";
14 import {
15 assertClientVpnCertificateDeleted,
16 assertClientVpnEndpointDeleted,
17 clientVpnAvailabilityZones,
18 clientVpnEndpointProps,
19 clientVpnTestTimeout,
20 expectClientVpnOwnershipTags,
21 importClientVpnCertificate,
22 readClientVpnEndpoint,
23 readClientVpnTargetNetworks,
24 waitForClientVpn,
25 } from "./fixtures/client-vpn.ts";
26
27 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ providers: AWS.providers() });
28 const certificate = beforeAll(importClientVpnCertificate("ClientVpnEndpointPrerequisites"));
29 const zones = beforeAll(clientVpnAvailabilityZones);
30 const Stack = Alchemy.Stack(
31 "ClientVpnEndpointPrerequisites",
32 { providers: AWS.providers(), state: Alchemy.localState() },
33 Effect.gen(function* () {
34 const certificateArn = yield* certificate;
35 const vpc = yield* Vpc("Vpc", { cidrBlock: "10.171.0.0/16" });
36 const subnet = yield* Subnet("Subnet", {
37 vpcId: vpc.vpcId,
38 cidrBlock: "10.171.1.0/24",
39 availabilityZone: (yield* zones).firstZone,
40 });
41 const firstGroup = yield* SecurityGroup("FirstGroup", {
42 vpcId: vpc.vpcId,
43 description: "Client VPN first security group",
44 });
45 const secondGroup = yield* SecurityGroup("SecondGroup", {
46 vpcId: vpc.vpcId,
47 description: "Client VPN second security group",
48 });
49 const logs = yield* LogGroup("ConnectionLogs", { retention: "1 day" });
50 const stream = yield* LogStream("ConnectionLogStream", { logGroupName: logs.logGroupName });
51 return { certificateArn, vpc, subnet, firstGroup, secondGroup, logs, stream };
52 }),
53 );
54 const prerequisites = beforeAll(deploy(Stack), { timeout: clientVpnTestTimeout });
55 afterAll(
56 destroy(Stack).pipe(
57 Effect.andThen(certificate.pipe(Effect.flatMap(assertClientVpnCertificateDeleted))),
58 ),
59 { timeout: clientVpnTestTimeout },
60 );
61
62 // Keep replacement generations within the account's Client VPN endpoint quota.
63 describe.sequential(
64 "Client VPN endpoints",
65 { tags: ["provider:aws", "provider:aws:acm", "provider:aws:ec2", "provider:aws:logs", "live"] },
66 () => {
67 test.provider.skipIf(!!process.env.FAST)(
68 "creates, lists, updates, removes optional settings, and deletes a Client VPN endpoint",
69 (stack) =>
70 Effect.gen(function* () {
71 yield* stack.destroy();
72 const fixture = yield* prerequisites;
73 const base = {
74 ...clientVpnEndpointProps(fixture.certificateArn),
75 vpcId: fixture.vpc.vpcId,
76 };
77 const deployEndpoint = (props: Partial<ClientVpnEndpointProps>) =>
78 stack.deploy(ClientVpnEndpoint("Endpoint", { ...base, ...props }));
79 const created = yield* deployEndpoint({
80 description: "Client VPN initial description",
81 dnsServers: ["1.1.1.1"],
82 vpnPort: 443,
83 splitTunnel: false,
84 connectionLogOptions: { enabled: false },
85 securityGroupIds: [fixture.firstGroup.groupId],
86 sessionTimeoutHours: 8,
87 clientLoginBannerOptions: { enabled: true, bannerText: "Initial banner" },
88 clientConnectOptions: { enabled: false },
89 selfServicePortal: "disabled",
90 tags: { Environment: "initial", RemoveMe: "yes" },
91 });
92 expect(created.clientVpnEndpointId).toMatch(/^cvpn-endpoint-/);
93 expect(created.dnsName).toBeTruthy();
94 expect(created.status).toBeDefined();
95 const initial = yield* waitForClientVpn(
96 readClientVpnEndpoint(created.clientVpnEndpointId),
97 (endpoint) => endpoint?.Status?.Code === "pending-associate",
98 created.clientVpnEndpointId,
99 );
100 expect(initial).toMatchObject({
101 ClientVpnEndpointId: created.clientVpnEndpointId,
102 ClientCidrBlock: base.clientCidrBlock,
103 ServerCertificateArn: fixture.certificateArn,
104 TransportProtocol: "udp",
105 VpnPort: 443,
106 SplitTunnel: false,
107 VpcId: fixture.vpc.vpcId,
108 SecurityGroupIds: [fixture.firstGroup.groupId],
109 DnsServers: ["1.1.1.1"],
110 SessionTimeoutHours: 8,
111 ConnectionLogOptions: { Enabled: false },
112 ClientLoginBannerOptions: { Enabled: true, BannerText: "Initial banner" },
113 AuthenticationOptions: [
114 {
115 Type: "certificate-authentication",
116 MutualAuthentication: { ClientRootCertificateChain: fixture.certificateArn },
117 },
118 ],
119 });
120 expectClientVpnOwnershipTags(initial?.Tags, stack, "Endpoint");
121 const provider = yield* Provider.findProvider(ClientVpnEndpoint);
122 const listed = yield* waitForClientVpn(
123 provider.list(),
124 (endpoints) =>
125 endpoints.some(
126 (endpoint) => endpoint.clientVpnEndpointId === created.clientVpnEndpointId,
127 ),
128 "Client VPN endpoint provider list",
129 );
130 expect(
131 listed.find((endpoint) => endpoint.clientVpnEndpointId === created.clientVpnEndpointId),
132 ).toMatchObject({ clientCidrBlock: base.clientCidrBlock });
133
134 const updated = yield* deployEndpoint({
135 description: "Client VPN updated description",
136 dnsServers: ["8.8.8.8", "8.8.4.4"],
137 vpnPort: 1194,
138 splitTunnel: true,
139 connectionLogOptions: {
140 enabled: true,
141 cloudwatchLogGroup: fixture.logs.logGroupName,
142 cloudwatchLogStream: fixture.stream.logStreamName,
143 },
144 securityGroupIds: [fixture.secondGroup.groupId],
145 sessionTimeoutHours: 10,
146 clientLoginBannerOptions: { enabled: true, bannerText: "Updated banner" },
147 tags: { Environment: "updated" },
148 });
149 expect(updated.clientVpnEndpointId).toBe(created.clientVpnEndpointId);
150 expect(updated).toMatchObject({
151 description: "Client VPN updated description",
152 dnsServers: ["8.8.8.8", "8.8.4.4"],
153 vpnPort: 1194,
154 splitTunnel: true,
155 securityGroupIds: [fixture.secondGroup.groupId],
156 sessionTimeoutHours: 10,
157 connectionLogOptions: {
158 enabled: true,
159 cloudwatchLogGroup: fixture.logs.logGroupName,
160 cloudwatchLogStream: fixture.stream.logStreamName,
161 },
162 clientLoginBannerOptions: { enabled: true, bannerText: "Updated banner" },
163 });
164 const observed = yield* waitForClientVpn(
165 readClientVpnEndpoint(updated.clientVpnEndpointId),
166 (endpoint) =>
167 endpoint?.Description === "Client VPN updated description" &&
168 endpoint.SessionTimeoutHours === 10 &&
169 endpoint.SecurityGroupIds?.includes(fixture.secondGroup.groupId) === true,
170 "updated endpoint settings",
171 );
172 expect(observed).toMatchObject({
173 DnsServers: ["8.8.8.8", "8.8.4.4"],
174 VpnPort: 1194,
175 SplitTunnel: true,
176 ConnectionLogOptions: {
177 Enabled: true,
178 CloudwatchLogGroup: fixture.logs.logGroupName,
179 CloudwatchLogStream: fixture.stream.logStreamName,
180 },
181 ClientLoginBannerOptions: { Enabled: true, BannerText: "Updated banner" },
182 });
183 expect(observed?.Tags).toContainEqual({ Key: "Environment", Value: "updated" });
184 expect(observed?.Tags?.some((tag) => tag.Key === "RemoveMe")).toBe(false);
185 expectClientVpnOwnershipTags(observed?.Tags, stack, "Endpoint");
186
187 const defaults = yield* deployEndpoint({});
188 expect(defaults.clientVpnEndpointId).toBe(created.clientVpnEndpointId);
189 expect(defaults).toMatchObject({
190 description: "",
191 dnsServers: [],
192 vpnPort: 443,
193 splitTunnel: false,
194 sessionTimeoutHours: 24,
195 connectionLogOptions: { enabled: false },
196 clientLoginBannerOptions: { enabled: false },
197 clientConnectOptions: { enabled: false },
198 });
199 const reset = yield* waitForClientVpn(
200 readClientVpnEndpoint(defaults.clientVpnEndpointId),
201 (endpoint) =>
202 endpoint !== undefined &&
203 !endpoint.Description &&
204 (endpoint.DnsServers ?? []).length === 0 &&
205 endpoint.SessionTimeoutHours === 24 &&
206 endpoint.ClientLoginBannerOptions?.Enabled !== true,
207 "endpoint default restoration",
208 );
209 expect(reset).toMatchObject({
210 VpnPort: 443,
211 SplitTunnel: false,
212 ConnectionLogOptions: { Enabled: false },
213 });
214 expect(reset?.ClientConnectOptions?.Enabled ?? false).toBe(false);
215 expect(reset?.ClientLoginBannerOptions?.Enabled ?? false).toBe(false);
216 expect(reset?.Tags?.some((tag) => tag.Key === "Environment")).toBe(false);
217 const groups = yield* ec2.describeSecurityGroups({
218 Filters: [
219 { Name: "vpc-id", Values: [fixture.vpc.vpcId] },
220 { Name: "group-name", Values: ["default"] },
221 ],
222 });
223 expect(reset?.SecurityGroupIds).toEqual([groups.SecurityGroups?.[0]?.GroupId]);
224 expectClientVpnOwnershipTags(reset?.Tags, stack, "Endpoint");
225 yield* stack.destroy();
226 yield* assertClientVpnEndpointDeleted(created.clientVpnEndpointId);
227 }),
228 { timeout: clientVpnTestTimeout },
229 );
230
231 test.provider.skipIf(!!process.env.FAST)(
232 "repairs out-of-band endpoint settings and tags with unchanged desired props",
233 (stack) =>
234 Effect.gen(function* () {
235 yield* stack.destroy();
236 const { certificateArn } = yield* prerequisites;
237 const props: ClientVpnEndpointProps = {
238 ...clientVpnEndpointProps(certificateArn),
239 description: "Managed description",
240 dnsServers: ["1.1.1.1"],
241 splitTunnel: true,
242 tags: { Environment: "managed" },
243 };
244 const program = ClientVpnEndpoint("Endpoint", props);
245 const created = yield* stack.deploy(program);
246 yield* ec2.modifyClientVpnEndpoint({
247 ClientVpnEndpointId: created.clientVpnEndpointId,
248 Description: "Out-of-band description",
249 DnsServers: { CustomDnsServers: ["8.8.8.8"], Enabled: true },
250 SplitTunnel: false,
251 });
252 yield* ec2.createTags({
253 Resources: [created.clientVpnEndpointId],
254 Tags: [
255 { Key: "Environment", Value: "drifted" },
256 { Key: "Unmanaged", Value: "remove" },
257 ],
258 });
259 yield* waitForClientVpn(
260 readClientVpnEndpoint(created.clientVpnEndpointId),
261 (endpoint) => endpoint?.Description === "Out-of-band description",
262 "out-of-band endpoint modification",
263 );
264 const plan = yield* stack.plan(program);
265 expect(plan.resources.Endpoint?.action).toBe("update");
266 const repaired = yield* stack.deploy(program);
267 expect(repaired.clientVpnEndpointId).toBe(created.clientVpnEndpointId);
268 const observed = yield* waitForClientVpn(
269 readClientVpnEndpoint(created.clientVpnEndpointId),
270 (endpoint) => endpoint?.Description === props.description,
271 "repaired endpoint",
272 );
273 expect(observed).toMatchObject({ DnsServers: ["1.1.1.1"], SplitTunnel: true });
274 expect(observed?.Tags).toContainEqual({ Key: "Environment", Value: "managed" });
275 expect(observed?.Tags?.some((tag) => tag.Key === "Unmanaged")).toBe(false);
276 expectClientVpnOwnershipTags(observed?.Tags, stack, "Endpoint");
277 yield* stack.destroy();
278 yield* assertClientVpnEndpointDeleted(created.clientVpnEndpointId);
279 }),
280 { timeout: clientVpnTestTimeout },
281 );
282
283 test.provider.skipIf(!!process.env.FAST)(
284 "enables group-only logging from a newly created dependency without replacing the endpoint",
285 (stack) =>
286 Effect.gen(function* () {
287 yield* stack.destroy();
288 const { certificateArn } = yield* prerequisites;
289 const program = (logging: boolean) =>
290 Effect.gen(function* () {
291 const logs = logging
292 ? yield* LogGroup("NewConnectionLogs", { retention: "1 day" })
293 : undefined;
294 const endpoint = yield* ClientVpnEndpoint("Endpoint", {
295 ...clientVpnEndpointProps(certificateArn),
296 connectionLogOptions: logs
297 ? { enabled: true, cloudwatchLogGroup: logs.logGroupName }
298 : undefined,
299 });
300 return { endpoint, logs };
301 });
302 const created = yield* stack.deploy(program(false));
303 const plan = yield* stack.plan(program(true));
304 expect(plan.resources.Endpoint?.action).toBe("update");
305 expect(plan.resources.NewConnectionLogs?.action).toBe("create");
306 const updated = yield* stack.deploy(program(true));
307 expect(updated.endpoint.clientVpnEndpointId).toBe(created.endpoint.clientVpnEndpointId);
308 const observed = yield* readClientVpnEndpoint(updated.endpoint.clientVpnEndpointId);
309 expect(observed?.ConnectionLogOptions).toMatchObject({
310 Enabled: true,
311 CloudwatchLogGroup: updated.logs?.logGroupName,
312 });
313 expect((yield* stack.plan(program(true))).resources.Endpoint?.action).toBe("noop");
314 yield* stack.destroy();
315 yield* assertClientVpnEndpointDeleted(updated.endpoint.clientVpnEndpointId);
316 }),
317 { timeout: clientVpnTestTimeout },
318 );
319
320 test.provider.skipIf(!!process.env.FAST)(
321 "fails with a typed error when the configured VPC has been deleted",
322 (stack) =>
323 Effect.gen(function* () {
324 yield* stack.destroy();
325 const { certificateArn } = yield* prerequisites;
326 const vpc = yield* stack.deploy(Vpc("DeletedVpc", { cidrBlock: "10.175.0.0/16" }));
327 yield* stack.destroy();
328 const failure = yield* stack
329 .deploy(
330 ClientVpnEndpoint("Endpoint", {
331 ...clientVpnEndpointProps(certificateArn),
332 vpcId: vpc.vpcId,
333 }),
334 )
335 .pipe(Effect.flip);
336 expect(failure).toMatchObject({ _tag: "InvalidVpcID.NotFound" });
337 yield* stack.destroy();
338 }),
339 { timeout: clientVpnTestTimeout },
340 );
341
342 test.provider.skipIf(!!process.env.FAST)(
343 "replaces an endpoint for client CIDR and transport protocol changes",
344 (stack) =>
345 Effect.gen(function* () {
346 yield* stack.destroy();
347 const { certificateArn } = yield* prerequisites;
348 const deployEndpoint = (clientCidrBlock: string, transportProtocol: "udp" | "tcp") =>
349 stack.deploy(
350 ClientVpnEndpoint("Endpoint", {
351 ...clientVpnEndpointProps(certificateArn),
352 clientCidrBlock,
353 transportProtocol,
354 }),
355 );
356 const created = yield* deployEndpoint("172.20.0.0/22", "udp");
357 const changedCidr = yield* deployEndpoint("172.20.4.0/22", "udp");
358 expect(changedCidr.clientVpnEndpointId).not.toBe(created.clientVpnEndpointId);
359 expect(yield* readClientVpnEndpoint(changedCidr.clientVpnEndpointId)).toMatchObject({
360 ClientCidrBlock: "172.20.4.0/22",
361 TransportProtocol: "udp",
362 });
363 yield* assertClientVpnEndpointDeleted(created.clientVpnEndpointId);
364 const changedProtocol = yield* deployEndpoint("172.20.4.0/22", "tcp");
365 expect(changedProtocol.clientVpnEndpointId).not.toBe(changedCidr.clientVpnEndpointId);
366 expect(yield* readClientVpnEndpoint(changedProtocol.clientVpnEndpointId)).toMatchObject({
367 ClientCidrBlock: "172.20.4.0/22",
368 TransportProtocol: "tcp",
369 });
370 yield* assertClientVpnEndpointDeleted(changedCidr.clientVpnEndpointId);
371 yield* stack.destroy();
372 yield* assertClientVpnEndpointDeleted(changedProtocol.clientVpnEndpointId);
373 }),
374 { timeout: clientVpnTestTimeout },
375 );
376
377 test.provider.skipIf(!!process.env.FAST)(
378 "deletes an endpoint whose target network was associated out of band",
379 (stack) =>
380 Effect.gen(function* () {
381 yield* stack.destroy();
382 const { certificateArn, vpc, subnet } = yield* prerequisites;
383 const endpoint = yield* stack.deploy(
384 ClientVpnEndpoint("Endpoint", {
385 ...clientVpnEndpointProps(certificateArn),
386 vpcId: vpc.vpcId,
387 }),
388 );
389 yield* ec2.associateClientVpnTargetNetwork({
390 ClientVpnEndpointId: endpoint.clientVpnEndpointId,
391 SubnetId: subnet.subnetId,
392 });
393 yield* waitForClientVpn(
394 readClientVpnTargetNetworks(endpoint.clientVpnEndpointId),
395 (networks) => networks.some((network) => network.Status?.Code === "associated"),
396 "out-of-band target network association",
397 );
398 yield* stack.destroy();
399 yield* assertClientVpnEndpointDeleted(endpoint.clientVpnEndpointId);
400 }),
401 { timeout: clientVpnTestTimeout },
402 );
403 },
404 );