@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 229 lines 9.98 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Assessment, Control, Framework } from "@alchemy.run/aws/AuditManager";
3 import { describe, expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as auditmanager from "@distilled.cloud/aws/auditmanager";
6 import * as Output from "alchemy/Output";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 // Audit Manager must be registered per account (RegisterAccount) before any
13 // control/framework/assessment operation succeeds — registration is a
14 // account-level side effect, so the full lifecycle is gated behind
15 // AWS_TEST_AUDITMANAGER=1. The ungated probes below prove the distilled
16 // wiring and the typed error union at near-zero cost in every CI pass.
17 describe(
18 "AWS.AuditManager",
19 { tags: ["provider:aws", "provider:aws:auditmanager", "live"] },
20 () => {
21 test.provider(
22 "getAccountStatus returns a typed account status",
23 (_stack) =>
24 Effect.gen(function* () {
25 const response = yield* auditmanager.getAccountStatus({});
26 expect(["ACTIVE", "INACTIVE", "PENDING_ACTIVATION"]).toContain(response.status);
27 }),
28 { timeout: 60_000 },
29 );
30
31 test.provider(
32 "getControl on a nonexistent id yields a typed error tag",
33 (_stack) =>
34 Effect.gen(function* () {
35 const error = yield* auditmanager
36 .getControl({ controlId: "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee" })
37 .pipe(Effect.flip);
38 // Registered accounts answer ResourceNotFoundException; unregistered
39 // accounts reject every control op with AccessDeniedException. Both
40 // are typed tags in the distilled union — the provider's read path
41 // depends on the former.
42 expect(["ResourceNotFoundException", "AccessDeniedException"]).toContain(error._tag);
43 }),
44 { timeout: 60_000 },
45 );
46
47 test.provider(
48 "registerAccount is typed: idempotent on registered accounts, AuditManagerMaintenanceMode otherwise",
49 (_stack) =>
50 Effect.gen(function* () {
51 const { status } = yield* auditmanager.getAccountStatus({});
52 if (status === "ACTIVE") {
53 // Already registered — re-registering is an idempotent no-op.
54 const response = yield* auditmanager.registerAccount({});
55 expect(response.status).toBe("ACTIVE");
56 } else {
57 // Audit Manager entered maintenance mode on 2026-04-30: new
58 // accounts can no longer be registered. The rejection surfaces as
59 // the typed tag patched into distilled (patches/auditmanager.json).
60 const error = yield* Effect.flip(auditmanager.registerAccount({}));
61 expect(error._tag).toBe("AuditManagerMaintenanceMode");
62 }
63 }),
64 { timeout: 60_000 },
65 );
66
67 test.provider.skipIf(!process.env.AWS_TEST_AUDITMANAGER)(
68 "control + framework + assessment lifecycle: create, update, destroy",
69 (stack) =>
70 Effect.gen(function* () {
71 yield* stack.destroy();
72
73 // Ensure the account is registered and wait for ACTIVE. On accounts
74 // that never enabled Audit Manager this fails with the typed
75 // AuditManagerMaintenanceMode tag — the service is in maintenance
76 // mode and new registrations are permanently rejected.
77 const { status } = yield* auditmanager.getAccountStatus({});
78 if (status !== "ACTIVE") {
79 yield* auditmanager.registerAccount({});
80 yield* Effect.gen(function* () {
81 const current = yield* auditmanager.getAccountStatus({});
82 if (current.status !== "ACTIVE") {
83 return yield* Effect.fail({ _tag: "NotActive" as const, status: current.status });
84 }
85 }).pipe(
86 Effect.retry({
87 while: (e) => e._tag === "NotActive",
88 schedule: Schedule.max([Schedule.spaced("5 seconds"), Schedule.recurs(12)]),
89 }),
90 );
91 }
92
93 const deploy = (props: {
94 controlDescription?: string;
95 frameworkDescription?: string;
96 assessmentDescription?: string;
97 }) =>
98 stack.deploy(
99 Effect.gen(function* () {
100 const control = yield* Control("EvidenceControl", {
101 description: props.controlDescription,
102 controlMappingSources: [
103 {
104 sourceName: "manual-evidence",
105 sourceDescription: "Manually uploaded evidence",
106 sourceSetUpOption: "Procedural_Controls_Mapping",
107 sourceType: "MANUAL",
108 },
109 ],
110 tags: { fixture: "auditmanager" },
111 });
112
113 const framework = yield* Framework("ComplianceFramework", {
114 description: props.frameworkDescription,
115 complianceType: "Internal",
116 controlSets: [{ name: "Operations", controls: [{ id: control.controlId }] }],
117 tags: { fixture: "auditmanager" },
118 });
119
120 const reports = yield* AWS.S3.Bucket("AuditReports", { forceDestroy: true });
121
122 const owner = yield* AWS.IAM.Role("AuditOwner", {
123 assumeRolePolicyDocument: {
124 Version: "2012-10-17",
125 Statement: [
126 {
127 Effect: "Allow",
128 Principal: { Service: ["auditmanager.amazonaws.com"] },
129 Action: ["sts:AssumeRole"],
130 },
131 ],
132 },
133 });
134
135 const assessment = yield* Assessment("Quarterly", {
136 description: props.assessmentDescription,
137 frameworkId: framework.frameworkId,
138 assessmentReportsDestination: {
139 destination: Output.interpolate`s3://${reports.bucketName}`,
140 },
141 roles: [{ roleType: "PROCESS_OWNER", roleArn: owner.roleArn }],
142 tags: { fixture: "auditmanager" },
143 });
144
145 return { control, framework, assessment };
146 }),
147 );
148
149 // Create.
150 const { control, framework, assessment } = yield* deploy({
151 controlDescription: "initial control",
152 frameworkDescription: "initial framework",
153 assessmentDescription: "initial assessment",
154 });
155 expect(control.controlId).toBeDefined();
156 expect(control.arn).toContain(":control/");
157 expect(control.type).toBe("Custom");
158 expect(framework.frameworkId).toBeDefined();
159 expect(framework.type).toBe("Custom");
160 expect(assessment.assessmentId).toBeDefined();
161 expect(assessment.status).toBe("ACTIVE");
162 expect(assessment.frameworkId).toBe(framework.frameworkId);
163
164 // Out-of-band verification via distilled.
165 const observedControl = yield* auditmanager.getControl({ controlId: control.controlId });
166 expect(observedControl.control?.name).toBe(control.name);
167 const observedFramework = yield* auditmanager.getAssessmentFramework({
168 frameworkId: framework.frameworkId,
169 });
170 expect(observedFramework.framework?.controlSets?.[0]?.controls?.[0]?.id).toBe(
171 control.controlId,
172 );
173 const observedAssessment = yield* auditmanager.getAssessment({
174 assessmentId: assessment.assessmentId,
175 });
176 expect(observedAssessment.assessment?.framework?.id).toBe(framework.frameworkId);
177
178 // Update in place — descriptions flow through the update APIs and
179 // ids are stable.
180 const updated = yield* deploy({
181 controlDescription: "updated control",
182 frameworkDescription: "updated framework",
183 assessmentDescription: "updated assessment",
184 });
185 expect(updated.control.controlId).toBe(control.controlId);
186 expect(updated.framework.frameworkId).toBe(framework.frameworkId);
187 expect(updated.assessment.assessmentId).toBe(assessment.assessmentId);
188 const reobservedFramework = yield* auditmanager.getAssessmentFramework({
189 frameworkId: framework.frameworkId,
190 });
191 expect(reobservedFramework.framework?.description).toBe("updated framework");
192
193 yield* stack.destroy();
194
195 // Typed wait-until-gone for each resource.
196 const assertGone = Effect.gen(function* () {
197 const controlGone = yield* auditmanager
198 .getControl({ controlId: control.controlId })
199 .pipe(
200 Effect.map(() => false),
201 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
202 );
203 const frameworkGone = yield* auditmanager
204 .getAssessmentFramework({ frameworkId: framework.frameworkId })
205 .pipe(
206 Effect.map(() => false),
207 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
208 );
209 const assessmentGone = yield* auditmanager
210 .getAssessment({ assessmentId: assessment.assessmentId })
211 .pipe(
212 Effect.map(() => false),
213 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
214 );
215 if (!controlGone || !frameworkGone || !assessmentGone) {
216 return yield* Effect.fail({ _tag: "StillExists" as const });
217 }
218 }).pipe(
219 Effect.retry({
220 while: (e: { _tag: string }) => e._tag === "StillExists",
221 schedule: Schedule.max([Schedule.spaced("5 seconds"), Schedule.recurs(10)]),
222 }),
223 );
224 yield* assertGone;
225 }),
226 { tags: ["provider:aws:iam", "provider:aws:s3"], timeout: 600_000 },
227 );
228 },
229 );