The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as CloudHSMV2 from "@alchemy.run/aws/CloudHSMV2"; |
| 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 | // Well-formed-but-nonexistent identifiers — every route drives its binding |
| 12 | // against these so the fixture exercises IAM grants + typed error decoding |
| 13 | // at zero cost (no cluster, no HSM, no backup is ever created). |
| 14 | const NONEXISTENT_CLUSTER_ID = "cluster-aaaaaaaaaaa"; |
| 15 | const NONEXISTENT_BACKUP_ID = "backup-aaaaaaaaaaa"; |
| 16 | |
| 17 | // Placeholder PEM material for InitializeCluster — the nonexistent cluster |
| 18 | // id is rejected before the certificates are parsed. |
| 19 | const PLACEHOLDER_PEM = "-----BEGIN CERTIFICATE-----\nMIIB\n-----END CERTIFICATE-----"; |
| 20 | |
| 21 | export class CloudHSMV2TestFunction extends Lambda.Function<Lambda.Function>()( |
| 22 | "CloudHSMV2TestFunction", |
| 23 | ) {} |
| 24 | |
| 25 | export default CloudHSMV2TestFunction.make( |
| 26 | { |
| 27 | main, |
| 28 | functionUrl: true, |
| 29 | }, |
| 30 | Effect.gen(function* () { |
| 31 | const describeClusters = yield* CloudHSMV2.DescribeClusters(); |
| 32 | const describeBackups = yield* CloudHSMV2.DescribeBackups(); |
| 33 | const deleteBackup = yield* CloudHSMV2.DeleteBackup(); |
| 34 | const restoreBackup = yield* CloudHSMV2.RestoreBackup(); |
| 35 | const modifyBackupAttributes = yield* CloudHSMV2.ModifyBackupAttributes(); |
| 36 | const copyBackupToRegion = yield* CloudHSMV2.CopyBackupToRegion(); |
| 37 | const initializeCluster = yield* CloudHSMV2.InitializeCluster(); |
| 38 | const getResourcePolicy = yield* CloudHSMV2.GetResourcePolicy(); |
| 39 | const putResourcePolicy = yield* CloudHSMV2.PutResourcePolicy(); |
| 40 | const deleteResourcePolicy = yield* CloudHSMV2.DeleteResourcePolicy(); |
| 41 | |
| 42 | const bound = { |
| 43 | describeClusters, |
| 44 | describeBackups, |
| 45 | deleteBackup, |
| 46 | restoreBackup, |
| 47 | modifyBackupAttributes, |
| 48 | copyBackupToRegion, |
| 49 | initializeCluster, |
| 50 | getResourcePolicy, |
| 51 | putResourcePolicy, |
| 52 | deleteResourcePolicy, |
| 53 | }; |
| 54 | |
| 55 | return { |
| 56 | fetch: Effect.gen(function* () { |
| 57 | const request = yield* HttpServerRequest; |
| 58 | const url = new URL(request.originalUrl); |
| 59 | const pathname = url.pathname; |
| 60 | |
| 61 | if (request.method === "GET" && pathname === "/bindings") { |
| 62 | return yield* HttpServerResponse.json({ |
| 63 | bound: Object.keys(bound), |
| 64 | }); |
| 65 | } |
| 66 | |
| 67 | if (request.method === "GET" && pathname === "/clusters") { |
| 68 | // A filter on a nonexistent id yields an empty page — proves the |
| 69 | // grant and the response schema decode deterministically. |
| 70 | const result = yield* describeClusters({ |
| 71 | Filters: { clusterIds: [NONEXISTENT_CLUSTER_ID] }, |
| 72 | }); |
| 73 | return yield* HttpServerResponse.json({ |
| 74 | count: (result.Clusters ?? []).length, |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | if (request.method === "GET" && pathname === "/backups") { |
| 79 | const result = yield* describeBackups({ |
| 80 | Filters: { backupIds: [NONEXISTENT_BACKUP_ID] }, |
| 81 | }); |
| 82 | return yield* HttpServerResponse.json({ |
| 83 | count: (result.Backups ?? []).length, |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | if (request.method === "GET" && pathname === "/backup-delete") { |
| 88 | // A nonexistent backup id must surface the service's typed |
| 89 | // not-found tag — an IAM gap would surface AccessDeniedException |
| 90 | // and fail the route with an opaque 500 instead. |
| 91 | const tag = yield* deleteBackup({ |
| 92 | BackupId: NONEXISTENT_BACKUP_ID, |
| 93 | }).pipe( |
| 94 | Effect.map(() => "Deleted"), |
| 95 | Effect.catchTag("CloudHsmResourceNotFoundException", (e) => Effect.succeed(e._tag)), |
| 96 | ); |
| 97 | return yield* HttpServerResponse.json({ tag }); |
| 98 | } |
| 99 | |
| 100 | if (request.method === "GET" && pathname === "/backup-restore") { |
| 101 | const tag = yield* restoreBackup({ |
| 102 | BackupId: NONEXISTENT_BACKUP_ID, |
| 103 | }).pipe( |
| 104 | Effect.map(() => "Restored"), |
| 105 | Effect.catchTag("CloudHsmResourceNotFoundException", (e) => Effect.succeed(e._tag)), |
| 106 | ); |
| 107 | return yield* HttpServerResponse.json({ tag }); |
| 108 | } |
| 109 | |
| 110 | if (request.method === "GET" && pathname === "/backup-modify") { |
| 111 | const tag = yield* modifyBackupAttributes({ |
| 112 | BackupId: NONEXISTENT_BACKUP_ID, |
| 113 | NeverExpires: true, |
| 114 | }).pipe( |
| 115 | Effect.map(() => "Modified"), |
| 116 | Effect.catchTag("CloudHsmResourceNotFoundException", (e) => Effect.succeed(e._tag)), |
| 117 | ); |
| 118 | return yield* HttpServerResponse.json({ tag }); |
| 119 | } |
| 120 | |
| 121 | if (request.method === "GET" && pathname === "/backup-copy") { |
| 122 | const region = yield* Effect.sync(() => process.env.AWS_REGION ?? "us-west-2"); |
| 123 | const tag = yield* copyBackupToRegion({ |
| 124 | DestinationRegion: region === "us-east-1" ? "us-west-2" : "us-east-1", |
| 125 | BackupId: NONEXISTENT_BACKUP_ID, |
| 126 | }).pipe( |
| 127 | Effect.map(() => "Copied"), |
| 128 | Effect.catchTag( |
| 129 | ["CloudHsmResourceNotFoundException", "CloudHsmInvalidRequestException"], |
| 130 | (e) => Effect.succeed(e._tag), |
| 131 | ), |
| 132 | ); |
| 133 | return yield* HttpServerResponse.json({ tag }); |
| 134 | } |
| 135 | |
| 136 | if (request.method === "GET" && pathname === "/cluster-init") { |
| 137 | const tag = yield* initializeCluster({ |
| 138 | ClusterId: NONEXISTENT_CLUSTER_ID, |
| 139 | SignedCert: PLACEHOLDER_PEM, |
| 140 | TrustAnchor: PLACEHOLDER_PEM, |
| 141 | }).pipe( |
| 142 | Effect.map(() => "Initialized"), |
| 143 | Effect.catchTag( |
| 144 | ["CloudHsmResourceNotFoundException", "CloudHsmInvalidRequestException"], |
| 145 | (e) => Effect.succeed(e._tag), |
| 146 | ), |
| 147 | ); |
| 148 | return yield* HttpServerResponse.json({ tag }); |
| 149 | } |
| 150 | |
| 151 | if (request.method === "GET" && pathname === "/policy-get") { |
| 152 | // An empty request fails service-side validation — reaching the |
| 153 | // service's validator at all proves the IAM grant. |
| 154 | const tag = yield* getResourcePolicy().pipe( |
| 155 | Effect.map(() => "Found"), |
| 156 | Effect.catchTag( |
| 157 | ["CloudHsmInvalidRequestException", "CloudHsmResourceNotFoundException"], |
| 158 | (e) => Effect.succeed(e._tag), |
| 159 | ), |
| 160 | ); |
| 161 | return yield* HttpServerResponse.json({ tag }); |
| 162 | } |
| 163 | |
| 164 | if (request.method === "GET" && pathname === "/policy-put") { |
| 165 | const tag = yield* putResourcePolicy({}).pipe( |
| 166 | Effect.map(() => "Put"), |
| 167 | Effect.catchTag( |
| 168 | ["CloudHsmInvalidRequestException", "CloudHsmResourceNotFoundException"], |
| 169 | (e) => Effect.succeed(e._tag), |
| 170 | ), |
| 171 | ); |
| 172 | return yield* HttpServerResponse.json({ tag }); |
| 173 | } |
| 174 | |
| 175 | if (request.method === "GET" && pathname === "/policy-delete") { |
| 176 | const tag = yield* deleteResourcePolicy({}).pipe( |
| 177 | Effect.map(() => "PolicyDeleted"), |
| 178 | Effect.catchTag( |
| 179 | ["CloudHsmInvalidRequestException", "CloudHsmResourceNotFoundException"], |
| 180 | (e) => Effect.succeed(e._tag), |
| 181 | ), |
| 182 | ); |
| 183 | return yield* HttpServerResponse.json({ tag }); |
| 184 | } |
| 185 | |
| 186 | return yield* HttpServerResponse.json( |
| 187 | { error: "Not found", method: request.method, pathname }, |
| 188 | { status: 404 }, |
| 189 | ); |
| 190 | }).pipe(Effect.orDie), |
| 191 | }; |
| 192 | }).pipe( |
| 193 | Effect.provide( |
| 194 | Layer.mergeAll( |
| 195 | CloudHSMV2.DescribeClustersHttp, |
| 196 | CloudHSMV2.DescribeBackupsHttp, |
| 197 | CloudHSMV2.DeleteBackupHttp, |
| 198 | CloudHSMV2.RestoreBackupHttp, |
| 199 | CloudHSMV2.ModifyBackupAttributesHttp, |
| 200 | CloudHSMV2.CopyBackupToRegionHttp, |
| 201 | CloudHSMV2.InitializeClusterHttp, |
| 202 | CloudHSMV2.GetResourcePolicyHttp, |
| 203 | CloudHSMV2.PutResourcePolicyHttp, |
| 204 | CloudHSMV2.DeleteResourcePolicyHttp, |
| 205 | ), |
| 206 | ), |
| 207 | ), |
| 208 | ); |