1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Introduce injectable for kube auth proxy certs

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-11-16 11:54:12 -05:00
parent ac23066eec
commit fad632c6ed
2 changed files with 25 additions and 25 deletions

View File

@ -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<KubeAuthProxyDependencies, "proxyCert"> = {
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);
};
},
});

View File

@ -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<string, selfsigned.SelfSignedCert>();
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;