mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Stop using @electron/remote to obtain app.getPath() (#4078)
* Stop using remote to obtain app.getPath() - Initialize entire map in bootstrap() - Updated unit tests Signed-off-by: Sebastian Malton <sebastian@malton.name> * Resolve PR comments Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix test Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix build Signed-off-by: Sebastian Malton <sebastian@malton.name> * Ensure that init can only be called once and catch errors Signed-off-by: Sebastian Malton <sebastian@malton.name> * Replace basically all uses of app.getPath() with AppPaths.get() Signed-off-by: Sebastian Malton <sebastian@malton.name> * Fix unit tests Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
1 parent
65b6908bf1
commit
f297407156
45 files changed
+521
-200
No files matched your search
@@ -23,12 +23,22 @@ import { UserStore } from "../../common/user-store";
|
||||
import { ContextHandler } from "../context-handler";
|
||||
import { PrometheusProvider, PrometheusProviderRegistry, PrometheusService } from "../prometheus";
|
||||
import mockFs from "mock-fs";
|
||||
import { AppPaths } from "../../common/app-paths";
|
||||
|
||||
jest.mock("electron", () => ({
|
||||
app: {
|
||||
getVersion: () => "99.99.99",
|
||||
getName: () => "lens",
|
||||
setName: jest.fn(),
|
||||
setPath: jest.fn(),
|
||||
getPath: () => "tmp",
|
||||
getLocale: () => "en",
|
||||
setLoginItemSettings: jest.fn(),
|
||||
},
|
||||
ipcMain: {
|
||||
on: jest.fn(),
|
||||
handle: jest.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
enum ServiceResult {
|
||||
@@ -76,6 +86,8 @@ function getHandler() {
|
||||
}) as any);
|
||||
}
|
||||
|
||||
AppPaths.init();
|
||||
|
||||
describe("ContextHandler", () => {
|
||||
beforeEach(() => {
|
||||
mockFs({
|
||||
|
||||
@@ -19,15 +19,6 @@
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
const logger = {
|
||||
silly: jest.fn(),
|
||||
debug: jest.fn(),
|
||||
log: jest.fn(),
|
||||
info: jest.fn(),
|
||||
error: jest.fn(),
|
||||
crit: jest.fn(),
|
||||
};
|
||||
|
||||
jest.mock("winston", () => ({
|
||||
format: {
|
||||
colorize: jest.fn(),
|
||||
@@ -35,26 +26,27 @@ jest.mock("winston", () => ({
|
||||
simple: jest.fn(),
|
||||
label: jest.fn(),
|
||||
timestamp: jest.fn(),
|
||||
printf: jest.fn()
|
||||
printf: jest.fn(),
|
||||
padLevels: jest.fn(),
|
||||
ms: jest.fn(),
|
||||
},
|
||||
createLogger: jest.fn().mockReturnValue(logger),
|
||||
createLogger: jest.fn().mockReturnValue({
|
||||
silly: jest.fn(),
|
||||
debug: jest.fn(),
|
||||
log: jest.fn(),
|
||||
info: jest.fn(),
|
||||
error: jest.fn(),
|
||||
crit: jest.fn(),
|
||||
}),
|
||||
transports: {
|
||||
Console: jest.fn(),
|
||||
File: jest.fn(),
|
||||
}
|
||||
}));
|
||||
|
||||
jest.mock("electron", () => ({
|
||||
app: {
|
||||
getPath: () => "tmp",
|
||||
setLoginItemSettings: jest.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock("../../common/ipc");
|
||||
jest.mock("child_process");
|
||||
jest.mock("tcp-port-used");
|
||||
//jest.mock("../utils/get-port");
|
||||
|
||||
import { Cluster } from "../cluster";
|
||||
import { KubeAuthProxy } from "../kube-auth-proxy";
|
||||
@@ -68,6 +60,7 @@ import { UserStore } from "../../common/user-store";
|
||||
import { Console } from "console";
|
||||
import { stdout, stderr } from "process";
|
||||
import mockFs from "mock-fs";
|
||||
import { AppPaths } from "../../common/app-paths";
|
||||
|
||||
console = new Console(stdout, stderr);
|
||||
|
||||
@@ -75,6 +68,23 @@ const mockBroadcastIpc = broadcastMessage as jest.MockedFunction<typeof broadcas
|
||||
const mockSpawn = spawn as jest.MockedFunction<typeof spawn>;
|
||||
const mockWaitUntilUsed = waitUntilUsed as jest.MockedFunction<typeof waitUntilUsed>;
|
||||
|
||||
jest.mock("electron", () => ({
|
||||
app: {
|
||||
getVersion: () => "99.99.99",
|
||||
getName: () => "lens",
|
||||
setName: jest.fn(),
|
||||
setPath: jest.fn(),
|
||||
getPath: () => "tmp",
|
||||
getLocale: () => "en",
|
||||
setLoginItemSettings: jest.fn(),
|
||||
},
|
||||
ipcMain: {
|
||||
on: jest.fn(),
|
||||
handle: jest.fn(),
|
||||
},
|
||||
}));
|
||||
AppPaths.init();
|
||||
|
||||
describe("kube auth proxy tests", () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
|
||||
@@ -28,12 +28,6 @@ const logger = {
|
||||
crit: jest.fn(),
|
||||
};
|
||||
|
||||
jest.mock("electron", () => ({
|
||||
app: {
|
||||
getPath: () => `/tmp`,
|
||||
},
|
||||
}));
|
||||
|
||||
jest.mock("winston", () => ({
|
||||
format: {
|
||||
colorize: jest.fn(),
|
||||
@@ -41,7 +35,9 @@ jest.mock("winston", () => ({
|
||||
simple: jest.fn(),
|
||||
label: jest.fn(),
|
||||
timestamp: jest.fn(),
|
||||
printf: jest.fn()
|
||||
padLevels: jest.fn(),
|
||||
ms: jest.fn(),
|
||||
printf: jest.fn(),
|
||||
},
|
||||
createLogger: jest.fn().mockReturnValue(logger),
|
||||
transports: {
|
||||
@@ -58,6 +54,25 @@ import fse from "fs-extra";
|
||||
import { loadYaml } from "@kubernetes/client-node";
|
||||
import { Console } from "console";
|
||||
import * as path from "path";
|
||||
import { AppPaths } from "../../common/app-paths";
|
||||
|
||||
jest.mock("electron", () => ({
|
||||
app: {
|
||||
getVersion: () => "99.99.99",
|
||||
getName: () => "lens",
|
||||
setName: jest.fn(),
|
||||
setPath: jest.fn(),
|
||||
getPath: () => "tmp",
|
||||
getLocale: () => "en",
|
||||
setLoginItemSettings: jest.fn(),
|
||||
},
|
||||
ipcMain: {
|
||||
on: jest.fn(),
|
||||
handle: jest.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
AppPaths.init();
|
||||
|
||||
console = new Console(process.stdout, process.stderr); // fix mockFS
|
||||
|
||||
@@ -111,7 +126,7 @@ describe("kubeconfig manager tests", () => {
|
||||
const kubeConfManager = new KubeconfigManager(cluster, contextHandler);
|
||||
|
||||
expect(logger.error).not.toBeCalled();
|
||||
expect(await kubeConfManager.getPath()).toBe(`${path.sep}tmp${path.sep}kubeconfig-foo`);
|
||||
expect(await kubeConfManager.getPath()).toBe(`tmp${path.sep}kubeconfig-foo`);
|
||||
// this causes an intermittent "ENXIO: no such device or address, read" error
|
||||
// const file = await fse.readFile(await kubeConfManager.getPath());
|
||||
const file = fse.readFileSync(await kubeConfManager.getPath());
|
||||
|
||||
@@ -19,8 +19,27 @@
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { AppPaths } from "../../common/app-paths";
|
||||
import { Router } from "../router";
|
||||
|
||||
jest.mock("electron", () => ({
|
||||
app: {
|
||||
getVersion: () => "99.99.99",
|
||||
getName: () => "lens",
|
||||
setName: jest.fn(),
|
||||
setPath: jest.fn(),
|
||||
getPath: () => "tmp",
|
||||
getLocale: () => "en",
|
||||
setLoginItemSettings: jest.fn(),
|
||||
},
|
||||
ipcMain: {
|
||||
on: jest.fn(),
|
||||
handle: jest.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
AppPaths.init();
|
||||
|
||||
describe("Router", () => {
|
||||
it("blocks path traversal attacks", async () => {
|
||||
const response: any = {
|
||||
|
||||
@@ -28,13 +28,26 @@ import mockFs from "mock-fs";
|
||||
import fs from "fs";
|
||||
import { ClusterStore } from "../../../common/cluster-store";
|
||||
import { ClusterManager } from "../../cluster-manager";
|
||||
import { AppPaths } from "../../../common/app-paths";
|
||||
|
||||
jest.mock("electron", () => ({
|
||||
app: {
|
||||
getPath: () => "/foo",
|
||||
getVersion: () => "99.99.99",
|
||||
getName: () => "lens",
|
||||
setName: jest.fn(),
|
||||
setPath: jest.fn(),
|
||||
getPath: () => "tmp",
|
||||
getLocale: () => "en",
|
||||
setLoginItemSettings: jest.fn(),
|
||||
},
|
||||
ipcMain: {
|
||||
on: jest.fn(),
|
||||
handle: jest.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
AppPaths.init();
|
||||
|
||||
describe("kubeconfig-sync.source tests", () => {
|
||||
beforeEach(() => {
|
||||
mockFs();
|
||||
|
||||
@@ -27,7 +27,7 @@ import path from "path";
|
||||
import { BaseStore } from "../common/base-store";
|
||||
import type { LensExtensionId } from "../extensions/lens-extension";
|
||||
import { toJS } from "../common/utils";
|
||||
import { getPath } from "../common/utils/getPath";
|
||||
import { AppPaths } from "../common/app-paths";
|
||||
|
||||
interface FSProvisionModel {
|
||||
extensions: Record<string, string>; // extension names to paths
|
||||
@@ -55,7 +55,7 @@ export class FilesystemProvisionerStore extends BaseStore<FSProvisionModel> {
|
||||
if (!this.registeredExtensions.has(extensionName)) {
|
||||
const salt = randomBytes(32).toString("hex");
|
||||
const hashedName = SHA256(`${extensionName}/${salt}`).toString();
|
||||
const dirPath = path.resolve(getPath("userData"), "extension_data", hashedName);
|
||||
const dirPath = path.resolve(AppPaths.get("userData"), "extension_data", hashedName);
|
||||
|
||||
this.registeredExtensions.set(extensionName, dirPath);
|
||||
}
|
||||
|
||||
+3
-9
@@ -63,13 +63,12 @@ import { ensureDir } from "fs-extra";
|
||||
import { Router } from "./router";
|
||||
import { initMenu } from "./menu";
|
||||
import { initTray } from "./tray";
|
||||
import * as path from "path";
|
||||
import { kubeApiRequest, shellApiRequest } from "./proxy-functions";
|
||||
import { AppPaths } from "../common/app-paths";
|
||||
|
||||
const onCloseCleanup = disposer();
|
||||
const onQuitCleanup = disposer();
|
||||
|
||||
const workingDir = path.join(app.getPath("appData"), appName);
|
||||
|
||||
SentryInit();
|
||||
app.setName(appName);
|
||||
@@ -82,12 +81,7 @@ if (app.setAsDefaultProtocolClient("lens")) {
|
||||
logger.info("📟 Protocol client register failed ❗");
|
||||
}
|
||||
|
||||
if (process.env.CICD) {
|
||||
app.setPath("appData", process.env.CICD);
|
||||
app.setPath("userData", path.join(process.env.CICD, appName));
|
||||
} else {
|
||||
app.setPath("userData", workingDir);
|
||||
}
|
||||
AppPaths.init();
|
||||
|
||||
if (process.env.LENS_DISABLE_GPU) {
|
||||
app.disableHardwareAcceleration();
|
||||
@@ -127,7 +121,7 @@ app.on("second-instance", (event, argv) => {
|
||||
});
|
||||
|
||||
app.on("ready", async () => {
|
||||
logger.info(`🚀 Starting ${productName} from "${app.getPath("exe")}"`);
|
||||
logger.info(`🚀 Starting ${productName} from "${AppPaths.get("exe")}"`);
|
||||
logger.info("🐚 Syncing shell environment");
|
||||
await shellSync();
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { app, BrowserWindow, dialog, IpcMainInvokeEvent } from "electron";
|
||||
import { BrowserWindow, dialog, IpcMainInvokeEvent } from "electron";
|
||||
import { KubernetesCluster } from "../../common/catalog-entities";
|
||||
import { clusterFrameMap } from "../../common/cluster-frames";
|
||||
import { clusterActivateHandler, clusterSetFrameIdHandler, clusterVisibilityHandler, clusterRefreshHandler, clusterDisconnectHandler, clusterKubectlApplyAllHandler, clusterKubectlDeleteAllHandler, clusterDeleteHandler, clusterSetDeletingHandler, clusterClearDeletingHandler } from "../../common/cluster-ipc";
|
||||
@@ -34,6 +34,7 @@ import { ResourceApplier } from "../resource-applier";
|
||||
import { WindowManager } from "../window-manager";
|
||||
import path from "path";
|
||||
import { remove } from "fs-extra";
|
||||
import { AppPaths } from "../../common/app-paths";
|
||||
|
||||
export function initIpcMainHandlers() {
|
||||
ipcMainHandle(clusterActivateHandler, (event, clusterId: ClusterId, force = false) => {
|
||||
@@ -99,7 +100,7 @@ export function initIpcMainHandlers() {
|
||||
|
||||
try {
|
||||
// remove the local storage file
|
||||
const localStorageFilePath = path.resolve(app.getPath("userData"), "lens-local-storage", `${cluster.id}.json`);
|
||||
const localStorageFilePath = path.resolve(AppPaths.get("userData"), "lens-local-storage", `${cluster.id}.json`);
|
||||
|
||||
await remove(localStorageFilePath);
|
||||
} catch {}
|
||||
|
||||
@@ -22,15 +22,15 @@
|
||||
import type { KubeConfig } from "@kubernetes/client-node";
|
||||
import type { Cluster } from "./cluster";
|
||||
import type { ContextHandler } from "./context-handler";
|
||||
import { app } from "electron";
|
||||
import path from "path";
|
||||
import fs from "fs-extra";
|
||||
import { dumpConfigYaml } from "../common/kube-helpers";
|
||||
import logger from "./logger";
|
||||
import { LensProxy } from "./lens-proxy";
|
||||
import { AppPaths } from "../common/app-paths";
|
||||
|
||||
export class KubeconfigManager {
|
||||
protected configDir = app.getPath("temp");
|
||||
protected configDir = AppPaths.get("temp");
|
||||
protected tempFile: string = null;
|
||||
|
||||
constructor(protected cluster: Cluster, protected contextHandler: ContextHandler) { }
|
||||
|
||||
+2
-2
@@ -31,8 +31,8 @@ import { customRequest } from "../common/request";
|
||||
import { getBundledKubectlVersion } from "../common/utils/app-version";
|
||||
import { isDevelopment, isWindows, isTestEnv } from "../common/vars";
|
||||
import { SemVer } from "semver";
|
||||
import { getPath } from "../common/utils/getPath";
|
||||
import { defaultPackageMirror, packageMirrors } from "../common/user-store/preferences-helpers";
|
||||
import { AppPaths } from "../common/app-paths";
|
||||
|
||||
const bundledVersion = getBundledKubectlVersion();
|
||||
const kubectlMap: Map<string, string> = new Map([
|
||||
@@ -81,7 +81,7 @@ export class Kubectl {
|
||||
protected dirname: string;
|
||||
|
||||
static get kubectlDir() {
|
||||
return path.join(getPath("userData"), "binaries", "kubectl");
|
||||
return path.join(AppPaths.get("userData"), "binaries", "kubectl");
|
||||
}
|
||||
|
||||
public static readonly bundledKubectlVersion: string = bundledVersion;
|
||||
|
||||
@@ -29,16 +29,28 @@ import { ExtensionLoader } from "../../../extensions/extension-loader";
|
||||
import { ExtensionsStore } from "../../../extensions/extensions-store";
|
||||
import { LensProtocolRouterMain } from "../router";
|
||||
import mockFs from "mock-fs";
|
||||
import { AppPaths } from "../../../common/app-paths";
|
||||
|
||||
jest.mock("../../../common/ipc");
|
||||
|
||||
jest.mock("electron", () => ({
|
||||
app: {
|
||||
getVersion: () => "99.99.99",
|
||||
getName: () => "lens",
|
||||
setName: jest.fn(),
|
||||
setPath: jest.fn(),
|
||||
getPath: () => "tmp",
|
||||
getLocale: () => "en",
|
||||
setLoginItemSettings: jest.fn(),
|
||||
},
|
||||
ipcMain: {
|
||||
on: jest.fn(),
|
||||
handle: jest.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
AppPaths.init();
|
||||
|
||||
function throwIfDefined(val: any): void {
|
||||
if (val != null) {
|
||||
throw val;
|
||||
|
||||
Reference in new issue
Block a user