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
 | 
					le-sni-auto
 | 
				
			||||||
===========
 | 
					===========
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					| Sponsored by [ppl](https://ppl.family)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
An auto-sni strategy for registering and renewing letsencrypt certificates using SNICallback.
 | 
					An auto-sni strategy for registering and renewing letsencrypt certificates using SNICallback.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
This does a couple of rather simple things:
 | 
					This does a couple of rather simple things:
 | 
				
			||||||
@ -29,8 +31,8 @@ With node-letsencrypt
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
var leSni = require('le-sni-auto').create({
 | 
					var leSni = require('le-sni-auto').create({
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  renewWithin: 10 * 24 * 60 * 60 1000       // do not renew more than 10 days before expiration
 | 
					  renewWithin: 14 * 24 * 60 * 60 1000     // do not renew more than 14 days before expiration
 | 
				
			||||||
, renewBy: 5 * 24 * 60 * 60 1000         // do not wait more than 5 days before expiration
 | 
					, renewBy: 10 * 24 * 60 * 60 1000         // do not wait more than 10 days before expiration
 | 
				
			||||||
 | 
					
 | 
				
			||||||
, tlsOptions: {
 | 
					, tlsOptions: {
 | 
				
			||||||
    rejectUnauthorized: true              // These options will be used with tls.createSecureContext()
 | 
					    rejectUnauthorized: true              // These options will be used with tls.createSecureContext()
 | 
				
			||||||
@ -62,9 +64,7 @@ http.createServer(le.middleware(redirectHttps));
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var app = require('express')();
 | 
					var app = require('express')();
 | 
				
			||||||
var httpsOptions = { SNICallback: le.sni.callback };
 | 
					https.createServer(le.tlsOptions, le.middleware(app)).listen(443);
 | 
				
			||||||
httpsOptions = require('localhost.daplie.com-certificates').merge(httpsOptions);
 | 
					 | 
				
			||||||
https.createServer(dummyCerts, le.middleware(app)).listen(443);
 | 
					 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
You can also provide a thunk-style `getCertificates(domain, certs, cb)`.
 | 
					You can also provide a thunk-style `getCertificates(domain, certs, cb)`.
 | 
				
			||||||
@ -78,8 +78,8 @@ Standalone
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var leSni = require('le-sni-auto').create({
 | 
					var leSni = require('le-sni-auto').create({
 | 
				
			||||||
  renewWithin: 10 * 24 * 60 * 60 1000       // do not renew prior to 10 days before expiration
 | 
					  renewWithin: 14 * 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
 | 
					, 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
 | 
					  // key (privkey.pem) and cert (cert.pem + chain.pem) will be provided by letsencrypt
 | 
				
			||||||
, tlsOptions: { rejectUnauthorized: true, requestCert: false, ca: null, crl: null }
 | 
					, 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
 | 
					var tlsOptions = {
 | 
				
			||||||
// (because default certificates are required as a fallback)
 | 
					 | 
				
			||||||
var httpsOptions = require('localhost.daplie.com-certificates').merge({
 | 
					 | 
				
			||||||
  SNICallback: leSni.sniCallback
 | 
					  SNICallback: leSni.sniCallback
 | 
				
			||||||
});
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
https.createServer(httpsOptions, app);
 | 
					https.createServer(tlsOptions, app);
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
You can also provide a thunk-style `getCertificates(domain, certs, cb)`.
 | 
					You can also provide a thunk-style `getCertificates(domain, certs, cb)`.
 | 
				
			||||||
@ -112,6 +110,7 @@ API
 | 
				
			|||||||
  * `renewBy` (default 2 days, min 12 hours)
 | 
					  * `renewBy` (default 2 days, min 12 hours)
 | 
				
			||||||
* `sniCallback(domain, cb)`
 | 
					* `sniCallback(domain, cb)`
 | 
				
			||||||
* `cacheCerts(certs)`
 | 
					* `cacheCerts(certs)`
 | 
				
			||||||
 | 
					* `uncacheDomain(domain)`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.renewWithin
 | 
					.renewWithin
 | 
				
			||||||
-----------
 | 
					-----------
 | 
				
			||||||
@ -140,22 +139,22 @@ You would set this to `10 * 24 * 60 * 60 * 1000`.
 | 
				
			|||||||
.sniCallback()
 | 
					.sniCallback()
 | 
				
			||||||
-----------
 | 
					-----------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
This gets passed to `https.createServer(httpsOptions, app)` as `httpsOptions.SNICallback`.
 | 
					This gets passed to `https.createServer(tlsOptions, app)` as `tlsOptions.SNICallback`.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```javascript
 | 
					```javascript
 | 
				
			||||||
var leSni = require('le-sni-auto').create({
 | 
					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
 | 
					  SNICallback: leSni.sniCallback
 | 
				
			||||||
});
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function app(req, res) {
 | 
					function app(req, res) {
 | 
				
			||||||
  res.end("Hello, World!");
 | 
					  res.end("Hello, World!");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
https.createServer(httpsOptions, app);
 | 
					https.createServer(tlsOptions, app);
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.cacheCerts()
 | 
					.cacheCerts()
 | 
				
			||||||
@ -164,7 +163,8 @@ https.createServer(httpsOptions, app);
 | 
				
			|||||||
Manually load a certificate into the cache.
 | 
					Manually load a certificate into the cache.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
This is useful in a cluster environment where the master
 | 
					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({
 | 
					leSni.cacheCerts({
 | 
				
			||||||
@ -174,5 +174,21 @@ leSni.cacheCerts({
 | 
				
			|||||||
, altnames: [ 'example.com', 'www.example.com' ]
 | 
					, altnames: [ 'example.com', 'www.example.com' ]
 | 
				
			||||||
, issuedAt: 1470975565000
 | 
					, issuedAt: 1470975565000
 | 
				
			||||||
, expiresAt: 1478751565000
 | 
					, 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';
 | 
					'use strict';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var DAY = 24 * 60 * 60 * 1000;
 | 
					var DAY = 24 * 60 * 60 * 1000;
 | 
				
			||||||
 | 
					var HOUR = 60 * 60 * 1000;
 | 
				
			||||||
var MIN = 60 * 1000;
 | 
					var MIN = 60 * 1000;
 | 
				
			||||||
var defaults = {
 | 
					var defaults = {
 | 
				
			||||||
  // don't renew before the renewWithin period
 | 
					  // don't renew before the renewWithin period
 | 
				
			||||||
  renewWithin: 7 * DAY
 | 
					  renewWithin: 30 * DAY
 | 
				
			||||||
, _renewWithinMin: 3 * DAY
 | 
					, _renewWithinMin: 3 * DAY
 | 
				
			||||||
  // renew before the renewBy period
 | 
					  // renew before the renewBy period
 | 
				
			||||||
, renewBy: 2 * DAY
 | 
					, renewBy: 21 * DAY
 | 
				
			||||||
, _renewByMin: Math.floor(DAY / 2)
 | 
					, _renewByMin: Math.floor(DAY / 2)
 | 
				
			||||||
  // just to account for clock skew really
 | 
					  // just to account for clock skew really
 | 
				
			||||||
, _dropDead: 5 * MIN
 | 
					, _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 }
 | 
					// autoSni = { renewWithin, renewBy, getCertificates, tlsOptions, _dbg_now }
 | 
				
			||||||
module.exports.create = function (autoSni) {
 | 
					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) { autoSni.renewWithin = autoSni.notBefore || defaults.renewWithin; }
 | 
				
			||||||
  if (autoSni.renewWithin < defaults._renewWithinMin) {
 | 
					  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) {
 | 
					  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 || {}; }
 | 
					  if (!autoSni.tlsOptions) { autoSni.tlsOptions = autoSni.httpsOptions || {}; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -63,7 +72,8 @@ module.exports.create = function (autoSni) {
 | 
				
			|||||||
        certs: certs
 | 
					        certs: certs
 | 
				
			||||||
      , tlsContext: 'string' === typeof certs.cert && tls.createSecureContext({
 | 
					      , tlsContext: 'string' === typeof certs.cert && tls.createSecureContext({
 | 
				
			||||||
          key: certs.privkey
 | 
					          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
 | 
					        , rejectUnauthorized: autoSni.tlsOptions.rejectUnauthorized
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        , requestCert: autoSni.tlsOptions.requestCert  // request peer verification
 | 
					        , requestCert: autoSni.tlsOptions.requestCert  // request peer verification
 | 
				
			||||||
@ -72,6 +82,7 @@ module.exports.create = function (autoSni) {
 | 
				
			|||||||
        }) || { '_fake_tls_context_': true }
 | 
					        }) || { '_fake_tls_context_': true }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      , subject: certs.subject
 | 
					      , subject: certs.subject
 | 
				
			||||||
 | 
					      , auto: 'undefined' === typeof certs.auto ? true : certs.auto
 | 
				
			||||||
        // stagger renewal time by a little bit of randomness
 | 
					        // stagger renewal time by a little bit of randomness
 | 
				
			||||||
      , renewAt: (certs.expiresAt - (autoSni.renewWithin - (autoSni._renewWindow * Math.random())))
 | 
					      , renewAt: (certs.expiresAt - (autoSni.renewWithin - (autoSni._renewWindow * Math.random())))
 | 
				
			||||||
        // err just barely on the side of safety
 | 
					        // 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
 | 
					    // automate certificate registration on request
 | 
				
			||||||
  , sniCallback: function (domain, cb) {
 | 
					  , sniCallback: function (domain, cb) {
 | 
				
			||||||
      var certMeta = autoSni._ipc[domain];
 | 
					      var certMeta = autoSni._ipc[domain];
 | 
				
			||||||
      var promise;
 | 
					      var promise;
 | 
				
			||||||
      var now = (autoSni._dbg_now || Date.now());
 | 
					      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);
 | 
					        //log(autoSni.debug, "LINK CERT", domain);
 | 
				
			||||||
        certMeta = autoSni._ipc[certMeta.subject];
 | 
					        certMeta = autoSni._ipc[certMeta.subject];
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
@ -104,21 +125,31 @@ module.exports.create = function (autoSni) {
 | 
				
			|||||||
      if (!certMeta) {
 | 
					      if (!certMeta) {
 | 
				
			||||||
        //log(autoSni.debug, "NO CERT", domain);
 | 
					        //log(autoSni.debug, "NO CERT", domain);
 | 
				
			||||||
        // we don't have a cert and must get one
 | 
					        // 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) {
 | 
					      else if (now >= certMeta.expiresNear) {
 | 
				
			||||||
        //log(autoSni.debug, "EXPIRED CERT");
 | 
					        //log(autoSni.debug, "EXPIRED CERT");
 | 
				
			||||||
        // we have a cert, but it's no good for the average user
 | 
					        // 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 {
 | 
					      } else {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // it's time to renew the cert
 | 
					        // it's time to renew the cert
 | 
				
			||||||
        if (now >= certMeta.renewAt) {
 | 
					        if (certMeta.auto && now >= certMeta.renewAt) {
 | 
				
			||||||
          //log(autoSni.debug, "RENEWABLE CERT");
 | 
					          //log(autoSni.debug, "RENEWABLE CERT");
 | 
				
			||||||
          // give the cert some time (2-5 min) to be validated and replaced before trying again
 | 
					          // 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());
 | 
					          certMeta.renewAt = (autoSni._dbg_now || Date.now()) + (2 * MIN) + (3 * MIN * Math.random());
 | 
				
			||||||
          // let the update happen in the background
 | 
					          // 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
 | 
					        // return the valid cert right away
 | 
				
			||||||
@ -127,12 +158,14 @@ module.exports.create = function (autoSni) {
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // promise the non-existent or expired cert
 | 
					      // promise the non-existent or expired cert
 | 
				
			||||||
      promise.then(autoSni.cacheCerts).then(function (certMeta) {
 | 
					      promise.then(function (certMeta) {
 | 
				
			||||||
        cb(null, certMeta.tlsContext);
 | 
					        cb(null, certMeta.tlsContext);
 | 
				
			||||||
      }, function (err) {
 | 
					      }, function (err) {
 | 
				
			||||||
        console.error('ERROR in le-sni-auto:');
 | 
					        // console.error('ERROR in le-sni-auto:');
 | 
				
			||||||
        console.error(err.stack || err);
 | 
					        // console.error(err.stack || err);
 | 
				
			||||||
        cb(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",
 | 
					  "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",
 | 
					  "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",
 | 
					  "main": "index.js",
 | 
				
			||||||
  "dependencies": {
 | 
					  "trulyOptionalDependencies": {
 | 
				
			||||||
    "bluebird": "^3.4.1"
 | 
					    "bluebird": "^3.5.1"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "devDependencies": {},
 | 
					  "devDependencies": {},
 | 
				
			||||||
  "scripts": {
 | 
					  "scripts": {
 | 
				
			||||||
@ -12,7 +13,7 @@
 | 
				
			|||||||
  },
 | 
					  },
 | 
				
			||||||
  "repository": {
 | 
					  "repository": {
 | 
				
			||||||
    "type": "git",
 | 
					    "type": "git",
 | 
				
			||||||
    "url": "git+https://github.com/Daplie/le-sni-auto.git"
 | 
					    "url": "https://git.coolaj86.com/coolaj86/le-sni-auto.js.git"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "keywords": [
 | 
					  "keywords": [
 | 
				
			||||||
    "le-sni",
 | 
					    "le-sni",
 | 
				
			||||||
@ -26,7 +27,6 @@
 | 
				
			|||||||
  "author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)",
 | 
					  "author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)",
 | 
				
			||||||
  "license": "(MIT OR Apache-2.0)",
 | 
					  "license": "(MIT OR Apache-2.0)",
 | 
				
			||||||
  "bugs": {
 | 
					  "bugs": {
 | 
				
			||||||
    "url": "https://github.com/Daplie/le-sni-auto/issues"
 | 
					    "url": "https://git.coolaj86.com/coolaj86/le-sni-auto.js/issues"
 | 
				
			||||||
  },
 | 
					  }
 | 
				
			||||||
  "homepage": "https://github.com/Daplie/le-sni-auto#readme"
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										143
									
								
								test.js
									
									
									
									
									
								
							
							
						
						
									
										143
									
								
								test.js
									
									
									
									
									
								
							@ -17,13 +17,19 @@ var CERT_2 = {
 | 
				
			|||||||
, subject: 'example.com'
 | 
					, subject: 'example.com'
 | 
				
			||||||
, altnames: ['example.com', 'www.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 count = 0;
 | 
				
			||||||
var expectedCount = 3;
 | 
					var expectedCount = 4;
 | 
				
			||||||
var tests = [
 | 
					var tests = [
 | 
				
			||||||
  function (domain, certs, cb) {
 | 
					  function (domain, certs, cb) {
 | 
				
			||||||
    count += 1;
 | 
					    count += 1;
 | 
				
			||||||
    console.log('#1 is 1 of 3');
 | 
					    console.log('#1 is 1 of 4');
 | 
				
			||||||
    if (!domain) {
 | 
					    if (!domain) {
 | 
				
			||||||
      throw new Error("should have a domain");
 | 
					      throw new Error("should have a domain");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -42,7 +48,7 @@ var tests = [
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
, function (domain, certs, cb) {
 | 
					, function (domain, certs, cb) {
 | 
				
			||||||
    count += 1;
 | 
					    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)
 | 
					    // NOTE: there's a very very small chance this will fail occasionally (if Math.random() < 0.01)
 | 
				
			||||||
    if (!certs) {
 | 
					    if (!certs) {
 | 
				
			||||||
      throw new Error("should have certs to renew (renewAt)");
 | 
					      throw new Error("should have certs to renew (renewAt)");
 | 
				
			||||||
@ -52,7 +58,7 @@ var tests = [
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
, function (domain, certs, cb) {
 | 
					, function (domain, certs, cb) {
 | 
				
			||||||
    count += 1;
 | 
					    count += 1;
 | 
				
			||||||
    console.log('#4 is 3 of 3');
 | 
					    console.log('#4 is 3 of 4');
 | 
				
			||||||
    if (!certs) {
 | 
					    if (!certs) {
 | 
				
			||||||
      throw new Error("should have certs to renew (expiresNear)");
 | 
					      throw new Error("should have certs to renew (expiresNear)");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -63,11 +69,24 @@ var tests = [
 | 
				
			|||||||
    console.log('#5 should NOT be called');
 | 
					    console.log('#5 should NOT be called');
 | 
				
			||||||
    throw new Error("Should not call register renew a certificate with more than 10 days left");
 | 
					    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) {
 | 
					].map(function (fn) {
 | 
				
			||||||
  return require('bluebird').promisify(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({
 | 
					var leSni = require('./').create({
 | 
				
			||||||
  notBefore: NOT_BEFORE
 | 
					  notBefore: NOT_BEFORE
 | 
				
			||||||
, notAfter: NOT_AFTER
 | 
					, notAfter: NOT_AFTER
 | 
				
			||||||
@ -75,10 +94,16 @@ var leSni = require('./').create({
 | 
				
			|||||||
, _dbg_now: START_DAY
 | 
					, _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) {
 | 
					leSni.sniCallback('example.com', function (err, tlsContext) {
 | 
				
			||||||
  if (err) { throw err; }
 | 
					  if (err) { throw err; }
 | 
				
			||||||
  if (!tlsContext._fake_tls_context_) {
 | 
					  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();
 | 
					  leSni.getCertificatesAsync = tests.shift();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -88,7 +113,63 @@ leSni.sniCallback('example.com', function (err, tlsContext) {
 | 
				
			|||||||
  leSni.sniCallback('example.com', function (err, tlsContext) {
 | 
					  leSni.sniCallback('example.com', function (err, tlsContext) {
 | 
				
			||||||
    if (err) { throw err; }
 | 
					    if (err) { throw err; }
 | 
				
			||||||
    if (!tlsContext._fake_tls_context_) {
 | 
					    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();
 | 
					            leSni.getCertificatesAsync = tests.shift();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -98,44 +179,26 @@ leSni.sniCallback('example.com', function (err, tlsContext) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            leSni.sniCallback('example.com', function (err, tlsContext) {
 | 
					            leSni.sniCallback('example.com', function (err, tlsContext) {
 | 
				
			||||||
      if (err) { throw err; }
 | 
					 | 
				
			||||||
              if (!tlsContext._fake_tls_context_) {
 | 
					              if (!tlsContext._fake_tls_context_) {
 | 
				
			||||||
        throw new Error("Did not return tlsContext 2");
 | 
					                throw new Error("Did not return tlsContext #7");
 | 
				
			||||||
      }
 | 
					              }
 | 
				
			||||||
      leSni.getCertificatesAsync = tests.shift();
 | 
					
 | 
				
			||||||
 | 
					              if (expectedCount !== count) {
 | 
				
			||||||
      leSni._dbg_now = EXPIRES_AT;
 | 
					                throw new Error("getCertificate only called " + count + " times");
 | 
				
			||||||
 | 
					              }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					              if (expectedShared !== shared) {
 | 
				
			||||||
 | 
					                throw new Error("wrongly used only " + shared + " shared promises");
 | 
				
			||||||
      leSni.sniCallback('example.com', function (err, tlsContext) {
 | 
					              }
 | 
				
			||||||
        if (err) { throw err; }
 | 
					
 | 
				
			||||||
        if (!tlsContext._fake_tls_context_) {
 | 
					              if (tests.length) {
 | 
				
			||||||
          throw new Error("Did not return tlsContext 2");
 | 
					                throw new Error("some test functions not run");
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        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");
 | 
					 | 
				
			||||||
              }
 | 
					              }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
          if (expectedCount === count && !tests.length) {
 | 
					 | 
				
			||||||
              console.log('PASS');
 | 
					              console.log('PASS');
 | 
				
			||||||
            return;
 | 
					 | 
				
			||||||
          }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
          throw new Error("only " + count + " of the register getCertificate were called");
 | 
					 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
 | 
					          });
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user