Skip to content

Commit

Permalink
Retrieve user info on secondary nodes as well
Browse files Browse the repository at this point in the history
Not doing this causes Puppet to display changes when adding secondary
users.
  • Loading branch information
stevenpost committed Jul 23, 2024
1 parent 9a77eb7 commit 374f46e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
39 changes: 17 additions & 22 deletions lib/puppet/provider/mongodb_user/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,23 @@
def self.instances
require 'json'

if db_ismaster
script = 'EJSON.stringify(db.system.users.find().toArray())'
# A hack to prevent prefetching failures until admin user is created
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

out = mongo_eval(script)
return [] if auth_enabled && (out.include?('requires authentication') || out.include?('not authorized on admin'))

users = JSON.parse out

users.map do |user|
new(name: user['_id'],
ensure: :present,
username: user['user'],
database: user['db'],
roles: from_roles(user['roles'], user['db']),
password_hash: user['credentials']['MONGODB-CR'],
scram_credentials: user['credentials']['SCRAM-SHA-1'])
end
else
Puppet.warning 'User info is available only from master host'
[]
script = 'EJSON.stringify(db.system.users.find().toArray())'
# A hack to prevent prefetching failures until admin user is created
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

out = mongo_eval(script)
return [] if auth_enabled && (out.include?('requires authentication') || out.include?('not authorized on admin'))

users = JSON.parse out

users.map do |user|
new(name: user['_id'],
ensure: :present,
username: user['user'],
database: user['db'],
roles: from_roles(user['roles'], user['db']),
password_hash: user['credentials']['MONGODB-CR'],
scram_credentials: user['credentials']['SCRAM-SHA-1'])
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/unit/puppet/provider/mongodb_user/mongodb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@
end
end

describe 'empty self.instances from slave' do
it 'doesn`t retrun array of users' do
allow(provider.class).to receive(:db_ismaster).and_return(false)
expect(provider.class.instances).to be_empty
describe 'same self.instances from slave' do

Check failure on line 74 in spec/unit/puppet/provider/mongodb_user/mongodb_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

RSpec/RepeatedExampleGroupBody: Repeated describe block body on line(s) [67] (https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/RepeatedExampleGroupBody)
it 'returns an array of users' do
usernames = provider.class.instances.map(&:username)
expect(parsed_users).to match_array(usernames)
end
end

Expand Down

0 comments on commit 374f46e

Please sign in to comment.