Skip to content

Commit 625f14e

Browse files
author
Ruben Bridgewater
committed
Fix address always set to 127.0.0.1:6379 in case the host/port is set in the tls options
1 parent f500398 commit 625f14e

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ function RedisClient (options, stream) {
5050
EventEmitter.call(this);
5151
var cnx_options = {};
5252
var self = this;
53+
/* istanbul ignore next: travis does not work with stunnel atm. Therefore the tls tests are skipped on travis */
54+
for (var tls_option in options.tls) { // jshint ignore: line
55+
cnx_options[tls_option] = options.tls[tls_option];
56+
// Copy the tls options into the general options to make sure the address is set right
57+
if (tls_option === 'port' || tls_option === 'host' || tls_option === 'path' || tls_option === 'family') {
58+
options[tls_option] = options.tls[tls_option];
59+
}
60+
}
5361
if (stream) {
5462
// The stream from the outside is used so no connection from this side is triggered but from the server this client should talk to
5563
// Reconnect etc won't work with this. This requires monkey patching to work, so it is not officially supported
@@ -64,10 +72,6 @@ function RedisClient (options, stream) {
6472
cnx_options.family = (!options.family && net.isIP(cnx_options.host)) || (options.family === 'IPv6' ? 6 : 4);
6573
this.address = cnx_options.host + ':' + cnx_options.port;
6674
}
67-
/* istanbul ignore next: travis does not work with stunnel atm. Therefore the tls tests are skipped on travis */
68-
for (var tls_option in options.tls) { // jshint ignore: line
69-
cnx_options[tls_option] = options.tls[tls_option];
70-
}
7175
// Warn on misusing deprecated functions
7276
if (typeof options.retry_strategy === 'function') {
7377
if ('max_attempts' in options) {

test/tls.spec.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ describe('TLS connection tests', function () {
6060
tls: tls_options
6161
});
6262
var time = 0;
63+
assert.strictEqual(client.address, '127.0.0.1:' + tls_port);
6364

6465
client.once('ready', function () {
6566
helper.killConnection(client);
@@ -87,18 +88,20 @@ describe('TLS connection tests', function () {
8788

8889
describe('when not connected', function () {
8990

90-
it('connect with host and port provided in the options object', function (done) {
91+
it('connect with host and port provided in the tls object', function (done) {
9192
if (skip) this.skip();
93+
var tls = utils.clone(tls_options);
94+
tls.port = tls_port;
95+
tls.host = 'localhost';
9296
client = redis.createClient({
93-
host: 'localhost',
9497
connect_timeout: 1000,
95-
port: tls_port,
96-
tls: tls_options
98+
tls: tls
9799
});
98100

99101
// verify connection is using TCP, not UNIX socket
100102
assert.strictEqual(client.connection_options.host, 'localhost');
101103
assert.strictEqual(client.connection_options.port, tls_port);
104+
assert.strictEqual(client.address, 'localhost:' + tls_port);
102105
assert(client.stream.encrypted);
103106

104107
client.set('foo', 'bar');
@@ -115,6 +118,7 @@ describe('TLS connection tests', function () {
115118
port: tls_port,
116119
tls: faulty_cert
117120
});
121+
assert.strictEqual(client.address, 'localhost:' + tls_port);
118122
client.on('error', function (err) {
119123
assert(/DEPTH_ZERO_SELF_SIGNED_CERT/.test(err.code || err.message), err);
120124
client.end(true);

0 commit comments

Comments
 (0)