forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tls: support changing credentials dynamically
This commit adds a setSecureContext() method to TLS servers. In order to maintain backwards compatibility, the method takes the options needed to create a new SecureContext, rather than an instance of SecureContext. Fixes: nodejs#4464 Refs: nodejs#10349 Refs: nodejs/help#603 Refs: nodejs#15115 PR-URL: nodejs#23644 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
- Loading branch information
1 parent
f54db0b
commit f47e208
Showing
3 changed files
with
215 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const https = require('https'); | ||
const fixtures = require('../common/fixtures'); | ||
const credentialOptions = [ | ||
{ | ||
key: fixtures.readKey('agent1-key.pem'), | ||
cert: fixtures.readKey('agent1-cert.pem'), | ||
ca: fixtures.readKey('ca1-cert.pem') | ||
}, | ||
{ | ||
key: fixtures.readKey('agent2-key.pem'), | ||
cert: fixtures.readKey('agent2-cert.pem'), | ||
ca: fixtures.readKey('ca2-cert.pem') | ||
} | ||
]; | ||
let requestsCount = 0; | ||
let firstResponse; | ||
|
||
const server = https.createServer(credentialOptions[0], (req, res) => { | ||
requestsCount++; | ||
|
||
if (requestsCount === 1) { | ||
firstResponse = res; | ||
firstResponse.write('multi-'); | ||
return; | ||
} else if (requestsCount === 3) { | ||
firstResponse.write('success-'); | ||
} | ||
|
||
res.end('success'); | ||
}); | ||
|
||
server.listen(0, common.mustCall(async () => { | ||
const { port } = server.address(); | ||
const firstRequest = makeRequest(port); | ||
|
||
assert.strictEqual(await makeRequest(port), 'success'); | ||
|
||
server.setSecureContext(credentialOptions[1]); | ||
firstResponse.write('request-'); | ||
await assert.rejects(async () => { | ||
await makeRequest(port); | ||
}, /^Error: self signed certificate$/); | ||
|
||
server.setSecureContext(credentialOptions[0]); | ||
assert.strictEqual(await makeRequest(port), 'success'); | ||
|
||
server.setSecureContext(credentialOptions[1]); | ||
firstResponse.end('fun!'); | ||
await assert.rejects(async () => { | ||
await makeRequest(port); | ||
}, /^Error: self signed certificate$/); | ||
|
||
assert.strictEqual(await firstRequest, 'multi-request-success-fun!'); | ||
server.close(); | ||
})); | ||
|
||
function makeRequest(port) { | ||
return new Promise((resolve, reject) => { | ||
const options = { | ||
rejectUnauthorized: true, | ||
ca: credentialOptions[0].ca, | ||
servername: 'agent1' | ||
}; | ||
|
||
https.get(`https://localhost:${port}`, options, (res) => { | ||
let response = ''; | ||
|
||
res.setEncoding('utf8'); | ||
|
||
res.on('data', (chunk) => { | ||
response += chunk; | ||
}); | ||
|
||
res.on('end', common.mustCall(() => { | ||
resolve(response); | ||
})); | ||
}).on('error', (err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} |