greenlock.js-ARCHIVED/lib/letiny-core.js

369 lines
13 KiB
JavaScript
Raw Normal View History

2015-12-15 11:37:39 +00:00
'use strict';
var PromiseA = require('bluebird');
2015-12-16 10:07:00 +00:00
var mkdirpAsync = PromiseA.promisify(require('mkdirp'));
2015-12-15 11:37:39 +00:00
var path = require('path');
var fs = PromiseA.promisifyAll(require('fs'));
2015-12-16 10:07:00 +00:00
var sfs = require('safe-replace');
2015-12-15 15:21:27 +00:00
var LE = require('../');
2015-12-16 09:11:31 +00:00
var LeCore = PromiseA.promisifyAll(require('letiny-core'));
2015-12-16 09:25:39 +00:00
var leCrypto = PromiseA.promisifyAll(LeCore.leCrypto);
2015-12-16 09:11:31 +00:00
2015-12-15 15:21:27 +00:00
var fetchFromConfigLiveDir = require('./common').fetchFromDisk;
2015-12-15 11:37:39 +00:00
var ipc = {}; // in-process cache
function getAcmeUrls(args) {
var now = Date.now();
// TODO check response header on request for cache time
if ((now - ipc.acmeUrlsUpdatedAt) < 10 * 60 * 1000) {
return PromiseA.resolve(ipc.acmeUrls);
}
2015-12-16 09:11:31 +00:00
return LeCore.getAcmeUrlsAsync(args.server).then(function (data) {
2015-12-15 11:37:39 +00:00
ipc.acmeUrlsUpdatedAt = Date.now();
2015-12-16 10:07:00 +00:00
ipc.acmeUrls = data;
2015-12-15 11:37:39 +00:00
return ipc.acmeUrls;
});
}
function createAccount(args, handlers) {
var os = require("os");
var localname = os.hostname();
// TODO support ECDSA
// arg.rsaBitLength args.rsaExponent
2015-12-16 09:25:39 +00:00
return leCrypto.generateRsaKeypairAsync(args.rsaBitLength, args.rsaExponent).then(function (pems) {
2015-12-15 12:45:36 +00:00
/* pems = { privateKeyPem, privateKeyJwk, publicKeyPem, publicKeyMd5 } */
2015-12-15 11:37:39 +00:00
2015-12-16 09:11:31 +00:00
return LeCore.registerNewAccountAsync({
2015-12-15 11:37:39 +00:00
email: args.email
2015-12-16 10:07:00 +00:00
, newRegUrl: args._acmeUrls.newReg
2015-12-15 11:37:39 +00:00
, agreeToTerms: function (tosUrl, agree) {
2015-12-15 12:45:36 +00:00
// args.email = email; // already there
2015-12-15 11:37:39 +00:00
args.tosUrl = tosUrl;
handlers.agreeToTerms(args, agree);
}
2015-12-15 12:45:36 +00:00
, accountPrivateKeyPem: pems.privateKeyPem
2015-12-16 10:07:00 +00:00
, debug: args.debug || handlers.debug
2015-12-15 11:37:39 +00:00
}).then(function (body) {
2015-12-15 13:21:26 +00:00
var accountDir = path.join(args.accountsDir, pems.publicKeyMd5);
return mkdirpAsync(accountDir).then(function () {
2015-12-15 12:45:36 +00:00
var isoDate = new Date().toISOString();
var accountMeta = {
creation_host: localname
, creation_dt: isoDate
};
2015-12-15 11:37:39 +00:00
return PromiseA.all([
2015-12-16 09:11:31 +00:00
// meta.json {"creation_host": "ns1.redirect-www.org", "creation_dt": "2015-12-11T04:14:38Z"}
2015-12-15 12:45:36 +00:00
fs.writeFileAsync(path.join(accountDir, 'meta.json'), JSON.stringify(accountMeta), 'utf8')
2015-12-16 09:11:31 +00:00
// private_key.json { "e", "d", "n", "q", "p", "kty", "qi", "dp", "dq" }
2015-12-15 12:45:36 +00:00
, fs.writeFileAsync(path.join(accountDir, 'private_key.json'), JSON.stringify(pems.privateKeyJwk), 'utf8')
2015-12-16 09:11:31 +00:00
// regr.json:
/*
{ body: { contact: [ 'mailto:coolaj86@gmail.com' ],
agreement: 'https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf',
key: { e: 'AQAB', kty: 'RSA', n: '...' } },
uri: 'https://acme-v01.api.letsencrypt.org/acme/reg/71272',
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' }
*/
2015-12-15 11:37:39 +00:00
, fs.writeFileAsync(path.join(accountDir, 'regr.json'), JSON.stringify({ body: body }), 'utf8')
2015-12-15 12:45:36 +00:00
]).then(function () {
return pems;
});
2015-12-15 11:37:39 +00:00
});
});
});
}
2015-12-15 12:45:36 +00:00
function getAccount(accountId, args, handlers) {
2015-12-15 11:37:39 +00:00
var accountDir = path.join(args.accountsDir, accountId);
var files = {};
var configs = ['meta.json', 'private_key.json', 'regr.json'];
return PromiseA.all(configs.map(function (filename) {
var keyname = filename.slice(0, -5);
return fs.readFileAsync(path.join(accountDir, filename), 'utf8').then(function (text) {
var data;
try {
data = JSON.parse(text);
} catch(e) {
files[keyname] = { error: e };
return;
}
files[keyname] = data;
}, function (err) {
2015-12-15 12:45:36 +00:00
files[keyname] = { error: err };
2015-12-15 11:37:39 +00:00
});
})).then(function () {
if (!Object.keys(files).every(function (key) {
return !files[key].error;
})) {
2015-12-16 09:34:39 +00:00
// TODO log renewal.conf
2015-12-15 11:37:39 +00:00
console.warn("Account '" + accountId + "' was currupt. No big deal (I think?). Creating a new one...");
2015-12-15 12:45:36 +00:00
return createAccount(args, handlers);
2015-12-15 11:37:39 +00:00
}
2015-12-16 09:25:39 +00:00
return leCrypto.parseAccountPrivateKeyAsync(files.private_key).then(function (keypair) {
2015-12-15 11:37:39 +00:00
files.accountId = accountId; // md5sum(publicKeyPem)
2015-12-15 12:45:36 +00:00
files.publicKeyMd5 = accountId; // md5sum(publicKeyPem)
2015-12-15 11:37:39 +00:00
files.publicKeyPem = keypair.publicKeyPem; // ascii PEM: ----BEGIN...
files.privateKeyPem = keypair.privateKeyPem; // ascii PEM: ----BEGIN...
files.privateKeyJson = keypair.private_key; // json { n: ..., e: ..., iq: ..., etc }
return files;
});
});
}
function getAccountByEmail(args) {
// If we read 10,000 account directories looking for
// just one email address, that could get crazy.
// We should have a folder per email and list
// each account as a file in the folder
// TODO
return PromiseA.resolve(null);
}
2015-12-15 15:21:27 +00:00
function getCertificateAsync(account, args, defaults, handlers) {
2015-12-15 11:37:39 +00:00
var pyconf = PromiseA.promisifyAll(require('pyconf'));
2015-12-16 09:25:39 +00:00
return leCrypto.generateRsaKeypairAsync(args.rsaBitLength, args.rsaExponent).then(function (domain) {
2015-12-16 10:07:00 +00:00
return LeCore.getCertificateAsync({
newAuthzUrl: args._acmeUrls.newAuthz
, newCertUrl: args._acmeUrls.newCert
2015-12-16 09:11:31 +00:00
2015-12-15 15:21:27 +00:00
, accountPrivateKeyPem: account.privateKeyPem
, domainPrivateKeyPem: domain.privateKeyPem
2015-12-16 09:11:31 +00:00
, domains: args.domains
2015-12-15 15:21:27 +00:00
, setChallenge: function (domain, key, value, done) {
args.domains = [domain];
args.webrootPath = args.webrootPath || defaults.webrootPath;
if (4 === handlers.setChallenge.length) {
handlers.setChallenge(args, key, value, done);
}
else if (5 === handlers.setChallenge.length) {
handlers.setChallenge(args, domain, key, value, done);
}
else {
done(new Error("handlers.setChallenge receives the wrong number of arguments"));
}
2015-12-15 15:21:27 +00:00
}
, removeChallenge: function (domain, key, done) {
args.domains = [domain];
args.webrootPath = args.webrootPath || defaults.webrootPath;
if (3 === handlers.removeChallenge.length) {
handlers.removeChallenge(args, key, done);
}
else if (4 === handlers.removeChallenge.length) {
handlers.removeChallenge(args, domain, key, done);
}
else {
done(new Error("handlers.removeChallenge receives the wrong number of arguments"));
}
2015-12-15 15:21:27 +00:00
}
}).then(function (result) {
2015-12-16 09:11:31 +00:00
// TODO write pems={ca,cert,key} to disk
var liveDir = path.join(args.configDir, 'live', args.domains[0]);
var certPath = path.join(liveDir, 'cert.pem');
var fullchainPath = path.join(liveDir, 'fullchain.pem');
var chainPath = path.join(liveDir, 'chain.pem');
var privkeyPath = path.join(liveDir, 'privkey.pem');
result.fullchain = result.cert + '\n' + result.ca;
// TODO write to archive first, then write to live
return mkdirpAsync(liveDir).then(function () {
2015-12-16 10:07:00 +00:00
return PromiseA.all([
2015-12-16 09:11:31 +00:00
sfs.writeFileAsync(certPath, result.cert, 'ascii')
, sfs.writeFileAsync(chainPath, result.chain, 'ascii')
, sfs.writeFileAsync(fullchainPath, result.fullchain, 'ascii')
, sfs.writeFileAsync(privkeyPath, result.key, 'ascii')
2015-12-16 12:57:53 +00:00
]).then(function () {
// TODO format result licesy
//console.log(liveDir);
//console.log(result);
return {
certPath: certPath
, chainPath: chainPath
, fullchainPath: fullchainPath
, privkeyPath: privkeyPath
};
});
2015-12-16 09:11:31 +00:00
});
2015-12-15 15:21:27 +00:00
});
});
}
2015-12-15 11:37:39 +00:00
2015-12-15 15:21:27 +00:00
function registerWithAcme(args, defaults, handlers) {
var pyconf = PromiseA.promisifyAll(require('pyconf'));
2015-12-16 10:07:00 +00:00
var server = args.server || defaults.server || LeCore.stagingServerUrl; // https://acme-v01.api.letsencrypt.org/directory
2015-12-15 15:21:27 +00:00
var acmeHostname = require('url').parse(server).hostname;
var configDir = args.configDir || defaults.configDir || LE.configDir;
args.server = server;
args.renewalDir = args.renewalDir || path.join(configDir, 'renewal', args.domains[0] + '.conf');
args.accountsDir = args.accountsDir || path.join(configDir, 'accounts', acmeHostname, 'directory');
return pyconf.readFileAsync(args.renewalDir).then(function (renewal) {
var accountId = renewal.account;
renewal = renewal.account;
return accountId;
}, function (err) {
2015-12-16 10:07:00 +00:00
if ("ENOENT" === err.code) {
2015-12-15 15:21:27 +00:00
return getAccountByEmail(args, handlers);
}
2015-12-15 11:37:39 +00:00
2015-12-15 15:21:27 +00:00
return PromiseA.reject(err);
}).then(function (accountId) {
// Note: the ACME urls are always fetched fresh on purpose
return getAcmeUrls(args).then(function (urls) {
args._acmeUrls = urls;
if (accountId) {
return getAccount(accountId, args, handlers);
} else {
return createAccount(args, handlers);
}
});
}).then(function (account) {
/*
if (renewal.account !== account) {
// the account has become corrupt, re-register
return;
}
*/
2015-12-16 12:57:53 +00:00
//console.log(account);
2015-12-15 15:21:27 +00:00
return fetchFromConfigLiveDir(args, defaults).then(function (certs) {
// if nothing, register and save
// if something, check date (don't register unless 30+ days)
// if good, don't bother registering
// (but if we get to the point that we're actually calling
// this function, that shouldn't be the case, right?)
2015-12-16 12:57:53 +00:00
//console.log(certs);
2015-12-15 15:21:27 +00:00
if (!certs) {
// no certs, seems like a good time to get some
return getCertificateAsync(account, args, defaults, handlers);
}
else if (certs.issuedAt > (27 * 24 * 60 * 60 * 1000)) {
// cert is at least 27 days old we can renew that
return getCertificateAsync(account, args, defaults, handlers);
}
2015-12-16 12:57:53 +00:00
else if (args.duplicate) {
2015-12-15 15:21:27 +00:00
// YOLO! I be gettin' fresh certs 'erday! Yo!
return getCertificateAsync(account, args, defaults, handlers);
}
else {
2015-12-16 12:57:53 +00:00
console.warn('[WARN] Ignoring renewal attempt for certificate less than 27 days old. Use args.duplicate to force.');
2015-12-15 15:21:27 +00:00
// We're happy with what we have
return certs;
}
});
/*
cert = /home/aj/node-letsencrypt/tests/letsencrypt.config/live/lds.io/cert.pem
privkey = /home/aj/node-letsencrypt/tests/letsencrypt.config/live/lds.io/privkey.pem
chain = /home/aj/node-letsencrypt/tests/letsencrypt.config/live/lds.io/chain.pem
fullchain = /home/aj/node-letsencrypt/tests/letsencrypt.config/live/lds.io/fullchain.pem
# Options and defaults used in the renewal process
[renewalparams]
apache_enmod = a2enmod
no_verify_ssl = False
ifaces = None
apache_dismod = a2dismod
register_unsafely_without_email = False
uir = None
installer = none
config_dir = /home/aj/node-letsencrypt/tests/letsencrypt.config
text_mode = True
func = <function obtain_cert at 0x7f46af0f02a8>
prepare = False
work_dir = /home/aj/node-letsencrypt/tests/letsencrypt.work
tos = True
init = False
http01_port = 80
duplicate = False
key_path = None
nginx = False
fullchain_path = /home/aj/node-letsencrypt/chain.pem
email = coolaj86@gmail.com
csr = None
agree_dev_preview = None
redirect = None
verbose_count = -3
config_file = None
renew_by_default = True
hsts = False
authenticator = webroot
domains = lds.io,
rsa_key_size = 2048
checkpoints = 1
manual_test_mode = False
apache = False
cert_path = /home/aj/node-letsencrypt/cert.pem
webroot_path = /home/aj/node-letsencrypt/examples/../tests/acme-challenge,
strict_permissions = False
apache_server_root = /etc/apache2
account = 1c41c64dfaf10d511db8aef0cc33b27f
manual_public_ip_logging_ok = False
chain_path = /home/aj/node-letsencrypt/chain.pem
standalone = False
manual = False
server = https://acme-staging.api.letsencrypt.org/directory
standalone_supported_challenges = "http-01,tls-sni-01"
webroot = True
apache_init_script = None
user_agent = None
apache_ctl = apache2ctl
apache_le_vhost_ext = -le-ssl.conf
debug = False
tls_sni_01_port = 443
logs_dir = /home/aj/node-letsencrypt/tests/letsencrypt.logs
configurator = None
[[webroot_map]]
lds.io = /home/aj/node-letsencrypt/examples/../tests/acme-challenge
*/
});
2015-12-15 11:37:39 +00:00
/*
2015-12-15 15:21:27 +00:00
return fs.readdirAsync(accountsDir, function (nodes) {
return PromiseA.all(nodes.map(function (node) {
var reMd5 = /[a-f0-9]{32}/i;
if (reMd5.test(node)) {
}
}));
});
2015-12-15 11:37:39 +00:00
*/
2015-12-15 15:21:27 +00:00
}
module.exports.create = function (defaults, handlers) {
defaults.server = defaults.server || LE.liveServer;
var wrapped = {
registerAsync: function (args) {
//require('./common').registerWithAcme(args, defaults, handlers);
return registerWithAcme(args, defaults, handlers);
2015-12-15 11:37:39 +00:00
}
, fetchAsync: function (args) {
2015-12-15 15:21:27 +00:00
return fetchFromConfigLiveDir(args, defaults);
2015-12-15 11:37:39 +00:00
}
};
return wrapped;
};