Skip to content

Commit

Permalink
Merge pull request #2537 from zowe/fix-overrides-file-writing2
Browse files Browse the repository at this point in the history
Fix for issue with overrides file not writing correctly
  • Loading branch information
JillieBeanSim authored Nov 1, 2023
2 parents 16f70e4 + 548a8df commit 7ecb12e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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", () => {
Expand Down
26 changes: 17 additions & 9 deletions packages/zowe-explorer/src/utils/ProfilesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -542,6 +548,10 @@ export class ProfilesUtils {

public static async initializeZoweFolder(): Promise<void> {
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.
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7ecb12e

Please sign in to comment.