Compare commits
19 Commits
Author | SHA1 | Date | |
---|---|---|---|
e0912d0f76 | |||
1f3e2670b9 | |||
|
6c8de20090 | ||
fd06582813 | |||
14458181bf | |||
|
65f0989b44 | ||
|
c92dde1b87 | ||
|
19dd9a95f4 | ||
|
894b687ff7 | ||
|
7cebf47125 | ||
|
ff39ea58c7 | ||
|
665e66263a | ||
|
b0d9c52c64 | ||
|
d75557a017 | ||
|
2be6227db2 | ||
|
7d743280fe | ||
|
d217e9721e | ||
|
7f826369a6 | ||
|
ee67a5bc8b |
52
README.md
52
README.md
@ -1,6 +1,8 @@
|
||||
le-sni-auto
|
||||
===========
|
||||
|
||||
| Sponsored by [ppl](https://ppl.family)
|
||||
|
||||
An auto-sni strategy for registering and renewing letsencrypt certificates using SNICallback.
|
||||
|
||||
This does a couple of rather simple things:
|
||||
@ -29,8 +31,8 @@ With node-letsencrypt
|
||||
|
||||
var leSni = require('le-sni-auto').create({
|
||||
|
||||
renewWithin: 10 * 24 * 60 * 60 1000 // do not renew more than 10 days before expiration
|
||||
, renewBy: 5 * 24 * 60 * 60 1000 // do not wait more than 5 days before expiration
|
||||
renewWithin: 14 * 24 * 60 * 60 1000 // do not renew more than 14 days before expiration
|
||||
, renewBy: 10 * 24 * 60 * 60 1000 // do not wait more than 10 days before expiration
|
||||
|
||||
, tlsOptions: {
|
||||
rejectUnauthorized: true // These options will be used with tls.createSecureContext()
|
||||
@ -62,9 +64,7 @@ http.createServer(le.middleware(redirectHttps));
|
||||
|
||||
|
||||
var app = require('express')();
|
||||
var httpsOptions = { SNICallback: le.sni.callback };
|
||||
httpsOptions = require('localhost.daplie.com-certificates').merge(httpsOptions);
|
||||
https.createServer(dummyCerts, le.middleware(app)).listen(443);
|
||||
https.createServer(le.tlsOptions, le.middleware(app)).listen(443);
|
||||
```
|
||||
|
||||
You can also provide a thunk-style `getCertificates(domain, certs, cb)`.
|
||||
@ -78,8 +78,8 @@ Standalone
|
||||
|
||||
|
||||
var leSni = require('le-sni-auto').create({
|
||||
renewWithin: 10 * 24 * 60 * 60 1000 // do not renew prior to 10 days before expiration
|
||||
, renewBy: 5 * 24 * 60 * 60 1000 // do not wait more than 5 days before expiration
|
||||
renewWithin: 14 * 24 * 60 * 60 1000 // do not renew prior to 10 days before expiration
|
||||
, renewBy: 10 * 24 * 60 * 60 1000 // do not wait more than 5 days before expiration
|
||||
|
||||
// key (privkey.pem) and cert (cert.pem + chain.pem) will be provided by letsencrypt
|
||||
, tlsOptions: { rejectUnauthorized: true, requestCert: false, ca: null, crl: null }
|
||||
@ -92,13 +92,11 @@ var leSni = require('le-sni-auto').create({
|
||||
|
||||
|
||||
|
||||
// some default certificates that work with localhost
|
||||
// (because default certificates are required as a fallback)
|
||||
var httpsOptions = require('localhost.daplie.com-certificates').merge({
|
||||
var tlsOptions = {
|
||||
SNICallback: leSni.sniCallback
|
||||
});
|
||||
};
|
||||
|
||||
https.createServer(httpsOptions, app);
|
||||
https.createServer(tlsOptions, app);
|
||||
```
|
||||
|
||||
You can also provide a thunk-style `getCertificates(domain, certs, cb)`.
|
||||
@ -112,6 +110,7 @@ API
|
||||
* `renewBy` (default 2 days, min 12 hours)
|
||||
* `sniCallback(domain, cb)`
|
||||
* `cacheCerts(certs)`
|
||||
* `uncacheDomain(domain)`
|
||||
|
||||
.renewWithin
|
||||
-----------
|
||||
@ -140,22 +139,22 @@ You would set this to `10 * 24 * 60 * 60 * 1000`.
|
||||
.sniCallback()
|
||||
-----------
|
||||
|
||||
This gets passed to `https.createServer(httpsOptions, app)` as `httpsOptions.SNICallback`.
|
||||
This gets passed to `https.createServer(tlsOptions, app)` as `tlsOptions.SNICallback`.
|
||||
|
||||
```javascript
|
||||
var leSni = require('le-sni-auto').create({
|
||||
renewWithin: 10 * 24 * 60 * 60 1000
|
||||
renewWithin: 14 * 24 * 60 * 60 1000
|
||||
});
|
||||
|
||||
var httpsOptions = require('localhost.daplie.com-certificates').merge({
|
||||
var tlsOptions = {
|
||||
SNICallback: leSni.sniCallback
|
||||
});
|
||||
};
|
||||
|
||||
function app(req, res) {
|
||||
res.end("Hello, World!");
|
||||
}
|
||||
|
||||
https.createServer(httpsOptions, app);
|
||||
https.createServer(tlsOptions, app);
|
||||
```
|
||||
|
||||
.cacheCerts()
|
||||
@ -164,7 +163,8 @@ https.createServer(httpsOptions, app);
|
||||
Manually load a certificate into the cache.
|
||||
|
||||
This is useful in a cluster environment where the master
|
||||
may wish to inform multiple workers of a new or renewed certificate.
|
||||
may wish to inform multiple workers of a new or renewed certificate,
|
||||
or to satisfy tls-sni-01 challenges.
|
||||
|
||||
```
|
||||
leSni.cacheCerts({
|
||||
@ -174,5 +174,21 @@ leSni.cacheCerts({
|
||||
, altnames: [ 'example.com', 'www.example.com' ]
|
||||
, issuedAt: 1470975565000
|
||||
, expiresAt: 1478751565000
|
||||
, auto: true
|
||||
});
|
||||
```
|
||||
|
||||
.uncacheCerts()
|
||||
-----------
|
||||
|
||||
Remove cached certificates from the cache.
|
||||
|
||||
This is useful once a tls-sni-01 challenge has been satisfied.
|
||||
|
||||
```
|
||||
leSni.uncacheCerts({
|
||||
, subject: 'example.com'
|
||||
, altnames: [ 'example.com', 'www.example.com' ]
|
||||
});
|
||||
```
|
||||
|
||||
|
63
index.js
63
index.js
@ -1,29 +1,38 @@
|
||||
'use strict';
|
||||
|
||||
var DAY = 24 * 60 * 60 * 1000;
|
||||
var HOUR = 60 * 60 * 1000;
|
||||
var MIN = 60 * 1000;
|
||||
var defaults = {
|
||||
// don't renew before the renewWithin period
|
||||
renewWithin: 7 * DAY
|
||||
renewWithin: 30 * DAY
|
||||
, _renewWithinMin: 3 * DAY
|
||||
// renew before the renewBy period
|
||||
, renewBy: 2 * DAY
|
||||
, renewBy: 21 * DAY
|
||||
, _renewByMin: Math.floor(DAY / 2)
|
||||
// just to account for clock skew really
|
||||
, _dropDead: 5 * MIN
|
||||
};
|
||||
var promisify = require('util').promisify;
|
||||
if (!promisify) {
|
||||
try {
|
||||
promisify = require('bluebird').promisify;
|
||||
} catch(e) {
|
||||
console.error("You're running an older version of node that doesn't have 'promisify'. Please run 'npm install bluebird --save'.");
|
||||
}
|
||||
}
|
||||
|
||||
// autoSni = { renewWithin, renewBy, getCertificates, tlsOptions, _dbg_now }
|
||||
module.exports.create = function (autoSni) {
|
||||
|
||||
if (!autoSni.getCertificatesAsync) { autoSni.getCertificatesAsync = require('bluebird').promisify(autoSni.getCertificates); }
|
||||
if (!autoSni.getCertificatesAsync) { autoSni.getCertificatesAsync = promisify(autoSni.getCertificates); }
|
||||
if (!autoSni.renewWithin) { autoSni.renewWithin = autoSni.notBefore || defaults.renewWithin; }
|
||||
if (autoSni.renewWithin < defaults._renewWithinMin) {
|
||||
throw new Error("options.renewWithin should be at least 3 days");
|
||||
throw new Error("options.renewWithin should be at least " + (defaults._renewWithinMin / DAY) + " days");
|
||||
}
|
||||
if (!autoSni.renewBy) { autoSni.renewBy = autoSni.notBefore || defaults.renewBy; }
|
||||
if (!autoSni.renewBy) { autoSni.renewBy = autoSni.notAfter || defaults.renewBy; }
|
||||
if (autoSni.renewBy < defaults._renewByMin) {
|
||||
throw new Error("options.renewBy should be at least 12 hours");
|
||||
throw new Error("options.renewBy should be at least " + (defaults._renewBy / HOUR) + " hours");
|
||||
}
|
||||
if (!autoSni.tlsOptions) { autoSni.tlsOptions = autoSni.httpsOptions || {}; }
|
||||
|
||||
@ -63,7 +72,8 @@ module.exports.create = function (autoSni) {
|
||||
certs: certs
|
||||
, tlsContext: 'string' === typeof certs.cert && tls.createSecureContext({
|
||||
key: certs.privkey
|
||||
, cert: certs.cert + certs.chain
|
||||
// backwards/forwards compat
|
||||
, cert: (certs.cert||'').replace(/[\r\n]+$/, '') + '\r\n' + certs.chain
|
||||
, rejectUnauthorized: autoSni.tlsOptions.rejectUnauthorized
|
||||
|
||||
, requestCert: autoSni.tlsOptions.requestCert // request peer verification
|
||||
@ -72,6 +82,7 @@ module.exports.create = function (autoSni) {
|
||||
}) || { '_fake_tls_context_': true }
|
||||
|
||||
, subject: certs.subject
|
||||
, auto: 'undefined' === typeof certs.auto ? true : certs.auto
|
||||
// stagger renewal time by a little bit of randomness
|
||||
, renewAt: (certs.expiresAt - (autoSni.renewWithin - (autoSni._renewWindow * Math.random())))
|
||||
// err just barely on the side of safety
|
||||
@ -90,13 +101,23 @@ module.exports.create = function (autoSni) {
|
||||
|
||||
|
||||
|
||||
, uncacheCerts: function (certs) {
|
||||
certs.altnames.forEach(function (domain) {
|
||||
delete autoSni._ipc[domain];
|
||||
});
|
||||
delete autoSni._ipc[certs.subject];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// automate certificate registration on request
|
||||
, sniCallback: function (domain, cb) {
|
||||
var certMeta = autoSni._ipc[domain];
|
||||
var promise;
|
||||
var now = (autoSni._dbg_now || Date.now());
|
||||
|
||||
if (certMeta && certMeta.subject !== domain) {
|
||||
if (certMeta && !certMeta.then && certMeta.subject !== domain) {
|
||||
//log(autoSni.debug, "LINK CERT", domain);
|
||||
certMeta = autoSni._ipc[certMeta.subject];
|
||||
}
|
||||
@ -104,21 +125,31 @@ module.exports.create = function (autoSni) {
|
||||
if (!certMeta) {
|
||||
//log(autoSni.debug, "NO CERT", domain);
|
||||
// we don't have a cert and must get one
|
||||
promise = autoSni.getCertificatesAsync(domain, null);
|
||||
promise = autoSni.getCertificatesAsync(domain, null).then(autoSni.cacheCerts);
|
||||
autoSni._ipc[domain] = promise;
|
||||
}
|
||||
else if (certMeta.then) {
|
||||
//log(autoSni.debug, "PROMISED CERT", domain);
|
||||
// we are already getting a cert
|
||||
promise = certMeta;
|
||||
}
|
||||
else if (now >= certMeta.expiresNear) {
|
||||
//log(autoSni.debug, "EXPIRED CERT");
|
||||
// we have a cert, but it's no good for the average user
|
||||
promise = autoSni.getCertificatesAsync(domain, certMeta.certs);
|
||||
promise = autoSni.getCertificatesAsync(domain, certMeta.certs).then(autoSni.cacheCerts);
|
||||
autoSni._ipc[certMeta.subject] = promise;
|
||||
} else {
|
||||
|
||||
// it's time to renew the cert
|
||||
if (now >= certMeta.renewAt) {
|
||||
if (certMeta.auto && now >= certMeta.renewAt) {
|
||||
//log(autoSni.debug, "RENEWABLE CERT");
|
||||
// give the cert some time (2-5 min) to be validated and replaced before trying again
|
||||
certMeta.renewAt = (autoSni._dbg_now || Date.now()) + (2 * MIN) + (3 * MIN * Math.random());
|
||||
// let the update happen in the background
|
||||
autoSni.getCertificatesAsync(domain, certMeta.certs).then(autoSni.cacheCerts);
|
||||
autoSni.getCertificatesAsync(domain, certMeta.certs).then(autoSni.cacheCerts, function (error) {
|
||||
// console.error('ERROR in le-sni-auto:');
|
||||
// console.error(err.stack || err);
|
||||
})
|
||||
}
|
||||
|
||||
// return the valid cert right away
|
||||
@ -127,12 +158,14 @@ module.exports.create = function (autoSni) {
|
||||
}
|
||||
|
||||
// promise the non-existent or expired cert
|
||||
promise.then(autoSni.cacheCerts).then(function (certMeta) {
|
||||
promise.then(function (certMeta) {
|
||||
cb(null, certMeta.tlsContext);
|
||||
}, function (err) {
|
||||
console.error('ERROR in le-sni-auto:');
|
||||
console.error(err.stack || err);
|
||||
// console.error('ERROR in le-sni-auto:');
|
||||
// console.error(err.stack || err);
|
||||
cb(err);
|
||||
// don't reuse this promise
|
||||
delete autoSni._ipc[certMeta && certMeta.subject ? certMeta.subject : domain];
|
||||
});
|
||||
}
|
||||
|
||||
|
5
package-lock.json
generated
Normal file
5
package-lock.json
generated
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "le-sni-auto",
|
||||
"version": "2.1.9",
|
||||
"lockfileVersion": 1
|
||||
}
|
14
package.json
14
package.json
@ -1,10 +1,11 @@
|
||||
{
|
||||
"name": "le-sni-auto",
|
||||
"version": "2.0.1",
|
||||
"version": "2.1.9",
|
||||
"description": "An auto-sni strategy for registering and renewing letsencrypt certificates using SNICallback",
|
||||
"homepage": "https://git.coolaj86.com/coolaj86/le-sni-auto.js",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"bluebird": "^3.4.1"
|
||||
"trulyOptionalDependencies": {
|
||||
"bluebird": "^3.5.1"
|
||||
},
|
||||
"devDependencies": {},
|
||||
"scripts": {
|
||||
@ -12,7 +13,7 @@
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/Daplie/le-sni-auto.git"
|
||||
"url": "https://git.coolaj86.com/coolaj86/le-sni-auto.js.git"
|
||||
},
|
||||
"keywords": [
|
||||
"le-sni",
|
||||
@ -26,7 +27,6 @@
|
||||
"author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)",
|
||||
"license": "(MIT OR Apache-2.0)",
|
||||
"bugs": {
|
||||
"url": "https://github.com/Daplie/le-sni-auto/issues"
|
||||
},
|
||||
"homepage": "https://github.com/Daplie/le-sni-auto#readme"
|
||||
"url": "https://git.coolaj86.com/coolaj86/le-sni-auto.js/issues"
|
||||
}
|
||||
}
|
||||
|
145
test.js
145
test.js
@ -17,13 +17,19 @@ var CERT_2 = {
|
||||
, subject: 'example.com'
|
||||
, altnames: ['example.com', 'www.example.com']
|
||||
};
|
||||
var CERT_3 = {
|
||||
expiresAt: EXPIRES_AT
|
||||
, subject: 'example.com'
|
||||
, altnames: ['example.com', 'www.example.com']
|
||||
, auto: false
|
||||
};
|
||||
|
||||
var count = 0;
|
||||
var expectedCount = 3;
|
||||
var expectedCount = 4;
|
||||
var tests = [
|
||||
function (domain, certs, cb) {
|
||||
count += 1;
|
||||
console.log('#1 is 1 of 3');
|
||||
console.log('#1 is 1 of 4');
|
||||
if (!domain) {
|
||||
throw new Error("should have a domain");
|
||||
}
|
||||
@ -42,7 +48,7 @@ var tests = [
|
||||
}
|
||||
, function (domain, certs, cb) {
|
||||
count += 1;
|
||||
console.log('#3 is 2 of 3');
|
||||
console.log('#3 is 2 of 4');
|
||||
// NOTE: there's a very very small chance this will fail occasionally (if Math.random() < 0.01)
|
||||
if (!certs) {
|
||||
throw new Error("should have certs to renew (renewAt)");
|
||||
@ -52,7 +58,7 @@ var tests = [
|
||||
}
|
||||
, function (domain, certs, cb) {
|
||||
count += 1;
|
||||
console.log('#4 is 3 of 3');
|
||||
console.log('#4 is 3 of 4');
|
||||
if (!certs) {
|
||||
throw new Error("should have certs to renew (expiresNear)");
|
||||
}
|
||||
@ -63,11 +69,24 @@ var tests = [
|
||||
console.log('#5 should NOT be called');
|
||||
throw new Error("Should not call register renew a certificate with more than 10 days left");
|
||||
}
|
||||
, function (domain, certs, cb) {
|
||||
count += 1;
|
||||
console.log('#6 is 4 of 4');
|
||||
if (certs) {
|
||||
throw new Error("should not have certs that have been uncached");
|
||||
}
|
||||
|
||||
cb(null, CERT_3);
|
||||
}
|
||||
, function (/*domain, certs, cb*/) {
|
||||
console.log('#7 should NOT be called');
|
||||
throw new Error("Should not call register renew a non-auto certificate");
|
||||
}
|
||||
].map(function (fn) {
|
||||
return require('bluebird').promisify(fn);
|
||||
});
|
||||
|
||||
// opts = { notBefore, notAfter, letsencrypt.renew, letsencrypt.register, httpsOptions }
|
||||
// opts = { notBefore, notAfter, letsencrypt.renew, letsencrypt.register, tlsOptions }
|
||||
var leSni = require('./').create({
|
||||
notBefore: NOT_BEFORE
|
||||
, notAfter: NOT_AFTER
|
||||
@ -75,10 +94,16 @@ var leSni = require('./').create({
|
||||
, _dbg_now: START_DAY
|
||||
});
|
||||
|
||||
var shared = 0;
|
||||
var expectedShared = 3;
|
||||
leSni.sniCallback('example.com', function (err, tlsContext) {
|
||||
if (err) { throw err; }
|
||||
shared += 1;
|
||||
});
|
||||
leSni.sniCallback('example.com', function (err, tlsContext) {
|
||||
if (err) { throw err; }
|
||||
if (!tlsContext._fake_tls_context_) {
|
||||
throw new Error("Did not return tlsContext 0");
|
||||
throw new Error("Did not return tlsContext #1");
|
||||
}
|
||||
leSni.getCertificatesAsync = tests.shift();
|
||||
|
||||
@ -88,7 +113,63 @@ leSni.sniCallback('example.com', function (err, tlsContext) {
|
||||
leSni.sniCallback('example.com', function (err, tlsContext) {
|
||||
if (err) { throw err; }
|
||||
if (!tlsContext._fake_tls_context_) {
|
||||
throw new Error("Did not return tlsContext 1");
|
||||
throw new Error("Did not return tlsContext #2");
|
||||
}
|
||||
leSni.getCertificatesAsync = tests.shift();
|
||||
|
||||
leSni._dbg_now = RENEWABLE_DAY;
|
||||
|
||||
|
||||
|
||||
|
||||
leSni.sniCallback('www.example.com', function (err, tlsContext) {
|
||||
if (err) { throw err; }
|
||||
shared += 1;
|
||||
});
|
||||
leSni.sniCallback('example.com', function (err, tlsContext) {
|
||||
if (err) { throw err; }
|
||||
if (!tlsContext._fake_tls_context_) {
|
||||
throw new Error("Did not return tlsContext #3");
|
||||
}
|
||||
leSni.getCertificatesAsync = tests.shift();
|
||||
|
||||
leSni._dbg_now = EXPIRES_AT;
|
||||
|
||||
|
||||
|
||||
|
||||
leSni.sniCallback('www.example.com', function (err, tlsContext) {
|
||||
if (err) { throw err; }
|
||||
shared += 1;
|
||||
});
|
||||
leSni.sniCallback('www.example.com', function (err, tlsContext) {
|
||||
if (err) { throw err; }
|
||||
if (!tlsContext._fake_tls_context_) {
|
||||
throw new Error("Did not return tlsContext #4");
|
||||
}
|
||||
leSni.getCertificatesAsync = tests.shift();
|
||||
|
||||
|
||||
|
||||
|
||||
leSni.sniCallback('www.example.com', function (err, tlsContext) {
|
||||
if (err) { throw err; }
|
||||
if (!tlsContext._fake_tls_context_) {
|
||||
throw new Error("Did not return tlsContext #5");
|
||||
}
|
||||
leSni.uncacheCerts({
|
||||
subject: 'example.com'
|
||||
, altnames: ['example.com', 'www.example.com']
|
||||
});
|
||||
leSni.getCertificatesAsync = tests.shift();
|
||||
|
||||
|
||||
|
||||
|
||||
leSni.sniCallback('example.com', function (err, tlsContext) {
|
||||
if (err) { throw err; }
|
||||
if (!tlsContext._fake_tls_context_) {
|
||||
throw new Error("Did not return tlsContext #6");
|
||||
}
|
||||
leSni.getCertificatesAsync = tests.shift();
|
||||
|
||||
@ -98,44 +179,26 @@ leSni.sniCallback('example.com', function (err, tlsContext) {
|
||||
|
||||
|
||||
leSni.sniCallback('example.com', function (err, tlsContext) {
|
||||
if (err) { throw err; }
|
||||
if (!tlsContext._fake_tls_context_) {
|
||||
throw new Error("Did not return tlsContext 2");
|
||||
}
|
||||
leSni.getCertificatesAsync = tests.shift();
|
||||
|
||||
leSni._dbg_now = EXPIRES_AT;
|
||||
|
||||
|
||||
|
||||
|
||||
leSni.sniCallback('example.com', function (err, tlsContext) {
|
||||
if (err) { throw err; }
|
||||
if (!tlsContext._fake_tls_context_) {
|
||||
throw new Error("Did not return tlsContext 2");
|
||||
}
|
||||
leSni.getCertificatesAsync = tests.shift();
|
||||
|
||||
|
||||
|
||||
|
||||
leSni.sniCallback('example.com', function (err, tlsContext) {
|
||||
if (err) { throw err; }
|
||||
if (!tlsContext._fake_tls_context_) {
|
||||
throw new Error("Did not return tlsContext 2");
|
||||
throw new Error("Did not return tlsContext #7");
|
||||
}
|
||||
|
||||
if (expectedCount !== count) {
|
||||
throw new Error("getCertificate only called " + count + " times");
|
||||
}
|
||||
|
||||
if (expectedShared !== shared) {
|
||||
throw new Error("wrongly used only " + shared + " shared promises");
|
||||
}
|
||||
|
||||
if (tests.length) {
|
||||
throw new Error("some test functions not run");
|
||||
}
|
||||
|
||||
if (expectedCount === count && !tests.length) {
|
||||
console.log('PASS');
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Error("only " + count + " of the register getCertificate were called");
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user