1import * as AVP from "@distilled.cloud/aws/verifiedpermissions";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Binding from "../../Binding.ts";
5import { isBindingHost } from "../Lambda/Function.ts";
6import { GetPolicies, type GetPoliciesRequest } from "./GetPolicies.ts";
7import type { PolicyStore } from "./PolicyStore.ts";
8
9export 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);