diff --git a/lib/certificates/set.js b/lib/certificates/set.js index 8e28def..0adb031 100644 --- a/lib/certificates/set.js +++ b/lib/certificates/set.js @@ -4,46 +4,39 @@ const fileNames = require("../fileNames"); module.exports.set = (opts, options, s3) => { console.log("certificates.set for ", opts.subject); - var pems = { - cert: opts.pems.cert - , chain: opts.pems.chain - , privkey: opts.pems.privkey + let objects = [ + { + Key: pathHelper.certificatesPath(options, opts.subject, fileNames.cert) + , Body: opts.pems.cert + } + , { + Key: pathHelper.certificatesPath(options, opts.subject, fileNames.chain) + , Body: opts.pems.chain + } + , { + Key: pathHelper.certificatesPath(options, opts.subject, fileNames.fullchain) + , Body: [opts.pems.cert, opts.pems.chain].join("\n") // for Apache, Nginx, etc + } + , { + Key: pathHelper.certificatesPath(options, opts.subject, fileNames.bundle) + , Body: [opts.pems.privkey, opts.pems.cert, opts.pems.chain].join("\n") // for HAProxy + } + ] + + var promises = []; + + for (let i = 0; i < objects.length; i++) { + const obj = objects[i]; + const promise = s3.putObject({ Key: obj.Key, Body: obj.Body, Bucket: options.bucketName }).promise().then((data) => { + console.log("Successfully set", obj.Key); + }).catch((err) => { + console.error("There was an error setting:", obj.Key); + throw err; + }); + promises.push(promise); } - var certPath = pathHelper.certificatesPath(options, opts.subject, fileNames.cert); - var chainPath = pathHelper.certificatesPath(options, opts.subject, fileNames.chain); - var fullchainPath = pathHelper.certificatesPath(options, opts.subject, fileNames.fullchain); - var bundlePath = pathHelper.certificatesPath(options, opts.subject, fileNames.bundle); - - var fullchainPem = [pems.cert, pems.chain].join("\n"); // for Apache, Nginx, etc - var bundlePem = [pems.privkey, pems.cert, pems.chain].join("\n"); // for HAProxy - - return Promise.all([ - s3.putObject({ Key: certPath, Body: pems.cert, Bucket: options.bucketName }).promise().then((data) => { - console.log("Successfully set", certPath); - }).catch((err) => { - console.error("There was an error setting cert.pem:", err.message); - throw err; - }), - s3.putObject({ Key: chainPath, Body: pems.chain, Bucket: options.bucketName }).promise().then((data) => { - console.log("Successfully set", chainPath); - }).catch((err) => { - console.error("There was an error setting chain.pem:", err.message); - throw err; - }), - s3.putObject({ Key: fullchainPath, Body: fullchainPem, Bucket: options.bucketName }).promise().then((data) => { - console.log("Successfully set", fullchainPath); - }).catch((err) => { - console.error("There was an error setting fullchain.pem:", err.message); - throw err; - }), - s3.putObject({ Key: bundlePath, Body: bundlePem, Bucket: options.bucketName }).promise().then((data) => { - console.log("Successfully set", bundlePath); - }).catch((err) => { - console.error("There was an error setting bundle.pem:", err.message); - throw err; - }) - ]).then((values) => { + return Promise.all(promises).then((values) => { return null; }).catch((err) => { console.error("There was an error setting the certificates:", err.message);