-
Notifications
You must be signed in to change notification settings - Fork 338
feat: Persistent multi-instance management and robust auth for fine-grained tokens #498
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
Open
letya999
wants to merge
10
commits into
zereight:main
Choose a base branch
from
letya999:feature/instance-management
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
01c95b9
feat: implement persistent instance management and robust auth for fi…
f287fe7
fix: address PR feedback (gitignore encoding, session tracking, legac…
8f4a3e6
fix: move dotenv to dependencies and allow session-specific URLs rega…
b5f0590
docs: update .env.example with cloud preset variables
f6e8b73
fix: address final P2 feedback (migration URL normalization and downl…
6a272e8
fix: route namespace tools through switched instance
e62a86b
fix: address maintainer review - auth guards, dotenv dep, neutral ali…
e3ba506
Merge remote-tracking branch 'origin/main' into feature/instance-mana…
2c8b82a
fix: address remaining PR review feedback
cd1fde3
fix: address follow-up review comments
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 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
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { spawn } from "node:child_process"; | ||
| import { once } from "node:events"; | ||
| import { describe, test, afterEach } from "node:test"; | ||
| import * as path from "node:path"; | ||
| import * as fs from "node:fs"; | ||
| import { findAvailablePort } from "./utils/server-launcher.js"; | ||
|
|
||
| const HOST = process.env.HOST || "127.0.0.1"; | ||
| const SERVER_PATH = path.resolve(process.cwd(), "build/index.js"); | ||
| const TEST_CONFIG_PATH = path.resolve(process.cwd(), "instances.test.json"); | ||
|
|
||
| const running = new Set<ReturnType<typeof spawn>>(); | ||
|
|
||
| function startServer(env: Record<string, string>, port: number) { | ||
| const child = spawn("node", [SERVER_PATH], { | ||
| env: { | ||
| ...process.env, | ||
| GITLAB_API_URL: "https://gitlab.example.com", | ||
| HOST, | ||
| PORT: String(port), | ||
| STREAMABLE_HTTP: "true", | ||
| REMOTE_AUTHORIZATION: "true", | ||
| GITLAB_MCP_OAUTH: "false", | ||
| GITLAB_PERSONAL_ACCESS_TOKEN: "glpat-master-token", | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| GITLAB_CONFIG_PATH: TEST_CONFIG_PATH, | ||
| ...env, | ||
| }, | ||
| stdio: ["ignore", "pipe", "pipe"], | ||
| }); | ||
|
|
||
| running.add(child); | ||
| child.once("exit", () => running.delete(child)); | ||
| return child; | ||
| } | ||
|
|
||
| async function waitForHealth(port: number, timeoutMs = 5000) { | ||
| const deadline = Date.now() + timeoutMs; | ||
| while (Date.now() < deadline) { | ||
| try { | ||
| const response = await fetch(`http://${HOST}:${port}/health`); | ||
| if (response.ok) return; | ||
| } catch { | ||
| // ignore | ||
| } | ||
| await new Promise(resolve => setTimeout(resolve, 100)); | ||
| } | ||
| throw new Error(`server did not become healthy on port ${port}`); | ||
| } | ||
|
|
||
| async function parseMcpResponse(response: Response) { | ||
| const contentType = response.headers.get("content-type"); | ||
| const sessionId = response.headers.get("mcp-session-id"); | ||
|
|
||
| if (contentType?.includes("text/event-stream")) { | ||
| const text = await response.text(); | ||
| const lines = text.split("\n"); | ||
| for (const line of lines) { | ||
| if (line.startsWith("data: ")) { | ||
| try { | ||
| return { result: JSON.parse(line.slice(6)), sessionId }; | ||
| } catch { | ||
| // ignore | ||
| } | ||
| } | ||
| } | ||
| return { result: null, sessionId }; | ||
| } | ||
|
|
||
| return { result: await response.json(), sessionId }; | ||
| } | ||
|
|
||
| async function callTool(port: number, sessionId: string, name: string, args: any) { | ||
| const response = await fetch(`http://${HOST}:${port}/mcp`, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| "Accept": "application/json, text/event-stream", | ||
| "Mcp-Session-Id": sessionId, | ||
| "Authorization": "Bearer glpat-this-is-a-long-enough-token-12345" | ||
| }, | ||
| body: JSON.stringify({ | ||
| jsonrpc: "2.0", | ||
| id: Math.floor(Math.random() * 1000), | ||
| method: "tools/call", | ||
| params: { | ||
| name, | ||
| arguments: args, | ||
| }, | ||
| }), | ||
| }); | ||
| return parseMcpResponse(response); | ||
| } | ||
|
|
||
| describe("Instance Management Security", () => { | ||
|
|
||
| test("disallows alias switching when REMOTE_AUTHORIZATION is active", async () => { | ||
| // Setup mock instances.test.json | ||
| fs.writeFileSync(TEST_CONFIG_PATH, JSON.stringify({ | ||
| active_alias: "default", | ||
| instances: { | ||
| secret: { | ||
| url: "https://gitlab.secret.com/api/v4", | ||
| token: "glpat-secret-token-long-enough-12345", | ||
| description: "Should not be accessible by alias in remote mode" | ||
| } | ||
| } | ||
| })); | ||
|
|
||
| try { | ||
| const port = await findAvailablePort(4400); | ||
| const child = startServer({ REMOTE_AUTHORIZATION: "true" }, port); | ||
|
|
||
| await waitForHealth(port); | ||
|
|
||
| // Initialize to get session id | ||
| const response = await fetch(`http://${HOST}:${port}/mcp`, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| "Accept": "application/json, text/event-stream", | ||
| "Authorization": "Bearer glpat-this-is-a-long-enough-token-12345" | ||
| }, | ||
| body: JSON.stringify({ | ||
| jsonrpc: "2.0", | ||
| id: 1, | ||
| method: "initialize", | ||
| params: { protocolVersion: "2025-03-26", capabilities: {}, clientInfo: { name: "test", version: "1" } }, | ||
| }), | ||
| }); | ||
| const { sessionId } = await parseMcpResponse(response); | ||
| assert.ok(sessionId, "Should have received a session id"); | ||
|
|
||
| // Try to switch by alias | ||
| const { result } = await callTool(port, sessionId!, "gitlab_switch_instance", { alias: "secret" }); | ||
|
|
||
| // In this server, some tool errors are returned as JSON-RPC errors | ||
| assert.ok(result.error, `Should have returned a JSON-RPC error. Result: ${JSON.stringify(result)}`); | ||
| assert.match(result.error.message, /Alias-based instance switching is disabled in remote\/OAuth modes/); | ||
|
|
||
| } finally { | ||
| if (fs.existsSync(TEST_CONFIG_PATH)) { | ||
| fs.unlinkSync(TEST_CONFIG_PATH); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| test("allows switching by direct apiUrl and token in remote mode", async () => { | ||
| const port = await findAvailablePort(4410); | ||
| startServer({ REMOTE_AUTHORIZATION: "true" }, port); | ||
| await waitForHealth(port); | ||
|
|
||
| const response = await fetch(`http://${HOST}:${port}/mcp`, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| "Accept": "application/json, text/event-stream", | ||
| "Authorization": "Bearer glpat-this-is-a-long-enough-token-12345" | ||
| }, | ||
| body: JSON.stringify({ | ||
| jsonrpc: "2.0", | ||
| id: 1, | ||
| method: "initialize", | ||
| params: { protocolVersion: "2025-03-26", capabilities: {}, clientInfo: { name: "test", version: "1" } }, | ||
| }), | ||
| }); | ||
| const { sessionId } = await parseMcpResponse(response); | ||
| assert.ok(sessionId, "Should have received a session id"); | ||
|
|
||
| const { result } = await callTool(port, sessionId!, "gitlab_switch_instance", { | ||
| apiUrl: "https://gitlab.custom.com", | ||
| token: "glpat-custom-token-long-enough-12345" | ||
| }); | ||
|
|
||
| if (result.error) { | ||
| throw new Error(`RPC error: ${JSON.stringify(result.error)}`); | ||
| } | ||
| const content = result.result?.content?.[0]; | ||
| assert.ok(content, `Should have returned content. Result: ${JSON.stringify(result)}`); | ||
| assert.ok(!content.isError, `Should not have returned an error content: ${content.text}`); | ||
| assert.match(content.text, /Successfully switched/); | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| for (const child of running) { | ||
| if (!child.killed) child.kill("SIGTERM"); | ||
| } | ||
| running.clear(); | ||
| if (fs.existsSync(TEST_CONFIG_PATH)) { | ||
| fs.unlinkSync(TEST_CONFIG_PATH); | ||
| } | ||
| }); | ||
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.
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.