The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { AccessPolicy, Collection, SecurityPolicy } from "@alchemy.run/aws/OpenSearchServerless"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as aoss from "@distilled.cloud/aws/opensearchserverless"; |
| 6 | import * as sts from "@distilled.cloud/aws/sts"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: AWS.providers() }); |
| 11 | |
| 12 | // Deterministic, collision-free names (≤32 chars, lowercase) reused across the |
| 13 | // collection and the policies that must reference its resource pattern. |
| 14 | const COLLECTION_NAME = "alchemy-test-aoss"; |
| 15 | const ENC_POLICY = "alchemy-aoss-enc"; |
| 16 | const NET_POLICY = "alchemy-aoss-net"; |
| 17 | const ACC_POLICY = "alchemy-aoss-acc"; |
| 18 | |
| 19 | const encryptionPolicy = { |
| 20 | Rules: [{ ResourceType: "collection", Resource: [`collection/${COLLECTION_NAME}`] }], |
| 21 | AWSOwnedKey: true, |
| 22 | }; |
| 23 | |
| 24 | const networkPolicy = [ |
| 25 | { |
| 26 | Rules: [ |
| 27 | { ResourceType: "collection", Resource: [`collection/${COLLECTION_NAME}`] }, |
| 28 | { ResourceType: "dashboard", Resource: [`collection/${COLLECTION_NAME}`] }, |
| 29 | ], |
| 30 | AllowFromPublic: true, |
| 31 | }, |
| 32 | ]; |
| 33 | |
| 34 | const accessPolicy = (principalArn: string) => [ |
| 35 | { |
| 36 | Rules: [ |
| 37 | { ResourceType: "index", Resource: [`index/${COLLECTION_NAME}/*`], Permission: ["aoss:*"] }, |
| 38 | { |
| 39 | ResourceType: "collection", |
| 40 | Resource: [`collection/${COLLECTION_NAME}`], |
| 41 | Permission: ["aoss:*"], |
| 42 | }, |
| 43 | ], |
| 44 | Principal: [principalArn], |
| 45 | }, |
| 46 | ]; |
| 47 | |
| 48 | // Ungated typed-error probe: proves the distilled error union carries the |
| 49 | // not-found tag that the SecurityPolicy/AccessPolicy read + delete paths depend |
| 50 | // on, and that batchGetCollection decodes the per-item error detail shape used |
| 51 | // to observe a missing collection. |
| 52 | test.provider( |
| 53 | "typed error semantics on nonexistent policies and collections", |
| 54 | () => |
| 55 | Effect.gen(function* () { |
| 56 | const policyError = yield* Effect.flip( |
| 57 | aoss.getSecurityPolicy({ type: "encryption", name: "alchemy-nonexistent-probe" }), |
| 58 | ); |
| 59 | expect(policyError._tag).toBe("ResourceNotFoundException"); |
| 60 | |
| 61 | // batchGetCollection does not throw for a missing collection — it reports |
| 62 | // it in collectionErrorDetails, which the Collection provider treats as |
| 63 | // "not present". |
| 64 | const batch = yield* aoss.batchGetCollection({ |
| 65 | names: ["alchemy-nonexistent-collection-probe"], |
| 66 | }); |
| 67 | expect(batch.collectionDetails ?? []).toHaveLength(0); |
| 68 | expect((batch.collectionErrorDetails ?? []).length).toBeGreaterThan(0); |
| 69 | }), |
| 70 | { tags: ["provider:aws", "provider:aws:opensearchserverless", "live"] }, |
| 71 | ); |
| 72 | |
| 73 | // Security and access policies are FREE (no OCU cost) and provision instantly, |
| 74 | // so their full lifecycle runs ungated. This exercises the bulk of the |
| 75 | // SecurityPolicy and AccessPolicy providers (create, no-op, update-with-version |
| 76 | // -bump, delete, verify-gone) without touching a billed collection. |
| 77 | test.provider( |
| 78 | "security + access policy lifecycle: create, update, destroy, verify gone", |
| 79 | (stack) => |
| 80 | Effect.gen(function* () { |
| 81 | // Clean slate in case a previous run died mid-flight. |
| 82 | yield* stack.destroy(); |
| 83 | |
| 84 | const { Account } = yield* sts.getCallerIdentity({}); |
| 85 | const principal = `arn:aws:iam::${Account}:role/alchemy-test-aoss-role`; |
| 86 | |
| 87 | // Create all three policies. |
| 88 | const created = yield* stack.deploy( |
| 89 | Effect.gen(function* () { |
| 90 | const enc = yield* SecurityPolicy("Enc", { |
| 91 | policyName: ENC_POLICY, |
| 92 | type: "encryption", |
| 93 | policy: encryptionPolicy, |
| 94 | description: "alchemy test encryption policy", |
| 95 | }); |
| 96 | const net = yield* SecurityPolicy("Net", { |
| 97 | policyName: NET_POLICY, |
| 98 | type: "network", |
| 99 | policy: networkPolicy, |
| 100 | }); |
| 101 | const acc = yield* AccessPolicy("Acc", { |
| 102 | policyName: ACC_POLICY, |
| 103 | policy: accessPolicy(principal), |
| 104 | }); |
| 105 | return { enc, net, acc }; |
| 106 | }), |
| 107 | ); |
| 108 | |
| 109 | expect(created.enc.policyName).toBe(ENC_POLICY); |
| 110 | expect(created.enc.type).toBe("encryption"); |
| 111 | expect(created.net.type).toBe("network"); |
| 112 | expect(created.acc.type).toBe("data"); |
| 113 | const initialNetVersion = created.net.policyVersion; |
| 114 | |
| 115 | // Out-of-band verification via distilled. |
| 116 | const encObserved = yield* aoss.getSecurityPolicy({ type: "encryption", name: ENC_POLICY }); |
| 117 | expect(encObserved.securityPolicyDetail?.name).toBe(ENC_POLICY); |
| 118 | const accObserved = yield* aoss.getAccessPolicy({ type: "data", name: ACC_POLICY }); |
| 119 | expect(accObserved.accessPolicyDetail?.name).toBe(ACC_POLICY); |
| 120 | |
| 121 | // No-op redeploy: policy version must not change. |
| 122 | const noop = yield* stack.deploy( |
| 123 | Effect.gen(function* () { |
| 124 | const enc = yield* SecurityPolicy("Enc", { |
| 125 | policyName: ENC_POLICY, |
| 126 | type: "encryption", |
| 127 | policy: encryptionPolicy, |
| 128 | description: "alchemy test encryption policy", |
| 129 | }); |
| 130 | const net = yield* SecurityPolicy("Net", { |
| 131 | policyName: NET_POLICY, |
| 132 | type: "network", |
| 133 | policy: networkPolicy, |
| 134 | }); |
| 135 | const acc = yield* AccessPolicy("Acc", { |
| 136 | policyName: ACC_POLICY, |
| 137 | policy: accessPolicy(principal), |
| 138 | }); |
| 139 | return { enc, net, acc }; |
| 140 | }), |
| 141 | ); |
| 142 | expect(noop.net.policyVersion).toBe(initialNetVersion); |
| 143 | |
| 144 | // Update the network policy document → new policy version. |
| 145 | const updated = yield* stack.deploy( |
| 146 | Effect.gen(function* () { |
| 147 | const enc = yield* SecurityPolicy("Enc", { |
| 148 | policyName: ENC_POLICY, |
| 149 | type: "encryption", |
| 150 | policy: encryptionPolicy, |
| 151 | description: "alchemy test encryption policy", |
| 152 | }); |
| 153 | const net = yield* SecurityPolicy("Net", { |
| 154 | policyName: NET_POLICY, |
| 155 | type: "network", |
| 156 | policy: [ |
| 157 | { |
| 158 | Rules: [ |
| 159 | { ResourceType: "collection", Resource: [`collection/${COLLECTION_NAME}`] }, |
| 160 | ], |
| 161 | AllowFromPublic: true, |
| 162 | }, |
| 163 | ], |
| 164 | }); |
| 165 | const acc = yield* AccessPolicy("Acc", { |
| 166 | policyName: ACC_POLICY, |
| 167 | policy: accessPolicy(principal), |
| 168 | }); |
| 169 | return { enc, net, acc }; |
| 170 | }), |
| 171 | ); |
| 172 | expect(updated.net.policyVersion).not.toBe(initialNetVersion); |
| 173 | |
| 174 | // Destroy and verify deletion out-of-band. |
| 175 | yield* stack.destroy(); |
| 176 | yield* assertPolicyGone("encryption", ENC_POLICY); |
| 177 | yield* assertPolicyGone("network", NET_POLICY); |
| 178 | yield* assertAccessPolicyGone(ACC_POLICY); |
| 179 | }), |
| 180 | { tags: ["provider:aws", "provider:aws:opensearchserverless", "live"], timeout: 120_000 }, |
| 181 | ); |
| 182 | |
| 183 | // A VECTORSEARCH collection is the vector store Bedrock Knowledge Bases require. |
| 184 | // Collections have a real-money OCU floor and take ~1-5 min to provision, so the |
| 185 | // full lifecycle is skipped under `--fast`, destroys IMMEDIATELY, and |
| 186 | // verifies the collection is gone. |
| 187 | test.provider.skipIf(!!process.env.FAST)( |
| 188 | "create VECTORSEARCH collection, verify ACTIVE, destroy, verify gone", |
| 189 | (stack) => |
| 190 | Effect.gen(function* () { |
| 191 | // Clean slate in case a previous run died mid-flight. |
| 192 | yield* stack.destroy(); |
| 193 | |
| 194 | const { Account } = yield* sts.getCallerIdentity({}); |
| 195 | const principal = `arn:aws:iam::${Account}:role/alchemy-test-aoss-role`; |
| 196 | |
| 197 | const { collection } = yield* stack.deploy( |
| 198 | Effect.gen(function* () { |
| 199 | // Encryption policy is a hard prerequisite for the collection. |
| 200 | const enc = yield* SecurityPolicy("Enc", { |
| 201 | policyName: ENC_POLICY, |
| 202 | type: "encryption", |
| 203 | policy: encryptionPolicy, |
| 204 | }); |
| 205 | const net = yield* SecurityPolicy("Net", { |
| 206 | policyName: NET_POLICY, |
| 207 | type: "network", |
| 208 | policy: networkPolicy, |
| 209 | }); |
| 210 | const acc = yield* AccessPolicy("Acc", { |
| 211 | policyName: ACC_POLICY, |
| 212 | policy: accessPolicy(principal), |
| 213 | }); |
| 214 | const collection = yield* Collection("Collection", { |
| 215 | collectionName: COLLECTION_NAME, |
| 216 | type: "VECTORSEARCH", |
| 217 | standbyReplicas: "DISABLED", |
| 218 | // Ensure the policies exist first (bindings/refs are unnecessary — |
| 219 | // the encryption policy is matched by resource pattern — but keep |
| 220 | // them referenced so the engine deploys them alongside). |
| 221 | tags: { encPolicy: enc.policyName, netPolicy: net.policyName }, |
| 222 | }); |
| 223 | return { collection, acc }; |
| 224 | }), |
| 225 | ); |
| 226 | |
| 227 | expect(collection.collectionName).toBe(COLLECTION_NAME); |
| 228 | expect(collection.type).toBe("VECTORSEARCH"); |
| 229 | expect(collection.collectionArn).toContain(":collection/"); |
| 230 | expect(collection.collectionId).toBeDefined(); |
| 231 | // The data-plane endpoint that a Bedrock Knowledge Base points at. |
| 232 | expect(collection.collectionEndpoint).toContain("aoss.amazonaws.com"); |
| 233 | |
| 234 | // Out-of-band verification via distilled: the collection is ACTIVE. |
| 235 | const observed = yield* aoss.batchGetCollection({ ids: [collection.collectionId] }); |
| 236 | const detail = observed.collectionDetails?.[0]; |
| 237 | expect(detail?.status).toBe("ACTIVE"); |
| 238 | expect(detail?.type).toBe("VECTORSEARCH"); |
| 239 | |
| 240 | // Destroy immediately — collections meter OCUs while they exist — and |
| 241 | // verify deletion out-of-band. |
| 242 | yield* stack.destroy(); |
| 243 | yield* assertCollectionGone(collection.collectionId); |
| 244 | }), |
| 245 | { tags: ["provider:aws", "provider:aws:opensearchserverless", "live"], timeout: 900_000 }, |
| 246 | ); |
| 247 | |
| 248 | const assertPolicyGone = (type: "encryption" | "network", name: string) => |
| 249 | aoss.getSecurityPolicy({ type, name }).pipe( |
| 250 | Effect.flip, |
| 251 | Effect.map((e) => expect(e._tag).toBe("ResourceNotFoundException")), |
| 252 | Effect.retry({ schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(5)]) }), |
| 253 | ); |
| 254 | |
| 255 | const assertAccessPolicyGone = (name: string) => |
| 256 | aoss.getAccessPolicy({ type: "data", name }).pipe( |
| 257 | Effect.flip, |
| 258 | Effect.map((e) => expect(e._tag).toBe("ResourceNotFoundException")), |
| 259 | Effect.retry({ schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(5)]) }), |
| 260 | ); |
| 261 | |
| 262 | // Deletion is verified as INITIATED (status `DELETING`) or fully gone — full |
| 263 | // disappearance takes another ~1-2 min server-side. |
| 264 | const assertCollectionGone = (id: string) => |
| 265 | Effect.gen(function* () { |
| 266 | const response = yield* aoss.batchGetCollection({ ids: [id] }); |
| 267 | const detail = response.collectionDetails?.[0]; |
| 268 | const status = detail?.status ?? "gone"; |
| 269 | if (status !== "gone" && status !== "DELETING") { |
| 270 | return yield* Effect.fail(new Error(`Collection '${id}' still exists (status: ${status})`)); |
| 271 | } |
| 272 | }).pipe( |
| 273 | Effect.retry({ schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(24)]) }), |
| 274 | ); |