Skip to content

Commit

Permalink
SIRE-9538 fix tests + logic within mongoshrc.js (auth was excluded wh…
Browse files Browse the repository at this point in the history
…en replset wasn't configured)

Reviewed-by: beilers, muspelkat
  • Loading branch information
Henning-B committed Jun 20, 2023
1 parent e61d1e3 commit 3a794b4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
2 changes: 1 addition & 1 deletion spec/acceptance/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
service_name = 'mongod'
package_name = 'mongodb-org-server'
end
major_version = fact('mongodb_version').split('.')[0].to_i
major_version = 6 #fact('mongodb_version').split('.')[0].to_i
mongo_cli = if major_version >= 5
'mongosh'
else
Expand Down
15 changes: 11 additions & 4 deletions spec/classes/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@

on_supported_os.each do |os, facts|
context "on #{os}" do
facts.merge({mongodb_version: '6.0.0'})
let(:facts) { facts }
major_version = 6
mongo_cli = if major_version >= 5
'mongosh'
else
'mongo'
end

let(:config_file) do
if facts[:os]['family'] == 'Debian'
Expand Down Expand Up @@ -71,7 +78,7 @@
it { is_expected.to contain_file(config_file).with_content(%r{^ fork: true$}) }
end

it { is_expected.to contain_file('/root/.mongorc.js').with_ensure('file').without_content(%r{db\.auth}) }
it { is_expected.to contain_file("/root/.#{mongo_cli}rc.js").with_ensure('file').without_content(%r{db\.auth}) }
it { is_expected.not_to contain_exec('fix dbpath permissions') }
end

Expand Down Expand Up @@ -165,7 +172,7 @@
end

it { is_expected.to contain_file(config_file).with_content(%r{^security\.authorization: enabled$}) }
it { is_expected.to contain_file('/root/.mongorc.js') }
it { is_expected.to contain_file("/root/.#{mongo_cli}rc.js") }
end

describe 'when specifying set_parameter array value' do
Expand Down Expand Up @@ -262,7 +269,7 @@
end

it {
is_expected.to contain_file('/root/.mongorc.js').
is_expected.to contain_file("/root/.#{mongo_cli}rc.js").
with_ensure('file').
with_owner('root').
with_group('root').
Expand All @@ -278,7 +285,7 @@
}
end

it { is_expected.to contain_file('/root/.mongorc.js').with_ensure('file').without_content(%r{db\.auth}) }
it { is_expected.to contain_file("/root/.#{mongo_cli}rc.js").with_ensure('file').without_content(%r{db\.auth}) }
end
end

Expand Down
54 changes: 28 additions & 26 deletions templates/mongoshrc.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,7 @@ function authRequired() {

if (authRequired()) {
<%- if @replset -%>
<%- if @admin_auth_mechanism == 'x509' -%>
db.getMongo().setReadPref()
try {
db.getSiblingDB('$external').auth(
{
mechanism: 'MONGODB-X509'
}
)
}
catch (err) {
// This isn't catching authentication errors as I'd expect...
exit
}
<%- else -%>
// rs.slaveOk + rs secondaryOk have been deprecated, use setReadPref when running mongodb version >=5
// rs.slaveOk + rs secondaryOk have been deprecated, use setReadPref when running mongodb version >=5
try {
version=parseInt(db.version().split('.')[0])
if (version>=5){
Expand All @@ -52,20 +38,36 @@ if (authRequired()) {
rs.secondaryOk()
}
}

catch (err) {
rs.slaveOk()
}
try {
var prev_db = db
db = db.getSiblingDB('admin')
db.auth('<%= @admin_username %>', '<%= @admin_password_unsensitive %>')
db = db.getSiblingDB(prev_db)
}
catch (err) {
// This isn't catching authentication errors as I'd expect...
exit
}
<% end -%>
<% end -%>


<%- if @admin_auth_mechanism == 'x509' -%>
try {
db.getSiblingDB('$external').auth(
{
mechanism: 'MONGODB-X509'
}
)
}
catch (err) {
// This isn't catching authentication errors as I'd expect...
exit
}
<%- else -%>
try {
var prev_db = db
db = db.getSiblingDB('admin')
db.auth('<%= @admin_username %>', '<%= @admin_password_unsensitive %>')
db = db.getSiblingDB(prev_db)
}
catch (err) {
// This isn't catching authentication errors as I'd expect...
exit
}
<% end -%>
}
<% end -%>

0 comments on commit 3a794b4

Please sign in to comment.