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

main
ts 107 lines 3.86 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { Detector } from "./Detector.ts";
4
5 /**
6 * Shared scaffolding for Amazon GuardDuty HTTP bindings.
7 *
8 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
9 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two builders
10 * below. Everything except the operation and the IAM action list is
11 * boilerplate.
12 */
13
14 /**
15 * Build the impl Effect for a GuardDuty operation scoped to a
16 * {@link Detector}: the deploy-time half grants `actions` (on the bound
17 * detector's ARN where the action supports resource-level permissions,
18 * otherwise on `*`), and the runtime half injects the detector's
19 * `DetectorId` into every request.
20 */
21 export const makeGuardDutyDetectorHttpBinding = <
22 I extends { DetectorId: string },
23 A,
24 E,
25 R,
26 >(options: {
27 /** Fully-qualified binding tag, e.g. `AWS.GuardDuty.ListFindings`. */
28 tag: string;
29 /** The distilled operation. */
30 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
31 /** IAM actions granted. */
32 actions: readonly string[];
33 /**
34 * Whether the IAM action supports resource-level permissions on the
35 * detector ARN. Most GuardDuty detector-scoped actions do NOT — IAM only
36 * evaluates them against `Resource: "*"` (verified empirically: an
37 * ARN-scoped Allow is an implicit deny for e.g. `ListFindings`,
38 * `ListMembers`, `CreateSampleFindings`). Only the coverage actions
39 * (`ListCoverage`, `GetCoverageStatistics`) opt in.
40 * @default false
41 */
42 resourceLevel?: boolean;
43 }) =>
44 Effect.gen(function* () {
45 const bind = yield* AWSBindings.Bind;
46 const op = yield* AWSBindings.withCredentials(options.operation);
47
48 return Effect.fn(function* (detector: Detector) {
49 const DetectorId = yield* detector.detectorId;
50 if (!globalThis.__ALCHEMY_RUNTIME__) {
51 yield* bind`${options.tag}(${detector})`({
52 policyStatements: [
53 {
54 Effect: "Allow",
55 Action: [...options.actions],
56 Resource: options.resourceLevel ? [detector.detectorArn] : ["*"],
57 },
58 ],
59 });
60 }
61 return Effect.fn(`${options.tag}(${detector.LogicalId})`)(function* (
62 request?: Omit<I, "DetectorId">,
63 ) {
64 const detectorId = yield* DetectorId;
65 return yield* op({ ...request, DetectorId: detectorId } as I);
66 });
67 });
68 });
69
70 /**
71 * Build the impl Effect for an account-level GuardDuty operation — the
72 * member-account invitation flow (`ListInvitations`, `DeclineInvitations`,
73 * `DeleteInvitations`, `GetInvitationsCount`), the organization-admin
74 * actions, and the on-demand malware scan operations. The deploy-time half
75 * grants `actions` on `*`: these operations either take no resource at all
76 * or target resources (EC2 instances, S3 objects, foreign detectors) whose
77 * ARNs are only known at runtime.
78 */
79 export const makeGuardDutyAccountHttpBinding = <I, A, E, R>(options: {
80 /** Fully-qualified binding tag, e.g. `AWS.GuardDuty.ListInvitations`. */
81 tag: string;
82 /** The distilled operation, invoked with the caller's request as-is. */
83 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
84 /** IAM actions granted on `*`. */
85 actions: readonly string[];
86 }) =>
87 Effect.gen(function* () {
88 const bind = yield* AWSBindings.Bind;
89 const op = yield* AWSBindings.withCredentials(options.operation);
90
91 return Effect.fn(function* () {
92 if (!globalThis.__ALCHEMY_RUNTIME__) {
93 yield* bind`${options.tag}()`({
94 policyStatements: [
95 {
96 Effect: "Allow",
97 Action: [...options.actions],
98 Resource: ["*"],
99 },
100 ],
101 });
102 }
103 return Effect.fn(options.tag)(function* (request?: I) {
104 return yield* op((request ?? {}) as I);
105 });
106 });
107 });