Skip to content

Commit 8e9183a

Browse files
authored
Update sentinel auth with explicit kwargs (#1221)
* updates README with new sentinel auth params * bump redis-client from >=0.16.0 to >=0.17.0 * update specs to use explicit sentinel credentials * drops sentinel preparation config * updates CHANGELOG
1 parent 01de51a commit 8e9183a

File tree

6 files changed

+31
-32
lines changed

6 files changed

+31
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Unreleased
22

33
- Fix `Redis#without_reconnect` for sentinel clients. Fix #1212.
4+
- Add `sentinel_username`, `sentinel_password` for sentinel clients. Bump `redis-client` to `>=0.17.0`. See #1213
45

56
# 5.0.7
67

README.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,29 +120,39 @@ but a few so that if one is down the client will try the next one. The client
120120
is able to remember the last Sentinel that was able to reply correctly and will
121121
use it for the next requests.
122122

123-
To [authenticate](https://redis.io/docs/management/sentinel/#configuring-sentinel-instances-with-authentication) Sentinel itself, you can specify the `username` and `password` options per instance. Exclude the `username` option if you're using password-only authentication.
123+
To [authenticate](https://redis.io/docs/management/sentinel/#configuring-sentinel-instances-with-authentication) Sentinel itself, you can specify the `sentinel_username` and `sentinel_password`. Exclude the `sentinel_username` option if you're using password-only authentication.
124124

125125
```ruby
126-
SENTINELS = [{ host: '127.0.0.1', port: 26380, username: 'appuser', password: 'mysecret' },
127-
{ host: '127.0.0.1', port: 26381, username: 'appuser', password: 'mysecret' }]
126+
SENTINELS = [{ host: '127.0.0.1', port: 26380},
127+
{ host: '127.0.0.1', port: 26381}]
128128

129-
redis = Redis.new(name: 'mymaster', sentinels: SENTINELS, role: :master)
129+
redis = Redis.new(name: 'mymaster', sentinels: SENTINELS, sentinel_username: 'appuser', sentinel_password: 'mysecret', role: :master)
130130
```
131131

132-
If you specify a username and/or password at the top level for your main Redis instance, Sentinel will default to using those credentials. You can pass nil or override them for each sentinel.
132+
If you specify a username and/or password at the top level for your main Redis instance, Sentinel *will not* using thouse credentials
133133

134134
```ruby
135135
# Use 'mysecret' to authenticate against the mymaster instance, but skip authentication for the sentinels:
136-
SENTINELS = [{ host: '127.0.0.1', port: 26380, password: nil },
137-
{ host: '127.0.0.1', port: 26381, password: nil }]
136+
SENTINELS = [{ host: '127.0.0.1', port: 26380 },
137+
{ host: '127.0.0.1', port: 26381 }]
138138

139139
redis = Redis.new(name: 'mymaster', sentinels: SENTINELS, role: :master, password: 'mysecret')
140140
```
141141

142-
Also the name can be passed as an url:
142+
So you have to provide Sentinel credential and Redis explictly even they are the same
143143

144144
```ruby
145-
redis = Redis.new(name: "redis://mymaster", sentinels: SENTINELS, role: :master)
145+
# Use 'mysecret' to authenticate against the mymaster instance and sentinel
146+
SENTINELS = [{ host: '127.0.0.1', port: 26380 },
147+
{ host: '127.0.0.1', port: 26381 }]
148+
149+
redis = Redis.new(name: 'mymaster', sentinels: SENTINELS, role: :master, password: 'mysecret', sentinel_password: 'mysecret')
150+
```
151+
152+
Also the `name`, `password`, `username` and `db` for Redis instance can be passed as an url:
153+
154+
```ruby
155+
redis = Redis.new(url: "redis://appuser:mysecret@mymaster/10", sentinels: SENTINELS, role: :master)
146156
```
147157

148158
## Cluster support

lib/redis.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,6 @@ def initialize_client(options)
137137
end
138138

139139
if options.key?(:sentinels)
140-
if url = options.delete(:url)
141-
uri = URI.parse(url)
142-
if !options.key?(:name) && uri.host
143-
options[:name] = uri.host
144-
end
145-
146-
if !options.key?(:password) && uri.password && !uri.password.empty?
147-
options[:password] = uri.password
148-
end
149-
150-
if !options.key?(:username) && uri.user && !uri.user.empty?
151-
options[:username] = uri.user
152-
end
153-
end
154-
155140
Client.sentinel(**options).new_client
156141
else
157142
Client.config(**options).new_client

redis.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ Gem::Specification.new do |s|
4545

4646
s.required_ruby_version = '>= 2.5.0'
4747

48-
s.add_runtime_dependency('redis-client', '>= 0.16.0')
48+
s.add_runtime_dependency('redis-client', '>= 0.17.0')
4949
end

test/sentinel/sentinel_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def test_sentinel_with_non_sentinel_options
205205
end
206206
end
207207

208-
assert_equal [%w[auth foo], %w[get-master-addr-by-name master1], ["sentinels", "master1"]], commands[:s1]
208+
assert_equal [%w[get-master-addr-by-name master1], ["sentinels", "master1"]], commands[:s1]
209209
assert_equal [%w[auth foo], %w[role]], commands[:m1]
210210
end
211211

@@ -252,8 +252,8 @@ def test_authentication_for_sentinel
252252

253253
RedisMock.start(master) do |master_port|
254254
RedisMock.start(sentinel.call(master_port)) do |sen_port|
255-
s = [{ host: '127.0.0.1', port: sen_port, password: 'foo' }]
256-
r = Redis.new(name: 'master1', sentinels: s, role: :master)
255+
s = [{ host: '127.0.0.1', port: sen_port }]
256+
r = Redis.new(name: 'master1', sentinels: s, role: :master, sentinel_password: 'foo')
257257
assert r.ping
258258
end
259259
end
@@ -308,8 +308,8 @@ def test_authentication_for_sentinel_and_redis
308308

309309
RedisMock.start(master) do |master_port|
310310
RedisMock.start(sentinel.call(master_port)) do |sen_port|
311-
s = [{ host: '127.0.0.1', port: sen_port, password: 'foo' }]
312-
r = Redis.new(name: 'master1', sentinels: s, role: :master, password: 'bar')
311+
s = [{ host: '127.0.0.1', port: sen_port }]
312+
r = Redis.new(name: 'master1', sentinels: s, role: :master, password: 'bar', sentinel_password: 'foo')
313313
assert r.ping
314314
end
315315
end
@@ -361,8 +361,8 @@ def test_authentication_with_acl
361361

362362
RedisMock.start(master) do |master_port|
363363
RedisMock.start(sentinel.call(master_port)) do |sen_port|
364-
s = [{ host: '127.0.0.1', port: sen_port, username: 'bob', password: 'foo' }]
365-
r = Redis.new(name: 'master1', sentinels: s, role: :master, username: 'alice', password: 'bar')
364+
s = [{ host: '127.0.0.1', port: sen_port }]
365+
r = Redis.new(name: 'master1', sentinels: s, role: :master, username: 'alice', password: 'bar', sentinel_username: 'bob', sentinel_password: 'foo')
366366
assert r.ping
367367
end
368368
end

test/support/conf/redis-7.2.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
appendonly no
2+
save ""
3+
enable-debug-command yes

0 commit comments

Comments
 (0)