From 371e95fd2265122113125e060c5c2bdd483c7ed5 Mon Sep 17 00:00:00 2001 From: Cyrille Date: Wed, 8 May 2019 17:12:22 +0100 Subject: [PATCH] refactoring --- index.js | 188 ++--------------------------- lib/accounts/check.js | 6 + lib/accounts/checkKeypair.js | 21 ++++ lib/accounts/set.js | 6 + lib/accounts/setKeypair.js | 22 ++++ lib/certificates/check.js | 44 +++++++ lib/certificates/checkKeypair.js | 21 ++++ lib/certificates/set.js | 52 ++++++++ lib/certificates/setKeypair.js | 18 +++ lib/fileNames.js | 10 ++ pathHelper.js => lib/pathHelper.js | 0 lib/s3.js | 0 12 files changed, 210 insertions(+), 178 deletions(-) create mode 100644 lib/accounts/check.js create mode 100644 lib/accounts/checkKeypair.js create mode 100644 lib/accounts/set.js create mode 100644 lib/accounts/setKeypair.js create mode 100644 lib/certificates/check.js create mode 100644 lib/certificates/checkKeypair.js create mode 100644 lib/certificates/set.js create mode 100644 lib/certificates/setKeypair.js create mode 100644 lib/fileNames.js rename pathHelper.js => lib/pathHelper.js (100%) create mode 100644 lib/s3.js diff --git a/index.js b/index.js index 7761768..2074a47 100644 --- a/index.js +++ b/index.js @@ -1,23 +1,3 @@ - -//////////////////////////// -// File names for support // -//////////////////////////// - -const fileNames = { - privkey: { - pem: 'privkey.pem' - , jwk: 'privkey.jwk' - } - , cert: 'cert.pem' - , chain: 'chain.pem' - , fullchain: 'fullchain.pem' - , bundle: 'bundle.pem' -} - -/////////////////////////// -// Setup the environment // -/////////////////////////// - var path = require('path'); var Promise = require('bluebird') @@ -35,7 +15,8 @@ const defaultOptions = { const s3 = new AWS.S3({ apiVersion: '2006-03-01' }); -const pathHelper = require('./pathHelper'); +const pathHelper = require('./lib/pathHelper'); +const fileNames = require('./lib/fileNames'); module.exports.create = (createOptions) => { @@ -58,179 +39,30 @@ module.exports.create = (createOptions) => { const handlers = { certificates: { check: (opts) => { - - var id = opts.certificate && opts.certificate.id || opts.subject; - console.log('certificates.check for', opts.subject); - - var privkeyPath = pathHelper.certificatesPath(options, id, fileNames.privkey.pem); - var certPath = pathHelper.certificatesPath(options, id, fileNames.cert); - var chainPath = pathHelper.certificatesPath(options, id, fileNames.chain); - - return Promise.all([ - s3.getObject({ Key: privkeyPath, Bucket: options.bucketName }).promise().then((data) => { - console.log('Successfully retrieved certificate privkey.pem'); - return data.Body.toString(); - }).catch((err) => { - console.error('There was an error retrieving your certificate privkey.pem:', err.message); - throw err; - }), - s3.getObject({ Key: certPath, Bucket: options.bucketName }).promise().then((data) => { - console.log('Successfully retrieved certificate cert.pem'); - return data.Body.toString(); - }).catch((err) => { - console.error('There was an error retrieving your certificate cert.pem:', err.message); - throw err; - }), - s3.getObject({ Key: chainPath, Bucket: options.bucketName }).promise().then((data) => { - console.log('Successfully retrieved certificate chain.pem'); - return data.Body.toString(); - }).catch((err) => { - console.error('There was an error retrieving your certificate chain.pem:', err.message); - throw err; - }) - ]).then((values) => { - // console.log('Promise.all(values):', values); - - return { - privkey: values[0] - , cert: values[1] - , chain: values[2] - } - }).catch((err) => { - console.error('There was an error checking the ceritifcates:', err.message); - return null; - }); + return require('./lib/certificates/check').check(opts, options, s3); }, checkKeypair: (opts) => { - console.log('certificates.checkKeypair for', opts.subject); - - id = opts.certificate.kid || opts.certificate.id || opts.subject; - - pemKeyPath = pathHelper.certificatesPath(options, id, fileNames.privkey.pem); - jwkKeyPath = pathHelper.certificatesPath(options, id, fileNames.privkey.jwk); - - return s3.getObject({ Key: pemKeyPath, Bucket: options.bucketName }).promise().then((data) => { - console.log('Successfully retrieved certificate PEM keypair.'); - return { - privateKeyPem: data.Body.toString() - } - }).catch((err) => { - console.error('There was an error retrieving your certificate PEM keypair:', err.message); - return null - }); - + return require('./lib/certificates/checkKeypair').checkKeypair(opts, options, s3); }, setKeypair: (opts) => { - id = opts.certificate.kid || opts.certificate.id || opts.subject; - console.log('certificates.setKeypair for', id); - - pemKeyPath = pathHelper.certificatesPath(options, id, fileNames.privkey.pem); - 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.'); - return null; - }).catch((err) => { - console.error('There was an error setting your PEM privateKey:', err.message); - throw err; - }); + return require('./lib/certificates/setKeypair').setKeypair(opts, options, s3); }, set: (opts) => { - console.log('certificates.set for ', opts.subject); - - var pems = { - cert: opts.pems.cert - , chain: opts.pems.chain - , privkey: opts.pems.privkey - } - - var certPath = pathHelper.certificatesPath(options, opts.subject, fileNames.cert); - var chainPath = pathHelper.certificatesPath(options, opts.subject, fileNames.chain); - var fullchainPath = pathHelper.certificatesPath(options, opts.subject, fileNames.fullchain); - var bundlePath = pathHelper.certificatesPath(options, opts.subject, fileNames.bundle); - - var fullchainPem = [pems.cert, pems.chain].join('\n'); // for Apache, Nginx, etc - var bundlePem = [pems.privkey, pems.cert, pems.chain].join('\n'); // for HAProxy - - return Promise.all([ - s3.putObject({ Key: certPath, Body: pems.cert, Bucket: options.bucketName }).promise().then((data) => { - console.log('Successfully set', certPath); - }).catch((err) => { - console.error('There was an error setting cert.pem:', err.message); - throw err; - }), - s3.putObject({ Key: chainPath, Body: pems.chain, Bucket: options.bucketName }).promise().then((data) => { - console.log('Successfully set', chainPath); - }).catch((err) => { - console.error('There was an error setting chain.pem:', err.message); - throw err; - }), - s3.putObject({ Key: fullchainPath, Body: fullchainPem, Bucket: options.bucketName }).promise().then((data) => { - console.log('Successfully set', fullchainPath); - }).catch((err) => { - console.error('There was an error setting fullchain.pem:', err.message); - throw err; - }), - s3.putObject({ Key: bundlePath, Body: bundlePem, Bucket: options.bucketName }).promise().then((data) => { - console.log('Successfully set', bundlePath); - }).catch((err) => { - console.error('There was an error setting bundle.pem:', err.message); - throw err; - }) - ]).then((values) => { - return null; - }).catch((err) => { - console.error('There was an error setting the certificates:', err.message); - throw err; - }); + return require('./lib/certificates/set').set(opts, options, s3); } }, accounts: { check: (opts) => { - console.log("accounts.check", opts.account.id); - // console.log(opts); + return require('./lib/accounts/check').check(opts, options, s3); }, checkKeypair: (opts) => { - var id = opts.account.id || opts.email || 'single-user'; - console.log('accounts.checkKeypair for', id); - - key = pathHelper.accountsPath(options, id) - - return s3.getObject({ Key: key, Bucket: options.bucketName }).promise().then((data) => { - console.log('Successfully retrieved account keypair.'); - var keypair = JSON.parse(data.Body.toString()); - return { - privateKeyPem: keypair.privateKeyPem // string PEM private key - , privateKeyJwk: keypair.privateKeyJwk // object JWK private key - }; - }).catch((err) => { - console.error('There was an error retrieving your account keypair:', err.message); - return null; - }); - + return require('./lib/accounts/checkKeypair').checkKeypair(opts, options, s3); }, setKeypair: (opts) => { - console.log('accounts.setKeypair for', opts.account); - - var id = opts.account.id || opts.email || 'single-user'; - key = pathHelper.accountsPath(options, id) - - var body = JSON.stringify({ - privateKeyPem: opts.keypair.privateKeyPem // string PEM - , privateKeyJwk: opts.keypair.privateKeyJwk // object JWK - }); - - return s3.putObject({ Key: key, Body: body, Bucket: options.bucketName }).promise().then((data) => { - console.log('Successfully created account keypair.'); - return null; - }).catch((err) => { - console.error('There was an error creating account keypair:', err.message); - return null; - }); - + return require('./lib/accounts/setKeypair').setKeypair(opts, options, s3); }, set: (opts) => { - console.log("accounts.set"); + return require('./lib/accounts/set').set(opts, options, s3); } } } diff --git a/lib/accounts/check.js b/lib/accounts/check.js new file mode 100644 index 0000000..24a3abe --- /dev/null +++ b/lib/accounts/check.js @@ -0,0 +1,6 @@ +const pathHelper = require('../pathHelper'); +const fileNames = require('../fileNames'); + +module.exports.check = (opts, options, s3) => { + 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 new file mode 100644 index 0000000..4188d29 --- /dev/null +++ b/lib/accounts/checkKeypair.js @@ -0,0 +1,21 @@ +const pathHelper = require('../pathHelper'); +const fileNames = require('../fileNames'); + +module.exports.checkKeypair = (opts, options, s3) => { + var id = opts.account.id || opts.email || 'single-user'; + console.log('accounts.checkKeypair for', id); + + key = pathHelper.accountsPath(options, id) + + return s3.getObject({ Key: key, Bucket: options.bucketName }).promise().then((data) => { + console.log('Successfully retrieved account keypair.'); + var keypair = JSON.parse(data.Body.toString()); + return { + privateKeyPem: keypair.privateKeyPem // string PEM private key + , privateKeyJwk: keypair.privateKeyJwk // object JWK private key + }; + }).catch((err) => { + console.error('There was an error retrieving your account keypair:', err.message); + return null; + }); +} \ No newline at end of file diff --git a/lib/accounts/set.js b/lib/accounts/set.js new file mode 100644 index 0000000..e1e9580 --- /dev/null +++ b/lib/accounts/set.js @@ -0,0 +1,6 @@ +const pathHelper = require('../pathHelper'); +const fileNames = require('../fileNames'); + +module.exports.set = (opts, options, s3) => { + console.log("accounts.set"); +} \ No newline at end of file diff --git a/lib/accounts/setKeypair.js b/lib/accounts/setKeypair.js new file mode 100644 index 0000000..a3c6edf --- /dev/null +++ b/lib/accounts/setKeypair.js @@ -0,0 +1,22 @@ +const pathHelper = require('../pathHelper'); +const fileNames = require('../fileNames'); + +module.exports.setKeypair = (opts, options, s3) => { + console.log('accounts.setKeypair for', opts.account); + + var id = opts.account.id || opts.email || 'single-user'; + key = pathHelper.accountsPath(options, id) + + var body = JSON.stringify({ + privateKeyPem: opts.keypair.privateKeyPem // string PEM + , privateKeyJwk: opts.keypair.privateKeyJwk // object JWK + }); + + return s3.putObject({ Key: key, Body: body, Bucket: options.bucketName }).promise().then((data) => { + console.log('Successfully created account keypair.'); + return null; + }).catch((err) => { + console.error('There was an error creating account keypair:', err.message); + return null; + }); +} \ No newline at end of file diff --git a/lib/certificates/check.js b/lib/certificates/check.js new file mode 100644 index 0000000..76e9bbe --- /dev/null +++ b/lib/certificates/check.js @@ -0,0 +1,44 @@ +const pathHelper = require('../pathHelper'); +const fileNames = require('../fileNames'); + +module.exports.check = (opts, options, s3) => { + var id = opts.certificate && opts.certificate.id || opts.subject; + console.log('certificates.check for', opts.subject); + + var privkeyPath = pathHelper.certificatesPath(options, id, fileNames.privkey.pem); + var certPath = pathHelper.certificatesPath(options, id, fileNames.cert); + var chainPath = pathHelper.certificatesPath(options, id, fileNames.chain); + + return Promise.all([ + s3.getObject({ Key: privkeyPath, Bucket: options.bucketName }).promise().then((data) => { + console.log('Successfully retrieved certificate privkey.pem'); + return data.Body.toString(); + }).catch((err) => { + console.error('There was an error retrieving your certificate privkey.pem:', err.message); + throw err; + }), + s3.getObject({ Key: certPath, Bucket: options.bucketName }).promise().then((data) => { + console.log('Successfully retrieved certificate cert.pem'); + return data.Body.toString(); + }).catch((err) => { + console.error('There was an error retrieving your certificate cert.pem:', err.message); + throw err; + }), + s3.getObject({ Key: chainPath, Bucket: options.bucketName }).promise().then((data) => { + console.log('Successfully retrieved certificate chain.pem'); + return data.Body.toString(); + }).catch((err) => { + console.error('There was an error retrieving your certificate chain.pem:', err.message); + throw err; + }) + ]).then((values) => { + return { + privkey: values[0] + , cert: values[1] + , chain: values[2] + } + }).catch((err) => { + console.error('There was an error checking the ceritifcates:', err.message); + return null; + }); +} \ No newline at end of file diff --git a/lib/certificates/checkKeypair.js b/lib/certificates/checkKeypair.js new file mode 100644 index 0000000..bf0ee29 --- /dev/null +++ b/lib/certificates/checkKeypair.js @@ -0,0 +1,21 @@ +const pathHelper = require('../pathHelper'); +const fileNames = require('../fileNames'); + +module.exports.checkKeypair = (opts, options, s3) => { + console.log('certificates.checkKeypair for', opts.subject); + + id = opts.certificate.kid || opts.certificate.id || opts.subject; + + pemKeyPath = pathHelper.certificatesPath(options, id, fileNames.privkey.pem); + jwkKeyPath = pathHelper.certificatesPath(options, id, fileNames.privkey.jwk); + + return s3.getObject({ Key: pemKeyPath, Bucket: options.bucketName }).promise().then((data) => { + console.log('Successfully retrieved certificate PEM keypair.'); + return { + privateKeyPem: data.Body.toString() + } + }).catch((err) => { + console.error('There was an error retrieving your certificate PEM keypair:', err.message); + return null; + }); +} \ No newline at end of file diff --git a/lib/certificates/set.js b/lib/certificates/set.js new file mode 100644 index 0000000..ff3fe86 --- /dev/null +++ b/lib/certificates/set.js @@ -0,0 +1,52 @@ +const pathHelper = require('../pathHelper'); +const fileNames = require('../fileNames'); + +module.exports.set = (opts, options, s3) => { + console.log('certificates.set for ', opts.subject); + + var pems = { + cert: opts.pems.cert + , chain: opts.pems.chain + , privkey: opts.pems.privkey + } + + var certPath = pathHelper.certificatesPath(options, opts.subject, fileNames.cert); + var chainPath = pathHelper.certificatesPath(options, opts.subject, fileNames.chain); + var fullchainPath = pathHelper.certificatesPath(options, opts.subject, fileNames.fullchain); + var bundlePath = pathHelper.certificatesPath(options, opts.subject, fileNames.bundle); + + var fullchainPem = [pems.cert, pems.chain].join('\n'); // for Apache, Nginx, etc + var bundlePem = [pems.privkey, pems.cert, pems.chain].join('\n'); // for HAProxy + + return Promise.all([ + s3.putObject({ Key: certPath, Body: pems.cert, Bucket: options.bucketName }).promise().then((data) => { + console.log('Successfully set', certPath); + }).catch((err) => { + console.error('There was an error setting cert.pem:', err.message); + throw err; + }), + s3.putObject({ Key: chainPath, Body: pems.chain, Bucket: options.bucketName }).promise().then((data) => { + console.log('Successfully set', chainPath); + }).catch((err) => { + console.error('There was an error setting chain.pem:', err.message); + throw err; + }), + s3.putObject({ Key: fullchainPath, Body: fullchainPem, Bucket: options.bucketName }).promise().then((data) => { + console.log('Successfully set', fullchainPath); + }).catch((err) => { + console.error('There was an error setting fullchain.pem:', err.message); + throw err; + }), + s3.putObject({ Key: bundlePath, Body: bundlePem, Bucket: options.bucketName }).promise().then((data) => { + console.log('Successfully set', bundlePath); + }).catch((err) => { + console.error('There was an error setting bundle.pem:', err.message); + throw err; + }) + ]).then((values) => { + return null; + }).catch((err) => { + console.error('There was an error setting the certificates:', err.message); + throw err; + }); +} \ No newline at end of file diff --git a/lib/certificates/setKeypair.js b/lib/certificates/setKeypair.js new file mode 100644 index 0000000..a4fd4cd --- /dev/null +++ b/lib/certificates/setKeypair.js @@ -0,0 +1,18 @@ +const pathHelper = require('../pathHelper'); +const fileNames = require('../fileNames'); + +module.exports.setKeypair = (opts, options, s3) => { + id = opts.certificate.kid || opts.certificate.id || opts.subject; + console.log('certificates.setKeypair for', id); + + pemKeyPath = pathHelper.certificatesPath(options, id, fileNames.privkey.pem); + 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.'); + return null; + }).catch((err) => { + console.error('There was an error setting your PEM privateKey:', err.message); + throw err; + }); +} \ No newline at end of file diff --git a/lib/fileNames.js b/lib/fileNames.js new file mode 100644 index 0000000..2f0f420 --- /dev/null +++ b/lib/fileNames.js @@ -0,0 +1,10 @@ +module.exports = { + privkey: { + pem: 'privkey.pem' + , jwk: 'privkey.jwk' + } + , cert: 'cert.pem' + , chain: 'chain.pem' + , fullchain: 'fullchain.pem' + , bundle: 'bundle.pem' +} \ No newline at end of file diff --git a/pathHelper.js b/lib/pathHelper.js similarity index 100% rename from pathHelper.js rename to lib/pathHelper.js diff --git a/lib/s3.js b/lib/s3.js new file mode 100644 index 0000000..e69de29