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

main
ts 207 lines 7.62 KB
Raw
1 import * as ds from "@distilled.cloud/aws/directory-service";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import type { Providers } from "../Providers.ts";
9 import { sameStringSet } from "./internal.ts";
10
11 export class ConditionalForwarderUnexpectedResponse extends Data.TaggedError(
12 "ConditionalForwarderUnexpectedResponse",
13 )<{
14 readonly message: string;
15 readonly cause?: unknown;
16 }> {}
17
18 export interface ConditionalForwarderProps {
19 /**
20 * Id of the {@link Directory} (AWS Managed Microsoft AD or AD Connector)
21 * the conditional forwarder is configured on. Simple AD does not support
22 * conditional forwarders. Changing the directory replaces the forwarder.
23 */
24 directoryId: string;
25 /**
26 * Fully qualified domain name of the remote domain DNS queries are
27 * forwarded to, e.g. `partner.example.com`. Changing the domain replaces
28 * the forwarder.
29 */
30 remoteDomainName: string;
31 /**
32 * IP addresses of the remote domain's DNS servers. Updated in place.
33 */
34 dnsIpAddrs: string[];
35 }
36
37 export interface ConditionalForwarder extends Resource<
38 "AWS.DirectoryService.ConditionalForwarder",
39 ConditionalForwarderProps,
40 {
41 /** The ID of the directory the forwarder is attached to. */
42 directoryId: string;
43 /** The fully qualified domain name the forwarder resolves. */
44 remoteDomainName: string;
45 /** The IP addresses of the remote DNS servers. */
46 dnsIpAddrs: string[];
47 /** The replication scope of the forwarder, e.g. `Domain`. */
48 replicationScope: string | undefined;
49 },
50 never,
51 Providers
52 > {}
53
54 /**
55 * A conditional forwarder on an AWS Managed Microsoft AD (or AD Connector)
56 * directory — forwards DNS queries for a remote domain to that domain's DNS
57 * servers. Conditional forwarders are the prerequisite for trust
58 * relationships with other domains.
59 * ### Creating a Conditional Forwarder
60 * **Example:** Forward a Partner Domain
61 * ```typescript
62 * const directory = yield* Directory("Corp", {
63 * type: "MicrosoftAD",
64 * name: "corp.example.com",
65 * password: Redacted.make("SuperSecret123!"),
66 * vpcId: vpc.vpcId,
67 * subnetIds: [subnetA.subnetId, subnetB.subnetId],
68 * });
69 * const forwarder = yield* ConditionalForwarder("Partner", {
70 * directoryId: directory.directoryId,
71 * remoteDomainName: "partner.example.com",
72 * dnsIpAddrs: ["10.10.0.2", "10.10.1.2"],
73 * });
74 * ```
75 *
76 * @resource
77 */
78 export const ConditionalForwarder = Resource<ConditionalForwarder>(
79 "AWS.DirectoryService.ConditionalForwarder",
80 );
81
82 // The directory transiently rejects forwarder mutations while its domain
83 // controllers are busy (DirectoryUnavailableException); bounded retry
84 // through that window.
85 const retryWhileDirectoryUnavailable = <A, E extends { readonly _tag: string }, R>(
86 self: Effect.Effect<A, E, R>,
87 ): Effect.Effect<A, E, R> =>
88 Effect.retry(self, {
89 while: (e) => e._tag === "DirectoryUnavailableException",
90 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(8)]),
91 });
92
93 export const ConditionalForwarderProvider = () =>
94 Provider.effect(
95 ConditionalForwarder,
96 Effect.gen(function* () {
97 const readForwarder = Effect.fn(function* (directoryId: string, remoteDomainName: string) {
98 const response = yield* ds
99 .describeConditionalForwarders({
100 DirectoryId: directoryId,
101 RemoteDomainNames: [remoteDomainName],
102 })
103 .pipe(Effect.catchTag("EntityDoesNotExistException", () => Effect.undefined));
104 return response?.ConditionalForwarders?.find(
105 (forwarder) => forwarder.RemoteDomainName === remoteDomainName,
106 );
107 });
108
109 const toAttrs = (
110 directoryId: string,
111 remoteDomainName: string,
112 forwarder: ds.ConditionalForwarder,
113 ) => ({
114 directoryId,
115 remoteDomainName,
116 dnsIpAddrs: [...(forwarder.DnsIpAddrs ?? [])],
117 replicationScope: forwarder.ReplicationScope,
118 });
119
120 return {
121 stables: ["directoryId", "remoteDomainName"],
122
123 diff: Effect.fn(function* ({ olds, news }) {
124 if (!isResolved(news)) return undefined;
125 if (olds === undefined || news === undefined) return undefined;
126 if (news.directoryId !== olds.directoryId) {
127 return { action: "replace" } as const;
128 }
129 if (news.remoteDomainName !== olds.remoteDomainName) {
130 return { action: "replace" } as const;
131 }
132 }),
133
134 read: Effect.fn(function* ({ olds, output }) {
135 const directoryId = output?.directoryId ?? olds?.directoryId;
136 const remoteDomainName = output?.remoteDomainName ?? olds?.remoteDomainName;
137 if (directoryId === undefined || remoteDomainName === undefined) {
138 return undefined;
139 }
140 const forwarder = yield* readForwarder(directoryId, remoteDomainName);
141 if (forwarder === undefined) return undefined;
142 return toAttrs(directoryId, remoteDomainName, forwarder);
143 }),
144
145 reconcile: Effect.fn(function* ({ news, session }) {
146 const props = news!;
147
148 // 1. Observe — cloud state is authoritative.
149 let observed = yield* readForwarder(props.directoryId, props.remoteDomainName);
150
151 // 2. Ensure — create if missing; tolerate AlreadyExists as a race.
152 if (observed === undefined) {
153 yield* ds
154 .createConditionalForwarder({
155 DirectoryId: props.directoryId,
156 RemoteDomainName: props.remoteDomainName,
157 DnsIpAddrs: props.dnsIpAddrs,
158 })
159 .pipe(
160 retryWhileDirectoryUnavailable,
161 Effect.catchTag("EntityAlreadyExistsException", () => Effect.void),
162 );
163 observed = yield* readForwarder(props.directoryId, props.remoteDomainName);
164 }
165
166 // 3. Sync — update the DNS addresses only when observed state
167 // differs from desired.
168 if (!sameStringSet(observed?.DnsIpAddrs, props.dnsIpAddrs)) {
169 yield* ds
170 .updateConditionalForwarder({
171 DirectoryId: props.directoryId,
172 RemoteDomainName: props.remoteDomainName,
173 DnsIpAddrs: props.dnsIpAddrs,
174 })
175 .pipe(retryWhileDirectoryUnavailable);
176 observed = yield* readForwarder(props.directoryId, props.remoteDomainName);
177 }
178 if (observed === undefined) {
179 return yield* new ConditionalForwarderUnexpectedResponse({
180 message: `conditional forwarder for '${props.remoteDomainName}' on '${props.directoryId}' not visible after create`,
181 });
182 }
183
184 yield* session.note(`${props.directoryId}/${props.remoteDomainName}`);
185 return toAttrs(props.directoryId, props.remoteDomainName, observed);
186 }),
187
188 delete: Effect.fn(function* ({ output }) {
189 yield* ds
190 .deleteConditionalForwarder({
191 DirectoryId: output.directoryId,
192 RemoteDomainName: output.remoteDomainName,
193 })
194 .pipe(
195 retryWhileDirectoryUnavailable,
196 // The forwarder — or the whole parent directory — being gone
197 // is success.
198 Effect.catchTag("EntityDoesNotExistException", () => Effect.void),
199 );
200 }),
201
202 // Conditional forwarders are keyed by their parent directory; there
203 // is no account-wide enumeration.
204 list: () => Effect.succeed([]),
205 };
206 }),
207 );