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-10 16:58:58 +02:00
parent 5176970438
commit 7207edd8f2
5 changed files with 85 additions and 22 deletions

View File

@ -15,6 +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";
export const getDiForUnitTesting = (
{ doGeneralOverrides } = { doGeneralOverrides: false },
@ -45,6 +47,8 @@ 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(writeJsonFileInjectable, () => () => {
throw new Error("Tried to write JSON file to file system without specifying explicit override.");

View File

@ -0,0 +1,26 @@
/**
* 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 { access, mkdir, writeFile } from "fs/promises";
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";
const createKubeAuthProxyCertFilesInjectable = getInjectable({
id: "create-kube-auth-proxy-cert-files",
instantiate: async (di) => {
const userData = di.inject(directoryForUserDataInjectable);
const certPath = getKubeAuthProxyCertificatePath(userData);
return createKubeAuthProxyCertFiles(certPath, {
generate: selfsigned.generate,
access, mkdir, writeFile,
});
},
});
export default createKubeAuthProxyCertFilesInjectable;

View File

@ -0,0 +1,52 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { access, mkdir, writeFile } from "fs/promises";
import path from "path";
import type * as selfsigned from "selfsigned";
type SelfSignedGenerate = typeof selfsigned.generate;
interface CreateKubeAuthProxyCertificateFilesDependencies {
generate: SelfSignedGenerate;
access: typeof access;
mkdir: typeof mkdir;
writeFile: typeof writeFile;
}
function getKubeAuthProxyCertificate(generate: SelfSignedGenerate): selfsigned.SelfSignedCert {
const opts = [
{ name: "commonName", value: "Lens Certificate Authority" },
{ name: "organizationName", value: "Lens" },
];
return generate(opts, {
keySize: 2048,
algorithm: "sha256",
days: 365,
extensions: [
{ name: "basicConstraints", cA: true },
{ name: "subjectAltName", altNames: [
{ type: 2, value: "localhost" },
{ type: 7, ip: "127.0.0.1" },
] },
],
});
}
export async function createKubeAuthProxyCertFiles(dir: string, dependencies: CreateKubeAuthProxyCertificateFilesDependencies): Promise<string> {
const cert = getKubeAuthProxyCertificate(dependencies.generate);
try {
await dependencies.access(dir);
} catch {
await dependencies.mkdir(dir);
}
await dependencies.writeFile(path.join(dir, "proxy.key"), cert.private);
await dependencies.writeFile(path.join(dir, "proxy.crt"), cert.cert);
return dir;
}

View File

@ -4,17 +4,14 @@
*/
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";
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) => {
const userData = await di.inject(directoryForUserDataInjectable);
const certPath = getKubeAuthProxyCertificatePath(userData);
await createKubeAuthProxyCertificateFiles(certPath, selfsigned.generate);
await di.inject(createKubeAuthProxyCertFilesInjectable);
},
instantiate: (di) => getKubeAuthProxyCertificatePath(di.inject(directoryForUserDataInjectable)),

View File

@ -3,7 +3,6 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { access, mkdir, writeFile } from "fs/promises";
import path from "path";
import type * as selfsigned from "selfsigned";
@ -32,18 +31,3 @@ export function getKubeAuthProxyCertificate(generate: SelfSignedGenerate): selfs
export function getKubeAuthProxyCertificatePath(baseDir: string) {
return path.join(baseDir, "kube-auth-proxy");
}
export async function createKubeAuthProxyCertificateFiles(dir: string, generateFunc: SelfSignedGenerate): Promise<string> {
const cert = getKubeAuthProxyCertificate(generateFunc);
try {
await access(dir);
} catch {
await mkdir(dir);
}
await writeFile(path.join(dir, "proxy.key"), cert.private);
await writeFile(path.join(dir, "proxy.crt"), cert.cert);
return dir;
}