From b484efb75eb73a791a13fe837241d1fc29c955ed Mon Sep 17 00:00:00 2001 From: Cyrille Date: Thu, 9 May 2019 21:01:39 +0100 Subject: [PATCH] Refactored certificates/checks --- lib/certificates/check.js | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/lib/certificates/check.js b/lib/certificates/check.js index 869f9a5..7c11488 100644 --- a/lib/certificates/check.js +++ b/lib/certificates/check.js @@ -5,33 +5,27 @@ module.exports.check = (opts, options, s3) => { var id = opts.certificate && opts.certificate.id || opts.subject; console.log("certificates.check for", opts.subject); - var privkeyPath = pathHelper.certificatesPath(options, id, fileNames.privkey.pem); - var certPath = pathHelper.certificatesPath(options, id, fileNames.cert); - var chainPath = pathHelper.certificatesPath(options, id, fileNames.chain); + let paths = [ + pathHelper.certificatesPath(options, id, fileNames.privkey.pem) + , pathHelper.certificatesPath(options, id, fileNames.cert) + , pathHelper.certificatesPath(options, id, fileNames.chain) + ] - return Promise.all([ - s3.getObject({ Key: privkeyPath, Bucket: options.bucketName }).promise().then((data) => { - console.log("Successfully retrieved certificate privkey.pem"); + var promises = []; + + for (let i = 0; i < paths.length; i++) { + const key = paths[i]; + const promise = s3.getObject({ Key: key, Bucket: options.bucketName }).promise().then((data) => { + console.log("Successfully retrieved certificate", key); return data.Body.toString(); }).catch((err) => { - console.error("There was an error retrieving your certificate privkey.pem:", err.message); - throw err; - }), - s3.getObject({ Key: certPath, Bucket: options.bucketName }).promise().then((data) => { - console.log("Successfully retrieved certificate cert.pem"); - return data.Body.toString(); - }).catch((err) => { - console.error("There was an error retrieving your certificate cert.pem:", err.message); - throw err; - }), - s3.getObject({ Key: chainPath, Bucket: options.bucketName }).promise().then((data) => { - console.log("Successfully retrieved certificate chain.pem"); - return data.Body.toString(); - }).catch((err) => { - console.error("There was an error retrieving your certificate chain.pem:", err.message); + console.error("There was an error retrieving your certificate", key); throw err; }) - ]).then((values) => { + promises.push(promise); + } + + return Promise.all(promises).then((values) => { return { privkey: values[0] , cert: values[1]