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

49 lines
1.6 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:16:52 +00:00
const getPaths = (opts, options, id) => {
2019-05-09 21:14:51 +00:00
return [
2019-05-09 20:01:39 +00:00
pathHelper.certificatesPath(options, id, fileNames.privkey.pem)
, pathHelper.certificatesPath(options, id, fileNames.cert)
, pathHelper.certificatesPath(options, id, fileNames.chain)
]
2019-05-09 21:14:51 +00:00
};
2019-05-09 21:16:52 +00:00
const getPromises = (options, paths) => {
let promises = [];
2019-05-09 20:01:39 +00:00
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);
}
2019-05-09 21:16:52 +00:00
return promises;
}
module.exports.check = (opts, options) => {
const id = opts.certificate && opts.certificate.id || opts.subject;
console.log("certificates.check for", opts.subject);
const paths = getPaths(opts, options, id);
const promises = getPromises(options, paths);
2019-05-09 20:01:39 +00:00
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-09 21:18:09 +00:00
console.error("There was an error checking the certificates:", err.message);
2019-05-08 16:12:22 +00:00
return null;
});
2019-05-08 23:01:23 +00:00
};