@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 181 lines 6.07 KB
Raw
1 import * as iam from "@distilled.cloud/aws/iam";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Effect from "effect/Effect";
6 import * as Stream from "effect/Stream";
7 import type { Providers } from "../Providers.ts";
8
9 export interface SigningCertificateProps {
10 /**
11 * User that owns the signing certificate.
12 */
13 userName: string;
14 /**
15 * X.509 signing certificate body.
16 */
17 certificateBody: string;
18 /**
19 * Desired certificate status.
20 * @default "Active"
21 */
22 status?: iam.StatusType;
23 }
24
25 export interface SigningCertificate extends Resource<
26 "AWS.IAM.SigningCertificate",
27 SigningCertificateProps,
28 {
29 /** The IAM user the signing certificate belongs to. */
30 userName: string;
31 /** The unique ID of the signing certificate. */
32 certificateId: string;
33 /** The PEM-encoded certificate body. */
34 certificateBody: string;
35 /** Whether the certificate is `Active` or `Inactive`. */
36 status: iam.StatusType;
37 /** When the certificate was uploaded. */
38 uploadDate: Date | undefined;
39 },
40 never,
41 Providers
42 > {}
43
44 /**
45 * An IAM signing certificate for a user.
46 *
47 * `SigningCertificate` uploads an X.509 signing certificate for legacy
48 * IAM-integrated workflows that still depend on user-scoped certificates.
49 * ### Managing User Certificates
50 * **Example:** Upload a Signing Certificate
51 * ```typescript
52 * const user = yield* User("Signer", {
53 * userName: "build-signer",
54 * });
55 *
56 * const certificate = yield* SigningCertificate("SigningCertificate", {
57 * userName: user.userName,
58 * certificateBody: "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",
59 * });
60 * ```
61 *
62 * @resource
63 */
64 export const SigningCertificate = Resource<SigningCertificate>("AWS.IAM.SigningCertificate");
65
66 export const SigningCertificateProvider = () =>
67 Provider.succeed(SigningCertificate, {
68 stables: ["certificateId"],
69 // IAM is a global service. `listSigningCertificates` requires a `UserName`,
70 // so we enumerate every IAM user first (paginated) and then list each
71 // user's signing certificates (also paginated) with bounded concurrency.
72 // The list response carries the full certificate body, so each entry maps
73 // directly to the `Attributes` shape `read` returns without further
74 // hydration.
75 list: Effect.fn(function* () {
76 const users = yield* iam.listUsers.pages({}).pipe(
77 Stream.runCollect,
78 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.Users ?? [])),
79 );
80 const perUser = yield* Effect.forEach(
81 users,
82 (user) =>
83 iam.listSigningCertificates.pages({ UserName: user.UserName }).pipe(
84 Stream.runCollect,
85 Effect.map((chunk) =>
86 Array.from(chunk)
87 .flatMap((page) => page.Certificates ?? [])
88 .map((cert) => ({
89 userName: cert.UserName,
90 certificateId: cert.CertificateId,
91 certificateBody: cert.CertificateBody,
92 status: cert.Status,
93 uploadDate: cert.UploadDate,
94 })),
95 ),
96 // The user may be deleted between enumeration and per-user list.
97 Effect.catchTag("NoSuchEntityException", () => Effect.succeed([])),
98 ),
99 { concurrency: 10 },
100 );
101 return perUser.flat();
102 }),
103 diff: Effect.fn(function* ({ olds, news }) {
104 if (!isResolved(news)) return;
105 if (olds.userName !== news.userName || olds.certificateBody !== news.certificateBody) {
106 return { action: "replace" } as const;
107 }
108 }),
109 read: Effect.fn(function* ({ output }) {
110 if (!output) {
111 return undefined;
112 }
113 const listed = yield* iam.listSigningCertificates({
114 UserName: output.userName,
115 });
116 const cert = listed.Certificates.find(
117 (entry) => entry.CertificateId === output.certificateId,
118 );
119 if (!cert?.CertificateId) {
120 return undefined;
121 }
122 return {
123 userName: cert.UserName,
124 certificateId: cert.CertificateId,
125 certificateBody: cert.CertificateBody,
126 status: cert.Status,
127 uploadDate: cert.UploadDate,
128 };
129 }),
130 reconcile: Effect.fn(function* ({ news, output, session }) {
131 // Observe — certificate ids are AWS-generated; we can only locate
132 // the live entry via the prior output. The certificate body is
133 // immutable (`diff` triggers replacement on body change), so a
134 // missing entry always means we need to upload.
135 const observed = output
136 ? yield* iam.listSigningCertificates({ UserName: output.userName }).pipe(
137 Effect.map((r) =>
138 r.Certificates.find((entry) => entry.CertificateId === output.certificateId),
139 ),
140 Effect.catchTag("NoSuchEntityException", () => Effect.undefined),
141 )
142 : undefined;
143
144 // Ensure — upload when missing.
145 let cert = observed;
146 if (!cert?.CertificateId) {
147 const uploaded = yield* iam.uploadSigningCertificate({
148 UserName: news.userName,
149 CertificateBody: news.certificateBody,
150 });
151 cert = uploaded.Certificate;
152 }
153
154 // Sync — apply the desired status when it differs.
155 const desiredStatus = news.status ?? cert.Status;
156 if (desiredStatus !== cert.Status) {
157 yield* iam.updateSigningCertificate({
158 UserName: news.userName,
159 CertificateId: cert.CertificateId,
160 Status: desiredStatus,
161 });
162 }
163
164 yield* session.note(cert.CertificateId);
165 return {
166 userName: cert.UserName,
167 certificateId: cert.CertificateId,
168 certificateBody: cert.CertificateBody,
169 status: desiredStatus,
170 uploadDate: cert.UploadDate,
171 };
172 }),
173 delete: Effect.fn(function* ({ output }) {
174 yield* iam
175 .deleteSigningCertificate({
176 UserName: output.userName,
177 CertificateId: output.certificateId,
178 })
179 .pipe(Effect.catchTag("NoSuchEntityException", () => Effect.void));
180 }),
181 });