Skip to content

Commit 2437eae

Browse files
nomura yoshihiroluin
nomura yoshihiro
authored andcommitted
feat(sentinel): support Sentinel instances with authentication. (#817)
1 parent 7684b66 commit 2437eae

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ redis.set('foo', 'bar');
673673
The arguments passed to the constructor are different from the ones you use to connect to a single node, where:
674674

675675
* `name` identifies a group of Redis instances composed of a master and one or more slaves (`mymaster` in the example);
676+
* `sentinelPassword` (optional) password for Sentinel instances.
676677
* `sentinels` are a list of sentinels to connect to. The list does not need to enumerate all your sentinel instances, but a few so that if one is down the client will try the next one.
677678
* `role` (optional) with a value of `slave` will return a random slave from the Sentinel group.
678679
* `preferredSlaves` (optional) can be used to prefer a particular slave or set of slaves based on priority. It accepts a function or array.

lib/connectors/SentinelConnector/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type PreferredSlaves =
2525
interface ISentinelConnectionOptions extends ITcpConnectionOptions {
2626
role: 'master' | 'slave'
2727
name: 'string'
28+
sentinelPassword?: 'string'
2829
sentinels: Array<ISentinelAddress>
2930
sentinelRetryStrategy?: (retryAttempts: number) => number
3031
preferredSlaves?: PreferredSlaves
@@ -219,6 +220,7 @@ export default class SentinelConnector extends AbstractConnector {
219220
var client = new Redis({
220221
port: endpoint.port || 26379,
221222
host: endpoint.host,
223+
password: this.options.sentinelPassword || null,
222224
family: endpoint.family || (isIIpcConnectionOptions(this.options) ? undefined : this.options.family),
223225
tls: this.options.sentinelTLS,
224226
retryStrategy: null,

test/functional/sentinel.js

+26
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,33 @@ describe('sentinel', function () {
186186
name: 'master'
187187
});
188188
});
189+
it('should connect to sentinel with authentication successfully', function (done) {
190+
var authed = false;
191+
var redisServer = new MockServer('17380', function (argv) {
192+
if (argv[0] === 'auth' && argv[1] === 'pass') {
193+
authed = true;
194+
} else if (argv[0] === 'get' && argv[1] === 'foo') {
195+
expect(authed).to.eql(true);
196+
redisServer.disconnect();
197+
done();
198+
}
199+
})
200+
var sentinel = new MockServer(27379, function (argv) {
201+
if (argv[0] === 'sentinel' && argv[1] === 'get-master-addr-by-name') {
202+
sentinel.disconnect(done);
203+
return ['127.0.0.1', '17380'];
204+
}
205+
});
189206

207+
var redis = new Redis({
208+
sentinelPassword: 'pass',
209+
sentinels: [
210+
{ host: '127.0.0.1', port: '27379' }
211+
],
212+
name: 'master'
213+
});
214+
redis.get('foo').catch(function () {});
215+
});
190216
});
191217

192218
describe('master', function () {

0 commit comments

Comments
 (0)