gl-store-s3.js/lib/certificates/set.js

47 lines
1.6 KiB
JavaScript

const AWS = require("aws-sdk");
const s3 = new AWS.S3({ apiVersion: "2006-03-01" });
const pathHelper = require("../pathHelper");
const fileNames = require("../fileNames");
module.exports.set = (opts, options) => {
console.log("certificates.set for ", opts.subject);
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);
}
return Promise.all(promises).then((values) => {
return null;
}).catch((err) => {
console.error("There was an error setting the certificates:", err.message);
throw err;
});
};