1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2022-03-14 15:45:13 +02:00
parent ff6cef38e7
commit 559203d837
20 changed files with 140 additions and 42 deletions

View File

@ -24,6 +24,8 @@ import { createClusterInjectionToken } from "../cluster/create-cluster-injection
import directoryForUserDataInjectable
from "../app-paths/directory-for-user-data/directory-for-user-data.injectable";
import kubeAuthProxyCaInjectable from "../../main/kube-auth-proxy/kube-auth-proxy-ca.injectable";
import createKubeAuthProxyCertFilesInjectable from "../../main/kube-auth-proxy/create-kube-auth-proxy-cert-files.injectable";
console = new Console(stdout, stderr);
@ -87,6 +89,8 @@ describe("cluster-store", () => {
mainDi = dis.mainDi;
mainDi.override(directoryForUserDataInjectable, () => "some-directory-for-user-data");
mainDi.override(createKubeAuthProxyCertFilesInjectable, () => ({}));
mainDi.override(kubeAuthProxyCaInjectable, () => ({}));
await dis.runSetups();

View File

@ -10,6 +10,7 @@ import type { CatalogEntity, CatalogEntityData, CatalogEntityKindData } from "..
import { HotbarStore } from "../hotbar-store";
import { getDiForUnitTesting } from "../../main/getDiForUnitTesting";
import directoryForUserDataInjectable from "../app-paths/directory-for-user-data/directory-for-user-data.injectable";
import writeFileInjectable from "../fs/write-file.injectable";
jest.mock("../../main/catalog/catalog-entity-registry", () => ({
catalogEntityRegistry: {
@ -115,6 +116,7 @@ describe("HotbarStore", () => {
beforeEach(async () => {
const di = getDiForUnitTesting({ doGeneralOverrides: true });
di.override(writeFileInjectable, () => () => undefined);
di.override(directoryForUserDataInjectable, () => "some-directory-for-user-data");
await di.runSetups();

View File

@ -32,6 +32,7 @@ import type { DiContainer } from "@ogre-tools/injectable";
import directoryForUserDataInjectable from "../app-paths/directory-for-user-data/directory-for-user-data.injectable";
import type { ClusterStoreModel } from "../cluster-store/cluster-store";
import { defaultTheme } from "../vars";
import writeFileInjectable from "../fs/write-file.injectable";
console = new Console(stdout, stderr);
@ -46,6 +47,7 @@ describe("user store tests", () => {
mainDi = dis.mainDi;
mainDi.override(writeFileInjectable, () => () => undefined);
mainDi.override(directoryForUserDataInjectable, () => "some-directory-for-user-data");
await dis.runSetups();

View File

@ -0,0 +1,25 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import path from "path";
import fsInjectable from "./fs.injectable";
const writeFileInjectable = getInjectable({
id: "write-file",
instantiate: (di) => {
const { writeFile, ensureDir } = di.inject(fsInjectable);
return async (filePath: string, content: string | Buffer) => {
await ensureDir(path.dirname(filePath), { mode: 0o755 });
await writeFile(filePath, content, {
encoding: "utf-8",
});
};
},
});
export default writeFileInjectable;

View File

@ -41,6 +41,7 @@ import type { ClusterModel } from "../../common/cluster-types";
import { createClusterInjectionToken } from "../../common/cluster/create-cluster-injection-token";
import authorizationReviewInjectable from "../../common/cluster/authorization-review.injectable";
import listNamespacesInjectable from "../../common/cluster/list-namespaces.injectable";
import createContextHandlerInjectable from "../context-handler/create-context-handler.injectable";
console = new Console(process.stdout, process.stderr); // fix mockFS
@ -81,6 +82,9 @@ describe("create clusters", () => {
di.override(authorizationReviewInjectable, () => () => () => Promise.resolve(true));
di.override(listNamespacesInjectable, () => () => () => Promise.resolve([ "default" ]));
di.override(createContextHandlerInjectable, () => () => {
throw new Error("you should never come here");
});
createCluster = di.inject(createClusterInjectionToken);

View File

@ -10,6 +10,8 @@ import mockFs from "mock-fs";
import { getDiForUnitTesting } from "../getDiForUnitTesting";
import createContextHandlerInjectable from "../context-handler/create-context-handler.injectable";
import type { Cluster } from "../../common/cluster/cluster";
import kubeAuthProxyCaInjectable from "../kube-auth-proxy/kube-auth-proxy-ca.injectable";
import createKubeAuthProxyInjectable from "../kube-auth-proxy/create-kube-auth-proxy.injectable";
jest.mock("electron", () => ({
app: {
@ -80,6 +82,9 @@ describe("ContextHandler", () => {
"tmp": {},
});
di.override(createKubeAuthProxyInjectable, () => ({}));
di.override(kubeAuthProxyCaInjectable, () => ({}));
await di.runSetups();
createContextHandler = di.inject(createContextHandlerInjectable);

View File

@ -50,6 +50,9 @@ import { getDiForUnitTesting } from "../getDiForUnitTesting";
import createKubeAuthProxyInjectable from "../kube-auth-proxy/create-kube-auth-proxy.injectable";
import { createClusterInjectionToken } from "../../common/cluster/create-cluster-injection-token";
import path from "path";
import spawnInjectable from "../child-process/spawn.injectable";
import kubeAuthProxyCaInjectable from "../kube-auth-proxy/kube-auth-proxy-ca.injectable";
import createKubeAuthProxyCertFilesInjectable from "../kube-auth-proxy/create-kube-auth-proxy-cert-files.injectable";
console = new Console(stdout, stderr);
@ -92,6 +95,10 @@ describe("kube auth proxy tests", () => {
const di = getDiForUnitTesting({ doGeneralOverrides: true });
di.override(spawnInjectable, () => mockSpawn);
di.override(createKubeAuthProxyCertFilesInjectable, () => ({}));
di.override(kubeAuthProxyCaInjectable, () => ({}));
mockFs(mockMinikubeConfig);
await di.runSetups();
@ -130,7 +137,7 @@ describe("kube auth proxy tests", () => {
beforeEach(async () => {
mockedCP = mock<ChildProcess>();
listeners = new EventEmitter();
jest.spyOn(Kubectl.prototype, "checkBinary").mockReturnValueOnce(Promise.resolve(true));
jest.spyOn(Kubectl.prototype, "ensureKubectl").mockReturnValueOnce(Promise.resolve(false));
mockedCP.on.mockImplementation((event: string, listener: (message: any, sendHandle: any) => void): ChildProcess => {

View File

@ -40,15 +40,18 @@ import * as path from "path";
import createKubeconfigManagerInjectable from "../kubeconfig-manager/create-kubeconfig-manager.injectable";
import { createClusterInjectionToken } from "../../common/cluster/create-cluster-injection-token";
import directoryForTempInjectable from "../../common/app-paths/directory-for-temp/directory-for-temp.injectable";
import createContextHandlerInjectable from "../context-handler/create-context-handler.injectable";
import type { DiContainer } from "@ogre-tools/injectable";
console = new Console(process.stdout, process.stderr); // fix mockFS
describe("kubeconfig manager tests", () => {
let cluster: Cluster;
let clusterFake: Cluster;
let createKubeconfigManager: (cluster: Cluster) => KubeconfigManager;
let di: DiContainer;
beforeEach(async () => {
const di = getDiForUnitTesting({ doGeneralOverrides: true });
di = getDiForUnitTesting({ doGeneralOverrides: true });
di.override(directoryForTempInjectable, () => "some-directory-for-temp");
@ -78,17 +81,21 @@ describe("kubeconfig manager tests", () => {
await di.runSetups();
di.override(createContextHandlerInjectable, () => () => {
throw new Error("you should never come here");
});
const createCluster = di.inject(createClusterInjectionToken);
createKubeconfigManager = di.inject(createKubeconfigManagerInjectable);
cluster = createCluster({
clusterFake = createCluster({
id: "foo",
contextName: "minikube",
kubeConfigPath: "minikube-config.yml",
});
cluster.contextHandler = {
clusterFake.contextHandler = {
ensureServer: () => Promise.resolve(),
} as any;
@ -100,7 +107,7 @@ describe("kubeconfig manager tests", () => {
});
it("should create 'temp' kube config with proxy", async () => {
const kubeConfManager = createKubeconfigManager(cluster);
const kubeConfManager = createKubeconfigManager(clusterFake);
expect(logger.error).not.toBeCalled();
expect(await kubeConfManager.getPath()).toBe(`some-directory-for-temp${path.sep}kubeconfig-foo`);
@ -115,7 +122,7 @@ describe("kubeconfig manager tests", () => {
});
it("should remove 'temp' kube config on unlink and remove reference from inside class", async () => {
const kubeConfManager = createKubeconfigManager(cluster);
const kubeConfManager = createKubeconfigManager(clusterFake);
const configPath = await kubeConfManager.getPath();

View File

@ -16,6 +16,8 @@ import { getDiForUnitTesting } from "../../getDiForUnitTesting";
import { createClusterInjectionToken } from "../../../common/cluster/create-cluster-injection-token";
import directoryForKubeConfigsInjectable
from "../../../common/app-paths/directory-for-kube-configs/directory-for-kube-configs.injectable";
import kubeAuthProxyCaInjectable from "../../kube-auth-proxy/kube-auth-proxy-ca.injectable";
import createKubeAuthProxyCertFilesInjectable from "../../kube-auth-proxy/create-kube-auth-proxy-cert-files.injectable";
jest.mock("electron", () => ({
@ -40,6 +42,9 @@ describe("kubeconfig-sync.source tests", () => {
beforeEach(async () => {
const di = getDiForUnitTesting({ doGeneralOverrides: true });
di.override(kubeAuthProxyCaInjectable, () => Promise.resolve(Buffer.from("ca")));
di.override(createKubeAuthProxyCertFilesInjectable, () => ({}));
mockFs();
await di.runSetups();

View File

@ -0,0 +1,17 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { spawn } from "child_process";
const spawnInjectable = getInjectable({
id: "spawn",
instantiate: () => {
return spawn;
},
causesSideEffects: true,
});
export default spawnInjectable;

View File

@ -12,8 +12,6 @@ import url, { UrlWithStringQuery } from "url";
import { CoreV1Api } from "@kubernetes/client-node";
import logger from "../logger";
import type { KubeAuthProxy } from "../kube-auth-proxy/kube-auth-proxy";
import path from "path";
import { readFile } from "fs/promises";
import type { CreateKubeAuthProxy } from "../kube-auth-proxy/create-kube-auth-proxy.injectable";
export interface PrometheusDetails {
@ -30,7 +28,7 @@ interface PrometheusServicePreferences {
interface Dependencies {
createKubeAuthProxy: CreateKubeAuthProxy;
certPath: string;
authProxyCa: Promise<Buffer>;
}
export class ContextHandler {
@ -126,7 +124,7 @@ export class ContextHandler {
}
async resolveAuthProxyCa() {
return readFile(path.join(this.dependencies.certPath, "proxy.crt"));
return this.dependencies.authProxyCa;
}
async getApiTarget(isLongRunningRequest = false): Promise<httpProxy.ServerOptions> {

View File

@ -6,15 +6,17 @@ import { getInjectable } from "@ogre-tools/injectable";
import type { Cluster } from "../../common/cluster/cluster";
import { ContextHandler } from "./context-handler";
import createKubeAuthProxyInjectable from "../kube-auth-proxy/create-kube-auth-proxy.injectable";
import getKubeAuthProxyCertDirInjectable from "../kube-auth-proxy/kube-auth-proxy-cert.injectable";
import kubeAuthProxyCaInjectable from "../kube-auth-proxy/kube-auth-proxy-ca.injectable";
const createContextHandlerInjectable = getInjectable({
id: "create-context-handler",
instantiate: (di) => {
const authProxyCa = di.inject(kubeAuthProxyCaInjectable);
const dependencies = {
createKubeAuthProxy: di.inject(createKubeAuthProxyInjectable),
certPath: di.inject(getKubeAuthProxyCertDirInjectable),
authProxyCa,
};
return (cluster: Cluster) => new ContextHandler(dependencies, cluster);

View File

@ -15,8 +15,8 @@ import registerChannelInjectable from "./app-paths/register-channel/register-cha
import writeJsonFileInjectable from "../common/fs/write-json-file.injectable";
import readJsonFileInjectable from "../common/fs/read-json-file.injectable";
import directoryForBundledBinariesInjectable from "../common/app-paths/directory-for-bundled-binaries/directory-for-bundled-binaries.injectable";
import getKubeAuthProxyCertDirInjectable from "./kube-auth-proxy/kube-auth-proxy-cert.injectable";
import createKubeAuthProxyCertFilesInjectable from "./kube-auth-proxy/create-kube-auth-proxy-cert-files.injectable";
import spawnInjectable from "./child-process/spawn.injectable";
import readFileInjectable from "../common/fs/read-file.injectable";
export const getDiForUnitTesting = (
{ doGeneralOverrides } = { doGeneralOverrides: false },
@ -47,8 +47,13 @@ export const getDiForUnitTesting = (
di.override(appNameInjectable, () => "some-electron-app-name");
di.override(registerChannelInjectable, () => () => undefined);
di.override(directoryForBundledBinariesInjectable, () => "some-bin-directory");
di.override(getKubeAuthProxyCertDirInjectable, () => "some-kube-auth-proxy-cert-directory");
di.override(createKubeAuthProxyCertFilesInjectable, async () => "does-not-matter");
di.override(spawnInjectable, () => () => {
return {
stderr: { on: jest.fn(), removeAllListeners: jest.fn() },
stdout: { on: jest.fn(), removeAllListeners: jest.fn() },
on: jest.fn(),
} as any;
});
di.override(writeJsonFileInjectable, () => () => {
throw new Error("Tried to write JSON file to file system without specifying explicit override.");
@ -57,6 +62,10 @@ export const getDiForUnitTesting = (
di.override(readJsonFileInjectable, () => () => {
throw new Error("Tried to read JSON file from file system without specifying explicit override.");
});
di.override(readFileInjectable, () => () => {
throw new Error("Tried to read a file from file system without specifying explicit override.");
});
}
return di;

View File

@ -3,24 +3,20 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { writeFile } from "fs/promises";
import { ensureDir } from "fs-extra";
import directoryForUserDataInjectable from "../../common/app-paths/directory-for-user-data/directory-for-user-data.injectable";
import { getKubeAuthProxyCertificatePath } from "./kube-auth-proxy-cert";
import * as selfsigned from "selfsigned";
import { createKubeAuthProxyCertFiles } from "./create-kube-auth-proxy-cert-files";
import getKubeAuthProxyCertDirInjectable from "./kube-auth-proxy-cert.injectable";
import writeFileInjectable from "../../common/fs/write-file.injectable";
const createKubeAuthProxyCertFilesInjectable = getInjectable({
id: "create-kube-auth-proxy-cert-files",
instantiate: async (di) => {
const userData = di.inject(directoryForUserDataInjectable);
const certPath = getKubeAuthProxyCertificatePath(userData);
const certPath = di.inject(getKubeAuthProxyCertDirInjectable);
return createKubeAuthProxyCertFiles(certPath, {
generate: selfsigned.generate,
ensureDir,
writeFile,
generate: selfsigned.generate,
writeFile: di.inject(writeFileInjectable),
});
},
});

View File

@ -3,8 +3,6 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { writeFile } from "fs/promises";
import type { ensureDir } from "fs-extra";
import path from "path";
import type * as selfsigned from "selfsigned";
@ -12,8 +10,7 @@ type SelfSignedGenerate = typeof selfsigned.generate;
interface CreateKubeAuthProxyCertificateFilesDependencies {
generate: SelfSignedGenerate;
ensureDir: typeof ensureDir;
writeFile: typeof writeFile;
writeFile: (path: string, content: string | Buffer) => Promise<void>;
}
function getKubeAuthProxyCertificate(generate: SelfSignedGenerate): selfsigned.SelfSignedCert {
@ -39,7 +36,6 @@ function getKubeAuthProxyCertificate(generate: SelfSignedGenerate): selfsigned.S
export async function createKubeAuthProxyCertFiles(dir: string, dependencies: CreateKubeAuthProxyCertificateFilesDependencies): Promise<string> {
const cert = getKubeAuthProxyCertificate(dependencies.generate);
await dependencies.ensureDir(dir);
await dependencies.writeFile(path.join(dir, "proxy.key"), cert.private);
await dependencies.writeFile(path.join(dir, "proxy.crt"), cert.cert);

View File

@ -4,12 +4,12 @@
*/
import { getInjectable } from "@ogre-tools/injectable";
import { KubeAuthProxy, KubeAuthProxyDependencies } from "./kube-auth-proxy";
import { spawn } from "child_process";
import type { Cluster } from "../../common/cluster/cluster";
import path from "path";
import { getBinaryName } from "../../common/vars";
import directoryForBundledBinariesInjectable from "../../common/app-paths/directory-for-bundled-binaries/directory-for-bundled-binaries.injectable";
import getKubeAuthProxyCertDirInjectable from "./kube-auth-proxy-cert.injectable";
import spawnInjectable from "../child-process/spawn.injectable";
import createKubeAuthProxyCertFilesInjectable from "./create-kube-auth-proxy-cert-files.injectable";
export type CreateKubeAuthProxy = (cluster: Cluster, environmentVariables: NodeJS.ProcessEnv) => KubeAuthProxy;
@ -20,8 +20,8 @@ const createKubeAuthProxyInjectable = getInjectable({
const binaryName = getBinaryName("lens-k8s-proxy");
const dependencies: KubeAuthProxyDependencies = {
proxyBinPath: path.join(di.inject(directoryForBundledBinariesInjectable), binaryName),
proxyCertPath: di.inject(getKubeAuthProxyCertDirInjectable),
spawn,
proxyCertPath: di.inject(createKubeAuthProxyCertFilesInjectable),
spawn: di.inject(spawnInjectable),
};
return (cluster: Cluster, environmentVariables: NodeJS.ProcessEnv) => (

View File

@ -0,0 +1,21 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import getKubeAuthProxyCertDirInjectable from "../kube-auth-proxy/kube-auth-proxy-cert.injectable";
import path from "path";
import readFileInjectable from "../../common/fs/read-file.injectable";
const kubeAuthProxyCaInjectable = getInjectable({
id: "kube-auth-proxy-ca",
instantiate: (di) => {
const certPath = di.inject(getKubeAuthProxyCertDirInjectable);
const readFile = di.inject(readFileInjectable);
return readFile(path.join(certPath, "proxy.crt"));
},
});
export default kubeAuthProxyCaInjectable;

View File

@ -5,15 +5,10 @@
import { getInjectable } from "@ogre-tools/injectable";
import directoryForUserDataInjectable from "../../common/app-paths/directory-for-user-data/directory-for-user-data.injectable";
import { getKubeAuthProxyCertificatePath } from "./kube-auth-proxy-cert";
import createKubeAuthProxyCertFilesInjectable from "./create-kube-auth-proxy-cert-files.injectable";
const getKubeAuthProxyCertDirInjectable = getInjectable({
id: "get-kube-auth-proxy-cert-dir",
setup: async (di) => {
await di.inject(createKubeAuthProxyCertFilesInjectable);
},
instantiate: (di) => getKubeAuthProxyCertificatePath(di.inject(directoryForUserDataInjectable)),
});

View File

@ -15,7 +15,7 @@ const startingServeRegex = /starting to serve on (?<address>.+)/i;
export interface KubeAuthProxyDependencies {
proxyBinPath: string;
proxyCertPath: string;
proxyCertPath: Promise<string>;
spawn: typeof spawn;
}
@ -44,7 +44,7 @@ export class KubeAuthProxy {
}
const proxyBin = this.dependencies.proxyBinPath;
const certPath = this.dependencies.proxyCertPath;
const certPath = await this.dependencies.proxyCertPath;
this.proxyProcess = this.dependencies.spawn(proxyBin, [], {
env: {

View File

@ -15,6 +15,7 @@ import { DeleteClusterDialog } from "../delete-cluster-dialog";
import type { ClusterModel } from "../../../../common/cluster-types";
import { getDisForUnitTesting } from "../../../../test-utils/get-dis-for-unit-testing";
import { createClusterInjectionToken } from "../../../../common/cluster/create-cluster-injection-token";
import createContextHandlerInjectable from "../../../../main/context-handler/create-context-handler.injectable";
jest.mock("electron", () => ({
app: {
@ -91,6 +92,8 @@ describe("<DeleteClusterDialog />", () => {
beforeEach(async () => {
const { mainDi, runSetups } = getDisForUnitTesting({ doGeneralOverrides: true });
mainDi.override(createContextHandlerInjectable, () => () => undefined);
mockFs();
await runSetups();