RUBY-311 ensure that ReplSetConnection connects to replica set members only.

This commit is contained in:
Kyle Banker 2011-08-26 12:40:13 -04:00
parent 85a53571bc
commit 8db62d2cbf
2 changed files with 37 additions and 11 deletions

View File

@ -80,11 +80,14 @@ module Mongo
self.config = self.connection['admin'].command({:ismaster => 1}, :socket => self.socket)
if self.config['msg'] && @logger
self.connection.logger.warn("MONGODB #{config['msg']}")
self.connection.log(:warn, "#{config['msg']}")
end
check_set_name
rescue OperationFailure, SocketError, SystemCallError, IOError => ex
check_set_membership(config)
check_set_name(config)
rescue ReplicaSetConnectionError, OperationFailure, SocketError, SystemCallError, IOError => ex
self.connection.log(:warn, "Attempted connection to node #{host_string} raised " +
"#{ex.class}: #{ex.message}")
return nil
end
@ -140,16 +143,25 @@ module Mongo
[host, port]
end
# Make sure that we're connected to the expected replica set.
def check_set_name
# Ensure that this node is a member of a replica set.
def check_set_membership(config)
if !config['hosts']
message = "Will not connect to #{host_string} because it's not a member " +
"of a replica set."
raise ReplicaSetConnectionError, message
end
end
# Ensure that this node is part of a replica set of the expected name.
def check_set_name(config)
if self.connection.replica_set_name
if !self.config['setName']
self.connection.logger.warn("MONGODB [warning] could not verify replica set name " +
if !config['setName']
self.connection.log(:warn, "Could not verify replica set name for member #{host_string} " +
"because ismaster does not return name in this version of MongoDB")
elsif self.connection.replica_set_name != self.config['setName']
raise ReplicaSetConnectionError,
"Attempting to connect to replica set '#{config['setName']}' " +
elsif self.connection.replica_set_name != config['setName']
message = "Attempting to connect to replica set '#{config['setName']}' on member #{host_string} " +
"but expected '#{self.connection.replica_set_name}'"
raise ReplicaSetConnectionError, message
end
end
end

View File

@ -3,7 +3,21 @@ require './test/test_helper'
class NodeTest < Test::Unit::TestCase
def setup
@connection = mock()
@connection = stub()
end
should "refuse to connect to node without 'hosts' key" do
node = Node.new(@connection, ['localhost', 27017])
TCPSocket.stubs(:new).returns(new_mock_socket)
admin_db = new_mock_db
admin_db.stubs(:command).returns({'ok' => 1, 'ismaster' => 1})
@connection.stubs(:[]).with('admin').returns(admin_db)
@connection.stubs(:connect_timeout).returns(nil)
@connection.expects(:log)
assert node.connect
assert node.set_config
end
should "load a node from an array" do