Skip to content

Commit 978034d

Browse files
authored
Merge pull request #701 from stevenpost/provider_cleanup
Provider cleanup
2 parents 6923d39 + 6ea7af6 commit 978034d

File tree

4 files changed

+43
-38
lines changed

4 files changed

+43
-38
lines changed

lib/puppet/provider/mongodb.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,6 @@ def mongo_version
192192
self.class.mongo_version
193193
end
194194

195-
def self.mongo_26?
196-
v = mongo_version
197-
!v[%r{^2\.6\.}].nil?
198-
end
199-
200-
def mongo_26?
201-
self.class.mongo_26?
202-
end
203-
204195
def self.mongo_4?
205196
v = mongo_version
206197
!v[%r{^4\.}].nil?

lib/puppet/provider/mongodb_user/mongodb.rb

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,12 @@ def create
5959
roles: role_hashes(@resource[:roles], @resource[:database]),
6060
}
6161

62-
if mongo_4? || mongo_5?
63-
if @resource[:auth_mechanism] == :scram_sha_256
64-
command[:mechanisms] = ['SCRAM-SHA-256']
65-
command[:pwd] = @resource[:password]
66-
command[:digestPassword] = true
67-
else
68-
command[:mechanisms] = ['SCRAM-SHA-1']
69-
command[:pwd] = password_hash
70-
command[:digestPassword] = false
71-
end
62+
if @resource[:auth_mechanism] == :scram_sha_256
63+
command[:mechanisms] = ['SCRAM-SHA-256']
64+
command[:pwd] = @resource[:password]
65+
command[:digestPassword] = true
7266
else
67+
command[:mechanisms] = ['SCRAM-SHA-1']
7368
command[:pwd] = password_hash
7469
command[:digestPassword] = false
7570
end
@@ -110,22 +105,15 @@ def password_hash=(_value)
110105
end
111106
end
112107

113-
def password=(value)
114-
if mongo_26?
115-
mongo_eval("db.changeUserPassword(#{@resource[:username].to_json}, #{value.to_json})", @resource[:database])
116-
else
117-
command = {
118-
updateUser: @resource[:username],
119-
pwd: @resource[:password],
120-
digestPassword: true
121-
}
108+
def password=(_value)
109+
command = {
110+
updateUser: @resource[:username],
111+
pwd: @resource[:password],
112+
digestPassword: true,
113+
mechanisms: @resource[:auth_mechanism] == :scram_sha_256 ? ['SCRAM-SHA-256'] : ['SCRAM-SHA-1']
114+
}
122115

123-
if mongo_4? || mongo_5?
124-
command[:mechanisms] = @resource[:auth_mechanism] == :scram_sha_256 ? ['SCRAM-SHA-256'] : ['SCRAM-SHA-1']
125-
end
126-
127-
mongo_eval("db.runCommand(#{command.to_json})", @resource[:database])
128-
end
116+
mongo_eval("db.runCommand(#{command.to_json})", @resource[:database])
129117
end
130118

131119
def roles=(roles)

spec/unit/puppet/provider/mongodb_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
describe 'mongo version detection' do
1616
v = {
17-
'2.6.x' => { '26' => true, '4' => false, '5' => false },
1817
'4.x.x' => { '26' => false, '4' => true, '5' => false },
1918
'5.x.x' => { '26' => false, '4' => false, '5' => true },
2019
'x.x.x' => { '26' => false, '4' => false, '5' => false }
@@ -23,7 +22,6 @@
2322
v.each do |key, results|
2423
it "version detection for [#{key}]" do
2524
allow(provider_class).to receive(:mongo_eval).with('db.version()').and_return(key)
26-
expect(provider_class.mongo_26?).to be results['26']
2725
expect(provider_class.mongo_4?).to be results['4']
2826
expect(provider_class.mongo_5?).to be results['5']
2927
end

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,34 @@
77
describe Puppet::Type.type(:mongodb_user).provider(:mongodb) do
88
let(:raw_users) do
99
[
10-
{ '_id' => 'admin.root', 'user' => 'root', 'db' => 'admin', 'credentials' => { 'MONGODB-CR' => 'pass', 'SCRAM-SHA-1' => { 'iterationCount' => 10_000, 'salt' => 'salt', 'storedKey' => 'storedKey', 'serverKey' => 'serverKey' } }, 'roles' => [{ 'role' => 'role2', 'db' => 'admin' }, { 'role' => 'role3', 'db' => 'user_database' }, { 'role' => 'role1', 'db' => 'admin' }] }
10+
{
11+
'_id' => 'admin.root',
12+
'user' => 'root',
13+
'db' => 'admin',
14+
'credentials' => {
15+
'MONGODB-CR' => 'pass',
16+
'SCRAM-SHA-1' => {
17+
'iterationCount' => 10_000,
18+
'salt' => 'salt',
19+
'storedKey' => 'storedKey',
20+
'serverKey' => 'serverKey'
21+
}
22+
},
23+
'roles' => [
24+
{
25+
'role' => 'role2',
26+
'db' => 'admin'
27+
},
28+
{
29+
'role' => 'role3',
30+
'db' => 'user_database'
31+
},
32+
{
33+
'role' => 'role1',
34+
'db' => 'admin'
35+
}
36+
]
37+
}
1138
].to_json
1239
end
1340

@@ -33,7 +60,7 @@
3360
mongodconffile = tmp.path
3461
allow(provider.class).to receive(:mongod_conf_file).and_return(mongodconffile)
3562
allow(provider.class).to receive(:mongo_eval).with('printjson(db.system.users.find().toArray())').and_return(raw_users)
36-
allow(provider.class).to receive(:mongo_version).and_return('2.6.x')
63+
allow(provider.class).to receive(:mongo_version).and_return('4.4.0')
3764
allow(provider.class).to receive(:db_ismaster).and_return(true)
3865
end
3966

@@ -58,6 +85,7 @@
5885
"createUser":"new_user",
5986
"customData":{"createdBy":"Puppet Mongodb_user['new_user']"},
6087
"roles":[{"role":"role1","db":"new_database"},{"role":"role2","db":"other_database"}],
88+
"mechanisms":["SCRAM-SHA-1"],
6189
"pwd":"pass",
6290
"digestPassword":false
6391
}

0 commit comments

Comments
 (0)