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

main
ts 177 lines 6.26 KB
Raw
1 import type * as NFW from "@distilled.cloud/aws/network-firewall";
2 import * as nfw from "@distilled.cloud/aws/network-firewall";
3 import { deepEqual, isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Effect from "effect/Effect";
7 import type { Providers } from "../Providers.ts";
8
9 export interface LoggingConfigurationProps {
10 /**
11 * ARN of the {@link Firewall} to attach the logging configuration to.
12 * Changing the firewall replaces the configuration.
13 */
14 firewallArn: string;
15 /**
16 * The log destinations, one per log type (`ALERT`, `FLOW`, `TLS`). Uses
17 * raw Network Firewall API structures. The provider converges the live
18 * configuration one change at a time, as the API requires.
19 */
20 logDestinationConfigs: NFW.LogDestinationConfig[];
21 }
22
23 export interface LoggingConfiguration extends Resource<
24 "AWS.NetworkFirewall.LoggingConfiguration",
25 LoggingConfigurationProps,
26 {
27 /** ARN of the firewall the logging configuration applies to. */
28 firewallArn: string;
29 },
30 never,
31 Providers
32 > {}
33
34 /**
35 * The logging configuration of an AWS Network Firewall {@link Firewall} —
36 * routes the firewall's `ALERT`, `FLOW`, and `TLS` logs to S3, CloudWatch
37 * Logs, or Kinesis Data Firehose destinations.
38 *
39 * A firewall has exactly one logging configuration; deleting this resource
40 * resets it to no logging.
41 * ### Configuring Logging
42 * **Example:** Flow logs to CloudWatch Logs
43 * ```typescript
44 * import * as Logs from "@alchemy.run/aws/Logs";
45 * import * as NetworkFirewall from "@alchemy.run/aws/NetworkFirewall";
46 *
47 * const logGroup = yield* Logs.LogGroup("FirewallLogs");
48 *
49 * yield* NetworkFirewall.LoggingConfiguration("Logging", {
50 * firewallArn: firewall.firewallArn,
51 * logDestinationConfigs: [
52 * {
53 * LogType: "FLOW",
54 * LogDestinationType: "CloudWatchLogs",
55 * LogDestination: { logGroup: logGroup.logGroupName },
56 * },
57 * ],
58 * });
59 * ```
60 *
61 * **Example:** Alert logs to S3
62 * ```typescript
63 * yield* NetworkFirewall.LoggingConfiguration("Logging", {
64 * firewallArn: firewall.firewallArn,
65 * logDestinationConfigs: [
66 * {
67 * LogType: "ALERT",
68 * LogDestinationType: "S3",
69 * LogDestination: { bucketName: bucket.bucketName, prefix: "alerts" },
70 * },
71 * ],
72 * });
73 * ```
74 *
75 * @resource
76 */
77 export const LoggingConfiguration = Resource<LoggingConfiguration>(
78 "AWS.NetworkFirewall.LoggingConfiguration",
79 );
80
81 export const LoggingConfigurationProvider = () =>
82 Provider.effect(
83 LoggingConfiguration,
84 Effect.gen(function* () {
85 const readConfigs = Effect.fn(function* (firewallArn: string) {
86 const response = yield* nfw.describeLoggingConfiguration({
87 FirewallArn: firewallArn,
88 });
89 return [...(response.LoggingConfiguration?.LogDestinationConfigs ?? [])];
90 });
91
92 const applyConfigs = Effect.fn(function* (
93 firewallArn: string,
94 configs: NFW.LogDestinationConfig[],
95 ) {
96 yield* nfw.updateLoggingConfiguration({
97 FirewallArn: firewallArn,
98 LoggingConfiguration: { LogDestinationConfigs: configs },
99 });
100 });
101
102 // Converge the live logging configuration to `desired`, one change per
103 // UpdateLoggingConfiguration call as the API requires (a single call
104 // may only add one config, remove one config, or change one existing
105 // destination).
106 const converge = Effect.fn(function* (
107 firewallArn: string,
108 desired: NFW.LogDestinationConfig[],
109 ) {
110 let current = yield* readConfigs(firewallArn);
111 const desiredByType = new Map(desired.map((d) => [d.LogType, d]));
112
113 // Remove log types no longer desired.
114 for (const config of current) {
115 if (!desiredByType.has(config.LogType)) {
116 current = current.filter((c) => c.LogType !== config.LogType);
117 yield* applyConfigs(firewallArn, current);
118 }
119 }
120 // Change destinations of existing log types.
121 for (const config of desired) {
122 const existing = current.find((c) => c.LogType === config.LogType);
123 if (existing !== undefined && !deepEqual(existing, config)) {
124 current = current.map((c) => (c.LogType === config.LogType ? config : c));
125 yield* applyConfigs(firewallArn, current);
126 }
127 }
128 // Add missing log types.
129 for (const config of desired) {
130 if (!current.some((c) => c.LogType === config.LogType)) {
131 current = [...current, config];
132 yield* applyConfigs(firewallArn, current);
133 }
134 }
135 });
136
137 return LoggingConfiguration.Provider.of({
138 stables: ["firewallArn"],
139
140 // Keyed by its parent firewall — not independently enumerable in a
141 // useful way (it is a singleton setting, not a discrete resource).
142 list: () => Effect.succeed([]),
143
144 read: Effect.fn(function* ({ olds, output }) {
145 const firewallArn = output?.firewallArn ?? olds?.firewallArn;
146 if (firewallArn === undefined) return undefined;
147 const response = yield* nfw
148 .describeLoggingConfiguration({ FirewallArn: firewallArn })
149 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
150 const configs = response?.LoggingConfiguration?.LogDestinationConfigs ?? [];
151 if (configs.length === 0) return undefined;
152 return { firewallArn };
153 }),
154
155 diff: Effect.fn(function* ({ news, olds }) {
156 if (!isResolved(news)) return undefined;
157 if (olds.firewallArn !== news.firewallArn) {
158 return { action: "replace" } as const;
159 }
160 }),
161
162 reconcile: Effect.fn(function* ({ news, session }) {
163 yield* converge(news.firewallArn, news.logDestinationConfigs);
164 yield* session.note(news.firewallArn);
165 return { firewallArn: news.firewallArn };
166 }),
167
168 delete: Effect.fn(function* ({ output }) {
169 // Reset to no logging, removing one destination per call. The
170 // firewall itself may already be gone — that is success.
171 yield* converge(output.firewallArn, []).pipe(
172 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
173 );
174 }),
175 });
176 }),
177 );