Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap certificates in running server after renewal. #1667

Merged
merged 1 commit into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function startHttpsGateway() {
});
}));

return Promise.all(promises);
return Promise.all(promises).then(() => servers.https);
}

function startHttpGateway() {
Expand Down Expand Up @@ -307,9 +307,10 @@ function startGateway() {
return startHttpGateway();
}

return startHttpsGateway().then(() => {
return startHttpsGateway().then((server) => {
TunnelService.hasTunnelToken().then(function(result) {
if (result) {
TunnelService.setServerHandle(server);
TunnelService.start();
}
});
Expand Down Expand Up @@ -341,7 +342,9 @@ if (config.get('cli')) {
// function to stop running server and start https
TunnelService.switchToHttps = function() {
stopHttpGateway();
startHttpsGateway();
startHttpsGateway().then((server) => {
TunnelService.setServerHandle(server);
});
};

// This part starts our Service Discovery process.
Expand Down
11 changes: 10 additions & 1 deletion src/certificate-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,10 @@ async function register(email, reclamationToken, subdomain, fulldomain,

/**
* Try to renew the certificates associated with this domain.
*
* @param {Object} server - HTTPS server handle
*/
async function renew() {
async function renew(server) {
if (DEBUG) {
console.debug('Starting renewal.');
}
Expand Down Expand Up @@ -259,6 +261,13 @@ async function renew() {
}

writeCertificates(results);

if (server) {
const ctx = server._sharedCreds.context;
ctx.setCert(results.cert);
ctx.setKey(results.privkey);
ctx.addCACert(results.chain);
}
} catch (err) {
console.error('Renewal failed:', err);
}
Expand Down
13 changes: 10 additions & 3 deletions src/ssltunnel.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const TunnelService = {
connected: new Deferred(),
pingInterval: null,
renewInterval: null,
server: null,

/*
* Router middleware to check if we have a ssl tunnel set.
Expand Down Expand Up @@ -65,6 +66,11 @@ const TunnelService = {
}
},

// Set a handle for the running https server, used when renewing certificates
setServerHandle: function(server) {
this.server = server;
},

// method that starts the client if the box has a registered tunnel
start: function(response, urlredirect) {
Settings.get('tunneltoken').then((result) => {
Expand Down Expand Up @@ -120,9 +126,10 @@ const TunnelService = {
PushService.init(`https://${endpoint}`);

// Try to renew certificates immediately, then daily.
CertificateManager.renew().then(() => {
this.renewInterval =
setInterval(CertificateManager.renew, 24 * 60 * 60 * 1000);
CertificateManager.renew(this.server).then(() => {
this.renewInterval = setInterval(() => {
CertificateManager.renew(this.server);
}, 24 * 60 * 60 * 1000);
});
}).catch(() => {});
} else {
Expand Down