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

40 lines
1.4 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 20:20:14 +00:00
module.exports.check = (opts, options) => {
2019-05-09 21:13:04 +00:00
const id = opts.certificate && opts.certificate.id || opts.subject;
2019-05-08 23:01:23 +00:00
console.log("certificates.check for", opts.subject);
2019-05-08 16:12:22 +00:00
2019-05-09 20:01:39 +00:00
let paths = [
pathHelper.certificatesPath(options, id, fileNames.privkey.pem)
, pathHelper.certificatesPath(options, id, fileNames.cert)
, pathHelper.certificatesPath(options, id, fileNames.chain)
]
2019-05-08 16:12:22 +00:00
2019-05-09 20:01:39 +00:00
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);
2019-05-08 16:12:22 +00:00
return data.Body.toString();
}).catch((err) => {
2019-05-09 20:01:39 +00:00
console.error("There was an error retrieving your certificate", key);
2019-05-08 16:12:22 +00:00
throw err;
})
2019-05-09 20:01:39 +00:00
promises.push(promise);
}
return Promise.all(promises).then((values) => {
2019-05-08 16:12:22 +00:00
return {
privkey: values[0]
, cert: values[1]
, chain: values[2]
2019-05-08 23:12:15 +00:00
};
2019-05-08 16:12:22 +00:00
}).catch((err) => {
2019-05-08 23:01:23 +00:00
console.error("There was an error checking the ceritifcates:", err.message);
2019-05-08 16:12:22 +00:00
return null;
});
2019-05-08 23:01:23 +00:00
};