Skip to content

Commit bede82f

Browse files
author
Brandon Black
committed
RUBY-572 adding auth tests for replica sets and sharded clusters
1 parent 4bbf6bf commit bede82f

File tree

7 files changed

+143
-85
lines changed

7 files changed

+143
-85
lines changed

tasks/testing.rake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,15 @@ namespace :test do
7070
end
7171

7272
Rake::TestTask.new(:replica_set) do |t|
73-
t.test_files = FileList['test/replica_set/*_test.rb'] - [
73+
disabled = [
7474
'test/replica_set/complex_connect_test.rb',
7575
'test/replica_set/count_test.rb',
7676
'test/replica_set/read_preference_test.rb'
7777
]
78+
79+
disabled << 'test/replica_set/authentication_test.rb' if ENV['MONGOD']
80+
81+
t.test_files = FileList['test/replica_set/*_test.rb'] - disabled
7882
t.libs << 'test'
7983
#t.verbose = true
8084
#t.options = '-v'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'test_helper'
2+
require 'shared/authentication'
3+
4+
class AuthenticationTest < Test::Unit::TestCase
5+
include Mongo
6+
include AuthenticationTests
7+
8+
def setup
9+
@client = MongoClient.new
10+
@db = @client[MONGO_TEST_DB]
11+
end
12+
13+
def teardown
14+
@db['system.users'].remove
15+
end
16+
17+
def test_authenticate_with_connection_uri
18+
@db.add_user('eunice', 'uritest')
19+
assert MongoClient.from_uri("mongodb://eunice:uritest@#{host_port}/#{@db.name}")
20+
end
21+
end

test/functional/db_test.rb

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -131,47 +131,6 @@ def test_pk_factory_reset
131131
end
132132
end
133133

134-
def test_authenticate
135-
@@db.add_user('spongebob', 'squarepants')
136-
assert_raise Mongo::AuthenticationError do
137-
assert !@@db.authenticate('nobody', 'nopassword')
138-
end
139-
assert_raise Mongo::AuthenticationError do
140-
assert !@@db.authenticate('spongebob' , 'squareliederhosen')
141-
end
142-
assert @@db.authenticate('spongebob', 'squarepants')
143-
@@db.logout
144-
@@db.remove_user('spongebob')
145-
end
146-
147-
def test_authenticate_with_special_characters
148-
assert @@db.add_user('foo:bar', '@foo')
149-
assert @@db.authenticate('foo:bar', '@foo')
150-
@@db.logout
151-
@@db.remove_user('foo:bar')
152-
end
153-
154-
def test_authenticate_read_only
155-
@@db.add_user('joebob', 'user', true) # read-only user
156-
assert @@db.authenticate('joebob', 'user')
157-
@@db.logout
158-
@@db.remove_user('joebob')
159-
end
160-
161-
def test_authenticate_with_connection_uri
162-
@@db.add_user('spongebob', 'squarepants')
163-
assert Mongo::MongoClient.from_uri("mongodb://spongebob:squarepants@#{host_port}/#{@@db.name}")
164-
165-
assert_raise Mongo::AuthenticationError do
166-
client = Mongo::MongoClient.from_uri("mongodb://wrong:info@#{host_port}/#{@@db.name}")
167-
client['test']['foo'].find_one
168-
end
169-
end
170-
171-
def test_logout
172-
assert @@db.logout
173-
end
174-
175134
def test_command
176135
assert_raise OperationFailure do
177136
@@db.command({:non_command => 1}, :check_response => true)
@@ -249,25 +208,11 @@ def test_text_port_number_raises_no_errors
249208
db.collection('users').remove
250209
end
251210

252-
def test_user_management
253-
@@db.add_user("bob", "secret")
254-
assert @@db.authenticate("bob", "secret")
255-
@@db.logout
256-
assert @@db.remove_user("bob")
257-
assert_raise Mongo::AuthenticationError do
258-
@@db.authenticate("bob", "secret")
259-
end
260-
end
261-
262-
def test_remove_non_existant_user
263-
assert !@@db.remove_user("joe")
264-
end
265-
266211
def test_stored_function_management
267212
@@db.add_stored_function("sum", "function (x, y) { return x + y; }")
268213
assert_equal @@db.eval("return sum(2,3);"), 5
269214
assert @@db.remove_stored_function("sum")
270-
assert_raise OperationFailure do
215+
assert_raise OperationFailure do
271216
@@db.eval("return sum(2,3);")
272217
end
273218
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'test_helper'
2+
require 'shared/authentication'
3+
4+
class ReplicaSetAuthenticationTest < Test::Unit::TestCase
5+
include Mongo
6+
include AuthenticationTests
7+
8+
def setup
9+
ensure_cluster(:rs)
10+
@client = MongoReplicaSetClient.new(@rs.repl_set_seeds, :name => @rs.repl_set_name, :connect_timeout => 60)
11+
@db = @client[MONGO_TEST_DB]
12+
end
13+
14+
def teardown
15+
@db['system.users'].remove
16+
end
17+
18+
def test_authenticate_with_connection_uri
19+
@db.add_user('eunice', 'uritest')
20+
assert MongoReplicaSetClient.from_uri(
21+
"mongodb://eunice:uritest@#{@rs.repl_set_seeds.join(',')}/#{@db.name}?replicaSet=#{@rs.repl_set_name}")
22+
end
23+
end

test/shared/authentication.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module AuthenticationTests
2+
def test_add_user
3+
@db.add_user('bob','user')
4+
assert @db['system.users'].find_one({:user => 'bob'})
5+
end
6+
7+
def test_remove_user
8+
@db.remove_user('bob')
9+
assert_nil @db['system.users'].find_one({:user => 'bob'})
10+
end
11+
12+
def test_remove_non_existent_user
13+
assert_equal @db.remove_user('joe'), false
14+
end
15+
16+
def test_authenticate
17+
@db.add_user('peggy', 'user')
18+
assert @db.authenticate('peggy', 'user')
19+
@db.remove_user('peggy')
20+
@db.logout
21+
end
22+
23+
def test_authenticate_non_existent_user
24+
assert_raise Mongo::AuthenticationError do
25+
@db.authenticate('frank', 'thetank')
26+
end
27+
end
28+
29+
def test_logout
30+
@db.add_user('peggy', 'user')
31+
assert @db.authenticate('peggy', 'user')
32+
assert @db.logout
33+
@db.remove_user('peggy')
34+
end
35+
36+
def test_authenticate_with_special_characters
37+
assert @db.add_user('foo:bar','@foo')
38+
assert @db.authenticate('foo:bar','@foo')
39+
@db.remove_user('foo:bar')
40+
@db.logout
41+
end
42+
43+
def test_authenticate_read_only
44+
@db.add_user('randy', 'readonly', true)
45+
assert @db.authenticate('randy', 'readonly')
46+
@db.remove_user('randy')
47+
@db.logout
48+
end
49+
end

test/tools/keyfile.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line num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16+
Ot8mZoCXxmvgnnJEKaQLB6dsG3CBCcLuPb3gx6XwmO+u

test/tools/mongo_config.rb

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def debug(level, arg)
2424
#
2525
module Mongo
2626
class Config
27-
DEFAULT_BASE_OPTS = { :host => 'localhost', :dbpath => 'data', :logpath => 'data/log'}
27+
DEFAULT_BASE_OPTS = { :host => 'localhost', :dbpath => 'data', :logpath => 'data/log' }
2828
DEFAULT_REPLICA_SET = DEFAULT_BASE_OPTS.merge( :replicas => 3, :arbiters => 0 )
2929
DEFAULT_SHARDED_SIMPLE = DEFAULT_BASE_OPTS.merge( :shards => 2, :configs => 1, :routers => 4 )
3030
DEFAULT_SHARDED_REPLICA = DEFAULT_SHARDED_SIMPLE.merge( :replicas => 3, :arbiters => 0)
@@ -35,7 +35,7 @@ class Config
3535
MONGODS_OPT_KEYS = [:mongods]
3636
CLUSTER_OPT_KEYS = SHARDING_OPT_KEYS + REPLICA_OPT_KEYS + MONGODS_OPT_KEYS
3737

38-
FLAGS = [:noprealloc, :smallfiles, :logappend, :configsvr, :shardsvr, :quiet, :fastsync]
38+
FLAGS = [:noprealloc, :smallfiles, :logappend, :configsvr, :shardsvr, :quiet, :fastsync, :auth]
3939

4040
DEFAULT_VERIFIES = 60
4141
BASE_PORT = 3000
@@ -80,51 +80,52 @@ def self.cluster(opts = DEFAULT_SHARDED_SIMPLE)
8080
end
8181

8282
def self.make_mongo(kind, opts)
83-
dbpath = opts[:dbpath]
84-
port = self.get_available_port
85-
path = "#{dbpath}/#{kind}-#{port}"
83+
dbpath = opts[:dbpath]
84+
port = self.get_available_port
85+
path = "#{dbpath}/#{kind}-#{port}"
8686
logpath = "#{path}/#{kind}.log"
8787

88-
{
89-
:host => opts[:host],
90-
:port => port,
91-
:logpath => logpath,
92-
:logappend => true
93-
}
88+
{ :host => opts[:host],
89+
:port => port,
90+
:logpath => logpath,
91+
:logappend => true }
9492
end
9593

9694
def self.make_mongod(kind, opts)
9795
params = make_mongo('mongods', opts)
9896

9997
mongod = ENV['MONGOD'] || 'mongod'
100-
path = File.dirname(params[:logpath])
98+
path = File.dirname(params[:logpath])
10199

102100
noprealloc = opts[:noprealloc] || true
103101
smallfiles = opts[:smallfiles] || true
104102
quiet = opts[:quiet] || true
105103
fast_sync = opts[:fastsync] || false
106-
107-
params.merge(
108-
:command => mongod,
109-
:dbpath => path,
110-
:smallfiles => smallfiles,
111-
:noprealloc => noprealloc,
112-
:quiet => quiet,
113-
:fastsync => fast_sync
114-
)
104+
auth = opts[:auth] || true
105+
106+
params.merge(:command => mongod,
107+
:dbpath => path,
108+
:smallfiles => smallfiles,
109+
:noprealloc => noprealloc,
110+
:quiet => quiet,
111+
:fastsync => fast_sync,
112+
:auth => auth)
115113
end
116114

117115
def self.make_replica(opts, count)
118-
params = make_mongod('replicas', opts)
116+
params = make_mongod('replicas', opts)
119117

120118
replSet = opts[:replSet] || 'ruby-driver-test'
121119
oplog_size = opts[:oplog_size] || 5
120+
keyFile = opts[:keyFile] || '/test/tools/keyfile.txt'
122121

123-
params.merge(
124-
:_id => count,
125-
:replSet => replSet,
126-
:oplogSize => oplog_size
127-
)
122+
keyFile = Dir.pwd << keyFile
123+
system "chmod 600 #{keyFile}"
124+
125+
params.merge(:_id => count,
126+
:replSet => replSet,
127+
:oplogSize => oplog_size,
128+
:keyFile => keyFile)
128129
end
129130

130131
def self.make_config(opts)

0 commit comments

Comments
 (0)