Skip to content

Commit 6849c58

Browse files
author
Johan De Wit
committed
add some more rspec tests, and fix template
1 parent d4e6d00 commit 6849c58

File tree

4 files changed

+92
-8
lines changed

4 files changed

+92
-8
lines changed

manifests/server/config.pp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,18 @@
129129
}
130130

131131
# TODO: we kind of use this file to force x509 autehntication in the providers when it exsists
132-
# Open for suugestions how to deal with this
133-
if $admin_auth_mechanism == 'x509' {
134-
$_ensure = 'present'
132+
# Open for suggestions how to deal with this
133+
if $admin_auth_mechanism == 'x509' and $handle_creds {
134+
$_ensure = 'file'
135135
} else {
136136
$_ensure = 'absent'
137137
}
138138

139139
file { '/root/.mongosh.yaml':
140140
ensure => $_ensure,
141-
mode => '0400',
141+
owner => 'root',
142+
group => 'root',
143+
mode => '0600',
142144
content => "---\n${admin_username}:\n tlsCertificateKeyFile: ${admin_tls_key}",
143145
}
144146

spec/classes/repo_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@
8686
it { is_expected.to raise_error(Puppet::Error, %r{not supported}) }
8787
end
8888
end
89+
90+
describe 'with older version' do
91+
let :params do
92+
{
93+
version: '3.0.2'
94+
}
95+
end
96+
97+
it { is_expected.to raise_error(Puppet::Error, %r{older than 4.4 are unsupported}) }
98+
end
8999
end
90100
end
91101
end

spec/classes/server_spec.rb

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@
4343
end
4444
end
4545

46+
let(:mongo_user) do
47+
if facts[:os]['family'] == 'Debian'
48+
'mongodb'
49+
else
50+
'mongod'
51+
end
52+
end
53+
54+
let(:mongo_group) do
55+
if facts[:os]['family'] == 'Debian'
56+
'mongodb'
57+
else
58+
'mongod'
59+
end
60+
end
61+
4662
describe 'with defaults' do
4763
it_behaves_like 'server classes'
4864

@@ -62,6 +78,8 @@
6278
it { is_expected.not_to contain_file(config_file).with_content(%r{fork}) }
6379

6480
it { is_expected.to contain_file('/root/.mongoshrc.js').with_ensure('file').without_content(%r{db\.auth}) }
81+
it { is_expected.to contain_file('/var/lib/mongo').with(ensure: 'directory', mode: '0750', owner: mongo_user, group: mongo_group) }
82+
6583
it { is_expected.not_to contain_exec('fix dbpath permissions') }
6684
end
6785

@@ -264,7 +282,7 @@
264282
end
265283

266284
describe 'with store_creds' do
267-
context 'true' do
285+
context 'true with scram_sha_1' do
268286
let :params do
269287
{
270288
admin_username: 'admin',
@@ -284,6 +302,60 @@
284302
}
285303
end
286304

305+
context 'true with scram_sha_256' do
306+
let :params do
307+
{
308+
admin_username: 'admin',
309+
admin_password: 'password',
310+
admin_auth_mechanism: 'scram_sha_256',
311+
admin_update_password: true,
312+
auth: true,
313+
store_creds: true
314+
}
315+
end
316+
317+
it {
318+
is_expected.to contain_file('/root/.mongoshrc.js').
319+
with_ensure('file').
320+
with_owner('root').
321+
with_group('root').
322+
with_mode('0600').
323+
with_content(%r{db\.auth\('admin', 'password'\)})
324+
}
325+
end
326+
327+
context 'true with x509' do
328+
let :params do
329+
{
330+
admin_username: 'subject=CN=admin,OU=some,O=company,ST=somewhere,C=EX',
331+
admin_auth_mechanism: 'x509',
332+
admin_tls_key: '/path/to/key',
333+
auth: true,
334+
store_creds: true
335+
}
336+
end
337+
338+
it {
339+
is_expected.to contain_file('/root/.mongoshrc.js').
340+
with_ensure('file').
341+
with_owner('root').
342+
with_group('root').
343+
with_mode('0600').
344+
with_content(%r{db\.getSiblingDB\('\$external'\)\.auth}).
345+
with_content(%r{mechanism: 'MONGODB-X509'})
346+
}
347+
348+
it {
349+
is_expected.to contain_file('/root/.mongosh.yaml').
350+
with_ensure('file').
351+
with_owner('root').
352+
with_group('root').
353+
with_mode('0600').
354+
with_content(%r{^subject=CN=admin,OU=some,O=company,ST=somewhere,C=EX:$}).
355+
with_content(%r{tlsCertificateKeyFile: /path/to/key})
356+
}
357+
end
358+
287359
context 'false' do
288360
let :params do
289361
{

templates/mongoshrc.js.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ function authRequired() {
3232
if (authRequired()) {
3333
<%- if @replset -%>
3434
db.getMongo().setReadPref('primaryPreferred')
35-
<%- if @admin_auth_mechanism == 'x509' -%>
35+
<%- end -%>
36+
<%- if @admin_auth_mechanism == 'x509' -%>
3637
try {
3738
db.getSiblingDB('$external').auth(
3839
{
@@ -44,7 +45,7 @@ if (authRequired()) {
4445
// This isn't catching authentication errors as I'd expect...
4546
throw(err)
4647
}
47-
<%- else -%>
48+
<%- else -%>
4849
try {
4950
var prev_db = db.getName()
5051
db = db.getSiblingDB('admin')
@@ -55,7 +56,6 @@ if (authRequired()) {
5556
// This isn't catching authentication errors as I'd expect...
5657
throw(err)
5758
}
58-
<%- end -%>
5959
<%- end -%>
6060
}
6161
<% end -%>

0 commit comments

Comments
 (0)