From f79677846cba1aa04d57d995666dcd6e93b9e038 Mon Sep 17 00:00:00 2001 From: Cyrille Date: Thu, 9 May 2019 21:20:14 +0100 Subject: [PATCH] Refactoring --- index.js | 50 +++----------------------------- lib/accounts.js | 18 ++++++++++++ lib/accounts/check.js | 2 +- lib/accounts/checkKeypair.js | 4 ++- lib/accounts/set.js | 4 ++- lib/accounts/setKeypair.js | 4 ++- lib/aws.js | 12 ++++++++ lib/certificates.js | 19 ++++++++++++ lib/certificates/check.js | 4 ++- lib/certificates/checkKeypair.js | 4 ++- lib/certificates/set.js | 4 ++- lib/certificates/setKeypair.js | 6 ++-- lib/debug.js | 7 +++++ 13 files changed, 83 insertions(+), 55 deletions(-) create mode 100644 lib/accounts.js create mode 100644 lib/aws.js create mode 100644 lib/certificates.js create mode 100644 lib/debug.js diff --git a/index.js b/index.js index e01d4b5..4eeec40 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/lib/accounts.js b/lib/accounts.js new file mode 100644 index 0000000..c546bf7 --- /dev/null +++ b/lib/accounts.js @@ -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; +}; \ No newline at end of file diff --git a/lib/accounts/check.js b/lib/accounts/check.js index cd37f84..df8b167 100644 --- a/lib/accounts/check.js +++ b/lib/accounts/check.js @@ -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); }; \ No newline at end of file diff --git a/lib/accounts/checkKeypair.js b/lib/accounts/checkKeypair.js index 804d23c..c515f6c 100644 --- a/lib/accounts/checkKeypair.js +++ b/lib/accounts/checkKeypair.js @@ -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); diff --git a/lib/accounts/set.js b/lib/accounts/set.js index 116e436..b35fb45 100644 --- a/lib/accounts/set.js +++ b/lib/accounts/set.js @@ -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"); }; \ No newline at end of file diff --git a/lib/accounts/setKeypair.js b/lib/accounts/setKeypair.js index 1e67e94..c7c0a8d 100644 --- a/lib/accounts/setKeypair.js +++ b/lib/accounts/setKeypair.js @@ -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"; diff --git a/lib/aws.js b/lib/aws.js new file mode 100644 index 0000000..76bcac2 --- /dev/null +++ b/lib/aws.js @@ -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 + }) + }); +} \ No newline at end of file diff --git a/lib/certificates.js b/lib/certificates.js new file mode 100644 index 0000000..1ddb553 --- /dev/null +++ b/lib/certificates.js @@ -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; +}; \ No newline at end of file diff --git a/lib/certificates/check.js b/lib/certificates/check.js index 7c11488..0c239ec 100644 --- a/lib/certificates/check.js +++ b/lib/certificates/check.js @@ -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); diff --git a/lib/certificates/checkKeypair.js b/lib/certificates/checkKeypair.js index 823e3e2..bb4a845 100644 --- a/lib/certificates/checkKeypair.js +++ b/lib/certificates/checkKeypair.js @@ -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; diff --git a/lib/certificates/set.js b/lib/certificates/set.js index 0adb031..f94eb0f 100644 --- a/lib/certificates/set.js +++ b/lib/certificates/set.js @@ -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 = [ diff --git a/lib/certificates/setKeypair.js b/lib/certificates/setKeypair.js index 98751c1..8a2273c 100644 --- a/lib/certificates/setKeypair.js +++ b/lib/certificates/setKeypair.js @@ -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."); diff --git a/lib/debug.js b/lib/debug.js new file mode 100644 index 0000000..635ab19 --- /dev/null +++ b/lib/debug.js @@ -0,0 +1,7 @@ +module.exports = (debug) => { + if (!debug) { + console = console || {}; + console.log = () => { }; + console.error = () => { }; + } +} \ No newline at end of file