Compare commits

..

11 Commits

2 changed files with 65 additions and 38 deletions

View File

@ -1,12 +1,29 @@
'use strict'; 'use strict';
/* global Promise */
var PromiseA = require('bluebird'); var PromiseA;
var mkdirpAsync = PromiseA.promisify(require('mkdirp')); try {
PromiseA = require('bluebird');
} catch(e) {
PromiseA = Promise;
}
var util = require('util');
function promisifyAll(obj) {
var aobj = {};
Object.keys(obj).forEach(function (key) {
aobj[key + 'Async'] = util.promisify(obj[key]);
});
return aobj;
}
var mkdirpAsync = util.promisify(require('mkdirp'));
var path = require('path'); var path = require('path');
var fs = PromiseA.promisifyAll(require('fs')); var fs = require('fs');
var readFileAsync = util.promisify(fs.readFile);
var readdirAsync = util.promisify(fs.readdir);
var writeFileAsync = util.promisify(fs.writeFile);
var statAsync = util.promisify(fs.stat);
var sfs = require('safe-replace'); var sfs = require('safe-replace');
var os = require('os'); var os = require('os');
var symlink = require('fs-symlink');
function log(debug) { function log(debug) {
if (debug) { if (debug) {
@ -21,7 +38,7 @@ function writeRenewalConfig(args) {
var pyobj = args.pyobj; var pyobj = args.pyobj;
pyobj.checkpoints = parseInt(pyobj.checkpoints, 10) || 0; pyobj.checkpoints = parseInt(pyobj.checkpoints, 10) || 0;
var pyconf = PromiseA.promisifyAll(require('pyconf')); var pyconf = promisifyAll(require('pyconf'));
var liveDir = args.liveDir || path.join(args.configDir, 'live', args.domains[0]); var liveDir = args.liveDir || path.join(args.configDir, 'live', args.domains[0]);
@ -101,10 +118,12 @@ function pyToJson(pyobj) {
return jsobj; return jsobj;
} }
var crypto = require('crypto');
var rnd = crypto.randomBytes(8).toString('hex');
var defaults = { var defaults = {
configDir: [ os.homedir(), 'letsencrypt', 'etc' ].join(path.sep) // /etc/letsencrypt/ configDir: [ os.homedir(), 'letsencrypt', 'etc' ].join(path.sep) // /etc/letsencrypt/
, logsDir: [ os.homedir(), 'tmp', 'acme', 'log' ].join(path.sep) // /var/log/letsencrypt/ , logsDir: [ os.tmpdir(), 'acme-' + rnd, 'log' ].join(path.sep) // /var/log/letsencrypt/
, webrootPath: [ os.homedir(), 'tmp', 'acme-challenge' ].join(path.sep) , webrootPath: [ os.tmpdir(), 'acme-' + rnd, 'acme-challenge' ].join(path.sep)
, accountsDir: [ ':configDir', 'accounts', ':serverDir' ].join(path.sep) , accountsDir: [ ':configDir', 'accounts', ':serverDir' ].join(path.sep)
, renewalPath: [ ':configDir', 'renewal', ':hostname.conf' ].join(path.sep) , renewalPath: [ ':configDir', 'renewal', ':hostname.conf' ].join(path.sep)
@ -117,6 +136,7 @@ var defaults = {
, fullchainPath: [ ':configDir', 'live', ':hostname', 'fullchain.pem' ].join(path.sep) , fullchainPath: [ ':configDir', 'live', ':hostname', 'fullchain.pem' ].join(path.sep)
, certPath: [ ':configDir', 'live', ':hostname', 'cert.pem' ].join(path.sep) , certPath: [ ':configDir', 'live', ':hostname', 'cert.pem' ].join(path.sep)
, chainPath: [ ':configDir', 'live', ':hostname', 'chain.pem' ].join(path.sep) , chainPath: [ ':configDir', 'live', ':hostname', 'chain.pem' ].join(path.sep)
, bundlePath: [ ':configDir', 'live', ':hostname', 'bundle.pem' ].join(path.sep)
, rsaKeySize: 2048 , rsaKeySize: 2048
}; };
@ -149,7 +169,7 @@ module.exports.create = function (configs) {
if (!keypath) { if (!keypath) {
return null; return null;
} }
return fs.readFileAsync(keypath, 'ascii').then(function (key) { return readFileAsync(keypath, 'ascii').then(function (key) {
if ('jwk' === format) { if ('jwk' === format) {
return { privateKeyJwk: JSON.parse(key) }; return { privateKeyJwk: JSON.parse(key) };
} }
@ -175,7 +195,7 @@ module.exports.create = function (configs) {
key = keypair.privateKeyPem; key = keypair.privateKeyPem;
} }
return fs.writeFileAsync(keypath, key, 'ascii').then(function () { return writeFileAsync(keypath, key, 'ascii').then(function () {
return keypair; return keypair;
}); });
}); });
@ -204,15 +224,15 @@ module.exports.create = function (configs) {
return PromiseA.reject(new Error("missing one or more of privkeyPath, fullchainPath, certPath, chainPath from options")); return PromiseA.reject(new Error("missing one or more of privkeyPath, fullchainPath, certPath, chainPath from options"));
} }
//, fs.readFileAsync(fullchainPath, 'ascii') //, readFileAsync(fullchainPath, 'ascii')
// note: if this ^^ gets added back in, the arrays below must change // note: if this ^^ gets added back in, the arrays below must change
return PromiseA.all([ return PromiseA.all([
fs.readFileAsync(args.privkeyPath, 'ascii') // 0 readFileAsync(args.privkeyPath, 'ascii') // 0
, fs.readFileAsync(args.certPath, 'ascii') // 1 , readFileAsync(args.certPath, 'ascii') // 1
, fs.readFileAsync(args.chainPath, 'ascii') // 2 , readFileAsync(args.chainPath, 'ascii') // 2
// stat the file, not the link // stat the file, not the link
, fs.statAsync(args.certPath) // 3 , statAsync(args.certPath) // 3
]).then(function (arr) { ]).then(function (arr) {
return { return {
privkey: arr[0] // privkey.pem privkey: arr[0] // privkey.pem
@ -244,9 +264,8 @@ module.exports.create = function (configs) {
var certPath = args.certPath || pyobj.cert || path.join(liveDir, 'cert.pem'); var certPath = args.certPath || pyobj.cert || path.join(liveDir, 'cert.pem');
var fullchainPath = args.fullchainPath || pyobj.fullchain || path.join(liveDir, 'fullchain.pem'); var fullchainPath = args.fullchainPath || pyobj.fullchain || path.join(liveDir, 'fullchain.pem');
var chainPath = args.chainPath || pyobj.chain || path.join(liveDir, 'chain.pem'); var chainPath = args.chainPath || pyobj.chain || path.join(liveDir, 'chain.pem');
var privkeyPath = args.privkeyPath || pyobj.privkey var privkeyPath = args.privkeyPath || pyobj.privkey || args.domainKeyPath || path.join(liveDir, 'privkey.pem');
|| args.domainKeyPath var bundlePath = args.bundlePath || pyobj.bundle || path.join(liveDir, 'bundle.pem');
|| path.join(liveDir, 'privkey.pem');
var archiveDir = args.archiveDir || path.join(args.configDir, 'archive', args.domains[0]); var archiveDir = args.archiveDir || path.join(args.configDir, 'archive', args.domains[0]);
@ -255,22 +274,27 @@ module.exports.create = function (configs) {
var fullchainArchive = path.join(archiveDir, 'fullchain' + checkpoints + '.pem'); var fullchainArchive = path.join(archiveDir, 'fullchain' + checkpoints + '.pem');
var chainArchive = path.join(archiveDir, 'chain'+ checkpoints + '.pem'); var chainArchive = path.join(archiveDir, 'chain'+ checkpoints + '.pem');
var privkeyArchive = path.join(archiveDir, 'privkey' + checkpoints + '.pem'); var privkeyArchive = path.join(archiveDir, 'privkey' + checkpoints + '.pem');
var bundleArchive = path.join(archiveDir, 'bundle' + checkpoints + '.pem');
return mkdirpAsync(archiveDir).then(function () { return mkdirpAsync(archiveDir).then(function () {
return PromiseA.all([ return PromiseA.all([
sfs.writeFileAsync(certArchive, pems.cert, 'ascii') sfs.writeFileAsync(certArchive, pems.cert, 'ascii')
, sfs.writeFileAsync(chainArchive, pems.chain, 'ascii') , sfs.writeFileAsync(chainArchive, pems.chain, 'ascii')
, sfs.writeFileAsync(fullchainArchive, pems.cert + pems.chain, 'ascii') , sfs.writeFileAsync(fullchainArchive, [ pems.cert, pems.chain ].join('\n'), 'ascii')
, sfs.writeFileAsync(privkeyArchive, pems.privkey, 'ascii') , sfs.writeFileAsync(privkeyArchive, pems.privkey, 'ascii')
, sfs.writeFileAsync(bundleArchive, pems.bundle, 'ascii')
]); ]);
}).then(function () { }).then(function () {
return mkdirpAsync(liveDir); return mkdirpAsync(liveDir);
}).then(function () { }).then(function () {
return PromiseA.all([ return PromiseA.all([
symlink(certArchive, certPath) sfs.writeFileAsync(certPath, pems.cert, 'ascii')
, symlink(chainArchive, chainPath) , sfs.writeFileAsync(chainPath, pems.chain, 'ascii')
, symlink(fullchainArchive, fullchainPath) // Most platforms need these two
, symlink(privkeyArchive, privkeyPath) , sfs.writeFileAsync(fullchainPath, [ pems.cert, pems.chain ].join('\n'), 'ascii')
, sfs.writeFileAsync(privkeyPath, pems.privkey, 'ascii')
// HAProxy needs "bundle.pem" aka "combined.pem"
, sfs.writeFileAsync(bundlePath, [ pems.privkey, pems.cert, pems.chain ].join('\n'), 'ascii')
]); ]);
}).then(function () { }).then(function () {
pyobj.checkpoints += 1; pyobj.checkpoints += 1;
@ -284,6 +308,8 @@ module.exports.create = function (configs) {
privkey: pems.privkey privkey: pems.privkey
, cert: pems.cert , cert: pems.cert
, chain: pems.chain , chain: pems.chain
, expires: pems.expires
, identifiers: pems.identifiers
/* /*
// TODO populate these only if they are actually known // TODO populate these only if they are actually known
@ -328,11 +354,11 @@ module.exports.create = function (configs) {
log(args.debug, "No email given"); log(args.debug, "No email given");
return PromiseA.resolve(null); return PromiseA.resolve(null);
} }
return fs.readdirAsync(args.accountsDir).then(function (nodes) { return readdirAsync(args.accountsDir).then(function (nodes) {
log(args.debug, "success reading arg.accountsDir"); log(args.debug, "success reading arg.accountsDir");
return PromiseA.all(nodes.map(function (node) { return PromiseA.all(nodes.map(function (node) {
return fs.readFileAsync(path.join(args.accountsDir, node, 'regr.json'), 'utf8').then(function (text) { return readFileAsync(path.join(args.accountsDir, node, 'regr.json'), 'utf8').then(function (text) {
var regr = JSON.parse(text); var regr = JSON.parse(text);
regr.__accountId = node; regr.__accountId = node;
@ -373,8 +399,8 @@ module.exports.create = function (configs) {
// Accounts // Accounts
, _getAccountIdByPublicKey: function (keypair) { , _getAccountIdByPublicKey: function (keypair) {
// we use insecure md5 - even though we know it's bad - because that's how the python client did // we use insecure md5 - even though we know it's bad - because that's how the python client did
const pubkey = keypair.publicKeyPem.replace(/\r/g, ''); var pubkey = keypair.publicKeyPem.replace(/\r/g, '');
return require('crypto').createHash('md5').update(pubkey).digest('hex'); return crypto.createHash('md5').update(pubkey).digest('hex');
} }
// Accounts // Accounts
, checkKeypairAsync: function (args) { , checkKeypairAsync: function (args) {
@ -430,7 +456,7 @@ module.exports.create = function (configs) {
return PromiseA.all(configs.map(function (filename) { return PromiseA.all(configs.map(function (filename) {
var keyname = filename.slice(0, -5); var keyname = filename.slice(0, -5);
return fs.readFileAsync(path.join(accountDir, filename), 'utf8').then(function (text) { return readFileAsync(path.join(accountDir, filename), 'utf8').then(function (text) {
var data; var data;
try { try {
@ -498,9 +524,9 @@ module.exports.create = function (configs) {
// TODO abstract file writing // TODO abstract file writing
return PromiseA.all([ return PromiseA.all([
// meta.json {"creation_host": "ns1.redirect-www.org", "creation_dt": "2015-12-11T04:14:38Z"} // meta.json {"creation_host": "ns1.redirect-www.org", "creation_dt": "2015-12-11T04:14:38Z"}
fs.writeFileAsync(path.join(accountDir, 'meta.json'), JSON.stringify(accountMeta), 'utf8') writeFileAsync(path.join(accountDir, 'meta.json'), JSON.stringify(accountMeta), 'utf8')
// private_key.json { "e", "d", "n", "q", "p", "kty", "qi", "dp", "dq" } // private_key.json { "e", "d", "n", "q", "p", "kty", "qi", "dp", "dq" }
, fs.writeFileAsync(path.join(accountDir, 'private_key.json'), JSON.stringify(reg.keypair.privateKeyJwk), 'utf8') , writeFileAsync(path.join(accountDir, 'private_key.json'), JSON.stringify(reg.keypair.privateKeyJwk), 'utf8')
// regr.json: // regr.json:
/* /*
{ body: { contact: [ 'mailto:coolaj86@gmail.com' ], { body: { contact: [ 'mailto:coolaj86@gmail.com' ],
@ -510,7 +536,7 @@ module.exports.create = function (configs) {
new_authzr_uri: 'https://acme-v01.api.letsencrypt.org/acme/new-authz', new_authzr_uri: 'https://acme-v01.api.letsencrypt.org/acme/new-authz',
terms_of_service: 'https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf' } terms_of_service: 'https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf' }
*/ */
, fs.writeFileAsync(path.join(accountDir, 'regr.json'), , writeFileAsync(path.join(accountDir, 'regr.json'),
JSON.stringify(regrBody), JSON.stringify(regrBody),
'utf8') 'utf8')
]); ]);
@ -526,7 +552,7 @@ module.exports.create = function (configs) {
} }
// Accounts // Accounts
, getAccountIdAsync: function (args) { , getAccountIdAsync: function (args) {
var pyconf = PromiseA.promisifyAll(require('pyconf')); var pyconf = promisifyAll(require('pyconf'));
return pyconf.readFileAsync(args.renewalPath).then(function (renewal) { return pyconf.readFileAsync(args.renewalPath).then(function (renewal) {
var accountId = renewal.account; var accountId = renewal.account;
@ -562,7 +588,7 @@ module.exports.create = function (configs) {
} }
// Configs // Configs
, _checkHelperAsync: function (args) { , _checkHelperAsync: function (args) {
var pyconf = PromiseA.promisifyAll(require('pyconf')); var pyconf = promisifyAll(require('pyconf'));
return pyconf.readFileAsync(args.renewalPath).then(function (pyobj) { return pyconf.readFileAsync(args.renewalPath).then(function (pyobj) {
return pyobj; return pyobj;
@ -616,7 +642,7 @@ module.exports.create = function (configs) {
, allAsync: function (copy) { , allAsync: function (copy) {
copy.domains = []; copy.domains = [];
return fs.readdirAsync(copy.renewalDir).then(function (nodes) { return readdirAsync(copy.renewalDir).then(function (nodes) {
nodes = nodes.filter(function (node) { nodes = nodes.filter(function (node) {
return /^[a-z0-9]+.*\.conf$/.test(node); return /^[a-z0-9]+.*\.conf$/.test(node);
}); });

View File

@ -1,6 +1,6 @@
{ {
"name": "le-store-certbot", "name": "le-store-certbot",
"version": "2.1.2", "version": "2.2.0",
"description": "The \"certbot\" storage strategy for Greenlock.js", "description": "The \"certbot\" storage strategy for Greenlock.js",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
@ -23,11 +23,12 @@
"url": "https://git.coolaj86.com/coolaj86/le-store-certbot.js/issues" "url": "https://git.coolaj86.com/coolaj86/le-store-certbot.js/issues"
}, },
"homepage": "https://git.coolaj86.com/coolaj86/le-store-certbot.js", "homepage": "https://git.coolaj86.com/coolaj86/le-store-certbot.js",
"trulyOptionalDependencies": {
"bluebird": "^3.5.1"
},
"dependencies": { "dependencies": {
"bluebird": "^3.5.1",
"fs-symlink": "^1.2.1",
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",
"pyconf": "^1.1.2", "pyconf": "^1.1.5",
"safe-replace": "^1.0.2" "safe-replace": "^1.0.3"
} }
} }