deferenced symlinks and downloaded the certs from live directory

This commit is contained in:
Rahul Somasundaram 2021-09-01 11:41:27 +05:30
parent 658acd147c
commit 32089ea272
No known key found for this signature in database
GPG Key ID: B8520CB207DD49D3

View File

@ -14,6 +14,7 @@ const letsencryptStaging = process.env.NODE_ENV !== 'production';
const letsencryptConfig = '/etc/letsencrypt.ini'; const letsencryptConfig = '/etc/letsencrypt.ini';
const certbotCommand = 'certbot'; const certbotCommand = 'certbot';
const archiver = require('archiver'); const archiver = require('archiver');
const path = require('path');
function omissions() { function omissions() {
return ['is_deleted']; return ['is_deleted'];
@ -350,22 +351,25 @@ const internalCertificate = {
}) })
.then((certificate) => { .then((certificate) => {
if (certificate.provider === 'letsencrypt') { if (certificate.provider === 'letsencrypt') {
const zipDirectory = '/etc/letsencrypt/archive/npm-' + data.id; const zipDirectory = '/etc/letsencrypt/live/npm-' + data.id;
if (!fs.existsSync(zipDirectory)) { if (!fs.existsSync(zipDirectory)) {
throw new error.ItemNotFoundError('Certificate ' + certificate.nice_name + ' does not exists'); throw new error.ItemNotFoundError('Certificate ' + certificate.nice_name + ' does not exists');
} }
let certFiles = fs.readdirSync(zipDirectory)
.filter((fn) => fn.endsWith('.pem'))
.map((fn) => fs.realpathSync(path.join(zipDirectory, fn)));
const downloadName = 'npm-' + data.id + '-' + `${Date.now()}.zip`; const downloadName = 'npm-' + data.id + '-' + `${Date.now()}.zip`;
const opName = '/tmp/' + downloadName; const opName = '/tmp/' + downloadName;
internalCertificate.zipDirectory(zipDirectory, opName) internalCertificate.zipFiles(certFiles, opName)
.then(() => { .then(() => {
logger.debug('zip completed : ', opName); logger.debug('zip completed : ', opName);
const resp = { const resp = {
fileName: opName fileName: opName
}; };
resolve(resp); resolve(resp);
}); }).catch((err) => reject(err));
} else { } else {
throw new error.ValidationError('Only Let\'sEncrypt certificates can be downloaded'); throw new error.ValidationError('Only Let\'sEncrypt certificates can be downloaded');
} }
@ -378,13 +382,18 @@ const internalCertificate = {
* @param {String} out * @param {String} out
* @returns {Promise} * @returns {Promise}
*/ */
zipDirectory(source, out) { zipFiles(source, out) {
const archive = archiver('zip', { zlib: { level: 9 } }); const archive = archiver('zip', { zlib: { level: 9 } });
const stream = fs.createWriteStream(out); const stream = fs.createWriteStream(out);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
source
.map((fl) => {
let fileName = path.basename(fl);
logger.debug(fileName, ' added to certificate download zip');
archive.file(fl, { name: fileName });
});
archive archive
.directory(source, false)
.on('error', (err) => reject(err)) .on('error', (err) => reject(err))
.pipe(stream); .pipe(stream);