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
| 1 | import * as iam from "@distilled.cloud/aws/iam"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import { createInternalTags, createTagsList, diffTags, hasTags } from "alchemy/Tags"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Redacted from "effect/Redacted"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { toTagRecord } from "./common.ts"; |
| 13 | |
| 14 | export class ServerCertificateConflict extends Data.TaggedError("ServerCertificateConflict")<{ |
| 15 | readonly message: string; |
| 16 | readonly cause?: unknown; |
| 17 | }> {} |
| 18 | |
| 19 | export class ServerCertificateUnexpectedResponse extends Data.TaggedError( |
| 20 | "ServerCertificateUnexpectedResponse", |
| 21 | )<{ |
| 22 | readonly message: string; |
| 23 | readonly cause?: unknown; |
| 24 | }> {} |
| 25 | |
| 26 | export interface ServerCertificateProps { |
| 27 | /** |
| 28 | * Name of the server certificate. If omitted, a deterministic name is generated. |
| 29 | */ |
| 30 | serverCertificateName?: string; |
| 31 | /** |
| 32 | * Optional IAM path prefix. |
| 33 | * @default "/" |
| 34 | */ |
| 35 | path?: string; |
| 36 | /** |
| 37 | * PEM-encoded leaf certificate body. |
| 38 | */ |
| 39 | certificateBody: string; |
| 40 | /** |
| 41 | * PEM-encoded private key. AWS never returns this after upload. |
| 42 | */ |
| 43 | privateKey: Redacted.Redacted<string> | string; |
| 44 | /** |
| 45 | * Optional PEM-encoded certificate chain. |
| 46 | */ |
| 47 | certificateChain?: string; |
| 48 | /** |
| 49 | * User-defined tags. |
| 50 | */ |
| 51 | tags?: Record<string, string>; |
| 52 | } |
| 53 | |
| 54 | export interface ServerCertificate extends Resource< |
| 55 | "AWS.IAM.ServerCertificate", |
| 56 | ServerCertificateProps, |
| 57 | { |
| 58 | /** The ARN of the server certificate. */ |
| 59 | serverCertificateArn: string; |
| 60 | /** The name of the server certificate. */ |
| 61 | serverCertificateName: string; |
| 62 | /** The stable unique ID of the server certificate. */ |
| 63 | serverCertificateId: string | undefined; |
| 64 | /** The IAM path of the server certificate. */ |
| 65 | path: string | undefined; |
| 66 | /** The PEM-encoded certificate body. */ |
| 67 | certificateBody: string; |
| 68 | /** The PEM-encoded certificate chain, if uploaded. */ |
| 69 | certificateChain: string | undefined; |
| 70 | /** When the certificate was uploaded. */ |
| 71 | uploadDate: Date | undefined; |
| 72 | /** When the certificate expires. */ |
| 73 | expiration: Date | undefined; |
| 74 | /** The tags applied to the certificate. */ |
| 75 | tags: Record<string, string>; |
| 76 | }, |
| 77 | never, |
| 78 | Providers |
| 79 | > {} |
| 80 | |
| 81 | /** |
| 82 | * An IAM server certificate. |
| 83 | * |
| 84 | * `ServerCertificate` uploads and tracks a TLS certificate bundle for legacy |
| 85 | * IAM-integrated services. The private key is write-only and should be provided |
| 86 | * as a redacted value when possible. |
| 87 | * ### Uploading Server Certificates |
| 88 | * **Example:** Upload a TLS Certificate |
| 89 | * ```typescript |
| 90 | * const certificate = yield* ServerCertificate("ApiTlsCertificate", { |
| 91 | * certificateBody: "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----", |
| 92 | * privateKey: Redacted.make( |
| 93 | * "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----", |
| 94 | * ), |
| 95 | * certificateChain: "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----", |
| 96 | * }); |
| 97 | * ``` |
| 98 | * |
| 99 | * @resource |
| 100 | */ |
| 101 | export const ServerCertificate = Resource<ServerCertificate>("AWS.IAM.ServerCertificate"); |
| 102 | |
| 103 | export const ServerCertificateProvider = () => |
| 104 | Provider.effect( |
| 105 | ServerCertificate, |
| 106 | Effect.gen(function* () { |
| 107 | const toName = (id: string, props: ServerCertificateProps) => |
| 108 | props.serverCertificateName |
| 109 | ? Effect.succeed(props.serverCertificateName) |
| 110 | : createPhysicalName({ id, maxLength: 128 }); |
| 111 | |
| 112 | const readCertificate = Effect.fn(function* (name: string) { |
| 113 | const response = yield* iam |
| 114 | .getServerCertificate({ |
| 115 | ServerCertificateName: name, |
| 116 | }) |
| 117 | .pipe(Effect.catchTag("NoSuchEntityException", () => Effect.undefined)); |
| 118 | return response?.ServerCertificate; |
| 119 | }); |
| 120 | |
| 121 | return { |
| 122 | stables: ["serverCertificateArn", "serverCertificateName", "serverCertificateId"], |
| 123 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 124 | if (!isResolved(news)) return; |
| 125 | if ( |
| 126 | (yield* toName(id, olds ?? ({} as ServerCertificateProps))) !== |
| 127 | (yield* toName(id, news)) || |
| 128 | (olds.path ?? "/") !== (news.path ?? "/") || |
| 129 | olds.certificateBody !== news.certificateBody || |
| 130 | olds.certificateChain !== news.certificateChain |
| 131 | ) { |
| 132 | return { action: "replace" } as const; |
| 133 | } |
| 134 | }), |
| 135 | read: Effect.fn(function* ({ id, olds, output }) { |
| 136 | const name = |
| 137 | output?.serverCertificateName ?? |
| 138 | (yield* toName(id, olds ?? ({} as ServerCertificateProps))); |
| 139 | const cert = yield* readCertificate(name); |
| 140 | if (!cert?.ServerCertificateMetadata?.Arn) { |
| 141 | return undefined; |
| 142 | } |
| 143 | const tags = yield* iam.listServerCertificateTags({ |
| 144 | ServerCertificateName: name, |
| 145 | }); |
| 146 | return { |
| 147 | serverCertificateArn: cert.ServerCertificateMetadata.Arn, |
| 148 | serverCertificateName: cert.ServerCertificateMetadata.ServerCertificateName, |
| 149 | serverCertificateId: cert.ServerCertificateMetadata.ServerCertificateId, |
| 150 | path: cert.ServerCertificateMetadata.Path, |
| 151 | certificateBody: cert.CertificateBody, |
| 152 | certificateChain: cert.CertificateChain, |
| 153 | uploadDate: cert.ServerCertificateMetadata.UploadDate, |
| 154 | expiration: cert.ServerCertificateMetadata.Expiration, |
| 155 | tags: toTagRecord(tags.Tags), |
| 156 | }; |
| 157 | }), |
| 158 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 159 | const name = output?.serverCertificateName ?? (yield* toName(id, news)); |
| 160 | const desiredTags = { |
| 161 | ...(yield* createInternalTags(id)), |
| 162 | ...news.tags, |
| 163 | }; |
| 164 | |
| 165 | // Observe — `getServerCertificate` returns the live cert (or |
| 166 | // absence). The cert body / chain are immutable (`diff` |
| 167 | // triggers replacement), so we never re-upload during sync. |
| 168 | let cert = yield* readCertificate(name); |
| 169 | |
| 170 | // Ensure — upload when missing. On race, verify alchemy |
| 171 | // ownership tags; bail out otherwise. |
| 172 | if (!cert?.ServerCertificateMetadata?.Arn) { |
| 173 | const created = yield* iam |
| 174 | .uploadServerCertificate({ |
| 175 | Path: news.path, |
| 176 | ServerCertificateName: name, |
| 177 | CertificateBody: news.certificateBody, |
| 178 | PrivateKey: |
| 179 | typeof news.privateKey === "string" |
| 180 | ? news.privateKey |
| 181 | : Redacted.value(news.privateKey), |
| 182 | CertificateChain: news.certificateChain, |
| 183 | Tags: createTagsList(desiredTags), |
| 184 | }) |
| 185 | .pipe( |
| 186 | Effect.catchTag("EntityAlreadyExistsException", () => |
| 187 | Effect.gen(function* () { |
| 188 | const existing = yield* readCertificate(name); |
| 189 | if (!existing?.ServerCertificateMetadata?.Arn) { |
| 190 | return yield* new ServerCertificateConflict({ |
| 191 | message: `Server certificate '${name}' already exists but could not be described`, |
| 192 | }); |
| 193 | } |
| 194 | if (!hasTags(desiredTags, existing.Tags)) { |
| 195 | return yield* new ServerCertificateConflict({ |
| 196 | message: `Server certificate '${name}' already exists and is not managed by alchemy`, |
| 197 | }); |
| 198 | } |
| 199 | return { |
| 200 | ServerCertificateMetadata: existing.ServerCertificateMetadata, |
| 201 | }; |
| 202 | }), |
| 203 | ), |
| 204 | ); |
| 205 | if (!created.ServerCertificateMetadata?.Arn) { |
| 206 | return yield* new ServerCertificateUnexpectedResponse({ |
| 207 | message: `uploadServerCertificate returned no metadata`, |
| 208 | }); |
| 209 | } |
| 210 | cert = yield* readCertificate(name); |
| 211 | } |
| 212 | |
| 213 | // Sync tags against the cloud's actual tags. |
| 214 | const observedTags = toTagRecord(cert?.Tags); |
| 215 | const { removed, upsert } = diffTags(observedTags, desiredTags); |
| 216 | if (upsert.length > 0) { |
| 217 | yield* iam.tagServerCertificate({ |
| 218 | ServerCertificateName: name, |
| 219 | Tags: upsert, |
| 220 | }); |
| 221 | } |
| 222 | if (removed.length > 0) { |
| 223 | yield* iam.untagServerCertificate({ |
| 224 | ServerCertificateName: name, |
| 225 | TagKeys: removed, |
| 226 | }); |
| 227 | } |
| 228 | |
| 229 | // Re-read for fresh metadata. |
| 230 | const fresh = yield* readCertificate(name); |
| 231 | const metadata = fresh?.ServerCertificateMetadata; |
| 232 | if (!metadata?.Arn || !metadata.ServerCertificateName) { |
| 233 | return yield* new ServerCertificateUnexpectedResponse({ |
| 234 | message: `Server certificate '${name}' was not readable after sync`, |
| 235 | }); |
| 236 | } |
| 237 | |
| 238 | yield* session.note(metadata.Arn); |
| 239 | return { |
| 240 | serverCertificateArn: metadata.Arn, |
| 241 | serverCertificateName: metadata.ServerCertificateName, |
| 242 | serverCertificateId: metadata.ServerCertificateId, |
| 243 | path: metadata.Path, |
| 244 | certificateBody: fresh?.CertificateBody ?? news.certificateBody, |
| 245 | certificateChain: fresh?.CertificateChain ?? news.certificateChain, |
| 246 | uploadDate: metadata.UploadDate, |
| 247 | expiration: metadata.Expiration, |
| 248 | tags: desiredTags, |
| 249 | }; |
| 250 | }), |
| 251 | list: () => |
| 252 | Effect.gen(function* () { |
| 253 | // IAM is global; `listServerCertificates` enumerates every cert in |
| 254 | // the account. Metadata lacks the body/chain/tags, so hydrate each |
| 255 | // entry to produce the full Attributes shape `read` returns. |
| 256 | const metadatas = yield* iam.listServerCertificates.pages({}).pipe( |
| 257 | Stream.runCollect, |
| 258 | Effect.map((chunk) => |
| 259 | Array.from(chunk).flatMap((page) => page.ServerCertificateMetadataList ?? []), |
| 260 | ), |
| 261 | ); |
| 262 | const rows = yield* Effect.forEach( |
| 263 | metadatas, |
| 264 | (meta) => |
| 265 | Effect.gen(function* () { |
| 266 | const cert = yield* readCertificate(meta.ServerCertificateName); |
| 267 | // Raced delete between list and hydrate — skip it. |
| 268 | if (!cert?.ServerCertificateMetadata?.Arn) { |
| 269 | return undefined; |
| 270 | } |
| 271 | const tags = yield* iam.listServerCertificateTags({ |
| 272 | ServerCertificateName: meta.ServerCertificateName, |
| 273 | }); |
| 274 | return { |
| 275 | serverCertificateArn: cert.ServerCertificateMetadata.Arn, |
| 276 | serverCertificateName: cert.ServerCertificateMetadata.ServerCertificateName, |
| 277 | serverCertificateId: cert.ServerCertificateMetadata.ServerCertificateId, |
| 278 | path: cert.ServerCertificateMetadata.Path, |
| 279 | certificateBody: cert.CertificateBody, |
| 280 | certificateChain: cert.CertificateChain, |
| 281 | uploadDate: cert.ServerCertificateMetadata.UploadDate, |
| 282 | expiration: cert.ServerCertificateMetadata.Expiration, |
| 283 | tags: toTagRecord(tags.Tags), |
| 284 | }; |
| 285 | }), |
| 286 | { concurrency: 10 }, |
| 287 | ); |
| 288 | return rows.filter((row) => row !== undefined); |
| 289 | }), |
| 290 | delete: Effect.fn(function* ({ output }) { |
| 291 | yield* iam |
| 292 | .deleteServerCertificate({ |
| 293 | ServerCertificateName: output.serverCertificateName, |
| 294 | }) |
| 295 | .pipe(Effect.catchTag("NoSuchEntityException", () => Effect.void)); |
| 296 | }), |
| 297 | }; |
| 298 | }), |
| 299 | ); |