-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#147 fix mTLS connection support, add tls tests
- Loading branch information
Showing
5 changed files
with
233 additions
and
24 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,190 @@ | ||
const vows = require('vows'); | ||
const assert = require('assert'); | ||
const selfsigned = require('selfsigned'); | ||
const tls = require('tls'); | ||
const winston = require('winston'); | ||
require('../lib/winston-syslog').Syslog; | ||
|
||
// Constants | ||
const HOST = 'localhost'; | ||
const PORT = 10514; | ||
|
||
// Certificate | ||
function generateCertificate() { | ||
// Generate server and client certificates | ||
const attributes = [{ name: 'commonName', value: 'localhost' }]; | ||
const x509 = selfsigned.generate(attributes, { | ||
days: 1, | ||
clientCertificate: true, | ||
extensions: [ | ||
{ | ||
name: 'keyUsage', | ||
keyCertSign: true, | ||
digitalSignature: true, | ||
nonRepudiation: true, | ||
keyEncipherment: true, | ||
dataEncipherment: true | ||
}, | ||
{ | ||
name: 'subjectAltName', | ||
altNames: [ | ||
{ | ||
type: 2, // DNS | ||
value: 'localhost' | ||
} | ||
] | ||
} | ||
] | ||
}); | ||
return x509; | ||
} | ||
|
||
const x509 = generateCertificate(); | ||
const validServerTLS = { | ||
key: x509.private, | ||
cert: x509.cert, | ||
ca: [x509.cert], | ||
rejectUnauthorized: true, | ||
requestCert: true | ||
}; | ||
|
||
const validClientTLS = { | ||
key: x509.clientprivate, | ||
cert: x509.clientcert, | ||
ca: [x509.cert], | ||
rejectUnauthorized: true | ||
}; | ||
|
||
const untrustedClientTLS = { | ||
...validClientTLS, | ||
ca: null | ||
}; | ||
|
||
const missingClientTLS = { | ||
ca: null, | ||
key: null, | ||
cert: null, | ||
rejectUnauthorized: false | ||
}; | ||
|
||
function wrapToPromise(target, event) { | ||
return new Promise((resolve) => { | ||
target.on(event, (...args) => resolve(...args)); | ||
}); | ||
} | ||
|
||
// Start TLS Server | ||
const serverEvents = { | ||
data: 'data', | ||
tlsClientError: 'tlsClientError', | ||
error: 'error', | ||
listening: 'listening', | ||
secureConnection: 'secureConnection' | ||
}; | ||
|
||
async function startServer({ host = HOST, port = PORT, tlsOpts } = {}) { | ||
const server = tls.createServer({ ...tlsOpts }); | ||
let clients = []; | ||
server.on('secureConnection', (client) => { | ||
clients.push(client); | ||
client.on('close', () => { | ||
clients = clients.filter((c) => c !== clients); | ||
}); | ||
client.on('data', (data) => { | ||
server.emit(serverEvents.data, data.toString()); | ||
}); | ||
}); | ||
server.forceClose = () => { | ||
clients.forEach((c) => c.destroy()); | ||
server.close(); | ||
}; | ||
server.listen({ host, port }); | ||
await wrapToPromise(server, serverEvents.listening); | ||
return server; | ||
} | ||
|
||
function initLogger({ host = HOST, port = PORT, tlsOpts } = {}) { | ||
const syslogOptions = { | ||
host, | ||
port, | ||
protocol: 'tls4', | ||
protocolOptions: { ...tlsOpts } | ||
}; | ||
const logger = winston.createLogger({ | ||
transports: [new winston.transports.Syslog(syslogOptions)] | ||
}); | ||
return logger; | ||
} | ||
|
||
const TEST_MESSAGE = 'Test Message'; | ||
const SYSLOG_FORMAT = `"message":"${TEST_MESSAGE}"`; | ||
|
||
let serverInstance; | ||
|
||
vows | ||
.describe('tls-connect') | ||
.addBatch({ | ||
'Trying to connect to a TLS server with mutual TLS': { | ||
'topic': function () { | ||
startServer({ tlsOpts: validServerTLS }).then((server) => { | ||
serverInstance = server; | ||
const promise = wrapToPromise(server, serverEvents.data); | ||
initLogger({ tlsOpts: validClientTLS }).info(TEST_MESSAGE); | ||
promise.then(msg => this.callback(null, msg)); | ||
}); | ||
}, | ||
'TLS server should receive log message': function (msg) { | ||
assert.include(msg, SYSLOG_FORMAT); | ||
}, | ||
'teardown': function () { | ||
serverInstance.forceClose(); | ||
} | ||
} | ||
}) | ||
.addBatch({ | ||
'Trying to connect to a TLS server with untrusted certificate': { | ||
'topic': function () { | ||
startServer({ tlsOpts: validServerTLS }).then((server) => { | ||
serverInstance = server; | ||
const promise = wrapToPromise(server, serverEvents.tlsClientError); | ||
const logger = initLogger({ tlsOpts: untrustedClientTLS }); | ||
logger.on('error', (loggerError) => { | ||
promise.then((serverError) => this.callback(null, loggerError, serverError)); | ||
}); | ||
logger.info(TEST_MESSAGE); | ||
}); | ||
}, | ||
'Client should refuse connection': function (e, loggerError, serverError) { | ||
assert.equal(loggerError.code, 'DEPTH_ZERO_SELF_SIGNED_CERT'); | ||
assert.include(loggerError.message, 'self signed certificate'); | ||
assert.equal(serverError.code, 'ECONNRESET'); | ||
assert.include(serverError.message, 'socket hang up'); | ||
}, | ||
'teardown': function () { | ||
serverInstance.forceClose(); | ||
} | ||
} | ||
}) | ||
.addBatch({ | ||
'Trying to connect to a TLS server without client certificate': { | ||
'topic': function () { | ||
startServer({ tlsOpts: validServerTLS }).then((server) => { | ||
serverInstance = server; | ||
const promise = wrapToPromise(server, serverEvents.tlsClientError); | ||
const logger = initLogger({ tlsOpts: missingClientTLS }); | ||
logger.on('error', (loggerError) => { | ||
promise.then((serverError) => this.callback(null, loggerError, serverError)); | ||
}); | ||
logger.info(TEST_MESSAGE); | ||
}); | ||
}, | ||
'Server should refuse connection': function (e, loggerError, serverError) { | ||
assert.equal(loggerError.code, 'ERR_SSL_TLSV13_ALERT_CERTIFICATE_REQUIRED'); | ||
assert.equal(serverError.code, 'ERR_SSL_PEER_DID_NOT_RETURN_A_CERTIFICATE'); | ||
}, | ||
'teardown': function () { | ||
serverInstance.forceClose(); | ||
} | ||
} | ||
}) | ||
.export(module); |