The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { Policy, PolicyStore, Schema } from "@alchemy.run/aws/VerifiedPermissions"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as avp from "@distilled.cloud/aws/verifiedpermissions"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Redacted from "effect/Redacted"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const unwrap = (v: string | Redacted.Redacted<string> | undefined) => |
| 12 | v === undefined ? undefined : Redacted.isRedacted(v) ? Redacted.value(v) : v; |
| 13 | |
| 14 | const { test } = Test.make({ providers: AWS.providers() }); |
| 15 | |
| 16 | const findStore = (policyStoreId: string) => |
| 17 | avp |
| 18 | .getPolicyStore({ policyStoreId, tags: true }) |
| 19 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined))); |
| 20 | |
| 21 | const findPolicy = (policyStoreId: string, policyId: string) => |
| 22 | avp |
| 23 | .getPolicy({ policyStoreId, policyId }) |
| 24 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined))); |
| 25 | |
| 26 | const findSchema = (policyStoreId: string) => |
| 27 | avp |
| 28 | .getSchema({ policyStoreId }) |
| 29 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined))); |
| 30 | |
| 31 | class StoreStillExists extends Data.TaggedError("StoreStillExists")<{ |
| 32 | readonly policyStoreId: string; |
| 33 | }> {} |
| 34 | |
| 35 | const assertStoreDeleted = (policyStoreId: string) => |
| 36 | findStore(policyStoreId).pipe( |
| 37 | Effect.flatMap((store) => |
| 38 | store === undefined ? Effect.void : Effect.fail(new StoreStillExists({ policyStoreId })), |
| 39 | ), |
| 40 | Effect.retry({ |
| 41 | while: (e) => e._tag === "StoreStillExists", |
| 42 | schedule: Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(15)]), |
| 43 | }), |
| 44 | ); |
| 45 | |
| 46 | const cedarSchema = JSON.stringify({ |
| 47 | PhotoApp: { |
| 48 | entityTypes: { |
| 49 | User: {}, |
| 50 | Photo: {}, |
| 51 | }, |
| 52 | actions: { |
| 53 | viewPhoto: { |
| 54 | appliesTo: { |
| 55 | principalTypes: ["User"], |
| 56 | resourceTypes: ["Photo"], |
| 57 | }, |
| 58 | }, |
| 59 | }, |
| 60 | }, |
| 61 | }); |
| 62 | |
| 63 | test.provider( |
| 64 | "policy store lifecycle: create store + schema + policy, update validation mode + policy statement, destroy", |
| 65 | (stack) => |
| 66 | Effect.gen(function* () { |
| 67 | yield* stack.destroy(); |
| 68 | |
| 69 | // AVP forbids changing a static policy's principal/resource SCOPE on |
| 70 | // update — only the description and conditions may change. Keep the |
| 71 | // scope fixed and vary the policy description across deploys. |
| 72 | const statement = `permit( |
| 73 | principal == PhotoApp::User::"alice", |
| 74 | action == PhotoApp::Action::"viewPhoto", |
| 75 | resource |
| 76 | );`; |
| 77 | |
| 78 | const makeStack = ( |
| 79 | validationMode: "OFF" | "STRICT", |
| 80 | description: string, |
| 81 | policyDescription: string, |
| 82 | ) => |
| 83 | Effect.gen(function* () { |
| 84 | const store = yield* PolicyStore("Store", { |
| 85 | validationMode, |
| 86 | description, |
| 87 | deletionProtection: "DISABLED", |
| 88 | tags: { Environment: "test" }, |
| 89 | }); |
| 90 | const schema = yield* Schema("Schema", { |
| 91 | policyStoreId: store.policyStoreId, |
| 92 | cedarJson: cedarSchema, |
| 93 | }); |
| 94 | const policy = yield* Policy("AllowUser", { |
| 95 | // depend on the schema so, under STRICT validation, the policy is |
| 96 | // created after PutSchema — Cedar validates the statement against |
| 97 | // the schema at CreatePolicy time |
| 98 | policyStoreId: schema.policyStoreId, |
| 99 | statement, |
| 100 | description: policyDescription, |
| 101 | }); |
| 102 | return { store, schema, policy }; |
| 103 | }); |
| 104 | |
| 105 | // create: STRICT validation requires the schema to be present |
| 106 | const { store, policy } = yield* stack.deploy( |
| 107 | makeStack("STRICT", "photo app", "initial policy"), |
| 108 | ); |
| 109 | expect(store.policyStoreId).toBeDefined(); |
| 110 | expect(store.policyStoreArn).toContain(":policy-store/"); |
| 111 | expect(policy.policyId).toBeDefined(); |
| 112 | expect(policy.policyStoreId).toBe(store.policyStoreId); |
| 113 | |
| 114 | // out-of-band verify |
| 115 | const created = yield* findStore(store.policyStoreId); |
| 116 | expect(created?.validationSettings.mode).toBe("STRICT"); |
| 117 | expect(unwrap(created?.description)).toBe("photo app"); |
| 118 | expect(created?.tags?.Environment).toBe("test"); |
| 119 | expect(created?.tags?.["alchemy::id"]).toBe("Store"); |
| 120 | |
| 121 | const createdSchema = yield* findSchema(store.policyStoreId); |
| 122 | expect(unwrap(createdSchema?.schema)).toContain("PhotoApp"); |
| 123 | |
| 124 | const createdPolicy = yield* findPolicy(store.policyStoreId, policy.policyId); |
| 125 | expect(createdPolicy?.policyType).toBe("STATIC"); |
| 126 | |
| 127 | // update: store validation mode OFF + description, and the policy's |
| 128 | // description in place (policyId is stable) |
| 129 | const updated = yield* stack.deploy(makeStack("OFF", "updated app", "updated policy")); |
| 130 | expect(updated.store.policyStoreId).toBe(store.policyStoreId); |
| 131 | expect(updated.policy.policyId).toBe(policy.policyId); |
| 132 | |
| 133 | const afterUpdate = yield* findStore(store.policyStoreId); |
| 134 | expect(afterUpdate?.validationSettings.mode).toBe("OFF"); |
| 135 | expect(unwrap(afterUpdate?.description)).toBe("updated app"); |
| 136 | |
| 137 | // destroy |
| 138 | yield* stack.destroy(); |
| 139 | yield* assertStoreDeleted(store.policyStoreId); |
| 140 | }), |
| 141 | { |
| 142 | tags: ["provider:aws", "provider:aws:verifiedpermissions", "live"], |
| 143 | timeout: 180_000, |
| 144 | }, |
| 145 | ); |