Skip to content

Commit

Permalink
fix: cert install for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonardoD committed Jan 22, 2025
1 parent caeeb8a commit 8151661
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions backend/src/utils/certs/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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!');

Expand All @@ -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!');
};

0 comments on commit 8151661

Please sign in to comment.