@zowe/zos-uss-for-zowe-sdk
Version:
Zowe SDK to interact with USS on z/OS
153 lines • 6.83 kB
JavaScript
;
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SshBaseHandler = void 0;
const imperative_1 = require("@zowe/imperative");
const SshSession_1 = require("./SshSession");
const ssh2_1 = require("ssh2");
const fs = require("fs");
/**
* This class is used by the various handlers in the project as the base class for their implementation.
*/
class SshBaseHandler {
/**
* This will grab the arguments and create a session before calling the subclass
* {@link SshBaseHandler#processWithSession} method.
*
* @param {IHandlerParameters} commandParameters Command parameters sent by imperative.
*
* @returns {Promise<void>}
*/
process(commandParameters) {
return __awaiter(this, void 0, void 0, function* () {
this.mHandlerParams = commandParameters;
const sshSessCfgOverride = [
{
propertyName: "privateKey",
propertiesOverridden: [
"password",
"tokenType",
"tokenValue",
"cert",
"certKey",
],
},
];
const sshSessCfg = SshSession_1.SshSession.createSshSessCfgFromArgs(commandParameters.arguments);
let sshSessCfgWithCreds = yield imperative_1.ConnectionPropsForSessCfg.addPropsOrPrompt(sshSessCfg, commandParameters.arguments, {
parms: commandParameters,
propertyOverrides: sshSessCfgOverride,
supportedAuthTypes: [imperative_1.SessConstants.AUTH_TYPE_BASIC],
});
this.mSession = new SshSession_1.SshSession(sshSessCfgWithCreds);
this.mArguments = commandParameters.arguments;
try {
yield this.processCmd(commandParameters);
}
catch (e) {
if (e.message.includes("but no passphrase given") ||
e.message.includes("bad passphrase?")) {
this.console.log("Initial key passphrase authentication failed!" + "\n");
const maxAttempts = 3;
let attempt = 0;
let success = false;
while (attempt < maxAttempts && !success) {
try {
sshSessCfgWithCreds =
yield imperative_1.ConnectionPropsForSessCfg.addPropsOrPrompt(sshSessCfgWithCreds, commandParameters.arguments, {
parms: commandParameters,
propertyOverrides: sshSessCfgOverride,
supportedAuthTypes: [
imperative_1.SessConstants.AUTH_TYPE_BASIC,
],
propsToPromptFor: [
{
name: "keyPassphrase",
isGivenValueValid: (givenValue) => {
let saveKP = true;
const result = ssh2_1.utils.parseKey(fs.readFileSync(sshSessCfgWithCreds.privateKey), givenValue);
if (result instanceof Error)
saveKP =
!result.message.includes("no passphrase given") &&
!result.message.includes("bad passphrase");
return saveKP;
},
},
],
});
this.mSession = new SshSession_1.SshSession(sshSessCfgWithCreds);
yield this.processCmd(commandParameters);
success = true;
}
catch (retryError) {
this.console.log("\n" +
`Key passphrase authentication failed! (${++attempt}/${maxAttempts})` +
"\n");
if (attempt >= maxAttempts) {
throw new Error("Maximum retry attempts reached. Authentication failed.");
}
}
}
}
else {
throw e;
}
}
});
}
/**
* Fail the command with an imperative error
* @param {IImperativeError} err - the imperative error parameters
*/
fail(err) {
throw new imperative_1.ImperativeError(err);
}
/**
* Returns the console interface for the command handler
* @returns {IHandlerResponseConsoleApi}
*/
get console() {
return this.mHandlerParams.response.console;
}
/**
* Returns the format interface for the command handler
* @returns {IHandlerFormatOutputApi}
*/
get format() {
return this.mHandlerParams.response.format;
}
/**
* Returns the format interface for the command handler
* @returns {IHandlerResponseDataApi}
*/
get data() {
return this.mHandlerParams.response.data;
}
/**
* Returns the format interface for the command handler
* @returns {IHandlerProgressApi}
*/
get progress() {
return this.mHandlerParams.response.progress;
}
}
exports.SshBaseHandler = SshBaseHandler;
//# sourceMappingURL=SshBaseHandler.js.map