Skip to content

Commit

Permalink
ssh: Add Linux ssh-agent support
Browse files Browse the repository at this point in the history
  • Loading branch information
sedwards2009 committed Jun 9, 2024
1 parent bb19059 commit f8da11c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
12 changes: 12 additions & 0 deletions extensions/SSHSessionBackend/src/SSHPty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface PtyOptions {
username: string;
privateKeyFilenames?: string[];
tryPasswordAuth: boolean;
agentSocketPath?: string;
}

enum PtyState {
Expand All @@ -48,6 +49,7 @@ export class SSHPty implements Pty {
#password: string = "";

#ptyOptions: PtyOptions = null;
#tryAgentAuth = false;
#verifyCallback: ssh2.VerifyCallback = null;

#permittedDataSize = 0;
Expand Down Expand Up @@ -154,11 +156,14 @@ export class SSHPty implements Pty {
});
});

this.#tryAgentAuth = options.agentSocketPath !== undefined;

this.#sshConnection.connect({
host: options.host,
port: options.port,
username: options.username,
tryKeyboard: true,
agent: options.agentSocketPath,
authHandler: (
methodsLeft: ssh2.AuthenticationType[],
partialSuccess: boolean,
Expand Down Expand Up @@ -359,6 +364,12 @@ export class SSHPty implements Pty {
partialSuccess: boolean,
callback: ssh2.NextAuthHandler): void {

if (this.#tryAgentAuth) {
this.#tryAgentAuth = false;
callback({type: "agent", agent: this.#ptyOptions.agentSocketPath, username: this.#ptyOptions.username});
return;
}

while (this.#remainingPrivateKeyFilenames.length !== 0) {
const keyFilename = this.#remainingPrivateKeyFilenames.pop();
if (this.#handlePrivateKeyAuth(keyFilename, callback)) {
Expand All @@ -368,6 +379,7 @@ export class SSHPty implements Pty {

if (this.#tryPasswordAuth) {
this.#startPasswordInput(callback);
return;
}

callback(<any>false);
Expand Down
25 changes: 22 additions & 3 deletions extensions/SSHSessionBackend/src/SSHSessionBackendExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import { PtyOptions, SSHPty } from "./SSHPty";
enum AuthenticationMethod {
DEFAULT_KEYS_PASSWORD,
PASSWORD_ONLY,
KEY_FILE_ONLY
KEY_FILE_ONLY,
SSH_AGENT_ONLY,
};

// Note: This is duplicated in SSHSessionEditorExtension.ts.
Expand Down Expand Up @@ -63,12 +64,15 @@ class SSHBackend implements SessionBackend {
tryPasswordAuth = true;
break;

case AuthenticationMethod.KEY_FILE_ONLY:
privateKeyFilenames.push(sessionConfig.keyFilePath);
break;

case AuthenticationMethod.PASSWORD_ONLY:
tryPasswordAuth = true;
break;

case AuthenticationMethod.KEY_FILE_ONLY:
privateKeyFilenames.push(sessionConfig.keyFilePath);
default:
break;
}

Expand All @@ -82,11 +86,26 @@ class SSHBackend implements SessionBackend {
username: username,
privateKeyFilenames,
tryPasswordAuth,
agentSocketPath: this.#createAgentSocketPath(sessionConfig),
};

return new SSHPty(this._log, options);
}

#createAgentSocketPath(sessionConfig: SSHSessionConfiguration): string {
const needAgent = sessionConfig.authenicationMethod === AuthenticationMethod.DEFAULT_KEYS_PASSWORD ||
sessionConfig.authenicationMethod === AuthenticationMethod.SSH_AGENT_ONLY;
if (! needAgent) {
return undefined;
}

if (process.platform === "win32") {
return undefined;
} else {
return process.env.SSH_AUTH_SOCK;
}
}

#createEnv(sessionOptions: CreateSessionOptions): EnvironmentMap {
const ptyEnv: EnvironmentMap = {};
const processEnv = process.env;
Expand Down
10 changes: 8 additions & 2 deletions extensions/SSHSessionEditor/src/SSHSessionEditorExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ let log: Logger = null;
enum AuthenticationMethod {
DEFAULT_KEYS_PASSWORD,
PASSWORD_ONLY,
KEY_FILE_ONLY
KEY_FILE_ONLY,
SSH_AGENT_ONLY,
};

const AUTHENTICATION_METHOD_LABELS = ["Default OpenSSH keys, Password", "Password only", "Key file only"];
const AUTHENTICATION_METHOD_LABELS = [
"SSH Agent, Default OpenSSH keys, Password",
"Password only",
"Key file only",
"SSH Agent only"
];

// Note: This is duplicated in SSHSessionBackendExtension.ts.
interface SSHSessionConfiguration extends SessionConfiguration {
Expand Down

0 comments on commit f8da11c

Please sign in to comment.