|
| 1 | +import { serve } from "bun"; |
| 2 | +import { |
| 3 | + afterEach, |
| 4 | + beforeAll, |
| 5 | + describe, |
| 6 | + expect, |
| 7 | + it, |
| 8 | + setDefaultTimeout, |
| 9 | +} from "bun:test"; |
| 10 | +import { |
| 11 | + execContainer, |
| 12 | + findResourceInstance, |
| 13 | + removeContainer, |
| 14 | + runContainer, |
| 15 | + runTerraformApply, |
| 16 | + runTerraformInit, |
| 17 | + testRequiredVariables, |
| 18 | +} from "~test"; |
| 19 | + |
| 20 | +let cleanupFunctions: (() => Promise<void>)[] = []; |
| 21 | +const registerCleanup = (cleanup: () => Promise<void>) => { |
| 22 | + cleanupFunctions.push(cleanup); |
| 23 | +}; |
| 24 | +afterEach(async () => { |
| 25 | + const cleanupFnsCopy = cleanupFunctions.slice().reverse(); |
| 26 | + cleanupFunctions = []; |
| 27 | + for (const cleanup of cleanupFnsCopy) { |
| 28 | + try { |
| 29 | + await cleanup(); |
| 30 | + } catch (error) { |
| 31 | + console.error("Error during cleanup:", error); |
| 32 | + } |
| 33 | + } |
| 34 | +}); |
| 35 | + |
| 36 | +const FAKE_CERT = |
| 37 | + "-----BEGIN CERTIFICATE-----\nMIIBfakecert\n-----END CERTIFICATE-----\n"; |
| 38 | + |
| 39 | +// Runs terraform apply to render the setup script, then starts a Docker |
| 40 | +// container where we can execute it against a mock server. |
| 41 | +const setupContainer = async (vars: Record<string, string> = {}) => { |
| 42 | + const state = await runTerraformApply(import.meta.dir, { |
| 43 | + agent_id: "foo", |
| 44 | + proxy_url: "https://aiproxy.example.com", |
| 45 | + ...vars, |
| 46 | + }); |
| 47 | + const instance = findResourceInstance(state, "coder_script"); |
| 48 | + const id = await runContainer("lorello/alpine-bash"); |
| 49 | + |
| 50 | + registerCleanup(async () => { |
| 51 | + await removeContainer(id); |
| 52 | + }); |
| 53 | + |
| 54 | + return { id, instance }; |
| 55 | +}; |
| 56 | + |
| 57 | +// Starts a mock HTTP server that simulates the Coder API certificate endpoint. |
| 58 | +// Returns the server and its base URL. |
| 59 | +const setupServer = (handler: (req: Request) => Response) => { |
| 60 | + const server = serve({ |
| 61 | + fetch: handler, |
| 62 | + port: 0, |
| 63 | + }); |
| 64 | + registerCleanup(async () => { |
| 65 | + server.stop(); |
| 66 | + }); |
| 67 | + return { |
| 68 | + server, |
| 69 | + // Base URL without trailing slash |
| 70 | + url: server.url.toString().slice(0, -1), |
| 71 | + }; |
| 72 | +}; |
| 73 | + |
| 74 | +setDefaultTimeout(30 * 1000); |
| 75 | + |
| 76 | +describe("aibridge-proxy", () => { |
| 77 | + beforeAll(async () => { |
| 78 | + await runTerraformInit(import.meta.dir); |
| 79 | + }); |
| 80 | + |
| 81 | + // Verify that agent_id and proxy_url are required. |
| 82 | + testRequiredVariables(import.meta.dir, { |
| 83 | + agent_id: "foo", |
| 84 | + proxy_url: "https://aiproxy.example.com", |
| 85 | + }); |
| 86 | + |
| 87 | + it("downloads the CA certificate successfully", async () => { |
| 88 | + let receivedToken = ""; |
| 89 | + const { url } = setupServer((req) => { |
| 90 | + const reqUrl = new URL(req.url); |
| 91 | + if (reqUrl.pathname === "/api/v2/aibridge/proxy/ca-cert.pem") { |
| 92 | + receivedToken = req.headers.get("Coder-Session-Token") || ""; |
| 93 | + return new Response(FAKE_CERT, { |
| 94 | + status: 200, |
| 95 | + headers: { "Content-Type": "application/x-pem-file" }, |
| 96 | + }); |
| 97 | + } |
| 98 | + return new Response("not found", { status: 404 }); |
| 99 | + }); |
| 100 | + |
| 101 | + const { id, instance } = await setupContainer(); |
| 102 | + |
| 103 | + // Override ACCESS_URL and SESSION_TOKEN at runtime to point at the mock server. |
| 104 | + const exec = await execContainer(id, [ |
| 105 | + "env", |
| 106 | + `ACCESS_URL=${url}`, |
| 107 | + "SESSION_TOKEN=test-session-token-123", |
| 108 | + "bash", |
| 109 | + "-c", |
| 110 | + instance.script, |
| 111 | + ]); |
| 112 | + expect(exec.exitCode).toBe(0); |
| 113 | + expect(exec.stdout).toContain( |
| 114 | + "AI Bridge Proxy CA certificate saved to /tmp/aibridge-proxy/ca-cert.pem", |
| 115 | + ); |
| 116 | + |
| 117 | + // Verify the cert was written to the default path. |
| 118 | + const certContent = await execContainer(id, [ |
| 119 | + "cat", |
| 120 | + "/tmp/aibridge-proxy/ca-cert.pem", |
| 121 | + ]); |
| 122 | + expect(certContent.stdout).toContain("BEGIN CERTIFICATE"); |
| 123 | + |
| 124 | + // Verify the session token was sent in the request header. |
| 125 | + expect(receivedToken).toBe("test-session-token-123"); |
| 126 | + }); |
| 127 | + |
| 128 | + it("fails when the server is unreachable", async () => { |
| 129 | + const { id, instance } = await setupContainer(); |
| 130 | + |
| 131 | + // Port 9999 has nothing listening, so curl will fail to connect. |
| 132 | + const exec = await execContainer(id, [ |
| 133 | + "env", |
| 134 | + "ACCESS_URL=http://localhost:9999", |
| 135 | + "SESSION_TOKEN=mock-token", |
| 136 | + "bash", |
| 137 | + "-c", |
| 138 | + instance.script, |
| 139 | + ]); |
| 140 | + expect(exec.exitCode).not.toBe(0); |
| 141 | + expect(exec.stdout).toContain( |
| 142 | + "AI Bridge Proxy setup failed: could not connect to", |
| 143 | + ); |
| 144 | + }); |
| 145 | + |
| 146 | + it("fails when the server returns a non-200 status", async () => { |
| 147 | + const { url } = setupServer(() => { |
| 148 | + return new Response("not found", { status: 404 }); |
| 149 | + }); |
| 150 | + |
| 151 | + const { id, instance } = await setupContainer(); |
| 152 | + |
| 153 | + const exec = await execContainer(id, [ |
| 154 | + "env", |
| 155 | + `ACCESS_URL=${url}`, |
| 156 | + "SESSION_TOKEN=mock-token", |
| 157 | + "bash", |
| 158 | + "-c", |
| 159 | + instance.script, |
| 160 | + ]); |
| 161 | + expect(exec.exitCode).not.toBe(0); |
| 162 | + expect(exec.stdout).toContain( |
| 163 | + "AI Bridge Proxy setup failed: unexpected response", |
| 164 | + ); |
| 165 | + }); |
| 166 | + |
| 167 | + it("fails when the server returns an empty response", async () => { |
| 168 | + const { url } = setupServer((req) => { |
| 169 | + const reqUrl = new URL(req.url); |
| 170 | + if (reqUrl.pathname === "/api/v2/aibridge/proxy/ca-cert.pem") { |
| 171 | + return new Response("", { status: 200 }); |
| 172 | + } |
| 173 | + return new Response("not found", { status: 404 }); |
| 174 | + }); |
| 175 | + |
| 176 | + const { id, instance } = await setupContainer(); |
| 177 | + |
| 178 | + const exec = await execContainer(id, [ |
| 179 | + "env", |
| 180 | + `ACCESS_URL=${url}`, |
| 181 | + "SESSION_TOKEN=mock-token", |
| 182 | + "bash", |
| 183 | + "-c", |
| 184 | + instance.script, |
| 185 | + ]); |
| 186 | + expect(exec.exitCode).not.toBe(0); |
| 187 | + expect(exec.stdout).toContain( |
| 188 | + "AI Bridge Proxy setup failed: downloaded certificate is empty.", |
| 189 | + ); |
| 190 | + }); |
| 191 | + |
| 192 | + it("saves the certificate to a custom path", async () => { |
| 193 | + const { url } = setupServer((req) => { |
| 194 | + const reqUrl = new URL(req.url); |
| 195 | + if (reqUrl.pathname === "/api/v2/aibridge/proxy/ca-cert.pem") { |
| 196 | + return new Response(FAKE_CERT, { |
| 197 | + status: 200, |
| 198 | + headers: { "Content-Type": "application/x-pem-file" }, |
| 199 | + }); |
| 200 | + } |
| 201 | + return new Response("not found", { status: 404 }); |
| 202 | + }); |
| 203 | + |
| 204 | + // Pass a custom cert_path to terraform apply so the script uses it. |
| 205 | + const { id, instance } = await setupContainer({ |
| 206 | + cert_path: "/tmp/custom/certs/proxy-ca.pem", |
| 207 | + }); |
| 208 | + |
| 209 | + const exec = await execContainer(id, [ |
| 210 | + "env", |
| 211 | + `ACCESS_URL=${url}`, |
| 212 | + "SESSION_TOKEN=mock-token", |
| 213 | + "bash", |
| 214 | + "-c", |
| 215 | + instance.script, |
| 216 | + ]); |
| 217 | + expect(exec.exitCode).toBe(0); |
| 218 | + expect(exec.stdout).toContain( |
| 219 | + "AI Bridge Proxy CA certificate saved to /tmp/custom/certs/proxy-ca.pem", |
| 220 | + ); |
| 221 | + |
| 222 | + const certContent = await execContainer(id, [ |
| 223 | + "cat", |
| 224 | + "/tmp/custom/certs/proxy-ca.pem", |
| 225 | + ]); |
| 226 | + expect(certContent.stdout).toContain("BEGIN CERTIFICATE"); |
| 227 | + }); |
| 228 | + |
| 229 | + it("does not create global proxy env vars via coder_env", async () => { |
| 230 | + const state = await runTerraformApply(import.meta.dir, { |
| 231 | + agent_id: "foo", |
| 232 | + proxy_url: "https://aiproxy.example.com", |
| 233 | + }); |
| 234 | + |
| 235 | + // Proxy env vars should NOT be set globally via coder_env. |
| 236 | + // They are intended to be scoped to specific tool processes. |
| 237 | + const proxyEnvVarNames = [ |
| 238 | + "HTTP_PROXY", |
| 239 | + "HTTPS_PROXY", |
| 240 | + "NODE_EXTRA_CA_CERTS", |
| 241 | + "SSL_CERT_FILE", |
| 242 | + "REQUESTS_CA_BUNDLE", |
| 243 | + "CURL_CA_BUNDLE", |
| 244 | + ]; |
| 245 | + const proxyEnvVars = state.resources.filter( |
| 246 | + (r) => |
| 247 | + r.type === "coder_env" && |
| 248 | + r.instances.some((i) => |
| 249 | + proxyEnvVarNames.includes(i.attributes.name as string), |
| 250 | + ), |
| 251 | + ); |
| 252 | + expect(proxyEnvVars.length).toBe(0); |
| 253 | + }); |
| 254 | +}); |
0 commit comments