@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.86 KB
Raw
1 import * as acmpca from "@distilled.cloud/aws/acm-pca";
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 { PolicyDocument } from "../IAM/Policy.ts";
8 import { normalizePolicyDocument, stringifyPolicyDocument } from "../IAM/Policy.ts";
9 import type { Providers } from "../Providers.ts";
10
11 export interface CertificateAuthorityPolicyProps {
12 /**
13 * The ARN of the private CA the resource-based policy is attached to.
14 * Changing this replaces the policy.
15 */
16 certificateAuthorityArn: string;
17 /**
18 * The resource-based permission policy, either as a structured
19 * {@link PolicyDocument} or a raw JSON string (escape hatch). The policy
20 * grants cross-account access on the CA to an Amazon Web Services
21 * account, organization, or organizational unit — a policy that would
22 * lock the CA owner out is rejected by AWS with the typed
23 * `LockoutPreventedException`.
24 */
25 policy: PolicyDocument | string;
26 }
27
28 export interface CertificateAuthorityPolicy extends Resource<
29 "AWS.ACMPCA.CertificateAuthorityPolicy",
30 CertificateAuthorityPolicyProps,
31 {
32 /** The ARN of the CA the policy is attached to. */
33 certificateAuthorityArn: string;
34 /** The attached policy document as a JSON string. */
35 policy: string;
36 },
37 never,
38 Providers
39 > {}
40
41 /**
42 * A resource-based policy attached to a private CA, granting cross-account
43 * access — e.g. allowing a Certificate Manager (ACM) user in another
44 * account to issue and renew certificates signed by this CA. This is the
45 * policy Amazon Web Services Resource Access Manager (RAM) manages when a
46 * CA is shared; attach it directly for fine-grained control.
47 *
48 * ### Attaching a CA Policy
49 * **Example:** Allow Another Account to Issue Certificates
50 * ```typescript
51 * import * as ACMPCA from "@alchemy.run/aws/ACMPCA";
52 *
53 * const policy = yield* ACMPCA.CertificateAuthorityPolicy("CrossAccount", {
54 * certificateAuthorityArn: ca.certificateAuthorityArn,
55 * policy: {
56 * Version: "2012-10-17",
57 * Statement: [
58 * {
59 * Effect: "Allow",
60 * Principal: { AWS: "arn:aws:iam::123456789012:root" },
61 * Action: [
62 * "acm-pca:DescribeCertificateAuthority",
63 * "acm-pca:GetCertificate",
64 * "acm-pca:GetCertificateAuthorityCertificate",
65 * "acm-pca:ListPermissions",
66 * "acm-pca:IssueCertificate",
67 * "acm-pca:RevokeCertificate",
68 * ],
69 * Resource: ca.certificateAuthorityArn,
70 * },
71 * ],
72 * },
73 * });
74 * ```
75 *
76 * @resource
77 */
78 export const CertificateAuthorityPolicy = Resource<CertificateAuthorityPolicy>(
79 "AWS.ACMPCA.CertificateAuthorityPolicy",
80 );
81
82 export const CertificateAuthorityPolicyProvider = () =>
83 Provider.effect(
84 CertificateAuthorityPolicy,
85 Effect.gen(function* () {
86 // A CA without a policy and a missing/deleted CA both answer
87 // getPolicy with ResourceNotFoundException — either way there is no
88 // policy to observe.
89 const observe = (certificateAuthorityArn: string) =>
90 acmpca.getPolicy({ ResourceArn: certificateAuthorityArn }).pipe(
91 Effect.map((response) => response.Policy),
92 Effect.catchTag(
93 ["ResourceNotFoundException", "InvalidStateException"],
94 () => Effect.undefined,
95 ),
96 );
97
98 const toDesired = (policy: PolicyDocument | string) =>
99 typeof policy === "string" ? policy : stringifyPolicyDocument(policy);
100
101 return {
102 stables: ["certificateAuthorityArn"],
103 diff: Effect.fn(function* ({ news, olds }) {
104 if (!("certificateAuthorityArn" in news)) return;
105 // The CA's ARN is stable across its updates, so an unresolved ARN
106 // means the CA is being replaced: the policy moves with it.
107 if (
108 !isResolved(news.certificateAuthorityArn) ||
109 news.certificateAuthorityArn !== olds.certificateAuthorityArn
110 ) {
111 return { action: "replace" } as const;
112 }
113 }),
114 read: Effect.fn(function* ({ olds, output }) {
115 const certificateAuthorityArn =
116 output?.certificateAuthorityArn ?? olds?.certificateAuthorityArn;
117 if (certificateAuthorityArn === undefined) return undefined;
118 const observed = yield* observe(certificateAuthorityArn);
119 if (observed === undefined) return undefined;
120 return { certificateAuthorityArn, policy: observed };
121 }),
122 list: () =>
123 Effect.gen(function* () {
124 // Policies hang off CAs: enumerate every non-deleted CA and
125 // collect the ones that actually carry a policy.
126 const cas = yield* acmpca.listCertificateAuthorities.items({}).pipe(
127 Stream.runCollect,
128 Effect.map((chunk) =>
129 Array.from(chunk)
130 .filter((ca) => ca.Arn !== undefined && ca.Status !== "DELETED")
131 .map((ca) => ca.Arn!),
132 ),
133 );
134 const policies = yield* Effect.forEach(
135 cas,
136 (arn) =>
137 observe(arn).pipe(
138 Effect.map((policy) =>
139 policy === undefined ? [] : [{ certificateAuthorityArn: arn, policy }],
140 ),
141 ),
142 { concurrency: 5 },
143 );
144 return policies.flat();
145 }),
146 reconcile: Effect.fn(function* ({ news, session }) {
147 const certificateAuthorityArn = news.certificateAuthorityArn;
148 const desired = toDesired(news.policy);
149
150 // OBSERVE — putPolicy is an upsert; compare the canonicalized
151 // live document against the canonicalized desired one so a
152 // re-deploy of an equivalent policy (key order, whitespace) skips
153 // the API call entirely.
154 const observed = yield* observe(certificateAuthorityArn);
155 if (
156 observed === undefined ||
157 normalizePolicyDocument(observed) !== normalizePolicyDocument(desired)
158 ) {
159 yield* acmpca.putPolicy({
160 ResourceArn: certificateAuthorityArn,
161 Policy: desired,
162 });
163 }
164
165 yield* session.note(certificateAuthorityArn);
166 return { certificateAuthorityArn, policy: desired };
167 }),
168 delete: Effect.fn(function* ({ output }) {
169 yield* acmpca.deletePolicy({ ResourceArn: output.certificateAuthorityArn }).pipe(
170 // CA already gone, no policy attached, or CA in a state
171 // (DELETED) where the policy is no longer addressable — the
172 // policy is gone with it.
173 Effect.catchTag(
174 ["ResourceNotFoundException", "InvalidStateException"],
175 () => Effect.void,
176 ),
177 );
178 }),
179 };
180 }),
181 );