The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type * as NFW from "@distilled.cloud/aws/network-firewall"; |
| 2 | import * as nfw from "@distilled.cloud/aws/network-firewall"; |
| 3 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 4 | import { deepEqual, isResolved } from "alchemy/Diff"; |
| 5 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import { Resource } from "alchemy/Resource"; |
| 8 | import { createInternalTags, diffTags, hasAlchemyTags } from "alchemy/Tags"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | nfwTagsToRecord, |
| 14 | recordToNfwTags, |
| 15 | retryWhileNfwInUse, |
| 16 | retryWhileNfwNotFound, |
| 17 | } from "./internal.ts"; |
| 18 | |
| 19 | export interface FirewallPolicyProps { |
| 20 | /** |
| 21 | * Name of the firewall policy. Must be 1-128 alphanumeric characters or |
| 22 | * hyphens. If omitted, a deterministic physical name is generated. |
| 23 | * Changing the name replaces the policy. |
| 24 | */ |
| 25 | firewallPolicyName?: string; |
| 26 | /** |
| 27 | * The policy definition: stateless/stateful rule group references, |
| 28 | * default actions, and engine options. Uses raw Network Firewall API |
| 29 | * structures. `StatelessDefaultActions` and |
| 30 | * `StatelessFragmentDefaultActions` are required. |
| 31 | */ |
| 32 | firewallPolicy: NFW.FirewallPolicy; |
| 33 | /** |
| 34 | * Human-readable description of the firewall policy. |
| 35 | */ |
| 36 | description?: string; |
| 37 | /** |
| 38 | * Tags to apply to the policy. Merged with internal Alchemy tags. |
| 39 | */ |
| 40 | tags?: Record<string, string>; |
| 41 | } |
| 42 | |
| 43 | export interface FirewallPolicy extends Resource< |
| 44 | "AWS.NetworkFirewall.FirewallPolicy", |
| 45 | FirewallPolicyProps, |
| 46 | { |
| 47 | /** Name of the firewall policy. */ |
| 48 | firewallPolicyName: string; |
| 49 | /** ARN of the firewall policy. */ |
| 50 | firewallPolicyArn: string; |
| 51 | /** Server-assigned unique id of the firewall policy. */ |
| 52 | firewallPolicyId: string; |
| 53 | }, |
| 54 | never, |
| 55 | Providers |
| 56 | > {} |
| 57 | |
| 58 | /** |
| 59 | * An AWS Network Firewall policy — defines a firewall's traffic inspection |
| 60 | * behavior as a collection of stateless and stateful |
| 61 | * {@link RuleGroup | rule group} references plus default actions. One policy |
| 62 | * can be shared by multiple {@link Firewall | firewalls}. |
| 63 | * ### Creating Policies |
| 64 | * **Example:** Pass-everything Policy |
| 65 | * ```typescript |
| 66 | * import * as NetworkFirewall from "@alchemy.run/aws/NetworkFirewall"; |
| 67 | * |
| 68 | * const policy = yield* NetworkFirewall.FirewallPolicy("Policy", { |
| 69 | * firewallPolicy: { |
| 70 | * StatelessDefaultActions: ["aws:pass"], |
| 71 | * StatelessFragmentDefaultActions: ["aws:pass"], |
| 72 | * }, |
| 73 | * }); |
| 74 | * ``` |
| 75 | * |
| 76 | * **Example:** Policy referencing Rule Groups |
| 77 | * ```typescript |
| 78 | * const stateless = yield* NetworkFirewall.RuleGroup("Stateless", { |
| 79 | * type: "STATELESS", |
| 80 | * capacity: 10, |
| 81 | * ruleGroup: { ... }, |
| 82 | * }); |
| 83 | * |
| 84 | * const policy = yield* NetworkFirewall.FirewallPolicy("Policy", { |
| 85 | * firewallPolicy: { |
| 86 | * StatelessDefaultActions: ["aws:forward_to_sfe"], |
| 87 | * StatelessFragmentDefaultActions: ["aws:forward_to_sfe"], |
| 88 | * StatelessRuleGroupReferences: [ |
| 89 | * { ResourceArn: stateless.ruleGroupArn, Priority: 1 }, |
| 90 | * ], |
| 91 | * }, |
| 92 | * }); |
| 93 | * ``` |
| 94 | * |
| 95 | * @resource |
| 96 | */ |
| 97 | export const FirewallPolicy = Resource<FirewallPolicy>("AWS.NetworkFirewall.FirewallPolicy"); |
| 98 | |
| 99 | export const FirewallPolicyProvider = () => |
| 100 | Provider.effect( |
| 101 | FirewallPolicy, |
| 102 | Effect.gen(function* () { |
| 103 | const createName = Effect.fn(function* (id: string, props: { firewallPolicyName?: string }) { |
| 104 | return props.firewallPolicyName ?? (yield* createPhysicalName({ id, maxLength: 128 })); |
| 105 | }); |
| 106 | |
| 107 | const toAttrs = (response: NFW.FirewallPolicyResponse): FirewallPolicy["Attributes"] => ({ |
| 108 | firewallPolicyName: response.FirewallPolicyName, |
| 109 | firewallPolicyArn: response.FirewallPolicyArn, |
| 110 | firewallPolicyId: response.FirewallPolicyId, |
| 111 | }); |
| 112 | |
| 113 | return FirewallPolicy.Provider.of({ |
| 114 | stables: ["firewallPolicyName", "firewallPolicyArn", "firewallPolicyId"], |
| 115 | |
| 116 | list: () => |
| 117 | Effect.gen(function* () { |
| 118 | const pages = yield* nfw.listFirewallPolicies.pages({}).pipe(Stream.runCollect); |
| 119 | const metas = Array.from(pages).flatMap((page) => page.FirewallPolicies ?? []); |
| 120 | const items = yield* Effect.forEach( |
| 121 | metas, |
| 122 | (meta) => |
| 123 | nfw.describeFirewallPolicy({ FirewallPolicyArn: meta.Arn }).pipe( |
| 124 | Effect.map((r) => toAttrs(r.FirewallPolicyResponse)), |
| 125 | Effect.catchTag("ResourceNotFoundException", () => Effect.undefined), |
| 126 | ), |
| 127 | { concurrency: 5 }, |
| 128 | ); |
| 129 | return items.filter((item): item is FirewallPolicy["Attributes"] => item !== undefined); |
| 130 | }), |
| 131 | |
| 132 | read: Effect.fn(function* ({ id, olds, output }) { |
| 133 | const name = output?.firewallPolicyName ?? (yield* createName(id, olds ?? {})); |
| 134 | const found = yield* nfw |
| 135 | .describeFirewallPolicy({ FirewallPolicyName: name }) |
| 136 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 137 | if (found === undefined) return undefined; |
| 138 | const attrs = toAttrs(found.FirewallPolicyResponse); |
| 139 | const tags = nfwTagsToRecord(found.FirewallPolicyResponse.Tags); |
| 140 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 141 | }), |
| 142 | |
| 143 | diff: Effect.fn(function* ({ id, news, olds }) { |
| 144 | if (!isResolved(news)) return undefined; |
| 145 | const oldName = yield* createName(id, olds); |
| 146 | const newName = yield* createName(id, news); |
| 147 | if (oldName !== newName) { |
| 148 | return { action: "replace" } as const; |
| 149 | } |
| 150 | }), |
| 151 | |
| 152 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 153 | const name = output?.firewallPolicyName ?? (yield* createName(id, news)); |
| 154 | const internalTags = yield* createInternalTags(id); |
| 155 | const desiredTags: Record<string, string> = { |
| 156 | ...news.tags, |
| 157 | ...internalTags, |
| 158 | }; |
| 159 | |
| 160 | // 1. Observe — cloud state is authoritative. |
| 161 | let observed = yield* nfw |
| 162 | .describeFirewallPolicy({ FirewallPolicyName: name }) |
| 163 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 164 | |
| 165 | // 2. Ensure — create if missing. |
| 166 | if (observed === undefined) { |
| 167 | yield* nfw.createFirewallPolicy({ |
| 168 | FirewallPolicyName: name, |
| 169 | FirewallPolicy: news.firewallPolicy, |
| 170 | Description: news.description, |
| 171 | Tags: recordToNfwTags(desiredTags), |
| 172 | }); |
| 173 | observed = yield* nfw |
| 174 | .describeFirewallPolicy({ FirewallPolicyName: name }) |
| 175 | .pipe(retryWhileNfwNotFound); |
| 176 | } |
| 177 | |
| 178 | // 3. Sync definition — compare desired against OBSERVED state. |
| 179 | const observedDescription = observed.FirewallPolicyResponse.Description; |
| 180 | const definitionDiffers = |
| 181 | !deepEqual(news.firewallPolicy, observed.FirewallPolicy) || |
| 182 | (news.description ?? undefined) !== observedDescription; |
| 183 | if (definitionDiffers) { |
| 184 | yield* nfw.updateFirewallPolicy({ |
| 185 | UpdateToken: observed.UpdateToken, |
| 186 | FirewallPolicyArn: observed.FirewallPolicyResponse.FirewallPolicyArn, |
| 187 | FirewallPolicy: news.firewallPolicy, |
| 188 | Description: news.description, |
| 189 | }); |
| 190 | } |
| 191 | |
| 192 | // 3b. Sync tags — diff against OBSERVED cloud tags. |
| 193 | const observedTags = nfwTagsToRecord(observed.FirewallPolicyResponse.Tags); |
| 194 | const { upsert, removed } = diffTags(observedTags, desiredTags); |
| 195 | const arn = observed.FirewallPolicyResponse.FirewallPolicyArn; |
| 196 | if (upsert.length > 0) { |
| 197 | yield* nfw.tagResource({ ResourceArn: arn, Tags: upsert }); |
| 198 | } |
| 199 | if (removed.length > 0) { |
| 200 | yield* nfw.untagResource({ ResourceArn: arn, TagKeys: removed }); |
| 201 | } |
| 202 | |
| 203 | yield* session.note(name); |
| 204 | return toAttrs(observed.FirewallPolicyResponse); |
| 205 | }), |
| 206 | |
| 207 | delete: Effect.fn(function* ({ output }) { |
| 208 | // A policy still associated to a firewall (deleted moments ago by |
| 209 | // the engine) rejects deletion with InvalidOperationException — |
| 210 | // retry through the release window. |
| 211 | yield* nfw |
| 212 | .deleteFirewallPolicy({ |
| 213 | FirewallPolicyArn: output.firewallPolicyArn, |
| 214 | }) |
| 215 | .pipe( |
| 216 | retryWhileNfwInUse, |
| 217 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 218 | ); |
| 219 | }), |
| 220 | }); |
| 221 | }), |
| 222 | ); |