|
| 1 | +const path = require("path"); |
| 2 | +const fs = require("fs"); |
| 3 | + |
| 4 | +const keyNameRegex = /^cert(?:ificate)?\.key$|^key\.pem$/i; |
| 5 | +const certNameRegex = /^cert(?:ificate)?\.(?:crt|pem)$/i; |
| 6 | + |
| 7 | +const assetsDir = path.join(__dirname, 'assets'); |
| 8 | + |
| 9 | +function findCertificates() { |
| 10 | + const certDir = path.join(assetsDir, 'cert'); |
| 11 | + const candidates = fs.readdirSync(certDir); |
| 12 | + const keyNames = candidates.filter(name => keyNameRegex.test(name)); |
| 13 | + const certNames = candidates.filter(name => certNameRegex.test(name)); |
| 14 | + |
| 15 | + const errors = []; |
| 16 | + for (const [a, b] of [[keyNames, 'key'], [certNames, 'certificate']]) { |
| 17 | + if (a.length === 0) { |
| 18 | + errors.push(`No ${b} file.`); |
| 19 | + } else if (a.length > 1) { |
| 20 | + errors.push(`Multiple ${b} files.`); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + if (keyNames.length === 0 && certNames.length === 0) { |
| 25 | + return {noCertFiles: true, badCertFiles: false, certDir, keyFilename: null, certFilename: null, errors} |
| 26 | + } |
| 27 | + if (errors.length > 0) { |
| 28 | + return {noCertFiles: false, badCertFiles: true, certDir, keyFilename: null, certFilename: null, errors} |
| 29 | + } |
| 30 | + |
| 31 | + const keyFilename = path.join(certDir, keyNames[0]); |
| 32 | + const certFilename = path.join(certDir, certNames[0]); |
| 33 | + return {keyFilename, certFilename, noCertFiles: false, badCertFiles: false, certDir, errors}; |
| 34 | +} |
| 35 | + |
| 36 | +function loadCertificates() { |
| 37 | + const result = findCertificates(); |
| 38 | + if (result.noCertFiles) { |
| 39 | + throw new Error(`No certificate files found in '${result.certDir}'.`); |
| 40 | + } |
| 41 | + if (result.badCertFiles) { |
| 42 | + throw new Error(`Ambiguous or incomplete certificate files found in '${result.certDir}'. ${result.errors.join(' ')}`); |
| 43 | + } |
| 44 | + const [privateKey, certificate] = [ |
| 45 | + fs.readFileSync(result.keyFilename, 'utf8'), |
| 46 | + fs.readFileSync(result.certFilename, 'utf8'), |
| 47 | + ]; |
| 48 | + return {privateKey, certificate}; |
| 49 | +} |
| 50 | + |
| 51 | +function createCertificates() { |
| 52 | + const result = findCertificates(); |
| 53 | + if (result.badCertFiles) { |
| 54 | + const msg = `Ambiguous or incomplete certificate files found in '${result.certDir}'. ${result.errors.join(' ')}`; |
| 55 | + console.error(msg); |
| 56 | + console.error('Throwing error because the built app will likely fail.') |
| 57 | + throw new Error(msg); |
| 58 | + } |
| 59 | + if (!result.noCertFiles) { |
| 60 | + console.log('INFO Certificate files already present. NOT creating certificates.'); |
| 61 | + return; |
| 62 | + } |
| 63 | + console.log('INFO Creating self signed certificate.'); |
| 64 | + const selfsigned = require('selfsigned'); |
| 65 | + const pems = selfsigned.generate([ |
| 66 | + { shortName: 'ST', value: 'Texas' }, |
| 67 | + { name: 'countryName', value: 'US' }, |
| 68 | + { name: 'localityName', value: 'Austin' }, |
| 69 | + { name: 'organizationName', value: 'Unchained' }, |
| 70 | + { name: 'commonName', value: 'GPG-Bridge' }, |
| 71 | + ], {}); |
| 72 | + |
| 73 | + const certDir = path.join(assetsDir, 'cert'); |
| 74 | + fs.writeFileSync(path.join(certDir, 'key.pem'), pems.private); |
| 75 | + fs.writeFileSync(path.join(certDir, 'cert.pem'), pems.cert); |
| 76 | +} |
| 77 | + |
| 78 | +module.exports = {loadCertificates, createCertificates}; |
| 79 | + |
0 commit comments