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:
Sebastian Malton authored and GitHub committed 2021-10-27 21:07:41 -04:00
1 parent 65b6908bf1
commit f297407156
45 files changed
+521 -200

No files matched your search

+12
View File
@@ -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({
+29 -19
View File
@@ -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();
+23 -8
View File
@@ -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
View File
@@ -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 = {