@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 249 lines 8.38 KB
Raw
1 import * as avp from "@distilled.cloud/aws/verifiedpermissions";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import type { Providers } from "../Providers.ts";
8
9 export class VerifiedPermissionsPolicyInvalidProps extends Data.TaggedError(
10 "VerifiedPermissionsPolicyInvalidProps",
11 )<{
12 readonly message: string;
13 readonly cause?: unknown;
14 }> {}
15
16 /**
17 * Identifies a Cedar entity, e.g.
18 * `{ entityType: "PhotoApp::User", entityId: "alice" }`.
19 */
20 export interface EntityIdentifier {
21 /** The Cedar entity type, e.g. `PhotoApp::User`. */
22 entityType: string;
23 /** The entity ID, e.g. `alice`. */
24 entityId: string;
25 }
26
27 export interface PolicyProps {
28 /**
29 * The ID of the policy store the policy belongs to. Changing the store
30 * replaces the policy.
31 */
32 policyStoreId: string;
33 /**
34 * The Cedar policy statement for a **static** policy, e.g.
35 * `permit(principal, action == Action::"view", resource);`. Mutable —
36 * updating re-submits the statement via `UpdatePolicy` (only annotations
37 * and conditions may change; the principal/resource scope is fixed).
38 *
39 * Exactly one of `statement` (static) or `templateId` (template-linked)
40 * must be provided.
41 */
42 statement?: string;
43 /**
44 * An optional description stored alongside a static policy statement.
45 */
46 description?: string;
47 /**
48 * The ID of a {@link PolicyTemplate} to instantiate as a
49 * **template-linked** policy. Template-linked policies cannot be updated
50 * in place — changing `templateId`, `principal`, or `resource` replaces
51 * the policy (the template itself is the mutable part).
52 */
53 templateId?: string;
54 /**
55 * The principal to fill into the template's `?principal` placeholder.
56 * Only valid with `templateId`.
57 */
58 principal?: EntityIdentifier;
59 /**
60 * The resource to fill into the template's `?resource` placeholder.
61 * Only valid with `templateId`.
62 */
63 resource?: EntityIdentifier;
64 }
65
66 export interface Policy extends Resource<
67 "AWS.VerifiedPermissions.Policy",
68 PolicyProps,
69 {
70 /**
71 * ID of the policy store the policy belongs to.
72 */
73 policyStoreId: string;
74 /**
75 * Service-assigned unique ID of the policy within the store.
76 */
77 policyId: string;
78 },
79 {},
80 Providers
81 > {}
82
83 /**
84 * A static Cedar policy in a Verified Permissions policy store. Static
85 * policies contain a complete Cedar statement and are evaluated for every
86 * matching authorization request.
87 * ### Creating Policies
88 * **Example:** Permit a Specific Principal
89 * ```typescript
90 * import * as AWS from "@alchemy.run/aws";
91 *
92 * const store = yield* AWS.VerifiedPermissions.PolicyStore("Store", {});
93 *
94 * yield* AWS.VerifiedPermissions.Policy("AllowAlice", {
95 * policyStoreId: store.policyStoreId,
96 * statement: `permit(
97 * principal == PhotoApp::User::"alice",
98 * action == PhotoApp::Action::"viewPhoto",
99 * resource
100 * );`,
101 * description: "Alice can view any photo",
102 * });
103 * ```
104 *
105 * ### Template-Linked Policies
106 * **Example:** Instantiate a Policy Template for a Principal
107 * ```typescript
108 * const template = yield* AWS.VerifiedPermissions.PolicyTemplate("ViewPhoto", {
109 * policyStoreId: store.policyStoreId,
110 * statement: `permit(
111 * principal == ?principal,
112 * action == PhotoApp::Action::"viewPhoto",
113 * resource
114 * );`,
115 * });
116 *
117 * yield* AWS.VerifiedPermissions.Policy("AliceCanView", {
118 * policyStoreId: store.policyStoreId,
119 * templateId: template.policyTemplateId,
120 * principal: { entityType: "PhotoApp::User", entityId: "alice" },
121 * });
122 * ```
123 *
124 * @resource
125 */
126 export const Policy = Resource<Policy>("AWS.VerifiedPermissions.Policy");
127
128 /** Desired props → the wire `PolicyDefinition` union. */
129 const toDefinition = (news: PolicyProps) =>
130 Effect.gen(function* () {
131 if (news.templateId !== undefined && news.statement !== undefined) {
132 return yield* new VerifiedPermissionsPolicyInvalidProps({
133 message:
134 "a Policy accepts either `statement` (static) or `templateId` (template-linked), not both",
135 });
136 }
137 if (news.templateId !== undefined) {
138 return {
139 templateLinked: {
140 policyTemplateId: news.templateId,
141 principal: news.principal,
142 resource: news.resource,
143 },
144 } as const;
145 }
146 if (news.statement === undefined) {
147 return yield* new VerifiedPermissionsPolicyInvalidProps({
148 message: "a Policy requires either `statement` (static) or `templateId` (template-linked)",
149 });
150 }
151 return {
152 static: { statement: news.statement, description: news.description },
153 } as const;
154 });
155
156 export const PolicyProvider = () =>
157 Provider.effect(
158 Policy,
159 Effect.gen(function* () {
160 const observe = Effect.fn(function* (policyStoreId: string, policyId: string) {
161 return yield* avp
162 .getPolicy({ policyStoreId, policyId })
163 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
164 });
165
166 return Policy.Provider.of({
167 stables: ["policyStoreId", "policyId"],
168
169 // child of a policy store — not enumerable account-wide
170 list: () => Effect.succeed([]),
171
172 read: Effect.fn(function* ({ olds, output }) {
173 const policyStoreId = output?.policyStoreId ?? olds?.policyStoreId;
174 const policyId = output?.policyId;
175 if (policyStoreId === undefined || policyId === undefined) {
176 return undefined;
177 }
178 const policy = yield* observe(policyStoreId, policyId);
179 if (policy === undefined) return undefined;
180 return { policyStoreId, policyId: policy.policyId };
181 }),
182
183 diff: Effect.fn(function* ({ news, olds }) {
184 if (!isResolved(news)) return undefined;
185 if (olds.policyStoreId !== news.policyStoreId) {
186 return { action: "replace" } as const;
187 }
188 // switching static <-> template-linked replaces; UpdatePolicy only
189 // supports static definitions, and a template-linked policy's
190 // template/principal/resource are fixed at creation
191 if (
192 (olds.templateId === undefined) !== (news.templateId === undefined) ||
193 (news.templateId !== undefined &&
194 (olds.templateId !== news.templateId ||
195 olds.principal?.entityType !== news.principal?.entityType ||
196 olds.principal?.entityId !== news.principal?.entityId ||
197 olds.resource?.entityType !== news.resource?.entityType ||
198 olds.resource?.entityId !== news.resource?.entityId))
199 ) {
200 return { action: "replace" } as const;
201 }
202 // static statement / description are mutable → default update path
203 }),
204
205 reconcile: Effect.fn(function* ({ news, output, session }) {
206 const definition = yield* toDefinition(news);
207
208 // 1. OBSERVE — cloud state is authoritative
209 const existing =
210 output?.policyId !== undefined
211 ? yield* observe(news.policyStoreId, output.policyId)
212 : undefined;
213
214 // 2. ENSURE / SYNC
215 let policyId: string;
216 if (existing === undefined) {
217 const created = yield* avp.createPolicy({
218 policyStoreId: news.policyStoreId,
219 definition,
220 });
221 policyId = created.policyId;
222 } else if ("static" in definition && definition.static !== undefined) {
223 const updated = yield* avp.updatePolicy({
224 policyStoreId: news.policyStoreId,
225 policyId: existing.policyId,
226 definition: { static: definition.static },
227 });
228 policyId = updated.policyId;
229 } else {
230 // template-linked policies have no in-place update — diff replaces
231 // on any change, so the observed policy is already converged
232 policyId = existing.policyId;
233 }
234
235 yield* session.note(policyId);
236 return { policyStoreId: news.policyStoreId, policyId };
237 }),
238
239 delete: Effect.fn(function* ({ output }) {
240 yield* avp
241 .deletePolicy({
242 policyStoreId: output.policyStoreId,
243 policyId: output.policyId,
244 })
245 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
246 }),
247 });
248 }),
249 );