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 f272d9e56f..ba64b69c69 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 @@ -6,14 +6,11 @@ import { getInjectable } from "@ogre-tools/injectable"; import type { KubeAuthProxyDependencies } from "./kube-auth-proxy"; import { KubeAuthProxy } from "./kube-auth-proxy"; import type { Cluster } from "../../common/cluster/cluster"; -import selfsigned from "selfsigned"; -import { getBinaryName } from "../../common/vars"; import spawnInjectable from "../child-process/spawn.injectable"; -import { getKubeAuthProxyCertificate } from "./get-kube-auth-proxy-certificate"; +import kubeAuthProxyCertificateInjectable from "./kube-auth-proxy-certificate.injectable"; import loggerInjectable from "../../common/logger.injectable"; -import baseBundledBinariesDirectoryInjectable from "../../common/vars/base-bundled-binaries-dir.injectable"; import waitUntilPortIsUsedInjectable from "./wait-until-port-is-used/wait-until-port-is-used.injectable"; -import joinPathsInjectable from "../../common/path/join-paths.injectable"; +import lensK8sProxyPathInjectable from "./lens-k8s-proxy-path.injectable"; export type CreateKubeAuthProxy = (cluster: Cluster, environmentVariables: NodeJS.ProcessEnv) => KubeAuthProxy; @@ -21,20 +18,20 @@ const createKubeAuthProxyInjectable = getInjectable({ id: "create-kube-auth-proxy", instantiate: (di): CreateKubeAuthProxy => { - const binaryName = getBinaryName("lens-k8s-proxy"); - const joinPaths = di.inject(joinPathsInjectable); + const dependencies: Omit = { + proxyBinPath: di.inject(lensK8sProxyPathInjectable), + spawn: di.inject(spawnInjectable), + logger: di.inject(loggerInjectable), + waitUntilPortIsUsed: di.inject(waitUntilPortIsUsedInjectable), + }; return (cluster: Cluster, environmentVariables: NodeJS.ProcessEnv) => { const clusterUrl = new URL(cluster.apiUrl); - const dependencies: KubeAuthProxyDependencies = { - proxyBinPath: joinPaths(di.inject(baseBundledBinariesDirectoryInjectable), binaryName), - proxyCert: getKubeAuthProxyCertificate(clusterUrl.hostname, selfsigned.generate), - spawn: di.inject(spawnInjectable), - logger: di.inject(loggerInjectable), - waitUntilPortIsUsed: di.inject(waitUntilPortIsUsedInjectable), - }; - return new KubeAuthProxy(dependencies, cluster, environmentVariables); + return new KubeAuthProxy({ + ...dependencies, + proxyCert: di.inject(kubeAuthProxyCertificateInjectable, clusterUrl.hostname), + }, cluster, environmentVariables); }; }, }); diff --git a/src/main/kube-auth-proxy/get-kube-auth-proxy-certificate.ts b/src/main/kube-auth-proxy/kube-auth-proxy-certificate.injectable.ts similarity index 60% rename from src/main/kube-auth-proxy/get-kube-auth-proxy-certificate.ts rename to src/main/kube-auth-proxy/kube-auth-proxy-certificate.injectable.ts index f6fa71702c..35358f1e52 100644 --- a/src/main/kube-auth-proxy/get-kube-auth-proxy-certificate.ts +++ b/src/main/kube-auth-proxy/kube-auth-proxy-certificate.injectable.ts @@ -3,15 +3,12 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ -import type * as selfsigned from "selfsigned"; -import { getOrInsertWith } from "../../common/utils"; +import { generate } from "selfsigned"; +import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable"; -type SelfSignedGenerate = typeof selfsigned.generate; - -const certCache = new Map(); - -export function getKubeAuthProxyCertificate(hostname: string, generate: SelfSignedGenerate): selfsigned.SelfSignedCert { - return getOrInsertWith(certCache, hostname, () => generate( +const kubeAuthProxyCertificateInjectable = getInjectable({ + id: "kube-auth-proxy-certificate", + instantiate: (di, hostname) => generate( [ { name: "commonName", value: "Lens Certificate Authority" }, { name: "organizationName", value: "Lens" }, @@ -31,5 +28,11 @@ export function getKubeAuthProxyCertificate(hostname: string, generate: SelfSign }, ], }, - )); -} + ), + lifecycle: lifecycleEnum.keyedSingleton({ + getInstanceKey: (di, hostname: string) => hostname, + }), +}); + +export default kubeAuthProxyCertificateInjectable; +