@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 174 lines 6.15 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 Effect from "effect/Effect";
6 import * as Redacted from "effect/Redacted";
7 import type { Providers } from "../Providers.ts";
8
9 const unwrap = (v: string | Redacted.Redacted<string> | undefined) =>
10 v === undefined ? undefined : Redacted.isRedacted(v) ? Redacted.value(v) : v;
11
12 export interface PolicyTemplateProps {
13 /**
14 * The ID of the policy store the template belongs to. Changing the store
15 * replaces the template.
16 */
17 policyStoreId: string;
18 /**
19 * The Cedar policy-template statement. Templates use the `?principal` and
20 * `?resource` placeholders which are filled in when a template-linked
21 * policy is created, e.g.
22 * `permit(principal == ?principal, action == PhotoApp::Action::"viewPhoto", resource);`.
23 * Mutable — updating re-submits the statement via `UpdatePolicyTemplate`.
24 */
25 statement: string;
26 /**
27 * An optional description stored alongside the template statement.
28 */
29 description?: string;
30 }
31
32 export interface PolicyTemplate extends Resource<
33 "AWS.VerifiedPermissions.PolicyTemplate",
34 PolicyTemplateProps,
35 {
36 /**
37 * ID of the policy store the template belongs to.
38 */
39 policyStoreId: string;
40 /**
41 * Service-assigned unique ID of the policy template within the store —
42 * pass it as the `templateId` of a template-linked {@link Policy}.
43 */
44 policyTemplateId: string;
45 },
46 {},
47 Providers
48 > {}
49
50 /**
51 * A Cedar policy template in a Verified Permissions policy store. Templates
52 * contain `?principal` / `?resource` placeholders; template-linked policies
53 * instantiate the template for a concrete principal and resource, and every
54 * linked policy automatically picks up template updates.
55 * ### Creating Policy Templates
56 * **Example:** Template with a Principal Placeholder
57 * ```typescript
58 * import * as AWS from "@alchemy.run/aws";
59 *
60 * const store = yield* AWS.VerifiedPermissions.PolicyStore("Store", {});
61 *
62 * const template = yield* AWS.VerifiedPermissions.PolicyTemplate("ViewPhoto", {
63 * policyStoreId: store.policyStoreId,
64 * statement: `permit(
65 * principal == ?principal,
66 * action == PhotoApp::Action::"viewPhoto",
67 * resource
68 * );`,
69 * description: "Grant a user access to view photos",
70 * });
71 * ```
72 *
73 * **Example:** Link a Policy to the Template
74 * ```typescript
75 * yield* AWS.VerifiedPermissions.Policy("AliceCanView", {
76 * policyStoreId: store.policyStoreId,
77 * templateId: template.policyTemplateId,
78 * principal: { entityType: "PhotoApp::User", entityId: "alice" },
79 * });
80 * ```
81 *
82 * @resource
83 */
84 export const PolicyTemplate = Resource<PolicyTemplate>("AWS.VerifiedPermissions.PolicyTemplate");
85
86 export const PolicyTemplateProvider = () =>
87 Provider.effect(
88 PolicyTemplate,
89 Effect.gen(function* () {
90 const observe = Effect.fn(function* (policyStoreId: string, policyTemplateId: string) {
91 return yield* avp
92 .getPolicyTemplate({ policyStoreId, policyTemplateId })
93 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
94 });
95
96 return PolicyTemplate.Provider.of({
97 stables: ["policyStoreId", "policyTemplateId"],
98
99 // child of a policy store — not enumerable account-wide
100 list: () => Effect.succeed([]),
101
102 read: Effect.fn(function* ({ olds, output }) {
103 const policyStoreId = output?.policyStoreId ?? olds?.policyStoreId;
104 const policyTemplateId = output?.policyTemplateId;
105 if (policyStoreId === undefined || policyTemplateId === undefined) {
106 return undefined;
107 }
108 const template = yield* observe(policyStoreId, policyTemplateId);
109 if (template === undefined) return undefined;
110 return { policyStoreId, policyTemplateId: template.policyTemplateId };
111 }),
112
113 diff: Effect.fn(function* ({ news, olds }) {
114 if (!isResolved(news)) return undefined;
115 if (olds.policyStoreId !== news.policyStoreId) {
116 return { action: "replace" } as const;
117 }
118 // statement / description are mutable → default update path
119 }),
120
121 reconcile: Effect.fn(function* ({ news, output, session }) {
122 // 1. OBSERVE — cloud state is authoritative
123 const existing =
124 output?.policyTemplateId !== undefined
125 ? yield* observe(news.policyStoreId, output.policyTemplateId)
126 : undefined;
127
128 // 2. ENSURE
129 if (existing === undefined) {
130 const created = yield* avp.createPolicyTemplate({
131 policyStoreId: news.policyStoreId,
132 statement: news.statement,
133 description: news.description,
134 });
135 yield* session.note(created.policyTemplateId);
136 return {
137 policyStoreId: news.policyStoreId,
138 policyTemplateId: created.policyTemplateId,
139 };
140 }
141
142 // 3. SYNC — diff observed statement/description against desired
143 const observedStatement = unwrap(existing.statement);
144 const observedDescription = unwrap(existing.description);
145 if (
146 observedStatement !== news.statement ||
147 observedDescription !== (news.description ?? undefined)
148 ) {
149 yield* avp.updatePolicyTemplate({
150 policyStoreId: news.policyStoreId,
151 policyTemplateId: existing.policyTemplateId,
152 statement: news.statement,
153 description: news.description,
154 });
155 }
156
157 yield* session.note(existing.policyTemplateId);
158 return {
159 policyStoreId: news.policyStoreId,
160 policyTemplateId: existing.policyTemplateId,
161 };
162 }),
163
164 delete: Effect.fn(function* ({ output }) {
165 yield* avp
166 .deletePolicyTemplate({
167 policyStoreId: output.policyStoreId,
168 policyTemplateId: output.policyTemplateId,
169 })
170 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
171 }),
172 });
173 }),
174 );