mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-10-30 15:15:24 +00:00
fix: prompt for jump host TOTP in terminal, file manager and metrics (#1312)
This commit is contained in:
1 parent
09259de01c
commit
41d0c0ab85
6 files changed
+211
-8
No files matched your search
@@ -1,5 +1,8 @@
|
||||
import { Client, type ConnectConfig } from "ssh2";
|
||||
import type { PluginSsh } from "@termix/plugin-sdk/backend";
|
||||
import type {
|
||||
PluginSsh,
|
||||
PluginSshPromptChannel,
|
||||
} from "@termix/plugin-sdk/backend";
|
||||
import {
|
||||
connectionLog,
|
||||
newSessionId,
|
||||
@@ -217,8 +220,48 @@ export async function startInteractive(
|
||||
connectionLog("info", "proxy", "Connecting via jump host chain"),
|
||||
);
|
||||
}
|
||||
const jumpHostPrompt: PluginSshPromptChannel = {
|
||||
ask: (request) =>
|
||||
new Promise((answer) => {
|
||||
if (request.kind !== "totp" || settled) {
|
||||
answer(null);
|
||||
return;
|
||||
}
|
||||
const sessionId = newSessionId("totp");
|
||||
sessions.addPending({
|
||||
client,
|
||||
finish: (responses) => answer(responses[0] ?? null),
|
||||
config,
|
||||
createdAt: Date.now(),
|
||||
sessionId,
|
||||
hostId: host.id,
|
||||
userId,
|
||||
prompts: [{ prompt: request.prompt, echo: false }],
|
||||
totpPromptIndex: 0,
|
||||
resolvedPassword: host.password ?? undefined,
|
||||
totpAttempts: 0,
|
||||
});
|
||||
logs.push(
|
||||
connectionLog(
|
||||
"info",
|
||||
"stats_totp",
|
||||
"Jump host TOTP verification required",
|
||||
),
|
||||
);
|
||||
settle(() =>
|
||||
resolve({
|
||||
success: false,
|
||||
requires_totp: true,
|
||||
sessionId,
|
||||
prompt: request.prompt,
|
||||
}),
|
||||
);
|
||||
}),
|
||||
};
|
||||
ssh
|
||||
.openTransport(host, config as Record<string, unknown>)
|
||||
.openTransport(host, config as Record<string, unknown>, {
|
||||
prompt: jumpHostPrompt,
|
||||
})
|
||||
.then((transport) => {
|
||||
if (transport.jumpClient) {
|
||||
const jumpClient = transport.jumpClient as Client;
|
||||
@@ -226,7 +269,9 @@ export async function startInteractive(
|
||||
}
|
||||
client.connect(config);
|
||||
})
|
||||
.catch((error) =>
|
||||
.catch((error) => {
|
||||
// Already answered with a jump host prompt: fail the pending submit.
|
||||
if (settled) client.emit("error", error);
|
||||
settle(() => {
|
||||
logs.push(
|
||||
connectionLog(
|
||||
@@ -236,8 +281,8 @@ export async function startInteractive(
|
||||
),
|
||||
);
|
||||
reject(error);
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
},
|
||||
);
|
||||
return { status: 200, body: { ...result, connectionLogs: logs } };
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { PluginSsh } from "@termix/plugin-sdk/backend";
|
||||
import { startInteractive } from "../../src/backend/interactive.js";
|
||||
import type { MetricsHost } from "../../src/backend/helpers.js";
|
||||
|
||||
describe("startInteractive through a jump host", () => {
|
||||
it("returns requires_totp when a jump host asks for a code", async () => {
|
||||
let answer: Promise<string | null> | null = null;
|
||||
const ssh = {
|
||||
prepare: vi.fn(async () => ({
|
||||
config: {},
|
||||
outcome: { status: "ready" },
|
||||
})),
|
||||
openTransport: vi.fn(
|
||||
(
|
||||
_host: unknown,
|
||||
_config: unknown,
|
||||
options?: {
|
||||
prompt?: { ask: (r: unknown) => Promise<string | null> };
|
||||
},
|
||||
) => {
|
||||
answer = options!.prompt!.ask({
|
||||
kind: "totp",
|
||||
prompt: "Verification code:",
|
||||
retry: false,
|
||||
});
|
||||
return new Promise(() => {});
|
||||
},
|
||||
),
|
||||
} as unknown as PluginSsh;
|
||||
const pending: Array<{ finish: (r: string[]) => void }> = [];
|
||||
const sessions = {
|
||||
get: () => undefined,
|
||||
addPending: (session: { finish: (r: string[]) => void }) =>
|
||||
pending.push(session),
|
||||
};
|
||||
const result = await startInteractive(
|
||||
{
|
||||
ssh,
|
||||
sessions: sessions as never,
|
||||
log: { info: vi.fn(), warn: vi.fn(), error: vi.fn() } as never,
|
||||
registerViewer: vi.fn(),
|
||||
},
|
||||
{
|
||||
id: 7,
|
||||
ip: "10.0.0.1",
|
||||
port: 22,
|
||||
jumpHosts: [{ hostId: 2 }],
|
||||
} as MetricsHost,
|
||||
"u1",
|
||||
);
|
||||
|
||||
expect(result.body).toMatchObject({
|
||||
requires_totp: true,
|
||||
prompt: "Verification code:",
|
||||
});
|
||||
pending[0].finish(["123456"]);
|
||||
await expect(answer).resolves.toBe("123456");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user