@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 221 lines 7.47 KB
Raw
1 import * as rolesanywhere from "@distilled.cloud/aws/rolesanywhere";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import { createInternalTags, hasAlchemyTags } from "alchemy/Tags";
8 import * as Effect from "effect/Effect";
9 import * as Stream from "effect/Stream";
10 import type { Providers } from "../Providers.ts";
11 import { readRolesAnywhereTags, syncRolesAnywhereTags, toWireTags } from "./internal.ts";
12
13 export interface CrlProps {
14 /**
15 * Name of the CRL. If omitted, a unique name is generated from the app,
16 * stage and logical ID. The name is updatable in place.
17 */
18 crlName?: string;
19 /**
20 * PEM-encoded certificate revocation list issued by the trust anchor's CA.
21 * IAM Roles Anywhere validates presented certificates against the CRL
22 * before issuing credentials.
23 */
24 crlData: string;
25 /**
26 * ARN of the trust anchor the CRL is associated with. Immutable after
27 * import — changing it replaces the CRL.
28 */
29 trustAnchorArn: string;
30 /**
31 * Whether the CRL is enabled. When enabled, certificates listed in the CRL
32 * are unauthorized to receive session credentials.
33 * @default true
34 */
35 enabled?: boolean;
36 /**
37 * User-defined tags for the CRL.
38 */
39 tags?: Record<string, string>;
40 }
41
42 export interface Crl extends Resource<
43 "AWS.RolesAnywhere.Crl",
44 CrlProps,
45 {
46 /**
47 * Unique ID of the CRL.
48 */
49 crlId: string;
50 /**
51 * ARN of the CRL.
52 */
53 crlArn: string;
54 /**
55 * Name of the CRL.
56 */
57 crlName: string;
58 /**
59 * ARN of the trust anchor the CRL applies to.
60 */
61 trustAnchorArn: string;
62 /**
63 * Whether the CRL is enabled (revocation checks are enforced).
64 */
65 enabled: boolean;
66 },
67 never,
68 Providers
69 > {}
70
71 /**
72 * An IAM Roles Anywhere certificate revocation list (CRL). A CRL is a
73 * PEM-encoded list of certificates revoked by the trust anchor's certificate
74 * authority; IAM Roles Anywhere refuses to vend credentials for revoked
75 * certificates while the CRL is enabled.
76 * ### Importing a CRL
77 * **Example:** CRL for a Trust Anchor
78 * ```typescript
79 * const anchor = yield* RolesAnywhere.TrustAnchor("Anchor", {
80 * certificateBundle: CA_CERTIFICATE_PEM,
81 * });
82 * const crl = yield* RolesAnywhere.Crl("Crl", {
83 * crlData: CRL_PEM,
84 * trustAnchorArn: anchor.trustAnchorArn,
85 * });
86 * ```
87 *
88 * ### Rotating the CRL
89 * **Example:** Updated Revocation Data
90 * ```typescript
91 * const crl = yield* RolesAnywhere.Crl("Crl", {
92 * crlData: NEXT_CRL_PEM, // re-deploy with the CA's latest CRL
93 * trustAnchorArn: anchor.trustAnchorArn,
94 * });
95 * ```
96 *
97 * @resource
98 */
99 export const Crl = Resource<Crl>("AWS.RolesAnywhere.Crl");
100
101 const toAttrs = (detail: rolesanywhere.CrlDetail) => ({
102 crlId: detail.crlId!,
103 crlArn: detail.crlArn!,
104 crlName: detail.name!,
105 trustAnchorArn: detail.trustAnchorArn!,
106 enabled: detail.enabled ?? false,
107 });
108
109 const decodeCrlData = (data: Uint8Array | undefined): string | undefined =>
110 data === undefined ? undefined : new TextDecoder().decode(data);
111
112 export const CrlProvider = () =>
113 Provider.effect(
114 Crl,
115 Effect.gen(function* () {
116 const createName = Effect.fn(function* (id: string, props: Partial<CrlProps>) {
117 return props.crlName ?? (yield* createPhysicalName({ id, maxLength: 255 }));
118 });
119
120 /** Find a CRL by its user-facing name across all pages. */
121 const findByName = (name: string) =>
122 rolesanywhere.listCrls.items({}).pipe(
123 Stream.filter((detail) => detail.name === name),
124 Stream.runHead,
125 Effect.map((head) => (head._tag === "Some" ? head.value : undefined)),
126 );
127
128 const getById = (crlId: string) =>
129 rolesanywhere.getCrl({ crlId }).pipe(
130 Effect.map((r) => r.crl),
131 Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
132 );
133
134 return {
135 stables: ["crlId", "crlArn"],
136
137 diff: Effect.fn(function* ({ olds, news }) {
138 if (!isResolved(news)) return undefined;
139 // updateCrl only accepts name + crlData — the trust anchor
140 // association is immutable, so a change forces a replacement.
141 if (olds?.trustAnchorArn !== undefined && olds.trustAnchorArn !== news.trustAnchorArn) {
142 return { action: "replace" } as const;
143 }
144 }),
145
146 read: Effect.fn(function* ({ id, olds, output }) {
147 const found = output?.crlId
148 ? yield* getById(output.crlId)
149 : yield* findByName(yield* createName(id, olds ?? {}));
150 if (found === undefined) return undefined;
151 const attrs = toAttrs(found);
152 const tags = yield* readRolesAnywhereTags(attrs.crlArn);
153 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
154 }),
155
156 reconcile: Effect.fn(function* ({ id, news, output, session }) {
157 const name = output?.crlName ?? (yield* createName(id, news));
158 const internalTags = yield* createInternalTags(id);
159 const desiredTags = { ...internalTags, ...news.tags };
160 const desiredEnabled = news.enabled ?? true;
161 const desiredPem = news.crlData.trim();
162 const encodePem = Effect.sync(() => new TextEncoder().encode(desiredPem));
163
164 // 1. Observe — cloud state is authoritative; output caches the id.
165 let live = output?.crlId ? yield* getById(output.crlId) : yield* findByName(name);
166
167 // 2. Ensure — import if missing.
168 if (live === undefined) {
169 const imported = yield* rolesanywhere.importCrl({
170 name,
171 crlData: yield* encodePem,
172 trustAnchorArn: news.trustAnchorArn,
173 enabled: desiredEnabled,
174 tags: toWireTags(desiredTags),
175 });
176 live = imported.crl;
177 } else {
178 // 3. Sync — converge name and revocation data.
179 const desiredName = news.crlName ?? live.name!;
180 const observedPem = decodeCrlData(live.crlData)?.trim();
181 if (live.name !== desiredName || observedPem !== desiredPem) {
182 const updated = yield* rolesanywhere.updateCrl({
183 crlId: live.crlId!,
184 name: desiredName,
185 crlData: yield* encodePem,
186 });
187 live = updated.crl;
188 }
189 }
190
191 if ((live.enabled ?? false) !== desiredEnabled) {
192 // The enable/disable response can echo the pre-toggle state — the
193 // successful call itself is authoritative for the flag.
194 yield* desiredEnabled
195 ? rolesanywhere.enableCrl({ crlId: live.crlId! })
196 : rolesanywhere.disableCrl({ crlId: live.crlId! });
197 live = { ...live, enabled: desiredEnabled };
198 }
199
200 // 3b. Sync tags against observed cloud tags.
201 yield* syncRolesAnywhereTags(live.crlArn!, desiredTags);
202
203 yield* session.note(live.crlId!);
204 return toAttrs(live);
205 }),
206
207 delete: Effect.fn(function* ({ output }) {
208 yield* rolesanywhere
209 .deleteCrl({ crlId: output.crlId })
210 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
211 }),
212
213 list: () =>
214 rolesanywhere.listCrls.items({}).pipe(
215 Stream.map(toAttrs),
216 Stream.runCollect,
217 Effect.map((chunk) => Array.from(chunk)),
218 ),
219 };
220 }),
221 );