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, 06:00 UTC
| 1 | import * as CloudTrail from "@alchemy.run/aws/CloudTrail"; |
| 2 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 3 | import * as S3 from "@alchemy.run/aws/S3"; |
| 4 | import * as Duration from "effect/Duration"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 7 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Result from "effect/Result"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import path from "pathe"; |
| 12 | |
| 13 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 14 | |
| 15 | /** |
| 16 | * Probe helper: run the bound operation and report either the success |
| 17 | * projection or the typed error tag, so the test can assert that operations |
| 18 | * rejected by CloudTrail fail with a TYPED tag (never an untyped catch-all). |
| 19 | */ |
| 20 | const tagOr = <A, E extends { _tag: string }, R>( |
| 21 | effect: Effect.Effect<A, E, R>, |
| 22 | onSuccess: (value: A) => Record<string, unknown>, |
| 23 | ) => |
| 24 | Effect.result(effect).pipe( |
| 25 | Effect.map((result) => |
| 26 | Result.isSuccess(result) ? onSuccess(result.success) : { errorTag: result.failure._tag }, |
| 27 | ), |
| 28 | ); |
| 29 | |
| 30 | export class CloudTrailTestFunction extends Lambda.Function<Lambda.Function>()( |
| 31 | "CloudTrailTestFunction", |
| 32 | ) {} |
| 33 | |
| 34 | export default CloudTrailTestFunction.make( |
| 35 | { |
| 36 | main, |
| 37 | functionUrl: true, |
| 38 | // LookupEvents scans + distilled's bounded retries must complete within |
| 39 | // the invocation; AWS's 3s default would intermittently time out. |
| 40 | timeout: Duration.seconds(30), |
| 41 | }, |
| 42 | Effect.gen(function* () { |
| 43 | // Holds the event-source markers (under events/). |
| 44 | const bucket = yield* S3.Bucket("CloudTrailEvents", { |
| 45 | forceDestroy: true, |
| 46 | }); |
| 47 | const BucketName = yield* bucket.bucketName; |
| 48 | |
| 49 | const putObject = yield* S3.PutObject(bucket); |
| 50 | const getObject = yield* S3.GetObject(bucket); |
| 51 | |
| 52 | // --- account-level bindings --- |
| 53 | const lookupEvents = yield* CloudTrail.LookupEvents(); |
| 54 | const listPublicKeys = yield* CloudTrail.ListPublicKeys(); |
| 55 | const listInsightsMetricData = yield* CloudTrail.ListInsightsMetricData(); |
| 56 | const listInsightsData = yield* CloudTrail.ListInsightsData(); |
| 57 | |
| 58 | // --- event source --- |
| 59 | // EventBridge receives every mutating management API call CloudTrail |
| 60 | // records; write a marker object per PutBucketTagging call so |
| 61 | // /events/check can observe the delivery out-of-band (the event may |
| 62 | // arrive on another instance). |
| 63 | yield* CloudTrail.consumeApiCallEvents( |
| 64 | { |
| 65 | eventSources: ["s3.amazonaws.com"], |
| 66 | eventNames: ["PutBucketTagging"], |
| 67 | }, |
| 68 | (events) => |
| 69 | Stream.runForEach(events, (event) => |
| 70 | putObject({ |
| 71 | Key: `events/${event.detail.requestParameters?.bucketName}`, |
| 72 | Body: JSON.stringify({ |
| 73 | eventName: event.detail.eventName, |
| 74 | eventSource: event.detail.eventSource, |
| 75 | eventID: event.detail.eventID, |
| 76 | }), |
| 77 | ContentType: "application/json", |
| 78 | }).pipe(Effect.orDie, Effect.asVoid), |
| 79 | ), |
| 80 | ); |
| 81 | |
| 82 | return { |
| 83 | fetch: Effect.gen(function* () { |
| 84 | const request = yield* HttpServerRequest; |
| 85 | const url = new URL(request.originalUrl); |
| 86 | const pathname = url.pathname; |
| 87 | const param = (name: string) => url.searchParams.get(name) ?? ""; |
| 88 | |
| 89 | // Report the fixture bucket so the test can tag it out-of-band. |
| 90 | if (request.method === "GET" && pathname === "/info") { |
| 91 | return yield* HttpServerResponse.json({ |
| 92 | bucketName: yield* BucketName, |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | // 90-day management event history — always available, no trail |
| 97 | // needed. Mutating calls always exist in an active test account. |
| 98 | if (request.method === "GET" && pathname === "/lookup") { |
| 99 | const result = yield* lookupEvents({ MaxResults: 5 }); |
| 100 | return yield* HttpServerResponse.json({ |
| 101 | count: (result.Events ?? []).length, |
| 102 | names: (result.Events ?? []).map((e) => e.EventName), |
| 103 | }); |
| 104 | } |
| 105 | |
| 106 | if (request.method === "GET" && pathname === "/public-keys") { |
| 107 | const result = yield* listPublicKeys(); |
| 108 | return yield* HttpServerResponse.json({ |
| 109 | fingerprints: (result.PublicKeyList ?? []).map((k) => k.Fingerprint), |
| 110 | }); |
| 111 | } |
| 112 | |
| 113 | // Insights metrics read; empty series when Insights has recorded no |
| 114 | // anomalies. A rejection must surface as a TYPED tag. |
| 115 | if (request.method === "GET" && pathname === "/insights-metric") { |
| 116 | return yield* HttpServerResponse.json( |
| 117 | yield* tagOr( |
| 118 | listInsightsMetricData({ |
| 119 | EventSource: "s3.amazonaws.com", |
| 120 | EventName: "PutObject", |
| 121 | InsightType: "ApiCallRateInsight", |
| 122 | MaxResults: 5, |
| 123 | }), |
| 124 | (result) => ({ |
| 125 | timestamps: (result.Timestamps ?? []).length, |
| 126 | values: (result.Values ?? []).length, |
| 127 | }), |
| 128 | ), |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | // Raw Insights events read; empty when Insights has recorded no |
| 133 | // anomalies (none are enabled in the test account). A rejection must |
| 134 | // surface as a TYPED tag. |
| 135 | if (request.method === "GET" && pathname === "/insights-data") { |
| 136 | return yield* HttpServerResponse.json( |
| 137 | yield* tagOr( |
| 138 | listInsightsData({ |
| 139 | InsightSource: "s3.amazonaws.com", |
| 140 | DataType: "InsightsEvents", |
| 141 | MaxResults: 5, |
| 142 | }), |
| 143 | (result) => ({ events: (result.Events ?? []).length }), |
| 144 | ), |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | // Has the API-call event for this bucket been delivered yet? |
| 149 | if (request.method === "GET" && pathname === "/events/check") { |
| 150 | const seen = yield* getObject({ |
| 151 | Key: `events/${param("bucket")}`, |
| 152 | }).pipe( |
| 153 | Effect.map(() => true), |
| 154 | Effect.catchTag("NoSuchKey", () => Effect.succeed(false)), |
| 155 | ); |
| 156 | return yield* HttpServerResponse.json({ seen }); |
| 157 | } |
| 158 | |
| 159 | return yield* HttpServerResponse.json( |
| 160 | { error: "Not found", method: request.method, pathname }, |
| 161 | { status: 404 }, |
| 162 | ); |
| 163 | }).pipe(Effect.orDie), |
| 164 | }; |
| 165 | }).pipe( |
| 166 | Effect.provide( |
| 167 | Layer.mergeAll( |
| 168 | Lambda.EventSource, |
| 169 | CloudTrail.LookupEventsHttp, |
| 170 | CloudTrail.ListPublicKeysHttp, |
| 171 | CloudTrail.ListInsightsMetricDataHttp, |
| 172 | CloudTrail.ListInsightsDataHttp, |
| 173 | S3.PutObjectHttp, |
| 174 | S3.GetObjectHttp, |
| 175 | ), |
| 176 | ), |
| 177 | ), |
| 178 | ); |