Skip to content

Commit cda4f6d

Browse files
author
Johan De Wit
committed
add x509 authentication to the rc file
1 parent 5d3cdc9 commit cda4f6d

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

lib/puppet/provider/mongodb_user/mongodb.rb

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,17 @@ def password_hash=(_value)
115115
end
116116

117117
def password=(value)
118-
if mongo_26?
119-
mongo_eval("db.changeUserPassword(#{@resource[:username].to_json}, #{value.to_json})", @resource[:database])
120-
else
121-
command = {
122-
updateUser: @resource[:username],
123-
pwd: @resource[:password],
124-
digestPassword: true
125-
}
126-
127-
if mongo_4? || mongo_5? || mongo_6?
128-
command[:mechanisms] = @resource[:auth_mechanism] == :scram_sha_256 ? ['SCRAM-SHA-256'] : ['SCRAM-SHA-1'] # rubocop:disable Naming/VariableNumber
129-
end
130-
131-
mongo_eval("db.runCommand(#{command.to_json})", @resource[:database])
118+
command = {
119+
updateUser: @resource[:username],
120+
pwd: @resource[:password],
121+
digestPassword: true
122+
}
123+
124+
if mongo_4? || mongo_5? || mongo_6?
125+
command[:mechanisms] = @resource[:auth_mechanism] == :scram_sha_256 ? ['SCRAM-SHA-256'] : ['SCRAM-SHA-1'] # rubocop:disable Naming/VariableNumber
132126
end
127+
128+
mongo_eval("db.runCommand(#{command.to_json})", @resource[:database])
133129
end
134130

135131
def roles=(roles)

templates/mongoshrc.js.erb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,21 @@ function authRequired() {
3030
}
3131

3232
if (authRequired()) {
33-
<%- if @admin_auth_mechanism != 'x509' -%>
34-
<%- if @replset -%>
33+
<%- if @replset -%>
3534
db.getMongo().setReadPref('primaryPreferred')
36-
<%- end -%>
35+
<%- if @admin_auth_mechanism == 'x509' -%>
36+
try {
37+
db.getSiblingDB('$external').auth(
38+
{
39+
mechanism: 'MONGODB-X509'
40+
}
41+
)
42+
}
43+
catch(err) {
44+
// This isn't catching authentication errors as I'd expect...
45+
throw(err)
46+
}
47+
<%- else -%>
3748
try {
3849
var prev_db = db.getName()
3950
db = db.getSiblingDB('admin')
@@ -44,6 +55,7 @@ if (authRequired()) {
4455
// This isn't catching authentication errors as I'd expect...
4556
throw(err)
4657
}
58+
<%- end -%>
4759
<%- end -%>
4860
}
4961
<% end -%>

0 commit comments

Comments
 (0)