mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-29 06:50:43 +01:00
feat(apps): add Railway connection and governed deployment tools (#13415)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Apps gives agents governed access to external resources. > - Operators need to inspect Railway services, read logs, deploy code, and run container commands. > - Railway offers hosted MCP with OAuth, but broad remote actions hide their internal operations. > - This PR adds a branded connection and fixed direct operations through the existing gateway. > - Separate SSH keys enable container commands under the same grants and policies. > - Operators can require approval for an action and inspect the resulting audit record. ## Linked Issues or Issue Description **Subsystem affected** Apps catalog, connection setup, gateway execution, and connection documentation. **Problem or motivation** Agents need Railway access through Paperclip. Operators need to grant and revoke that access, inspect available actions, and govern deployment and container operations without giving agents provider credentials. **Proposed solution** Reuse hosted MCP OAuth, vault storage, catalog discovery, grants, and the gateway. Probe the actual credential before enabling fixed GraphQL operations. Use a dedicated grant-owned SSH key for bounded container commands. **Alternatives considered** A catalog entry alone cannot execute the missing operations. The hosted general agent has opaque internal effects. An unrestricted CLI runtime can bypass action policy and inherit ambient credentials. **Roadmap alignment** This extends the existing MCP Tool Gateway & Apps path and the Connected Apps direction in ROADMAP.md. It does not add a plugin or parallel connection service. Related PRs #311, #939, and #7861 concern hosting Paperclip on Railway. They do not add this outbound Apps connection. The separate shared agent-picker fix is #13414 and is not included here. ## What Changed - Add the generated Railway catalog entry, official marks, provenance, and OAuth setup guidance. - Add fixed service/deployment status, bounded logs, and redeploy/restart/rollback tools. Block source deployment until the provider can atomically bind the approved repository and commit. - Verify API access with an explicit workspace before exposing direct tools. - Add grant-owned SSH key setup and a bounded runner with host verification, target checks, isolated state, and cleanup. - Block the opaque hosted railway-agent and accept-deploy actions. Preserve normal Allowed defaults and Ask-first policies for other actions. - Quarantine new or changed Railway schemas after initial discovery, including reconnect. - Add provider, lifecycle, gateway, SSH, UI, and browser fixtures. Document setup, limitations, and the release checklist. ## Verification - Security follow-up: removed the unsafe source-deployment mutation. Direct calls and old active catalog entries are denied before any upstream request, including normalized aliases. Refresh marks retired entries disabled. All 386 focused Railway, catalog and gateway tests passed, and server TypeScript checking passed. Full [GitHub CI](https://github.com/paperclipai/paperclip/actions/runs/35139421144) passed ond86530ab9, including typecheck, build, all tests, runner checks, and browser tests. Superagent passed and confirmed the P2 fix. Greptile reviewed the same commit at 5/5 with no findings. - CI follow-up: fixed the missing Railway SSH operation in the OpenAPI document, including its request schema, operator-only authentication, and error responses. The failure reproduced locally before the fix; all 403 selected API, Railway, catalog, and artwork tests passed after it. Synced current master and resolved the catalog/artwork conflicts. - After rebase: 440 focused provider, lifecycle, gateway, catalog, and container-panel tests passed. AppDetail and AppsConnect passed another 196 tests. - Full typecheck, build, token gates, and the gallery browser check passed after rebase. - During implementation, full build and the gallery browser check passed. Shared generic-MCP fixtures covered OAuth callback/state/issuer binding and failure paths. - Local live consent and tools/list succeeded. There were 44 active hosted actions and two blocked actions. A workspace-bound API probe and direct project/service/environment reads succeeded. The inspected project had no deployed services. No provider mutation ran. - Full GitHub CI passed on commit303340f19, including all server/workspace test groups, typecheck, build, runtime verification, release dry run, and browser tests. The original local full-run attempt was incomplete; the complete automated suite is now verified in CI. Manual review: connect Railway, review the actual actions, install for an agent, and run a resource read through the gateway. Choose Ask first before testing a deployment mutation. Configure a dedicated key only when container access is needed. **Release qualification is still open.** Live agent gateway reads/logs, rejected and approved deployment calls, refresh/revoke, public HTTPS consent, and SSH enrollment/commands/cleanup need an authorized disposable service. The passing API diagnostic does not replace those tests. See doc/connections/RAILWAY.md and RAILWAY-REVIEW.md. ## Risks Overall risk is medium. New runtime behavior is gated to Railway connections, but the PR changes shared catalog, credential lifecycle, and gateway code. A regression in those paths can affect other Apps connections. The highest-impact operations are Railway deployments and container commands. - Provider consent can authorize an entire workspace. Catalog labels are not local resource allowlists. Direct tools check target membership, and provider permissions still apply. - Shell commands have broad internal authority. Action policy cannot approve each internal shell step. Timeouts close the local connection but cannot guarantee remote child-process termination. - Log and command output may contain application secrets that pattern redaction cannot recognize. - Source deployment is unavailable until the provider supports atomic repository/commit binding. Existing deployments can still be redeployed, restarted or rolled back. - No database migration is required. Rollback can remove promotion and direct dispatch while preserving connection data and the generic MCP path. - Live Railway qualification must still pass before release acceptance. ## Model Used OpenAI Codex, based on GPT-6, with code execution and browser testing. An independent read-only security agent reviewed the local implementation. The exact serving model ID and context window were not exposed in this session. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
d0b67bfe71
commit
6fe8e30625
31 files changed
+1807
-91
No files matched your search
@@ -0,0 +1,35 @@
|
||||
// Public discovery metadata observed 2026-09-13. Tool descriptors below are
|
||||
// deterministic examples of documented tools, not an authenticated tools/list capture.
|
||||
export const railwayResourceMetadata = {
|
||||
resource: "https://mcp.railway.com",
|
||||
authorization_servers: ["https://backboard.railway.com"],
|
||||
scopes_supported: ["openid", "profile", "email", "offline_access", "workspace:member"],
|
||||
bearer_methods_supported: ["header"],
|
||||
};
|
||||
export const railwayAuthorizationMetadata = {
|
||||
issuer: "https://backboard.railway.com",
|
||||
authorization_endpoint: "https://backboard.railway.com/oauth/auth?resource=https%3A%2F%2Fbackboard.railway.com",
|
||||
token_endpoint: "https://backboard.railway.com/oauth/token",
|
||||
registration_endpoint: "https://backboard.railway.com/oauth/register",
|
||||
scopes_supported: railwayResourceMetadata.scopes_supported,
|
||||
response_types_supported: ["code"],
|
||||
grant_types_supported: ["authorization_code", "refresh_token", "urn:ietf:params:oauth:grant-type:device_code"],
|
||||
token_endpoint_auth_methods_supported: ["client_secret_basic", "client_secret_post", "none", "private_key_jwt"],
|
||||
code_challenge_methods_supported: ["S256"],
|
||||
};
|
||||
export const target = {
|
||||
projectId: "11111111-1111-4111-8111-111111111111",
|
||||
environmentId: "22222222-2222-4222-8222-222222222222",
|
||||
serviceId: "33333333-3333-4333-8333-333333333333",
|
||||
deploymentId: "44444444-4444-4444-8444-444444444444",
|
||||
};
|
||||
export const instanceId = "55555555-5555-4555-8555-555555555555";
|
||||
export const targetData = {
|
||||
project: { id: target.projectId },
|
||||
environment: { id: target.environmentId, projectId: target.projectId },
|
||||
service: { id: target.serviceId, projectId: target.projectId },
|
||||
serviceInstance: { environmentId: target.environmentId, serviceId: target.serviceId, source: { repo: "example/app" } },
|
||||
};
|
||||
export const deploymentData = {
|
||||
deployment: { ...target, id: target.deploymentId, status: "SUCCESS", canRedeploy: true, canRollback: true, instances: [{ id: instanceId }] },
|
||||
};
|
||||
@@ -0,0 +1,198 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import { agents, approvals, companies, companyMemberships, companySecrets, connectionGrants, createDb, heartbeatRuns, issues, toolCatalogEntries, toolActionRequests, toolConnections, toolPolicies, toolProfileBindings, toolProfiles, toolAccessAuditEvents } from "@paperclipai/db";
|
||||
import { toolAccessService } from "../services/tool-access.js";
|
||||
import { createToolGatewayService } from "../services/tool-gateway.js";
|
||||
import { RAILWAY_API_URL, RAILWAY_MCP_URL, RAILWAY_QUERIES } from "../services/railway.js";
|
||||
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";
|
||||
import { deploymentData, railwayAuthorizationMetadata, railwayResourceMetadata, target, targetData } from "./fixtures/railway/provider.js";
|
||||
const support = await getEmbeddedPostgresTestSupport();
|
||||
const actor = { actorType: "user" as const, actorId: "railway-reviewer" };
|
||||
const redirectUri = "http://localhost:3100/api/tools/oauth/callback";
|
||||
const token = "railway-fixture-opaque-access-token";
|
||||
const initialTools = [
|
||||
{ name: "list-projects", inputSchema: { type: "object", properties: {} }, annotations: { readOnlyHint: true } },
|
||||
{ name: "redeploy", inputSchema: { type: "object", properties: { deploymentId: { type: "string" } } }, annotations: { readOnlyHint: true } },
|
||||
{ name: "railway-agent", annotations: { readOnlyHint: true } },
|
||||
{ name: "accept-deploy", annotations: { readOnlyHint: true } },
|
||||
];
|
||||
|
||||
(support.supported ? describe : describe.skip)("Railway connection lifecycle and gateway", () => {
|
||||
let db: ReturnType<typeof createDb>;
|
||||
let temp: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
|
||||
beforeAll(async () => { temp = await startEmbeddedPostgresTestDatabase("paperclip-railway-"); db = createDb(temp.connectionString); }, 20000);
|
||||
afterAll(async () => { await temp?.cleanup(); });
|
||||
|
||||
async function fixture() {
|
||||
const [company] = await db.insert(companies).values({ name: "Railway fixture", issuePrefix: `RW${randomUUID().slice(0, 6)}` }).returning();
|
||||
await db.insert(companyMemberships).values({ companyId: company.id, principalType: "user", principalId: actor.actorId, status: "active", membershipRole: "admin" });
|
||||
let tools: unknown[] = [...initialTools];
|
||||
let apiStatus = 200;
|
||||
let tokenStatus = 200;
|
||||
const request = vi.fn(async (url: string, init: RequestInit) => {
|
||||
const body = init.body ? String(init.body) : "";
|
||||
if (url === RAILWAY_API_URL) {
|
||||
if (apiStatus !== 200) return new Response("private provider error", { status: apiStatus });
|
||||
const { query, variables } = JSON.parse(body);
|
||||
// Workspace-scoped OAuth rejects account-wide projects even with HTTP 200.
|
||||
if (query === RAILWAY_QUERIES.projects && !variables?.workspaceId) return Response.json({ errors: [{ message: "Not Authorized", extensions: { code: "INTERNAL_SERVER_ERROR" } }], data: null });
|
||||
return Response.json({ data: query === RAILWAY_QUERIES.target ? targetData : query === RAILWAY_QUERIES.deployment ? deploymentData : query === RAILWAY_QUERIES.restart ? { deploymentRestart: true } : { projects: { edges: [] } } });
|
||||
}
|
||||
if (url.replace(/\/$/, "") === RAILWAY_MCP_URL && init.method === "POST") {
|
||||
if (new Headers(init.headers).get("authorization") !== `Bearer ${token}`) return new Response("", { status: 401, headers: { "www-authenticate": 'Bearer resource_metadata="https://mcp.railway.com/.well-known/oauth-protected-resource"' } });
|
||||
if (JSON.parse(body).method === "tools/call") {
|
||||
expect(JSON.parse(body).params).toEqual({ name: "list-workspaces", arguments: {} });
|
||||
return Response.json({ jsonrpc: "2.0", id: "paperclip-railway-workspace-probe", result: { structuredContent: { workspaces: [{ id: target.projectId }] } } });
|
||||
}
|
||||
return Response.json({ jsonrpc: "2.0", id: "paperclip-catalog-refresh", result: { tools } });
|
||||
}
|
||||
if (url.includes("oauth-protected-resource")) return Response.json(railwayResourceMetadata);
|
||||
if (url.includes("oauth-authorization-server")) return Response.json(railwayAuthorizationMetadata);
|
||||
if (url === railwayAuthorizationMetadata.registration_endpoint) return Response.json({ ...JSON.parse(body), client_id: "railway-fixture-client" });
|
||||
if (url === railwayAuthorizationMetadata.token_endpoint) return tokenStatus === 200 ? Response.json({ access_token: token, refresh_token: "railway-fixture-refresh", expires_in: 3600, token_type: "Bearer" }) : Response.json({ error: "invalid_grant", error_description: "private provider message" }, { status: tokenStatus });
|
||||
return new Response("", { status: 404 });
|
||||
});
|
||||
const service = toolAccessService(db, { remoteHttpRequest: request, remoteHttpEndpointLookup: async () => [{ address: "8.8.8.8", family: 4 }] });
|
||||
const connection = await service.connectGalleryApp(company.id, { galleryKey: "railway", methodKey: "mcp-oauth", name: "Railway" }, actor);
|
||||
const start = await service.startOAuth(company.id, connection.connectionId, { redirectUri, actor });
|
||||
const url = new URL(start.authorizationUrl);
|
||||
expect(start.registrationSource).toBe("dcr");
|
||||
expect(url.searchParams.get("prompt")).toBe("consent");
|
||||
expect(url.searchParams.get("scope")).toContain("offline_access");
|
||||
expect(url.searchParams.get("code_challenge_method")).toBe("S256");
|
||||
expect(url.searchParams.get("resource")).toBe(RAILWAY_MCP_URL);
|
||||
await service.completeOAuthCallback({ state: url.searchParams.get("state")!, code: "fixture-code", iss: railwayAuthorizationMetadata.issuer, redirectUri, actor });
|
||||
return { company, service, connectionId: connection.connectionId, request, setTools: (next: unknown[]) => { tools = next; }, setApiStatus: (next: number) => { apiStatus = next; }, setTokenStatus: (next: number) => { tokenStatus = next; } };
|
||||
}
|
||||
|
||||
it("discovers direct tools, blocks opaque actions and quarantines changed tools even on reconnect", async () => {
|
||||
const f = await fixture();
|
||||
const rows = await f.service.listCatalog(f.connectionId);
|
||||
expect((await f.service.getConnection(f.connectionId))?.config?.railwayApiStatus).toBe("available");
|
||||
expect(rows.find((r) => r.toolName === "paperclip-railway-read-logs")?.status).toBe("active");
|
||||
expect(rows.find((r) => r.toolName === "redeploy")?.riskLevel).toBe("destructive");
|
||||
expect(rows.filter((r) => ["railway-agent", "accept-deploy"].includes(r.toolName)).every((r) => r.status === "disabled")).toBe(true);
|
||||
f.setTools([...initialTools.map((tool) => tool.name === "list-projects" ? { ...tool, inputSchema: { type: "object", properties: { changed: { type: "string" } } } } : tool), { name: "new-tool" }]);
|
||||
await f.service.refreshCatalog(f.connectionId, actor);
|
||||
const changed = await f.service.listCatalog(f.connectionId);
|
||||
expect(changed.find((r) => r.toolName === "list-projects")?.status).toBe("quarantined");
|
||||
expect(changed.find((r) => r.toolName === "new-tool")?.status).toBe("quarantined");
|
||||
const start = await f.service.startOAuth(f.company.id, f.connectionId, { redirectUri, actor });
|
||||
await f.service.completeOAuthCallback({ state: new URL(start.authorizationUrl).searchParams.get("state")!, code: "reconnect-code", iss: railwayAuthorizationMetadata.issuer, redirectUri, actor });
|
||||
const after = await f.service.listCatalog(f.connectionId);
|
||||
expect(after.find((r) => r.toolName === "new-tool")?.status).toBe("quarantined");
|
||||
expect(JSON.stringify(await f.service.getConnection(f.connectionId))).not.toContain(token);
|
||||
f.setTools([{ name: "paperclip_railway_restart" }]);
|
||||
await expect(f.service.refreshCatalog(f.connectionId, actor)).rejects.toThrow("Railway advertised a reserved Paperclip action");
|
||||
});
|
||||
|
||||
it("keeps direct operations unavailable when API acceptance fails and reports refresh failure safely", async () => {
|
||||
const f = await fixture();
|
||||
f.setApiStatus(403);
|
||||
await f.service.refreshCatalog(f.connectionId, actor);
|
||||
expect((await f.service.getConnection(f.connectionId))?.config?.railwayApiStatus).toBe("unavailable");
|
||||
const [grant] = await db.select().from(connectionGrants).where(eq(connectionGrants.connectionId, f.connectionId));
|
||||
f.setTokenStatus(400);
|
||||
let failure: unknown;
|
||||
try { await f.service.refreshOAuthGrantCredentials({ companyId: f.company.id, connectionId: f.connectionId, grantId: grant.id, forceRefresh: true, actor }); }
|
||||
catch (error) { failure = error; }
|
||||
expect(failure).toBeTruthy();
|
||||
expect(String(failure)).not.toContain("private provider message");
|
||||
expect(JSON.stringify(await f.service.getConnection(f.connectionId))).not.toContain(token);
|
||||
});
|
||||
|
||||
it("denies retired source-deployment entries before refresh and disables them on refresh", async () => {
|
||||
const f = await fixture();
|
||||
const [agent] = await db.insert(agents).values({ companyId: f.company.id, name: "Railway operator", role: "engineer", adapterType: "process", adapterConfig: {} }).returning();
|
||||
const [run] = await db.insert(heartbeatRuns).values({ companyId: f.company.id, agentId: agent.id, invocationSource: "on_demand", status: "running" }).returning();
|
||||
const [profile] = await db.insert(toolProfiles).values({ companyId: f.company.id, name: "Railway tools", profileKey: randomUUID(), defaultAction: "allow" }).returning();
|
||||
await db.insert(toolProfileBindings).values({ companyId: f.company.id, profileId: profile.id, targetType: "agent", targetId: agent.id });
|
||||
const [existing] = await db.select().from(toolCatalogEntries).where(and(eq(toolCatalogEntries.connectionId, f.connectionId), eq(toolCatalogEntries.toolName, "paperclip-railway-restart")));
|
||||
const names = ["paperclip-railway-deploy-revision", "paperclip_railway_deploy_revision", "paperclipRailwayDeployRevision"];
|
||||
// Reproduce active catalog rows persisted by an older server, before refresh.
|
||||
await db.insert(toolCatalogEntries).values(names.map((name) => ({ ...existing, id: randomUUID(), name, toolName: name, inputSchema: { type: "object" } })));
|
||||
const gateway = createToolGatewayService(db, { remoteHttpRequest: f.request });
|
||||
const session = await gateway.createSession({ companyId: f.company.id, agentId: agent.id, runId: run.id });
|
||||
const listed = await gateway.listToolsForSession(session.token);
|
||||
expect(listed.some((tool) => names.includes(tool.upstreamToolName ?? ""))).toBe(false);
|
||||
const restart = listed.find((tool) => tool.upstreamToolName === "paperclip-railway-restart")!;
|
||||
expect(restart).toBeTruthy();
|
||||
f.request.mockClear();
|
||||
const { deploymentId: _, ...ids } = target;
|
||||
const parameters = { ...ids, repository: "example/app", commitSha: "a".repeat(40) };
|
||||
await expect(gateway.executeTool({ sessionToken: session.token, tool: restart.name.replace("paperclip-railway-restart", names[0]), parameters, idempotencyKey: randomUUID() })).rejects.toMatchObject({ reasonCode: "tool_not_found" });
|
||||
for (const toolName of names) {
|
||||
await expect(gateway.executeTestCall({ companyId: f.company.id, connectionId: f.connectionId, agentId: agent.id, userId: actor.actorId, toolName, parameters })).rejects.toMatchObject({ reasonCode: "tool_not_found" });
|
||||
}
|
||||
expect(f.request).not.toHaveBeenCalled();
|
||||
await f.service.refreshCatalog(f.connectionId, actor);
|
||||
const retired = (await f.service.listCatalog(f.connectionId)).filter((entry) => names.includes(entry.toolName));
|
||||
expect(retired).toHaveLength(names.length);
|
||||
expect(retired.every((entry) => entry.status === "disabled")).toBe(true);
|
||||
});
|
||||
|
||||
it("preserves the dedicated SSH grant key on reconnect and removes it while disconnected", async () => {
|
||||
const f = await fixture();
|
||||
const [grant] = await db.select().from(connectionGrants).where(eq(connectionGrants.connectionId, f.connectionId));
|
||||
const setup = await f.service.configureRailwaySsh(f.connectionId, f.company.id, { action: "prepare", grantId: grant.id }, actor);
|
||||
expect(setup?.publicKey).toMatch(/^ssh-ed25519 /);
|
||||
const [keyGrant] = await db.select().from(connectionGrants).where(eq(connectionGrants.id, grant.id));
|
||||
const ref = keyGrant.credentialSecretRefs.find((r) => r.configPath === "railway.ssh_private_key")!;
|
||||
expect(ref).toBeTruthy();
|
||||
const start = await f.service.startOAuth(f.company.id, f.connectionId, { redirectUri, actor });
|
||||
await f.service.completeOAuthCallback({ state: new URL(start.authorizationUrl).searchParams.get("state")!, code: "reconnect", iss: railwayAuthorizationMetadata.issuer, redirectUri, actor });
|
||||
const [reconnected] = await db.select().from(connectionGrants).where(eq(connectionGrants.id, grant.id));
|
||||
expect(reconnected.credentialSecretRefs).toContainEqual(ref);
|
||||
await f.service.revokeConnectionGrant(f.connectionId, grant.id, actor);
|
||||
await db.update(toolConnections).set({ status: "disabled" }).where(eq(toolConnections.id, f.connectionId));
|
||||
await f.service.configureRailwaySsh(f.connectionId, f.company.id, { action: "remove", grantId: grant.id }, actor);
|
||||
expect(await db.select().from(companySecrets).where(eq(companySecrets.id, ref.secretId))).toHaveLength(0);
|
||||
expect((await f.service.getConnection(f.connectionId))?.config?.railwaySsh).toBeNull();
|
||||
});
|
||||
|
||||
it("enforces policy, grant, run and company boundaries before API execution", async () => {
|
||||
const f = await fixture();
|
||||
const [agent] = await db.insert(agents).values({ companyId: f.company.id, name: "Railway operator", role: "engineer", adapterType: "process", adapterConfig: {} }).returning();
|
||||
const [issue] = await db.insert(issues).values({ companyId: f.company.id, title: "Railway proof", assigneeAgentId: agent.id }).returning();
|
||||
const [run] = await db.insert(heartbeatRuns).values({ companyId: f.company.id, agentId: agent.id, invocationSource: "on_demand", status: "running", contextSnapshot: { issueId: issue.id } }).returning();
|
||||
const [profile] = await db.insert(toolProfiles).values({ companyId: f.company.id, name: "Railway tools", profileKey: randomUUID(), defaultAction: "allow" }).returning();
|
||||
await db.insert(toolProfileBindings).values({ companyId: f.company.id, profileId: profile.id, targetType: "agent", targetId: agent.id });
|
||||
const gateway = createToolGatewayService(db, { remoteHttpRequest: f.request, toolActionSigningSecret: "railway-fixture-signing-key" });
|
||||
let session = await gateway.createSession({ companyId: f.company.id, agentId: agent.id, runId: run.id });
|
||||
const tool = (await gateway.listToolsForSession(session.token)).find((r) => r.upstreamToolName === "paperclip-railway-restart")!;
|
||||
expect(tool).toBeTruthy();
|
||||
const [policy] = await db.insert(toolPolicies).values({ companyId: f.company.id, name: "Approve Railway restart", policyType: "require_approval", selectors: { connectionId: f.connectionId }, priority: 10 }).returning();
|
||||
f.request.mockClear();
|
||||
await expect(gateway.executeTool({ sessionToken: session.token, tool: tool.name, parameters: target, idempotencyKey: randomUUID() })).rejects.toMatchObject({ reasonCode: "approval_required" });
|
||||
expect(f.request).not.toHaveBeenCalled();
|
||||
const [pending] = await db.select().from(toolActionRequests).where(eq(toolActionRequests.companyId, f.company.id));
|
||||
await gateway.declineActionRequest({ companyId: f.company.id, actionRequestId: pending.id, actor: { userId: actor.actorId } });
|
||||
expect(f.request).not.toHaveBeenCalled();
|
||||
await expect(gateway.executeTool({ sessionToken: session.token, tool: tool.name, parameters: target, idempotencyKey: randomUUID() })).rejects.toMatchObject({ status: 409 });
|
||||
expect(f.request).not.toHaveBeenCalled();
|
||||
const [nextIssue] = await db.insert(issues).values({ companyId: f.company.id, title: "New Railway operation", assigneeAgentId: agent.id }).returning();
|
||||
const [nextRun] = await db.insert(heartbeatRuns).values({ companyId: f.company.id, agentId: agent.id, invocationSource: "on_demand", status: "running", contextSnapshot: { issueId: nextIssue.id } }).returning();
|
||||
session = await gateway.createSession({ companyId: f.company.id, agentId: agent.id, runId: nextRun.id });
|
||||
await db.update(toolPolicies).set({ policyType: "block" }).where(eq(toolPolicies.id, policy.id));
|
||||
await expect(gateway.executeTool({ sessionToken: session.token, tool: tool.name, parameters: target, idempotencyKey: randomUUID() })).rejects.toMatchObject({ status: 403 });
|
||||
expect(f.request).not.toHaveBeenCalled();
|
||||
await db.update(toolPolicies).set({ policyType: "require_approval" }).where(eq(toolPolicies.id, policy.id));
|
||||
await expect(gateway.executeTool({ sessionToken: session.token, tool: tool.name, parameters: target, idempotencyKey: randomUUID() })).rejects.toMatchObject({ reasonCode: "approval_required" });
|
||||
const [approved] = await db.select().from(toolActionRequests).where(and(eq(toolActionRequests.companyId, f.company.id), eq(toolActionRequests.status, "pending")));
|
||||
if (approved.approvalId) await db.update(approvals).set({ status: "approved", decidedByUserId: actor.actorId, decidedAt: new Date() }).where(eq(approvals.id, approved.approvalId));
|
||||
await gateway.approveActionRequest({ companyId: f.company.id, actionRequestId: approved.id, actor: { userId: actor.actorId } });
|
||||
expect(f.request.mock.calls.some(([, init]) => JSON.parse(String(init.body)).query === RAILWAY_QUERIES.restart)).toBe(true);
|
||||
await db.delete(toolPolicies).where(eq(toolPolicies.id, policy.id));
|
||||
expect(f.request.mock.calls.filter(([, init]) => String(init.body).includes("mutation"))).toHaveLength(1);
|
||||
expect(JSON.stringify(await db.select().from(toolAccessAuditEvents).where(eq(toolAccessAuditEvents.companyId, f.company.id)))).not.toContain(token);
|
||||
f.request.mockClear();
|
||||
const [revokedGrant] = await db.select().from(connectionGrants).where(eq(connectionGrants.connectionId, f.connectionId));
|
||||
await f.service.revokeConnectionGrant(f.connectionId, revokedGrant.id, actor);
|
||||
await expect(gateway.executeTool({ sessionToken: session.token, tool: tool.name, parameters: { ...target, deploymentId: randomUUID() }, idempotencyKey: randomUUID() })).rejects.toMatchObject({ status: 409, reasonCode: "organization_authorization_required" });
|
||||
expect(f.request).not.toHaveBeenCalled();
|
||||
await expect(gateway.createSession({ companyId: randomUUID(), agentId: agent.id, runId: run.id })).rejects.toThrow();
|
||||
await db.update(heartbeatRuns).set({ status: "succeeded" }).where(eq(heartbeatRuns.id, nextRun.id));
|
||||
await expect(gateway.executeTool({ sessionToken: session.token, tool: tool.name, parameters: target, idempotencyKey: randomUUID() })).rejects.toMatchObject({ status: 401, reasonCode: "session_run_inactive" });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,69 @@
|
||||
import { EventEmitter } from "node:events";
|
||||
import { PassThrough } from "node:stream";
|
||||
import { access, readFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { instanceId } from "./fixtures/railway/provider.js";
|
||||
const { spawnMock } = vi.hoisted(() => ({ spawnMock: vi.fn() }));
|
||||
vi.mock("node:child_process", async (original) => ({ ...await original<typeof import("node:child_process")>(), spawn: spawnMock }));
|
||||
import { generateRailwaySshKey, railwaySshArguments, runRailwaySshCommand, validateRailwayKnownHosts } from "../services/railway-ssh.js";
|
||||
|
||||
const host = "ssh.railway.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFixture";
|
||||
function input(signal = new AbortController().signal) { return { deploymentInstanceId: instanceId, command: "printf 'hello'", timeoutSeconds: 1, privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----\nfixture\n", knownHosts: host, signal }; }
|
||||
function fakeProcess(action: (child: EventEmitter & { stdout: PassThrough; stderr: PassThrough; stdin: PassThrough; kill: ReturnType<typeof vi.fn> }, script: string) => void) {
|
||||
spawnMock.mockImplementation(() => {
|
||||
const child = Object.assign(new EventEmitter(), { stdout: new PassThrough(), stderr: new PassThrough(), stdin: new PassThrough(), kill: vi.fn(() => { queueMicrotask(() => child.emit("close", null)); return true; }) });
|
||||
let script = "";
|
||||
child.stdin.on("data", (chunk) => { script += chunk; });
|
||||
child.stdin.on("finish", () => action(child, script));
|
||||
return child;
|
||||
});
|
||||
}
|
||||
afterEach(() => { vi.clearAllMocks(); });
|
||||
|
||||
describe("Railway isolated container command runner", () => {
|
||||
it("generates a fresh real key without retaining files", async () => {
|
||||
const key = await generateRailwaySshKey();
|
||||
expect(key.publicKey).toMatch(/^ssh-ed25519 /);
|
||||
expect(key.privateKey).toMatch(/^-----BEGIN OPENSSH PRIVATE KEY-----/);
|
||||
});
|
||||
it("rejects untrusted aliases and ambient SSH state", () => {
|
||||
for (const line of ["* ssh-ed25519 AAAA", "evil.test ssh-ed25519 AAAA", `${host}\nHost *`, "@cert-authority " + host]) expect(() => validateRailwayKnownHosts(line)).toThrow();
|
||||
const args = railwaySshArguments("/tmp/dedicated", instanceId);
|
||||
expect(args).toEqual(expect.arrayContaining(["/dev/null", "IdentityAgent=none", "StrictHostKeyChecking=yes", "IdentitiesOnly=yes", "ForwardAgent=no", "ControlPath=none"]));
|
||||
expect(args.slice(-3)).toEqual(["--", `${instanceId}@ssh.railway.com`, "sh -s"]);
|
||||
});
|
||||
it("requires remote completion and cleans its isolated directory", async () => {
|
||||
fakeProcess((child, script) => {
|
||||
expect(script).toContain("</dev/null");
|
||||
const marker = script.match(/paperclip_railway_completed_[a-f0-9]+/)![0];
|
||||
child.stdout.write(`hello\n${marker}:7\n`);
|
||||
child.emit("close", 7);
|
||||
});
|
||||
await expect(runRailwaySshCommand(input())).resolves.toMatchObject({ exitCode: 7, stdout: "hello", timedOut: false, truncated: false });
|
||||
const args = spawnMock.mock.calls[0][1] as string[];
|
||||
const keyPath = args[args.indexOf("-i") + 1];
|
||||
await expect(access(path.dirname(keyPath))).rejects.toThrow();
|
||||
expect(spawnMock.mock.calls[0][2].env).toEqual({ PATH: "/usr/bin:/bin", LANG: "C.UTF-8" });
|
||||
});
|
||||
it("never treats local success or stdin failure as confirmed remote success", async () => {
|
||||
fakeProcess((child) => { child.stdin.emit("error", new Error("EPIPE")); child.emit("close", 0); });
|
||||
await expect(runRailwaySshCommand(input())).rejects.toMatchObject({ code: "railway_ssh_command_unconfirmed" });
|
||||
});
|
||||
it("kills commands that exceed the output limit", async () => {
|
||||
fakeProcess((child) => { child.stdout.write("x".repeat(70000)); });
|
||||
const result = await runRailwaySshCommand(input());
|
||||
expect(result.truncated).toBe(true);
|
||||
expect(Buffer.byteLength(result.stdout)).toBe(65536);
|
||||
});
|
||||
it("kills on timeout and cancellation and still removes private material", async () => {
|
||||
fakeProcess(() => {});
|
||||
const result = await runRailwaySshCommand(input());
|
||||
expect(result.timedOut).toBe(true);
|
||||
const controller = new AbortController();
|
||||
fakeProcess(() => controller.abort());
|
||||
await expect(runRailwaySshCommand(input(controller.signal))).rejects.toMatchObject({ name: "AbortError" });
|
||||
const args = spawnMock.mock.calls.at(-1)![1] as string[];
|
||||
await expect(readFile(args[args.indexOf("-i") + 1])).rejects.toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,153 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { railwayCommandBudgetMs, createRailwayClient, discoverRailwayWorkspace, isRailwayConnection, isRailwayEndpoint, isRailwayToolBlocked, RAILWAY_API_URL, RAILWAY_MCP_URL, RAILWAY_QUERIES, RAILWAY_TOOLS, railwayRisk } from "../services/railway.js";
|
||||
import { deploymentData, instanceId, target, targetData } from "./fixtures/railway/provider.js";
|
||||
|
||||
function fixture(responder?: (query: string) => Response | undefined) {
|
||||
const request = vi.fn(async (_url: string, init: RequestInit) => {
|
||||
const { query } = JSON.parse(String(init.body));
|
||||
return responder?.(query) ?? Response.json({ data: query === RAILWAY_QUERIES.target ? targetData : query === RAILWAY_QUERIES.deployment ? deploymentData : { deploymentRestart: true } });
|
||||
});
|
||||
const runCommand = vi.fn(async () => ({ exitCode: 0, stdout: "ok", stderr: "" }));
|
||||
const controller = new AbortController();
|
||||
return { request, runCommand, controller, client: createRailwayClient({ authorization: "Bearer railway-fixture-secret", request, runCommand, signal: controller.signal }) };
|
||||
}
|
||||
|
||||
describe("Railway governed operations", () => {
|
||||
it("binds project listing and the access probe to an explicit workspace", async () => {
|
||||
const f = fixture(() => Response.json({ data: { projects: { edges: [] } } }));
|
||||
await expect(f.client.call("paperclip-railway-list-projects", {})).rejects.toMatchObject({ code: "railway_invalid_arguments" });
|
||||
expect(f.request).not.toHaveBeenCalled();
|
||||
await f.client.probe(target.projectId);
|
||||
await f.client.call("paperclip-railway-list-projects", { workspaceId: target.projectId, first: 5 });
|
||||
expect(f.request.mock.calls.map(([, init]) => JSON.parse(String(init.body)).variables)).toEqual([
|
||||
{ workspaceId: target.projectId, first: 1 }, { workspaceId: target.projectId, first: 5 },
|
||||
]);
|
||||
expect(RAILWAY_QUERIES.projects).toContain("projects(workspaceId:$workspaceId,");
|
||||
});
|
||||
|
||||
it.each(["structured", "sse"])("discovers workspace access from the hosted %s response without account profile scopes", async (format) => {
|
||||
const data = { workspaces: [{ id: target.projectId }] };
|
||||
const payload = { jsonrpc: "2.0", id: "paperclip-railway-workspace-probe", result: format === "structured" ? { structuredContent: data } : { content: [{ type: "text", text: JSON.stringify(data) }] } };
|
||||
const request = vi.fn(async () => format === "structured" ? Response.json(payload) : new Response(`data: ${JSON.stringify(payload)}\n\n`, { headers: { "content-type": "text/event-stream" } }));
|
||||
await expect(discoverRailwayWorkspace({ authorization: "Bearer fixture", request, signal: new AbortController().signal })).resolves.toBe(target.projectId);
|
||||
expect(request).toHaveBeenCalledWith(RAILWAY_MCP_URL, expect.objectContaining({ redirect: "error", body: expect.stringContaining('"name":"list-workspaces"') }));
|
||||
});
|
||||
|
||||
it("keeps operations unavailable for empty or failed workspace discovery without exposing provider output", async () => {
|
||||
for (const result of [{ structuredContent: { workspaces: [] } }, { isError: true, content: [{ type: "text", text: "private provider details" }] }]) {
|
||||
const request = vi.fn(async () => Response.json({ result }));
|
||||
await expect(discoverRailwayWorkspace({ authorization: "Bearer fixture", request, signal: new AbortController().signal })).rejects.toThrow(/Railway/);
|
||||
}
|
||||
});
|
||||
|
||||
it("recognizes Railway's HTTP 200 authorization errors without echoing provider details", async () => {
|
||||
const f = fixture(() => Response.json({ errors: [{ message: "Not Authorized", extensions: { code: "INTERNAL_SERVER_ERROR", private: "provider secret" } }], data: null }));
|
||||
await expect(f.client.probe(target.projectId)).rejects.toMatchObject({ code: "railway_api_authorization_required", status: 403, message: expect.stringContaining("workspace selected during consent") });
|
||||
expect(f.request).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
it("requires exact provider identity and treats shell and remote agents as privileged", () => {
|
||||
expect(isRailwayEndpoint("https://mcp.railway.com/")).toBe(true);
|
||||
for (const url of ["https://mcp.railway.com/path", "https://mcp.railway.com?token=x", "https://mcp.railway.com.evil.test", "http://mcp.railway.com"]) expect(isRailwayEndpoint(url)).toBe(false);
|
||||
expect(isRailwayConnection({ transport: "mcp_remote", authKind: "oauth", credentialSource: "paperclip_vault", config: { url: "https://mcp.railway.com", sourceTemplateKey: "railway", connectionMethodKey: "mcp-oauth" } })).toBe(true);
|
||||
expect(railwayRisk("railwayAgent")).toBe("destructive");
|
||||
expect(isRailwayToolBlocked("accept_deploy")).toBe(true);
|
||||
expect(railwayRisk("paperclip-railway-run-command")).toBe("destructive");
|
||||
expect(railwayRisk("unfamiliar-tool")).toBe("write");
|
||||
expect(RAILWAY_TOOLS).toHaveLength(11);
|
||||
expect(RAILWAY_TOOLS.map((tool) => tool.name)).not.toContain("paperclip-railway-deploy-revision");
|
||||
});
|
||||
|
||||
it("gives container commands time for target checks without exceeding the gateway limit", () => {
|
||||
expect(railwayCommandBudgetMs({})).toBe(40000);
|
||||
expect(railwayCommandBudgetMs({ timeoutSeconds: 1 })).toBe(11000);
|
||||
expect(railwayCommandBudgetMs({ timeoutSeconds: 60 })).toBe(60000);
|
||||
expect(railwayCommandBudgetMs({ timeoutSeconds: 999 })).toBe(60000);
|
||||
});
|
||||
|
||||
it("checks full deployment membership before a single fixed mutation", async () => {
|
||||
const f = fixture();
|
||||
await expect(f.client.call("paperclip-railway-restart", target)).resolves.toEqual({ deploymentRestart: true, targetDeploymentId: target.deploymentId });
|
||||
expect(f.request.mock.calls.map(([, init]) => JSON.parse(String(init.body)).query)).toEqual([RAILWAY_QUERIES.target, RAILWAY_QUERIES.deployment, RAILWAY_QUERIES.restart]);
|
||||
for (const [url, init] of f.request.mock.calls) {
|
||||
expect(url).toBe(RAILWAY_API_URL);
|
||||
expect(init.redirect).toBe("error");
|
||||
expect(init.headers).toMatchObject({ Authorization: "Bearer railway-fixture-secret" });
|
||||
}
|
||||
expect(JSON.parse(String(f.request.mock.calls[2][1].body)).variables).toEqual({ deploymentId: target.deploymentId });
|
||||
});
|
||||
|
||||
it.each(["project", "environment", "service", "deployment"])("denies a mismatched %s before mutation", async (field) => {
|
||||
const f = fixture((q) => {
|
||||
if (field === "deployment" && q === RAILWAY_QUERIES.deployment) return Response.json({ data: { deployment: { ...deploymentData.deployment, serviceId: instanceId } } });
|
||||
if (field !== "deployment" && q === RAILWAY_QUERIES.target) return Response.json({ data: { ...targetData, [field]: { ...targetData[field as keyof typeof targetData], id: instanceId } } });
|
||||
});
|
||||
await expect(f.client.call("paperclip-railway-restart", target)).rejects.toMatchObject({ code: "railway_target_mismatch" });
|
||||
expect(f.request.mock.calls.every(([, init]) => !JSON.parse(String(init.body)).query.startsWith("mutation"))).toBe(true);
|
||||
});
|
||||
|
||||
it("bounds and redacts logs without selecting variables", async () => {
|
||||
const f = fixture((q) => q === RAILWAY_QUERIES.runtimeLogs ? Response.json({ data: { deploymentLogs: Array.from({ length: 20 }, () => ({ message: `railway-fixture-secret ${"x".repeat(9000)}`, severity: "INFO" })) } }) : undefined);
|
||||
const result = await f.client.call("paperclip-railway-read-logs", { ...target, limit: 10 });
|
||||
expect(JSON.stringify(result)).not.toContain("railway-fixture-secret");
|
||||
expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThan(66000);
|
||||
expect(result).toMatchObject({ truncated: true });
|
||||
await expect(f.client.call("paperclip-railway-read-logs", { ...target, limit: 501 })).rejects.toMatchObject({ code: "railway_invalid_arguments" });
|
||||
expect(Object.values(RAILWAY_QUERIES).join(" ")).not.toMatch(/variableCollection|variables\s*\{/);
|
||||
});
|
||||
|
||||
it("reports when a single long log line was cut", async () => {
|
||||
const f = fixture((q) => q === RAILWAY_QUERIES.runtimeLogs ? Response.json({ data: { deploymentLogs: [{ message: "x".repeat(20000) }] } }) : undefined);
|
||||
await expect(f.client.call("paperclip-railway-read-logs", target)).resolves.toMatchObject({ truncated: true, limitReached: false });
|
||||
});
|
||||
|
||||
it.each([401, 403, 429, 500])("sanitizes HTTP %s without retries", async (status) => {
|
||||
const f = fixture(() => new Response("provider secret", { status }));
|
||||
await expect(f.client.probe(target.projectId)).rejects.toThrow(/Railway/);
|
||||
expect(f.request).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("does not expose GraphQL error details or retry an ambiguous mutation", async () => {
|
||||
const f = fixture((q) => q === RAILWAY_QUERIES.restart ? Response.json({ errors: [{ message: "sensitive provider payload" }] }) : undefined);
|
||||
await expect(f.client.call("paperclip-railway-restart", target)).rejects.toMatchObject({ code: "railway_api_error" });
|
||||
expect(f.request).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["restart", { deploymentRestart: false }],
|
||||
["rollback", { deploymentRollback: false }],
|
||||
["redeploy", { deploymentRedeploy: null }],
|
||||
["redeploy", { deploymentRedeploy: { id: "not-a-deployment-id" } }],
|
||||
])("does not report an unconfirmed %s as successful", async (operation, data) => {
|
||||
const f = fixture((q) => q.startsWith("mutation") ? Response.json({ data }) : undefined);
|
||||
await expect(f.client.call(`paperclip-railway-${operation}`, target)).rejects.toMatchObject({ code: "railway_operation_unconfirmed" });
|
||||
expect(f.request).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it("rejects oversized responses, cancelled calls and GraphQL passthrough", async () => {
|
||||
const f = fixture(() => new Response("x".repeat(1024 * 1024 + 1)));
|
||||
await expect(f.client.probe(target.projectId)).rejects.toMatchObject({ code: "railway_output_limit" });
|
||||
await expect(f.client.call("paperclip-railway-list-projects", { query: "mutation Evil" })).rejects.toMatchObject({ code: "railway_invalid_arguments" });
|
||||
f.controller.abort();
|
||||
await expect(f.client.probe(target.projectId)).rejects.toMatchObject({ name: "AbortError" });
|
||||
expect(f.request).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it.each(["paperclip-railway-deploy-revision", "paperclip_railway_deploy_revision", "paperclipRailwayDeployRevision"])("blocks %s before any repository preflight or deployment mutation", async (name) => {
|
||||
// Even a matching repository in the preflight can change before mutation.
|
||||
// Without an atomic provider binding, no upstream request is safe to send.
|
||||
const f = fixture();
|
||||
const { deploymentId: _, ...ids } = target;
|
||||
await expect(f.client.call(name, { ...ids, repository: "example/app", commitSha: "a".repeat(40) })).rejects.toMatchObject({ code: "railway_action_blocked", status: 403 });
|
||||
expect(f.request).not.toHaveBeenCalled();
|
||||
expect(isRailwayToolBlocked(name)).toBe(true);
|
||||
expect(railwayRisk(name)).toBe("destructive");
|
||||
});
|
||||
|
||||
it("allows only an instance in the exact running deployment to reach SSH", async () => {
|
||||
const f = fixture();
|
||||
await expect(f.client.call("paperclip-railway-run-command", { ...target, deploymentInstanceId: target.serviceId, command: "true" })).rejects.toMatchObject({ code: "railway_target_mismatch" });
|
||||
expect(f.runCommand).not.toHaveBeenCalled();
|
||||
await f.client.call("paperclip-railway-run-command", { ...target, deploymentInstanceId: instanceId, command: "true" });
|
||||
expect(f.runCommand).toHaveBeenCalledWith({ deploymentInstanceId: instanceId, command: "true", timeoutSeconds: 30, signal: f.controller.signal });
|
||||
});
|
||||
});
|
||||
@@ -5099,7 +5099,7 @@ describeEmbeddedPostgres("tool access service", () => {
|
||||
"github",
|
||||
]),
|
||||
);
|
||||
expect(res.body.apps).toHaveLength(46);
|
||||
expect(res.body.apps).toHaveLength(47);
|
||||
expect(
|
||||
res.body.apps.find((app: { slug: string }) => app.slug === "gmail")
|
||||
.ownershipAvailability,
|
||||
|
||||
@@ -213,6 +213,7 @@ import {
|
||||
workspaceFileResourceQuerySchema,
|
||||
// Tool access
|
||||
connectToolAppSchema,
|
||||
configureRailwaySshSchema,
|
||||
createToolApplicationSchema,
|
||||
updateToolApplicationSchema,
|
||||
createToolConnectionSchema,
|
||||
@@ -1400,6 +1401,7 @@ const BOARD_ONLY_OPERATIONS = new Set([
|
||||
"DELETE /api/tool-connections/{connectionId}",
|
||||
"POST /api/tool-connections/{connectionId}/health-check",
|
||||
"POST /api/tool-connections/{connectionId}/reconnect",
|
||||
"POST /api/tool-connections/{connectionId}/railway/ssh",
|
||||
"POST /api/tool-connections/{connectionId}/catalog/refresh",
|
||||
"GET /api/tool-connections/{connectionId}/catalog",
|
||||
"GET /api/tool-connections/{connectionId}/activity",
|
||||
@@ -10286,6 +10288,23 @@ registerCurrentRoute({
|
||||
summary: "List the broker services behind a tool connection",
|
||||
});
|
||||
|
||||
registerCurrentRoute({
|
||||
method: "post",
|
||||
path: "/api/tool-connections/{connectionId}/railway/ssh",
|
||||
tags: ["tool-access"],
|
||||
summary: "Prepare, enable, or remove a Railway container SSH key",
|
||||
body: configureRailwaySshSchema,
|
||||
responses: {
|
||||
200: r.ok(),
|
||||
400: r.badRequest,
|
||||
401: r.unauthorized,
|
||||
403: r.forbidden,
|
||||
404: r.notFound,
|
||||
409: r.conflict,
|
||||
422: r.unprocessable,
|
||||
},
|
||||
});
|
||||
|
||||
registerCurrentRoute({
|
||||
method: "post",
|
||||
path: "/api/tool-connections/{connectionId}/services/{toolkitSlug}/connect",
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
type ToolConnection,
|
||||
type ToolConnectionCreateCapabilities,
|
||||
connectToolAppSchema,
|
||||
configureRailwaySshSchema,
|
||||
createConnectionGrantDelegationSchema,
|
||||
createToolStdioCommandTemplateSchema,
|
||||
createToolApplicationSchema,
|
||||
@@ -55,6 +56,7 @@ import { getActorInfo, assertBoard, assertCompanyAccess, assertInstanceAdmin, ge
|
||||
import { badRequest, forbidden, HttpError, notFound, unprocessable } from "../errors.js";
|
||||
import { accessService, logActivity, toolAccessPolicyService, toolAccessService, vercelConnectIntegrationStatus } from "../services/index.js";
|
||||
import { ToolGatewayHttpError, type ToolGatewayService } from "../services/tool-gateway.js";
|
||||
import { RailwayError } from "../services/railway.js";
|
||||
import type { ComposioClient } from "../services/composio.js";
|
||||
import type { VercelConnectClient } from "../services/vercel-connect.js";
|
||||
import {
|
||||
@@ -1679,6 +1681,19 @@ function connectorEnrollmentPrincipal(req: Request): string {
|
||||
res.json(await svc.listComposioServices(connection.id, getActorInfo(req)));
|
||||
});
|
||||
|
||||
router.post("/tool-connections/:connectionId/railway/ssh", validate(configureRailwaySshSchema), async (req, res) => {
|
||||
assertBoard(req);
|
||||
const connection = await getAccessibleResource(req, res, svc.getConnection(req.params.connectionId as string), "Tool connection not found");
|
||||
if (!connection) return;
|
||||
await assertToolConnectionConfigureAccess(req, connection);
|
||||
const setup = await svc.configureRailwaySsh(connection.id, connection.companyId, req.body, getActorInfo(req)).catch((error) => {
|
||||
if (error instanceof RailwayError) throw new HttpError(error.status, error.message);
|
||||
throw error;
|
||||
});
|
||||
await logActivity(db, { companyId: connection.companyId, actorType: "user", actorId: req.actor.userId ?? "board", action: "tool_connection.railway_ssh_updated", entityType: "tool_connection", entityId: connection.id, details: { action: req.body.action, grantId: req.body.grantId, enabled: setup?.enabled ?? false } });
|
||||
res.json(setup);
|
||||
});
|
||||
|
||||
router.post("/tool-connections/:connectionId/services/:toolkitSlug/connect", async (req, res) => {
|
||||
const connection = await getAccessibleResource(req, res, svc.getConnection(req.params.connectionId as string), "Tool connection not found");
|
||||
if (!connection) return;
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import { execFile, spawn } from "node:child_process";
|
||||
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { RailwayError, type RailwaySshInput } from "./railway.js";
|
||||
import { redactSensitiveText } from "../redaction.js";
|
||||
|
||||
export const RAILWAY_SSH_SECRET_PATH = "railway.ssh_private_key";
|
||||
|
||||
export function validateRailwayKnownHosts(value: string): string {
|
||||
if (value.length > 8192) throw new RailwayError("railway_ssh_host_key_invalid", "The Railway host key is too long.", 400);
|
||||
const lines = value.trim().split(/\r?\n/);
|
||||
if (lines.length === 0 || lines.length > 5 || lines.some((line) => !/^ssh\.railway\.com (ssh-ed25519|ssh-rsa|ecdsa-sha2-nistp256) [A-Za-z0-9+/]+={0,2}$/.test(line))) {
|
||||
throw new RailwayError("railway_ssh_host_key_invalid", "Paste verified known_hosts lines for ssh.railway.com only, without aliases, wildcards or comments.", 400);
|
||||
}
|
||||
return lines.join("\n") + "\n";
|
||||
}
|
||||
|
||||
export async function generateRailwaySshKey(): Promise<{ publicKey: string; privateKey: string }> {
|
||||
const directory = await mkdtemp(path.join(tmpdir(), "paperclip-railway-key-"));
|
||||
try {
|
||||
const keyPath = path.join(directory, "identity");
|
||||
await promisify(execFile)("/usr/bin/ssh-keygen", ["-q", "-t", "ed25519", "-N", "", "-C", "paperclip-railway", "-f", keyPath], { timeout: 10_000, env: { PATH: "/usr/bin:/bin" } });
|
||||
return { publicKey: (await readFile(`${keyPath}.pub`, "utf8")).trim(), privateKey: await readFile(keyPath, "utf8") };
|
||||
} catch {
|
||||
throw new RailwayError("railway_ssh_unavailable", "Generating a Railway key requires system OpenSSH (ssh-keygen) on the Paperclip runtime.", 422);
|
||||
} finally { await rm(directory, { recursive: true, force: true }); }
|
||||
}
|
||||
|
||||
export function railwaySshArguments(directory: string, instanceId: string): string[] {
|
||||
if (!/^[a-f0-9-]{36}$/i.test(instanceId)) throw new RailwayError("railway_target_mismatch", "Invalid Railway container instance.", 400);
|
||||
return [
|
||||
"-F", "/dev/null", "-T", "-i", path.join(directory, "identity"),
|
||||
"-o", "BatchMode=yes", "-o", "IdentitiesOnly=yes", "-o", "IdentityAgent=none",
|
||||
"-o", "ForwardAgent=no", "-o", "ClearAllForwardings=yes", "-o", "ControlMaster=no",
|
||||
"-o", "ControlPath=none", "-o", "PermitLocalCommand=no", "-o", "StrictHostKeyChecking=yes",
|
||||
"-o", `UserKnownHostsFile=${path.join(directory, "known_hosts")}`, "-o", "GlobalKnownHostsFile=/dev/null",
|
||||
"-o", "ConnectTimeout=10", "-o", "ServerAliveInterval=5", "-o", "ServerAliveCountMax=2",
|
||||
"--", `${instanceId}@ssh.railway.com`, "sh -s",
|
||||
];
|
||||
}
|
||||
|
||||
export async function runRailwaySshCommand(input: RailwaySshInput & { privateKey: string; knownHosts: string }) {
|
||||
input.signal.throwIfAborted();
|
||||
const knownHosts = validateRailwayKnownHosts(input.knownHosts);
|
||||
if (!input.privateKey.startsWith("-----BEGIN OPENSSH PRIVATE KEY-----")) throw new RailwayError("railway_ssh_key_invalid", "Regenerate the Railway connection's SSH key.", 422);
|
||||
const directory = await mkdtemp(path.join(tmpdir(), "paperclip-railway-command-"));
|
||||
try {
|
||||
await writeFile(path.join(directory, "identity"), input.privateKey, { mode: 0o600 });
|
||||
await writeFile(path.join(directory, "known_hosts"), knownHosts, { mode: 0o600 });
|
||||
input.signal.throwIfAborted();
|
||||
return await new Promise<{ exitCode: number | null; stdout: string; stderr: string; truncated: boolean; timedOut: boolean }>((resolve, reject) => {
|
||||
// No developer SSH config/agent, CLI login, provider token or ambient env.
|
||||
const child = spawn("/usr/bin/ssh", railwaySshArguments(directory, input.deploymentInstanceId), { env: { PATH: "/usr/bin:/bin", LANG: "C.UTF-8" }, stdio: ["pipe", "pipe", "pipe"] });
|
||||
let stdout = "", stderr = "", bytes = 0, truncated = false, timedOut = false, deliveryFailed = false;
|
||||
const marker = `paperclip_railway_completed_${randomBytes(16).toString("hex")}`;
|
||||
const stop = () => { child.kill("SIGKILL"); };
|
||||
const receive = (chunk: Buffer, stream: "out" | "err") => {
|
||||
const remaining = Math.max(0, 64 * 1024 - bytes);
|
||||
bytes += chunk.length;
|
||||
const text = chunk.subarray(0, remaining).toString("utf8");
|
||||
if (stream === "out") stdout += text; else stderr += text;
|
||||
if (bytes > 64 * 1024) { truncated = true; stop(); }
|
||||
};
|
||||
child.stdout.on("data", (chunk: Buffer) => receive(chunk, "out"));
|
||||
child.stderr.on("data", (chunk: Buffer) => receive(chunk, "err"));
|
||||
const timer = setTimeout(() => { timedOut = true; stop(); }, input.timeoutSeconds * 1000);
|
||||
const abort = () => stop();
|
||||
input.signal.addEventListener("abort", abort, { once: true });
|
||||
if (input.signal.aborted) abort();
|
||||
const cleanup = () => { clearTimeout(timer); input.signal.removeEventListener("abort", abort); };
|
||||
child.once("error", () => { cleanup(); reject(new RailwayError("railway_ssh_unavailable", "System OpenSSH is unavailable on this Paperclip runtime.", 422)); });
|
||||
child.once("close", (exitCode) => {
|
||||
cleanup();
|
||||
if (input.signal.aborted) { reject(input.signal.reason); return; }
|
||||
const completion = new RegExp(`\\n${marker}:(\\d+)\\r?\\n?$`).exec(stdout);
|
||||
if (!truncated && !timedOut && (deliveryFailed || !completion)) {
|
||||
reject(new RailwayError("railway_ssh_command_unconfirmed", "The container did not confirm command completion. Check SSH key registration, the trusted host key and deployment status before retrying."));
|
||||
return;
|
||||
}
|
||||
if (completion) stdout = stdout.slice(0, completion.index);
|
||||
resolve({ exitCode: completion ? Number(completion[1]) : exitCode, stdout: redactSensitiveText(stdout), stderr: redactSensitiveText(stderr), truncated, timedOut });
|
||||
});
|
||||
child.stdin.on("error", () => { deliveryFailed = true; });
|
||||
const quotedCommand = "'" + input.command.replace(/'/g, "'\\''") + "'";
|
||||
child.stdin.end(`sh -c ${quotedCommand} </dev/null\npaperclip_command_status=$?\nprintf '\\n${marker}:%d\\n' "$paperclip_command_status"\nexit "$paperclip_command_status"\n`);
|
||||
});
|
||||
} finally { await rm(directory, { recursive: true, force: true }); }
|
||||
}
|
||||
@@ -0,0 +1,311 @@
|
||||
import { z } from "zod";
|
||||
import { redactSensitiveText } from "../redaction.js";
|
||||
import { initializeMcpHttpSession, mcpHttpRequestHeaders, parseMcpHttpResponseBody } from "./mcp-http.js";
|
||||
|
||||
export const RAILWAY_MCP_URL = "https://mcp.railway.com";
|
||||
export const RAILWAY_API_URL = "https://backboard.railway.com/graphql/v2";
|
||||
export const RAILWAY_TOOL_PREFIX = "paperclip-railway-";
|
||||
/** Reserve time for target checks while staying inside the gateway's 60s cap. */
|
||||
export function railwayCommandBudgetMs(parameters: unknown): number {
|
||||
const seconds = parameters && typeof parameters === "object" ? (parameters as Record<string, unknown>).timeoutSeconds : undefined;
|
||||
const requested = typeof seconds === "number" && Number.isFinite(seconds) ? seconds : 30;
|
||||
return Math.min(60_000, (Math.max(1, requested) + 10) * 1000);
|
||||
}
|
||||
export const RAILWAY_BLOCKED_TOOLS = new Set([
|
||||
"railway-agent", "accept-deploy",
|
||||
// A separate repository preflight cannot bind serviceInstanceDeployV2 to the
|
||||
// approved repository. Keep old catalog entries/calls blocked until Railway
|
||||
// provides an atomic repository + revision mutation.
|
||||
`${RAILWAY_TOOL_PREFIX}deploy-revision`,
|
||||
]);
|
||||
export function normalizeRailwayToolName(name: string): string {
|
||||
return name.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase().replace(/[:._-]+/g, "-");
|
||||
}
|
||||
export function isRailwayToolBlocked(name: string): boolean {
|
||||
return RAILWAY_BLOCKED_TOOLS.has(normalizeRailwayToolName(name));
|
||||
}
|
||||
|
||||
/** Both branding and an exact endpoint are required for the built-in API bridge. */
|
||||
export function isRailwayConnection(connection: {
|
||||
transport: string; authKind: string; credentialSource?: string;
|
||||
config: Record<string, unknown>;
|
||||
}): boolean {
|
||||
return connection.transport === "mcp_remote" && connection.authKind === "oauth"
|
||||
&& connection.credentialSource === "paperclip_vault"
|
||||
&& connection.config.sourceTemplateKey === "railway"
|
||||
&& connection.config.connectionMethodKey === "mcp-oauth"
|
||||
&& isRailwayEndpoint(connection.config.url);
|
||||
}
|
||||
|
||||
export function isRailwayEndpoint(value: unknown): boolean {
|
||||
return value === RAILWAY_MCP_URL || value === `${RAILWAY_MCP_URL}/`;
|
||||
}
|
||||
|
||||
const id = z.string().uuid();
|
||||
const paging = { first: z.number().int().min(1).max(100).default(25), after: z.string().max(512).optional() };
|
||||
const target = { projectId: id, environmentId: id, serviceId: id };
|
||||
const deploymentTarget = { ...target, deploymentId: id };
|
||||
const timestamp = z.string().datetime({ offset: true }).optional();
|
||||
const schema = {
|
||||
"list-projects": z.object({ workspaceId: id, ...paging }).strict(),
|
||||
"list-services": z.object({ projectId: id, ...paging }).strict(),
|
||||
"list-environments": z.object({ projectId: id, ...paging }).strict(),
|
||||
"service-status": z.object(target).strict(),
|
||||
"list-deployments": z.object({ ...target, ...paging }).strict(),
|
||||
"deployment-status": z.object(deploymentTarget).strict(),
|
||||
"read-logs": z.object({ ...deploymentTarget, kind: z.enum(["build", "runtime"]).default("runtime"), limit: z.number().int().min(1).max(500).default(100), startDate: timestamp, endDate: timestamp, filter: z.string().max(500).optional() }).strict(),
|
||||
redeploy: z.object(deploymentTarget).strict(),
|
||||
restart: z.object(deploymentTarget).strict(),
|
||||
rollback: z.object(deploymentTarget).strict(),
|
||||
"run-command": z.object({ ...deploymentTarget, deploymentInstanceId: id, command: z.string().min(1).max(8192), timeoutSeconds: z.number().int().min(1).max(60).default(30) }).strict(),
|
||||
};
|
||||
type Operation = keyof typeof schema;
|
||||
const titles: Record<Operation, string> = {
|
||||
"list-projects": "List projects (direct)",
|
||||
"list-services": "List services (direct)",
|
||||
"list-environments": "List environments",
|
||||
"service-status": "Get service status",
|
||||
"list-deployments": "List deployments",
|
||||
"deployment-status": "Get deployment status",
|
||||
"read-logs": "Read deployment logs",
|
||||
redeploy: "Redeploy a deployment",
|
||||
restart: "Restart a deployment",
|
||||
rollback: "Roll back to a deployment",
|
||||
"run-command": "Run a container command",
|
||||
};
|
||||
const descriptions: Record<Operation, string> = {
|
||||
"list-projects": "List Railway projects in an explicit authorized workspace, with bounded pagination. Use the hosted list-workspaces action to find workspace IDs.",
|
||||
"list-services": "List services in one Railway project. Use service-status to inspect a specific environment.",
|
||||
"list-environments": "List environments in one Railway project.",
|
||||
"service-status": "Inspect a service's running and latest deployments in an explicit project and environment.",
|
||||
"list-deployments": "List deployments for one explicit project, environment, and service.",
|
||||
"deployment-status": "Inspect an exact deployment and its container instance IDs.",
|
||||
"read-logs": "Read at most 500 build or runtime log lines for an exact deployment. Output is bounded; logs may contain sensitive application data.",
|
||||
redeploy: "Redeploy an exact deployment using its previous image. This changes a running service.",
|
||||
restart: "Restart an exact deployment without rebuilding. This interrupts a running service.",
|
||||
rollback: "Roll back to an exact eligible deployment. This changes a running service.",
|
||||
"run-command": "Run a bounded noninteractive shell command in an exact deployed container using this connection's configured SSH key. Broad privileged access: commands can read secrets and mutate application data. Requires Container access setup. Timeout closes SSH; remote child termination is not guaranteed.",
|
||||
};
|
||||
const reads = new Set<Operation>(["list-projects", "list-services", "list-environments", "service-status", "list-deployments", "deployment-status", "read-logs"]);
|
||||
|
||||
export const RAILWAY_TOOLS = Object.entries(schema).map(([operation, validator]) => ({
|
||||
name: `${RAILWAY_TOOL_PREFIX}${operation}`,
|
||||
title: titles[operation as Operation],
|
||||
description: descriptions[operation as Operation],
|
||||
inputSchema: z.toJSONSchema(validator, { target: "draft-7", io: "input" }) as Record<string, unknown>,
|
||||
annotations: { readOnlyHint: reads.has(operation as Operation), destructiveHint: !reads.has(operation as Operation), idempotentHint: reads.has(operation as Operation), openWorldHint: true },
|
||||
}));
|
||||
|
||||
export function railwayRisk(name: string): "read" | "write" | "destructive" {
|
||||
name = normalizeRailwayToolName(name);
|
||||
if (isRailwayToolBlocked(name)) return "destructive";
|
||||
const operation = name.slice(RAILWAY_TOOL_PREFIX.length) as Operation;
|
||||
if (name.startsWith(RAILWAY_TOOL_PREFIX) && operation in schema) return reads.has(operation) ? "read" : "destructive";
|
||||
if (["whoami", "list-projects", "list-services", "list-feature-flags", "get-feature-flag"].includes(name)) return "read";
|
||||
if (["redeploy", "accept-deploy", "railway-agent", "delete-feature-flag"].includes(name)) return "destructive";
|
||||
return "write";
|
||||
}
|
||||
|
||||
export class RailwayError extends Error {
|
||||
constructor(readonly code: string, message: string, readonly status = 502) { super(message); this.name = "RailwayError"; }
|
||||
}
|
||||
|
||||
export interface RailwaySshInput {
|
||||
deploymentInstanceId: string; command: string; timeoutSeconds: number; signal: AbortSignal;
|
||||
}
|
||||
export interface RailwayClientOptions {
|
||||
authorization: string;
|
||||
request: (url: string, init: RequestInit) => Promise<Response>;
|
||||
signal: AbortSignal;
|
||||
runCommand?: (input: RailwaySshInput) => Promise<unknown>;
|
||||
}
|
||||
|
||||
const pageInfo = "pageInfo { hasNextPage endCursor }";
|
||||
const deploymentFields = "id projectId environmentId serviceId status createdAt url canRedeploy canRollback";
|
||||
const instanceFields = "id environmentId serviceId serviceName source { repo } latestDeployment { id status } activeDeployments { id status }";
|
||||
|
||||
/** All query documents are authored here. Caller input is only ever variables. */
|
||||
export const RAILWAY_QUERIES = {
|
||||
projects: `query PaperclipRailwayProjects($workspaceId:String!,$first:Int!,$after:String) { projects(workspaceId:$workspaceId,first:$first,after:$after) { edges { node { id name workspaceId } } ${pageInfo} } }`,
|
||||
services: `query PaperclipRailwayServices($projectId:String!,$first:Int!,$after:String) { project(id:$projectId) { id services(first:$first,after:$after) { edges { node { id name projectId } } ${pageInfo} } } }`,
|
||||
environments: `query PaperclipRailwayEnvironments($projectId:String!,$first:Int!,$after:String) { project(id:$projectId) { id environments(first:$first,after:$after) { edges { node { id name projectId } } ${pageInfo} } } }`,
|
||||
target: `query PaperclipRailwayTarget($projectId:String!,$environmentId:String!,$serviceId:String!) { project(id:$projectId) { id } environment(id:$environmentId) { id projectId } service(id:$serviceId) { id projectId } serviceInstance(environmentId:$environmentId,serviceId:$serviceId) { ${instanceFields} } }`,
|
||||
deployment: `query PaperclipRailwayDeployment($deploymentId:String!) { deployment(id:$deploymentId) { ${deploymentFields} instances { id } } }`,
|
||||
deployments: `query PaperclipRailwayDeployments($input:DeploymentListInput!,$first:Int!,$after:String) { deployments(input:$input,first:$first,after:$after) { edges { node { ${deploymentFields} } } ${pageInfo} } }`,
|
||||
buildLogs: `query PaperclipRailwayBuildLogs($deploymentId:String!,$limit:Int!,$startDate:DateTime,$endDate:DateTime,$filter:String) { buildLogs(deploymentId:$deploymentId,limit:$limit,startDate:$startDate,endDate:$endDate,filter:$filter) { timestamp message severity } }`,
|
||||
runtimeLogs: `query PaperclipRailwayRuntimeLogs($deploymentId:String!,$limit:Int!,$startDate:DateTime,$endDate:DateTime,$filter:String) { deploymentLogs(deploymentId:$deploymentId,limit:$limit,startDate:$startDate,endDate:$endDate,filter:$filter) { timestamp message severity } }`,
|
||||
redeploy: `mutation PaperclipRailwayRedeploy($deploymentId:String!) { deploymentRedeploy(id:$deploymentId,usePreviousImageTag:true) { id status } }`,
|
||||
restart: `mutation PaperclipRailwayRestart($deploymentId:String!) { deploymentRestart(id:$deploymentId) }`,
|
||||
rollback: `mutation PaperclipRailwayRollback($deploymentId:String!) { deploymentRollback(id:$deploymentId) }`,
|
||||
};
|
||||
|
||||
function record(value: unknown): Record<string, any> {
|
||||
return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, any> : {};
|
||||
}
|
||||
|
||||
async function boundedResponseText(response: Response, signal: AbortSignal): Promise<string> {
|
||||
const reader = response.body?.getReader();
|
||||
if (!reader) throw new RailwayError("railway_invalid_response", "Railway returned an empty response.");
|
||||
const chunks: Uint8Array[] = [];
|
||||
let size = 0;
|
||||
try {
|
||||
for (;;) {
|
||||
signal.throwIfAborted();
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
size += value.byteLength;
|
||||
if (size > 1024 * 1024) throw new RailwayError("railway_output_limit", "Railway's response exceeded the limit. Request fewer results or a shorter log interval.");
|
||||
chunks.push(value);
|
||||
}
|
||||
} finally { await reader.cancel().catch(() => {}); }
|
||||
return Buffer.concat(chunks).toString("utf8");
|
||||
}
|
||||
|
||||
/** Discover a consented workspace without requesting account-wide API access. */
|
||||
export async function discoverRailwayWorkspace(options: RailwayClientOptions): Promise<string> {
|
||||
const send = (init: RequestInit) => options.request(RAILWAY_MCP_URL, { ...init, redirect: "error", signal: options.signal });
|
||||
const headers = { Authorization: options.authorization };
|
||||
const list = (requestHeaders: Record<string, string>) => send({
|
||||
method: "POST", headers: mcpHttpRequestHeaders(requestHeaders),
|
||||
body: JSON.stringify({ jsonrpc: "2.0", id: "paperclip-railway-workspace-probe", method: "tools/call", params: { name: "list-workspaces", arguments: {} } }),
|
||||
});
|
||||
let response = await list(headers);
|
||||
if (response.status === 400) {
|
||||
await response.body?.cancel();
|
||||
response = await list(await initializeMcpHttpSession({ send, headers, requestId: "paperclip-railway-workspace-probe" }));
|
||||
}
|
||||
if (!response.ok) {
|
||||
await response.body?.cancel();
|
||||
throw new RailwayError("railway_workspace_discovery_failed", "Railway's hosted connection is connected, but workspace access could not be checked. Refresh actions to try again.");
|
||||
}
|
||||
const body = await boundedResponseText(response, options.signal);
|
||||
let data: Record<string, any>;
|
||||
try {
|
||||
const payload = record(parseMcpHttpResponseBody(body, response.headers.get("content-type")));
|
||||
const result = record(payload.result);
|
||||
if (payload.error || result.isError) throw new Error("Workspace discovery failed");
|
||||
data = record(result.structuredContent ?? JSON.parse(result.content?.find((item: any) => item.type === "text")?.text ?? "{}"));
|
||||
} catch { throw new RailwayError("railway_workspace_discovery_failed", "Railway could not list authorized workspaces. Refresh actions or reconnect and select a workspace."); }
|
||||
const workspaceId = Array.isArray(data.workspaces) ? data.workspaces.find((workspace) => id.safeParse(workspace?.id).success)?.id : undefined;
|
||||
if (!workspaceId) throw new RailwayError("railway_workspace_required", "No authorized Railway workspace was found. Reconnect Railway and select a workspace to enable direct operations.", 403);
|
||||
return workspaceId;
|
||||
}
|
||||
|
||||
export function createRailwayClient(options: RailwayClientOptions) {
|
||||
if (!/^Bearer [^\r\n]+$/.test(options.authorization)) throw new RailwayError("railway_authorization_required", "Reconnect Railway to authorize API access.", 401);
|
||||
const secret = options.authorization.slice(7);
|
||||
const redact = (value: unknown) => JSON.parse(redactSensitiveText(JSON.stringify(value).split(secret).join("[REDACTED]")));
|
||||
|
||||
async function query(document: string, variables: Record<string, unknown>): Promise<Record<string, any>> {
|
||||
options.signal.throwIfAborted();
|
||||
let response: Response;
|
||||
try {
|
||||
response = await options.request(RAILWAY_API_URL, { method: "POST", redirect: "error", signal: options.signal, headers: { "content-type": "application/json", Authorization: options.authorization }, body: JSON.stringify({ query: document, variables }) });
|
||||
} catch (error) {
|
||||
if (options.signal.aborted) throw options.signal.reason;
|
||||
throw new RailwayError("railway_request_failed", "Railway could not be reached. A deployment request may have succeeded; inspect deployment status before retrying.");
|
||||
}
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
await response.body?.cancel();
|
||||
throw new RailwayError("railway_api_authorization_required", "Railway rejected API access. Reconnect with access to the required workspace or project. Hosted connection tokens are used only if Railway accepts them for API access.", response.status);
|
||||
}
|
||||
if (!response.ok) {
|
||||
await response.body?.cancel();
|
||||
throw new RailwayError(response.status === 429 ? "railway_rate_limited" : "railway_api_unavailable", response.status === 429 ? "Railway is rate limiting requests. Wait before trying again." : "Railway is unavailable. Check deployment status before retrying a deployment operation.");
|
||||
}
|
||||
const body = await boundedResponseText(response, options.signal);
|
||||
let payload: Record<string, any>;
|
||||
try { payload = record(JSON.parse(body)); }
|
||||
catch { throw new RailwayError("railway_invalid_response", "Railway returned an invalid API response."); }
|
||||
if (payload.errors) {
|
||||
// Provider errors can echo variables, credentials or application secrets.
|
||||
if (Array.isArray(payload.errors) && payload.errors.some((error) => ["UNAUTHENTICATED", "FORBIDDEN"].includes(error?.extensions?.code) || ["Not Authorized", "Unauthorized", "Forbidden"].includes(error?.message))) {
|
||||
throw new RailwayError("railway_api_authorization_required", "Railway denied this API request. Use IDs from a workspace selected during consent, or reconnect to grant access to the required workspace.", 403);
|
||||
}
|
||||
throw new RailwayError("railway_api_error", "Railway could not complete the request. Check target IDs, resource permissions, and deployment eligibility. Inspect status before retrying a mutation.");
|
||||
}
|
||||
if (!payload.data || typeof payload.data !== "object") throw new RailwayError("railway_invalid_response", "Railway returned no API data.");
|
||||
return payload.data;
|
||||
}
|
||||
|
||||
async function validateTarget(args: Record<string, any>) {
|
||||
const data = await query(RAILWAY_QUERIES.target, { projectId: args.projectId, environmentId: args.environmentId, serviceId: args.serviceId });
|
||||
if (data.project?.id !== args.projectId || data.environment?.id !== args.environmentId || data.environment?.projectId !== args.projectId || data.service?.id !== args.serviceId || data.service?.projectId !== args.projectId || data.serviceInstance?.environmentId !== args.environmentId || data.serviceInstance?.serviceId !== args.serviceId) {
|
||||
throw new RailwayError("railway_target_mismatch", "The service and environment do not belong to the selected Railway project.", 403);
|
||||
}
|
||||
return data.serviceInstance;
|
||||
}
|
||||
|
||||
async function validateDeployment(args: Record<string, any>) {
|
||||
const data = await query(RAILWAY_QUERIES.deployment, { deploymentId: args.deploymentId });
|
||||
const d = record(data.deployment);
|
||||
if (d.id !== args.deploymentId || d.projectId !== args.projectId || d.environmentId !== args.environmentId || d.serviceId !== args.serviceId) throw new RailwayError("railway_target_mismatch", "The deployment does not belong to the selected Railway target.", 403);
|
||||
return d;
|
||||
}
|
||||
|
||||
return {
|
||||
async probe(workspaceId: string) {
|
||||
if (!id.safeParse(workspaceId).success) throw new RailwayError("railway_workspace_required", "Choose an authorized Railway workspace before checking API access.", 400);
|
||||
await query(RAILWAY_QUERIES.projects, { workspaceId, first: 1 });
|
||||
},
|
||||
async call(name: string, parameters: unknown): Promise<unknown> {
|
||||
if (isRailwayToolBlocked(name)) throw new RailwayError("railway_action_blocked", "This Railway action cannot bind its effects to an approved target. Use redeploy, restart, or rollback for an existing deployment.", 403);
|
||||
const operation = name.slice(RAILWAY_TOOL_PREFIX.length) as Operation;
|
||||
if (!name.startsWith(RAILWAY_TOOL_PREFIX) || !Object.hasOwn(schema, operation)) throw new RailwayError("railway_unknown_tool", "Unknown Railway operation.", 400);
|
||||
const parsed = schema[operation].safeParse(parameters);
|
||||
if (!parsed.success) throw new RailwayError("railway_invalid_arguments", "Invalid Railway operation arguments. Use the exact IDs and limits in the action schema.", 400);
|
||||
const args = parsed.data as Record<string, any>;
|
||||
let result: unknown;
|
||||
if (operation === "list-projects") result = await query(RAILWAY_QUERIES.projects, args);
|
||||
else if (operation === "list-services" || operation === "list-environments") result = await query(operation === "list-services" ? RAILWAY_QUERIES.services : RAILWAY_QUERIES.environments, args);
|
||||
else {
|
||||
const instance = await validateTarget(args);
|
||||
const deployment = args.deploymentId ? await validateDeployment(args) : null;
|
||||
switch (operation) {
|
||||
case "service-status": result = instance; break;
|
||||
case "deployment-status": result = deployment; break;
|
||||
case "list-deployments": result = await query(RAILWAY_QUERIES.deployments, { input: { projectId: args.projectId, environmentId: args.environmentId, serviceId: args.serviceId }, first: args.first, after: args.after }); break;
|
||||
case "read-logs": {
|
||||
if (args.startDate && args.endDate && Date.parse(args.startDate) > Date.parse(args.endDate)) throw new RailwayError("railway_invalid_arguments", "Log start time must precede end time.", 400);
|
||||
const data = await query(args.kind === "build" ? RAILWAY_QUERIES.buildLogs : RAILWAY_QUERIES.runtimeLogs, { deploymentId: args.deploymentId, limit: args.limit, startDate: args.startDate, endDate: args.endDate, filter: args.filter });
|
||||
const lines = data[args.kind === "build" ? "buildLogs" : "deploymentLogs"];
|
||||
if (!Array.isArray(lines)) throw new RailwayError("railway_invalid_response", "Railway returned invalid log data.");
|
||||
let bytes = 0;
|
||||
let messageTruncated = false;
|
||||
const bounded = [];
|
||||
for (const line of lines.slice(0, args.limit)) {
|
||||
const message = String(line.message ?? "");
|
||||
if (message.length > 8192) messageTruncated = true;
|
||||
const safe = redact({ timestamp: line.timestamp, severity: line.severity, message: message.slice(0, 8192) });
|
||||
bytes += Buffer.byteLength(JSON.stringify(safe));
|
||||
if (bytes > 64 * 1024) break;
|
||||
bounded.push(safe);
|
||||
}
|
||||
result = { deploymentId: args.deploymentId, kind: args.kind, lines: bounded, truncated: messageTruncated || bounded.length < lines.length, limitReached: lines.length >= args.limit }; break;
|
||||
}
|
||||
case "redeploy":
|
||||
if (!deployment?.canRedeploy) throw new RailwayError("railway_deployment_ineligible", "Railway does not allow this deployment to be redeployed.", 409);
|
||||
result = await query(RAILWAY_QUERIES.redeploy, { deploymentId: args.deploymentId });
|
||||
if (!id.safeParse(record(record(result).deploymentRedeploy).id).success) throw new RailwayError("railway_operation_unconfirmed", "Railway did not confirm a resulting deployment. Inspect deployment status before retrying.");
|
||||
break;
|
||||
case "restart":
|
||||
result = await query(RAILWAY_QUERIES.restart, { deploymentId: args.deploymentId });
|
||||
if (record(result).deploymentRestart !== true) throw new RailwayError("railway_operation_unconfirmed", "Railway did not confirm the restart. Inspect deployment status before retrying.");
|
||||
result = { ...record(result), targetDeploymentId: args.deploymentId };
|
||||
break;
|
||||
case "rollback":
|
||||
if (!deployment?.canRollback) throw new RailwayError("railway_deployment_ineligible", "Railway does not allow rollback to this deployment.", 409);
|
||||
result = await query(RAILWAY_QUERIES.rollback, { deploymentId: args.deploymentId });
|
||||
if (record(result).deploymentRollback !== true) throw new RailwayError("railway_operation_unconfirmed", "Railway did not confirm the rollback. Inspect deployment status before retrying.");
|
||||
result = { ...record(result), targetDeploymentId: args.deploymentId };
|
||||
break;
|
||||
case "run-command":
|
||||
if (!Array.isArray(deployment?.instances) || !deployment.instances.some((entry: { id: string }) => entry.id === args.deploymentInstanceId) || deployment.status !== "SUCCESS") throw new RailwayError("railway_target_mismatch", "The container instance is not part of the selected running deployment.", 403);
|
||||
if (!options.runCommand) throw new RailwayError("railway_ssh_setup_required", "Configure Container access on this Railway connection before running commands.", 422);
|
||||
result = await options.runCommand({ deploymentInstanceId: args.deploymentInstanceId, command: args.command, timeoutSeconds: args.timeoutSeconds, signal: options.signal }); break;
|
||||
}
|
||||
}
|
||||
return redact(result ?? null);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -205,6 +205,9 @@ import {
|
||||
} from "./remote-url-credentials.js";
|
||||
import { secretService } from "./secrets.js";
|
||||
import { agentmailApi } from "./agentmail-api.js";
|
||||
import type { ConfigureRailwaySsh, RailwaySshSetup } from "@paperclipai/shared";
|
||||
import { generateRailwaySshKey, RAILWAY_SSH_SECRET_PATH, validateRailwayKnownHosts } from "./railway-ssh.js";
|
||||
import { createRailwayClient, discoverRailwayWorkspace, isRailwayConnection, isRailwayEndpoint, isRailwayToolBlocked, normalizeRailwayToolName, RAILWAY_TOOLS, RAILWAY_TOOL_PREFIX, railwayRisk, RailwayError } from "./railway.js";
|
||||
import { toolAccessPolicyService } from "./tool-access-policy.js";
|
||||
import {
|
||||
readSignedToolArgumentsPayload,
|
||||
@@ -2312,6 +2315,10 @@ export function classifyRisk(
|
||||
if (annotations.destructiveHint === true || annotations.destructive === true)
|
||||
return "destructive";
|
||||
const normalizedToolName = normalizedProviderToolName(tool.name);
|
||||
if (sourceTemplateKey === "railway") {
|
||||
const reviewed = railwayRisk(normalizedToolName);
|
||||
return reviewed === "read" && (annotations.readOnlyHint === false || annotations.writeHint === true) ? "write" : reviewed;
|
||||
}
|
||||
if (sourceTemplateKey === "posthog" && normalizedToolName === "exec")
|
||||
return "destructive";
|
||||
if (
|
||||
@@ -5453,7 +5460,9 @@ export function toolAccessService(
|
||||
const [updated] = await dbClient
|
||||
.update(connectionGrants)
|
||||
.set({
|
||||
credentialSecretRefs: connection.credentialSecretRefs,
|
||||
credentialSecretRefs: isRailwayConnection(connection)
|
||||
? [...connection.credentialSecretRefs, ...existing.credentialSecretRefs.filter((ref) => ref.configPath === RAILWAY_SSH_SECRET_PATH && !connection.credentialSecretRefs.some((candidate) => candidate.configPath === ref.configPath))]
|
||||
: connection.credentialSecretRefs,
|
||||
status: "active",
|
||||
revokedAt: null,
|
||||
revokedByAgentId: null,
|
||||
@@ -6947,9 +6956,34 @@ export function toolAccessService(
|
||||
: Array.isArray(payloadTools)
|
||||
? payloadTools
|
||||
: [];
|
||||
return tools
|
||||
const descriptors = tools
|
||||
.map((tool) => normalizeToolDescriptor(tool))
|
||||
.filter((tool): tool is McpToolDescriptor => Boolean(tool));
|
||||
if (!isRailwayConnection(connection)) return descriptors;
|
||||
if (descriptors.some((tool) => normalizeRailwayToolName(tool.name).startsWith(RAILWAY_TOOL_PREFIX))) {
|
||||
throw unprocessable("Railway advertised a reserved Paperclip action name. Refresh is blocked pending review.", { code: "railway_tool_name_collision" });
|
||||
}
|
||||
let apiStatus = "available";
|
||||
let apiMessage = "Direct Railway service, log, and deployment tools are available.";
|
||||
try {
|
||||
const railwayOptions = {
|
||||
authorization: headers.Authorization ?? "",
|
||||
request: (url: string, init: RequestInit) => requestRemoteHttpEndpoint(new URL(url), init),
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
};
|
||||
const workspaceId = await discoverRailwayWorkspace(railwayOptions);
|
||||
await createRailwayClient(railwayOptions).probe(workspaceId);
|
||||
} catch (error) {
|
||||
apiStatus = "unavailable";
|
||||
apiMessage = error instanceof RailwayError ? error.message : "Railway API access could not be verified. Refresh actions or reconnect Railway.";
|
||||
}
|
||||
// API interoperability is verified with the actual credential; the presence
|
||||
// of an OAuth token alone never enables the additional capability surface.
|
||||
const nextConfig = { ...connection.config, railwayApiStatus: apiStatus, railwayApiMessage: apiMessage };
|
||||
await db.update(toolConnections).set({ config: nextConfig, transportConfig: nextConfig, updatedAt: now() }).where(and(eq(toolConnections.id, connection.id), eq(toolConnections.companyId, connection.companyId)));
|
||||
connection.config = nextConfig;
|
||||
connection.transportConfig = nextConfig;
|
||||
return apiStatus === "available" ? [...descriptors, ...RAILWAY_TOOLS] : descriptors;
|
||||
}
|
||||
|
||||
async function localTools(
|
||||
@@ -7729,6 +7763,15 @@ export function toolAccessService(
|
||||
const existingByName = new Map(
|
||||
existingRows.map((entry) => [entry.toolName, entry]),
|
||||
);
|
||||
// Retired native actions are absent from discovery, but old catalog rows
|
||||
// still need to show as disabled. Gateway denial also applies before refresh.
|
||||
const blockedRailwayEntryIds = isRailwayEndpoint(connection.config.url)
|
||||
? existingRows.filter((entry) => isRailwayToolBlocked(entry.toolName)).map((entry) => entry.id)
|
||||
: [];
|
||||
if (blockedRailwayEntryIds.length > 0) {
|
||||
await db.update(toolCatalogEntries).set({ status: "disabled", updatedAt: refreshedAt })
|
||||
.where(and(eq(toolCatalogEntries.connectionId, connection.id), inArray(toolCatalogEntries.id, blockedRailwayEntryIds)));
|
||||
}
|
||||
const updatedEntries: ToolCatalogEntry[] = [];
|
||||
let quarantinedCount = 0;
|
||||
const sourceTemplateKey =
|
||||
@@ -7755,14 +7798,15 @@ export function toolAccessService(
|
||||
? googleProfileValue
|
||||
: null;
|
||||
const quarantineOnRefresh =
|
||||
!refreshOptions.enableAllByDefault &&
|
||||
(!refreshOptions.enableAllByDefault || (isRailwayEndpoint(connection.config.url) && existingRows.length > 0)) &&
|
||||
shouldQuarantineNewEntries(connection) &&
|
||||
(connection.status === "active" ||
|
||||
(isRailwayEndpoint(connection.config.url) && existingRows.length > 0) ||
|
||||
sourceTemplateKey === "posthog" ||
|
||||
refreshOptions.quarantineManagedOAuthDraft === true);
|
||||
const safeDefault = asRecord(connection.config).safeDefault === true;
|
||||
for (const descriptor of descriptors) {
|
||||
const riskLevel = classifyRisk(descriptor, sourceTemplateKey);
|
||||
const riskLevel = classifyRisk(descriptor, isRailwayEndpoint(connection.config.url) ? "railway" : sourceTemplateKey);
|
||||
const hash = descriptorHash(descriptor, riskLevel);
|
||||
const schemaHash = stableHash(descriptor.inputSchema ?? {});
|
||||
const existing = existingByName.get(descriptor.name);
|
||||
@@ -7774,11 +7818,11 @@ export function toolAccessService(
|
||||
(!existing || changed) &&
|
||||
existing?.status !== "disabled" &&
|
||||
(!safeDefault || riskLevel !== "read");
|
||||
const googlePermanentlyBlocked = Boolean(
|
||||
const providerPermanentlyBlocked = Boolean(
|
||||
googleProfile &&
|
||||
!isGoogleWorkspaceToolAllowed(googleProfile, descriptor),
|
||||
);
|
||||
const status = googlePermanentlyBlocked
|
||||
) || (isRailwayEndpoint(connection.config.url) && isRailwayToolBlocked(descriptor.name));
|
||||
const status = providerPermanentlyBlocked
|
||||
? "disabled"
|
||||
: shouldQuarantine
|
||||
? "quarantined"
|
||||
@@ -7787,7 +7831,7 @@ export function toolAccessService(
|
||||
: quarantineOnRefresh && existing?.status === "quarantined"
|
||||
? "quarantined"
|
||||
: "active";
|
||||
if (shouldQuarantine && !googlePermanentlyBlocked) quarantinedCount += 1;
|
||||
if (shouldQuarantine && !providerPermanentlyBlocked) quarantinedCount += 1;
|
||||
|
||||
if (existing) {
|
||||
const [updated] = await db
|
||||
@@ -7853,10 +7897,14 @@ export function toolAccessService(
|
||||
}
|
||||
}
|
||||
|
||||
const normalizedConfig = refreshOptions.enableAllByDefault
|
||||
const normalizedConfig = isRailwayEndpoint(connection.config.url)
|
||||
? { ...connection.config, quarantineNewEntries: true }
|
||||
: refreshOptions.enableAllByDefault
|
||||
? { ...connection.config, quarantineNewEntries: false }
|
||||
: connection.config;
|
||||
const normalizedTransportConfig = refreshOptions.enableAllByDefault
|
||||
const normalizedTransportConfig = isRailwayEndpoint(connection.config.url)
|
||||
? { ...connection.transportConfig, quarantineNewEntries: true }
|
||||
: refreshOptions.enableAllByDefault
|
||||
? { ...connection.transportConfig, quarantineNewEntries: false }
|
||||
: connection.transportConfig;
|
||||
const [updatedConnection] = await db
|
||||
@@ -9032,7 +9080,7 @@ export function toolAccessService(
|
||||
function oauthSecretRef(
|
||||
connection: typeof toolConnections.$inferSelect,
|
||||
configPath:
|
||||
"oauth.access_token" | "oauth.refresh_token" | "oauth.client_secret",
|
||||
"oauth.access_token" | "oauth.refresh_token" | "oauth.client_secret" | "railway.ssh_private_key",
|
||||
) {
|
||||
return (
|
||||
connection.credentialSecretRefs.find(
|
||||
@@ -9471,7 +9519,7 @@ export function toolAccessService(
|
||||
companyId: string;
|
||||
connection: typeof toolConnections.$inferSelect;
|
||||
configPath:
|
||||
"oauth.access_token" | "oauth.refresh_token" | "oauth.client_secret";
|
||||
"oauth.access_token" | "oauth.refresh_token" | "oauth.client_secret" | "railway.ssh_private_key";
|
||||
label: string;
|
||||
value: string;
|
||||
actor?: ActorInfo;
|
||||
@@ -12658,7 +12706,7 @@ export function toolAccessService(
|
||||
// Grant-backed setup keeps the full discovered catalog selectable;
|
||||
// the wizard projects the app's action defaults into policies at
|
||||
// finish time instead of using catalog quarantine as access state.
|
||||
quarantineNewEntries: false,
|
||||
quarantineNewEntries: galleryEntry.slug === "railway",
|
||||
...(galleryEntry.slug === "posthog" ? { safeDefault: true } : {}),
|
||||
}
|
||||
: { ...baseConfig, quarantineNewEntries: false, unverifiedServer: true };
|
||||
@@ -18564,6 +18612,42 @@ export function toolAccessService(
|
||||
|
||||
refreshCatalog,
|
||||
|
||||
configureRailwaySsh: async (connectionId: string, companyId: string, input: ConfigureRailwaySsh, actor: ActorInfo): Promise<RailwaySshSetup | null> => {
|
||||
const knownHosts = input.action === "enable" ? validateRailwayKnownHosts(input.knownHosts) : "";
|
||||
const setup = await db.transaction(async (tx) => {
|
||||
const [connection] = await tx.select().from(toolConnections).where(and(eq(toolConnections.id, connectionId), eq(toolConnections.companyId, companyId))).for("update");
|
||||
if (!connection || !isRailwayConnection(connection) || (connection.status !== "active" && input.action !== "remove")) throw unprocessable("Connect Railway before configuring container access.");
|
||||
const [grant] = await tx.select().from(connectionGrants).where(and(eq(connectionGrants.id, input.grantId), eq(connectionGrants.connectionId, connectionId), eq(connectionGrants.companyId, companyId))).for("update");
|
||||
if (!grant || (grant.status !== "active" && input.action !== "remove")) throw unprocessable("Select an active Railway authorization.");
|
||||
if (grant.kind === "user" && (actor.actorType !== "user" || actor.actorId !== grant.subjectUserId)) throw forbidden("Only this authorization's owner can configure its SSH key.");
|
||||
const existing = asRecord(connection.config.railwaySsh);
|
||||
if (existing.grantId && existing.grantId !== grant.id) throw conflict("Remove the existing container key before choosing another authorization.");
|
||||
const currentRef = grant.credentialSecretRefs.find((ref) => ref.configPath === RAILWAY_SSH_SECRET_PATH);
|
||||
let next: RailwaySshSetup | null = null;
|
||||
let refs = grant.credentialSecretRefs;
|
||||
if (input.action === "remove") {
|
||||
if (currentRef) await secretService(tx).remove(currentRef.secretId);
|
||||
refs = refs.filter((ref) => ref.configPath !== RAILWAY_SSH_SECRET_PATH);
|
||||
} else if (input.action === "prepare") {
|
||||
if (currentRef && typeof existing.publicKey === "string") return existing as unknown as RailwaySshSetup;
|
||||
const key = await generateRailwaySshKey();
|
||||
const ref = await createOrRotateOAuthSecret({ companyId, connection, configPath: RAILWAY_SSH_SECRET_PATH, label: "Railway container SSH key", value: key.privateKey, existingRefs: [], ownerUserId: grant.kind === "user" ? grant.subjectUserId ?? undefined : undefined, actor }, { dbClient: tx, secretClient: secretService(tx) });
|
||||
refs = [...refs.filter((ref) => ref.configPath !== RAILWAY_SSH_SECRET_PATH), ref];
|
||||
next = { grantId: grant.id, publicKey: key.publicKey, knownHosts: "", enabled: false };
|
||||
} else {
|
||||
if (!currentRef || typeof existing.publicKey !== "string") throw unprocessable("Generate and register a container key first.");
|
||||
next = { grantId: grant.id, publicKey: existing.publicKey, knownHosts, enabled: true };
|
||||
}
|
||||
await tx.update(connectionGrants).set({ credentialSecretRefs: refs, updatedAt: now() }).where(and(eq(connectionGrants.id, grant.id), eq(connectionGrants.companyId, companyId)));
|
||||
const config = { ...connection.config, railwaySsh: next };
|
||||
const [updated] = await tx.update(toolConnections).set({ config, transportConfig: config, updatedAt: now() }).where(and(eq(toolConnections.id, connectionId), eq(toolConnections.companyId, companyId))).returning();
|
||||
await syncCredentialBindings(updated, [], tx);
|
||||
return next;
|
||||
});
|
||||
await audit({ companyId, connectionId, action: "tool_connection.railway_ssh_updated", outcome: "success", actor, details: { action: input.action, grantId: input.grantId, enabled: setup?.enabled ?? false } });
|
||||
return setup;
|
||||
},
|
||||
|
||||
listAppsNeedingAttention,
|
||||
|
||||
sweepConnectionHealth,
|
||||
|
||||
@@ -85,6 +85,8 @@ import type {
|
||||
} from "./plugin-tool-dispatcher.js";
|
||||
import { logActivity, type LogActivityInput } from "./activity-log.js";
|
||||
import { secretService } from "./secrets.js";
|
||||
import { railwayCommandBudgetMs, createRailwayClient, isRailwayConnection, isRailwayEndpoint, isRailwayToolBlocked, normalizeRailwayToolName, RAILWAY_API_URL, RAILWAY_TOOL_PREFIX, RailwayError } from "./railway.js";
|
||||
import { RAILWAY_SSH_SECRET_PATH, runRailwaySshCommand } from "./railway-ssh.js";
|
||||
import {
|
||||
initializeMcpHttpSession,
|
||||
mcpHttpRequestHeaders,
|
||||
@@ -378,7 +380,7 @@ type RemoteHttpExecutionResult = {
|
||||
type RemoteHttpExecutionAudit = {
|
||||
transport: "mcp_remote";
|
||||
request: {
|
||||
protocol: "MCP JSON-RPC 2.0";
|
||||
protocol: "MCP JSON-RPC 2.0" | "Railway GraphQL" | "Railway GraphQL + SSH";
|
||||
httpMethod: "POST";
|
||||
endpoint: string;
|
||||
mcpMethod: "tools/call";
|
||||
@@ -1218,11 +1220,12 @@ export function createToolGatewayService(
|
||||
.orderBy(toolConnections.name, toolCatalogEntries.name);
|
||||
|
||||
const eligibleRows = rows.filter(
|
||||
({ connection, application }) =>
|
||||
(connection.transport === "mcp_remote" &&
|
||||
({ catalogEntry, connection, application }) =>
|
||||
!(isRailwayEndpoint(connection.config.url) && (isRailwayToolBlocked(catalogEntry.toolName) || (normalizeRailwayToolName(catalogEntry.toolName).startsWith(RAILWAY_TOOL_PREFIX) && connection.config.railwayApiStatus !== "available"))) &&
|
||||
((connection.transport === "mcp_remote" &&
|
||||
application.type === "mcp_http") ||
|
||||
(connection.transport === "local_stdio" &&
|
||||
application.type === "mcp_stdio"),
|
||||
application.type === "mcp_stdio")),
|
||||
);
|
||||
const baseNames = eligibleRows.map(
|
||||
({ catalogEntry, connection, application }) => {
|
||||
@@ -4672,6 +4675,9 @@ export function createToolGatewayService(
|
||||
},
|
||||
);
|
||||
}
|
||||
if (isRailwayEndpoint(connection.config.url) && isRailwayToolBlocked(entry.toolName)) {
|
||||
throw new ToolGatewayHttpError(403, "This Railway action cannot bind its effects to an approved target. Use redeploy, restart, or rollback for an existing deployment.", "railway_action_blocked");
|
||||
}
|
||||
return { entry, connection };
|
||||
}
|
||||
|
||||
@@ -5741,11 +5747,15 @@ export function createToolGatewayService(
|
||||
ms: number,
|
||||
invocationId: string,
|
||||
callerHeaders?: ExecuteGatewayToolInput["callerHeaders"],
|
||||
useDefaultTimeout = false,
|
||||
): Promise<RemoteHttpExecutionResult> {
|
||||
const { entry, connection } = await resolveConnectedRemoteTool(
|
||||
session,
|
||||
tool,
|
||||
);
|
||||
if (useDefaultTimeout && isRailwayConnection(connection) && entry.toolName === `${RAILWAY_TOOL_PREFIX}run-command`) {
|
||||
ms = railwayCommandBudgetMs(parameters);
|
||||
}
|
||||
const grant = await resolveConnectionGrant(session, connection);
|
||||
const composioScopeRevision = `${grant.id}:${grant.status}:${grant.updatedAt.toISOString()}`;
|
||||
const composioChild = composioChildConfig(connection);
|
||||
@@ -5802,6 +5812,35 @@ export function createToolGatewayService(
|
||||
// letting the tighter default cut a legitimately slow tool short.
|
||||
responseTimeoutMs: ms,
|
||||
});
|
||||
if (isRailwayEndpoint(connection.config.url) && normalizeRailwayToolName(entry.toolName).startsWith(RAILWAY_TOOL_PREFIX)) {
|
||||
if (!isRailwayConnection(connection) || connection.config.railwayApiStatus !== "available") {
|
||||
throw new ToolGatewayHttpError(422, "Railway API access is not verified. Refresh actions or reconnect this Railway connection.", "railway_api_not_verified");
|
||||
}
|
||||
const ssh = asRecord(connection.config.railwaySsh);
|
||||
const sshRef = grant.credentialSecretRefs.find((ref) => ref.configPath === RAILWAY_SSH_SECRET_PATH);
|
||||
execution.request.endpoint = RAILWAY_API_URL;
|
||||
execution.request.protocol = entry.toolName === `${RAILWAY_TOOL_PREFIX}run-command` ? "Railway GraphQL + SSH" : "Railway GraphQL";
|
||||
const client = createRailwayClient({
|
||||
authorization: credentialHeaders.Authorization ?? "",
|
||||
signal: controller.signal,
|
||||
request: dispatchRemote,
|
||||
runCommand: ssh?.grantId === grant.id && ssh?.enabled === true && sshRef
|
||||
? async (input) => runRailwaySshCommand({
|
||||
...input,
|
||||
privateKey: await resolveGrantSecretValue(session, connection, grant, sshRef),
|
||||
knownHosts: typeof ssh.knownHosts === "string" ? ssh.knownHosts : "",
|
||||
})
|
||||
: undefined,
|
||||
});
|
||||
const data = await client.call(entry.toolName, parameters);
|
||||
const record = asRecord(data);
|
||||
const failedCommand = entry.toolName === `${RAILWAY_TOOL_PREFIX}run-command` && (record?.exitCode !== 0 || record?.timedOut === true || record?.truncated === true);
|
||||
return {
|
||||
result: normalizeMcpToolResult({ content: [{ type: "text", text: JSON.stringify(data) }], structuredContent: data, isError: failedCommand }, "mcp_http", entry.toolName === `${RAILWAY_TOOL_PREFIX}run-command`, "railway"),
|
||||
headerSummary,
|
||||
execution,
|
||||
};
|
||||
}
|
||||
let requestHeaders = headers;
|
||||
if (connection.config.mcpSessionRequired === true) {
|
||||
requestHeaders = await initializeMcpHttpSession({
|
||||
@@ -6078,6 +6117,9 @@ export function createToolGatewayService(
|
||||
);
|
||||
return { result, headerSummary, execution };
|
||||
} catch (error) {
|
||||
if (error instanceof RailwayError) {
|
||||
throw new ToolGatewayHttpError(error.status, error.message, error.code, { connectionId: connection.id, catalogEntryId: entry.id, execution });
|
||||
}
|
||||
if (error instanceof ToolGatewayHttpError) {
|
||||
throw new ToolGatewayHttpError(
|
||||
error.status,
|
||||
@@ -6979,6 +7021,8 @@ export function createToolGatewayService(
|
||||
args.parameters,
|
||||
executionTimeoutMs,
|
||||
args.invocationId,
|
||||
undefined,
|
||||
args.timeoutMs === undefined,
|
||||
)
|
||||
: args.tool.providerType === "mcp_local_stdio"
|
||||
? await executeLocalStdioTool(
|
||||
@@ -10212,6 +10256,7 @@ export function createToolGatewayService(
|
||||
executionTimeoutMs,
|
||||
invocationId,
|
||||
input.callerHeaders,
|
||||
input.timeoutMs === undefined,
|
||||
)
|
||||
: tool.providerType === "mcp_local_stdio"
|
||||
? await executeLocalStdioTool(
|
||||
|
||||
Reference in new issue
Block a user