Skip to content

Commit e5a3b5e

Browse files
committed
added endpoint to download certificates
1 parent 5e9ff4d commit e5a3b5e

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

backend/internal/certificate.js

+45
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const internalHost = require('./host');
1313
const letsencryptStaging = process.env.NODE_ENV !== 'production';
1414
const letsencryptConfig = '/etc/letsencrypt.ini';
1515
const certbotCommand = 'certbot';
16+
const archiver = require('archiver');
1617

1718
function omissions() {
1819
return ['is_deleted'];
@@ -335,6 +336,50 @@ const internalCertificate = {
335336
});
336337
},
337338

339+
/**
340+
* @param {Object} data
341+
* @param {Number} data.id
342+
* @returns {Promise}
343+
*/
344+
download: (data) => {
345+
const downloadName = "npm-" + data.id + "-" + `${Date.now()}.zip`;
346+
const opName = '/tmp/' + downloadName;
347+
const zipDirectory = "/etc/letsencrypt/live/npm-" + data.id
348+
349+
return new Promise((resolve, reject) => {
350+
internalCertificate.zipDirectory(zipDirectory, opName)
351+
.then(() => {
352+
logger.debug("zip completed : ", opName)
353+
const resp = {
354+
fileName: opName
355+
}
356+
resolve(resp)
357+
}).catch(err => {
358+
reject(err)
359+
})
360+
});
361+
},
362+
363+
/**
364+
* @param {String} source
365+
* @param {String} out
366+
* @returns {Promise}
367+
*/
368+
zipDirectory(source, out) {
369+
const archive = archiver('zip', { zlib: { level: 9 } });
370+
const stream = fs.createWriteStream(out);
371+
372+
return new Promise((resolve, reject) => {
373+
archive
374+
.directory(source, false)
375+
.on('error', err => reject(err))
376+
.pipe(stream);
377+
378+
stream.on('close', () => resolve());
379+
archive.finalize();
380+
});
381+
},
382+
338383
/**
339384
* @param {Access} access
340385
* @param {Object} data

backend/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "js/index.js",
66
"dependencies": {
77
"ajv": "^6.12.0",
8+
"archiver": "^5.3.0",
89
"batchflow": "^0.4.0",
910
"bcrypt": "^5.0.0",
1011
"body-parser": "^1.19.0",

backend/routes/api/nginx/certificates.js

+29
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,35 @@ router
209209
.catch(next);
210210
});
211211

212+
213+
/**
214+
* Download LE Certs
215+
*
216+
* /api/nginx/certificates/123/download
217+
*/
218+
router
219+
.route('/:certificate_id/download')
220+
.options((req, res) => {
221+
res.sendStatus(204);
222+
})
223+
.all(jwtdecode())
224+
225+
/**
226+
* POST /api/nginx/certificates/123/download
227+
*
228+
* Renew certificate
229+
*/
230+
.get((req, res, next) => {
231+
internalCertificate.download({
232+
id: parseInt(req.params.certificate_id, 10)
233+
})
234+
.then((result) => {
235+
res.status(200)
236+
.download(result.fileName);
237+
})
238+
.catch(next);
239+
});
240+
212241
/**
213242
* Validate Certs before saving
214243
*

0 commit comments

Comments
 (0)