-
Notifications
You must be signed in to change notification settings - Fork 3k
refactor(ollama): migrate auth proxy to .mts #6974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9a93975
refactor(ollama): migrate auth proxy to .mts
laitingsheng 4ef1f0a
Merge remote-tracking branch 'origin/main' into chore/6926-ollama-pro…
laitingsheng cd97347
fix(ollama): tighten auth proxy process matching
prekshivyas 4d5b0e9
Merge remote-tracking branch 'origin/main' into codex/pr-6949
prekshivyas 3a4e4c6
test(ollama): cover occupied auth proxy port
prekshivyas 1573fb5
merge(inference): reconcile Ollama adapter migration
cv 71d74ae
fix(ollama): handle backend stream aborts
cv 2ba63b1
test(ollama): keep proxy cleanup linear
cv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| #!/usr/bin/env node | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| /** | ||
| * Authenticated reverse proxy for Ollama. | ||
| * | ||
| * Ollama has no built-in authentication. This proxy sits in front of it, | ||
| * validating a Bearer token before forwarding requests. Ollama binds to | ||
| * 127.0.0.1 (localhost only) while the proxy listens on 0.0.0.0 so the | ||
| * OpenShell gateway (running in a container) can reach it. | ||
| * | ||
| * Env: | ||
| * OLLAMA_PROXY_TOKEN — required, the Bearer token to validate | ||
| * OLLAMA_PROXY_PORT — listen port (default: 11435) | ||
| * OLLAMA_BACKEND_PORT — Ollama port on localhost (default: 11434) | ||
| */ | ||
|
|
||
| import crypto from "node:crypto"; | ||
| import http from "node:http"; | ||
|
|
||
| const TOKEN = process.env.OLLAMA_PROXY_TOKEN; | ||
| if (!TOKEN) { | ||
| console.error("OLLAMA_PROXY_TOKEN required"); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const LISTEN_PORT = parseInt(process.env.OLLAMA_PROXY_PORT || "11435", 10); | ||
| const BACKEND_PORT = parseInt(process.env.OLLAMA_BACKEND_PORT || "11434", 10); | ||
|
|
||
| const server = http.createServer( | ||
| (clientReq: http.IncomingMessage, clientRes: http.ServerResponse) => { | ||
| // Every request must present a valid Bearer token. The proxy binds 0.0.0.0 | ||
| // so the OpenShell sandbox container can reach it via the docker bridge — | ||
| // which also means anything else with network reach to the host could, | ||
| // so unauthenticated requests are uniformly rejected (no health-check | ||
| // bypass for /api/tags). DevTest T5987914: "calls without | ||
| // Authorization: Bearer TOKEN should NOT return 200." See #3338. | ||
| // Compare buffers, not JS strings: a non-ASCII Authorization header | ||
| // can have the same .length as the expected string but a different byte | ||
| // length, which would make crypto.timingSafeEqual throw and crash the | ||
| // proxy (it binds 0.0.0.0). Build buffers first, gate timingSafeEqual on | ||
| // matching byte length. | ||
| const auth = clientReq.headers.authorization; | ||
| const expectedBuf = Buffer.from(`Bearer ${TOKEN}`); | ||
| const authBuf = typeof auth === "string" ? Buffer.from(auth) : null; | ||
| const tokenMatch = | ||
| authBuf !== null && | ||
| authBuf.length === expectedBuf.length && | ||
| crypto.timingSafeEqual(authBuf, expectedBuf); | ||
| if (!tokenMatch) { | ||
| clientRes.writeHead(401, { "Content-Type": "text/plain" }); | ||
| clientRes.end("Unauthorized"); | ||
| return; | ||
| } | ||
|
|
||
| // Strip the auth header before forwarding to Ollama | ||
| const headers = { ...clientReq.headers }; | ||
| delete headers.authorization; | ||
| delete headers.host; | ||
|
|
||
| const handleBackendError = (err: Error): void => { | ||
| if (clientRes.destroyed || clientRes.writableEnded) return; | ||
| if (clientRes.headersSent) { | ||
| clientRes.destroy(); | ||
| return; | ||
| } | ||
| clientRes.writeHead(502, { "Content-Type": "text/plain" }); | ||
| clientRes.end(`Ollama backend error: ${err.message}`); | ||
| }; | ||
|
|
||
| const proxyReq = http.request( | ||
| { | ||
| hostname: "127.0.0.1", | ||
| port: BACKEND_PORT, | ||
| path: clientReq.url, | ||
| method: clientReq.method, | ||
| headers, | ||
| }, | ||
| (proxyRes: http.IncomingMessage) => { | ||
| proxyRes.once("error", handleBackendError); | ||
| clientRes.writeHead(proxyRes.statusCode ?? 502, proxyRes.headers); | ||
| proxyRes.pipe(clientRes); | ||
| }, | ||
| ); | ||
|
|
||
| const destroyUpstream = (): void => { | ||
| if (!proxyReq.destroyed) proxyReq.destroy(); | ||
| }; | ||
| clientReq.once("aborted", destroyUpstream); | ||
| clientRes.once("close", () => { | ||
| if (!clientRes.writableFinished) destroyUpstream(); | ||
| }); | ||
| proxyReq.once("error", handleBackendError); | ||
|
|
||
| clientReq.pipe(proxyReq); | ||
| }, | ||
| ); | ||
|
|
||
| // The proxy binds 0.0.0.0, so an unhandled listen error (most commonly | ||
| // EADDRINUSE when the port is already taken) would crash with an uncaught | ||
| // exception. Exit cleanly with a non-zero code instead; the host-side | ||
| // startOllamaAuthProxy() detects the missing process and reports the port | ||
| // owner with remediation. See #4820. | ||
| server.on("error", (err: NodeJS.ErrnoException) => { | ||
| if (err && err.code === "EADDRINUSE") { | ||
| console.error(`Ollama auth proxy: port ${LISTEN_PORT} is already in use`); | ||
| } else { | ||
| console.error(`Ollama auth proxy failed to start: ${err && err.message ? err.message : err}`); | ||
| } | ||
| process.exit(1); | ||
| }); | ||
|
|
||
| server.listen(LISTEN_PORT, "0.0.0.0", () => { | ||
| console.log(`Ollama auth proxy listening on 0.0.0.0:${LISTEN_PORT} -> 127.0.0.1:${BACKEND_PORT}`); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.