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 AWS from "@alchemy.run/aws"; |
| 2 | import { AWSEnvironment } from "@alchemy.run/aws/Environment"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as osis from "@distilled.cloud/aws/osis"; |
| 6 | import * as Output from "alchemy/Output"; |
| 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 | // Ungated typed-error probe: proves the distilled error union carries the |
| 13 | // not-found tag this provider's read/delete paths depend on. |
| 14 | test.provider( |
| 15 | "getPipeline on a nonexistent pipeline fails with ResourceNotFoundException", |
| 16 | () => |
| 17 | Effect.gen(function* () { |
| 18 | const error = yield* Effect.flip( |
| 19 | osis.getPipeline({ PipelineName: "alchemy-nonexistent-probe" }), |
| 20 | ); |
| 21 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 22 | }), |
| 23 | { tags: ["provider:aws", "provider:aws:osis", "live"] }, |
| 24 | ); |
| 25 | |
| 26 | // Ungated probe for the ResourcePolicy provider's read path: OSIS reports |
| 27 | // "no policy" as a SUCCESS response with the empty document `"{}"` (not a |
| 28 | // ResourceNotFoundException) — the provider's absent-detection depends on |
| 29 | // this observed behavior. |
| 30 | test.provider( |
| 31 | "getResourcePolicy on a nonexistent pipeline returns the empty document", |
| 32 | () => |
| 33 | Effect.gen(function* () { |
| 34 | const { region, accountId } = yield* AWSEnvironment.current; |
| 35 | const response = yield* osis.getResourcePolicy({ |
| 36 | ResourceArn: `arn:aws:osis:${region}:${accountId}:pipeline/alchemy-nonexistent-probe`, |
| 37 | }); |
| 38 | expect(response.Policy === undefined || response.Policy === "{}").toBe(true); |
| 39 | }), |
| 40 | { tags: ["provider:aws", "provider:aws:osis", "live"] }, |
| 41 | ); |
| 42 | |
| 43 | // Ungated probe for the PipelineEndpoint provider's observe path: the list |
| 44 | // API answers (typed) even when the account has no endpoints. |
| 45 | test.provider( |
| 46 | "listPipelineEndpoints succeeds on an empty account", |
| 47 | () => |
| 48 | Effect.gen(function* () { |
| 49 | const response = yield* osis.listPipelineEndpoints({}); |
| 50 | expect(Array.isArray(response.PipelineEndpoints ?? [])).toBe(true); |
| 51 | }), |
| 52 | { tags: ["provider:aws", "provider:aws:osis", "live"] }, |
| 53 | ); |
| 54 | |
| 55 | // Data Prepper configuration: HTTP source draining to an S3 sink. The role |
| 56 | // and bucket are provisioned by the same stack; interpolation defers the |
| 57 | // concrete values until deploy time. |
| 58 | const pipelineConfig = ( |
| 59 | roleArn: Output.Output<string>, |
| 60 | bucketName: Output.Output<string>, |
| 61 | region: string, |
| 62 | ) => Output.interpolate`version: "2" |
| 63 | log-pipeline: |
| 64 | source: |
| 65 | http: |
| 66 | path: "/logs/ingest" |
| 67 | sink: |
| 68 | - s3: |
| 69 | aws: |
| 70 | sts_role_arn: "${roleArn}" |
| 71 | region: "${region}" |
| 72 | bucket: "${bucketName}" |
| 73 | threshold: |
| 74 | event_collect_timeout: "60s" |
| 75 | codec: |
| 76 | ndjson: |
| 77 | `; |
| 78 | |
| 79 | // Deletion is verified as INITIATED (status DELETING, irreversible) or fully |
| 80 | // gone. Full disappearance takes a few more minutes server-side. |
| 81 | const assertPipelineDeleting = (name: string) => |
| 82 | Effect.gen(function* () { |
| 83 | const status = yield* osis.getPipeline({ PipelineName: name }).pipe( |
| 84 | Effect.map((response) => response.Pipeline?.Status ?? "gone"), |
| 85 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("gone" as const)), |
| 86 | ); |
| 87 | if (status !== "gone" && status !== "DELETING") { |
| 88 | return yield* Effect.fail(new Error(`pipeline '${name}' still exists (status: ${status})`)); |
| 89 | } |
| 90 | }).pipe( |
| 91 | Effect.retry({ schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(18)]) }), |
| 92 | ); |
| 93 | |
| 94 | // OSIS pipelines take ~5-10 minutes to provision and are billed per |
| 95 | // Ingestion-OCU-hour while they exist (minimum 1 OCU). The full lifecycle — |
| 96 | // pipeline + resource policy + VPC pipeline endpoint — is skipped under |
| 97 | // `--fast` and always destroys what it created. |
| 98 | test.provider.skipIf(!!process.env.FAST)( |
| 99 | "create http-to-s3 pipeline with resource policy and VPC endpoint, verify, destroy", |
| 100 | (stack) => |
| 101 | Effect.gen(function* () { |
| 102 | yield* stack.destroy(); |
| 103 | |
| 104 | const { region, accountId } = yield* AWSEnvironment.current; |
| 105 | |
| 106 | const { pipeline, policy, endpoint } = yield* stack.deploy( |
| 107 | Effect.gen(function* () { |
| 108 | const bucket = yield* AWS.S3.Bucket("Sink", { forceDestroy: true }); |
| 109 | const role = yield* AWS.IAM.Role("PipelineRole", { |
| 110 | assumeRolePolicyDocument: { |
| 111 | Version: "2012-10-17", |
| 112 | Statement: [ |
| 113 | { |
| 114 | Effect: "Allow", |
| 115 | Principal: { Service: ["osis-pipelines.amazonaws.com"] }, |
| 116 | Action: ["sts:AssumeRole"], |
| 117 | }, |
| 118 | ], |
| 119 | }, |
| 120 | inlinePolicies: { |
| 121 | "s3-sink": { |
| 122 | Version: "2012-10-17", |
| 123 | Statement: [ |
| 124 | { |
| 125 | Effect: "Allow", |
| 126 | Action: ["s3:PutObject", "s3:AbortMultipartUpload"], |
| 127 | Resource: [Output.interpolate`${bucket.bucketArn}/*`], |
| 128 | }, |
| 129 | { |
| 130 | Effect: "Allow", |
| 131 | Action: ["s3:GetBucketLocation", "s3:ListBucket"], |
| 132 | Resource: [bucket.bucketArn], |
| 133 | }, |
| 134 | ], |
| 135 | }, |
| 136 | }, |
| 137 | }); |
| 138 | // Pipeline endpoints only attach to VPC pipelines ("The pipeline is |
| 139 | // not configured for VPC."), must live in a DIFFERENT VPC than the |
| 140 | // pipeline ("Creating a pipeline endpoint in the same VPC as the |
| 141 | // pipeline is not supported."), and every VPC involved needs DNS |
| 142 | // support + hostnames enabled. |
| 143 | const network = (name: string, octet: number) => |
| 144 | Effect.gen(function* () { |
| 145 | const vpc = yield* AWS.EC2.Vpc(`${name}Vpc`, { |
| 146 | cidrBlock: `10.${octet}.0.0/16`, |
| 147 | enableDnsSupport: true, |
| 148 | enableDnsHostnames: true, |
| 149 | }); |
| 150 | const subnet = yield* AWS.EC2.Subnet(`${name}Subnet`, { |
| 151 | vpcId: vpc.vpcId, |
| 152 | cidrBlock: `10.${octet}.1.0/24`, |
| 153 | availabilityZone: `${region}a`, |
| 154 | }); |
| 155 | const securityGroup = yield* AWS.EC2.SecurityGroup(`${name}SG`, { |
| 156 | vpcId: vpc.vpcId, |
| 157 | description: `osis ${name} fixture`, |
| 158 | ingress: [ |
| 159 | { ipProtocol: "tcp", fromPort: 443, toPort: 443, cidrIpv4: `10.${octet}.0.0/16` }, |
| 160 | ], |
| 161 | }); |
| 162 | return { |
| 163 | subnetIds: [subnet.subnetId], |
| 164 | securityGroupIds: [securityGroup.groupId], |
| 165 | }; |
| 166 | }); |
| 167 | const pipelineNetwork = yield* network("Pipeline", 42); |
| 168 | const endpointNetwork = yield* network("Endpoint", 43); |
| 169 | |
| 170 | const pipeline = yield* AWS.OSIS.Pipeline("Logs", { |
| 171 | minUnits: 1, |
| 172 | maxUnits: 1, |
| 173 | pipelineConfigurationBody: pipelineConfig(role.roleArn, bucket.bucketName, region), |
| 174 | vpcOptions: pipelineNetwork, |
| 175 | tags: { fixture: "osis-pipeline" }, |
| 176 | }); |
| 177 | |
| 178 | // Resource-based policy: OSIS only accepts `osis:CreatePipelineEndpoint` |
| 179 | // grants to 12-digit account IDs (an `arn:aws:iam::…:root` |
| 180 | // principal or `osis:Ingest` is rejected as "Invalid resource |
| 181 | // policy."). |
| 182 | const policy = yield* AWS.OSIS.ResourcePolicy("IngestPolicy", { |
| 183 | resourceArn: pipeline.pipelineArn, |
| 184 | policy: Output.interpolate`{ |
| 185 | "Version": "2012-10-17", |
| 186 | "Statement": [ |
| 187 | { |
| 188 | "Effect": "Allow", |
| 189 | "Principal": { "AWS": ["${accountId}"] }, |
| 190 | "Action": "osis:CreatePipelineEndpoint", |
| 191 | "Resource": "${pipeline.pipelineArn}" |
| 192 | } |
| 193 | ] |
| 194 | }`, |
| 195 | }); |
| 196 | |
| 197 | // VPC pipeline endpoint: private ingest into the pipeline. |
| 198 | const endpoint = yield* AWS.OSIS.PipelineEndpoint("Private", { |
| 199 | pipelineArn: pipeline.pipelineArn, |
| 200 | vpcOptions: endpointNetwork, |
| 201 | }); |
| 202 | |
| 203 | return { pipeline, policy, endpoint }; |
| 204 | }), |
| 205 | ); |
| 206 | |
| 207 | expect(pipeline.pipelineName).toBeDefined(); |
| 208 | expect(pipeline.pipelineArn).toContain(":pipeline/"); |
| 209 | expect(pipeline.status).toBe("ACTIVE"); |
| 210 | expect(pipeline.minUnits).toBe(1); |
| 211 | expect(pipeline.maxUnits).toBe(1); |
| 212 | expect(pipeline.ingestEndpointUrls?.length).toBeGreaterThan(0); |
| 213 | |
| 214 | // Out-of-band verification via distilled. |
| 215 | const described = yield* osis.getPipeline({ PipelineName: pipeline.pipelineName }); |
| 216 | expect(described.Pipeline?.Status).toBe("ACTIVE"); |
| 217 | expect(described.Pipeline?.MinUnits).toBe(1); |
| 218 | expect(described.Pipeline?.PipelineConfigurationBody).toContain("log-pipeline"); |
| 219 | |
| 220 | // Resource policy: attached and readable out-of-band. |
| 221 | expect(policy.resourceArn).toBe(pipeline.pipelineArn); |
| 222 | const readPolicy = yield* osis.getResourcePolicy({ ResourceArn: pipeline.pipelineArn }); |
| 223 | expect(readPolicy.Policy).toContain("osis:CreatePipelineEndpoint"); |
| 224 | |
| 225 | // VPC pipeline endpoint: created and visible out-of-band. |
| 226 | // Endpoint ids are opaque (e.g. "a8Wl1Q8FKm1WXWrrY5fD"), not `pe-` prefixed. |
| 227 | expect(endpoint.endpointId).toBeTruthy(); |
| 228 | expect(endpoint.pipelineArn).toBe(pipeline.pipelineArn); |
| 229 | expect(endpoint.status).toBe("ACTIVE"); |
| 230 | const endpoints = yield* osis.listPipelineEndpoints({}); |
| 231 | expect((endpoints.PipelineEndpoints ?? []).map((e) => e.EndpointId)).toContain( |
| 232 | endpoint.endpointId, |
| 233 | ); |
| 234 | |
| 235 | // Destroy immediately — pipelines bill per OCU-hour — and verify |
| 236 | // deletion was initiated out-of-band. |
| 237 | yield* stack.destroy(); |
| 238 | yield* assertPipelineDeleting(pipeline.pipelineName); |
| 239 | }), |
| 240 | // pipeline create (~5-10 min) + endpoint create (~5 min) + delete |
| 241 | // initiation, one test. |
| 242 | { |
| 243 | tags: [ |
| 244 | "provider:aws", |
| 245 | "provider:aws:ec2", |
| 246 | "provider:aws:iam", |
| 247 | "provider:aws:osis", |
| 248 | "provider:aws:s3", |
| 249 | "live", |
| 250 | ], |
| 251 | timeout: 1_800_000, |
| 252 | }, |
| 253 | ); |