From 2f293626935b277f060de4599fb8acd7effb1db1 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 11 Nov 2019 22:38:32 -0700 Subject: [PATCH] wip: cleanup --- bin/tmpl/server.tmpl.js | 2 +- challenges-underlay.js | 97 ------------------- greenlock.js | 16 ++- lib/challenges-wrapper.js | 88 +++++++++++++++++ manager-underlay.js => lib/manager-wrapper.js | 4 +- 5 files changed, 104 insertions(+), 103 deletions(-) delete mode 100644 challenges-underlay.js create mode 100644 lib/challenges-wrapper.js rename manager-underlay.js => lib/manager-wrapper.js (99%) diff --git a/bin/tmpl/server.tmpl.js b/bin/tmpl/server.tmpl.js index 3dcc225..3a04849 100644 --- a/bin/tmpl/server.tmpl.js +++ b/bin/tmpl/server.tmpl.js @@ -3,7 +3,7 @@ require('greenlock-express') .init(function() { return { - greenlock: require('./greenlock.js'), + packageRoot: __dirname, // whether or not to run at cloudscale cluster: false diff --git a/challenges-underlay.js b/challenges-underlay.js deleted file mode 100644 index 423aea3..0000000 --- a/challenges-underlay.js +++ /dev/null @@ -1,97 +0,0 @@ -'use strict'; - -var Greenlock = require('./'); - -module.exports.wrap = function(greenlock) { - greenlock.challenges = {}; - greenlock.challenges.get = function(chall) { - // TODO pick one and warn on the others - // (just here due to some backwards compat issues with early v3 plugins) - var servername = - chall.servername || - chall.altname || - (chall.identifier && chall.identifier.value); - - // TODO some sort of caching to prevent database hits? - return greenlock - ._config({ servername: servername }) - .then(function(site) { - if (!site) { - return null; - } - - // Hmm... this _should_ be impossible - if (!site.challenges || !site.challenges['http-01']) { - var copy = JSON.parse(JSON.stringify(site)); - sanitizeCopiedConf(copy); - sanitizeCopiedConf(copy.store); - if (site.challenges) { - sanitizeCopiedConf(copy.challenges['http-01']); - sanitizeCopiedConf(copy.challenges['dns-01']); - sanitizeCopiedConf(copy.challenges['tls-alpn-01']); - } - console.warn('[Bug] Please report this error:'); - console.warn( - '\terror: http-01 challenge requested, but not even a default http-01 config exists' - ); - console.warn('\tservername:', JSON.stringify(servername)); - console.warn('\tsite:', JSON.stringify(copy)); - return null; - } - - return Greenlock._loadChallenge(site.challenges, 'http-01'); - }) - .then(function(plugin) { - if (!plugin) { - return null; - } - return plugin - .get({ - challenge: { - type: chall.type, - //hostname: chall.servername, - altname: chall.servername, - identifier: { value: chall.servername }, - token: chall.token - } - }) - .then(function(result) { - var keyAuth; - var keyAuthDigest; - if (result) { - // backwards compat that shouldn't be dropped - // because new v3 modules had to do this to be - // backwards compatible with Greenlock v2.7 at - // the time. - if (result.challenge) { - result = result.challenge; - } - keyAuth = result.keyAuthorization; - keyAuthDigest = result.keyAuthorizationDigest; - } - if (/dns/.test(chall.type)) { - return { - keyAuthorizationDigest: keyAuthDigest - }; - } - return { - keyAuthorization: keyAuth - }; - }); - }); - }; -}; - -function sanitizeCopiedConf(copy) { - if (!copy) { - return; - } - - Object.keys(copy).forEach(function(k) { - if (/(api|key|token)/i.test(k) && 'string' === typeof copy[k]) { - copy[k] = '**redacted**'; - } - }); - - return copy; -} diff --git a/greenlock.js b/greenlock.js index 48b3435..b8664ca 100644 --- a/greenlock.js +++ b/greenlock.js @@ -12,7 +12,10 @@ var E = require('./errors.js'); var P = require('./plugins.js'); var A = require('./accounts.js'); var C = require('./certificates.js'); + var DIR = require('./lib/directory-url.js'); +var ChWrapper = require('./lib/challenges-wrapper.js'); +var MngWrapper = require('./lib/manager-wrapper.js'); var UserEvents = require('./user-events.js'); var GreenlockRc = require('./greenlockrc.js'); @@ -61,6 +64,13 @@ G.create = function(gconf) { var rc = GreenlockRc.resolve(gconf); gconf = Object.assign(rc, gconf); + // OK: /path/to/blah + // OK: npm-name-blah + // NOT OK: ./rel/path/to/blah + if ('.' === (gconf.manager || '')[0]) { + gconf.manager = gconf.packageRoot + '/' + gconf.manager; + } + // Wraps each of the following with appropriate error checking // greenlock.manager.defaults // greenlock.sites.add @@ -68,7 +78,7 @@ G.create = function(gconf) { // greenlock.sites.remove // greenlock.sites.find // greenlock.sites.get - require('./manager-underlay.js').wrap(greenlock, gconf); + MngWrapper.wrap(greenlock, gconf); // The goal here is to reduce boilerplate, such as error checking // and duration parsing, that a manager must implement greenlock.sites.add = greenlock.add = greenlock.manager.add; @@ -78,9 +88,9 @@ G.create = function(gconf) { // Exports challenges.get for Greenlock Express HTTP-01, // and whatever odd use case pops up, I suppose // greenlock.challenges.get - require('./challenges-underlay.js').wrap(greenlock); + ChWrapper.wrap(greenlock); - DIR._getStagingDirectoryUrl('', gconf.staging); + DIR._getDefaultDirectoryUrl('', gconf.staging); if (gconf.directoryUrl) { gdefaults.directoryUrl = gconf.directoryUrl; } diff --git a/lib/challenges-wrapper.js b/lib/challenges-wrapper.js new file mode 100644 index 0000000..b5df78d --- /dev/null +++ b/lib/challenges-wrapper.js @@ -0,0 +1,88 @@ +'use strict'; + +var Greenlock = require('../'); + +module.exports.wrap = function(greenlock) { + greenlock.challenges = {}; + greenlock.challenges.get = async function(chall) { + // TODO pick one and warn on the others + // (just here due to some backwards compat issues with early v3 plugins) + var servername = + chall.servername || + chall.altname || + (chall.identifier && chall.identifier.value); + + // TODO some sort of caching to prevent database hits? + var site = await greenlock._config({ servername: servername }); + if (!site) { + return null; + } + + // Hmm... this _should_ be impossible + if (!site.challenges || !site.challenges['http-01']) { + var copy = JSON.parse(JSON.stringify(site)); + sanitizeCopiedConf(copy); + sanitizeCopiedConf(copy.store); + if (site.challenges) { + sanitizeCopiedConf(copy.challenges['http-01']); + sanitizeCopiedConf(copy.challenges['dns-01']); + sanitizeCopiedConf(copy.challenges['tls-alpn-01']); + } + console.warn('[Bug] Please report this error:'); + console.warn( + '\terror: http-01 challenge requested, but not even a default http-01 config exists' + ); + console.warn('\tservername:', JSON.stringify(servername)); + console.warn('\tsite:', JSON.stringify(copy)); + return null; + } + + var plugin = await Greenlock._loadChallenge(site.challenges, 'http-01'); + if (!plugin) { + return null; + } + + var keyAuth; + var keyAuthDigest; + var result = await plugin.get({ + challenge: { + type: chall.type, + //hostname: chall.servername, + altname: chall.servername, + identifier: { value: chall.servername }, + token: chall.token + } + }); + if (result) { + // backwards compat that shouldn't be dropped + // because new v3 modules had to do this to be + // backwards compatible with Greenlock v2.7 at + // the time. + if (result.challenge) { + result = result.challenge; + } + keyAuth = result.keyAuthorization; + keyAuthDigest = result.keyAuthorizationDigest; + } + + if (/dns/.test(chall.type)) { + return { keyAuthorizationDigest: keyAuthDigest }; + } + + return { keyAuthorization: keyAuth }; + }; +}; + +function sanitizeCopiedConf(copy) { + if (!copy) { + return; + } + + Object.keys(copy).forEach(function(k) { + if (/(api|key|token)/i.test(k) && 'string' === typeof copy[k]) { + copy[k] = '**redacted**'; + } + }); + + return copy; +} diff --git a/manager-underlay.js b/lib/manager-wrapper.js similarity index 99% rename from manager-underlay.js rename to lib/manager-wrapper.js index 0325622..a5e3e94 100644 --- a/manager-underlay.js +++ b/lib/manager-wrapper.js @@ -1,7 +1,7 @@ 'use strict'; -var U = require('./utils.js'); -var E = require('./errors.js'); +var U = require('../utils.js'); +var E = require('../errors.js'); var warned = {};