UNPKG

@zowe/imperative

Version:
250 lines 11 kB
"use strict"; /* * 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 _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.CredentialManagerOverride = void 0; const path = require("path"); const fs_extra_1 = require("fs-extra"); const utilities_1 = require("../../utilities"); const error_1 = require("../../error"); const events_1 = require("../../events"); const EventOperator_1 = require("../../events/src/EventOperator"); /** * This class provides access to the known set of credential manager overrides * and functions to manipulate which credential manager is in use. * Other credential managers can replace the default credential manager. * Both CLI plugins and Zowe Explorer extensions can override the default * credential manager. However, only one credential manager will be in effect * on a given computer. The last component to override the credential * manager wins. */ class CredentialManagerOverride { //________________________________________________________________________ /** * Get the credential manager information for the specified credential manager * display name. * * @param credMgrDisplayName - display name of the credential manager * * @returns An ICredentialManagerNameMap or * null if the specified plugin is not a known credential manager. */ static getCredMgrInfoByDisplayName(credMgrDisplayName) { var _b; return (_b = this.KNOWN_CRED_MGRS.find((credMgr) => credMgr.credMgrDisplayName === credMgrDisplayName)) !== null && _b !== void 0 ? _b : null; } //________________________________________________________________________ /** * Get the credential manager information for the specified plugin. * * @param pluginName - Name of the plugin package * * @returns An ICredentialManagerNameMap or * null if the specified plugin is not a known credential manager. */ static getCredMgrInfoByPlugin(pluginName) { var _b; return (_b = this.KNOWN_CRED_MGRS.find((credMgr) => credMgr.credMgrPluginName === pluginName)) !== null && _b !== void 0 ? _b : null; } //________________________________________________________________________ /** * Get the credential manager information for the specified plugin. * * @param ZEExtName - Name of the Zowe Explorer extension * * @returns An ICredentialManagerNameMap or * null if the specified extension is not a known credential manager. */ static getCredMgrInfoByZEExt(ZEExtName) { var _b; return (_b = this.KNOWN_CRED_MGRS.find((credMgr) => credMgr.credMgrZEName === ZEExtName)) !== null && _b !== void 0 ? _b : null; } //________________________________________________________________________ /** * Get the known credential managers. * * @returns An array of credential managers. */ static getKnownCredMgrs() { return this.KNOWN_CRED_MGRS; } //________________________________________________________________________ /** * Get the active credential manager. * * @returns Information about the current redential managers or false if none is set. */ static getCurrentCredMgr() { try { const settings = this.getSettingsFileJson(); return settings.json.overrides.CredentialManager; } catch (err) { return this.DEFAULT_CRED_MGR_NAME; } } /** * Record the specified credential manager in the configuration of overrides. * A plugin or ZE extension that provides a credential manager would record * its credential manager name upon installation. * * @param newCredMgrName * The display name of your credential manager. * * @throws An ImperativeError upon error. */ static recordCredMgrInConfig(newCredMgrName) { const credMgrInfo = _a.getCredMgrInfoByDisplayName(newCredMgrName); if (credMgrInfo === null) { /* We do not have a known credMgr. We do not permit overriding by an * unknown credMgr. Form a message of known credential managers. */ throw new error_1.ImperativeError({ msg: `The credential manager name '${newCredMgrName}' is an unknown ` + `credential manager. The previous credential manager will NOT be overridden. ` + `Valid credential managers are:` + this.KNOWN_CRED_MGRS.map(knownCredMgr => `\n${knownCredMgr.credMgrDisplayName}`).join('') }); } // read in the existing settings file let settings; try { settings = this.getSettingsFileJson(); } catch (error) { throw new error_1.ImperativeError({ msg: "Due to error in settings file, unable to override the credential manager with '" + newCredMgrName + "'" + "\nReason: " + error.message }); } // set to the new credMgr and write the settings file settings.json.overrides.CredentialManager = newCredMgrName; try { (0, fs_extra_1.writeJsonSync)(settings.fileName, settings.json, { spaces: 2 }); EventOperator_1.EventOperator.getZoweProcessor().emitZoweEvent(events_1.ZoweSharedEvents.ON_CREDENTIAL_MANAGER_CHANGED); } catch (error) { throw new error_1.ImperativeError({ msg: "Unable to write settings file = " + settings.fileName + "\nReason: " + error.message }); } } //________________________________________________________________________ /** * Record the default Zowe CLI credential manager in the configuration of * overrides. The specified credential manager will be replaced with the * default Zowe CLI credential manager. A plugin or ZE extension that provides * a credential manager would replace itself with the default credential * manager when it is being uninstalled. * * @param credMgrToReplace * The display name of your credential manager. This name must also * be the credential manager currently recorded in the configuration * of overrides. Otherwise, no replacement will be performed. * Specifying your own name is intended to prevent a plugin from * inadvertently replacing another plugin's credential manager. * * @throws An ImperativeError upon error. */ static recordDefaultCredMgrInConfig(credMgrToReplace) { // read in the existing settings file let settings; try { settings = this.getSettingsFileJson(); } catch (error) { throw new error_1.ImperativeError({ msg: "Due to error in settings file, unable to replace the credential manager named '" + credMgrToReplace + "'" + "\nReason: " + error.message }); } // we only permit a credential manager to restore from itself back to our default if (settings.json.overrides.CredentialManager != credMgrToReplace) { throw new error_1.ImperativeError({ msg: `An attempt to revert Credential Manager = '${credMgrToReplace}' ` + `to the default Credential Manager = '${this.DEFAULT_CRED_MGR_NAME}' ` + `failed. The value '${credMgrToReplace}' must be the current value ` + `in settings file = '${settings.fileName}'. Instead, ` + `the current value is '${settings.json.overrides.CredentialManager}'. ` + "The current Credential Manager has not been replaced." }); } // reset to our default credMgr and write the settings file settings.json.overrides.CredentialManager = this.DEFAULT_CRED_MGR_NAME; try { (0, fs_extra_1.writeJsonSync)(settings.fileName, settings.json, { spaces: 2 }); EventOperator_1.EventOperator.getZoweProcessor().emitZoweEvent(events_1.ZoweSharedEvents.ON_CREDENTIAL_MANAGER_CHANGED); } catch (error) { throw new error_1.ImperativeError({ msg: "Unable to write settings file = " + settings.fileName + "\nReason: " + error.message }); } } //________________________________________________________________________ /** * Get the contents of the $ZOWE_CLI_HOME/settings/imperative.json file. * The resulting JSON is guaranteed to contain the key * 'overrides.CredentialManager'. * * @returns A 'settings' object with the properties: fileName and json. * The json object contains the contents of the settings file. * * @throws An ImperativeError if the file does not exist or have the key. */ static getSettingsFileJson() { var _b, _c; const settings = { fileName: "", json: {} }; try { settings.fileName = path.join(utilities_1.ImperativeConfig.instance.cliHome, "settings", "imperative.json"); settings.json = (0, fs_extra_1.readJsonSync)(settings.fileName); } catch (error) { throw new error_1.ImperativeError({ msg: "Unable to read settings file = " + settings.fileName + "\nReason: " + error.message }); } if (typeof ((_c = (_b = settings.json) === null || _b === void 0 ? void 0 : _b.overrides) === null || _c === void 0 ? void 0 : _c.CredentialManager) === "undefined") { throw new error_1.ImperativeError({ msg: "The property key 'overrides.CredentialManager' does not exist in settings file = " + settings.fileName }); } return settings; } } exports.CredentialManagerOverride = CredentialManagerOverride; _a = CredentialManagerOverride; CredentialManagerOverride.CRED_MGR_SETTING_NAME = "CredentialManager"; CredentialManagerOverride.DEFAULT_CRED_MGR_NAME = "@zowe/cli"; CredentialManagerOverride.KNOWN_CRED_MGRS = [ { "credMgrDisplayName": _a.DEFAULT_CRED_MGR_NAME }, { "credMgrDisplayName": "Secrets for Kubernetes", "credMgrPluginName": "@zowe/secrets-for-kubernetes-for-zowe-cli", "credMgrZEName": "Zowe.secrets-for-kubernetes" }, { "credMgrDisplayName": false }, ]; //# sourceMappingURL=CredentialManagerOverride.js.map