Skip to content

Commit

Permalink
Set correct auth_mechanism for updateUser
Browse files Browse the repository at this point in the history
Currently the mongodb command `updateUser` defaults to SCRAM-SHA-256 but you can't update these passwords.

And also show an error when the update goes wrong.
  • Loading branch information
JvGinkel committed Jan 26, 2023
1 parent 60e16ce commit c6bd001
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/puppet/provider/mongodb_user/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ def password_hash=(_value)
command = {
updateUser: @resource[:username],
pwd: @resource[:password_hash],
digestPassword: false
digestPassword: false,
mechanisms: @resource[:auth_mechanism] == :scram_sha_1 ? ['SCRAM-SHA-1'] : ['SCRAM-SHA-256'],
}

mongo_eval("db.runCommand(#{command.to_json})", @resource[:database])
out = JSON.parse(mongo_eval("db.runCommand(#{command.to_json})", @resource[:database]))
raise "Failed update User password for user '#{@resource[:username]}'\n#{out}" if out['ok'].zero?
else
Puppet.warning 'User password operations are available only from master host'
end
Expand Down
5 changes: 3 additions & 2 deletions spec/unit/puppet/provider/mongodb_user/mongodb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@
{
"updateUser":"new_user",
"pwd":"pass",
"digestPassword":false
"digestPassword":false,
"mechanisms":["SCRAM-SHA-1"]
}
EOS
allow(provider).to receive(:mongo_eval).
with("db.runCommand(#{cmd_json})", 'new_database')
with("db.runCommand(#{cmd_json})", 'new_database').and_return('{"ok": 1}')
provider.password_hash = 'newpass'
expect(provider).to have_received(:mongo_eval)
end
Expand Down

0 comments on commit c6bd001

Please sign in to comment.