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

Fix win-ca failing on electron renderer on windows

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-09-08 08:07:16 -04:00
parent 9bad482237
commit 2702ed7468
2 changed files with 50 additions and 18 deletions

View File

@ -3,26 +3,40 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import { getInjectable } from "@ogre-tools/injectable"; import { getInjectable } from "@ogre-tools/injectable";
import wincaAPI from "win-ca/api"; import execFileInjectable from "../fs/exec-file.injectable";
import { requestSystemCAsInjectionToken } from "./request-system-cas-token"; import { requestSystemCAsInjectionToken } from "./request-system-cas-token";
const pemEncoding = (hexEncodedCert: String) => {
const certData = Buffer.from(hexEncodedCert, "hex").toString('base64');
const lines = ['-----BEGIN CERTIFICATE-----'];
for (let i = 0; i < certData.length; i += 64) {
lines.push(certData.substring(i, i + 64));
}
lines.push('-----END CERTIFICATE-----', '');
return lines.join("\r\n");
}
const requestSystemCAsInjectable = getInjectable({ const requestSystemCAsInjectable = getInjectable({
id: "request-system-cas", id: "request-system-cas",
instantiate: () => { instantiate: (di) => {
return () => new Promise<string[]>((resolve) => { const wincaRootsExePath: string = __non_webpack_require__.resolve("win-ca/lib/roots.exe");
const CAs: string[] = []; const execFile = di.inject(execFileInjectable);
wincaAPI({ return async () => {
format: wincaAPI.der2.pem, /**
inject: false, * This needs to be done manually because for some reason calling the api from "win-ca"
ondata: (ca: string) => { * directly fails to load "child_process" correctly on renderer
CAs.push(ca); */
}, const output = await execFile(wincaRootsExePath);
onend: () => {
resolve(CAs); return output
}, .split("\r\n")
}); .filter(Boolean)
}); .map(pemEncoding);
};
}, },
causesSideEffects: true, causesSideEffects: true,
injectionToken: requestSystemCAsInjectionToken, injectionToken: requestSystemCAsInjectionToken,

View File

@ -7,7 +7,11 @@ import type { ExecFileOptions } from "child_process";
import { execFile } from "child_process"; import { execFile } from "child_process";
import { promisify } from "util"; import { promisify } from "util";
export type ExecFile = (filePath: string, args: string[], options: ExecFileOptions) => Promise<string>; export interface ExecFile {
(filePath: string): Promise<string>;
(filePath: string, argsOrOptions: string[] | ExecFileOptions): Promise<string>;
(filePath: string, args: string[], options: ExecFileOptions): Promise<string>;
}
const execFileInjectable = getInjectable({ const execFileInjectable = getInjectable({
id: "exec-file", id: "exec-file",
@ -15,8 +19,22 @@ const execFileInjectable = getInjectable({
instantiate: (): ExecFile => { instantiate: (): ExecFile => {
const asyncExecFile = promisify(execFile); const asyncExecFile = promisify(execFile);
return async (filePath, args, options) => { return async (filePath: string, argsOrOptions?: string[] | ExecFileOptions, maybeOptions?: ExecFileOptions) => {
const result = await asyncExecFile(filePath, args, options); let args: string[];
let options: ExecFileOptions;
if (Array.isArray(argsOrOptions)) {
args = argsOrOptions;
options = maybeOptions ?? {};
} else {
args = [];
options = maybeOptions ?? argsOrOptions ?? {};
}
const result = await asyncExecFile(filePath, args, {
encoding: "utf-8",
...options,
});
return result.stdout; return result.stdout;
}; };