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
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Core from "@alchemy.run/test/Core"; |
| 5 | import * as sts from "@distilled.cloud/aws/sts"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import Inspector2TestFunctionLive, { Inspector2TestFunction } from "./handler"; |
| 12 | |
| 13 | const testOptions = { providers: AWS.providers() }; |
| 14 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 15 | const sharedStack = Core.scratchStack(testOptions, "Inspector2Bindings"); |
| 16 | |
| 17 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 18 | // over 60s on a fresh deploy. |
| 19 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 20 | |
| 21 | let baseUrl: string; |
| 22 | |
| 23 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 24 | readonly status: number; |
| 25 | readonly body: string; |
| 26 | }> {} |
| 27 | |
| 28 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 29 | // (cold re-init, IAM propagation on the freshly attached policy that the |
| 30 | // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine |
| 31 | // 4xx/assertion failure surfaces immediately. |
| 32 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 33 | HttpClient.execute(request).pipe( |
| 34 | Effect.flatMap((response) => |
| 35 | response.status >= 500 |
| 36 | ? response.text.pipe( |
| 37 | Effect.flatMap((body) => |
| 38 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 39 | ), |
| 40 | ) |
| 41 | : Effect.succeed(response), |
| 42 | ), |
| 43 | Effect.retry({ |
| 44 | while: (e) => e._tag === "TransientUpstream", |
| 45 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 46 | }), |
| 47 | ); |
| 48 | |
| 49 | const getJson = (path: string) => |
| 50 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 51 | |
| 52 | // Accounts where Inspector scanning is not enabled answer some data-plane |
| 53 | // reads with a typed AccessDeniedException — either outcome proves the |
| 54 | // binding + IAM wiring end-to-end. |
| 55 | const DISABLED_OK = ["AccessDeniedException", "ValidationException"]; |
| 56 | |
| 57 | describe.sequential( |
| 58 | "Inspector2 Bindings", |
| 59 | { |
| 60 | tags: [ |
| 61 | "provider:aws", |
| 62 | "provider:aws:batch", |
| 63 | "provider:aws:inspector2", |
| 64 | "provider:aws:lambda", |
| 65 | "live", |
| 66 | ], |
| 67 | }, |
| 68 | () => { |
| 69 | beforeAll( |
| 70 | Effect.gen(function* () { |
| 71 | yield* Effect.logInfo("Inspector2 test setup: destroying previous resources"); |
| 72 | yield* sharedStack.destroy(); |
| 73 | |
| 74 | yield* Effect.logInfo("Inspector2 test setup: deploying fixture"); |
| 75 | const attrs = yield* sharedStack.deploy( |
| 76 | Effect.gen(function* () { |
| 77 | return yield* Inspector2TestFunction; |
| 78 | }).pipe(Effect.provide(Inspector2TestFunctionLive)), |
| 79 | ); |
| 80 | |
| 81 | expect(attrs.functionUrl).toBeTruthy(); |
| 82 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 83 | |
| 84 | const readinessUrl = `${baseUrl}/bindings`; |
| 85 | yield* Effect.logInfo(`Inspector2 test setup: probing readiness at ${readinessUrl}`); |
| 86 | yield* HttpClient.get(readinessUrl).pipe( |
| 87 | Effect.flatMap((response) => |
| 88 | response.status === 200 |
| 89 | ? Effect.succeed(response) |
| 90 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 91 | ), |
| 92 | Effect.tapError((error) => |
| 93 | Effect.logWarning(`Inspector2 test setup: fixture not ready yet (${String(error)})`), |
| 94 | ), |
| 95 | Effect.retry({ schedule: readinessPolicy }), |
| 96 | ); |
| 97 | }), |
| 98 | { timeout: 240_000 }, |
| 99 | ); |
| 100 | |
| 101 | afterAll( |
| 102 | Effect.gen(function* () { |
| 103 | yield* sharedStack.destroy(); |
| 104 | }), |
| 105 | { timeout: 120_000 }, |
| 106 | ); |
| 107 | |
| 108 | describe("binding registration", () => { |
| 109 | test.provider("all 44 capabilities initialize in the runtime", (_stack) => |
| 110 | Effect.gen(function* () { |
| 111 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 112 | expect(response.bound).toHaveLength(44); |
| 113 | expect(response.bound).toContain("listFindings"); |
| 114 | expect(response.bound).toContain("searchVulnerabilities"); |
| 115 | expect(response.bound).toContain("createSbomExport"); |
| 116 | expect(response.bound).toContain("enableDelegatedAdminAccount"); |
| 117 | }), |
| 118 | ); |
| 119 | }); |
| 120 | |
| 121 | describe("ListFindings", () => { |
| 122 | test.provider("lists findings (or typed error when disabled)", (_stack) => |
| 123 | Effect.gen(function* () { |
| 124 | const response = (yield* getJson("/findings")) as { count?: number; errorTag?: string }; |
| 125 | if (response.errorTag) { |
| 126 | expect(DISABLED_OK).toContain(response.errorTag); |
| 127 | } else { |
| 128 | expect(response.count).toBeGreaterThanOrEqual(0); |
| 129 | } |
| 130 | }), |
| 131 | ); |
| 132 | }); |
| 133 | |
| 134 | describe("ListCoverage", () => { |
| 135 | test.provider("lists covered resources", (_stack) => |
| 136 | Effect.gen(function* () { |
| 137 | const response = (yield* getJson("/coverage")) as { count?: number; errorTag?: string }; |
| 138 | if (response.errorTag) { |
| 139 | expect(DISABLED_OK).toContain(response.errorTag); |
| 140 | } else { |
| 141 | expect(response.count).toBeGreaterThanOrEqual(0); |
| 142 | } |
| 143 | }), |
| 144 | ); |
| 145 | }); |
| 146 | |
| 147 | describe("SearchVulnerabilities", () => { |
| 148 | test.provider("looks up log4shell in the vulnerability intel", (_stack) => |
| 149 | Effect.gen(function* () { |
| 150 | const response = (yield* getJson("/vulnerability?id=CVE-2021-44228")) as { |
| 151 | ids?: string[]; |
| 152 | errorTag?: string; |
| 153 | }; |
| 154 | if (response.errorTag) { |
| 155 | expect(DISABLED_OK).toContain(response.errorTag); |
| 156 | } else { |
| 157 | expect(response.ids).toContain("CVE-2021-44228"); |
| 158 | } |
| 159 | }), |
| 160 | ); |
| 161 | }); |
| 162 | |
| 163 | describe("ListUsageTotals", () => { |
| 164 | test.provider("answers with usage totals", (_stack) => |
| 165 | Effect.gen(function* () { |
| 166 | const response = (yield* getJson("/usage")) as { totals?: number; errorTag?: string }; |
| 167 | if (response.errorTag) { |
| 168 | expect(DISABLED_OK).toContain(response.errorTag); |
| 169 | } else { |
| 170 | expect(response.totals).toBeGreaterThanOrEqual(0); |
| 171 | } |
| 172 | }), |
| 173 | ); |
| 174 | }); |
| 175 | |
| 176 | describe("ListAccountPermissions", () => { |
| 177 | test.provider("enumerates granted Inspector permissions", (_stack) => |
| 178 | Effect.gen(function* () { |
| 179 | const response = (yield* getJson("/account-permissions")) as { |
| 180 | permissions?: number; |
| 181 | errorTag?: string; |
| 182 | }; |
| 183 | if (response.errorTag) { |
| 184 | expect(DISABLED_OK).toContain(response.errorTag); |
| 185 | } else { |
| 186 | expect(response.permissions).toBeGreaterThanOrEqual(0); |
| 187 | } |
| 188 | }), |
| 189 | ); |
| 190 | }); |
| 191 | |
| 192 | describe("BatchGetFreeTrialInfo", () => { |
| 193 | test.provider("reports free-trial status for the account", (_stack) => |
| 194 | Effect.gen(function* () { |
| 195 | const { Account } = yield* sts.getCallerIdentity({}); |
| 196 | const response = (yield* getJson(`/free-trial?account=${Account}`)) as { |
| 197 | accounts?: number; |
| 198 | failed?: number; |
| 199 | errorTag?: string; |
| 200 | }; |
| 201 | if (response.errorTag) { |
| 202 | expect(DISABLED_OK).toContain(response.errorTag); |
| 203 | } else { |
| 204 | expect((response.accounts ?? 0) + (response.failed ?? 0)).toBe(1); |
| 205 | } |
| 206 | }), |
| 207 | ); |
| 208 | }); |
| 209 | |
| 210 | describe("GetConfiguration", () => { |
| 211 | test.provider("reads the account scan configuration", (_stack) => |
| 212 | Effect.gen(function* () { |
| 213 | const response = (yield* getJson("/config")) as { |
| 214 | hasEcrConfiguration?: boolean; |
| 215 | errorTag?: string; |
| 216 | }; |
| 217 | if (response.errorTag) { |
| 218 | expect(DISABLED_OK).toContain(response.errorTag); |
| 219 | } else { |
| 220 | expect(typeof response.hasEcrConfiguration).toBe("boolean"); |
| 221 | } |
| 222 | }), |
| 223 | ); |
| 224 | }); |
| 225 | |
| 226 | describe("GetEc2DeepInspectionConfiguration", () => { |
| 227 | test.provider("reads deep-inspection status", (_stack) => |
| 228 | Effect.gen(function* () { |
| 229 | const response = (yield* getJson("/deep-inspection")) as { |
| 230 | status?: string; |
| 231 | errorTag?: string; |
| 232 | }; |
| 233 | if (response.errorTag) { |
| 234 | expect(DISABLED_OK).toContain(response.errorTag); |
| 235 | } else { |
| 236 | expect(typeof response.status).toBe("string"); |
| 237 | } |
| 238 | }), |
| 239 | ); |
| 240 | }); |
| 241 | |
| 242 | describe("GetEncryptionKey", () => { |
| 243 | test.provider("reads the ECR package-scan key", (_stack) => |
| 244 | Effect.gen(function* () { |
| 245 | const response = (yield* getJson("/encryption-key")) as { |
| 246 | kmsKeyId?: string; |
| 247 | errorTag?: string; |
| 248 | }; |
| 249 | if (response.errorTag) { |
| 250 | // ResourceNotFoundException = no customer-managed key configured. |
| 251 | expect([...DISABLED_OK, "ResourceNotFoundException"]).toContain(response.errorTag); |
| 252 | } else { |
| 253 | expect(response.kmsKeyId).toBeTruthy(); |
| 254 | } |
| 255 | }), |
| 256 | ); |
| 257 | }); |
| 258 | |
| 259 | describe("ListCisScans", () => { |
| 260 | test.provider("enumerates CIS scans", (_stack) => |
| 261 | Effect.gen(function* () { |
| 262 | const response = (yield* getJson("/cis-scans")) as { scans?: number; errorTag?: string }; |
| 263 | if (response.errorTag) { |
| 264 | expect(DISABLED_OK).toContain(response.errorTag); |
| 265 | } else { |
| 266 | expect(response.scans).toBeGreaterThanOrEqual(0); |
| 267 | } |
| 268 | }), |
| 269 | ); |
| 270 | }); |
| 271 | |
| 272 | describe("ListMembers", () => { |
| 273 | test.provider("enumerates organization members", (_stack) => |
| 274 | Effect.gen(function* () { |
| 275 | const response = (yield* getJson("/members")) as { members?: number; errorTag?: string }; |
| 276 | if (response.errorTag) { |
| 277 | expect(DISABLED_OK).toContain(response.errorTag); |
| 278 | } else { |
| 279 | expect(response.members).toBeGreaterThanOrEqual(0); |
| 280 | } |
| 281 | }), |
| 282 | ); |
| 283 | }); |
| 284 | |
| 285 | describe("DescribeOrganizationConfiguration", () => { |
| 286 | test.provider( |
| 287 | "answers (or rejects with a typed error for a non-delegated account)", |
| 288 | (_stack) => |
| 289 | Effect.gen(function* () { |
| 290 | const response = (yield* getJson("/org-config")) as { |
| 291 | maxAccountLimitReached?: boolean; |
| 292 | errorTag?: string; |
| 293 | }; |
| 294 | if (response.errorTag) { |
| 295 | expect(DISABLED_OK).toContain(response.errorTag); |
| 296 | } else { |
| 297 | expect(typeof response.maxAccountLimitReached).toBe("boolean"); |
| 298 | } |
| 299 | }), |
| 300 | ); |
| 301 | }); |
| 302 | |
| 303 | describe("GetDelegatedAdminAccount", () => { |
| 304 | test.provider("answers (or rejects with a typed error outside an organization)", (_stack) => |
| 305 | Effect.gen(function* () { |
| 306 | const response = (yield* getJson("/delegated-admin")) as { |
| 307 | accountId?: string; |
| 308 | errorTag?: string; |
| 309 | }; |
| 310 | if (response.errorTag) { |
| 311 | expect([...DISABLED_OK, "ResourceNotFoundException"]).toContain(response.errorTag); |
| 312 | } else { |
| 313 | expect(response.accountId === undefined || true).toBe(true); |
| 314 | } |
| 315 | }), |
| 316 | ); |
| 317 | }); |
| 318 | |
| 319 | describe("GetFindingsReportStatus", () => { |
| 320 | test.provider("a nonexistent report id comes back as a typed error", (_stack) => |
| 321 | Effect.gen(function* () { |
| 322 | const response = (yield* getJson("/report-status")) as { |
| 323 | status?: string; |
| 324 | errorTag?: string; |
| 325 | }; |
| 326 | if (response.errorTag) { |
| 327 | expect([...DISABLED_OK, "ResourceNotFoundException"]).toContain(response.errorTag); |
| 328 | } else { |
| 329 | expect(typeof response.status).toBe("string"); |
| 330 | } |
| 331 | }), |
| 332 | ); |
| 333 | }); |
| 334 | }, |
| 335 | ); |