The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as eks from "@distilled.cloud/aws/eks"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import type { Input } from "alchemy/Input"; |
| 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 Data from "effect/Data"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import type { Providers } from "../Providers.ts"; |
| 13 | |
| 14 | export class PodIdentityAssociationUnexpectedResponse extends Data.TaggedError( |
| 15 | "PodIdentityAssociationUnexpectedResponse", |
| 16 | )<{ |
| 17 | readonly message: string; |
| 18 | readonly cause?: unknown; |
| 19 | }> {} |
| 20 | |
| 21 | export interface PodIdentityAssociationProps { |
| 22 | /** |
| 23 | * Target cluster name. |
| 24 | */ |
| 25 | clusterName: Input<string>; |
| 26 | /** |
| 27 | * Kubernetes namespace that owns the service account. |
| 28 | */ |
| 29 | namespace: string; |
| 30 | /** |
| 31 | * Kubernetes service account name. |
| 32 | */ |
| 33 | serviceAccount: string; |
| 34 | /** |
| 35 | * IAM role ARN assumed by pods for this association. |
| 36 | */ |
| 37 | roleArn: Input<string>; |
| 38 | /** |
| 39 | * Disable session tags for the issued credentials. |
| 40 | */ |
| 41 | disableSessionTags?: boolean; |
| 42 | /** |
| 43 | * Optional target role ARN for chained role assumption. |
| 44 | */ |
| 45 | targetRoleArn?: Input<string>; |
| 46 | /** |
| 47 | * Optional inline session policy JSON. |
| 48 | */ |
| 49 | policy?: string; |
| 50 | /** |
| 51 | * User-defined tags to apply to the association. |
| 52 | */ |
| 53 | tags?: Record<string, string>; |
| 54 | } |
| 55 | |
| 56 | export interface PodIdentityAssociation extends Resource< |
| 57 | "AWS.EKS.PodIdentityAssociation", |
| 58 | PodIdentityAssociationProps, |
| 59 | { |
| 60 | /** The ARN of the pod identity association. */ |
| 61 | associationArn: string; |
| 62 | /** The ID of the pod identity association. */ |
| 63 | associationId: string; |
| 64 | /** The name of the EKS cluster the association belongs to. */ |
| 65 | clusterName: string; |
| 66 | /** The Kubernetes namespace of the bound service account. */ |
| 67 | namespace: string; |
| 68 | /** The name of the Kubernetes service account bound to the role. */ |
| 69 | serviceAccount: string; |
| 70 | /** The ARN of the IAM role pods assume via the association. */ |
| 71 | roleArn: string; |
| 72 | /** Whether EKS session tags are disabled on the assumed-role session. */ |
| 73 | disableSessionTags: boolean; |
| 74 | /** The ARN of the target role for role chaining, if configured. */ |
| 75 | targetRoleArn: string | undefined; |
| 76 | /** The external ID EKS uses when assuming the target role. */ |
| 77 | externalId: string | undefined; |
| 78 | /** The ARN of the add-on that owns the association, if add-on managed. */ |
| 79 | ownerArn: string | undefined; |
| 80 | /** The inline policy document attached to the generated role, if any. */ |
| 81 | policy: string | undefined; |
| 82 | /** The tags applied to the association. */ |
| 83 | tags: Record<string, string>; |
| 84 | }, |
| 85 | never, |
| 86 | Providers |
| 87 | > {} |
| 88 | |
| 89 | /** |
| 90 | * An Amazon EKS pod identity association that binds a service account to an IAM role. |
| 91 | * |
| 92 | * `PodIdentityAssociation` is the canonical workload-identity resource for EKS |
| 93 | * clusters that use EKS Pod Identity instead of IRSA. |
| 94 | * ### Managing Pod Identity |
| 95 | * **Example:** Bind a Service Account to a Role |
| 96 | * ```typescript |
| 97 | * const association = yield* PodIdentityAssociation("ApiIdentity", { |
| 98 | * clusterName: cluster.clusterName, |
| 99 | * namespace: "default", |
| 100 | * serviceAccount: "api", |
| 101 | * roleArn: podRole.roleArn, |
| 102 | * }); |
| 103 | * ``` |
| 104 | * |
| 105 | * @resource |
| 106 | */ |
| 107 | export const PodIdentityAssociation = Resource<PodIdentityAssociation>( |
| 108 | "AWS.EKS.PodIdentityAssociation", |
| 109 | ); |
| 110 | |
| 111 | export const PodIdentityAssociationProvider = () => |
| 112 | Provider.effect( |
| 113 | PodIdentityAssociation, |
| 114 | Effect.gen(function* () { |
| 115 | const toClientRequestToken = (id: string, action: string) => |
| 116 | createPhysicalName({ |
| 117 | id: `${id}-${action}`, |
| 118 | maxLength: 64, |
| 119 | delimiter: "-", |
| 120 | }); |
| 121 | |
| 122 | return { |
| 123 | stables: ["associationArn", "associationId"], |
| 124 | // `list()` enumerates every pod identity association across the |
| 125 | // account/region. The list op is cluster-scoped, so first enumerate all |
| 126 | // clusters (`listClusters`), then list each cluster's associations |
| 127 | // (`listPodIdentityAssociations`), then hydrate each summary via |
| 128 | // `describePodIdentityAssociation` to produce the full Attributes shape. |
| 129 | list: () => |
| 130 | Effect.gen(function* () { |
| 131 | const clusterNames = yield* eks.listClusters.pages({}).pipe( |
| 132 | Stream.runCollect, |
| 133 | Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.clusters ?? [])), |
| 134 | ); |
| 135 | |
| 136 | const perCluster = yield* Effect.forEach( |
| 137 | clusterNames, |
| 138 | (clusterName) => |
| 139 | eks.listPodIdentityAssociations.pages({ clusterName }).pipe( |
| 140 | Stream.runCollect, |
| 141 | Effect.map((chunk) => |
| 142 | Array.from(chunk).flatMap((page) => page.associations ?? []), |
| 143 | ), |
| 144 | Effect.flatMap((summaries) => |
| 145 | Effect.forEach( |
| 146 | summaries, |
| 147 | (summary) => |
| 148 | summary.associationId |
| 149 | ? readAssociationById({ |
| 150 | clusterName, |
| 151 | associationId: summary.associationId, |
| 152 | }) |
| 153 | : Effect.undefined, |
| 154 | { concurrency: 5 }, |
| 155 | ), |
| 156 | ), |
| 157 | ), |
| 158 | { concurrency: 5 }, |
| 159 | ); |
| 160 | |
| 161 | return perCluster |
| 162 | .flat() |
| 163 | .filter( |
| 164 | (association): association is NonNullable<typeof association> => |
| 165 | association !== undefined, |
| 166 | ); |
| 167 | }), |
| 168 | diff: Effect.fn(function* ({ olds, news }) { |
| 169 | if (!isResolved(news)) return; |
| 170 | if (olds.clusterName !== news.clusterName) { |
| 171 | return { action: "replace" } as const; |
| 172 | } |
| 173 | |
| 174 | if (olds.namespace !== news.namespace) { |
| 175 | return { action: "replace" } as const; |
| 176 | } |
| 177 | |
| 178 | if (olds.serviceAccount !== news.serviceAccount) { |
| 179 | return { action: "replace" } as const; |
| 180 | } |
| 181 | }), |
| 182 | read: Effect.fn(function* ({ id, olds, output }) { |
| 183 | if (output?.associationId) { |
| 184 | return yield* readAssociationById({ |
| 185 | clusterName: output.clusterName, |
| 186 | associationId: output.associationId, |
| 187 | }); |
| 188 | } |
| 189 | |
| 190 | return yield* findAssociation({ |
| 191 | id, |
| 192 | clusterName: olds.clusterName as string, |
| 193 | namespace: olds.namespace, |
| 194 | serviceAccount: olds.serviceAccount, |
| 195 | }); |
| 196 | }), |
| 197 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 198 | const clusterName = news.clusterName as string; |
| 199 | const desiredTags = { |
| 200 | ...(yield* createInternalTags(id)), |
| 201 | ...news.tags, |
| 202 | }; |
| 203 | |
| 204 | // Observe — locate the association either by stored |
| 205 | // associationId or by listing on (cluster, namespace, sa). |
| 206 | let state = output?.associationId |
| 207 | ? yield* readAssociationById({ |
| 208 | clusterName, |
| 209 | associationId: output.associationId, |
| 210 | }) |
| 211 | : yield* findAssociation({ |
| 212 | id, |
| 213 | clusterName, |
| 214 | namespace: news.namespace, |
| 215 | serviceAccount: news.serviceAccount, |
| 216 | }); |
| 217 | |
| 218 | // Ensure — create if missing. Tolerate `ResourceInUseException` |
| 219 | // as a race with a peer reconciler. |
| 220 | if (!state) { |
| 221 | yield* eks |
| 222 | .createPodIdentityAssociation({ |
| 223 | clusterName, |
| 224 | namespace: news.namespace, |
| 225 | serviceAccount: news.serviceAccount, |
| 226 | roleArn: news.roleArn as string, |
| 227 | disableSessionTags: news.disableSessionTags, |
| 228 | targetRoleArn: news.targetRoleArn as string | undefined, |
| 229 | policy: news.policy, |
| 230 | tags: desiredTags, |
| 231 | clientRequestToken: yield* toClientRequestToken(id, "create"), |
| 232 | }) |
| 233 | .pipe(Effect.catchTag("ResourceInUseException", () => Effect.void)); |
| 234 | |
| 235 | state = yield* findAssociation({ |
| 236 | id, |
| 237 | clusterName, |
| 238 | namespace: news.namespace, |
| 239 | serviceAccount: news.serviceAccount, |
| 240 | }); |
| 241 | |
| 242 | if (!state) { |
| 243 | return yield* new PodIdentityAssociationUnexpectedResponse({ |
| 244 | message: `PodIdentityAssociation '${news.namespace}/${news.serviceAccount}' could not be read after creation`, |
| 245 | }); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // Sync mutable fields — observed ↔ desired. |
| 250 | const desiredRoleArn = news.roleArn as string; |
| 251 | const desiredTargetRoleArn = news.targetRoleArn as string | undefined; |
| 252 | const desiredDisableSessionTags = news.disableSessionTags ?? false; |
| 253 | if ( |
| 254 | state.roleArn !== desiredRoleArn || |
| 255 | state.disableSessionTags !== desiredDisableSessionTags || |
| 256 | state.targetRoleArn !== desiredTargetRoleArn || |
| 257 | state.policy !== news.policy |
| 258 | ) { |
| 259 | yield* eks.updatePodIdentityAssociation({ |
| 260 | clusterName, |
| 261 | associationId: state.associationId, |
| 262 | roleArn: desiredRoleArn, |
| 263 | disableSessionTags: news.disableSessionTags, |
| 264 | targetRoleArn: desiredTargetRoleArn, |
| 265 | policy: news.policy, |
| 266 | clientRequestToken: yield* toClientRequestToken(id, "update"), |
| 267 | }); |
| 268 | } |
| 269 | |
| 270 | // Sync tags — diff observed cloud tags against desired. |
| 271 | const { removed, upsert } = diffTags(state.tags, desiredTags); |
| 272 | if (upsert.length > 0) { |
| 273 | yield* eks.tagResource({ |
| 274 | resourceArn: state.associationArn, |
| 275 | tags: Object.fromEntries(upsert.map((tag) => [tag.Key, tag.Value] as const)), |
| 276 | }); |
| 277 | } |
| 278 | if (removed.length > 0) { |
| 279 | yield* eks.untagResource({ |
| 280 | resourceArn: state.associationArn, |
| 281 | tagKeys: removed, |
| 282 | }); |
| 283 | } |
| 284 | |
| 285 | // Re-read final state for fresh attributes. |
| 286 | const final = yield* readAssociationById({ |
| 287 | clusterName, |
| 288 | associationId: state.associationId, |
| 289 | }); |
| 290 | if (!final) { |
| 291 | return yield* new PodIdentityAssociationUnexpectedResponse({ |
| 292 | message: `PodIdentityAssociation '${state.associationId}' could not be read after reconcile`, |
| 293 | }); |
| 294 | } |
| 295 | |
| 296 | yield* session.note(final.associationArn); |
| 297 | return final; |
| 298 | }), |
| 299 | delete: Effect.fn(function* ({ output }) { |
| 300 | yield* eks |
| 301 | .deletePodIdentityAssociation({ |
| 302 | clusterName: output.clusterName, |
| 303 | associationId: output.associationId, |
| 304 | }) |
| 305 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 306 | }), |
| 307 | }; |
| 308 | }), |
| 309 | ); |
| 310 | |
| 311 | const normalizeTags = (tags: Record<string, string | undefined> | undefined) => |
| 312 | Object.fromEntries( |
| 313 | Object.entries(tags ?? {}).filter((entry): entry is [string, string] => entry[1] !== undefined), |
| 314 | ); |
| 315 | |
| 316 | const mapAssociation = (association: eks.PodIdentityAssociation) => ({ |
| 317 | associationArn: association.associationArn!, |
| 318 | associationId: association.associationId!, |
| 319 | clusterName: association.clusterName!, |
| 320 | namespace: association.namespace!, |
| 321 | serviceAccount: association.serviceAccount!, |
| 322 | roleArn: association.roleArn!, |
| 323 | disableSessionTags: association.disableSessionTags ?? false, |
| 324 | targetRoleArn: association.targetRoleArn, |
| 325 | externalId: association.externalId, |
| 326 | ownerArn: association.ownerArn, |
| 327 | policy: association.policy, |
| 328 | tags: normalizeTags(association.tags), |
| 329 | }); |
| 330 | |
| 331 | const readAssociationById = Effect.fn(function* ({ |
| 332 | clusterName, |
| 333 | associationId, |
| 334 | }: { |
| 335 | clusterName: string; |
| 336 | associationId: string; |
| 337 | }) { |
| 338 | const response = yield* eks |
| 339 | .describePodIdentityAssociation({ |
| 340 | clusterName, |
| 341 | associationId, |
| 342 | }) |
| 343 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 344 | |
| 345 | const association = response?.association; |
| 346 | if ( |
| 347 | !association?.associationArn || |
| 348 | !association.associationId || |
| 349 | !association.clusterName || |
| 350 | !association.namespace || |
| 351 | !association.serviceAccount || |
| 352 | !association.roleArn |
| 353 | ) { |
| 354 | return undefined; |
| 355 | } |
| 356 | |
| 357 | return mapAssociation(association); |
| 358 | }); |
| 359 | |
| 360 | const findAssociation = Effect.fn(function* ({ |
| 361 | id, |
| 362 | clusterName, |
| 363 | namespace, |
| 364 | serviceAccount, |
| 365 | }: { |
| 366 | id: string; |
| 367 | clusterName: string; |
| 368 | namespace: string; |
| 369 | serviceAccount: string; |
| 370 | }) { |
| 371 | let nextToken: string | undefined; |
| 372 | type Association = { |
| 373 | associationArn: string; |
| 374 | associationId: string; |
| 375 | clusterName: string; |
| 376 | namespace: string; |
| 377 | serviceAccount: string; |
| 378 | roleArn: string; |
| 379 | disableSessionTags: boolean; |
| 380 | targetRoleArn: string | undefined; |
| 381 | externalId: string | undefined; |
| 382 | ownerArn: string | undefined; |
| 383 | policy: string | undefined; |
| 384 | tags: Record<string, string>; |
| 385 | }; |
| 386 | let firstMatch: Association | undefined; |
| 387 | |
| 388 | while (true) { |
| 389 | const response = yield* eks.listPodIdentityAssociations({ |
| 390 | clusterName, |
| 391 | namespace, |
| 392 | serviceAccount, |
| 393 | nextToken, |
| 394 | }); |
| 395 | |
| 396 | for (const summary of response.associations ?? []) { |
| 397 | if (!summary.associationId) { |
| 398 | continue; |
| 399 | } |
| 400 | |
| 401 | const association = yield* readAssociationById({ |
| 402 | clusterName, |
| 403 | associationId: summary.associationId, |
| 404 | }); |
| 405 | |
| 406 | if (!association) continue; |
| 407 | |
| 408 | if (yield* hasAlchemyTags(id, association.tags)) { |
| 409 | return association as Association; |
| 410 | } |
| 411 | firstMatch ??= association as Association; |
| 412 | } |
| 413 | |
| 414 | if (!response.nextToken) { |
| 415 | return firstMatch ? Unowned(firstMatch) : undefined; |
| 416 | } |
| 417 | |
| 418 | nextToken = response.nextToken; |
| 419 | } |
| 420 | }); |