The best way to control Claude Code, Codex and more from any computer or phone

main
ts 123 lines 3.8 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 type { Providers } from "../Providers.ts";
7
8 export interface SchemaProps {
9 /**
10 * The ID of the policy store the schema belongs to. A store has at most one
11 * schema. Changing the store replaces the schema.
12 */
13 policyStoreId: string;
14 /**
15 * The Cedar JSON schema document (as a JSON string). Defines the entity
16 * types, actions, and the relationships between them used to validate
17 * policies when the store's validation mode is `STRICT`. Mutable — updating
18 * re-submits the schema via `PutSchema`.
19 */
20 cedarJson: string;
21 }
22
23 export interface Schema extends Resource<
24 "AWS.VerifiedPermissions.Schema",
25 SchemaProps,
26 {
27 /**
28 * ID of the policy store the schema belongs to.
29 */
30 policyStoreId: string;
31 },
32 {},
33 Providers
34 > {}
35
36 /**
37 * The Cedar schema for a Verified Permissions policy store. The schema
38 * declares the entity types and actions your policies reference; with
39 * `validationMode: "STRICT"` on the store, policies and templates are
40 * validated against it at submission time.
41 *
42 * A policy store has at most one schema — `PutSchema` is an upsert that fully
43 * replaces the previous schema.
44 * ### Defining a Schema
45 * **Example:** Photo App Schema
46 * ```typescript
47 * import * as AWS from "@alchemy.run/aws";
48 *
49 * const store = yield* AWS.VerifiedPermissions.PolicyStore("Store", {
50 * validationMode: "STRICT",
51 * });
52 *
53 * yield* AWS.VerifiedPermissions.Schema("Schema", {
54 * policyStoreId: store.policyStoreId,
55 * cedarJson: JSON.stringify({
56 * PhotoApp: {
57 * entityTypes: {
58 * User: {},
59 * Photo: {},
60 * },
61 * actions: {
62 * viewPhoto: {
63 * appliesTo: {
64 * principalTypes: ["User"],
65 * resourceTypes: ["Photo"],
66 * },
67 * },
68 * },
69 * },
70 * }),
71 * });
72 * ```
73 *
74 * @resource
75 */
76 export const Schema = Resource<Schema>("AWS.VerifiedPermissions.Schema");
77
78 export const SchemaProvider = () =>
79 Provider.effect(
80 Schema,
81 Effect.gen(function* () {
82 return Schema.Provider.of({
83 stables: ["policyStoreId"],
84
85 // singleton child of a policy store — not enumerable account-wide
86 list: () => Effect.succeed([]),
87
88 read: Effect.fn(function* ({ olds, output }) {
89 const policyStoreId = output?.policyStoreId ?? olds?.policyStoreId;
90 if (policyStoreId === undefined) return undefined;
91 const schema = yield* avp
92 .getSchema({ policyStoreId })
93 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
94 if (schema === undefined) return undefined;
95 return { policyStoreId };
96 }),
97
98 diff: Effect.fn(function* ({ news, olds }) {
99 if (!isResolved(news)) return undefined;
100 if (olds.policyStoreId !== news.policyStoreId) {
101 return { action: "replace" } as const;
102 }
103 // cedarJson is mutable → default update path
104 }),
105
106 reconcile: Effect.fn(function* ({ news, session }) {
107 // PutSchema is a full upsert — idempotent for both create and update
108 yield* avp.putSchema({
109 policyStoreId: news.policyStoreId,
110 definition: { cedarJson: news.cedarJson },
111 });
112 yield* session.note(news.policyStoreId);
113 return { policyStoreId: news.policyStoreId };
114 }),
115
116 delete: Effect.fn(function* () {
117 // Verified Permissions has no DeleteSchema API — the schema is
118 // removed when the policy store is deleted. Nothing to do.
119 yield* Effect.void;
120 }),
121 });
122 }),
123 );