Compare commits

...
5 changed files with 215 additions and 7 deletions

No files matched your search

+5 -4
View File
@@ -16,6 +16,7 @@ import { formatHostPort } from "../domain/host";
import {
buildQuickConnectHost,
getQuickConnectDefaultPort,
isQuickConnectIdentityCompatible,
isQuickConnectIdentityUsable,
type QuickConnectAuthMethod,
type QuickConnectProtocol,
@@ -104,13 +105,13 @@ const QuickConnectWizard: React.FC<QuickConnectWizardProps> = ({
[keys, protocol, selectedIdentity],
);
const identityOptions = useMemo(
() => identities.map((identity) => ({
() => identities.filter((identity) => isQuickConnectIdentityCompatible(identity, target)).map((identity) => ({
value: identity.id,
label: identity.label,
sublabel: identity.username,
icon: <User size={14} />,
})),
[identities],
[identities, target],
);
const detachSelectedIdentity = () => {
@@ -131,7 +132,7 @@ const QuickConnectWizard: React.FC<QuickConnectWizardProps> = ({
return;
}
const identity = identities.find((candidate) => candidate.id === identityId);
if (!identity) return;
if (!identity || !isQuickConnectIdentityCompatible(identity, target)) return;
setSelectedIdentityId(identity.id);
setUsername(identity.username);
setAuthMethod(identity.authMethod);
@@ -431,7 +432,7 @@ const QuickConnectWizard: React.FC<QuickConnectWizardProps> = ({
</div>
</div>
{protocol !== "telnet" && identities.length > 0 && (
{protocol !== "telnet" && identityOptions.length > 0 && (
<div className="flex flex-col gap-4">
<div className="flex flex-col gap-3">
<Label className="text-sm font-medium">{t("quickConnect.identity.label")}</Label>
+121
View File
@@ -0,0 +1,121 @@
import test from "node:test";
import assert from "node:assert/strict";
import {
isQuickConnectInput,
parseQuickConnectInput,
} from "./quickConnect";
test("quick connect keeps plain user@host targets unchanged", () => {
assert.deepEqual(parseQuickConnectInput("root@10.2.0.8"), {
hostname: "10.2.0.8",
username: "root",
port: undefined,
});
assert.deepEqual(parseQuickConnectInput("10.2.0.8:2200"), {
hostname: "10.2.0.8",
username: undefined,
port: 2200,
});
});
test("quick connect keeps the asset selector in the JumpServer username", () => {
assert.deepEqual(parseQuickConnectInput("root@10.2.0.8@jump.corp.example"), {
hostname: "jump.corp.example",
username: "root@10.2.0.8",
port: undefined,
isJumpServerLogin: true,
});
assert.equal(isQuickConnectInput("root@10.2.0.8@jump.corp.example"), true);
});
test("quick connect parses the four-part JumpServer login from issue 3523", () => {
assert.deepEqual(
parseQuickConnectInput("chenyi@root@10.2.0.8@devjumpserver.example.cn"),
{
hostname: "devjumpserver.example.cn",
username: "chenyi@root@10.2.0.8",
port: undefined,
isJumpServerLogin: true,
},
);
});
test("quick connect applies a suffix port to the JumpServer endpoint", () => {
assert.deepEqual(parseQuickConnectInput("chenyi@root@10.2.0.8@jump:2222"), {
hostname: "jump",
username: "chenyi@root@10.2.0.8",
port: 2222,
isJumpServerLogin: true,
});
});
test("ssh command ports apply to the JumpServer endpoint", () => {
assert.deepEqual(parseQuickConnectInput("ssh -p 2222 chenyi@root@10.2.0.8@jump.corp.example"), {
hostname: "jump.corp.example",
username: "chenyi@root@10.2.0.8",
port: 2222,
isJumpServerLogin: true,
});
assert.deepEqual(parseQuickConnectInput("ssh -o Port=2200 chenyi@root@10.2.0.8@jump"), {
hostname: "jump",
username: "chenyi@root@10.2.0.8",
port: 2200,
isJumpServerLogin: true,
});
});
test("ssh HostName override keeps the JumpServer login name", () => {
assert.deepEqual(
parseQuickConnectInput("ssh -o HostName=actual-jump.example -p 2222 chenyi@root@10.2.0.8@jump"),
{
hostname: "actual-jump.example",
username: "chenyi@root@10.2.0.8",
port: 2222,
isJumpServerLogin: true,
},
);
assert.deepEqual(
parseQuickConnectInput("ssh -o HostName=actual-jump.example chenyi@root@10.2.0.8@alias_name"),
{
hostname: "actual-jump.example",
username: "chenyi@root@10.2.0.8",
port: undefined,
isJumpServerLogin: true,
},
);
});
test("ordinary SSH options still resolve single-@ targets", () => {
assert.deepEqual(parseQuickConnectInput("ssh -p 2200 deploy@host.example"), {
hostname: "host.example",
username: "deploy",
port: 2200,
});
assert.deepEqual(parseQuickConnectInput("ssh -o HostName=actual.example -l deploy alias"), {
hostname: "actual.example",
username: "deploy",
port: undefined,
});
assert.deepEqual(parseQuickConnectInput("ssh -o HostName=actual.example deploy@alias_name"), {
hostname: "actual.example",
username: "deploy",
port: undefined,
});
assert.deepEqual(parseQuickConnectInput("ssh -l user@realm host.example"), {
hostname: "host.example",
username: "user@realm",
port: undefined,
});
assert.deepEqual(parseQuickConnectInput("ssh -o User=user@realm host.example"), {
hostname: "host.example",
username: "user@realm",
port: undefined,
});
});
test("quick connect rejects malformed multi-@ inputs", () => {
assert.equal(parseQuickConnectInput("a@b@c@d@e"), null);
assert.equal(parseQuickConnectInput("bad user@10.2.0.8@jump"), null);
assert.equal(parseQuickConnectInput("root@10.2.0.8@"), null);
assert.equal(parseQuickConnectInput("root@bad host@jump"), null);
});
+61 -3
View File
@@ -2,6 +2,8 @@ export interface QuickConnectTarget {
hostname: string;
username?: string;
port?: number;
/** The @ separators in the address are part of a JumpServer login name. */
isJumpServerLogin?: true;
}
interface QuickConnectParseResult {
@@ -77,6 +79,47 @@ const parseDirectTarget = (input: string): QuickConnectTarget | null => {
};
};
/** Non-empty pieces of a JumpServer login name. */
const USERNAME_SEGMENT_RE = /^[^\s@]+$/;
const parseUsernameFromHostToken = (input: string): string | undefined => {
const segments = input.trim().split("@");
if (segments.length < 2 || segments.length > 4 || !segments[segments.length - 1]) {
return undefined;
}
const usernameSegments = segments.slice(0, -1);
return usernameSegments.every((segment) => USERNAME_SEGMENT_RE.test(segment))
? usernameSegments.join("@")
: undefined;
};
/**
* JumpServer selects the asset from the SSH login name. OpenSSH connects to
* the final host and sends every preceding segment as one username, including
* the @ separators. This is a single SSH connection, not a ProxyJump chain.
*/
const parseJumpServerTarget = (input: string): QuickConnectTarget | null => {
const trimmed = input.trim();
const atCount = (trimmed.match(/@/g) || []).length;
if (atCount < 2 || atCount > 3) return null;
const segments = trimmed.split("@");
const host = parseDirectTarget(segments[segments.length - 1]);
const username = parseUsernameFromHostToken(trimmed);
if (!host || !username) return null;
return {
hostname: host.hostname,
username,
port: host.port,
isJumpServerLogin: true,
};
};
/** Parse a quick connect target, accepting JumpServer login names. */
const parseTargetWithCompositeUser = (input: string): QuickConnectTarget | null =>
parseJumpServerTarget(input) ?? parseDirectTarget(input);
const sshArgOptions = new Set([
"-b",
"-c",
@@ -248,9 +291,21 @@ const parseSshCommand = (input: string): QuickConnectParseResult | null => {
if (!hostToken) return null;
const hostTokenTarget = parseTargetWithCompositeUser(hostToken);
const hostTokenAtCount = (hostToken.match(/@/g) || []).length;
const hasCompositeHostToken = hostTokenAtCount >= 2
&& hostTokenAtCount <= 3
&& Boolean(parseUsernameFromHostToken(hostToken));
const hostnameOverride = optionHostname ? parseDirectTarget(optionHostname) : null;
const base = optionHostname
? parseDirectTarget(optionHostname)
: parseDirectTarget(hostToken);
? hostnameOverride && {
...hostnameOverride,
username: hostTokenTarget?.username
?? parseUsernameFromHostToken(hostToken)
?? hostnameOverride.username,
...(hasCompositeHostToken ? { isJumpServerLogin: true as const } : {}),
}
: hostTokenTarget;
if (!base) return null;
if (portInvalid) return null;
@@ -273,6 +328,9 @@ const parseSshCommand = (input: string): QuickConnectParseResult | null => {
hostname: base.hostname,
username: optionUsername || username || base.username,
port: resolvedPort,
...(base.isJumpServerLogin && !optionUsername && !username
? { isJumpServerLogin: true as const }
: {}),
},
warnings: Array.from(new Set(warnings)),
};
@@ -288,7 +346,7 @@ export function parseQuickConnectInputWithWarnings(
const sshTarget = parseSshCommand(trimmed);
if (sshTarget) return sshTarget;
return { target: parseDirectTarget(trimmed), warnings: [] };
return { target: parseTargetWithCompositeUser(trimmed), warnings: [] };
}
// Parse user@host:port or ssh command formats
+22
View File
@@ -2,12 +2,34 @@ import assert from "node:assert/strict";
import test from "node:test";
import {
buildQuickConnectHost,
isQuickConnectIdentityCompatible,
isQuickConnectIdentityUsable,
} from "./quickConnectHost.ts";
import { resolveHostAuth } from "./sshAuth.ts";
const target = { hostname: "example.com" };
test("JumpServer login only offers identities with the full asset selector", () => {
const identity = {
id: "jump-user",
label: "Jump user",
username: "chenyi",
authMethod: "password" as const,
password: "secret",
created: 1,
};
const jumpTarget = { hostname: "jump.example", username: "chenyi@root@10.2.0.8", isJumpServerLogin: true as const };
assert.equal(isQuickConnectIdentityCompatible(identity, jumpTarget), false);
assert.equal(isQuickConnectIdentityCompatible(
{ ...identity, username: "other" }, jumpTarget,
), false);
assert.equal(isQuickConnectIdentityCompatible(
{ ...identity, username: jumpTarget.username }, jumpTarget,
), true);
assert.equal(isQuickConnectIdentityCompatible(identity, { hostname: "jump.example", username: "chenyi" }), true);
assert.equal(isQuickConnectIdentityCompatible(identity, { hostname: "host.example", username: "user@realm" }), true);
});
test("quick connect keeps a selected credential preset as the host identity", () => {
const host = buildQuickConnectHost({
id: "quick-1",
+6
View File
@@ -21,6 +21,12 @@ export const isQuickConnectIdentityUsable = (
return Boolean(identity.keyId && keys.some((key) => key.id === identity.keyId));
};
export const isQuickConnectIdentityCompatible = (
identity: Identity,
target: QuickConnectTarget,
): boolean => !target.isJumpServerLogin
|| identity.username === target.username;
type BuildQuickConnectHostInput = {
id: string;
createdAt: number;