Skip to content

Commit

Permalink
feat: added method to validate the ssh connection
Browse files Browse the repository at this point in the history
Signed-off-by: Fernando Rijo Cedeno <37381190+zFernand0@users.noreply.github.com>
  • Loading branch information
zFernand0 committed Nov 1, 2024
1 parent 97402aa commit 2d918c7
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions packages/zosuss/src/Shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
*/

import { Logger, ImperativeError } from "@zowe/imperative";
import { Logger, ImperativeError, IProfileLoaded } from "@zowe/imperative";

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import IProfileLoaded.
import { ClientChannel, Client } from "ssh2";
import { SshSession } from "./SshSession";
import { ZosUssMessages } from "./constants/ZosUss.messages";
Expand All @@ -24,19 +24,9 @@ export class Shell {
public static executeSsh(session: SshSession,
command: string,
stdoutHandler: (data: string) => void): Promise<any> {
const authsAllowed = ["none"];
let hasAuthFailed = false;
const promise = new Promise<any>((resolve, reject) => {
const conn = new Client();

// These are needed for authenticationHandler
// The order is critical as this is the order of authentication that will be used.
if (session.ISshSession.privateKey != null && session.ISshSession.privateKey !== "undefined") {
authsAllowed.push("publickey");
}
if (session.ISshSession.password != null && session.ISshSession.password !== "undefined") {
authsAllowed.push("password");
}
conn.on("ready", () => {
conn.shell((err: any, stream: ClientChannel) => {
if (err) { throw err; }
Expand Down Expand Up @@ -77,6 +67,7 @@ export class Shell {
return;
}
dataBuffer += data;

if (dataBuffer.includes("\r")) {
// when data is not received with complete lines,
// slice the last incomplete line and put it back to dataBuffer until it gets the complete line,
Expand All @@ -101,6 +92,7 @@ export class Shell {
else if (isUserCommand && dataToPrint.length != 0) {
if (!dataToPrint.startsWith('\r\n$ '+cmd) && !dataToPrint.startsWith('\r<')){
//only prints command output
if (dataToPrint.startsWith("\r\n$ ")) dataToPrint = dataToPrint.replace(/\r\n\$\s/, "\r\n");
stdoutHandler(dataToPrint);
dataToPrint = "";
}
Expand Down Expand Up @@ -140,22 +132,37 @@ export class Shell {
}));
}
});
conn.connect({
host: session.ISshSession.hostname,
port: session.ISshSession.port,
username: session.ISshSession.user,
password: session.ISshSession.password,
privateKey: session.ISshSession.privateKey != null && session.ISshSession.privateKey !== "undefined" ?
require("fs").readFileSync(session.ISshSession.privateKey) : "",
passphrase: session.ISshSession.keyPassphrase,
authHandler: this.authenticationHandler(authsAllowed),
readyTimeout: session.ISshSession.handshakeTimeout != null && session.ISshSession.handshakeTimeout !== undefined ?
session.ISshSession.handshakeTimeout : 0
} as any);
Shell.connect(conn, session);
});
return promise;
}

private static connect(connection: Client, session: SshSession) {
const authsAllowed = ["none"];

// These are needed for authenticationHandler
// The order is critical as this is the order of authentication that will be used.
if (session.ISshSession.privateKey != null && session.ISshSession.privateKey !== "undefined") {
authsAllowed.push("publickey");
}
if (session.ISshSession.password != null && session.ISshSession.password !== "undefined") {
authsAllowed.push("password");
}

connection.connect({
host: session.ISshSession.hostname,
port: session.ISshSession.port,
username: session.ISshSession.user,
password: session.ISshSession.password,
privateKey: session.ISshSession.privateKey != null && session.ISshSession.privateKey !== "undefined" ?
require("fs").readFileSync(session.ISshSession.privateKey) : "",
passphrase: session.ISshSession.keyPassphrase,
authHandler: this.authenticationHandler(authsAllowed),
readyTimeout: session.ISshSession.handshakeTimeout != null && session.ISshSession.handshakeTimeout !== undefined ?
session.ISshSession.handshakeTimeout : 0
} as any);
}

public static async executeSshCwd(session: SshSession,
command: string,
cwd: string,
Expand All @@ -164,6 +171,15 @@ export class Shell {
return this.executeSsh(session, cwdCommand, stdoutHandler);
}

public static async isConnectionValid(session: SshSession): Promise<boolean>{
return new Promise((resolve, _) => {
const conn = new Client();
conn.on("ready", () => conn.end() && resolve(true))
.on("error", () => resolve(false));
Shell.connect(conn, session);
});
}

private static authenticationHandler(authsAllowed: string[]) {
let authPos = 0;
return (methodsLeft: string[], partialSuccess: boolean, callback: any) => {
Expand Down

0 comments on commit 2d918c7

Please sign in to comment.