Skip to content

Commit 374f46e

Browse files
committed
Retrieve user info on secondary nodes as well
Not doing this causes Puppet to display changes when adding secondary users.
1 parent 9a77eb7 commit 374f46e

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
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/unit/puppet/provider/mongodb_user/mongodb_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@
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
74+
describe 'same self.instances from slave' do
75+
it 'returns an array of users' do
76+
usernames = provider.class.instances.map(&:username)
77+
expect(parsed_users).to match_array(usernames)
7878
end
7979
end
8080

0 commit comments

Comments
 (0)