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

main
ts 196 lines 6.34 KB
Raw
1 import * as aa from "@distilled.cloud/aws/accessanalyzer";
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 { Providers } from "../Providers.ts";
7
8 /**
9 * A single filter criterion evaluated against a finding attribute. Provide at
10 * least one comparison; multiple comparisons on one criterion are ANDed.
11 */
12 export interface ArchiveRuleCriterion {
13 /** The attribute value must equal one of these values. */
14 eq?: string[];
15 /** The attribute value must not equal any of these values. */
16 neq?: string[];
17 /** The attribute value must contain one of these substrings. */
18 contains?: string[];
19 /** The attribute must (or must not) be present. */
20 exists?: boolean;
21 }
22
23 export interface ArchiveRuleProps {
24 /**
25 * The name of the analyzer the rule belongs to. Changing the analyzer
26 * replaces the rule.
27 */
28 analyzerName: string;
29 /**
30 * The name of the archive rule, unique within the analyzer. Changing the
31 * name replaces the rule.
32 */
33 ruleName: string;
34 /**
35 * The filter criteria keyed by finding attribute (e.g. `principal.AWS`,
36 * `resource`, `condition.aws:PrincipalOrgID`, `isPublic`). New findings
37 * matching every criterion are automatically archived. Mutable — changing
38 * the filter updates the rule in place.
39 */
40 filter: Record<string, ArchiveRuleCriterion>;
41 }
42
43 export interface ArchiveRule extends Resource<
44 "AWS.AccessAnalyzer.ArchiveRule",
45 ArchiveRuleProps,
46 {
47 analyzerName: string;
48 ruleName: string;
49 },
50 {},
51 Providers
52 > {}
53
54 /**
55 * An archive rule for an IAM Access Analyzer — automatically archives new
56 * findings that match the filter criteria, so expected cross-account or
57 * public grants don't clutter the active findings list.
58 *
59 * Archive rules apply only to findings created after the rule; existing
60 * findings are unaffected.
61 * ### Creating Archive Rules
62 * **Example:** Archive Findings from a Trusted Account
63 * ```typescript
64 * import * as AWS from "@alchemy.run/aws";
65 *
66 * const analyzer = yield* AWS.AccessAnalyzer.Analyzer("AccountAnalyzer", {});
67 *
68 * yield* AWS.AccessAnalyzer.ArchiveRule("TrustedAccount", {
69 * analyzerName: analyzer.analyzerName,
70 * ruleName: "trusted-account",
71 * filter: {
72 * "principal.AWS": { eq: ["123456789012"] },
73 * },
74 * });
75 * ```
76 *
77 * **Example:** Archive Public S3 Findings
78 * ```typescript
79 * yield* AWS.AccessAnalyzer.ArchiveRule("PublicBuckets", {
80 * analyzerName: analyzer.analyzerName,
81 * ruleName: "public-buckets",
82 * filter: {
83 * resourceType: { eq: ["AWS::S3::Bucket"] },
84 * isPublic: { eq: ["true"] },
85 * },
86 * });
87 * ```
88 *
89 * @resource
90 */
91 export const ArchiveRule = Resource<ArchiveRule>("AWS.AccessAnalyzer.ArchiveRule");
92
93 export const ArchiveRuleProvider = () =>
94 Provider.effect(
95 ArchiveRule,
96 Effect.gen(function* () {
97 const toFilter = (filter: Record<string, ArchiveRuleCriterion>) =>
98 Object.fromEntries(
99 Object.entries(filter).map(([key, criterion]) => [
100 key,
101 {
102 eq: criterion.eq,
103 neq: criterion.neq,
104 contains: criterion.contains,
105 exists: criterion.exists,
106 },
107 ]),
108 );
109
110 const observe = Effect.fn(function* (analyzerName: string, ruleName: string) {
111 return yield* aa.getArchiveRule({ analyzerName, ruleName }).pipe(
112 Effect.map((r) => r.archiveRule),
113 Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
114 );
115 });
116
117 return ArchiveRule.Provider.of({
118 stables: ["analyzerName", "ruleName"],
119
120 // sub-resource keyed by parent analyzer + rule name — not enumerable
121 // account-wide without the parent
122 list: () => Effect.succeed([]),
123
124 read: Effect.fn(function* ({ olds, output }) {
125 const analyzerName = output?.analyzerName ?? olds?.analyzerName;
126 const ruleName = output?.ruleName ?? olds?.ruleName;
127 if (analyzerName === undefined || ruleName === undefined) {
128 return undefined;
129 }
130 const rule = yield* observe(analyzerName, ruleName);
131 if (rule === undefined) return undefined;
132 return { analyzerName, ruleName: rule.ruleName };
133 }),
134
135 diff: Effect.fn(function* ({ news, olds }) {
136 if (!isResolved(news)) return undefined;
137 if (olds.analyzerName !== news.analyzerName) {
138 return { action: "replace" } as const;
139 }
140 if (olds.ruleName !== news.ruleName) {
141 return { action: "replace" } as const;
142 }
143 // filter is mutable → default update path
144 }),
145
146 reconcile: Effect.fn(function* ({ news, session }) {
147 const filter = toFilter(news.filter);
148
149 // 1. OBSERVE — cloud state is authoritative
150 const existing = yield* observe(news.analyzerName, news.ruleName);
151
152 // 2. ENSURE / SYNC — createArchiveRule for a new rule, otherwise
153 // updateArchiveRule to converge the filter. Both are idempotent.
154 if (existing === undefined) {
155 yield* aa
156 .createArchiveRule({
157 analyzerName: news.analyzerName,
158 ruleName: news.ruleName,
159 filter,
160 })
161 .pipe(
162 // a concurrent create — fall through to update the filter
163 Effect.catchTag("ConflictException", () =>
164 aa.updateArchiveRule({
165 analyzerName: news.analyzerName,
166 ruleName: news.ruleName,
167 filter,
168 }),
169 ),
170 );
171 } else {
172 yield* aa.updateArchiveRule({
173 analyzerName: news.analyzerName,
174 ruleName: news.ruleName,
175 filter,
176 });
177 }
178
179 yield* session.note(news.ruleName);
180 return {
181 analyzerName: news.analyzerName,
182 ruleName: news.ruleName,
183 };
184 }),
185
186 delete: Effect.fn(function* ({ output }) {
187 yield* aa
188 .deleteArchiveRule({
189 analyzerName: output.analyzerName,
190 ruleName: output.ruleName,
191 })
192 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
193 }),
194 });
195 }),
196 );