diff --git a/src/main/kube-auth-proxy/create-kube-auth-proxy.injectable.ts b/src/main/kube-auth-proxy/create-kube-auth-proxy.injectable.ts index 9536264be4..845ae92912 100644 --- a/src/main/kube-auth-proxy/create-kube-auth-proxy.injectable.ts +++ b/src/main/kube-auth-proxy/create-kube-auth-proxy.injectable.ts @@ -4,6 +4,7 @@ */ 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"; @@ -18,6 +19,7 @@ const createKubeAuthProxyInjectable = getInjectable({ const dependencies: KubeAuthProxyDependencies = { proxyBinPath: path.join(di.inject(directoryForBundledBinariesInjectable), binaryName), proxyCertPath: di.inject(getKubeAuthProxyCertDirInjectable), + spawn, }; return (cluster: Cluster, environmentVariables: NodeJS.ProcessEnv) => ( diff --git a/src/main/kube-auth-proxy/kube-auth-proxy-cert.injectable.ts b/src/main/kube-auth-proxy/kube-auth-proxy-cert.injectable.ts index 7c9935d6e3..733d6dffea 100644 --- a/src/main/kube-auth-proxy/kube-auth-proxy-cert.injectable.ts +++ b/src/main/kube-auth-proxy/kube-auth-proxy-cert.injectable.ts @@ -5,6 +5,7 @@ import { getInjectable } from "@ogre-tools/injectable"; import directoryForUserDataInjectable from "../../common/app-paths/directory-for-user-data/directory-for-user-data.injectable"; import { createKubeAuthProxyCertificateFiles, getKubeAuthProxyCertificatePath } from "./kube-auth-proxy-cert"; +import * as selfsigned from "selfsigned"; const getKubeAuthProxyCertDirInjectable = getInjectable({ id: "get-kube-auth-proxy-cert-dir", @@ -13,7 +14,7 @@ const getKubeAuthProxyCertDirInjectable = getInjectable({ const userData = await di.inject(directoryForUserDataInjectable); const certPath = getKubeAuthProxyCertificatePath(userData); - await createKubeAuthProxyCertificateFiles(certPath); + await createKubeAuthProxyCertificateFiles(certPath, selfsigned.generate); }, instantiate: (di) => getKubeAuthProxyCertificatePath(di.inject(directoryForUserDataInjectable)), diff --git a/src/main/kube-auth-proxy/kube-auth-proxy-cert.ts b/src/main/kube-auth-proxy/kube-auth-proxy-cert.ts index bbbd8e6c1e..b3016d7882 100644 --- a/src/main/kube-auth-proxy/kube-auth-proxy-cert.ts +++ b/src/main/kube-auth-proxy/kube-auth-proxy-cert.ts @@ -5,15 +5,17 @@ import { access, mkdir, writeFile } from "fs/promises"; import path from "path"; -import * as selfsigned from "selfsigned"; +import type * as selfsigned from "selfsigned"; -export function getKubeAuthProxyCertificate(): selfsigned.SelfSignedCert { +type SelfSignedGenerate = typeof selfsigned.generate; + +export function getKubeAuthProxyCertificate(generate: SelfSignedGenerate): selfsigned.SelfSignedCert { const opts = [ { name: "commonName", value: "Lens Certificate Authority" }, { name: "organizationName", value: "Lens" }, ]; - return selfsigned.generate(opts, { + return generate(opts, { keySize: 2048, algorithm: "sha256", days: 365, @@ -31,8 +33,8 @@ export function getKubeAuthProxyCertificatePath(baseDir: string) { return path.join(baseDir, "kube-auth-proxy"); } -export async function createKubeAuthProxyCertificateFiles(dir: string): Promise { - const cert = getKubeAuthProxyCertificate(); +export async function createKubeAuthProxyCertificateFiles(dir: string, generateFunc: SelfSignedGenerate): Promise { + const cert = getKubeAuthProxyCertificate(generateFunc); try { await access(dir); diff --git a/src/main/kube-auth-proxy/kube-auth-proxy.ts b/src/main/kube-auth-proxy/kube-auth-proxy.ts index 3606bd5b7d..62e75ee15c 100644 --- a/src/main/kube-auth-proxy/kube-auth-proxy.ts +++ b/src/main/kube-auth-proxy/kube-auth-proxy.ts @@ -3,7 +3,7 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ -import { ChildProcess, spawn } from "child_process"; +import type { ChildProcess, spawn } from "child_process"; import { waitUntilUsed } from "tcp-port-used"; import { randomBytes } from "crypto"; import type { Cluster } from "../../common/cluster/cluster"; @@ -16,6 +16,7 @@ const startingServeRegex = /starting to serve on (?
.+)/i; export interface KubeAuthProxyDependencies { proxyBinPath: string; proxyCertPath: string; + spawn: typeof spawn; } export class KubeAuthProxy { @@ -45,7 +46,7 @@ export class KubeAuthProxy { const proxyBin = this.dependencies.proxyBinPath; const certPath = await this.dependencies.proxyCertPath; - this.proxyProcess = spawn(proxyBin, [], { + this.proxyProcess = this.dependencies.spawn(proxyBin, [], { env: { ...this.env, KUBECONFIG: this.cluster.kubeConfigPath,