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 { Analyzer, ArchiveRule } from "@alchemy.run/aws/AccessAnalyzer"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as aa from "@distilled.cloud/aws/accessanalyzer"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import type * as Duration from "effect/Duration"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import { makeAccessAnalyzerTestLease } from "./TestLease.ts"; |
| 11 | |
| 12 | const { test, beforeAll, afterAll } = Test.make({ |
| 13 | providers: AWS.providers(), |
| 14 | }); |
| 15 | const testLease = makeAccessAnalyzerTestLease(); |
| 16 | |
| 17 | beforeAll(testLease.acquire, { timeout: 240_000 }); |
| 18 | afterAll(testLease.release); |
| 19 | |
| 20 | const findAnalyzer = (name: string) => |
| 21 | aa.getAnalyzer({ analyzerName: name }).pipe( |
| 22 | Effect.map((r) => r.analyzer), |
| 23 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)), |
| 24 | ); |
| 25 | |
| 26 | const findArchiveRule = (analyzerName: string, ruleName: string) => |
| 27 | aa.getArchiveRule({ analyzerName, ruleName }).pipe( |
| 28 | Effect.map((r) => r.archiveRule), |
| 29 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)), |
| 30 | ); |
| 31 | |
| 32 | class AnalyzerStillExists extends Data.TaggedError("AnalyzerStillExists")<{ |
| 33 | readonly name: string; |
| 34 | }> {} |
| 35 | |
| 36 | const assertAnalyzerDeleted = (name: string) => |
| 37 | findAnalyzer(name).pipe( |
| 38 | Effect.flatMap((analyzer) => |
| 39 | analyzer === undefined ? Effect.void : Effect.fail(new AnalyzerStillExists({ name })), |
| 40 | ), |
| 41 | Effect.retry({ |
| 42 | while: (e) => e._tag === "AnalyzerStillExists", |
| 43 | schedule: Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(15)]), |
| 44 | }), |
| 45 | ); |
| 46 | |
| 47 | test.provider( |
| 48 | "analyzer lifecycle: create ACCOUNT analyzer, update tags, manage archive rule, destroy", |
| 49 | (stack) => |
| 50 | Effect.gen(function* () { |
| 51 | yield* stack.destroy(); |
| 52 | |
| 53 | const analyzerName = "alchemy-test-external-access"; |
| 54 | |
| 55 | const makeStack = (tagValue: string, ruleFilterValue: string) => |
| 56 | Effect.gen(function* () { |
| 57 | const analyzer = yield* Analyzer("AccountAnalyzer", { |
| 58 | analyzerName, |
| 59 | type: "ACCOUNT", |
| 60 | tags: { Environment: tagValue }, |
| 61 | }); |
| 62 | const rule = yield* ArchiveRule("TrustedAccount", { |
| 63 | analyzerName: analyzer.analyzerName, |
| 64 | ruleName: "trusted-account", |
| 65 | filter: { |
| 66 | "principal.AWS": { eq: [ruleFilterValue] }, |
| 67 | }, |
| 68 | }); |
| 69 | return { analyzer, rule }; |
| 70 | }); |
| 71 | |
| 72 | // create |
| 73 | const { analyzer } = yield* stack.deploy(makeStack("test", "111111111111")); |
| 74 | expect(analyzer.analyzerName).toBe(analyzerName); |
| 75 | expect(analyzer.analyzerArn).toContain(":analyzer/"); |
| 76 | expect(analyzer.type).toBe("ACCOUNT"); |
| 77 | |
| 78 | // out-of-band verify |
| 79 | const created = yield* findAnalyzer(analyzerName); |
| 80 | expect(created?.type).toBe("ACCOUNT"); |
| 81 | expect(created?.tags?.Environment).toBe("test"); |
| 82 | expect(created?.tags?.["alchemy::id"]).toBe("AccountAnalyzer"); |
| 83 | |
| 84 | const createdRule = yield* findArchiveRule(analyzerName, "trusted-account"); |
| 85 | expect(createdRule?.filter?.["principal.AWS"]?.eq).toEqual(["111111111111"]); |
| 86 | |
| 87 | // update tags + archive rule filter in place (no replacement) |
| 88 | const updated = yield* stack.deploy(makeStack("prod", "222222222222")); |
| 89 | expect(updated.analyzer.analyzerArn).toBe(analyzer.analyzerArn); |
| 90 | |
| 91 | const afterTagUpdate = yield* findAnalyzer(analyzerName); |
| 92 | expect(afterTagUpdate?.tags?.Environment).toBe("prod"); |
| 93 | |
| 94 | const afterRuleUpdate = yield* findArchiveRule(analyzerName, "trusted-account"); |
| 95 | expect(afterRuleUpdate?.filter?.["principal.AWS"]?.eq).toEqual(["222222222222"]); |
| 96 | |
| 97 | // destroy |
| 98 | yield* stack.destroy(); |
| 99 | yield* assertAnalyzerDeleted(analyzerName); |
| 100 | }), |
| 101 | { |
| 102 | tags: ["provider:aws", "provider:aws:accessanalyzer", "live"], |
| 103 | timeout: 180_000, |
| 104 | }, |
| 105 | ); |
| 106 | |
| 107 | const observedUnusedAccessAge = (analyzer: aa.AnalyzerSummary | undefined): number | undefined => |
| 108 | analyzer?.configuration?.unusedAccess?.unusedAccessAge; |
| 109 | |
| 110 | test.provider( |
| 111 | "unused-access analyzer: create with a tracking period, replace on change, destroy", |
| 112 | (stack) => |
| 113 | Effect.gen(function* () { |
| 114 | yield* stack.destroy(); |
| 115 | |
| 116 | const analyzerName = "alchemy-test-unused-access-age"; |
| 117 | |
| 118 | const makeStack = (unusedAccessAge: Duration.Input) => |
| 119 | Effect.gen(function* () { |
| 120 | return { |
| 121 | analyzer: yield* Analyzer("UnusedAccessAnalyzer", { |
| 122 | analyzerName, |
| 123 | type: "ACCOUNT_UNUSED_ACCESS", |
| 124 | unusedAccessAge, |
| 125 | }), |
| 126 | }; |
| 127 | }); |
| 128 | |
| 129 | // create with a 180-day tracking period |
| 130 | const { analyzer } = yield* stack.deploy(makeStack("180 days")); |
| 131 | expect(analyzer.type).toBe("ACCOUNT_UNUSED_ACCESS"); |
| 132 | |
| 133 | const created = yield* findAnalyzer(analyzerName); |
| 134 | expect(observedUnusedAccessAge(created)).toBe(180); |
| 135 | |
| 136 | // the tracking period is create-only — changing it replaces the |
| 137 | // analyzer (delete-first, keeping the pinned name). The new observed |
| 138 | // period is the proof: the API rejects in-place updates. |
| 139 | const updated = yield* stack.deploy(makeStack("365 days")); |
| 140 | expect(updated.analyzer.analyzerName).toBe(analyzerName); |
| 141 | |
| 142 | const afterReplace = yield* findAnalyzer(analyzerName); |
| 143 | expect(observedUnusedAccessAge(afterReplace)).toBe(365); |
| 144 | |
| 145 | // destroy |
| 146 | yield* stack.destroy(); |
| 147 | yield* assertAnalyzerDeleted(analyzerName); |
| 148 | }), |
| 149 | { |
| 150 | tags: ["provider:aws", "provider:aws:accessanalyzer", "live"], |
| 151 | timeout: 180_000, |
| 152 | }, |
| 153 | ); |