Skip to content

Commit

Permalink
new feature:
Browse files Browse the repository at this point in the history
- sslAllowInvalidHostnames
AUTH and SSL support added
  • Loading branch information
Thomas Tischner committed May 19, 2017
1 parent 4a927d1 commit 103e568
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 4 deletions.
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\"")
else
'not_responding'
end
Expand Down
10 changes: 10 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,7 @@ def self.db_ismaster
out.gsub!(/ObjectId\(([^)]*)\)/, '\1')
out.gsub!(/ISODate\((.+?)\)/, '\1 ')
out.gsub!(/^Error\:.+/, '')
out.gsub!(/^.*warning\:.+/, '') # remove warnings if sslAllowInvalidHostnames is true
res = JSON.parse out

return res['ismaster']
Expand Down Expand Up @@ -185,6 +194,7 @@ 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
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"

# Parse the JSON output and return
JSON.parse(output)
Expand Down
2 changes: 2 additions & 0 deletions manifests/server.pp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
$ssl_key = undef,
$ssl_ca = undef,
$ssl_weak_cert = false,
$ssl_invalid_hostnames = false,
$restart = $mongodb::params::restart,
$storage_engine = undef,

Expand All @@ -97,6 +98,7 @@
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
3 changes: 3 additions & 0 deletions manifests/server/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
$ssl_key = $mongodb::server::ssl_key
$ssl_ca = $mongodb::server::ssl_ca
$ssl_weak_cert = $mongodb::server::ssl_weak_cert
$ssl_invalid_hostnames = $mongodb::server::ssl_invalid_hostnames
$storage_engine = $mongodb::server::storage_engine
$version = $mongodb::server::version

Expand Down Expand Up @@ -145,6 +146,7 @@
# - $ssl_ca
# - $ssl_key
# - $ssl_weak_cert
# - $ssl_invalid_hostnames
# - $syslog
# - $system_logrotate
# - $verbose
Expand Down Expand Up @@ -201,6 +203,7 @@
# - $ssl_ca
# - $ssl_key
# - $ssl_weak_cert
# - $ssl_invalid_hostnames
# - storage_engine_internal
# - $syslog
# - $verbose
Expand Down
5 changes: 4 additions & 1 deletion templates/mongodb.conf.2.6.erb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ security.javascriptEnabled: <%= @noscripting %>

#Net
<% if @ipv6 -%>
net.ipv6=<%= @ipv6 %>
net.ipv6: <%= @ipv6 %>
<% end -%>
<% if @bind_ip -%>
net.bindIp: <%= Array(@bind_ip).join(',') %>
Expand Down Expand Up @@ -117,6 +117,9 @@ net.ssl.CAFile: <%= @ssl_ca %>
<% if @ssl_weak_cert -%>
net.ssl.weakCertificateValidation: <%= @ssl_weak_cert %>
<% end -%>
<% if @ssl_invalid_hostnames -%>
net.ssl.allowInvalidHostnames: <%= @ssl_invalid_hostnames %>
<% end -%>
<% end -%>

#Replication
Expand Down
3 changes: 3 additions & 0 deletions templates/mongodb.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,7 @@ sslCAFile = <%= @ssl_ca %>
# - after 3.0.0: sslAllowConnectionsWithoutCertificates
sslWeakCertificateValidation = <%= @ssl_weak_cert %>
<% end -%>
<% if @ssl_invalid_hostnames -%>
net.ssl.allowInvalidHostnames = <%= @ssl_invalid_hostnames %>
<% end -%>
<% end -%>

0 comments on commit 103e568

Please sign in to comment.