Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue with overrides file not writing correctly #2537

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading