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 ControlTower from "@alchemy.run/aws/ControlTower"; |
| 2 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import path from "pathe"; |
| 8 | |
| 9 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 10 | |
| 11 | // A well-formed-but-nonexistent operation id — drives the typed error paths |
| 12 | // for the operation-status bindings (proving the IAM grant + typed union; |
| 13 | // an IAM gap would surface AccessDeniedException instead). |
| 14 | const NONEXISTENT_OPERATION_ID = "00000000-0000-0000-0000-000000000000"; |
| 15 | |
| 16 | // A well-formed-but-foreign landing zone ARN — the testing account has no |
| 17 | // landing zone, so any identifier is absent. |
| 18 | const NONEXISTENT_LANDING_ZONE_ARN = |
| 19 | "arn:aws:controltower:us-west-2:111111111111:landingzone/AAAAAAAAAAAAAAAA"; |
| 20 | |
| 21 | export class ControlTowerTestFunction extends Lambda.Function<Lambda.Function>()( |
| 22 | "ControlTowerTestFunction", |
| 23 | ) {} |
| 24 | |
| 25 | export default ControlTowerTestFunction.make( |
| 26 | { |
| 27 | main, |
| 28 | functionUrl: true, |
| 29 | }, |
| 30 | Effect.gen(function* () { |
| 31 | // --- account-level bindings (Control Tower has no data plane; these |
| 32 | // are the read/audit + operation-status APIs a governance function |
| 33 | // uses) --- |
| 34 | const listBaselines = yield* ControlTower.ListBaselines(); |
| 35 | const getBaseline = yield* ControlTower.GetBaseline(); |
| 36 | const listEnabledBaselines = yield* ControlTower.ListEnabledBaselines(); |
| 37 | const getBaselineOperation = yield* ControlTower.GetBaselineOperation(); |
| 38 | const listEnabledControls = yield* ControlTower.ListEnabledControls(); |
| 39 | const getControlOperation = yield* ControlTower.GetControlOperation(); |
| 40 | const listControlOperations = yield* ControlTower.ListControlOperations(); |
| 41 | const listLandingZones = yield* ControlTower.ListLandingZones(); |
| 42 | const getLandingZone = yield* ControlTower.GetLandingZone(); |
| 43 | const getLandingZoneOperation = yield* ControlTower.GetLandingZoneOperation(); |
| 44 | const listLandingZoneOperations = yield* ControlTower.ListLandingZoneOperations(); |
| 45 | |
| 46 | const bound = { |
| 47 | listBaselines, |
| 48 | getBaseline, |
| 49 | listEnabledBaselines, |
| 50 | getBaselineOperation, |
| 51 | listEnabledControls, |
| 52 | getControlOperation, |
| 53 | listControlOperations, |
| 54 | listLandingZones, |
| 55 | getLandingZone, |
| 56 | getLandingZoneOperation, |
| 57 | listLandingZoneOperations, |
| 58 | }; |
| 59 | |
| 60 | return { |
| 61 | fetch: Effect.gen(function* () { |
| 62 | const request = yield* HttpServerRequest; |
| 63 | const url = new URL(request.originalUrl); |
| 64 | const pathname = url.pathname; |
| 65 | |
| 66 | if (request.method === "GET" && pathname === "/bindings") { |
| 67 | return yield* HttpServerResponse.json({ |
| 68 | bound: Object.keys(bound), |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | if (request.method === "GET" && pathname === "/baselines") { |
| 73 | // The baseline catalog is served even without a landing zone; |
| 74 | // some org configurations reject the call outright — but always |
| 75 | // with a typed tag. |
| 76 | const result = yield* listBaselines().pipe( |
| 77 | Effect.map((r) => ({ |
| 78 | ok: true as const, |
| 79 | names: r.baselines.map((b) => b.name ?? null), |
| 80 | })), |
| 81 | Effect.catchTag(["AccessDeniedException", "UnauthorizedException"], (e) => |
| 82 | Effect.succeed({ ok: false as const, tag: e._tag }), |
| 83 | ), |
| 84 | ); |
| 85 | return yield* HttpServerResponse.json(result); |
| 86 | } |
| 87 | |
| 88 | if (request.method === "GET" && pathname === "/baseline") { |
| 89 | // Discover a baseline ARN from the catalog, then read it back — |
| 90 | // proves GetBaseline's grant end-to-end. |
| 91 | const result = yield* listBaselines().pipe( |
| 92 | Effect.flatMap((r) => |
| 93 | Effect.gen(function* () { |
| 94 | const arn = r.baselines.find((b) => b.name === "AWSControlTowerBaseline")?.arn; |
| 95 | if (arn === undefined) { |
| 96 | return { ok: false as const, tag: "NoCatalog" }; |
| 97 | } |
| 98 | const b = yield* getBaseline({ baselineIdentifier: arn }); |
| 99 | return { ok: true as const, name: b.name }; |
| 100 | }), |
| 101 | ), |
| 102 | Effect.catchTag( |
| 103 | ["AccessDeniedException", "UnauthorizedException", "ResourceNotFoundException"], |
| 104 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 105 | ), |
| 106 | ); |
| 107 | return yield* HttpServerResponse.json(result); |
| 108 | } |
| 109 | |
| 110 | if (request.method === "GET" && pathname === "/enabled-baselines") { |
| 111 | // The baseline API family rejects accounts without a landing |
| 112 | // zone with the typed UnauthorizedException (distilled patch). |
| 113 | const result = yield* listEnabledBaselines().pipe( |
| 114 | Effect.map((r) => ({ |
| 115 | ok: true as const, |
| 116 | count: r.enabledBaselines.length, |
| 117 | })), |
| 118 | Effect.catchTag( |
| 119 | ["AccessDeniedException", "UnauthorizedException", "ValidationException"], |
| 120 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 121 | ), |
| 122 | ); |
| 123 | return yield* HttpServerResponse.json(result); |
| 124 | } |
| 125 | |
| 126 | if (request.method === "GET" && pathname === "/enabled-controls") { |
| 127 | const result = yield* listEnabledControls().pipe( |
| 128 | Effect.map((r) => ({ |
| 129 | ok: true as const, |
| 130 | count: r.enabledControls.length, |
| 131 | })), |
| 132 | Effect.catchTag( |
| 133 | ["AccessDeniedException", "ResourceNotFoundException", "ValidationException"], |
| 134 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 135 | ), |
| 136 | ); |
| 137 | return yield* HttpServerResponse.json(result); |
| 138 | } |
| 139 | |
| 140 | if (request.method === "GET" && pathname === "/control-operations") { |
| 141 | const result = yield* listControlOperations().pipe( |
| 142 | Effect.map((r) => ({ |
| 143 | ok: true as const, |
| 144 | count: r.controlOperations.length, |
| 145 | })), |
| 146 | // ListControlOperations never throws ResourceNotFoundException |
| 147 | // (per the distilled union + AWS docs) — only the access/validation |
| 148 | // family applies here. |
| 149 | Effect.catchTag(["AccessDeniedException", "ValidationException"], (e) => |
| 150 | Effect.succeed({ ok: false as const, tag: e._tag }), |
| 151 | ), |
| 152 | ); |
| 153 | return yield* HttpServerResponse.json(result); |
| 154 | } |
| 155 | |
| 156 | if (request.method === "GET" && pathname === "/landing-zones") { |
| 157 | // The testing account has no landing zone — an empty (or |
| 158 | // singleton) list, never a crash. |
| 159 | const result = yield* listLandingZones().pipe( |
| 160 | Effect.map((r) => ({ |
| 161 | ok: true as const, |
| 162 | count: r.landingZones.length, |
| 163 | })), |
| 164 | Effect.catchTag(["AccessDeniedException", "UnauthorizedException"], (e) => |
| 165 | Effect.succeed({ ok: false as const, tag: e._tag }), |
| 166 | ), |
| 167 | ); |
| 168 | return yield* HttpServerResponse.json(result); |
| 169 | } |
| 170 | |
| 171 | if (request.method === "GET" && pathname === "/landing-zone-operations") { |
| 172 | const result = yield* listLandingZoneOperations().pipe( |
| 173 | Effect.map((r) => ({ |
| 174 | ok: true as const, |
| 175 | count: r.landingZoneOperations.length, |
| 176 | })), |
| 177 | Effect.catchTag( |
| 178 | ["AccessDeniedException", "UnauthorizedException", "ValidationException"], |
| 179 | (e) => Effect.succeed({ ok: false as const, tag: e._tag }), |
| 180 | ), |
| 181 | ); |
| 182 | return yield* HttpServerResponse.json(result); |
| 183 | } |
| 184 | |
| 185 | if (request.method === "GET" && pathname === "/landing-zone-not-found") { |
| 186 | const tag = yield* getLandingZone({ |
| 187 | landingZoneIdentifier: NONEXISTENT_LANDING_ZONE_ARN, |
| 188 | }).pipe( |
| 189 | Effect.map(() => "Found"), |
| 190 | Effect.catchTag( |
| 191 | [ |
| 192 | "ResourceNotFoundException", |
| 193 | "ValidationException", |
| 194 | "AccessDeniedException", |
| 195 | "UnauthorizedException", |
| 196 | ], |
| 197 | (e) => Effect.succeed(e._tag), |
| 198 | ), |
| 199 | ); |
| 200 | return yield* HttpServerResponse.json({ tag }); |
| 201 | } |
| 202 | |
| 203 | if (request.method === "GET" && pathname === "/control-operation-not-found") { |
| 204 | const tag = yield* getControlOperation({ |
| 205 | operationIdentifier: NONEXISTENT_OPERATION_ID, |
| 206 | }).pipe( |
| 207 | Effect.map(() => "Found"), |
| 208 | Effect.catchTag( |
| 209 | ["ResourceNotFoundException", "ValidationException", "AccessDeniedException"], |
| 210 | (e) => Effect.succeed(e._tag), |
| 211 | ), |
| 212 | ); |
| 213 | return yield* HttpServerResponse.json({ tag }); |
| 214 | } |
| 215 | |
| 216 | if (request.method === "GET" && pathname === "/baseline-operation-not-found") { |
| 217 | const result = yield* getBaselineOperation({ |
| 218 | operationIdentifier: NONEXISTENT_OPERATION_ID, |
| 219 | }).pipe( |
| 220 | Effect.map(() => ({ tag: "Found", message: "" })), |
| 221 | Effect.catchTag( |
| 222 | [ |
| 223 | "ResourceNotFoundException", |
| 224 | "ValidationException", |
| 225 | "AccessDeniedException", |
| 226 | "UnauthorizedException", |
| 227 | ], |
| 228 | (e) => Effect.succeed({ tag: e._tag, message: e.message }), |
| 229 | ), |
| 230 | ); |
| 231 | return yield* HttpServerResponse.json(result); |
| 232 | } |
| 233 | |
| 234 | if (request.method === "GET" && pathname === "/landing-zone-operation-not-found") { |
| 235 | const result = yield* getLandingZoneOperation({ |
| 236 | operationIdentifier: NONEXISTENT_OPERATION_ID, |
| 237 | }).pipe( |
| 238 | Effect.map(() => ({ tag: "Found", message: "" })), |
| 239 | Effect.catchTag( |
| 240 | [ |
| 241 | "ResourceNotFoundException", |
| 242 | "ValidationException", |
| 243 | "AccessDeniedException", |
| 244 | "UnauthorizedException", |
| 245 | ], |
| 246 | (e) => Effect.succeed({ tag: e._tag, message: e.message }), |
| 247 | ), |
| 248 | ); |
| 249 | return yield* HttpServerResponse.json(result); |
| 250 | } |
| 251 | |
| 252 | return yield* HttpServerResponse.json( |
| 253 | { error: "Not found", method: request.method, pathname }, |
| 254 | { status: 404 }, |
| 255 | ); |
| 256 | }).pipe(Effect.orDie), |
| 257 | }; |
| 258 | }).pipe( |
| 259 | Effect.provide( |
| 260 | Layer.mergeAll( |
| 261 | ControlTower.ListBaselinesHttp, |
| 262 | ControlTower.GetBaselineHttp, |
| 263 | ControlTower.ListEnabledBaselinesHttp, |
| 264 | ControlTower.GetBaselineOperationHttp, |
| 265 | ControlTower.ListEnabledControlsHttp, |
| 266 | ControlTower.GetControlOperationHttp, |
| 267 | ControlTower.ListControlOperationsHttp, |
| 268 | ControlTower.ListLandingZonesHttp, |
| 269 | ControlTower.GetLandingZoneHttp, |
| 270 | ControlTower.GetLandingZoneOperationHttp, |
| 271 | ControlTower.ListLandingZoneOperationsHttp, |
| 272 | ), |
| 273 | ), |
| 274 | ), |
| 275 | ); |