Skip to content

Commit 7b35131

Browse files
author
Johan De Wit
committed
[authentication] make ismaster work with authentication enbaled before user admin is created
1 parent bcad078 commit 7b35131

File tree

4 files changed

+43
-17
lines changed

4 files changed

+43
-17
lines changed

lib/facter/is_master.rb

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,71 @@ def get_options_from_hash_config(config)
1616
# - sslMode is "requireSSL"
1717
# - Parameter --sslPEMKeyFile is set
1818
# - Parameter --sslCAFile is set
19-
result << "--ssl --host #{Facter.value(:fqdn)}" if ['allowSSL', 'preferSSL', 'requireSSL'].include? config['net.ssl.mode'] || !config['net.ssl.PEMKeyFile'].nil? || !config['net.ssl.CAFile'].nil?
19+
result << "--ssl --host #{Facter.value(:fqdn)}" if config['net.ssl.mode'] == 'requireSSL' || !config['net.ssl.PEMKeyFile'].nil? || !config['net.ssl.CAFile'].nil?
2020
result << "--sslPEMKeyFile #{config['net.ssl.PEMKeyFile']}" unless config['net.ssl.PEMKeyFile'].nil?
2121
result << "--sslCAFile #{config['net.ssl.CAFile']}" unless config['net.ssl.CAFile'].nil?
22-
result << "--sslAllowInvalidHostnames" if config['net.ssl.allowInvalidHostnames'] == true
2322
# use --tls and --host if:
2423
# - tlsMode is "requireTLS"
2524
# - Parameter --tlsCertificateKeyFile is set
2625
# - Parameter --tlsCAFile is set
27-
result << "--tls --host #{Facter.value(:fqdn)}" if ['allowTLS', 'prefeTLS', 'requireTLS'].include? config['net.tls.mode'] || !config['net.tls.certificateKeyFile'].nil? || !config['net.tls.CAFile'].nil?
26+
result << "--tls --host #{Facter.value(:fqdn)}" if config['net.tls.mode'] == 'requireTLS' || !config['net.tls.certificateKeyFile'].nil? || !config['net.tls.CAFile'].nil?
2827
result << "--tlsCertificateKeyFile #{config['net.tls.certificateKeyFile']}" unless config['net.tls.certificateKeyFile'].nil?
2928
result << "--tlsCAFile #{config['net.tls.CAFile']}" unless config['net.tls.CAFile'].nil?
30-
result << "--tlsAllowInvalidHostnames" if config['net.tls.allowInvalidHostnames'] == true
3129

3230
result << '--ipv6' unless config['net.ipv6'].nil?
3331

3432
result.join(' ')
3533
end
3634

35+
def get_options_from_keyvalue_config(file)
36+
config = {}
37+
File.readlines(file).map do |line|
38+
k, v = line.split('=')
39+
config[k.rstrip] = v.lstrip.chomp if k && v
40+
end
41+
42+
result = []
43+
44+
result << "--port #{config['port']}" unless config['port'].nil?
45+
# use --ssl and --host if:
46+
# - sslMode is "requireSSL"
47+
# - Parameter --sslPEMKeyFile is set
48+
# - Parameter --sslCAFile is set
49+
result << "--ssl --host #{Facter.value(:fqdn)}" if config['ssl'] == 'requireSSL' || !config['sslcert'].nil? || !config['sslca'].nil?
50+
result << "--sslPEMKeyFile #{config['sslcert']}" unless config['sslcert'].nil?
51+
result << "--sslCAFile #{config['sslca']}" unless config['sslca'].nil?
52+
# use --tls and --host if:
53+
# - tlsMode is "requireTLS"
54+
# - Parameter --tlsCertificateKeyFile is set
55+
# - Parameter --tlsCAFile is set
56+
result << "--tls --host #{Facter.value(:fqdn)}" if config['tls'] == 'requireTLS' || !config['tlscert'].nil? || !config['tlsca'].nil?
57+
result << "--tlsCertificateKeyFile #{config['tlscert']}" unless config['tlscert'].nil?
58+
result << "--tlsCAFile #{config['tlsca']}" unless config['tlsca'].nil?
59+
60+
result << '--ipv6' unless config['ipv6'].nil?
61+
62+
result.join(' ')
63+
end
64+
3765
def get_options_from_config(file)
3866
config = YAML.load_file(file)
39-
get_options_from_hash_config(config)
67+
if config.is_a?(Hash) # Using a valid YAML file for mongo 2.6
68+
get_options_from_hash_config(config)
69+
else # It has to be a key-value config file
70+
get_options_from_keyvalue_config(file)
71+
end
4072
end
4173

