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

56 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-05-09 20:20:14 +00:00
const AWS = require("aws-sdk");
const s3 = new AWS.S3({ apiVersion: "2006-03-01" });
2019-05-08 23:01:23 +00:00
const pathHelper = require("../pathHelper");
const fileNames = require("../fileNames");
2019-05-08 16:12:22 +00:00
2019-05-09 21:18:59 +00:00
const getObjects = (opts, options) => {
2019-05-09 20:36:29 +00:00
return [
2019-05-09 19:51:41 +00:00
{
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
}
2019-05-09 20:36:29 +00:00
];
};
2019-05-08 16:12:22 +00:00
2019-05-09 21:18:59 +00:00
const getPromises = (objects, options) => {
2019-05-09 20:36:29 +00:00
let promises = [];
2019-05-08 16:12:22 +00:00
2019-05-09 19:51:41 +00:00
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);
2019-05-08 16:12:22 +00:00
}).catch((err) => {
2019-05-09 19:51:41 +00:00
console.error("There was an error setting:", obj.Key);
2019-05-08 16:12:22 +00:00
throw err;
2019-05-09 19:51:41 +00:00
});
promises.push(promise);
}
2019-05-09 20:36:29 +00:00
return promises;
};
module.exports.set = (opts, options) => {
console.log("certificates.set for ", opts.subject);
2019-05-09 21:18:59 +00:00
const objects = getObjects(opts, options);
const promises = getPromises(objects, options);
2019-05-09 20:36:29 +00:00
2019-05-09 19:51:41 +00:00
return Promise.all(promises).then((values) => {
2019-05-08 16:12:22 +00:00
return null;
}).catch((err) => {
2019-05-08 23:01:23 +00:00
console.error("There was an error setting the certificates:", err.message);
2019-05-08 16:12:22 +00:00
throw err;
});
2019-05-09 07:36:47 +00:00
};