diff --git a/lib/puppet/provider/mongodb.rb b/lib/puppet/provider/mongodb.rb index f0adc575f..4fdac4ab5 100644 --- a/lib/puppet/provider/mongodb.rb +++ b/lib/puppet/provider/mongodb.rb @@ -104,7 +104,6 @@ def self.mongo_cmd(db, host, cmd) end args += ['--eval', "\"#{cmd}\""] - Puppet.warning "mongosh: #{args}" mongo(args) end @@ -157,9 +156,9 @@ def self.auth_enabled(config = nil) end # Mongo Command Wrapper - def self.mongo_eval(cmd, db = 'admin', retries = 2, host = nil) + def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil) retry_count = retries - retry_sleep = 1 + retry_sleep = 3 cmd = mongorc_file + cmd if mongorc_file out = nil @@ -179,14 +178,12 @@ def self.mongo_eval(cmd, db = 'admin', retries = 2, host = nil) 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 = 2, host = nil) + def mongo_eval(cmd, db = 'admin', retries = 10, host = nil) self.class.mongo_eval(cmd, db, retries, host) end diff --git a/lib/puppet/provider/mongodb_database/mongodb.rb b/lib/puppet/provider/mongodb_database/mongodb.rb index 50909066d..641078cd7 100644 --- a/lib/puppet/provider/mongodb_database/mongodb.rb +++ b/lib/puppet/provider/mongodb_database/mongodb.rb @@ -9,22 +9,7 @@ def self.instances require 'json' - # pre_cmd = ' - # try { - # version=parseInt(db.version().split(\'.\')[0]) - # if (version>=5){ - # db.getMongo().setReadPref(\'nearest\') - # } - # else{ - # rs.secondaryOk() - # } - # } - # catch (err) { - # rs.slaveOk() - # } - # '.squeeze(' ') mongo_eval_result = mongo_eval("JSON.stringify(db.getMongo().getDBs())") - Puppet.warning "#### DBS #{mongo_eval_result} ####" dbs = JSON.parse mongo_eval_result @@ -45,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' diff --git a/lib/puppet/provider/mongodb_replset/mongo.rb b/lib/puppet/provider/mongodb_replset/mongo.rb index e88ec102f..fe1bd14b3 100644 --- a/lib/puppet/provider/mongodb_replset/mongo.rb +++ b/lib/puppet/provider/mongodb_replset/mongo.rb @@ -156,7 +156,7 @@ def get_hosts_status(members) status = rs_status(host) raise Puppet::Error, "Can't configure replicaset #{name}, host #{host} is not supposed to be part of a replicaset." if status.key?('errmsg') && status['errmsg'] == 'not running with --replSet' - if auth_enabled && status.key?('errmsg') && (status['errmsg'].include?('unauthorized') || status['errmsg'].include?('not authorized') || status['errmsg'].include?('requires authentication')) || status.include?('command replSetGetStatus requires authentication') + if auth_enabled && status.key?('errmsg') && (status['errmsg'].include?('unauthorized') || status['errmsg'].include?('not authorized') || status['errmsg'].include?('requires authentication')) Puppet.warning "Host #{host} is available, but you are unauthorized because of authentication is enabled: #{auth_enabled}" alive.push(member) end diff --git a/lib/puppet/util/mongodb_output.rb b/lib/puppet/util/mongodb_output.rb deleted file mode 100644 index b6f26cbf2..000000000 --- a/lib/puppet/util/mongodb_output.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -module Puppet - module Util - module MongodbOutput - def self.sanitize(data) - # Dirty hack to remove JavaScript objects - data.gsub!(%r{\w+\((\d+).+?\)}, '\1') # Remove extra parameters from 'Timestamp(1462971623, 1)' Objects - data.gsub!(%r{\w+\((.+?)\)}, '\1') - - data.gsub!(%r{^Error:.+}, '') - data.gsub!(%r{^.*warning:.+}, '') # remove warnings if sslAllowInvalidHostnames is true - data.gsub!(%r{^.*The server certificate does not match the host name.+}, '') # remove warnings if sslAllowInvalidHostnames is true mongo 3.x - data - end - end - end -end diff --git a/manifests/server.pp b/manifests/server.pp index 4e97f8bc8..7931bc828 100644 --- a/manifests/server.pp +++ b/manifests/server.pp @@ -127,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 => $auth_mechanism, + password => $admin_password, } # Make sure it runs before other DB creation @@ -170,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'] } } } diff --git a/manifests/server/config.pp b/manifests/server/config.pp index a5c545c0f..448192af4 100644 --- a/manifests/server/config.pp +++ b/manifests/server/config.pp @@ -175,10 +175,10 @@ $admin_password } if $handle_creds { - if versioncmp($version, '6.0') >= 0 { - $major_version_greater6 = true + if versioncmp($version, '5.0') >= 0 { + $major_version_greater5 = true } else { - $major_version_greater6 = false + $major_version_greater5 = false } file { $rcfile: ensure => file, diff --git a/manifests/server/install.pp b/manifests/server/install.pp index 4a03f1b25..9e4d36da0 100644 --- a/manifests/server/install.pp +++ b/manifests/server/install.pp @@ -36,7 +36,7 @@ } unless defined(Package[$mongosh_package_name]) { package { 'mongodb_mongosh': - ensure => latest, + ensure => present, name => $mongosh_package_name, tag => 'mongodb_mongosh_package', } diff --git a/spec/unit/puppet/util/mongodb_output_spec.rb b/spec/unit/puppet/util/mongodb_output_spec.rb deleted file mode 100644 index fb3a1219d..000000000 --- a/spec/unit/puppet/util/mongodb_output_spec.rb +++ /dev/null @@ -1,67 +0,0 @@ -require 'spec_helper' # rubocop:todo Style/FrozenStringLiteralComment Frozen strings break the regex below -require 'puppet/util/mongodb_output' -require 'json' - -describe Puppet::Util::MongodbOutput do - let(:bson_data) do - <<-EOT - { - "setName": "rs_test", - "ismaster": true, - "secondary": false, - "hosts": [ - "mongo1:27017" - ], - "primary": "mongo1:27017", - "me": "mongo1:27017", - "maxBsonObjectSize": 16777216, - "maxMessageSizeBytes": 48000000, - "hash": BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), - "keyId": NumberLong(0), - "clusterTime": Timestamp(1538381287, 1), - "replicaSetId": ObjectId("5bb1d270137a581ebd3d61f2"), - "slaveDelay": NumberLong(-1), - "majorityWriteDate": ISODate("2018-10-01T08:08:01Z"), - "lastHeartbeat": ISODate("2018-10-01T08:08:05.859Z"), - "ok": 1 - } - EOT - end - - let(:json_data) do - <<-EOT - { - "setName": "rs_test", - "ismaster": true, - "secondary": false, - "hosts": [ - "mongo1:27017" - ], - "primary": "mongo1:27017", - "me": "mongo1:27017", - "maxBsonObjectSize": 16777216, - "maxMessageSizeBytes": 48000000, - "hash": 0, - "keyId": 0, - "clusterTime": 1538381287, - "replicaSetId": "5bb1d270137a581ebd3d61f2", - "slaveDelay": -1, - "majorityWriteDate": "2018-10-01T08:08:01Z", - "lastHeartbeat": "2018-10-01T08:08:05.859Z", - "ok": 1 - } - EOT - end - - describe '.sanitize' do - it 'returns a valid json' do - sanitized_json = described_class.sanitize(bson_data) - expect { JSON.parse(sanitized_json) }.not_to raise_error - end - - it 'replaces data types' do - sanitized_json = described_class.sanitize(bson_data) - expect(JSON.parse(sanitized_json)).to include(JSON.parse(json_data)) - end - end -end diff --git a/templates/mongorc.js.erb b/templates/mongorc.js.erb deleted file mode 100644 index 9e060306f..000000000 --- a/templates/mongorc.js.erb +++ /dev/null @@ -1,49 +0,0 @@ -function rsReconfigMember(member){ - var cfg = rs.config() - cfg.members.forEach(function(part,index,memberArray){ - if (member.host == part.host) { - for(k in member){ - memberArray[index][k] = member[k] - } - } - }) - return rs.reconfig(cfg) -} - -function rsReconfigSettings(settings){ - var cfg = rs.config() - cfg.settings = settings - return rs.reconfig(cfg) -} - -<% if @auth and @store_creds -%> -function authRequired() { - try { - return db.serverCmdLineOpts().code == 13; - } catch (err) { - return false; - } -} - -if (authRequired()) { - <%- if @replset -%> - // rs.slaveOk has been deprecated, use secondaryOk if available - try { - rs.secondaryOk() - } - catch (err) { - rs.slaveOk() - } - <%- end -%> - 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... - abort('Unknown error') - } -} -<% end -%> diff --git a/templates/mongoshrc.js.erb b/templates/mongoshrc.js.erb index 72e6d7068..f21cb4f7c 100644 --- a/templates/mongoshrc.js.erb +++ b/templates/mongoshrc.js.erb @@ -30,7 +30,7 @@ if (authRequired()) { <%- if @replset -%> // rs.slaveOk + rs secondaryOk have been deprecated, use setReadPref when running mongodb version >=5 try { - <%- if @major_version_greater6 -%> + <%- if @major_version_greater5 -%> db.getMongo().setReadPref('nearest') <%- else -%> rs.secondaryOk() @@ -56,10 +56,8 @@ if (authRequired()) { } <%- else -%> try { - var prev_db = db - db = db.getSiblingDB('admin') - db.auth('<%= @admin_username %>', '<%= @admin_password_unsensitive %>') - db = db.getSiblingDB(prev_db) + admin = db.getSiblingDB('admin') + admin.auth('<%= @admin_username %>', '<%= @admin_password_unsensitive %>') } catch (err) { // This isn't catching authentication errors as I'd expect...