Skip to content

Commit 3afa42f

Browse files
authored
Merge pull request #766 from stevenpost/user_creation
Fix secondary users on a replicaset being marked as changed
2 parents 048acb0 + 97bcbfb commit 3afa42f

File tree

3 files changed

+45
-29
lines changed

3 files changed

+45
-29
lines changed

lib/puppet/provider/mongodb_user/mongodb.rb

+17-22
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,23 @@
99
def self.instances
1010
require 'json'
1111

12-
if db_ismaster
13-
script = 'EJSON.stringify(db.system.users.find().toArray())'
14-
# A hack to prevent prefetching failures until admin user is created
15-
script = "try {#{script}} catch (e) { if (e.message.match(/requires authentication/) || e.message.match(/not authorized on admin/)) { 'not authorized on admin' } else {throw e}}" if auth_enabled
16-
17-
out = mongo_eval(script)
18-
return [] if auth_enabled && (out.include?('requires authentication') || out.include?('not authorized on admin'))
19-
20-
users = JSON.parse out
21-
22-
users.map do |user|
23-
new(name: user['_id'],
24-
ensure: :present,
25-
username: user['user'],
26-
database: user['db'],
27-
roles: from_roles(user['roles'], user['db']),
28-
password_hash: user['credentials']['MONGODB-CR'],
29-
scram_credentials: user['credentials']['SCRAM-SHA-1'])
30-
end
31-
else
32-
Puppet.warning 'User info is available only from master host'
33-
[]
12+
script = 'EJSON.stringify(db.system.users.find().toArray())'
13+
# A hack to prevent prefetching failures until admin user is created
14+
script = "try {#{script}} catch (e) { if (e.message.match(/requires authentication/) || e.message.match(/not authorized on admin/)) { 'not authorized on admin' } else {throw e}}" if auth_enabled
15+
16+
out = mongo_eval(script)
17+
return [] if auth_enabled && (out.include?('requires authentication') || out.include?('not authorized on admin'))
18+
19+
users = JSON.parse out
20+
21+
users.map do |user|
22+
new(name: user['_id'],
23+
ensure: :present,
24+
username: user['user'],
25+
database: user['db'],
26+
roles: from_roles(user['roles'], user['db']),
27+
password_hash: user['credentials']['MONGODB-CR'],
28+
scram_credentials: user['credentials']['SCRAM-SHA-1'])
3429
end
3530
end
3631

spec/acceptance/replset_spec.rb

+28
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ class { 'mongodb::globals':
7373
expect(r.stdout).to match %r{some value}
7474
end
7575
end
76+
77+
it 'create a user' do
78+
pp = <<-EOS
79+
mongodb_user {'testuser':
80+
ensure => present,
81+
password_hash => mongodb_password('testuser', 'passw0rd'),
82+
database => 'testdb',
83+
roles => ['readWrite', 'dbAdmin'],
84+
}
85+
EOS
86+
87+
apply_manifest_on(hosts, pp, catch_failures: true)
88+
apply_manifest_on(hosts, pp, catch_changes: true)
89+
end
7690
end
7791

7892
describe 'mongodb::server with replset_members' do
@@ -347,5 +361,19 @@ class { 'mongodb::globals':
347361
expect(r.stdout).to match %r{created_by_puppet}
348362
end
349363
end
364+
365+
it 'create a user' do
366+
pp = <<-EOS
367+
mongodb_user {'testuser':
368+
ensure => present,
369+
password_hash => mongodb_password('testuser', 'passw0rd'),
370+
database => 'testdb',
371+
roles => ['readWrite', 'dbAdmin'],
372+
}
373+
EOS
374+
375+
apply_manifest_on(hosts, pp, catch_failures: true)
376+
apply_manifest_on(hosts, pp, catch_changes: true)
377+
end
350378
end
351379
end

spec/unit/puppet/provider/mongodb_user/mongodb_spec.rb

-7
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,6 @@
7171
end
7272
end
7373

74-
describe 'empty self.instances from slave' do
75-
it 'doesn`t retrun array of users' do
76-
allow(provider.class).to receive(:db_ismaster).and_return(false)
77-
expect(provider.class.instances).to be_empty
78-
end
79-
end
80-
8174
describe 'create' do
8275
it 'creates a user' do
8376
cmd_json = <<-EOS.gsub(%r{^\s*}, '').gsub(%r{$\n}, '')

0 commit comments

Comments
 (0)