Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

catch errors based on missing authentication to allow creation of admin user on replicaset setup #479

Closed
wants to merge 12 commits into from
12 changes: 10 additions & 2 deletions lib/puppet/provider/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,17 @@ def self.conn_string

def self.db_ismaster
cmd_ismaster = 'db.isMaster().ismaster'
cmd_ismaster = mongorc_file + cmd_ismaster if mongorc_file
db = 'admin'
res = mongo_cmd(db, conn_string, cmd_ismaster).to_s.chomp

full_command = if mongorc_file
mongorc_file + cmd_ismaster
else
cmd_ismaster
end
res = mongo_cmd(db, conn_string, full_command).to_s.chomp
if res =~ %r{Authentication failed}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You retry the same command twice if there's an authentication failure (previous iteration didn't have this). You probably want to change this to if !mongorc_file && res =~ %r{Authentication failed} to get the same result.

res = mongo_cmd(db, conn_string, cmd_ismaster).to_s.chomp
end
res.eql?('true') ? true : false
end

Expand Down
13 changes: 9 additions & 4 deletions lib/puppet/provider/mongodb_database/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@

def self.instances
require 'json'
dbs = JSON.parse mongo_eval('printjson(db.getMongo().getDBs())')
begin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this indenting. You can write

def self.instances
  # code
rescue
  # code
end

dbs = JSON.parse mongo_eval('printjson(db.getMongo().getDBs())')

dbs['databases'].map do |db|
new(name: db['name'],
ensure: :present)
dbs['databases'].map do |db|
new(name: db['name'],
ensure: :present)
end
rescue => e
Puppet.warning("Getting instances of mongodb_database failed: #{e}")
[]
end
end

Expand Down
29 changes: 17 additions & 12 deletions lib/puppet/provider/mongodb_user/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,21 @@ def self.instances
end
return allusers
else
users = JSON.parse mongo_eval('printjson(db.system.users.find().toArray())')

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'])
begin
users = JSON.parse mongo_eval('printjson(db.system.users.find().toArray())')

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
rescue => e
Puppet.warning "Could not get instances for mongodb_database: #{e}"
[]
end
end
else
Expand Down Expand Up @@ -84,7 +89,7 @@ def create
"roles": #{@resource[:roles].to_json},
"digestPassword": false
}
EOS
EOS

mongo_eval("db.runCommand(#{cmd_json})", @resource[:database])
end
Expand Down Expand Up @@ -155,7 +160,7 @@ def roles=(roles)
else
grant = roles - @property_hash[:roles]
unless grant.empty?
mongo_eval("db.getSiblingDB('#{@resource[:database]}').grantRolesToUser('#{@resource[:username]}', #{grant. to_json})")
mongo_eval("db.getSiblingDB('#{@resource[:database]}').grantRolesToUser('#{@resource[:username]}', #{grant.to_json})")
end

revoke = @property_hash[:roles] - roles
Expand Down