Skip to content

Commit ac560c2

Browse files
authored
Merge pull request #810 from estolfo/RUBY-1122-handle-auth-failure
RUBY-1122 Mark a server as unknown when auth fails
2 parents 62db30f + 4e8a74d commit ac560c2

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

lib/mongo/server.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class Server
6363
:replica_set_name,
6464
:secondary?,
6565
:standalone?,
66-
:unknown?
66+
:unknown?,
67+
:unknown!
6768

6869
# Is this server equal to another?
6970
#
@@ -229,5 +230,24 @@ def reconnect!
229230
def with_connection(&block)
230231
pool.with_connection(&block)
231232
end
233+
234+
# Handle authentication failure.
235+
#
236+
# @example Handle possible authentication failure.
237+
# server.handle_auth_failure! do
238+
# Auth.get(user).login(self)
239+
# end
240+
#
241+
# @raise [ Auth::Unauthorized ] If the authentication failed.
242+
#
243+
# @return [ Object ] The result of the block execution.
244+
#
245+
# @since 2.3.0
246+
def handle_auth_failure!
247+
yield
248+
rescue Auth::Unauthorized
249+
unknown!
250+
raise
251+
end
232252
end
233253
end

lib/mongo/server/connection.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ def deliver(messages)
162162
def authenticate!
163163
if options[:user]
164164
user = Auth::User.new(Options::Redacted.new(:auth_mech => default_mechanism).merge(options))
165-
Auth.get(user).login(self)
165+
@server.handle_auth_failure! do
166+
Auth.get(user).login(self)
167+
end
166168
end
167169
end
168170

spec/mongo/server/connection_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@
113113
TEST_OPTIONS.merge(
114114
:user => 'notauser',
115115
:password => 'password',
116-
:database => TEST_DB )
116+
:database => TEST_DB,
117+
:heartbeat_frequency => 30)
117118
)
118119
end
119120

@@ -130,7 +131,6 @@
130131
end
131132

132133
it 'marks the server as unknown' do
133-
pending 'Server must be set as unknown'
134134
expect(server).to be_unknown
135135
end
136136
end

0 commit comments

Comments
 (0)