The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as Config from "@alchemy.run/aws/Config"; |
| 2 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 6 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import path from "pathe"; |
| 10 | |
| 11 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 12 | |
| 13 | export class ConfigTestFunction extends Lambda.Function<Lambda.Function>()("ConfigTestFunction") {} |
| 14 | |
| 15 | /** |
| 16 | * Every route answers `{ …fields }` on success or `{ errorTag }` when the |
| 17 | * operation fails with a TYPED error — the test asserts the tag is in a |
| 18 | * route-specific allowlist, which proves both the binding wiring and the |
| 19 | * IAM grant. An untyped error crashes into a 500 instead. |
| 20 | */ |
| 21 | const errorTagged = <A, E extends { _tag: string }, R>( |
| 22 | effect: Effect.Effect<A, E, R>, |
| 23 | ): Effect.Effect<A | { errorTag: string; errorMessage?: string }, never, R> => |
| 24 | effect.pipe( |
| 25 | Effect.map((a): A | { errorTag: string; errorMessage?: string } => a), |
| 26 | Effect.catch((e) => |
| 27 | Effect.succeed({ |
| 28 | errorTag: e._tag, |
| 29 | errorMessage: (e as { Message?: string }).Message ?? (e as { message?: string }).message, |
| 30 | }), |
| 31 | ), |
| 32 | ); |
| 33 | |
| 34 | // One binding per Config capability. DeliverConfigSnapshot has no route |
| 35 | // here: it binds to the account's singleton DeliveryChannel, whose |
| 36 | // lifecycle is gated behind AWS_TEST_CONFIG_RECORDER (see |
| 37 | // ConfigurationRecorder.test.ts) — deploying a second channel ungated |
| 38 | // would clobber the account setup. |
| 39 | export default ConfigTestFunction.make( |
| 40 | { |
| 41 | main, |
| 42 | functionUrl: true, |
| 43 | // Several routes fan out Config API calls — AWS's 3s default |
| 44 | // intermittently times out under cold starts. |
| 45 | timeout: Duration.seconds(30), |
| 46 | }, |
| 47 | Effect.gen(function* () { |
| 48 | // A managed rule the rule-scoped bindings are bound to. Requires a |
| 49 | // configuration recorder in the account/region — the test's beforeAll |
| 50 | // stands one up out-of-band before deploying this fixture. |
| 51 | const rule = yield* Config.ConfigRule("BindingRule", { |
| 52 | description: "alchemy config-bindings fixture rule", |
| 53 | source: { |
| 54 | owner: "AWS", |
| 55 | sourceIdentifier: "S3_BUCKET_VERSIONING_ENABLED", |
| 56 | }, |
| 57 | scope: { complianceResourceTypes: ["AWS::S3::Bucket"] }, |
| 58 | }); |
| 59 | |
| 60 | // Event source: subscribe the host to Config compliance-change events. |
| 61 | // The deploy proves the EventBridge rule + invoke permission wiring. |
| 62 | yield* Config.consumeConfigEvents({ kinds: ["compliance"] }, (events) => |
| 63 | Stream.runForEach(events, (event) => |
| 64 | Effect.log( |
| 65 | `config compliance: ${event.detail.configRuleName} -> ${event.detail.resourceId}`, |
| 66 | ), |
| 67 | ), |
| 68 | ); |
| 69 | |
| 70 | // Account-level bindings. |
| 71 | const batchGetResourceConfig = yield* Config.BatchGetResourceConfig(); |
| 72 | const getResourceConfigHistory = yield* Config.GetResourceConfigHistory(); |
| 73 | const listDiscoveredResources = yield* Config.ListDiscoveredResources(); |
| 74 | const getDiscoveredResourceCounts = yield* Config.GetDiscoveredResourceCounts(); |
| 75 | const selectResourceConfig = yield* Config.SelectResourceConfig(); |
| 76 | const describeComplianceByConfigRule = yield* Config.DescribeComplianceByConfigRule(); |
| 77 | const describeComplianceByResource = yield* Config.DescribeComplianceByResource(); |
| 78 | const getComplianceDetailsByResource = yield* Config.GetComplianceDetailsByResource(); |
| 79 | const getComplianceSummaryByConfigRule = yield* Config.GetComplianceSummaryByConfigRule(); |
| 80 | const getComplianceSummaryByResourceType = yield* Config.GetComplianceSummaryByResourceType(); |
| 81 | const describeConfigRuleEvaluationStatus = yield* Config.DescribeConfigRuleEvaluationStatus(); |
| 82 | const putEvaluations = yield* Config.PutEvaluations(); |
| 83 | const putResourceConfig = yield* Config.PutResourceConfig(); |
| 84 | const deleteResourceConfig = yield* Config.DeleteResourceConfig(); |
| 85 | const startResourceEvaluation = yield* Config.StartResourceEvaluation(); |
| 86 | const getResourceEvaluationSummary = yield* Config.GetResourceEvaluationSummary(); |
| 87 | const listResourceEvaluations = yield* Config.ListResourceEvaluations(); |
| 88 | |
| 89 | // Rule-scoped bindings (the rule name is injected automatically). |
| 90 | const getComplianceDetailsByConfigRule = yield* Config.GetComplianceDetailsByConfigRule(rule); |
| 91 | const startConfigRulesEvaluation = yield* Config.StartConfigRulesEvaluation(rule); |
| 92 | const putExternalEvaluation = yield* Config.PutExternalEvaluation(rule); |
| 93 | |
| 94 | const RuleName = yield* rule.configRuleName; |
| 95 | |
| 96 | return { |
| 97 | fetch: Effect.gen(function* () { |
| 98 | const request = yield* HttpServerRequest; |
| 99 | const url = new URL(request.originalUrl); |
| 100 | const route = `${request.method} ${url.pathname}`; |
| 101 | |
| 102 | switch (route) { |
| 103 | case "GET /health": |
| 104 | return HttpServerResponse.text("ok"); |
| 105 | |
| 106 | // ── Discovering resources / querying ────────────────────────── |
| 107 | |
| 108 | case "GET /select-resource-config": { |
| 109 | const result = yield* selectResourceConfig({ |
| 110 | Expression: "SELECT resourceId WHERE resourceType = 'AWS::S3::Bucket'", |
| 111 | }); |
| 112 | return yield* HttpServerResponse.json({ |
| 113 | results: result.Results ?? [], |
| 114 | }); |
| 115 | } |
| 116 | case "GET /list-discovered-resources": { |
| 117 | const result = yield* listDiscoveredResources({ |
| 118 | resourceType: "AWS::S3::Bucket", |
| 119 | }); |
| 120 | return yield* HttpServerResponse.json({ |
| 121 | identifiers: result.resourceIdentifiers ?? [], |
| 122 | }); |
| 123 | } |
| 124 | case "GET /get-discovered-resource-counts": { |
| 125 | const result = yield* getDiscoveredResourceCounts(); |
| 126 | return yield* HttpServerResponse.json({ |
| 127 | total: result.totalDiscoveredResources ?? 0, |
| 128 | counts: result.resourceCounts ?? [], |
| 129 | }); |
| 130 | } |
| 131 | case "POST /batch-get-resource-config": { |
| 132 | const result = yield* batchGetResourceConfig({ |
| 133 | resourceKeys: [ |
| 134 | { |
| 135 | resourceType: "AWS::S3::Bucket", |
| 136 | resourceId: "alchemy-nonexistent-bucket-probe", |
| 137 | }, |
| 138 | ], |
| 139 | }); |
| 140 | return yield* HttpServerResponse.json({ |
| 141 | items: result.baseConfigurationItems ?? [], |
| 142 | unprocessed: result.unprocessedResourceKeys ?? [], |
| 143 | }); |
| 144 | } |
| 145 | case "GET /get-resource-config-history": { |
| 146 | // The probe resource is never discovered — the typed |
| 147 | // ResourceNotDiscoveredException proves binding + IAM. |
| 148 | const result = yield* errorTagged( |
| 149 | getResourceConfigHistory({ |
| 150 | resourceType: "AWS::S3::Bucket", |
| 151 | resourceId: "alchemy-nonexistent-bucket-probe", |
| 152 | }), |
| 153 | ); |
| 154 | return yield* HttpServerResponse.json( |
| 155 | "errorTag" in result ? result : { items: result.configurationItems ?? [] }, |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | // ── Compliance reads ─────────────────────────────────────────── |
| 160 | |
| 161 | case "GET /describe-compliance-by-config-rule": { |
| 162 | const ruleName = yield* RuleName; |
| 163 | const result = yield* describeComplianceByConfigRule({ |
| 164 | ConfigRuleNames: [ruleName], |
| 165 | }); |
| 166 | return yield* HttpServerResponse.json({ |
| 167 | ruleName, |
| 168 | compliances: result.ComplianceByConfigRules ?? [], |
| 169 | }); |
| 170 | } |
| 171 | case "GET /describe-compliance-by-resource": { |
| 172 | const result = yield* describeComplianceByResource({ |
| 173 | ResourceType: "AWS::S3::Bucket", |
| 174 | }); |
| 175 | return yield* HttpServerResponse.json({ |
| 176 | compliances: result.ComplianceByResources ?? [], |
| 177 | }); |
| 178 | } |
| 179 | case "GET /get-compliance-details-by-resource": { |
| 180 | const result = yield* getComplianceDetailsByResource({ |
| 181 | ResourceType: "AWS::S3::Bucket", |
| 182 | ResourceId: "alchemy-nonexistent-bucket-probe", |
| 183 | }); |
| 184 | return yield* HttpServerResponse.json({ |
| 185 | evaluations: result.EvaluationResults ?? [], |
| 186 | }); |
| 187 | } |
| 188 | case "GET /get-compliance-summary-by-config-rule": { |
| 189 | const result = yield* getComplianceSummaryByConfigRule(); |
| 190 | return yield* HttpServerResponse.json({ |
| 191 | summary: result.ComplianceSummary ?? null, |
| 192 | }); |
| 193 | } |
| 194 | case "GET /get-compliance-summary-by-resource-type": { |
| 195 | const result = yield* getComplianceSummaryByResourceType(); |
| 196 | return yield* HttpServerResponse.json({ |
| 197 | summaries: result.ComplianceSummariesByResourceType ?? [], |
| 198 | }); |
| 199 | } |
| 200 | case "GET /get-compliance-details-by-config-rule": { |
| 201 | const result = yield* getComplianceDetailsByConfigRule(); |
| 202 | return yield* HttpServerResponse.json({ |
| 203 | ruleName: yield* RuleName, |
| 204 | evaluations: result.EvaluationResults ?? [], |
| 205 | }); |
| 206 | } |
| 207 | |
| 208 | // ── Rule evaluation ──────────────────────────────────────────── |
| 209 | |
| 210 | case "GET /describe-config-rule-evaluation-status": { |
| 211 | const ruleName = yield* RuleName; |
| 212 | const result = yield* describeConfigRuleEvaluationStatus({ |
| 213 | ConfigRuleNames: [ruleName], |
| 214 | }); |
| 215 | return yield* HttpServerResponse.json({ |
| 216 | ruleName, |
| 217 | statuses: (result.ConfigRulesEvaluationStatus ?? []).map((s) => s.ConfigRuleName), |
| 218 | }); |
| 219 | } |
| 220 | case "POST /start-config-rules-evaluation": { |
| 221 | // Back-to-back runs can reject with ResourceInUseException / |
| 222 | // LimitExceededException — typed either way. |
| 223 | const result = yield* errorTagged(startConfigRulesEvaluation()); |
| 224 | return yield* HttpServerResponse.json("errorTag" in result ? result : { ok: true }); |
| 225 | } |
| 226 | case "POST /put-evaluations": { |
| 227 | // TestMode validates the evaluations without pushing them; an |
| 228 | // out-of-band token can still reject with the typed |
| 229 | // InvalidResultTokenException. |
| 230 | const result = yield* errorTagged( |
| 231 | putEvaluations({ |
| 232 | ResultToken: "alchemy-config-bindings-test-token", |
| 233 | TestMode: true, |
| 234 | Evaluations: [ |
| 235 | { |
| 236 | ComplianceResourceType: "AWS::S3::Bucket", |
| 237 | ComplianceResourceId: "alchemy-nonexistent-bucket-probe", |
| 238 | ComplianceType: "COMPLIANT", |
| 239 | OrderingTimestamp: new Date(), |
| 240 | }, |
| 241 | ], |
| 242 | }), |
| 243 | ); |
| 244 | return yield* HttpServerResponse.json( |
| 245 | "errorTag" in result ? result : { failed: result.FailedEvaluations ?? [] }, |
| 246 | ); |
| 247 | } |
| 248 | case "POST /put-external-evaluation": { |
| 249 | // The fixture rule is a managed rule, not an external-evaluation |
| 250 | // rule — the typed InvalidParameterValueException proves binding |
| 251 | // + IAM. |
| 252 | const result = yield* errorTagged( |
| 253 | putExternalEvaluation({ |
| 254 | ExternalEvaluation: { |
| 255 | ComplianceResourceType: "AWS::S3::Bucket", |
| 256 | ComplianceResourceId: "alchemy-nonexistent-bucket-probe", |
| 257 | ComplianceType: "NOT_APPLICABLE", |
| 258 | OrderingTimestamp: new Date(), |
| 259 | }, |
| 260 | }), |
| 261 | ); |
| 262 | return yield* HttpServerResponse.json("errorTag" in result ? result : { ok: true }); |
| 263 | } |
| 264 | |
| 265 | // ── Custom resource recording ────────────────────────────────── |
| 266 | |
| 267 | case "POST /put-resource-config": { |
| 268 | // Recording a third-party type requires a running recorder — |
| 269 | // NoRunningConfigurationRecorderException is typed and proves |
| 270 | // binding + IAM when the fixture recorder is stopped. |
| 271 | const result = yield* errorTagged( |
| 272 | putResourceConfig({ |
| 273 | ResourceType: "MyCompany::Alchemy::Fixture", |
| 274 | SchemaVersionId: "1.0", |
| 275 | ResourceId: "alchemy-config-bindings-fixture", |
| 276 | Configuration: JSON.stringify({ fixture: true }), |
| 277 | }), |
| 278 | ); |
| 279 | return yield* HttpServerResponse.json("errorTag" in result ? result : { ok: true }); |
| 280 | } |
| 281 | case "POST /delete-resource-config": { |
| 282 | const result = yield* errorTagged( |
| 283 | deleteResourceConfig({ |
| 284 | ResourceType: "MyCompany::Alchemy::Fixture", |
| 285 | ResourceId: "alchemy-config-bindings-fixture", |
| 286 | }), |
| 287 | ); |
| 288 | return yield* HttpServerResponse.json("errorTag" in result ? result : { ok: true }); |
| 289 | } |
| 290 | |
| 291 | // ── Proactive resource evaluation ────────────────────────────── |
| 292 | |
| 293 | case "POST /start-resource-evaluation": { |
| 294 | const result = yield* errorTagged( |
| 295 | startResourceEvaluation({ |
| 296 | EvaluationMode: "PROACTIVE", |
| 297 | ResourceDetails: { |
| 298 | ResourceId: "alchemy-config-bindings-proactive", |
| 299 | ResourceType: "AWS::S3::Bucket", |
| 300 | ResourceConfiguration: JSON.stringify({ |
| 301 | BucketName: "alchemy-config-bindings-proactive", |
| 302 | }), |
| 303 | ResourceConfigurationSchemaType: "CFN_RESOURCE_SCHEMA", |
| 304 | }, |
| 305 | }), |
| 306 | ); |
| 307 | return yield* HttpServerResponse.json( |
| 308 | "errorTag" in result ? result : { id: result.ResourceEvaluationId }, |
| 309 | ); |
| 310 | } |
| 311 | case "GET /get-resource-evaluation-summary": { |
| 312 | const id = url.searchParams.get("id") ?? ""; |
| 313 | const result = yield* errorTagged( |
| 314 | getResourceEvaluationSummary({ ResourceEvaluationId: id }), |
| 315 | ); |
| 316 | return yield* HttpServerResponse.json( |
| 317 | "errorTag" in result |
| 318 | ? result |
| 319 | : { |
| 320 | id: result.ResourceEvaluationId, |
| 321 | status: result.EvaluationStatus?.Status, |
| 322 | }, |
| 323 | ); |
| 324 | } |
| 325 | case "GET /list-resource-evaluations": { |
| 326 | const result = yield* listResourceEvaluations({ |
| 327 | Filters: { EvaluationMode: "PROACTIVE" }, |
| 328 | }); |
| 329 | return yield* HttpServerResponse.json({ |
| 330 | evaluations: result.ResourceEvaluations ?? [], |
| 331 | }); |
| 332 | } |
| 333 | |
| 334 | default: |
| 335 | return yield* HttpServerResponse.json({ error: "Not found", route }, { status: 404 }); |
| 336 | } |
| 337 | }).pipe(Effect.orDie), |
| 338 | }; |
| 339 | }).pipe( |
| 340 | Effect.provide( |
| 341 | Layer.mergeAll( |
| 342 | Lambda.EventSource, |
| 343 | Config.BatchGetResourceConfigHttp, |
| 344 | Config.DeleteResourceConfigHttp, |
| 345 | Config.DescribeComplianceByConfigRuleHttp, |
| 346 | Config.DescribeComplianceByResourceHttp, |
| 347 | Config.DescribeConfigRuleEvaluationStatusHttp, |
| 348 | Config.GetComplianceDetailsByConfigRuleHttp, |
| 349 | Config.GetComplianceDetailsByResourceHttp, |
| 350 | Config.GetComplianceSummaryByConfigRuleHttp, |
| 351 | Config.GetComplianceSummaryByResourceTypeHttp, |
| 352 | Config.GetDiscoveredResourceCountsHttp, |
| 353 | Config.GetResourceConfigHistoryHttp, |
| 354 | Config.GetResourceEvaluationSummaryHttp, |
| 355 | Config.ListDiscoveredResourcesHttp, |
| 356 | Config.ListResourceEvaluationsHttp, |
| 357 | Config.PutEvaluationsHttp, |
| 358 | Config.PutExternalEvaluationHttp, |
| 359 | Config.PutResourceConfigHttp, |
| 360 | Config.SelectResourceConfigHttp, |
| 361 | Config.StartConfigRulesEvaluationHttp, |
| 362 | Config.StartResourceEvaluationHttp, |
| 363 | ), |
| 364 | ), |
| 365 | ), |
| 366 | ); |