I'd like to update ssl certificates on node.js http2 server without restarting (to avoid any downtime). Also I don't want to use any 3rd party modules for this work. Only pure nodejs. Is it possible?
Right now when certificate about to expire, i just restarting the script.
const https = require('http2');
const server = https.createSecureServer({
ca: fs.readFileSync('chain.pem'),
cert: fs.readFileSync('cert.pem', 'utf8'),//fullchain
key: fs.readFileSync('privkey.pem', 'utf8'),
allowHTTP1: true,
},
I expect to be able to watch if cert files were updated (using fs.watch() for example), and to update certificates in http2 server on the fly...
You would only need to restart the Server if you manually generate the SSL certificate due to exception conditions such as changes in hostname or host IP in your etc/hosts.
Root certificate installation on Windows should never require a restart.
Use this procedure when you need to add or replace the SSL certificate for Insight. At the end of this procedure, you will need to restart the server, so you should perform this procedure during a time of low user activity.
As mentioned by Jake, setSecureContext() do the magic. Seems it can update certificate without breaking current connections. Something like:
setTimeout(function () {server.setSecureContext({
ca: fs.readFileSync('chain.pem'),
cert: fs.readFileSync('cert.pem', 'utf8'),//fullchain
key: fs.readFileSync('privkey.pem', 'utf8')
})},86400000)
Yes, you can just use sniCallBack():
const https = require('http2');
const server = https.createSecureServer({
ca: fs.readFileSync('chain.pem'),
cert: fs.readFileSync('cert.pem', 'utf8'),//fullchain
key: fs.readFileSync('privkey.pem', 'utf8'),
allowHTTP1: true,
SNICallback: (servername, cb) => {
// here you can even change up the `SecureContext`
// based on `servername` if you want
cb(null, server);
}
},
This may be a bit outdated so try it out and ask me if anything doesn't work because the solution source code that I found here is a bit different.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With