| 1 | import * as AVP from "@distilled.cloud/aws/verifiedpermissions"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Binding from "../../Binding.ts"; |
| 5 | import { isBindingHost } from "../Lambda/Function.ts"; |
| 6 | import { GetPolicies, type GetPoliciesRequest } from "./GetPolicies.ts"; |
| 7 | import type { PolicyStore } from "./PolicyStore.ts"; |
| 8 | |
| 9 | export const GetPoliciesHttp = Layer.effect( |
| 10 | GetPolicies, |
| 11 | Effect.gen(function* () { |
| 12 | const batchGetPolicy = yield* AVP.batchGetPolicy; |
| 13 | |
| 14 | return Effect.fn(function* <S extends PolicyStore>(store: S) { |
| 15 | const PolicyStoreId = yield* store.policyStoreId; |
| 16 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 17 | const host = yield* Binding.Host; |
| 18 | if (isBindingHost(host)) { |
| 19 | yield* host.bind`Allow(${host}, AWS.VerifiedPermissions.GetPolicies(${store}))`( |
| 20 | { |
| 21 | policyStatements: [ |
| 22 | { |
| 23 | Effect: "Allow", |
| 24 | // BatchGetPolicy authorizes under verifiedpermissions:GetPolicy |
| 25 | Action: [ |
| 26 | "verifiedpermissions:GetPolicy", |
| 27 | "verifiedpermissions:BatchGetPolicy", |
| 28 | ], |
| 29 | Resource: [store.policyStoreArn], |
| 30 | }, |
| 31 | ], |
| 32 | }, |
| 33 | ); |
| 34 | } |
| 35 | } |
| 36 | const label = store.LogicalId; |
| 37 | return { |
| 38 | batchGetPolicy: Effect.fn( |
| 39 | `AWS.VerifiedPermissions.BatchGetPolicy(${label})`, |
| 40 | )(function* (request: GetPoliciesRequest) { |
| 41 | const policyStoreId = yield* PolicyStoreId; |
| 42 | return yield* batchGetPolicy({ |
| 43 | requests: request.policyIds.map((policyId) => ({ |
| 44 | policyStoreId, |
| 45 | policyId, |
| 46 | })), |
| 47 | }); |
| 48 | }), |
| 49 | }; |
| 50 | }); |
| 51 | }), |
| 52 | ); |