@@ -2,6 +2,7 @@ import os from 'node:os'
22import { consola as log } from 'consola'
33import type { Cert , CertPath , TlsOption } from '../types'
44import { config } from '../config'
5+ import { CERT_CONSTANTS , LOG_CATEGORIES } from '../constants'
56import { debugLog , findFoldersWithFile , runCommand } from '../utils'
67import { storeCACertificate , storeCertificate } from './store'
78
@@ -15,7 +16,7 @@ interface TrustStoreHandler {
1516const macOSTrustStoreHandler : TrustStoreHandler = {
1617 platform : 'darwin' ,
1718 async addCertificate ( caCertPath : string , options ?: TlsOption ) : Promise < void > {
18- debugLog ( 'trust' , 'Adding certificate to macOS keychain' , options ?. verbose )
19+ debugLog ( LOG_CATEGORIES . TRUST , 'Adding certificate to macOS keychain' , options ?. verbose )
1920 await runCommand (
2021 `sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ${ caCertPath } ` ,
2122 )
@@ -26,7 +27,7 @@ const macOSTrustStoreHandler: TrustStoreHandler = {
2627const windowsTrustStoreHandler : TrustStoreHandler = {
2728 platform : 'win32' ,
2829 async addCertificate ( caCertPath : string , options ?: TlsOption ) : Promise < void > {
29- debugLog ( 'trust' , 'Adding certificate to Windows certificate store' , options ?. verbose )
30+ debugLog ( LOG_CATEGORIES . TRUST , 'Adding certificate to Windows certificate store' , options ?. verbose )
3031 await runCommand ( `certutil -f -v -addstore -enterprise Root ${ caCertPath } ` )
3132 } ,
3233}
@@ -35,12 +36,12 @@ const windowsTrustStoreHandler: TrustStoreHandler = {
3536const linuxTrustStoreHandler : TrustStoreHandler = {
3637 platform : 'linux' ,
3738 async addCertificate ( caCertPath : string , options ?: TlsOption ) : Promise < void > {
38- debugLog ( 'trust' , 'Adding certificate to Linux certificate store' , options ?. verbose )
39+ debugLog ( LOG_CATEGORIES . TRUST , 'Adding certificate to Linux certificate store' , options ?. verbose )
3940 const rootDirectory = os . homedir ( )
40- const targetFileName = 'cert9.db'
41- const args = 'TC, C, C'
41+ const targetFileName = CERT_CONSTANTS . LINUX_CERT_DB_FILENAME
42+ const args = CERT_CONSTANTS . LINUX_TRUST_ARGS
4243
43- debugLog ( 'trust' , `Searching for certificate databases in ${ rootDirectory } ` , options ?. verbose )
44+ debugLog ( LOG_CATEGORIES . TRUST , `Searching for certificate databases in ${ rootDirectory } ` , options ?. verbose )
4445 const foldersWithFile = findFoldersWithFile ( rootDirectory , targetFileName )
4546
4647 if ( foldersWithFile . length === 0 ) {
@@ -49,17 +50,17 @@ const linuxTrustStoreHandler: TrustStoreHandler = {
4950 }
5051
5152 for ( const folder of foldersWithFile ) {
52- debugLog ( 'trust' , `Processing certificate database in ${ folder } ` , options ?. verbose )
53+ debugLog ( LOG_CATEGORIES . TRUST , `Processing certificate database in ${ folder } ` , options ?. verbose )
5354 try {
54- debugLog ( 'trust' , `Attempting to delete existing cert for ${ config . commonName } ` , options ?. verbose )
55+ debugLog ( LOG_CATEGORIES . TRUST , `Attempting to delete existing cert for ${ config . commonName } ` , options ?. verbose )
5556 await runCommand ( `certutil -d sql:${ folder } -D -n ${ config . commonName } ` )
5657 }
5758 catch ( error ) {
58- debugLog ( 'trust' , `Warning: Error deleting existing cert: ${ error } ` , options ?. verbose )
59+ debugLog ( LOG_CATEGORIES . TRUST , `Warning: Error deleting existing cert: ${ error } ` , options ?. verbose )
5960 console . warn ( `Error deleting existing cert: ${ error } ` )
6061 }
6162
62- debugLog ( 'trust' , `Adding new certificate to ${ folder } ` , options ?. verbose )
63+ debugLog ( LOG_CATEGORIES . TRUST , `Adding new certificate to ${ folder } ` , options ?. verbose )
6364 await runCommand ( `certutil -d sql:${ folder } -A -t ${ args } -n ${ config . commonName } -i ${ caCertPath } ` )
6465
6566 log . info ( `Cert added to ${ folder } ` )
@@ -82,25 +83,25 @@ const trustStoreHandlers: Record<string, TrustStoreHandler> = {
8283 * @returns The path to the stored certificate
8384 */
8485export async function addCertToSystemTrustStoreAndSaveCert ( cert : Cert , caCert : string , options ?: TlsOption ) : Promise < CertPath > {
85- debugLog ( 'trust' , `Adding certificate to system trust store with options: ${ JSON . stringify ( options ) } ` , options ?. verbose )
86- debugLog ( 'trust' , 'Storing certificate and private key' , options ?. verbose )
86+ debugLog ( LOG_CATEGORIES . TRUST , `Adding certificate to system trust store with options: ${ JSON . stringify ( options ) } ` , options ?. verbose )
87+ debugLog ( LOG_CATEGORIES . TRUST , 'Storing certificate and private key' , options ?. verbose )
8788 const certPath = storeCertificate ( cert , options )
8889
89- debugLog ( 'trust' , 'Storing CA certificate' , options ?. verbose )
90+ debugLog ( LOG_CATEGORIES . TRUST , 'Storing CA certificate' , options ?. verbose )
9091 const caCertPath = storeCACertificate ( caCert , options )
9192
9293 const platform = os . platform ( )
93- debugLog ( 'trust' , `Detected platform: ${ platform } ` , options ?. verbose )
94+ debugLog ( LOG_CATEGORIES . TRUST , `Detected platform: ${ platform } ` , options ?. verbose )
9495
9596 const handler = trustStoreHandlers [ platform ]
9697 if ( ! handler ) {
9798 const errorMsg = `Unsupported platform: ${ platform } `
98- debugLog ( 'trust' , `Error: ${ errorMsg } ` , options ?. verbose )
99+ debugLog ( LOG_CATEGORIES . TRUST , `Error: ${ errorMsg } ` , options ?. verbose )
99100 throw new Error ( errorMsg )
100101 }
101102
102103 await handler . addCertificate ( caCertPath , options )
103104
104- debugLog ( 'trust' , 'Certificate successfully added to system trust store' , options ?. verbose )
105+ debugLog ( LOG_CATEGORIES . TRUST , 'Certificate successfully added to system trust store' , options ?. verbose )
105106 return certPath
106107}
0 commit comments