From 548a8df61c4a8befcb066324ba175fa6b6284e2c Mon Sep 17 00:00:00 2001 From: Rudy Flores <68666202+rudyflores@users.noreply.github.com> Date: Mon, 30 Oct 2023 18:07:57 -0400 Subject: [PATCH] move to new PR against main Signed-off-by: Rudy Flores <68666202+rudyflores@users.noreply.github.com> --- .../__unit__/utils/ProfilesUtils.unit.test.ts | 18 +++++++++++++ .../zowe-explorer/src/utils/ProfilesUtils.ts | 26 ++++++++++++------- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/packages/zowe-explorer/__tests__/__unit__/utils/ProfilesUtils.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/utils/ProfilesUtils.unit.test.ts index a0a75e8549..6bd720ab07 100644 --- a/packages/zowe-explorer/__tests__/__unit__/utils/ProfilesUtils.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/utils/ProfilesUtils.unit.test.ts @@ -485,6 +485,7 @@ describe("ProfilesUtils unit tests", () => { it("should skip creating directories and files that already exist", async () => { const blockMocks = createBlockMocks(); + jest.spyOn(profUtils.ProfilesUtils, "getCredentialManagerOverride").mockReturnValue("@zowe/cli"); blockMocks.mockGetDirectValue.mockReturnValue("@zowe/cli"); blockMocks.mockExistsSync.mockReturnValue(true); const fileJson = blockMocks.mockFileRead; @@ -777,6 +778,23 @@ describe("ProfilesUtils unit tests", () => { expect(profUtils.ProfilesUtils.getCredentialManagerOverride()).toBe("My Custom Credential Manager"); expect(zoweLoggerTraceSpy).toBeCalledTimes(1); }); + + it("should return default manager if the override file does not exist", () => { + const zoweLoggerTraceSpy = jest.spyOn(ZoweLogger, "trace"); + const zoweLoggerInfoSpy = jest.spyOn(ZoweLogger, "info"); + + jest.spyOn(fs, "readFileSync").mockImplementation(() => { + throw new Error("test"); + }); + try { + profUtils.ProfilesUtils.getCredentialManagerOverride(); + } catch (err) { + expect(err).toBe("test"); + } + + expect(zoweLoggerTraceSpy).toBeCalledTimes(1); + expect(zoweLoggerInfoSpy).toBeCalledTimes(1); + }); }); describe("setupCustomCredentialManager", () => { diff --git a/packages/zowe-explorer/src/utils/ProfilesUtils.ts b/packages/zowe-explorer/src/utils/ProfilesUtils.ts index 9d5418d4e9..d5557c7e14 100644 --- a/packages/zowe-explorer/src/utils/ProfilesUtils.ts +++ b/packages/zowe-explorer/src/utils/ProfilesUtils.ts @@ -233,13 +233,19 @@ export class ProfilesUtils { */ public static getCredentialManagerOverride(): string { ZoweLogger.trace("ProfilesUtils.getCredentialManagerOverride called."); - const settingsFile = path.join(getZoweDir(), "settings", "imperative.json"); - const imperativeConfig = JSON.parse(fs.readFileSync(settingsFile).toString()); - const credentialManagerOverride = imperativeConfig?.overrides[imperative.CredentialManagerOverride.CRED_MGR_SETTING_NAME]; - if (typeof credentialManagerOverride === "string") { - return credentialManagerOverride; + try { + const settingsFilePath = path.join(getZoweDir(), "settings", "imperative.json"); + const settingsFile = fs.readFileSync(settingsFilePath); + const imperativeConfig = JSON.parse(settingsFile.toString()); + const credentialManagerOverride = imperativeConfig?.overrides[imperative.CredentialManagerOverride.CRED_MGR_SETTING_NAME]; + if (typeof credentialManagerOverride === "string") { + return credentialManagerOverride; + } + return imperative.CredentialManagerOverride.DEFAULT_CRED_MGR_NAME; + } catch (err) { + ZoweLogger.info("imperative.json does not exist, returning the default override of @zowe/cli"); + return imperative.CredentialManagerOverride.DEFAULT_CRED_MGR_NAME; } - return imperative.CredentialManagerOverride.DEFAULT_CRED_MGR_NAME; } /** @@ -542,6 +548,10 @@ export class ProfilesUtils { public static async initializeZoweFolder(): Promise { ZoweLogger.trace("ProfilesUtils.initializeZoweFolder called."); + // ensure the Secure Credentials Enabled value is read + // set globals.PROFILE_SECURITY value accordingly + const credentialManagerMap = ProfilesUtils.getCredentialManagerOverride(); + await globals.setGlobalSecurityValue(credentialManagerMap ?? globals.ZOWE_CLI_SCM); // Ensure that ~/.zowe folder exists // Ensure that the ~/.zowe/settings/imperative.json exists // TODO: update code below once this imperative issue is resolved. @@ -555,9 +565,6 @@ export class ProfilesUtils { fs.mkdirSync(settingsPath); } ProfilesUtils.writeOverridesFile(); - // set global variable of security value to existing override - // this will later get reverted to default in getProfilesInfo.ts if user chooses to - await ProfilesUtils.updateCredentialManagerSetting(ProfilesUtils.getCredentialManagerOverride()); // If not using team config, ensure that the ~/.zowe/profiles directory // exists with appropriate types within if (!imperative.ImperativeConfig.instance.config?.exists) { @@ -609,6 +616,7 @@ export class ProfilesUtils { } else { settings = { ...defaultImperativeJson }; } + settings.overrides.CredentialManager = globals.PROFILE_SECURITY; const newData = JSON.stringify(settings, null, 2); ZoweLogger.debug( localize("writeOverridesFile.updateFile", "Updating imperative.json Credential Manager to {0}.\n{1}", globals.PROFILE_SECURITY, newData)