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

main
ts 159 lines 5.51 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 AuthPolicyProps {
11 /**
12 * The ID or ARN of the service network or service the auth policy applies
13 * to. Immutable — changing it replaces the resource.
14 */
15 resourceIdentifier: string;
16 /**
17 * The IAM auth policy controlling access to the service network or service,
18 * either as a structured {@link PolicyDocument} or a raw JSON string
19 * (escape hatch).
20 */
21 policy: PolicyDocument | string;
22 }
23
24 export interface AuthPolicy extends Resource<
25 "AWS.VpcLattice.AuthPolicy",
26 AuthPolicyProps,
27 {
28 /**
29 * ID or ARN of the service network or service the policy is attached to.
30 */
31 resourceIdentifier: string;
32 /**
33 * The attached policy document as a JSON string.
34 */
35 policy: string;
36 /**
37 * Policy state reported by the API (`Active` or `Inactive` — `Inactive`
38 * when the target's `authType` is `NONE`).
39 */
40 state: string | undefined;
41 },
42 never,
43 Providers
44 > {}
45
46 /**
47 * An auth policy for a VPC Lattice service network or service — the IAM
48 * resource policy evaluated on every request when the target's `authType`
49 * is `AWS_IAM`.
50 *
51 * ### Attaching Auth Policies
52 * **Example:** Allow Authenticated Invoke on a Service Network
53 * ```typescript
54 * const network = yield* ServiceNetwork("SecureNetwork", {
55 * authType: "AWS_IAM",
56 * });
57 * const authPolicy = yield* AuthPolicy("NetworkAuthPolicy", {
58 * resourceIdentifier: network.serviceNetworkId,
59 * policy: {
60 * Version: "2012-10-17",
61 * Statement: [
62 * {
63 * Effect: "Allow",
64 * Principal: { AWS: "*" },
65 * Action: ["vpc-lattice-svcs:Invoke"],
66 * Resource: "*",
67 * },
68 * ],
69 * },
70 * });
71 * ```
72 *
73 * **Example:** Raw JSON Escape Hatch
74 * ```typescript
75 * const authPolicy = yield* AuthPolicy("ServiceAuthPolicy", {
76 * resourceIdentifier: service.serviceId,
77 * policy: JSON.stringify({
78 * Version: "2012-10-17",
79 * Statement: [{ Effect: "Allow", Principal: "*", Action: "vpc-lattice-svcs:Invoke", Resource: "*" }],
80 * }),
81 * });
82 * ```
83 *
84 * @resource
85 */
86 export const AuthPolicy = Resource<AuthPolicy>("AWS.VpcLattice.AuthPolicy");
87
88 export const AuthPolicyProvider = () =>
89 Provider.effect(
90 AuthPolicy,
91 Effect.gen(function* () {
92 // A missing parent and a parent without an auth policy are both
93 // "no policy" — getAuthPolicy answers with an empty body for the
94 // latter, so treat an absent `policy` as non-existence.
95 const observe = (resourceIdentifier: string) =>
96 vpclattice.getAuthPolicy({ resourceIdentifier }).pipe(
97 Effect.map((response) => (response.policy === undefined ? undefined : response)),
98 Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
99 );
100
101 const toDesired = (policy: PolicyDocument | string) =>
102 typeof policy === "string" ? policy : stringifyPolicyDocument(policy);
103
104 return {
105 stables: ["resourceIdentifier"],
106 diff: Effect.fn(function* ({ olds, news }) {
107 if (!isResolved(news)) return;
108 if (olds && olds.resourceIdentifier !== news.resourceIdentifier) {
109 return { action: "replace" } as const;
110 }
111 }),
112 read: Effect.fn(function* ({ olds, output }) {
113 const resourceIdentifier = output?.resourceIdentifier ?? olds?.resourceIdentifier;
114 if (!resourceIdentifier) return undefined;
115 const observed = yield* observe(resourceIdentifier);
116 if (!observed?.policy) return undefined;
117 return {
118 resourceIdentifier,
119 policy: observed.policy,
120 state: observed.state,
121 };
122 }),
123 reconcile: Effect.fn(function* ({ news, session }) {
124 const resourceIdentifier = news.resourceIdentifier;
125 const desired = toDesired(news.policy);
126
127 // Observe — putAuthPolicy is an upsert. Compare the canonicalized
128 // live document against the canonicalized desired one so a
129 // re-deploy of an equivalent policy (key order, whitespace) skips
130 // the API call entirely.
131 const observed = yield* observe(resourceIdentifier);
132 let state = observed?.state;
133 if (
134 observed?.policy === undefined ||
135 normalizePolicyDocument(observed.policy) !== normalizePolicyDocument(desired)
136 ) {
137 const put = yield* vpclattice.putAuthPolicy({
138 resourceIdentifier,
139 policy: desired,
140 });
141 state = put.state;
142 }
143
144 yield* session.note(resourceIdentifier);
145 return { resourceIdentifier, policy: desired, state };
146 }),
147 // Auth policies are sub-resources keyed entirely by their parent
148 // service network / service — there is no enumeration API.
149 list: () => Effect.succeed([] as AuthPolicy["Attributes"][]),
150 delete: Effect.fn(function* ({ output }) {
151 yield* vpclattice
152 .deleteAuthPolicy({
153 resourceIdentifier: output.resourceIdentifier,
154 })
155 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
156 }),
157 };
158 }),
159 );