Skip to content

Commit d1f5c38

Browse files
authored
Merge pull request #760 from stevenpost/admin_password/fix
Backslashes in a password need to be escaped
2 parents 3c22469 + 2ab8ca2 commit d1f5c38

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

spec/acceptance/server_spec.rb

+79
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ class { 'mongodb::globals':
8585
end
8686

8787
describe 'installation using authentication' do
88+
after :all do
89+
pp = <<-EOS
90+
class { 'mongodb::globals':
91+
#{repo_ver_param}
92+
}
93+
-> class { 'mongodb::server':
94+
ensure => absent,
95+
package_ensure => purged,
96+
service_ensure => stopped
97+
}
98+
-> class { 'mongodb::client':
99+
ensure => purged
100+
}
101+
EOS
102+
103+
apply_manifest(pp, catch_failures: true)
104+
end
105+
88106
it 'works with no errors' do
89107
pp = <<-EOS
90108
class { 'mongodb::globals':
@@ -154,6 +172,67 @@ class { 'mongodb::globals':
154172
end
155173
end
156174

175+
describe 'installation using authentication with complex password' do
176+
it 'works with no errors' do
177+
pp = <<-EOS
178+
class { 'mongodb::globals':
179+
#{repo_ver_param}
180+
}
181+
-> class { 'mongodb::server':
182+
auth => true,
183+
create_admin => true,
184+
handle_creds => true,
185+
store_creds => true,
186+
admin_username => 'admin',
187+
admin_password => 'admin_\\\\_\\'_"_&_password',
188+
restart => true,
189+
}
190+
class { 'mongodb::client': }
191+
EOS
192+
193+
apply_manifest(pp, catch_failures: true)
194+
apply_manifest(pp, catch_changes: true)
195+
end
196+
197+
describe package(package_name) do
198+
it { is_expected.to be_installed }
199+
end
200+
201+
describe file(config_file) do
202+
it { is_expected.to be_file }
203+
end
204+
205+
describe service(service_name) do
206+
it { is_expected.to be_enabled }
207+
it { is_expected.to be_running }
208+
end
209+
210+
describe port(27_017) do
211+
it { is_expected.to be_listening }
212+
end
213+
214+
describe command('mongosh --quiet --eval "db.serverCmdLineOpts().ok"') do
215+
its(:stderr) { is_expected.to match %r{requires authentication} }
216+
end
217+
218+
describe file('/root/.mongoshrc.js') do
219+
it { is_expected.to be_file }
220+
it { is_expected.to be_owned_by 'root' }
221+
it { is_expected.to be_grouped_into 'root' }
222+
it { is_expected.to be_mode 600 }
223+
it { is_expected.to contain 'admin.auth(\'admin\', \'admin_\\\\_\\\'_"_&_password\')' }
224+
end
225+
226+
describe command("mongosh admin --quiet --eval \"load('/root/.mongoshrc.js');EJSON.stringify(db.getUser('admin')['customData'])\"") do
227+
its(:exit_status) { is_expected.to eq 0 }
228+
its(:stdout) { is_expected.to match "{\"createdBy\":\"Puppet Mongodb_user['User admin on db admin']\"}\n" }
229+
end
230+
231+
describe command('mongod --version') do
232+
its(:exit_status) { is_expected.to eq 0 }
233+
end
234+
end
235+
157236
describe 'uninstallation' do
158237
it 'uninstalls mongodb' do
159238
pp = <<-EOS

spec/classes/server_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,22 @@
452452
with_mode('0600').
453453
with_content(%r{admin\.auth\('admin', 'password'\)})
454454
}
455+
456+
context 'with complex password' do
457+
let :params do
458+
{
459+
admin_username: 'admin',
460+
admin_password: 'complex_\\_\'_"_&_password',
461+
auth: true,
462+
store_creds: true
463+
}
464+
end
465+
466+
it {
467+
is_expected.to contain_file('/root/.mongoshrc.js').
468+
with_content(%r{admin\.auth\('admin', 'complex_\\\\_\\'_"_&_password'\)})
469+
}
470+
end
455471
end
456472

457473
context 'false' do

templates/mongoshrc.js.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ if (authRequired()) {
3434
<%- end -%>
3535
try {
3636
admin = db.getSiblingDB('admin')
37-
admin.auth('<%= @admin_username %>', '<%= @admin_password_unsensitive %>')
37+
admin.auth('<%= @admin_username %>', '<%= @admin_password_unsensitive.gsub('\\','\\\\\\\\').gsub("'","\\\\'") %>')
3838
}
3939
catch (err) {
4040
// Silently ignore this error, we can't really do anything about it.

0 commit comments

Comments
 (0)