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

main
ts 102 lines 3.73 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { LogGroup } from "@alchemy.run/aws/Logs";
3 import { LoggingConfiguration, WebACL } from "@alchemy.run/aws/WAFv2";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as wafv2 from "@distilled.cloud/aws/wafv2";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 class LoggingConfigStillExists extends Data.TaggedError("LoggingConfigStillExists")<{
14 readonly resourceArn: string;
15 }> {}
16
17 const assertLoggingDeleted = (resourceArn: string) =>
18 wafv2.getLoggingConfiguration({ ResourceArn: resourceArn }).pipe(
19 Effect.flatMap(() => Effect.fail(new LoggingConfigStillExists({ resourceArn }))),
20 Effect.catchTag("WAFNonexistentItemException", () => Effect.void),
21 Effect.retry({
22 while: (e) => e._tag === "LoggingConfigStillExists",
23 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
24 }),
25 );
26
27 test.provider(
28 "enable logging to CloudWatch Logs, add a filter, delete",
29 (stack) =>
30 Effect.gen(function* () {
31 // reconcile away any prior partial/crashed deployment
32 yield* stack.destroy();
33
34 // Destinations must be named aws-waf-logs-*.
35 const deployed = yield* stack.deploy(
36 Effect.gen(function* () {
37 const acl = yield* WebACL("LoggedAcl", {});
38 const logGroup = yield* LogGroup("WafLogGroup", {
39 logGroupName: "aws-waf-logs-alchemy-wafv2-test",
40 });
41 const logging = yield* LoggingConfiguration("Logging", {
42 resourceArn: acl.webAclArn,
43 logDestinationConfigs: [logGroup.logGroupArn],
44 });
45 return { acl, logging };
46 }),
47 );
48
49 expect(deployed.logging.scope).toBe("REGIONAL");
50 expect(deployed.logging.logDestinationConfigs).toHaveLength(1);
51
52 // out-of-band verification via distilled
53 const created = yield* wafv2.getLoggingConfiguration({
54 ResourceArn: deployed.acl.webAclArn,
55 });
56 expect(created.LoggingConfiguration?.LogDestinationConfigs?.[0]).toContain(
57 "aws-waf-logs-alchemy-wafv2-test",
58 );
59 expect(created.LoggingConfiguration?.LoggingFilter).toBeUndefined();
60
61 // update in place: add a redacted field + logging filter
62 yield* stack.deploy(
63 Effect.gen(function* () {
64 const acl = yield* WebACL("LoggedAcl", {});
65 const logGroup = yield* LogGroup("WafLogGroup", {
66 logGroupName: "aws-waf-logs-alchemy-wafv2-test",
67 });
68 const logging = yield* LoggingConfiguration("Logging", {
69 resourceArn: acl.webAclArn,
70 logDestinationConfigs: [logGroup.logGroupArn],
71 redactedFields: [{ SingleHeader: { Name: "authorization" } }],
72 loggingFilter: {
73 DefaultBehavior: "KEEP",
74 Filters: [
75 {
76 Behavior: "DROP",
77 Requirement: "MEETS_ANY",
78 Conditions: [{ ActionCondition: { Action: "ALLOW" } }],
79 },
80 ],
81 },
82 });
83 return { acl, logging };
84 }),
85 );
86
87 const updated = yield* wafv2.getLoggingConfiguration({
88 ResourceArn: deployed.acl.webAclArn,
89 });
90 expect(updated.LoggingConfiguration?.LoggingFilter?.DefaultBehavior).toBe("KEEP");
91 expect(updated.LoggingConfiguration?.RedactedFields?.[0]?.SingleHeader?.Name).toBe(
92 "authorization",
93 );
94
95 yield* stack.destroy();
96 yield* assertLoggingDeleted(deployed.acl.webAclArn);
97 }),
98 {
99 tags: ["provider:aws", "provider:aws:logs", "provider:aws:wafv2", "live"],
100 timeout: 300_000,
101 },
102 );