From 33a329d65b97de5ef000e5f544647058f4d8b5b4 Mon Sep 17 00:00:00 2001 From: ZhuchkaTriplesix Date: Thu, 26 Nov 2026 02:47:37 +0300 Subject: [PATCH] feat(frontend): SSH bastion on New Connection forms Wire Flutter-parity SSH tunnel UI onto existing ssh_tunnel_* IPC for Postgres/MySQL/Redis/Mongo connect + test, and tear down on disconnect. --- apps/tauri/src/App.vue | 4 + apps/tauri/src/api/sshTunnel.test.ts | 44 ++++ apps/tauri/src/api/sshTunnel.ts | 112 ++++++++ apps/tauri/src/stores/connections.ts | 124 +++++++-- apps/tauri/src/ui/NewConnectionDialog.vue | 38 +++ apps/tauri/src/ui/SshTunnelSection.vue | 299 ++++++++++++++++++++++ 6 files changed, 602 insertions(+), 19 deletions(-) create mode 100644 apps/tauri/src/api/sshTunnel.test.ts create mode 100644 apps/tauri/src/api/sshTunnel.ts create mode 100644 apps/tauri/src/ui/SshTunnelSection.vue diff --git a/apps/tauri/src/App.vue b/apps/tauri/src/App.vue index 4dad15a5..622f8499 100644 --- a/apps/tauri/src/App.vue +++ b/apps/tauri/src/App.vue @@ -100,6 +100,7 @@ async function onConnectPostgresFromDialog(payload: PostgresConnectPayload) { database: payload.database, user: payload.user, password: payload.password, + ssh: payload.ssh, }); } catch { // status already set in store @@ -115,6 +116,7 @@ async function onConnectMysqlFromDialog(payload: MysqlConnectPayload) { database: payload.database, user: payload.user, password: payload.password, + ssh: payload.ssh, }); } catch { // status already set in store @@ -130,6 +132,7 @@ async function onConnectRedisFromDialog(payload: RedisConnectPayload) { database: payload.database, username: payload.username || null, password: payload.password || null, + ssh: payload.ssh, }); } catch { // status already set in store @@ -146,6 +149,7 @@ async function onConnectMongoFromDialog(payload: MongoConnectPayload) { username: payload.username || null, password: payload.password || null, authSource: payload.authSource || "admin", + ssh: payload.ssh, }); } catch { // status already set in store diff --git a/apps/tauri/src/api/sshTunnel.test.ts b/apps/tauri/src/api/sshTunnel.test.ts new file mode 100644 index 00000000..8b948292 --- /dev/null +++ b/apps/tauri/src/api/sshTunnel.test.ts @@ -0,0 +1,44 @@ +import { describe, expect, it } from "vitest"; +import { + emptySshTunnelForm, + sshFormIsValid, + toOpenRequest, +} from "./sshTunnel"; + +describe("sshTunnel helpers", () => { + it("treats disabled form as valid", () => { + expect(sshFormIsValid(emptySshTunnelForm())).toBe(true); + }); + + it("requires password auth fields when enabled", () => { + const form = emptySshTunnelForm(); + form.enabled = true; + form.host = "bastion"; + form.username = "ubuntu"; + form.authType = "password"; + expect(sshFormIsValid(form)).toBe(false); + form.password = "secret"; + expect(sshFormIsValid(form)).toBe(true); + }); + + it("maps form to open request", () => { + const form = emptySshTunnelForm(); + form.enabled = true; + form.host = "bastion"; + form.port = 2222; + form.username = "ubuntu"; + form.authType = "privateKey"; + form.privateKeyPath = "/tmp/id_ed25519"; + form.passphrase = "x"; + expect(toOpenRequest(form, "10.0.0.5", 5432)).toEqual({ + bastionHost: "bastion", + bastionPort: 2222, + username: "ubuntu", + password: null, + privateKeyPath: "/tmp/id_ed25519", + privateKeyPassphrase: "x", + remoteHost: "10.0.0.5", + remotePort: 5432, + }); + }); +}); diff --git a/apps/tauri/src/api/sshTunnel.ts b/apps/tauri/src/api/sshTunnel.ts new file mode 100644 index 00000000..8dc5980d --- /dev/null +++ b/apps/tauri/src/api/sshTunnel.ts @@ -0,0 +1,112 @@ +import { invoke } from "@tauri-apps/api/core"; + +export type SshAuthType = "password" | "privateKey"; + +/** Non-secret + session secrets for the New Connection SSH section. */ +export type SshTunnelForm = { + enabled: boolean; + host: string; + port: number; + username: string; + authType: SshAuthType; + password: string; + privateKeyPath: string; + passphrase: string; +}; + +export type SshTunnelOpenRequest = { + bastionHost: string; + bastionPort: number; + username: string; + password?: string | null; + privateKeyPem?: string | null; + privateKeyPath?: string | null; + privateKeyPassphrase?: string | null; + remoteHost: string; + remotePort: number; +}; + +export type SshTunnelInfo = { + id: string; + localHost: string; + localPort: number; + remoteHost: string; + remotePort: number; +}; + +export function emptySshTunnelForm(): SshTunnelForm { + return { + enabled: false, + host: "", + port: 22, + username: "", + authType: "password", + password: "", + privateKeyPath: "", + passphrase: "", + }; +} + +export function sshFormIsValid(form: SshTunnelForm): boolean { + if (!form.enabled) return true; + const port = Number(form.port); + return ( + form.host.trim().length > 0 && + form.username.trim().length > 0 && + Number.isFinite(port) && + port > 0 && + port <= 65535 && + (form.authType === "password" + ? form.password.length > 0 + : form.privateKeyPath.trim().length > 0) + ); +} + +export function toOpenRequest( + form: SshTunnelForm, + remoteHost: string, + remotePort: number, +): SshTunnelOpenRequest { + return { + bastionHost: form.host.trim(), + bastionPort: Number(form.port) || 22, + username: form.username.trim(), + password: + form.authType === "password" ? form.password || null : null, + privateKeyPath: + form.authType === "privateKey" + ? form.privateKeyPath.trim() || null + : null, + privateKeyPassphrase: + form.authType === "privateKey" + ? form.passphrase || null + : null, + remoteHost, + remotePort, + }; +} + +export async function sshTunnelOpen( + request: SshTunnelOpenRequest, +): Promise { + return invoke("ssh_tunnel_open", { request }); +} + +export async function sshTunnelClose(id: string): Promise { + return invoke("ssh_tunnel_close", { id }); +} + +export async function sshTunnelList(): Promise { + return invoke("ssh_tunnel_list"); +} + +/** Open then close — verifies bastion auth + forward path. */ +export async function sshTunnelTest( + form: SshTunnelForm, + remoteHost: string, + remotePort: number, +): Promise { + const info = await sshTunnelOpen(toOpenRequest(form, remoteHost, remotePort)); + await sshTunnelClose(info.id).catch(() => undefined); + return info; +} diff --git a/apps/tauri/src/stores/connections.ts b/apps/tauri/src/stores/connections.ts index 8fd81443..9b5f8f2c 100644 --- a/apps/tauri/src/stores/connections.ts +++ b/apps/tauri/src/stores/connections.ts @@ -13,6 +13,12 @@ import { } from "../api/connection"; import { profilesConnectMongo } from "../api/mongo"; import { profilesConnectRedis } from "../api/redis"; +import { + sshTunnelClose, + sshTunnelOpen, + toOpenRequest, + type SshTunnelForm, +} from "../api/sshTunnel"; import { DEMO_CONNECTION } from "../mock/demoCatalog"; import { useFoldersStore } from "./folders"; import { useShellNavStore } from "./shellNav"; @@ -34,6 +40,11 @@ export type LiveConnection = { folderId?: string | null; /** Live session open in the registry. */ connected?: boolean; + /** Active SSH local-forward id (session-only). */ + sshTunnelId?: string | null; + /** Bastion target as typed by the user (before local forward). */ + sshRemoteHost?: string | null; + sshRemotePort?: number | null; }; /** Session-only passwords — never written to ProfileStore (keyring: #1878). */ @@ -47,8 +58,9 @@ function fileName(path: string): string { function endpointLabel(conn: LiveConnection): string { if (conn.mock) return "Demo (offline)"; if (conn.driver === "sqlite") return conn.path ?? ""; - const host = conn.host ?? "localhost"; + const host = conn.sshRemoteHost ?? conn.host ?? "localhost"; const port = + conn.sshRemotePort ?? conn.port ?? (conn.driver === "mysql" ? 3306 @@ -58,7 +70,24 @@ function endpointLabel(conn: LiveConnection): string { ? 27017 : 5432); const db = conn.database ?? (conn.driver === "redis" ? "0" : ""); - return `${host}:${port}/${db}`; + const via = conn.sshTunnelId ? " · via SSH" : ""; + return `${host}:${port}/${db}${via}`; +} + +async function openSshIfNeeded( + ssh: SshTunnelForm | null | undefined, + remoteHost: string, + remotePort: number, +): Promise<{ host: string; port: number; tunnelId: string | null }> { + if (!ssh?.enabled) { + return { host: remoteHost, port: remotePort, tunnelId: null }; + } + const info = await sshTunnelOpen(toOpenRequest(ssh, remoteHost, remotePort)); + return { + host: info.localHost, + port: info.localPort, + tunnelId: info.id, + }; } function fromProfile(p: ConnectionProfile, connected = false): LiveConnection | null { @@ -307,6 +336,7 @@ export const useConnectionsStore = defineStore("connections", () => { password?: string | null; folderId?: string | null; id?: string | null; + ssh?: SshTunnelForm | null; }) { return connectServerDriver("postgres", opts); } @@ -320,6 +350,7 @@ export const useConnectionsStore = defineStore("connections", () => { password?: string | null; folderId?: string | null; id?: string | null; + ssh?: SshTunnelForm | null; }) { return connectServerDriver("mysql", opts); } @@ -333,6 +364,7 @@ export const useConnectionsStore = defineStore("connections", () => { password?: string | null; folderId?: string | null; id?: string | null; + ssh?: SshTunnelForm | null; }) { const status = useShellStatusStore(); const nav = useShellNavStore(); @@ -340,13 +372,24 @@ export const useConnectionsStore = defineStore("connections", () => { opts.name.trim() || `${opts.host}:${opts.port}/${opts.database ?? "0"}`; status.setQueryMetrics({ busy: true, - message: `Connecting Redis ${opts.host}:${opts.port}…`, + message: opts.ssh?.enabled + ? `Opening SSH tunnel to ${opts.host}:${opts.port}…` + : `Connecting Redis ${opts.host}:${opts.port}…`, }); + let tunnelId: string | null = null; try { + const forward = await openSshIfNeeded(opts.ssh, opts.host, opts.port); + tunnelId = forward.tunnelId; + if (tunnelId) { + status.setQueryMetrics({ + busy: true, + message: `Connecting Redis via SSH ${forward.host}:${forward.port}…`, + }); + } const profile = await profilesConnectRedis({ name, - host: opts.host, - port: opts.port, + host: forward.host, + port: forward.port, database: opts.database ?? "0", username: opts.username ?? null, password: opts.password ?? null, @@ -358,12 +401,15 @@ export const useConnectionsStore = defineStore("connections", () => { id: profile.id, driver: "redis", name: profile.name, - host: profile.host ?? opts.host, - port: profile.port != null ? Number(profile.port) : opts.port, + host: profile.host ?? forward.host, + port: profile.port != null ? Number(profile.port) : forward.port, database: profile.database_name ?? opts.database ?? "0", user: profile.username ?? opts.username ?? undefined, folderId: profile.folder_id ?? opts.folderId ?? null, connected: true, + sshTunnelId: tunnelId, + sshRemoteHost: tunnelId ? opts.host : null, + sshRemotePort: tunnelId ? opts.port : null, }; upsert(conn); nav.setExpanded(conn.id, true); @@ -375,6 +421,7 @@ export const useConnectionsStore = defineStore("connections", () => { }); return conn; } catch (err) { + if (tunnelId) await sshTunnelClose(tunnelId).catch(() => undefined); const msg = formatConnectionError(err); status.setQueryMetrics({ busy: false, message: msg }); throw err; @@ -391,6 +438,7 @@ export const useConnectionsStore = defineStore("connections", () => { authSource?: string | null; folderId?: string | null; id?: string | null; + ssh?: SshTunnelForm | null; }) { const status = useShellStatusStore(); const nav = useShellNavStore(); @@ -399,13 +447,24 @@ export const useConnectionsStore = defineStore("connections", () => { `${opts.host}:${opts.port}/${opts.database}`; status.setQueryMetrics({ busy: true, - message: `Connecting Mongo ${opts.host}:${opts.port}…`, + message: opts.ssh?.enabled + ? `Opening SSH tunnel to ${opts.host}:${opts.port}…` + : `Connecting Mongo ${opts.host}:${opts.port}…`, }); + let tunnelId: string | null = null; try { + const forward = await openSshIfNeeded(opts.ssh, opts.host, opts.port); + tunnelId = forward.tunnelId; + if (tunnelId) { + status.setQueryMetrics({ + busy: true, + message: `Connecting Mongo via SSH ${forward.host}:${forward.port}…`, + }); + } const profile = await profilesConnectMongo({ name, - host: opts.host, - port: opts.port, + host: forward.host, + port: forward.port, database: opts.database, username: opts.username ?? null, password: opts.password ?? null, @@ -418,12 +477,15 @@ export const useConnectionsStore = defineStore("connections", () => { id: profile.id, driver: "mongo", name: profile.name, - host: profile.host ?? opts.host, - port: profile.port != null ? Number(profile.port) : opts.port, + host: profile.host ?? forward.host, + port: profile.port != null ? Number(profile.port) : forward.port, database: profile.database_name ?? opts.database, user: profile.username ?? opts.username ?? undefined, folderId: profile.folder_id ?? opts.folderId ?? null, connected: true, + sshTunnelId: tunnelId, + sshRemoteHost: tunnelId ? opts.host : null, + sshRemotePort: tunnelId ? opts.port : null, }; upsert(conn); nav.setExpanded(conn.id, true); @@ -435,6 +497,7 @@ export const useConnectionsStore = defineStore("connections", () => { }); return conn; } catch (err) { + if (tunnelId) await sshTunnelClose(tunnelId).catch(() => undefined); const msg = formatConnectionError(err); status.setQueryMetrics({ busy: false, message: msg }); throw err; @@ -452,6 +515,7 @@ export const useConnectionsStore = defineStore("connections", () => { password?: string | null; folderId?: string | null; id?: string | null; + ssh?: SshTunnelForm | null; }, ) { const status = useShellStatusStore(); @@ -461,15 +525,26 @@ export const useConnectionsStore = defineStore("connections", () => { `${opts.user}@${opts.host}/${opts.database}`; status.setQueryMetrics({ busy: true, - message: `Connecting ${opts.host}:${opts.port}…`, + message: opts.ssh?.enabled + ? `Opening SSH tunnel to ${opts.host}:${opts.port}…` + : `Connecting ${opts.host}:${opts.port}…`, }); + let tunnelId: string | null = null; try { + const forward = await openSshIfNeeded(opts.ssh, opts.host, opts.port); + tunnelId = forward.tunnelId; + if (tunnelId) { + status.setQueryMetrics({ + busy: true, + message: `Connecting via SSH ${forward.host}:${forward.port}…`, + }); + } const profile = driver === "postgres" ? await profilesConnectPostgres({ name, - host: opts.host, - port: opts.port, + host: forward.host, + port: forward.port, database: opts.database, user: opts.user, password: opts.password ?? null, @@ -478,8 +553,8 @@ export const useConnectionsStore = defineStore("connections", () => { }) : await profilesConnectMysql({ name, - host: opts.host, - port: opts.port, + host: forward.host, + port: forward.port, database: opts.database, user: opts.user, password: opts.password ?? null, @@ -491,12 +566,15 @@ export const useConnectionsStore = defineStore("connections", () => { id: profile.id, driver, name: profile.name, - host: profile.host ?? opts.host, - port: profile.port != null ? Number(profile.port) : opts.port, + host: profile.host ?? forward.host, + port: profile.port != null ? Number(profile.port) : forward.port, database: profile.database_name ?? opts.database, user: profile.username ?? opts.user, folderId: profile.folder_id ?? opts.folderId ?? null, connected: true, + sshTunnelId: tunnelId, + sshRemoteHost: tunnelId ? opts.host : null, + sshRemotePort: tunnelId ? opts.port : null, }; upsert(conn); nav.setExpanded(conn.id, true); @@ -508,6 +586,7 @@ export const useConnectionsStore = defineStore("connections", () => { }); return conn; } catch (err) { + if (tunnelId) await sshTunnelClose(tunnelId).catch(() => undefined); const msg = formatConnectionError(err); status.setQueryMetrics({ busy: false, message: msg }); throw err; @@ -574,6 +653,10 @@ export const useConnectionsStore = defineStore("connections", () => { try { if (conn && !conn.mock && conn.connected) await dbDisconnect(id); } finally { + if (conn?.sshTunnelId) { + await sshTunnelClose(conn.sshTunnelId).catch(() => undefined); + conn.sshTunnelId = null; + } if (conn && !conn.mock) { conn.connected = false; byId.set(id, conn); @@ -603,6 +686,9 @@ export const useConnectionsStore = defineStore("connections", () => { /* already closed */ } } + if (conn.sshTunnelId) { + await sshTunnelClose(conn.sshTunnelId).catch(() => undefined); + } await profilesDelete(id); } } finally { diff --git a/apps/tauri/src/ui/NewConnectionDialog.vue b/apps/tauri/src/ui/NewConnectionDialog.vue index fa731679..4694ef3f 100644 --- a/apps/tauri/src/ui/NewConnectionDialog.vue +++ b/apps/tauri/src/ui/NewConnectionDialog.vue @@ -3,7 +3,13 @@ import { computed, nextTick, onUnmounted, ref, watch } from "vue"; // Must not be named `open` — it would shadow the `open` prop/model in the // template and make `v-if="open"` always truthy (Save connected, dialog stuck). import { open as pickSqliteFile } from "@tauri-apps/plugin-dialog"; +import { + emptySshTunnelForm, + sshFormIsValid, + type SshTunnelForm, +} from "../api/sshTunnel"; import QueryaButton from "./QueryaButton.vue"; +import SshTunnelSection from "./SshTunnelSection.vue"; import { QueryaIcon } from "./icons"; export type BuiltInDriverId = @@ -27,6 +33,7 @@ export type PostgresConnectPayload = { database: string; user: string; password: string; + ssh?: SshTunnelForm; }; export type MysqlConnectPayload = { @@ -37,6 +44,7 @@ export type MysqlConnectPayload = { database: string; user: string; password: string; + ssh?: SshTunnelForm; }; export type RedisConnectPayload = { @@ -47,6 +55,7 @@ export type RedisConnectPayload = { database: string; username: string; password: string; + ssh?: SshTunnelForm; }; export type MongoConnectPayload = { @@ -58,6 +67,7 @@ export type MongoConnectPayload = { username: string; password: string; authSource: string; + ssh?: SshTunnelForm; }; type Category = "all" | "sql" | "nosql"; @@ -148,6 +158,7 @@ const database = ref("querya"); const user = ref("querya"); const password = ref("querya"); const authSource = ref("admin"); +const ssh = ref(emptySshTunnelForm()); const error = ref(null); const stubDriver = ref(null); @@ -225,10 +236,25 @@ function reset() { database.value = "querya"; user.value = "querya"; password.value = "querya"; + authSource.value = "admin"; + ssh.value = emptySshTunnelForm(); error.value = null; stubDriver.value = null; } +function sshPayload(): SshTunnelForm | undefined { + return ssh.value.enabled ? { ...ssh.value } : undefined; +} + +function requireSshValid(): boolean { + if (sshFormIsValid(ssh.value)) return true; + error.value = + ssh.value.authType === "privateKey" + ? "SSH bastion needs host, user, and a private key path." + : "SSH bastion needs host, user, and password."; + return false; +} + watch(open, async (v) => { if (!v) return; reset(); @@ -371,6 +397,7 @@ function submitPostgres() { error.value = "Host, port, database, and user are required."; return; } + if (!requireSshValid()) return; dismiss(); emit("connectPostgres", { driver: "postgresql", @@ -380,6 +407,7 @@ function submitPostgres() { database: db, user: u, password: password.value, + ssh: sshPayload(), }); } @@ -397,6 +425,7 @@ function submitRedis() { error.value = "Host and port are required."; return; } + if (!requireSshValid()) return; dismiss(); emit("connectRedis", { driver: "redis", @@ -406,6 +435,7 @@ function submitRedis() { database: db, username: user.value.trim(), password: password.value, + ssh: sshPayload(), }); } @@ -423,6 +453,7 @@ function submitMongo() { error.value = "Host, port and database are required."; return; } + if (!requireSshValid()) return; dismiss(); emit("connectMongo", { driver: "mongodb", @@ -433,6 +464,7 @@ function submitMongo() { username: user.value.trim(), password: password.value, authSource: authSource.value.trim() || "admin", + ssh: sshPayload(), }); } @@ -451,6 +483,7 @@ function submitMysql() { error.value = "Host, port, database and user are required."; return; } + if (!requireSshValid()) return; dismiss(); emit("connectMysql", { driver: "mysql", @@ -460,6 +493,7 @@ function submitMysql() { database: db, user: u, password: password.value, + ssh: sshPayload(), }); } @@ -734,6 +768,7 @@ onUnmounted(() => window.removeEventListener("keydown", onKey, true));

Password is stored in the OS keyring (not in the profile database).

+

{{ error }}

@@ -809,6 +844,7 @@ onUnmounted(() => window.removeEventListener("keydown", onKey, true));

Password is stored in the OS keyring (not in the profile database).

+

{{ error }}

@@ -877,6 +913,7 @@ onUnmounted(() => window.removeEventListener("keydown", onKey, true)); placeholder="optional" /> +

{{ error }}