4274
Facter.add('mongodb_is_master') do
4375
setcode do
44-
if %w[mongo mongod].all? { |m| Facter::Util::Resolution.which m }
76+
if %w[mongosh mongod].all? { |m| Facter::Util::Resolution.which m }
4577
file = mongod_conf_file
4678
if file
4779
options = get_options_from_config(file)
4880
e = File.exist?('/root/.mongoshrc.js') ? 'load(\'/root/.mongoshrc.js\'); ' : ''
4981

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

5385
if $CHILD_STATUS.success?
5486
Facter::Core::Execution.exec("mongosh --quiet #{options} --eval \"#{e}db.isMaster().ismaster\"")

lib/facter/mongodb_version.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
Facter.add(:mongodb_version) do
44
setcode do
5-
if Facter::Core::Execution.which('mongo')
6-
mongodb_version = Facter::Core::Execution.execute('mongo --version 2>&1')
7-
%r{MongoDB shell version:?\s+v?([\w.]+)}.match(mongodb_version)[1]
5+
if Facter::Core::Execution.which('mongod')
6+
mongodb_version = Facter::Core::Execution.execute('mongod --version 2>&1')
7+
%r{^db version:?\s+v?([\w.]+)}.match(mongodb_version)[1]
88
end
99
end
1010
end

lib/puppet/provider/mongodb_shard/mongo.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
mk_resource_methods
1515

16-
commands mongo: 'mongo'
16+
commands mongosh: 'mongosh'
1717

1818
def initialize(value = {})
1919
super(value)

lib/puppet/provider/mongodb_user/mongodb.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ def self.instances
1818
return [] if auth_enabled && (out.include?('requires authentication') || out.include?('not authorized on admin'))
1919

2020
users = JSON.parse out
21-
Puppet.debug("Result of users in self.instances: #{users}")
22-
Puppet.debug("Type of users in self.instances: #{users.class}")
2321

2422
users.map do |user|
25-
Puppet.debug("Fetching user #{user}")
2623
db = if user['db'] == '$external'
2724
# For external users, we need to retreive the original DB name from here.
2825
user['customData']['createdBy'][%r{.* (.*)'\]$}, 1]
@@ -55,7 +52,6 @@ def self.prefetch(resources)
5552
mk_resource_methods
5653

5754
def create
58-
Puppet.debug("In mongodb_user.create. Only works when on the primery node")
5955
if db_ismaster
6056
password_hash = @resource[:password_hash]
6157
password_hash = Puppet::Util::MongodbMd5er.md5(@resource[:username], @resource[:password]) if !password_hash && @resource[:password]
@@ -85,10 +81,8 @@ def create
8581
end
8682

8783
if @resource[:auth_mechanism] == :x509
88-
Puppet.debug("Creating user for x509 with command #{command}")
8984
mongo_eval("db.getSiblingDB(\"$external\").runCommand(#{command.to_json})", @resource[:database])
9085
else
91-
Puppet.debug("Creating user for with command #{command}")
9286
mongo_eval("db.runCommand(#{command.to_json})", @resource[:database])
9387
end
9488

0 commit comments

Comments
 (0)