@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, 04:45 UTC

main
ts 152 lines 5.47 KB
Raw
1 import * as s3control from "@distilled.cloud/aws/s3-control";
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 { AWSEnvironment } from "../Environment.ts";
7 import {
8 normalizePolicyDocument,
9 type PolicyDocument,
10 stringifyPolicyDocument,
11 } from "../IAM/Policy.ts";
12 import type { Providers } from "../Providers.ts";
13
14 export interface AccessPointPolicyProps {
15 /**
16 * Name of the access point the policy is attached to.
17 *
18 * Changing the access point replaces the policy.
19 */
20 accessPointName: string;
21 /**
22 * The resource policy granting access through the access point, as a
23 * typed {@link PolicyDocument} or a raw JSON string (escape hatch /
24 * adoption). Object-level actions target `${accessPointArn}/object/${key}`.
25 */
26 policy: PolicyDocument | string;
27 }
28
29 export interface AccessPointPolicy extends Resource<
30 "AWS.S3Control.AccessPointPolicy",
31 AccessPointPolicyProps,
32 {
33 /**
34 * Name of the access point the policy is attached to.
35 */
36 accessPointName: string;
37 },
38 never,
39 Providers
40 > {}
41
42 /**
43 * The resource policy of an S3 Access Point. Grants principals access to
44 * objects through the access point — the delegated replacement for a giant
45 * shared bucket policy.
46 *
47 * Note that the underlying bucket must delegate access control to the access
48 * point (or the principals must also be allowed by the bucket policy).
49 * ### Attaching a Policy
50 * **Example:** Allow a role to read objects through the access point
51 * ```typescript
52 * import * as S3Control from "@alchemy.run/aws/S3Control";
53 *
54 * const accessPoint = yield* S3Control.AccessPoint("data-ap", {
55 * bucket: bucket.bucketName,
56 * });
57 *
58 * yield* S3Control.AccessPointPolicy("data-ap-policy", {
59 * accessPointName: accessPoint.accessPointName,
60 * policy: {
61 * Version: "2012-10-17",
62 * Statement: [
63 * {
64 * Effect: "Allow",
65 * Principal: { AWS: `arn:aws:iam::${accountId}:role/reader` },
66 * Action: ["s3:GetObject"],
67 * Resource: [Output.interpolate`${accessPoint.accessPointArn}/object/*`],
68 * },
69 * ],
70 * },
71 * });
72 * ```
73 *
74 * @resource
75 */
76 export const AccessPointPolicy = Resource<AccessPointPolicy>("AWS.S3Control.AccessPointPolicy");
77
78 export const AccessPointPolicyProvider = () =>
79 Provider.effect(
80 AccessPointPolicy,
81 Effect.gen(function* () {
82 const observePolicy = (accountId: string, name: string) =>
83 s3control.getAccessPointPolicy({ AccountId: accountId, Name: name }).pipe(
84 Effect.map((r) => r.Policy),
85 Effect.catchTag(["NoSuchAccessPointPolicy", "NoSuchAccessPoint"], () => Effect.undefined),
86 );
87
88 const desiredPolicy = (props: AccessPointPolicyProps) =>
89 typeof props.policy === "string" ? props.policy : stringifyPolicyDocument(props.policy);
90
91 return AccessPointPolicy.Provider.of({
92 stables: ["accessPointName"],
93 // Sub-resource keyed by its access point — there is no account-wide
94 // enumeration of access point policies, and the policy is deleted
95 // with its access point, so nuke discovers it via the parent.
96 list: () => Effect.succeed([]),
97 read: Effect.fn(function* ({ olds, output }) {
98 const { accountId } = yield* AWSEnvironment.current;
99 const name = output?.accessPointName ?? olds?.accessPointName;
100 if (name === undefined) return undefined;
101 const policy = yield* observePolicy(accountId, name);
102 if (policy === undefined) return undefined;
103 return { accessPointName: name };
104 }),
105 diff: Effect.fn(function* ({ news, olds }) {
106 if (!isResolved(news)) return undefined;
107 if (olds !== undefined && olds.accessPointName !== news.accessPointName) {
108 return { action: "replace" } as const;
109 }
110 // fall through: engine default update path (policy content)
111 }),
112 reconcile: Effect.fn(function* ({ news, session }) {
113 const { accountId } = yield* AWSEnvironment.current;
114 const name = news.accessPointName;
115
116 // 1. OBSERVE — read the policy currently attached in the cloud.
117 const observed = yield* observePolicy(accountId, name);
118
119 // 2. SYNC — apply only when the desired document differs after
120 // canonicalization (key order / whitespace insensitive), so a
121 // re-deploy of an unchanged document is a no-op.
122 const desired = desiredPolicy(news);
123 if (
124 observed === undefined ||
125 normalizePolicyDocument(observed) !== normalizePolicyDocument(desired)
126 ) {
127 yield* s3control.putAccessPointPolicy({
128 AccountId: accountId,
129 Name: name,
130 Policy: desired,
131 });
132 }
133
134 yield* session.note(name);
135 return { accessPointName: name };
136 }),
137 delete: Effect.fn(function* ({ output }) {
138 const { accountId } = yield* AWSEnvironment.current;
139 yield* s3control
140 .deleteAccessPointPolicy({
141 AccountId: accountId,
142 Name: output.accessPointName,
143 })
144 .pipe(
145 // Idempotent delete — a policy (or its whole access point)
146 // that is already gone is success.
147 Effect.catchTag(["NoSuchAccessPointPolicy", "NoSuchAccessPoint"], () => Effect.void),
148 );
149 }),
150 });
151 }),
152 );