Refactoring

This commit is contained in:
Cyrille 2019-05-09 21:20:14 +01:00
parent b484efb75e
commit f79677846c
13 changed files with 83 additions and 55 deletions

View File

@ -1,9 +1,6 @@
var path = require("path");
var Promise = require("bluebird");
const AWS = require("aws-sdk");
AWS.config.setPromisesDependency(Promise);
const defaultOptions = {
accessKeyId: null
, secretAccessKey: null
@ -13,8 +10,6 @@ const defaultOptions = {
, configDir: "acme/"
};
const s3 = new AWS.S3({ apiVersion: "2006-03-01" });
const pathHelper = require("./lib/pathHelper");
const fileNames = require("./lib/fileNames");
@ -22,49 +17,12 @@ module.exports.create = (createOptions) => {
const options = Object.assign({}, defaultOptions, createOptions);
if (!options.debug) {
console = console || {};
console.log = () => { };
console.error = () => { };
}
AWS.config.update({
region: options.bucketRegion
, credentials: new AWS.Credentials({
accessKeyId: options.accessKeyId
, secretAccessKey: options.secretAccessKey
})
});
require('./lib/debug')(options.debug);
require('./lib/aws')(options);
const handlers = {
certificates: {
check: (opts) => {
return require("./lib/certificates/check").check(opts, options, s3);
},
checkKeypair: (opts) => {
return require("./lib/certificates/checkKeypair").checkKeypair(opts, options, s3);
},
setKeypair: (opts) => {
return require("./lib/certificates/setKeypair").setKeypair(opts, options, s3);
},
set: (opts) => {
return require("./lib/certificates/set").set(opts, options, s3);
}
},
accounts: {
check: (opts) => {
return require("./lib/accounts/check").check(opts, options, s3);
},
checkKeypair: (opts) => {
return require("./lib/accounts/checkKeypair").checkKeypair(opts, options, s3);
},
setKeypair: (opts) => {
return require("./lib/accounts/setKeypair").setKeypair(opts, options, s3);
},
set: (opts) => {
return require("./lib/accounts/set").set(opts, options, s3);
}
}
certificates: require("./lib/certificates")(options)
, accounts: require("./lib/accounts")(options)
};
return handlers;

18
lib/accounts.js Normal file
View File

@ -0,0 +1,18 @@
module.exports = (options) => {
const handlers = {
check: (opts) => {
return require("./accounts/check").check(opts, options);
},
checkKeypair: (opts) => {
return require("./accounts/checkKeypair").checkKeypair(opts, options);
},
setKeypair: (opts) => {
return require("./accounts/setKeypair").setKeypair(opts, options);
},
set: (opts) => {
return require("./accounts/set").set(opts, options);
}
};
return handlers;
};

View File

@ -1,6 +1,6 @@
const pathHelper = require("../pathHelper");
const fileNames = require("../fileNames");
module.exports.check = (opts, options, s3) => {
module.exports.check = (opts, options) => {
console.log("accounts.check", opts.account.id);
};

View File

@ -1,7 +1,9 @@
const AWS = require("aws-sdk");
const s3 = new AWS.S3({ apiVersion: "2006-03-01" });
const pathHelper = require("../pathHelper");
const fileNames = require("../fileNames");
module.exports.checkKeypair = (opts, options, s3) => {
module.exports.checkKeypair = (opts, options) => {
let id = opts.account.id || opts.email || "single-user";
console.log("accounts.checkKeypair for", id);

View File

@ -1,6 +1,8 @@
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, s3) => {
module.exports.set = (opts, options) => {
console.log("accounts.set");
};

View File

@ -1,7 +1,9 @@
const AWS = require("aws-sdk");
const s3 = new AWS.S3({ apiVersion: "2006-03-01" });
const pathHelper = require("../pathHelper");
const fileNames = require("../fileNames");
module.exports.setKeypair = (opts, options, s3) => {
module.exports.setKeypair = (opts, options) => {
console.log("accounts.setKeypair for", opts.account);
let id = opts.account.id || opts.email || "single-user";

12
lib/aws.js Normal file
View File

@ -0,0 +1,12 @@
const AWS = require("aws-sdk");
AWS.config.setPromisesDependency(Promise);
module.exports = (options) => {
AWS.config.update({
region: options.bucketRegion
, credentials: new AWS.Credentials({
accessKeyId: options.accessKeyId
, secretAccessKey: options.secretAccessKey
})
});
}

19
lib/certificates.js Normal file
View File

@ -0,0 +1,19 @@
module.exports = (options) => {
const handlers = {
check: (opts) => {
return require("./certificates/check").check(opts, options);
},
checkKeypair: (opts) => {
return require("./certificates/checkKeypair").checkKeypair(opts, options);
},
setKeypair: (opts) => {
return require("./certificates/setKeypair").setKeypair(opts, options);
},
set: (opts) => {
return require("./certificates/set").set(opts, options);
}
};
return handlers;
};

View File

@ -1,7 +1,9 @@
const AWS = require("aws-sdk");
const s3 = new AWS.S3({ apiVersion: "2006-03-01" });
const pathHelper = require("../pathHelper");
const fileNames = require("../fileNames");
module.exports.check = (opts, options, s3) => {
module.exports.check = (opts, options) => {
var id = opts.certificate && opts.certificate.id || opts.subject;
console.log("certificates.check for", opts.subject);

View File

@ -1,7 +1,9 @@
const AWS = require("aws-sdk");
const s3 = new AWS.S3({ apiVersion: "2006-03-01" });
const pathHelper = require("../pathHelper");
const fileNames = require("../fileNames");
module.exports.checkKeypair = (opts, options, s3) => {
module.exports.checkKeypair = (opts, options) => {
console.log("certificates.checkKeypair for", opts.subject);
let id = opts.certificate.kid || opts.certificate.id || opts.subject;

View File

@ -1,7 +1,9 @@
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, s3) => {
module.exports.set = (opts, options) => {
console.log("certificates.set for ", opts.subject);
let objects = [

View File

@ -1,12 +1,14 @@
const AWS = require("aws-sdk");
const s3 = new AWS.S3({ apiVersion: "2006-03-01" });
const pathHelper = require("../pathHelper");
const fileNames = require("../fileNames");
module.exports.setKeypair = (opts, options, s3) => {
module.exports.setKeypair = (opts, options) => {
let id = opts.certificate.kid || opts.certificate.id || opts.subject;
console.log("certificates.setKeypair for", id);
let pemKeyPath = pathHelper.certificatesPath(options, id, fileNames.privkey.pem);
let jwkKeyPath = pathHelper.certificatesPath(options, id, fileNames.privkey.jwk);
// let jwkKeyPath = pathHelper.certificatesPath(options, id, fileNames.privkey.jwk);
return s3.putObject({ Key: pemKeyPath, Body: opts.keypair.privateKeyPem, Bucket: options.bucketName }).promise().then((data) => {
console.log("Successfully set the PEM privateKey.");

7
lib/debug.js Normal file
View File

@ -0,0 +1,7 @@
module.exports = (debug) => {
if (!debug) {
console = console || {};
console.log = () => { };
console.error = () => { };
}
}