Skip to content

Commit

Permalink
chore: Prepare ZE for Secrets SDK
Browse files Browse the repository at this point in the history
Signed-off-by: Trae Yelovich <[email protected]>
  • Loading branch information
traeok committed Jul 27, 2023
1 parent 25235b8 commit 79fc350
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("KeytarCredentialManager", () => {
loggerWarnSpy = jest.spyOn(imperative.Logger.prototype, "warn").mockImplementation();
});

it("should handle CredentialManager in Imperative settings", () => {
it("should handle CredentialManager in Imperative settings", async () => {
jest.spyOn(fs, "existsSync").mockReturnValueOnce(true);
const readFileSyncSpy = jest.spyOn(fs, "readFileSync").mockReturnValue(
JSON.stringify({
Expand All @@ -64,14 +64,14 @@ describe("KeytarCredentialManager", () => {
},
})
);
const keytar = KeytarCredentialManager.getSecurityModules("@traeok/keytar-rs", false);
const { keyring: keytar } = await import("@zowe/secrets-for-zowe-sdk");
expect(keytar).toBeDefined();
expect(loggerWarnSpy).not.toHaveBeenCalled();
expect(readFileSyncSpy).toHaveBeenCalledTimes(1);
expect(Object.keys(keytar as object).length).toBe(5);
});

it("should handle credential-manager in Imperative settings", () => {
it("should handle credential-manager in Imperative settings", async () => {
jest.spyOn(fs, "existsSync").mockReturnValueOnce(true);
const readFileSyncSpy = jest.spyOn(fs, "readFileSync").mockReturnValue(
JSON.stringify({
Expand All @@ -80,14 +80,14 @@ describe("KeytarCredentialManager", () => {
},
})
);
const keytar = KeytarCredentialManager.getSecurityModules("@traeok/keytar-rs", false);
const { keyring: keytar } = await import("@zowe/secrets-for-zowe-sdk");
expect(keytar).toBeDefined();
expect(loggerWarnSpy).not.toHaveBeenCalled();
expect(readFileSyncSpy).toHaveBeenCalledTimes(1);
expect(Object.keys(keytar as object).length).toBe(5);
});

it("should handle CredentialManager in Imperative settings - Theia", () => {
it("should handle CredentialManager in Imperative settings - Theia", async () => {
jest.spyOn(fs, "existsSync").mockReturnValueOnce(true);
const readFileSyncSpy = jest.spyOn(fs, "readFileSync").mockReturnValue(
JSON.stringify({
Expand All @@ -97,42 +97,42 @@ describe("KeytarCredentialManager", () => {
})
);
jest.spyOn(process, "cwd").mockReturnValueOnce(__dirname + "/../../../../..");
const keytar = KeytarCredentialManager.getSecurityModules("@traeok/keytar-rs", true);
const { keyring: keytar } = await import("@zowe/secrets-for-zowe-sdk");
expect(keytar).toBeDefined();
expect(loggerWarnSpy).not.toHaveBeenCalled();
expect(readFileSyncSpy).toHaveBeenCalledTimes(1);
expect(Object.keys(keytar as object).length).toBe(5);
});

it("should handle empty Imperative settings", () => {
it("should handle empty Imperative settings", async () => {
jest.spyOn(fs, "existsSync").mockReturnValueOnce(true);
const readFileSyncSpy = jest.spyOn(fs, "readFileSync").mockReturnValue(JSON.stringify({}));
const keytar = KeytarCredentialManager.getSecurityModules("@traeok/keytar-rs", false);
const { keyring: keytar } = await import("@zowe/secrets-for-zowe-sdk");
expect(loggerWarnSpy).not.toHaveBeenCalled();
expect(readFileSyncSpy).toHaveBeenCalledTimes(1);
expect(keytar).toBeUndefined();
});

it("should handle non-existent Imperative settings", () => {
it("should handle non-existent Imperative settings", async () => {
jest.spyOn(fs, "existsSync").mockReturnValueOnce(false);
const readFileSyncSpy = jest.spyOn(fs, "readFileSync");
const keytar = KeytarCredentialManager.getSecurityModules("@traeok/keytar-rs", false);
const { keyring: keytar } = await import("@zowe/secrets-for-zowe-sdk");
expect(loggerWarnSpy).not.toHaveBeenCalled();
expect(readFileSyncSpy).not.toHaveBeenCalled();
expect(keytar).toBeUndefined();
});

it("should handle error loading Imperative settings", () => {
it("should handle error loading Imperative settings", async () => {
jest.spyOn(fs, "existsSync").mockReturnValueOnce(true);
const readFileSyncSpy = jest.spyOn(fs, "readFileSync").mockReturnValueOnce("invalid json");
const keytar = KeytarCredentialManager.getSecurityModules("@traeok/keytar-rs", false);
const { keyring: keytar } = await import("@zowe/secrets-for-zowe-sdk");
expect(loggerWarnSpy).toHaveBeenCalledTimes(1);
expect(loggerWarnSpy.mock.calls[0][0].message).toContain("Unexpected token");
expect(readFileSyncSpy).toHaveBeenCalledTimes(1);
expect(keytar).toBeUndefined();
});

it("should handle error loading invalid security module", () => {
it("should handle error loading invalid security module", async () => {
jest.spyOn(fs, "existsSync").mockReturnValueOnce(true);
const readFileSyncSpy = jest.spyOn(fs, "readFileSync").mockReturnValue(
JSON.stringify({
Expand All @@ -141,7 +141,7 @@ describe("KeytarCredentialManager", () => {
},
})
);
const keytar = KeytarCredentialManager.getSecurityModules("keytar_bad", false);
const keytar = require("nonexistent-keytar");
expect(loggerWarnSpy).toHaveBeenCalledTimes(2);
expect(loggerWarnSpy.mock.calls[0][0].message).toContain("Cannot find module");
expect(readFileSyncSpy).toHaveBeenCalledTimes(1);
Expand Down
3 changes: 2 additions & 1 deletion packages/zowe-explorer-api/src/profiles/ProfilesCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export class ProfilesCache {

public async getProfileInfo(envTheia = false): Promise<zowe.imperative.ProfileInfo> {
const mProfileInfo = new zowe.imperative.ProfileInfo("zowe", {
credMgrOverride: zowe.imperative.ProfileCredentials.defaultCredMgrWithKeytar(() => getSecurityModules("@traeok/keytar-rs", envTheia)),
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
credMgrOverride: zowe.imperative.ProfileCredentials.defaultCredMgrWithKeytar(() => require("@zowe/secrets-for-zowe-sdk").keyring),
});
await mProfileInfo.readProfilesFromDisk({ homeDir: getZoweDir(), projectDir: this.cwd ?? undefined });
return mProfileInfo;
Expand Down
2 changes: 1 addition & 1 deletion packages/zowe-explorer-api/src/security/KeytarApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class KeytarApi {
const profiles = new ProfilesCache(log, vscode.workspace.workspaceFolders?.[0]?.uri.fsPath);
const scsActive = profiles.isSecureCredentialPluginActive();
if (scsActive) {
const keytar = KeytarCredentialManager.getSecurityModules("@traeok/keytar-rs", isTheia);
const { keyring: keytar } = await import("@zowe/secrets-for-zowe-sdk");
if (!initialized && keytar) {
KeytarCredentialManager.keytar = keytar as unknown as KeytarModule;
await imperative.CredentialManagerFactory.initialize({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,7 @@ export class KeytarCredentialManager extends imperative.AbstractCredentialManage
*/
export function getSecurityModules(moduleName: string, isTheia: boolean): NodeModule | undefined {
const r = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return r(moduleName);
/*// Workaround for Theia issue (https://github.com/eclipse-theia/theia/issues/4935)
// Workaround for Theia issue (https://github.com/eclipse-theia/theia/issues/4935)
const appRoot = isTheia ? process.cwd() : vscode.env.appRoot;
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
Expand All @@ -228,5 +226,5 @@ export function getSecurityModules(moduleName: string, isTheia: boolean): NodeMo
} catch (err) {
imperative.Logger.getAppLogger().warn(err as string);
}
return undefined;*/
return undefined;
}
3 changes: 0 additions & 3 deletions packages/zowe-explorer-ftp-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@
"@zowe/zowe-explorer-api": "2.10.0-SNAPSHOT",
"tmp": "0.2.1"
},
"resolutions": {
"**/@zowe/imperative": "file:../imperative"
},
"devDependencies": {
"@types/tmp": "0.2.0",
"concurrently": "^5.2.0",
Expand Down
1 change: 0 additions & 1 deletion packages/zowe-explorer-ftp-extension/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const config = {
externals: {
// Add modules that cannot be webpack'ed, 📖 -> https://webpack.js.org/configuration/externals/
vscode: "commonjs vscode", // the vscode-module is created on-the-fly and must be excluded. Add other modules that cannot be webpack'ed, 📖 -> https://webpack.js.org/configuration/externals/
keytar: "commonjs keytar",
"spdx-exceptions": "commonjs spdx-exceptions",
"spdx-license-ids": "commonjs spdx-license-ids",
"spdx-license-ids/deprecated": "commonjs spdx-license-ids/deprecated",
Expand Down
1 change: 1 addition & 0 deletions packages/zowe-explorer/.vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
!out/src/nls.metadata*.json
!resources/**/*.png
!resources/**/*.svg
!prebuilds/**

!CHANGELOG.md
!LICENSE
Expand Down
6 changes: 3 additions & 3 deletions packages/zowe-explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1905,12 +1905,12 @@
]
},
"scripts": {
"build": "gulp build && yarn license && webpack --mode development",
"build": "gulp build && yarn license && ts-node ./scripts/getSecretsPrebuilds.ts && webpack --mode development",
"build:integration": "yarn createTestProfileData && yarn license && tsc --pretty --project tsconfig-tests.json",
"test:unit": "jest \".*__tests__.*\\.unit\\.test\\.ts\" --coverage",
"test:theia": "mocha ./out/__tests__/__theia__/*.theia.test.js --timeout 50000 --reporter mocha-multi-reporters --reporter-options configFile=.mocharc.json",
"test": "yarn test:unit",
"vscode:prepublish": "gulp build && webpack --mode production && yarn updateStrings && yarn run compile-web",
"vscode:prepublish": "gulp build && ts-node ./scripts/getSecretsPrebuilds.ts && webpack --mode production && yarn updateStrings && yarn run compile-web",
"updateStrings": "node ../../scripts/stringUpdateScript.js && prettier --write --loglevel warn ./i18n package.nls*",
"package": "vsce package --allow-star-activation --yarn && node ../../scripts/mv-pack.js vscode-extension-for-zowe vsix",
"license": "node ../../scripts/updateLicenses.js",
Expand All @@ -1934,7 +1934,6 @@
},
"devDependencies": {
"@napi-rs/cli": "^2.16.1",
"@traeok/keytar-rs": "github:traeok/keytar-rs",
"@types/chai": "^4.2.6",
"@types/chai-as-promised": "^7.1.0",
"@types/expect": "^1.20.3",
Expand Down Expand Up @@ -1972,6 +1971,7 @@
"webpack-cli": "^3.3.11"
},
"dependencies": {
"@zowe/secrets-for-zowe-sdk": "7.18.0-next.1",
"@zowe/zowe-explorer-api": "2.10.0-SNAPSHOT",
"fs-extra": "8.0.1",
"isbinaryfile": "4.0.4",
Expand Down
4 changes: 4 additions & 0 deletions packages/zowe-explorer/scripts/getSecretsPrebuilds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { copySync } from "fs-extra";
import { join, resolve } from "path";
const secretsPkgDir = resolve(require.resolve("@zowe/secrets-for-zowe-sdk"), "..");
copySync(join(secretsPkgDir, "prebuilds"), resolve(__dirname, "..", "prebuilds"));
5 changes: 3 additions & 2 deletions packages/zowe-explorer/src/utils/ProfilesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import * as globals from "../globals";
import * as path from "path";
import * as fs from "fs";
import * as util from "util";
import { getSecurityModules, IZoweTreeNode, ZoweTreeNode, getZoweDir, getFullPath, Gui } from "@zowe/zowe-explorer-api";
import { IZoweTreeNode, ZoweTreeNode, getZoweDir, getFullPath, Gui } from "@zowe/zowe-explorer-api";
import { Profiles } from "../Profiles";
import * as nls from "vscode-nls";
import { imperative, getImperativeConfig } from "@zowe/cli";
Expand Down Expand Up @@ -274,7 +274,8 @@ export class ProfilesUtils {
ZoweLogger.info(localize("ProfilesUtils.getProfileInfo.usingDefault", "No custom credential managers found, using the default instead."));
await ProfilesUtils.updateCredentialManagerSetting(globals.ZOWE_CLI_SCM);
return new imperative.ProfileInfo("zowe", {
credMgrOverride: imperative.ProfileCredentials.defaultCredMgrWithKeytar(() => getSecurityModules("@traeok/keytar-rs", envTheia)),
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
credMgrOverride: imperative.ProfileCredentials.defaultCredMgrWithKeytar(() => require("@zowe/secrets-for-zowe-sdk").keyring),
});
}

Expand Down
1 change: 0 additions & 1 deletion packages/zowe-explorer/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const config = {
externals: {
// Add modules that cannot be webpack'ed, 📖 -> https://webpack.js.org/configuration/externals/
vscode: "commonjs vscode", // the vscode-module is created on-the-fly and must be excluded. Add other modules that cannot be webpack'ed, 📖 -> https://webpack.js.org/configuration/externals/
keytar: "commonjs keytar",
"spdx-exceptions": "commonjs spdx-exceptions",
"spdx-license-ids": "commonjs spdx-license-ids",
"spdx-license-ids/deprecated": "commonjs spdx-license-ids/deprecated",
Expand Down

0 comments on commit 79fc350

Please sign in to comment.