@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 136 lines 5.4 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as ObservabilityAdmin from "@alchemy.run/aws/ObservabilityAdmin";
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 Result from "effect/Result";
9 import path from "pathe";
10
11 const main = path.resolve(import.meta.dirname, "handler.ts");
12
13 export class ObservabilityAdminBindingsFunction extends Lambda.Function<Lambda.Function>()(
14 "ObservabilityAdminBindingsFunction",
15 ) {}
16
17 export default ObservabilityAdminBindingsFunction.make(
18 {
19 main,
20 functionUrl: true,
21 timeout: Duration.seconds(30),
22 },
23 Effect.gen(function* () {
24 // A short-lived rule exercising the rule-scoped grant + name injection.
25 // It exists for well under the ~24h AWS Config discovery window, so it
26 // never actually configures flow logs. (Requires the account to be
27 // onboarded to telemetry config; the test's beforeAll ensures that
28 // out-of-band.)
29 const rule = yield* ObservabilityAdmin.TelemetryRule("BindingsRule", {
30 resourceType: "AWS::EC2::VPC",
31 telemetryType: "Logs",
32 telemetrySourceTypes: ["VPC_FLOW_LOGS"],
33 destinationConfiguration: {
34 DestinationType: "cloud-watch-logs",
35 Retention: "30 days",
36 },
37 });
38
39 const bound = {
40 listResourceTelemetry: yield* ObservabilityAdmin.ListResourceTelemetry(),
41 getTelemetryEvaluationStatus: yield* ObservabilityAdmin.GetTelemetryEvaluationStatus(),
42 getTelemetryEnrichmentStatus: yield* ObservabilityAdmin.GetTelemetryEnrichmentStatus(),
43 listTelemetryRules: yield* ObservabilityAdmin.ListTelemetryRules(),
44 getTelemetryRule: yield* ObservabilityAdmin.GetTelemetryRule(rule),
45 };
46
47 return {
48 fetch: Effect.gen(function* () {
49 const request = yield* HttpServerRequest;
50 const url = new URL(request.originalUrl);
51 const pathname = url.pathname;
52
53 if (request.method === "GET" && pathname === "/bindings") {
54 return yield* HttpServerResponse.json({
55 bound: Object.keys(bound),
56 });
57 }
58
59 // Account-level telemetry audit data plane. Right after onboarding
60 // the audit backend may answer with a service-side error until
61 // AWS Config discovery warms up (documented as up to 24h), so
62 // report the typed tag instead of dying — an IAM gap would surface
63 // as AccessDeniedException, which the test rejects.
64 if (request.method === "GET" && pathname === "/resource-telemetry") {
65 const result = yield* Effect.result(
66 bound.listResourceTelemetry({
67 ResourceTypes: ["AWS::EC2::VPC"],
68 MaxResults: 10,
69 }),
70 );
71 return yield* HttpServerResponse.json(
72 Result.isSuccess(result)
73 ? {
74 tag: "ok",
75 count: (result.success.TelemetryConfigurations ?? []).length,
76 }
77 : { tag: result.failure._tag, error: String(result.failure) },
78 );
79 }
80
81 // Account onboarding status.
82 if (request.method === "GET" && pathname === "/evaluation-status") {
83 const { Status } = yield* bound.getTelemetryEvaluationStatus();
84 return yield* HttpServerResponse.json({
85 status: Status ?? "NOT_STARTED",
86 });
87 }
88
89 // Enrichment status — a never-onboarded account answers with a typed
90 // ResourceNotFoundException, which still proves the grant.
91 if (request.method === "GET" && pathname === "/enrichment-status") {
92 const status = yield* bound.getTelemetryEnrichmentStatus().pipe(
93 Effect.map((r) => r.Status ?? "Stopped"),
94 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("NotOnboarded")),
95 );
96 return yield* HttpServerResponse.json({ status });
97 }
98
99 // Account-level rule enumeration — must include the bound rule.
100 if (request.method === "GET" && pathname === "/rules") {
101 const { TelemetryRuleSummaries } = yield* bound.listTelemetryRules();
102 return yield* HttpServerResponse.json({
103 names: (TelemetryRuleSummaries ?? []).flatMap((summary) =>
104 summary.RuleName === undefined ? [] : [summary.RuleName],
105 ),
106 });
107 }
108
109 // Rule-scoped read — the rule name is injected from the binding.
110 if (request.method === "GET" && pathname === "/rule") {
111 const got = yield* bound.getTelemetryRule();
112 return yield* HttpServerResponse.json({
113 ruleName: got.RuleName,
114 telemetryType: got.TelemetryRule?.TelemetryType,
115 retentionInDays: got.TelemetryRule?.DestinationConfiguration?.RetentionInDays,
116 });
117 }
118
119 return yield* HttpServerResponse.json(
120 { error: "Not found", method: request.method, pathname },
121 { status: 404 },
122 );
123 }).pipe(Effect.orDie),
124 };
125 }).pipe(
126 Effect.provide(
127 Layer.mergeAll(
128 ObservabilityAdmin.ListResourceTelemetryHttp,
129 ObservabilityAdmin.GetTelemetryEvaluationStatusHttp,
130 ObservabilityAdmin.GetTelemetryEnrichmentStatusHttp,
131 ObservabilityAdmin.ListTelemetryRulesHttp,
132 ObservabilityAdmin.GetTelemetryRuleHttp,
133 ),
134 ),
135 ),
136 );