The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { Subnet, Vpc } from "@alchemy.run/aws/EC2"; |
| 3 | import type { ClientVpnEndpointProps } from "@alchemy.run/aws/EC2/ClientVpnEndpoint"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import { withProviders } from "@alchemy.run/test/Core"; |
| 6 | import * as acm from "@distilled.cloud/aws/acm"; |
| 7 | import * as ec2 from "@distilled.cloud/aws/ec2"; |
| 8 | import { createInternalTags, createTagsList } from "alchemy/Tags"; |
| 9 | import * as Data from "effect/Data"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as Redacted from "effect/Redacted"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import * as Stream from "effect/Stream"; |
| 14 | import { |
| 15 | CLIENT_VPN_CA_PEM, |
| 16 | CLIENT_VPN_CERTIFICATE_PEM, |
| 17 | CLIENT_VPN_PRIVATE_KEY_PEM, |
| 18 | } from "./client-vpn-certificate.ts"; |
| 19 | |
| 20 | // A lifecycle test performs several create, replacement, and delete operations. |
| 21 | export const clientVpnTestTimeout = |
| 22 | Number(process.env.AWS_CLIENT_VPN_TEST_TIMEOUT_MINUTES ?? 60) * 60_000; |
| 23 | if (!Number.isFinite(clientVpnTestTimeout) || clientVpnTestTimeout <= 0) { |
| 24 | throw new RangeError("AWS_CLIENT_VPN_TEST_TIMEOUT_MINUTES must be positive and finite"); |
| 25 | } |
| 26 | |
| 27 | class ClientVpnFixtureNotReady extends Data.TaggedError("ClientVpnFixtureNotReady")<{ |
| 28 | readonly resource: string; |
| 29 | }> {} |
| 30 | |
| 31 | // ACM.Certificate only requests public certificates; VPN mutual auth needs an imported CA chain. |
| 32 | export const importClientVpnCertificate = (stackName: string) => |
| 33 | withProviders( |
| 34 | Effect.acquireRelease( |
| 35 | Effect.gen(function* () { |
| 36 | const tags = yield* createInternalTags("Certificate"); |
| 37 | const pem = yield* Effect.sync(() => { |
| 38 | const encoder = new TextEncoder(); |
| 39 | return { |
| 40 | Certificate: encoder.encode(CLIENT_VPN_CERTIFICATE_PEM), |
| 41 | PrivateKey: Redacted.make(encoder.encode(CLIENT_VPN_PRIVATE_KEY_PEM)), |
| 42 | CertificateChain: encoder.encode(CLIENT_VPN_CA_PEM), |
| 43 | }; |
| 44 | }); |
| 45 | const imported = yield* acm.importCertificate({ |
| 46 | ...pem, |
| 47 | Tags: createTagsList(tags), |
| 48 | }); |
| 49 | if (!imported.CertificateArn) { |
| 50 | return yield* Effect.fail( |
| 51 | new ClientVpnFixtureNotReady({ |
| 52 | resource: "ACM certificate import", |
| 53 | }), |
| 54 | ); |
| 55 | } |
| 56 | return imported.CertificateArn; |
| 57 | }), |
| 58 | (certificateArn) => |
| 59 | acm.deleteCertificate({ CertificateArn: certificateArn }).pipe( |
| 60 | Effect.retry({ |
| 61 | while: (error) => error._tag === "ResourceInUseException", |
| 62 | schedule: Schedule.spaced("5 seconds").pipe( |
| 63 | Schedule.upTo({ duration: clientVpnTestTimeout }), |
| 64 | ), |
| 65 | }), |
| 66 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 67 | Effect.orDie, |
| 68 | ), |
| 69 | ), |
| 70 | { providers: AWS.providers() }, |
| 71 | stackName, |
| 72 | ); |
| 73 | |
| 74 | export const assertClientVpnCertificateDeleted = (certificateArn: string) => |
| 75 | withProviders( |
| 76 | acm.describeCertificate({ CertificateArn: certificateArn }).pipe( |
| 77 | Effect.flatMap(() => Effect.fail(new ClientVpnFixtureNotReady({ resource: certificateArn }))), |
| 78 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 79 | ), |
| 80 | { providers: AWS.providers() }, |
| 81 | "ClientVpnCertificateCleanup", |
| 82 | ); |
| 83 | |
| 84 | export const clientVpnEndpointProps = (certificateArn: string): ClientVpnEndpointProps => ({ |
| 85 | clientCidrBlock: "172.20.0.0/22", |
| 86 | serverCertificateArn: certificateArn, |
| 87 | authenticationOptions: [ |
| 88 | { |
| 89 | type: "certificate-authentication", |
| 90 | mutualAuthentication: { |
| 91 | clientRootCertificateChainArn: certificateArn, |
| 92 | }, |
| 93 | }, |
| 94 | ], |
| 95 | }); |
| 96 | |
| 97 | export const clientVpnAvailabilityZones = withProviders( |
| 98 | Effect.gen(function* () { |
| 99 | const zones = yield* ec2.describeAvailabilityZones({ |
| 100 | Filters: [ |
| 101 | { Name: "state", Values: ["available"] }, |
| 102 | { Name: "zone-type", Values: ["availability-zone"] }, |
| 103 | ], |
| 104 | }); |
| 105 | const [firstZone, secondZone] = (zones.AvailabilityZones ?? []) |
| 106 | .map((zone) => zone.ZoneName) |
| 107 | .filter((name): name is string => name !== undefined) |
| 108 | .sort(); |
| 109 | if (!firstZone || !secondZone) { |
| 110 | return yield* Effect.fail( |
| 111 | new ClientVpnFixtureNotReady({ |
| 112 | resource: "two AWS availability zones", |
| 113 | }), |
| 114 | ); |
| 115 | } |
| 116 | return { firstZone, secondZone }; |
| 117 | }), |
| 118 | { providers: AWS.providers() }, |
| 119 | "ClientVpnNetworkPrerequisites", |
| 120 | ); |
| 121 | |
| 122 | export const clientVpnNetwork = ({ |
| 123 | firstZone, |
| 124 | secondZone, |
| 125 | }: { |
| 126 | firstZone: string; |
| 127 | secondZone: string; |
| 128 | }) => |
| 129 | Effect.gen(function* () { |
| 130 | const vpc = yield* Vpc("Vpc", { |
| 131 | cidrBlock: "10.171.0.0/16", |
| 132 | enableDnsSupport: true, |
| 133 | enableDnsHostnames: true, |
| 134 | }); |
| 135 | const firstSubnet = yield* Subnet("FirstSubnet", { |
| 136 | vpcId: vpc.vpcId, |
| 137 | cidrBlock: "10.171.1.0/24", |
| 138 | availabilityZone: firstZone, |
| 139 | }); |
| 140 | const secondSubnet = yield* Subnet("SecondSubnet", { |
| 141 | vpcId: vpc.vpcId, |
| 142 | cidrBlock: "10.171.2.0/24", |
| 143 | availabilityZone: secondZone, |
| 144 | }); |
| 145 | return { vpc, firstSubnet, secondSubnet }; |
| 146 | }); |
| 147 | |
| 148 | export const waitForClientVpn = <A, E, R>( |
| 149 | observed: Effect.Effect<A, E, R>, |
| 150 | ready: (value: A) => boolean, |
| 151 | resource: string, |
| 152 | ) => |
| 153 | observed.pipe( |
| 154 | Effect.flatMap((value) => |
| 155 | ready(value) |
| 156 | ? Effect.succeed(value) |
| 157 | : Effect.fail(new ClientVpnFixtureNotReady({ resource })), |
| 158 | ), |
| 159 | Effect.retry({ |
| 160 | while: (error) => error instanceof ClientVpnFixtureNotReady, |
| 161 | schedule: Schedule.spaced("5 seconds").pipe( |
| 162 | Schedule.upTo({ duration: clientVpnTestTimeout }), |
| 163 | ), |
| 164 | }), |
| 165 | ); |
| 166 | |
| 167 | export const readClientVpnEndpoint = (clientVpnEndpointId: string) => |
| 168 | ec2 |
| 169 | .describeClientVpnEndpoints({ |
| 170 | ClientVpnEndpointIds: [clientVpnEndpointId], |
| 171 | }) |
| 172 | .pipe( |
| 173 | Effect.map((response) => |
| 174 | response.ClientVpnEndpoints?.find( |
| 175 | (endpoint) => endpoint.ClientVpnEndpointId === clientVpnEndpointId, |
| 176 | ), |
| 177 | ), |
| 178 | Effect.catchTag("InvalidClientVpnEndpointId.NotFound", () => Effect.succeed(undefined)), |
| 179 | ); |
| 180 | |
| 181 | export const assertClientVpnEndpointDeleted = (clientVpnEndpointId: string) => |
| 182 | waitForClientVpn( |
| 183 | readClientVpnEndpoint(clientVpnEndpointId), |
| 184 | (endpoint) => endpoint === undefined || endpoint.Status?.Code === "deleted", |
| 185 | clientVpnEndpointId, |
| 186 | ); |
| 187 | |
| 188 | export const readClientVpnAuthorizationRules = (clientVpnEndpointId: string) => |
| 189 | ec2.describeClientVpnAuthorizationRules |
| 190 | .items({ ClientVpnEndpointId: clientVpnEndpointId }) |
| 191 | .pipe(Stream.runCollect); |
| 192 | |
| 193 | export const readClientVpnTargetNetworks = (clientVpnEndpointId: string) => |
| 194 | ec2.describeClientVpnTargetNetworks |
| 195 | .items({ ClientVpnEndpointId: clientVpnEndpointId }) |
| 196 | .pipe(Stream.runCollect); |
| 197 | |
| 198 | export const readClientVpnRoutes = (clientVpnEndpointId: string) => |
| 199 | ec2.describeClientVpnRoutes |
| 200 | .items({ ClientVpnEndpointId: clientVpnEndpointId }) |
| 201 | .pipe(Stream.runCollect); |
| 202 | |
| 203 | export const assertClientVpnAuthorizationDeleted = ( |
| 204 | clientVpnEndpointId: string, |
| 205 | targetNetworkCidr: string, |
| 206 | ) => |
| 207 | waitForClientVpn( |
| 208 | readClientVpnAuthorizationRules(clientVpnEndpointId), |
| 209 | (rules) => |
| 210 | !rules.some( |
| 211 | (rule) => rule.DestinationCidr === targetNetworkCidr && rule.Status?.Code !== "revoked", |
| 212 | ), |
| 213 | `authorization ${targetNetworkCidr}`, |
| 214 | ); |
| 215 | |
| 216 | export const assertClientVpnAssociationDeleted = ( |
| 217 | clientVpnEndpointId: string, |
| 218 | associationId: string, |
| 219 | ) => |
| 220 | waitForClientVpn( |
| 221 | readClientVpnTargetNetworks(clientVpnEndpointId), |
| 222 | (networks) => |
| 223 | !networks.some( |
| 224 | (network) => |
| 225 | network.AssociationId === associationId && network.Status?.Code !== "disassociated", |
| 226 | ), |
| 227 | associationId, |
| 228 | ); |
| 229 | |
| 230 | export const assertClientVpnRouteDeleted = ( |
| 231 | clientVpnEndpointId: string, |
| 232 | destinationCidrBlock: string, |
| 233 | targetVpcSubnetId: string, |
| 234 | ) => |
| 235 | waitForClientVpn( |
| 236 | readClientVpnRoutes(clientVpnEndpointId), |
| 237 | (routes) => |
| 238 | !routes.some( |
| 239 | (route) => |
| 240 | route.DestinationCidr === destinationCidrBlock && |
| 241 | route.TargetSubnet === targetVpcSubnetId && |
| 242 | route.Status?.Code !== "deleted", |
| 243 | ), |
| 244 | `route ${destinationCidrBlock} via ${targetVpcSubnetId}`, |
| 245 | ); |
| 246 | |
| 247 | export const expectClientVpnOwnershipTags = ( |
| 248 | tags: ec2.Tag[] | undefined, |
| 249 | stack: { name: string; stage: string }, |
| 250 | id: string, |
| 251 | ) => { |
| 252 | expect(tags).toEqual( |
| 253 | expect.arrayContaining([ |
| 254 | { Key: "alchemy::stack", Value: stack.name }, |
| 255 | { Key: "alchemy::stage", Value: stack.stage }, |
| 256 | { Key: "alchemy::id", Value: id }, |
| 257 | ]), |
| 258 | ); |
| 259 | }; |