@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 223 lines 7.73 KB
Raw
1 import * as avp from "@distilled.cloud/aws/verifiedpermissions";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import { createInternalTags, diffTags, hasAlchemyTags } from "alchemy/Tags";
7 import * as Effect from "effect/Effect";
8 import * as Redacted from "effect/Redacted";
9 import * as Stream from "effect/Stream";
10 import type { Providers } from "../Providers.ts";
11
12 /**
13 * Schema-validation strictness applied to Cedar policies and templates
14 * submitted to the store.
15 *
16 * - `OFF` — no schema validation.
17 * - `STRICT` — policies must reference entity types and actions declared in
18 * the store's schema (requires a schema to be present).
19 */
20 export type ValidationMode = "OFF" | "STRICT";
21
22 export interface PolicyStoreProps {
23 /**
24 * The schema-validation mode for the policy store.
25 * @default "OFF"
26 */
27 validationMode?: ValidationMode;
28 /**
29 * A human-readable description for the policy store.
30 */
31 description?: string;
32 /**
33 * When `ENABLED`, the store cannot be deleted until protection is disabled.
34 * Leave `DISABLED` for ephemeral / test stores.
35 * @default "DISABLED"
36 */
37 deletionProtection?: "ENABLED" | "DISABLED";
38 /**
39 * Tags to apply to the policy store. Merged with internal Alchemy tags.
40 */
41 tags?: Record<string, string>;
42 }
43
44 export interface PolicyStore extends Resource<
45 "AWS.VerifiedPermissions.PolicyStore",
46 PolicyStoreProps,
47 {
48 /**
49 * Service-assigned unique ID of the policy store — pass it to
50 * {@link Policy}, {@link Schema}, and `IsAuthorized`.
51 */
52 policyStoreId: string;
53 /**
54 * ARN of the policy store.
55 */
56 policyStoreArn: string;
57 },
58 {},
59 Providers
60 > {}
61
62 /**
63 * An Amazon Verified Permissions policy store — the container for Cedar
64 * policies, policy templates, and a schema. Authorization requests
65 * (`IsAuthorized`) are evaluated against all policies in a store.
66 * ### Creating a Policy Store
67 * **Example:** Basic Policy Store
68 * ```typescript
69 * import * as AWS from "@alchemy.run/aws";
70 *
71 * const store = yield* AWS.VerifiedPermissions.PolicyStore("Store", {
72 * validationMode: "OFF",
73 * });
74 * ```
75 *
76 * **Example:** Strict Validation with a Schema
77 * ```typescript
78 * const store = yield* AWS.VerifiedPermissions.PolicyStore("Store", {
79 * validationMode: "STRICT",
80 * description: "Photo app authorization",
81 * });
82 *
83 * yield* AWS.VerifiedPermissions.Schema("Schema", {
84 * policyStoreId: store.policyStoreId,
85 * cedarJson: JSON.stringify({
86 * PhotoApp: {
87 * entityTypes: { User: {}, Photo: {} },
88 * actions: { viewPhoto: { appliesTo: { principalTypes: ["User"], resourceTypes: ["Photo"] } } },
89 * },
90 * }),
91 * });
92 * ```
93 *
94 * @resource
95 */
96 export const PolicyStore = Resource<PolicyStore>("AWS.VerifiedPermissions.PolicyStore");
97
98 export const PolicyStoreProvider = () =>
99 Provider.effect(
100 PolicyStore,
101 Effect.gen(function* () {
102 const observe = Effect.fn(function* (policyStoreId: string) {
103 return yield* avp
104 .getPolicyStore({ policyStoreId, tags: true })
105 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
106 });
107
108 return PolicyStore.Provider.of({
109 stables: ["policyStoreId", "policyStoreArn"],
110
111 list: () =>
112 Effect.gen(function* () {
113 const pages = yield* avp.listPolicyStores.pages({}).pipe(Stream.runCollect);
114 const items = Array.from(pages).flatMap((page) => page.policyStores ?? []);
115 return items.map((item) => ({
116 policyStoreId: item.policyStoreId,
117 policyStoreArn: item.arn,
118 }));
119 }),
120
121 read: Effect.fn(function* ({ id, output }) {
122 if (output?.policyStoreId === undefined) return undefined;
123 const store = yield* observe(output.policyStoreId);
124 if (store === undefined) return undefined;
125 const attrs = {
126 policyStoreId: store.policyStoreId,
127 policyStoreArn: store.arn,
128 };
129 const tags = (store.tags ?? {}) as Record<string, string>;
130 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
131 }),
132
133 // no props trigger replacement — validationMode / description /
134 // deletionProtection are all mutable via updatePolicyStore
135 diff: Effect.fn(function* ({ news }) {
136 if (!isResolved(news)) return undefined;
137 return undefined;
138 }),
139
140 reconcile: Effect.fn(function* ({ id, news, output, session }) {
141 const internalTags = yield* createInternalTags(id);
142 const desiredTags = { ...news.tags, ...internalTags };
143 const validationMode = news.validationMode ?? "OFF";
144
145 // 1. OBSERVE — cloud state is authoritative
146 let store =
147 output?.policyStoreId !== undefined ? yield* observe(output.policyStoreId) : undefined;
148
149 // 2. ENSURE — createPolicyStore returns id + arn directly
150 if (store === undefined) {
151 const created = yield* avp.createPolicyStore({
152 validationSettings: { mode: validationMode },
153 description: news.description,
154 deletionProtection: news.deletionProtection,
155 tags: desiredTags,
156 });
157 store = yield* observe(created.policyStoreId);
158 if (store === undefined) {
159 return {
160 policyStoreId: created.policyStoreId,
161 policyStoreArn: created.arn,
162 };
163 }
164 }
165
166 // 3. SYNC — validation / description / deletion protection
167 const observedMode = store.validationSettings.mode;
168 // description decodes as a Redacted (SensitiveString) — unwrap to
169 // compare against the desired plain-string value
170 const observedDescription =
171 store.description === undefined
172 ? undefined
173 : Redacted.isRedacted(store.description)
174 ? Redacted.value(store.description)
175 : store.description;
176 const observedProtection = store.deletionProtection ?? "DISABLED";
177 const desiredProtection = news.deletionProtection ?? "DISABLED";
178 if (
179 observedMode !== validationMode ||
180 observedDescription !== (news.description ?? undefined) ||
181 observedProtection !== desiredProtection
182 ) {
183 yield* avp.updatePolicyStore({
184 policyStoreId: store.policyStoreId,
185 validationSettings: { mode: validationMode },
186 description: news.description,
187 deletionProtection: news.deletionProtection,
188 });
189 }
190
191 // 3b. SYNC TAGS — diff against OBSERVED cloud tags
192 const observedTags = (store.tags ?? {}) as Record<string, string>;
193 const { upsert, removed } = diffTags(observedTags, desiredTags);
194 if (upsert.length > 0) {
195 yield* avp.tagResource({
196 resourceArn: store.arn,
197 tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])),
198 });
199 }
200 if (removed.length > 0) {
201 yield* avp.untagResource({
202 resourceArn: store.arn,
203 tagKeys: removed,
204 });
205 }
206
207 yield* session.note(store.policyStoreId);
208 return {
209 policyStoreId: store.policyStoreId,
210 policyStoreArn: store.arn,
211 };
212 }),
213
214 delete: Effect.fn(function* ({ output }) {
215 // deletePolicyStore is idempotent — returns 200 even if the store
216 // does not exist
217 yield* avp.deletePolicyStore({
218 policyStoreId: output.policyStoreId,
219 });
220 }),
221 });
222 }),
223 );