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

main
ts 138 lines 5 KB
Raw
1 import * as vpclattice from "@distilled.cloud/aws/vpc-lattice";
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 { PolicyDocument } from "../IAM/Policy.ts";
7 import { normalizePolicyDocument, stringifyPolicyDocument } from "../IAM/Policy.ts";
8 import type { Providers } from "../Providers.ts";
9
10 export interface ResourcePolicyProps {
11 /**
12 * The ARN of the service network or service the resource policy is attached
13 * to. Immutable — changing it replaces the resource.
14 */
15 resourceArn: string;
16 /**
17 * The resource-based permission policy, either as a structured
18 * {@link PolicyDocument} or a raw JSON string (escape hatch). Must contain
19 * the same actions and condition statements as the RAM permission for
20 * sharing services and service networks.
21 */
22 policy: PolicyDocument | string;
23 }
24
25 export interface ResourcePolicy extends Resource<
26 "AWS.VpcLattice.ResourcePolicy",
27 ResourcePolicyProps,
28 {
29 /**
30 * ARN of the service network or service the policy is attached to.
31 */
32 resourceArn: string;
33 /**
34 * The attached policy document as a JSON string.
35 */
36 policy: string;
37 },
38 never,
39 Providers
40 > {}
41
42 /**
43 * A resource-based permission policy on a VPC Lattice service or service
44 * network — the policy AWS RAM manages when sharing Lattice resources across
45 * accounts, attachable directly for fine-grained cross-account control.
46 *
47 * ### Attaching Resource Policies
48 * **Example:** Allow Another Account to Associate with a Service Network
49 * ```typescript
50 * const network = yield* ServiceNetwork("SharedNetwork", {});
51 * const policy = yield* ResourcePolicy("SharePolicy", {
52 * resourceArn: network.serviceNetworkArn,
53 * policy: {
54 * Version: "2012-10-17",
55 * Statement: [
56 * {
57 * Effect: "Allow",
58 * Principal: { AWS: "arn:aws:iam::123456789012:root" },
59 * Action: [
60 * "vpc-lattice:CreateServiceNetworkVpcAssociation",
61 * "vpc-lattice:CreateServiceNetworkServiceAssociation",
62 * "vpc-lattice:GetServiceNetwork",
63 * ],
64 * Resource: network.serviceNetworkArn,
65 * },
66 * ],
67 * },
68 * });
69 * ```
70 *
71 * @resource
72 */
73 export const ResourcePolicy = Resource<ResourcePolicy>("AWS.VpcLattice.ResourcePolicy");
74
75 export const ResourcePolicyProvider = () =>
76 Provider.effect(
77 ResourcePolicy,
78 Effect.gen(function* () {
79 // A missing parent and a parent without a policy are both "no policy" —
80 // getResourcePolicy answers with an empty body for the latter, so treat
81 // an absent `policy` as non-existence.
82 const observe = (resourceArn: string) =>
83 vpclattice.getResourcePolicy({ resourceArn }).pipe(
84 Effect.map((response) => response.policy),
85 Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
86 );
87
88 const toDesired = (policy: PolicyDocument | string) =>
89 typeof policy === "string" ? policy : stringifyPolicyDocument(policy);
90
91 return {
92 stables: ["resourceArn"],
93 diff: Effect.fn(function* ({ olds, news }) {
94 if (!isResolved(news)) return;
95 if (olds && olds.resourceArn !== news.resourceArn) {
96 return { action: "replace" } as const;
97 }
98 }),
99 read: Effect.fn(function* ({ olds, output }) {
100 const resourceArn = output?.resourceArn ?? olds?.resourceArn;
101 if (!resourceArn) return undefined;
102 const observed = yield* observe(resourceArn);
103 if (observed === undefined) return undefined;
104 return { resourceArn, policy: observed };
105 }),
106 reconcile: Effect.fn(function* ({ news, session }) {
107 const resourceArn = news.resourceArn;
108 const desired = toDesired(news.policy);
109
110 // Observe — putResourcePolicy is an upsert. Compare the
111 // canonicalized live document against the canonicalized desired one
112 // so a re-deploy of an equivalent policy (key order, whitespace)
113 // skips the API call entirely.
114 const observed = yield* observe(resourceArn);
115 if (
116 observed === undefined ||
117 normalizePolicyDocument(observed) !== normalizePolicyDocument(desired)
118 ) {
119 yield* vpclattice.putResourcePolicy({
120 resourceArn,
121 policy: desired,
122 });
123 }
124
125 yield* session.note(resourceArn);
126 return { resourceArn, policy: desired };
127 }),
128 // Resource policies are sub-resources keyed entirely by their parent
129 // service network / service ARN — there is no enumeration API.
130 list: () => Effect.succeed([] as ResourcePolicy["Attributes"][]),
131 delete: Effect.fn(function* ({ output }) {
132 yield* vpclattice
133 .deleteResourcePolicy({ resourceArn: output.resourceArn })
134 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
135 }),
136 };
137 }),
138 );