Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/SIRE-9538'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernd Eilers committed Aug 23, 2023
2 parents 6c5a2e6 + 1ace857 commit a2d78d5
Show file tree
Hide file tree
Showing 21 changed files with 168 additions and 186 deletions.
9 changes: 9 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,7 @@ The following parameters are available in the `mongodb::server` class:
* [`service_status`](#-mongodb--server--service_status)
* [`package_ensure`](#-mongodb--server--package_ensure)
* [`package_name`](#-mongodb--server--package_name)
* [`mongosh_package_name`](#-mongodb--server--mongosh_package_name)
* [`logpath`](#-mongodb--server--logpath)
* [`bind_ip`](#-mongodb--server--bind_ip)
* [`ipv6`](#-mongodb--server--ipv6)
Expand Down Expand Up @@ -1459,6 +1460,14 @@ Data type: `String`

Default value: `$mongodb::params::server_package_name`

##### <a name="-mongodb--server--mongosh_package_name"></a>`mongosh_package_name`

Data type: `String`



Default value: `$mongodb::params::mongosh_package_name`

##### <a name="-mongodb--server--logpath"></a>`logpath`

Data type: `Variant[Boolean, Stdlib::Absolutepath]`
Expand Down
8 changes: 4 additions & 4 deletions lib/facter/is_master.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ def get_options_from_config(file)

Facter.add('mongodb_is_master') do
setcode do
if %w[mongo mongod].all? { |m| Facter::Util::Resolution.which m }
if %w[mongosh mongod].all? { |m| Facter::Util::Resolution.which m }
file = mongod_conf_file
if file
options = get_options_from_config(file)
e = File.exist?('/root/.mongorc.js') ? 'load(\'/root/.mongorc.js\'); ' : ''
e = File.exist?('/root/.mongoshrc.js') ? 'load(\'/root/.mongoshrc.js\'); ' : ''

# Check if the mongodb server is responding:
Facter::Core::Execution.exec("mongo --quiet #{options} --eval \"#{e}printjson(db.adminCommand({ ping: 1 }))\"")
Facter::Core::Execution.exec("mongosh --quiet #{options} --eval \"#{e}EJSON.stringify(db.adminCommand({ ping: 1 }))\"")

if $CHILD_STATUS.success?
Facter::Core::Execution.exec("mongo --quiet #{options} --eval \"#{e}db.isMaster().ismaster\"")
Facter::Core::Execution.exec("mongosh --quiet #{options} --eval \"#{e}db.isMaster().ismaster\"")
else
'not_responding'
end
Expand Down
6 changes: 3 additions & 3 deletions lib/facter/mongodb_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

Facter.add(:mongodb_version) do
setcode do
if Facter::Core::Execution.which('mongo')
mongodb_version = Facter::Core::Execution.execute('mongo --version 2>&1')
%r{MongoDB shell version:?\s+v?([\w.]+)}.match(mongodb_version)[1]
if Facter::Core::Execution.which('mongod')
mongodb_version = Facter::Core::Execution.execute('mongod --version 2>&1')
%r{db version:?\s+v?([\w.]+)}.match(mongodb_version)[1]
end
end
end
27 changes: 20 additions & 7 deletions lib/puppet/provider/mongodb.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# frozen_string_literal: true

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..'))
require 'puppet/util/mongodb_output'

require 'yaml'
require 'json'

class Puppet::Provider::Mongodb < Puppet::Provider
# Without initvars commands won't work.
initvars
commands mongo: 'mongo'
# TODO: do we still need to support mongo ? Since it is removed from 6.x, not in this PR
commands mongo: 'mongosh'

# Optional defaults file
def self.mongorc_file
"load('#{Facter.value(:root_home)}/.mongorc.js'); " if File.file?("#{Facter.value(:root_home)}/.mongorc.js")
"load('#{Facter.value(:root_home)}/.mongoshrc.js'); " if File.file?("#{Facter.value(:root_home)}/.mongoshrc.js")
end

def mongorc_file
Expand Down Expand Up @@ -43,7 +44,7 @@ def self.mongo_conf
'tlsca' => config['net.tls.CAFile'],
'auth' => config['security.authorization'],
'shardsvr' => config['sharding.clusterRole'],
'confsvr' => config['sharding.clusterRole']
'confsvr' => config['sharding.clusterRole'],
}
end

Expand Down Expand Up @@ -92,15 +93,16 @@ def self.mongo_cmd(db, host, cmd)

if tls_is_enabled(config)
args.push('--tls')
args += ['--tlsCertificateKeyFile', config['tlscert']]

tls_ca = config['tlsca']
args += ['--tlsCAFile', tls_ca] unless tls_ca.nil?

args += ['--tlsCertificateKeyFile', config['tlscert']]

args.push('--tlsAllowInvalidHostnames') if tls_invalid_hostnames(config)
end

args += ['--eval', cmd]
args += ['--eval', "\"#{cmd}\""]
mongo(args)
end

Expand Down Expand Up @@ -169,14 +171,15 @@ def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
retry_count -= 1
if retry_count.positive?
Puppet.debug "Request failed: '#{e.message}' Retry: '#{retries - retry_count}'"
out = { 'errmsg' => e.message }
sleep retry_sleep
retry
end
end

raise Puppet::ExecutionFailure, "Could not evaluate MongoDB shell command: #{cmd}" unless out

Puppet::Util::MongodbOutput.sanitize(out)
out
end

def mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
Expand All @@ -192,6 +195,7 @@ def mongo_version
self.class.mongo_version
end

# TODO: moingosh only from 4.2 bersion ?, so do we remove this?
def self.mongo_26?
v = mongo_version
!v[%r{^2\.6\.}].nil?
Expand All @@ -218,4 +222,13 @@ def self.mongo_5?
def mongo_5?
self.class.mongo_5?
end

def self.mongo_6?
v = mongo_version
!v[%r{^6\.}].nil?
end

def mongo_6?
self.class.mongo_6?
end
end
7 changes: 4 additions & 3 deletions lib/puppet/provider/mongodb_database/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
def self.instances
require 'json'

pre_cmd = 'try { rs.secondaryOk() } catch (err) { rs.slaveOk() }'
dbs = JSON.parse mongo_eval("#{pre_cmd};printjson(db.getMongo().getDBs())")
mongo_eval_result = mongo_eval('JSON.stringify(db.getMongo().getDBs())')

dbs = JSON.parse mongo_eval_result

dbs['databases'].map do |db|
new(name: db['name'],
Expand All @@ -29,7 +30,7 @@ def self.prefetch(resources)

def create
if db_ismaster
out = mongo_eval('db.dummyData.insert({"created_by_puppet": 1})', @resource[:name])
out = mongo_eval('db.dummyData.insertOne({"created_by_puppet": 1})', @resource[:name])
raise "Failed to create DB '#{@resource[:name]}'\n#{out}" if %r{writeError} =~ out
else
Puppet.warning 'Database creation is available only from master host'
Expand Down
10 changes: 7 additions & 3 deletions lib/puppet/provider/mongodb_replset/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def set_members
Puppet.debug 'Replica set initialization has successfully ended'
return true
else
Puppet.debug "Wainting for replica initialization. Retry: #{n}"
Puppet.debug "Waiting for replica initialization. Retry: #{n}"
sleep retry_sleep
next
end
Expand Down Expand Up @@ -383,7 +383,7 @@ def mongo_command(command, host, retries = 4)

def self.mongo_command(command, host = nil, retries = 4)
begin
output = mongo_eval("printjson(#{command})", 'admin', retries, host)
output = mongo_eval("EJSON.stringify(#{command})", 'admin', retries, host)
rescue Puppet::ExecutionFailure => e
Puppet.debug "Got an exception: #{e}"
raise
Expand All @@ -394,6 +394,10 @@ def self.mongo_command(command, host = nil, retries = 4)
output = '{}' if output == "\nnull\n"

# Parse the JSON output and return
JSON.parse(output)
begin
JSON.parse(output)
rescue e
output
end
end
end
2 changes: 1 addition & 1 deletion lib/puppet/provider/mongodb_shard/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def self.mongo_command(command, host = nil, _retries = 4)
args = []
args << '--quiet'
args << ['--host', host] if host
args << ['--eval', "printjson(#{command})"]
args << ['--eval', "EJSON.stringify(#{command})"]
output = mongo(args.flatten)
rescue Puppet::ExecutionFailure => e
raise unless e =~ %r{Error: couldn't connect to server} && wait <= (2**max_wait)
Expand Down
6 changes: 3 additions & 3 deletions lib/puppet/provider/mongodb_user/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.instances
require 'json'

if db_ismaster
script = 'printjson(db.system.users.find().toArray())'
script = 'EJSON.stringify(db.system.users.find().toArray())'
# A hack to prevent prefetching failures until admin user is created
script = "try {#{script}} catch (e) { if (e.message.match(/not authorized on admin/)) { 'not authorized on admin' } else {throw e}}" if auth_enabled

Expand Down Expand Up @@ -59,7 +59,7 @@ def create
roles: role_hashes(@resource[:roles], @resource[:database]),
}

if mongo_4? || mongo_5?
if mongo_4? || mongo_5? || mongo_6?
if @resource[:auth_mechanism] == :scram_sha_256 # rubocop:disable Naming/VariableNumber
command[:mechanisms] = ['SCRAM-SHA-256']
command[:pwd] = @resource[:password]
Expand Down Expand Up @@ -120,7 +120,7 @@ def password=(value)
digestPassword: true
}

if mongo_4? || mongo_5?
if mongo_4? || mongo_5? || mongo_6?
command[:mechanisms] = @resource[:auth_mechanism] == :scram_sha_256 ? ['SCRAM-SHA-256'] : ['SCRAM-SHA-1'] # rubocop:disable Naming/VariableNumber
end

Expand Down
18 changes: 0 additions & 18 deletions lib/puppet/util/mongodb_output.rb

This file was deleted.

4 changes: 3 additions & 1 deletion manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
]
$handle_creds = true
$store_creds = false
$rcfile = "${facts['root_home']}/.mongorc.js"
$rcfile = "${facts['root_home']}/.mongoshrc.js"
$dbpath_fix = false

$manage_package = pick($mongodb::globals::manage_package, $mongodb::globals::manage_package_repo, false)
Expand All @@ -38,6 +38,8 @@
$package_ensure_mongos = true
}

$mongosh_package_name = 'mongodb-mongosh'

# Amazon Linux's OS Family is 'Linux', operating system 'Amazon'.
case $facts['os']['family'] {
'RedHat', 'Linux', 'Suse': {
Expand Down
16 changes: 9 additions & 7 deletions manifests/server.pp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
Optional[Enum['stopped', 'running']] $service_status = $mongodb::params::service_status,
Variant[Boolean, String] $package_ensure = $mongodb::params::package_ensure,
String $package_name = $mongodb::params::server_package_name,
String $mongosh_package_name = $mongodb::params::mongosh_package_name,
Variant[Boolean, Stdlib::Absolutepath] $logpath = $mongodb::params::logpath,
Array[Stdlib::IP::Address] $bind_ip = $mongodb::params::bind_ip,
Optional[Boolean] $ipv6 = undef,
Expand Down Expand Up @@ -126,12 +127,13 @@
$admin_password
}
if $create_admin and ($service_ensure == 'running' or $service_ensure == true) {
mongodb::db { 'admin':
user => $admin_username,
auth_mechanism => $admin_auth_mechanism,
password => $admin_password_unsensitive,
roles => $admin_roles,
update_password => $admin_update_password,
mongodb_user { 'admin user':
ensure => present,
username => $admin_username,
database => 'admin',
roles => $admin_roles,
auth_mechanism => $admin_auth_mechanism,
password => $admin_password,
}

# Make sure it runs before other DB creation
Expand Down Expand Up @@ -169,7 +171,7 @@

# Make sure that the ordering is correct
if $create_admin {
Class['mongodb::replset'] -> Mongodb::Db['admin']
Class['mongodb::replset'] -> Mongodb_user['admin user']
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion manifests/server/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
if $handle_creds {
file { $rcfile:
ensure => file,
content => template('mongodb/mongorc.js.erb'),
content => template('mongodb/mongoshrc.js.erb'),
owner => 'root',
group => 'root',
mode => '0600',
Expand Down
12 changes: 10 additions & 2 deletions manifests/server/install.pp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# PRIVATE CLASS: do not call directly
class mongodb::server::install {
$package_ensure = $mongodb::server::package_ensure
$package_name = $mongodb::server::package_name
$package_ensure = $mongodb::server::package_ensure
$package_name = $mongodb::server::package_name
$mongosh_package_name = $mongodb::server::mongosh_package_name

case $package_ensure {
true: {
Expand Down Expand Up @@ -33,4 +34,11 @@
tag => 'mongodb_package',
}
}
unless defined(Package[$mongosh_package_name]) {
package { 'mongodb_mongosh':
ensure => present,
name => $mongosh_package_name,
tag => 'mongodb_mongosh_package',
}
}
}
12 changes: 9 additions & 3 deletions spec/acceptance/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
service_name = 'mongod'
package_name = 'mongodb-org-server'
end
major_version = 6 # fact('mongodb_version').split('.')[0].to_i
mongo_cli = if major_version >= 5
'mongosh'
else
'mongo'
end

describe 'installation' do
it 'works with no errors' do
Expand Down Expand Up @@ -143,15 +149,15 @@ class { 'mongodb::client': }
its(:stdout) { is_expected.to match '13' }
end

describe file('/root/.mongorc.js') do
describe file("/root/.#{mongo_cli}.js") do
it { is_expected.to be_file }

Check failure on line 153 in spec/acceptance/server_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Debian 10

mongodb::server class installation using authentication File "/root/.mongosh.js" is expected to be file Failure/Error: it { is_expected.to be_file } expected `File "/root/.mongosh.js".file?` to be truthy, got false
it { is_expected.to be_owned_by 'root' }

Check failure on line 154 in spec/acceptance/server_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Debian 10

mongodb::server class installation using authentication File "/root/.mongosh.js" is expected to be owned by "root" Failure/Error: it { is_expected.to be_owned_by 'root' } expected `File "/root/.mongosh.js".owned_by?("root")` to be truthy, got false
it { is_expected.to be_grouped_into 'root' }

Check failure on line 155 in spec/acceptance/server_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Debian 10

mongodb::server class installation using authentication File "/root/.mongosh.js" is expected to be grouped into "root" Failure/Error: it { is_expected.to be_grouped_into 'root' } expected `File "/root/.mongosh.js".grouped_into?("root")` to be truthy, got false
it { is_expected.to be_mode 600 }

Check failure on line 156 in spec/acceptance/server_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Debian 10

mongodb::server class installation using authentication File "/root/.mongosh.js" is expected to be mode 600 Failure/Error: it { is_expected.to be_mode 600 } expected `File "/root/.mongosh.js".mode?(600)` to be truthy, got false
it { is_expected.to contain 'db.auth(\'admin\', \'password\')' }
it { is_expected.to contain 'admin.auth(\'admin\', \'password\')' }

Check failure on line 157 in spec/acceptance/server_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Debian 10

mongodb::server class installation using authentication File "/root/.mongosh.js" is expected to contain "admin.auth('admin', 'password')" Failure/Error: it { is_expected.to contain 'admin.auth(\'admin\', \'password\')' } expected File "/root/.mongosh.js" to contain "admin.auth('admin', 'password')"
end

describe command("mongo admin --quiet --eval \"load('/root/.mongorc.js');printjson(db.getUser('admin')['customData'])\"") do
describe command("#{mongo_cli} admin --quiet --eval \"load('/root/.#{mongo_cli}.js');printjson(db.getUser('admin')['customData'])\"") do
its(:exit_status) { is_expected.to eq 0 }

Check failure on line 161 in spec/acceptance/server_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / 7 - Debian 10

mongodb::server class installation using authentication Command "mongosh admin --quiet --eval "load('/root/.mongosh.js');printjson(db.getUser('admin')['customData'])"" exit_status is expected to eq 0 Failure/Error: its(:exit_status) { is_expected.to eq 0 } expected: 0 got: 1 (compared using ==)
its(:stdout) { is_expected.to match "{ \"createdBy\" : \"Puppet Mongodb_user['User admin on db admin']\" }\n" }
end
Expand Down
Loading

0 comments on commit a2d78d5

Please sign in to comment.