Skip to content

Commit c6bd001

Browse files
committed
Set correct auth_mechanism for updateUser
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.
1 parent 60e16ce commit c6bd001

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

lib/puppet/provider/mongodb_user/mongodb.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,12 @@ def password_hash=(_value)
101101
command = {
102102
updateUser: @resource[:username],
103103
pwd: @resource[:password_hash],
104-
digestPassword: false
104+
digestPassword: false,
105+
mechanisms: @resource[:auth_mechanism] == :scram_sha_1 ? ['SCRAM-SHA-1'] : ['SCRAM-SHA-256'],
105106
}
106107

107-
mongo_eval("db.runCommand(#{command.to_json})", @resource[:database])
108+
out = JSON.parse(mongo_eval("db.runCommand(#{command.to_json})", @resource[:database]))
109+
raise "Failed update User password for user '#{@resource[:username]}'\n#{out}" if out['ok'].zero?
108110
else
109111
Puppet.warning 'User password operations are available only from master host'
110112
end

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,12 @@
9393
{
9494
"updateUser":"new_user",
9595
"pwd":"pass",
96-
"digestPassword":false
96+
"digestPassword":false,
97+
"mechanisms":["SCRAM-SHA-1"]
9798
}
9899
EOS
99100
allow(provider).to receive(:mongo_eval).
100-
with("db.runCommand(#{cmd_json})", 'new_database')
101+
with("db.runCommand(#{cmd_json})", 'new_database').and_return('{"ok": 1}')
101102
provider.password_hash = 'newpass'
102103
expect(provider).to have_received(:mongo_eval)
103104
end

0 commit comments

Comments
 (0)