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, PolicyTemplate } 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 Effect from "effect/Effect"; |
| 7 | import * as Redacted from "effect/Redacted"; |
| 8 | |
| 9 | const unwrap = (v: string | Redacted.Redacted<string> | undefined) => |
| 10 | v === undefined ? undefined : Redacted.isRedacted(v) ? Redacted.value(v) : v; |
| 11 | |
| 12 | const { test } = Test.make({ providers: AWS.providers() }); |
| 13 | |
| 14 | const findTemplate = (policyStoreId: string, policyTemplateId: string) => |
| 15 | avp |
| 16 | .getPolicyTemplate({ policyStoreId, policyTemplateId }) |
| 17 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined))); |
| 18 | |
| 19 | const findPolicy = (policyStoreId: string, policyId: string) => |
| 20 | avp |
| 21 | .getPolicy({ policyStoreId, policyId }) |
| 22 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined))); |
| 23 | |
| 24 | const templateStatement = `permit( |
| 25 | principal == ?principal, |
| 26 | action == PhotoApp::Action::"viewPhoto", |
| 27 | resource |
| 28 | );`; |
| 29 | |
| 30 | test.provider( |
| 31 | "policy template lifecycle: create template + template-linked policy, update description, destroy", |
| 32 | (stack) => |
| 33 | Effect.gen(function* () { |
| 34 | yield* stack.destroy(); |
| 35 | |
| 36 | const makeStack = (description: string) => |
| 37 | Effect.gen(function* () { |
| 38 | const store = yield* PolicyStore("Store", { |
| 39 | validationMode: "OFF", |
| 40 | }); |
| 41 | const template = yield* PolicyTemplate("ViewPhoto", { |
| 42 | policyStoreId: store.policyStoreId, |
| 43 | statement: templateStatement, |
| 44 | description, |
| 45 | }); |
| 46 | const linked = yield* Policy("AliceCanView", { |
| 47 | policyStoreId: store.policyStoreId, |
| 48 | templateId: template.policyTemplateId, |
| 49 | principal: { entityType: "PhotoApp::User", entityId: "alice" }, |
| 50 | }); |
| 51 | return { store, template, linked }; |
| 52 | }); |
| 53 | |
| 54 | // create |
| 55 | const { store, template, linked } = yield* stack.deploy(makeStack("initial description")); |
| 56 | expect(template.policyTemplateId).toBeDefined(); |
| 57 | expect(linked.policyId).toBeDefined(); |
| 58 | |
| 59 | // out-of-band verify |
| 60 | const created = yield* findTemplate(store.policyStoreId, template.policyTemplateId); |
| 61 | expect(unwrap(created?.statement)).toContain("?principal"); |
| 62 | expect(unwrap(created?.description)).toBe("initial description"); |
| 63 | |
| 64 | const createdLinked = yield* findPolicy(store.policyStoreId, linked.policyId); |
| 65 | expect(createdLinked?.policyType).toBe("TEMPLATE_LINKED"); |
| 66 | |
| 67 | // update the template description in place (ids are stable) |
| 68 | const updated = yield* stack.deploy(makeStack("updated description")); |
| 69 | expect(updated.template.policyTemplateId).toBe(template.policyTemplateId); |
| 70 | expect(updated.linked.policyId).toBe(linked.policyId); |
| 71 | |
| 72 | const afterUpdate = yield* findTemplate(store.policyStoreId, template.policyTemplateId); |
| 73 | expect(unwrap(afterUpdate?.description)).toBe("updated description"); |
| 74 | |
| 75 | // destroy — linked policy must delete before the template |
| 76 | yield* stack.destroy(); |
| 77 | const goneTemplate = yield* findTemplate(store.policyStoreId, template.policyTemplateId); |
| 78 | expect(goneTemplate).toBeUndefined(); |
| 79 | }), |
| 80 | { |
| 81 | tags: ["provider:aws", "provider:aws:verifiedpermissions", "live"], |
| 82 | timeout: 180_000, |
| 83 | }, |
| 84 | ); |