Skip to content

Commit 4c745b1

Browse files
committed
close connection when redis returns WRONGPASS
1 parent 7655bb8 commit 4c745b1

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

lib/redis_client.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,9 @@ def connect
849849
connect_error = CannotConnectError.with_config(error.message, config)
850850
connect_error.set_backtrace(error.backtrace)
851851
raise connect_error
852+
rescue AuthenticationError
853+
@raw_connection&.close
854+
raise
852855
rescue CommandError => error
853856
if error.message.match?(/ERR unknown command ['`]HELLO['`]/)
854857
raise UnsupportedServer,

test/shared/redis_client_tests.rb

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,12 @@ def test_command_missing
328328
end
329329

330330
def test_authentication
331-
@redis.call("ACL", "SETUSER", "AzureDiamond", ">hunter2", "on", "+PING")
331+
@redis.call("ACL", "DELUSER", "AzureDiamond")
332+
@redis.call("ACL", "SETUSER", "AzureDiamond", ">hunter2", "on", "+PING", "+CLIENT")
333+
@redis.call("ACL", "DELUSER", "backup_admin")
334+
@redis.call("ACL", "SETUSER", "backup_admin", ">hunter2", "on", "~*", "&*", "+@all")
335+
backup = new_client(username: "backup_admin", password: "hunter2")
336+
backup.call("ACL", "SETUSER", "default", "off")
332337

333338
client = new_client(username: "AzureDiamond", password: "hunter2")
334339
assert_equal "PONG", client.call("PING")
@@ -337,10 +342,57 @@ def test_authentication
337342
client.call("GET", "foo")
338343
end
339344

345+
# Wrong password
340346
client = new_client(username: "AzureDiamond", password: "trolilol")
341-
assert_raises RedisClient::AuthenticationError do
347+
error = assert_raises RedisClient::AuthenticationError do
342348
client.call("PING")
343349
end
350+
assert_match(/WRONGPASS invalid username-password pair/, error.message)
351+
352+
# The same error is raised, this shows that the client retried AUTH and didn't fall back to the default user
353+
error = assert_raises RedisClient::AuthenticationError do
354+
client.call("PING")
355+
end
356+
assert_match(/WRONGPASS invalid username-password pair/, error.message)
357+
358+
# Correct password, but user disabled
359+
backup.call("ACL", "SETUSER", "AzureDiamond", "<hunter2", ">trolilol", "off")
360+
error = assert_raises RedisClient::AuthenticationError do
361+
client.call_once("PING")
362+
end
363+
assert_match(/WRONGPASS invalid username-password pair/, error.message)
364+
365+
# Correct password, user enabled
366+
backup.call("ACL", "SETUSER", "AzureDiamond", "on")
367+
assert_equal "PONG", client.call_once("PING")
368+
assert_match(/user=AzureDiamond/, client.call("CLIENT", "INFO"))
369+
370+
# Wrong username
371+
client = new_client(username: "GreenOpal", password: "trolilol")
372+
error = assert_raises RedisClient::AuthenticationError do
373+
client.call("PING")
374+
end
375+
assert_match(/WRONGPASS invalid username-password pair/, error.message)
376+
ensure
377+
backup.call("ACL", "SETUSER", "default", "on")
378+
end
379+
380+
def test_noauth
381+
@redis.call("ACL", "DELUSER", "AzureDiamond")
382+
@redis.call("ACL", "SETUSER", "AzureDiamond", ">hunter2", "on", "~*", "&*", "+@all")
383+
backup = new_client(username: "AzureDiamond", password: "hunter2")
384+
backup.call("ACL", "SETUSER", "default", "off")
385+
386+
client = new_client(protocol: 2)
387+
error = assert_raises RedisClient::CommandError do
388+
client.call("PING")
389+
end
390+
assert_match(/NOAUTH Authentication required/, error.message)
391+
392+
backup.call("ACL", "SETUSER", "default", "on")
393+
client.call("PING")
394+
ensure
395+
backup.call("ACL", "SETUSER", "default", "on")
344396
end
345397

346398
def test_transaction

0 commit comments

Comments
 (0)