Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MODULES-5483: Auth and FQDN certs => Fail #369

Merged
merged 5 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,10 @@ Default: <>
Set to true to disable mandatory SSL client authentication
Default: False

##### `ssl_invalid_hostnames`
Set to true to disable fqdn SSL cert check
Default: False

##### `service_manage`
Whether or not the MongoDB service resource should be part of the catalog.
Default: true
Expand Down
29 changes: 26 additions & 3 deletions lib/facter/is_master.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ def get_mongod_conf_file
unless config['net.port'].nil?
mongoPort = "--port #{config['net.port']}"
end
if config['net.ssl.mode'] == "requireSSL"
ssl = "--ssl --host #{Facter.value(:fqdn)}"
end
unless config['net.ssl.PEMKeyFile'].nil?
sslkey = "--sslPEMKeyFile #{config['net.ssl.PEMKeyFile']}"
end
unless config['net.ssl.CAFile'].nil?
sslca = "--sslCAFile #{config['net.ssl.CAFile']}"
end
unless config['net.ipv6'].nil?
ipv6 = "--ipv6"
end
else # It has to be a key-value config file
config = {}
File.readlines(file).collect do |line|
Expand All @@ -29,15 +41,26 @@ def get_mongod_conf_file
unless config['port'].nil?
mongoPort = "--port #{config['port']}"
end
if config['ssl'] == "requireSSL"
ssl = "--ssl --host #{Facter.value(:fqdn)}"
end
unless config['sslcert'].nil?
sslkey = "--sslPEMKeyFile #{config['sslcert']}"
end
unless config['sslca'].nil?
sslca = "--sslCAFile #{config['sslca']}"
end
unless config['ipv6'].nil?
ipv6 = "--ipv6"
end
end
e = File.exists?('/root/.mongorc.js') ? 'load(\'/root/.mongorc.js\'); ' : ''

# Check if the mongodb server is responding:
Facter::Core::Execution.exec("mongo --quiet #{mongoPort} --eval \"#{e}printjson(db.adminCommand({ ping: 1 }))\"")
Facter::Core::Execution.exec("mongo --quiet #{ssl} #{sslkey} #{sslca} #{ipv6} #{mongoPort} --eval \"#{e}printjson(db.adminCommand({ ping: 1 }))\"")

if $?.success?
mongo_output = Facter::Core::Execution.exec("mongo --quiet #{mongoPort} --eval \"#{e}printjson(db.isMaster())\"")
JSON.parse(mongo_output.gsub(/\w+\(.+?\)/, '"foo"'))['ismaster'] ||= false
Facter::Core::Execution.exec("mongo --quiet #{ssl} #{sslkey} #{sslca} #{ipv6} #{mongoPort} --eval \"#{e}db.isMaster().ismaster\"")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this changes behavior such that previously it returned true or false and now would return 'true' or 'false' as strings. Is this intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic was implemented by #348 and i just copied it.
All i can say is that it works as expected:

[root@grldb01vp ~]# puppet facts | grep is_mas
    "mongodb_is_master": "false",
[root@grldb02vp ~]# puppet facts | grep is_mas
    "mongodb_is_master": "false",
[root@grldb03vp ~]# puppet facts | grep is_mas
    "mongodb_is_master": "true",

else
'not_responding'
end
Expand Down
12 changes: 12 additions & 0 deletions lib/puppet/provider/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def self.get_mongo_conf
config_hash['bindip'] = config['net.bindIp']
config_hash['port'] = config['net.port']
config_hash['ipv6'] = config['net.ipv6']
config_hash['allowInvalidHostnames'] = config['net.ssl.allowInvalidHostnames']
config_hash['ssl'] = config['net.ssl.mode']
config_hash['sslcert'] = config['net.ssl.PEMKeyFile']
config_hash['sslca'] = config['net.ssl.CAFile']
Expand All @@ -57,6 +58,7 @@ def self.get_mongo_conf
config_hash['port'] = config['port']
config_hash['ipv6'] = config['ipv6']
config_hash['ssl'] = config['sslOnNormalPorts']
config_hash['allowInvalidHostnames'] = config['allowInvalidHostnames']
config_hash['sslcert'] = config['sslPEMKeyFile']
config_hash['sslca'] = config['sslCAFile']
config_hash['auth'] = config['auth']
Expand All @@ -78,11 +80,17 @@ def self.ssl_is_enabled(config=nil)
ssl_mode.nil? ? false : ssl_mode != 'disabled'
end

