diff --git a/backend/src/utils/certs/helpers.ts b/backend/src/utils/certs/helpers.ts index 1a2289f4b..7367c61ea 100644 --- a/backend/src/utils/certs/helpers.ts +++ b/backend/src/utils/certs/helpers.ts @@ -11,19 +11,16 @@ export const runCommand = (command: string, options: ExecOptions = {}) => { export const installRootCert = async (certPath: string) => { try { - // Read the certificate file content - const certData = readFileSync(certPath, { encoding: 'utf-8' }); - // Check if the certificate already exists in the system's root trust store switch (process.platform) { case 'darwin': - await handleMacCert(certPath, certData); + await handleMacCert(certPath); break; case 'win32': await handleWindowsCert(certPath); break; case 'linux': - await handleLinuxCert(certPath, certData); + await handleLinuxCert(certPath); break; default: console.info('Unsupported platform:', process.platform); @@ -34,7 +31,8 @@ export const installRootCert = async (certPath: string) => { } }; -const handleMacCert = async (certPath: string, certData: string) => { +const handleMacCert = async (certPath: string) => { + const certData = readFileSync(certPath, { encoding: 'utf-8' }); const result = await runCommand(`security find-certificate -a -c "${certData}" /Library/Keychains/System.keychain`); if (result.exitCode === 0) return console.info('Root certificate already installed on Mac!'); @@ -55,14 +53,11 @@ const handleWindowsCert = async (certPath: string) => { console.info('Root certificate installed successfully on Windows!'); }; -const handleLinuxCert = async (certPath: string, certData: string) => { - const certHash = await runCommand(`openssl x509 -in ${certData} -noout -fingerprint -sha256`); - const certFingerprint = certHash.exitCode === 0 ? certHash : null; - - const result = await runCommand(`grep -rl ${certFingerprint} /usr/local/share/ca-certificates/`); - if (result.exitCode === 0) return console.info('Root certificate already installed on Linux!'); +const handleLinuxCert = async (certPath: string) => { + const rootPath = '/usr/local/share/ca-certificates/'; - await runCommand(`sudo cp ${certPath} /usr/local/share/ca-certificates/`); + await runCommand(`sudo mkdir -p ${certPath} ${rootPath}`); + await runCommand(`sudo cp -n ${certPath} ${rootPath}/cella.crt`); await runCommand('sudo update-ca-certificates'); console.info('Root certificate installed successfully on Linux!'); };