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

main
ts 187 lines 6.6 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as websecurityscanner from "@distilled.cloud/gcp/websecurityscanner_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13 const missingNameOf = (project: string) => `projects/${project}/scanConfigs/1`;
14
15 const waitUntilGone = (name: string) =>
16 websecurityscanner.getProjectsScanConfigs({ name }).pipe(
17 Effect.as("found" as const),
18 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
19 Effect.repeat({
20 schedule: Schedule.spaced("1 second"),
21 until: (status) => status === "gone",
22 times: 10,
23 }),
24 );
25
26 test.provider(
27 "getProjectsScanConfigs on a missing config fails with a typed tag",
28 (stack) =>
29 Effect.gen(function* () {
30 const { project } = yield* GcpEnvironment.current;
31
32 yield* stack.destroy();
33
34 const error = yield* Effect.flip(
35 websecurityscanner.getProjectsScanConfigs({
36 name: missingNameOf(project),
37 }),
38 );
39 expect(error._tag).toEqual("NotFound");
40
41 const page = yield* websecurityscanner.listProjectsScanConfigs({
42 parent: `projects/${project}`,
43 pageSize: 10,
44 });
45 expect((page.scanConfigs ?? []).map((config) => config.name)).not.toContain(
46 missingNameOf(project),
47 );
48
49 yield* stack.destroy();
50 }).pipe(logLevel),
51 {
52 tags: ["provider:gcp", "provider:gcp:websecurityscanner", "live"],
53 timeout: 90_000,
54 },
55 );
56
57 test.provider(
58 "createProjectsScanConfigs with an unreserved IP fails with StartingUrlIpNotReserved",
59 (stack) =>
60 Effect.gen(function* () {
61 const { project } = yield* GcpEnvironment.current;
62 yield* stack.destroy();
63
64 // 203.0.113.0/24 is TEST-NET-3; it can never be reserved in the project.
65 const error = yield* Effect.flip(
66 websecurityscanner.createProjectsScanConfigs({
67 parent: `projects/${project}`,
68 body: {
69 displayName: "unreserved",
70 startingUrls: ["http://203.0.113.9/"],
71 targetPlatforms: ["COMPUTE"],
72 exportToSecurityCommandCenter: "DISABLED",
73 },
74 }),
75 );
76 expect(error._tag).toEqual("StartingUrlIpNotReserved");
77
78 yield* stack.destroy();
79 }).pipe(logLevel),
80 {
81 tags: ["provider:gcp", "provider:gcp:websecurityscanner", "live"],
82 timeout: 90_000,
83 },
84 );
85
86 test.provider(
87 "create, update, and delete a scan config",
88 (stack) =>
89 Effect.gen(function* () {
90 yield* stack.destroy();
91
92 const reserved = yield* stack.deploy(
93 Effect.gen(function* () {
94 return yield* GCP.Compute.Address("ScannerIp", {
95 region: "us-central1",
96 });
97 }),
98 );
99 expect(reserved.address).toEqual(expect.any(String));
100 const startingUrl = `http://${reserved.address}`;
101
102 const created = yield* stack.deploy(
103 Effect.gen(function* () {
104 const ip = yield* GCP.Compute.Address("ScannerIp", {
105 addressName: reserved.addressName,
106 region: "us-central1",
107 });
108 const scan = yield* GCP.WebSecurityScanner.ScanConfig("Scan", {
109 displayName: "alchemy scan",
110 startingUrls: [startingUrl],
111 targetPlatforms: ["COMPUTE"],
112 maxQps: 5,
113 userAgent: "CHROME_LINUX",
114 exportToSecurityCommandCenter: "DISABLED",
115 riskLevel: "LOW",
116 });
117 return { ip, scan };
118 }),
119 );
120
121 expect(created.scan.name).toContain("/scanConfigs/");
122 expect(created.scan.scanConfigId).toEqual(expect.any(String));
123 expect(created.scan.displayName).toEqual("alchemy scan");
124 expect(created.scan.startingUrls).toEqual([`http://${created.ip.address}`]);
125 expect(created.scan.maxQps).toEqual(5);
126 expect(created.scan.userAgent).toEqual("CHROME_LINUX");
127 expect(created.scan.targetPlatforms).toEqual(["COMPUTE"]);
128 expect(created.scan.exportToSecurityCommandCenter).toEqual("DISABLED");
129 expect(created.scan.riskLevel).toEqual("LOW");
130
131 const fetched = yield* websecurityscanner.getProjectsScanConfigs({
132 name: created.scan.name,
133 });
134 expect(fetched.name).toEqual(created.scan.name);
135 expect(fetched.displayName).toEqual("alchemy scan");
136 expect(fetched.startingUrls).toEqual([`http://${created.ip.address}`]);
137 expect(fetched.maxQps).toEqual(5);
138 expect(fetched.targetPlatforms).toEqual(["COMPUTE"]);
139
140 const updated = yield* stack.deploy(
141 Effect.gen(function* () {
142 const ip = yield* GCP.Compute.Address("ScannerIp", {
143 addressName: created.ip.addressName,
144 region: "us-central1",
145 });
146 const scan = yield* GCP.WebSecurityScanner.ScanConfig("Scan", {
147 scanConfigId: created.scan.scanConfigId,
148 displayName: "alchemy scan v2",
149 startingUrls: [startingUrl],
150 targetPlatforms: ["COMPUTE"],
151 maxQps: 10,
152 userAgent: "CHROME_ANDROID",
153 blacklistPatterns: [`${startingUrl}/logout`],
154 ignoreHttpStatusErrors: true,
155 exportToSecurityCommandCenter: "DISABLED",
156 riskLevel: "LOW",
157 });
158 return { ip, scan };
159 }),
160 );
161
162 expect(updated.scan.name).toEqual(created.scan.name);
163 expect(updated.scan.scanConfigId).toEqual(created.scan.scanConfigId);
164 expect(updated.scan.displayName).toEqual("alchemy scan v2");
165 expect(updated.scan.maxQps).toEqual(10);
166 expect(updated.scan.userAgent).toEqual("CHROME_ANDROID");
167 expect(updated.scan.blacklistPatterns).toEqual([`http://${updated.ip.address}/logout`]);
168 expect(updated.scan.ignoreHttpStatusErrors).toEqual(true);
169
170 const fetchedUpdate = yield* websecurityscanner.getProjectsScanConfigs({
171 name: created.scan.name,
172 });
173 expect(fetchedUpdate.displayName).toEqual("alchemy scan v2");
174 expect(fetchedUpdate.maxQps).toEqual(10);
175 expect(fetchedUpdate.userAgent).toEqual("CHROME_ANDROID");
176 expect(fetchedUpdate.ignoreHttpStatusErrors).toEqual(true);
177
178 yield* stack.destroy();
179
180 const gone = yield* waitUntilGone(created.scan.name);
181 expect(gone).toEqual("gone");
182 }).pipe(logLevel),
183 {
184 tags: ["provider:gcp", "provider:gcp:websecurityscanner", "live"],
185 timeout: 90_000,
186 },
187 );