def self.ssl_invalid_hostnames(config=nil)
config ||= get_mongo_conf
config['allowInvalidHostnames']
end

def self.mongo_cmd(db, host, cmd)
config = get_mongo_conf

args = [db, '--quiet', '--host', host]
args.push('--ipv6') if ipv6_is_enabled(config)
args.push('--sslAllowInvalidHostnames') if ssl_invalid_hostnames(config)

if ssl_is_enabled(config)
args.push('--ssl')
Expand Down Expand Up @@ -139,6 +147,8 @@ def self.db_ismaster
out.gsub!(/ObjectId\(([^)]*)\)/, '\1')
out.gsub!(/ISODate\((.+?)\)/, '\1 ')
out.gsub!(/^Error\:.+/, '')
out.gsub!(/^.*warning\:.+/, '') # remove warnings if sslAllowInvalidHostnames is true
out.gsub!(/^.*The server certificate does not match the host name.+/, '') # remove warnings if sslAllowInvalidHostnames is true mongo 3.x
res = JSON.parse out

return res['ismaster']
Expand Down Expand Up @@ -185,6 +195,8 @@ def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
out.gsub!(/#{data_type}\(([^)]*)\)/, '\1')
end
out.gsub!(/^Error\:.+/, '')
out.gsub!(/^.*warning\:.+/, '') # remove warnings if sslAllowInvalidHostnames is true
out.gsub!(/^.*The server certificate does not match the host name.+/, '') # remove warnings if sslAllowInvalidHostnames is true mongo 3.x
out
end

Expand Down
1 change: 1 addition & 0 deletions lib/puppet/provider/mongodb_replset/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ def self.mongo_command(command, host=nil, retries=4)

#Hack to avoid non-json empty sets
output = "{}" if output == "null\n"
output = "{}" if output == "\nnull\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this change? Could you describe it as part of the commit message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a mongo connection with --sslAllowInvalidHostnames is made, it prints a warning like:
2017-05-16T10:56:27.679+0200 warning: The server certificate does not match the host name 127.0.0.1
This warning gets filtered but there is still a newline and if the return is null, the content will be:
"\nnull\n"
So this just extends the already implemented logic to still work with the new feature.


# Parse the JSON output and return
JSON.parse(output)
Expand Down
175 changes: 85 additions & 90 deletions manifests/server.pp
Original file line number Diff line number Diff line change
@@ -1,102 +1,97 @@
# This installs a MongoDB server. See README.md for more details.
class mongodb::server (
$ensure = $mongodb::params::ensure,

$user = $mongodb::params::user,
$group = $mongodb::params::group,

$config = $mongodb::params::config,
$dbpath = $mongodb::params::dbpath,
$dbpath_fix = $mongodb::params::dbpath_fix,
$pidfilepath = $mongodb::params::pidfilepath,
$pidfilemode = $mongodb::params::pidfilemode,
$manage_pidfile = $mongodb::params::manage_pidfile,
$rcfile = $mongodb::params::rcfile,

$service_manage = $mongodb::params::service_manage,
$service_provider = $mongodb::params::service_provider,
$service_name = $mongodb::params::service_name,
$service_enable = $mongodb::params::service_enable,
$service_ensure = $mongodb::params::service_ensure,
$service_status = $mongodb::params::service_status,

$package_ensure = $mongodb::params::package_ensure,
$package_name = $mongodb::params::server_package_name,

$logpath = $mongodb::params::logpath,
$bind_ip = $mongodb::params::bind_ip,
$ipv6 = undef,
$logappend = true,
$system_logrotate = undef,
$fork = $mongodb::params::fork,
$port = undef,
$journal = $mongodb::params::journal,
$nojournal = undef,
$smallfiles = undef,
$cpu = undef,
$auth = false,
$noauth = undef,
$verbose = undef,
$verbositylevel = undef,
$objcheck = undef,
$quota = undef,
$quotafiles = undef,
$diaglog = undef,
$directoryperdb = undef,
$profile = undef,
$maxconns = undef,
$oplog_size = undef,
$nohints = undef,
$nohttpinterface = undef,
$noscripting = undef,
$notablescan = undef,
$noprealloc = undef,
$nssize = undef,
$mms_token = undef,
$mms_name = undef,
$mms_interval = undef,
$replset = undef,
$replset_config = undef,
$replset_members = undef,
$configsvr = undef,
$shardsvr = undef,
$rest = undef,
$quiet = undef,
$slowms = undef,
$keyfile = undef,
$key = undef,
$set_parameter = undef,
$syslog = undef,
$config_content = undef,
$config_template = undef,
$ssl = undef,
$ssl_key = undef,
$ssl_ca = undef,
$ssl_weak_cert = false,
$restart = $mongodb::params::restart,
$storage_engine = undef,

$create_admin = $mongodb::params::create_admin,
$admin_username = $mongodb::params::admin_username,
$admin_password = undef,
$store_creds = $mongodb::params::store_creds,
$admin_roles = ['userAdmin', 'readWrite', 'dbAdmin',
'dbAdminAnyDatabase', 'readAnyDatabase',
'readWriteAnyDatabase', 'userAdminAnyDatabase',
'clusterAdmin', 'clusterManager', 'clusterMonitor',
'hostManager', 'root', 'restore'],

$ensure = $mongodb::params::ensure,
$user = $mongodb::params::user,
$group = $mongodb::params::group,
$config = $mongodb::params::config,
$dbpath = $mongodb::params::dbpath,
$dbpath_fix = $mongodb::params::dbpath_fix,
$pidfilepath = $mongodb::params::pidfilepath,
$pidfilemode = $mongodb::params::pidfilemode,
$manage_pidfile = $mongodb::params::manage_pidfile,
$rcfile = $mongodb::params::rcfile,
$service_manage = $mongodb::params::service_manage,
$service_provider = $mongodb::params::service_provider,
$service_name = $mongodb::params::service_name,
$service_enable = $mongodb::params::service_enable,
$service_ensure = $mongodb::params::service_ensure,
$service_status = $mongodb::params::service_status,
$package_ensure = $mongodb::params::package_ensure,
$package_name = $mongodb::params::server_package_name,
$logpath = $mongodb::params::logpath,
$bind_ip = $mongodb::params::bind_ip,
$ipv6 = undef,
$logappend = true,
$system_logrotate = undef,
$fork = $mongodb::params::fork,
$port = undef,
$journal = $mongodb::params::journal,
$nojournal = undef,
$smallfiles = undef,
$cpu = undef,
$auth = false,
$noauth = undef,
$verbose = undef,
$verbositylevel = undef,
$objcheck = undef,
$quota = undef,
$quotafiles = undef,
$diaglog = undef,
$directoryperdb = undef,
$profile = undef,
$maxconns = undef,
$oplog_size = undef,
$nohints = undef,
$nohttpinterface = undef,
$noscripting = undef,
$notablescan = undef,
$noprealloc = undef,
$nssize = undef,
$mms_token = undef,
$mms_name = undef,
$mms_interval = undef,
$replset = undef,
$replset_config = undef,
$replset_members = undef,
$configsvr = undef,
$shardsvr = undef,
$rest = undef,
$quiet = undef,
$slowms = undef,
$keyfile = undef,
$key = undef,
$set_parameter = undef,
$syslog = undef,
$config_content = undef,
$config_template = undef,
$ssl = undef,
$ssl_key = undef,
$ssl_ca = undef,
$ssl_weak_cert = false,
$ssl_invalid_hostnames = false,
$restart = $mongodb::params::restart,
$storage_engine = undef,
$create_admin = $mongodb::params::create_admin,
$admin_username = $mongodb::params::admin_username,
$admin_password = undef,
$store_creds = $mongodb::params::store_creds,
$admin_roles = ['userAdmin', 'readWrite', 'dbAdmin',
'dbAdminAnyDatabase', 'readAnyDatabase',
'readWriteAnyDatabase', 'userAdminAnyDatabase',
'clusterAdmin', 'clusterManager', 'clusterMonitor',
'hostManager', 'root', 'restore'],
# Deprecated parameters
$master = undef,
$slave = undef,
$only = undef,
$source = undef,
$master = undef,
$slave = undef,
$only = undef,
$source = undef,
) inherits mongodb::params {


if $ssl {
validate_string($ssl_key, $ssl_ca)
validate_bool($ssl_weak_cert)
validate_bool($ssl_invalid_hostnames)
}

if ($ensure == 'present' or $ensure == true) {
Expand Down
Loading