Skip to content

Commit 8be9ff3

Browse files
author
Johan De Wit
committed
No authentication possible when no user is yet created
1 parent 92e334e commit 8be9ff3

File tree

4 files changed

+142
-20
lines changed

4 files changed

+142
-20
lines changed

lib/puppet/provider/mongodb.rb

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def self.tls_invalid_hostnames(config = nil)
7575
end
7676

7777
def self.mongosh_cmd(db, host, cmd)
78+
Puppet.debug("MONGODB: in self.mongosh_cmd")
7879
config = mongo_conf
7980

8081
args = [db, '--quiet', '--host', host]
@@ -101,7 +102,10 @@ def self.mongosh_cmd(db, host, cmd)
101102
end
102103

103104
args += ['--eval', cmd]
104-
mongosh(args)
105+
Puppet.debug("MONGODB: in self.mongosh_cmd executing with args #{args}")
106+
out = mongosh(args)
107+
Puppet.debug("MONGODB: in self.mongosh_cmd results in #{out}")
108+
out
105109
end
106110

107111
def self.conn_string
@@ -140,6 +144,14 @@ def self.db_ismaster
140144
cmd_ismaster = mongoshrc_file + cmd_ismaster if mongoshrc_file
141145
db = 'admin'
142146
res = mongosh_cmd(db, conn_string, cmd_ismaster).to_s.split(%r{\n}).last.chomp
147+
148+
# Retry command without authentication when mongorc_file is set and authentication failed
149+
if mongorc_file && res =~ %r{Authentication failed}
150+
res = mongosh_cmd(db, conn_string, 'db.isMaster().ismaster').to_s.chomp
151+
end
152+
153+
Puppet.debug("In self.db_is_master with res is #{res}")
154+
143155
res.eql?('true')
144156
end
145157

@@ -153,29 +165,65 @@ def self.auth_enabled(config = nil)
153165
config['auth'] && config['auth'] != 'disabled'
154166
end
155167

168+
def self.rs_initiated?
169+
cmd_status = "rs.status('localhost').set"
170+
cmd_status = mongoshrc_file + cmd_status if mongoshrc_file
171+
db = 'admin'
172+
res = mongosh_cmd(db, conn_string, cmd_ismaster).to_s.split(%r{\n}).last.chomp
173+
Puppet.debug("MONGODB: In self.rs_initiated? with res is #{res}")
174+
175+
# Retry command without authentication when mongorc_file is set and authentication failed
176+
if mongorc_file && res =~ %r{Authentication failed}
177+
res = mongosh_cmd(db, conn_string, "rs.status('localhost').set").to_s.chomp
178+
end
179+
Puppet.debug("MONGODB: In self.rs_initiated? without auth res is #{res}")
180+
181+
res == @resource[:name]
182+
end
183+
184+
def rs_initiated?
185+
self.rs_initiated?
186+
end
187+
156188
# Mongo Command Wrapper
157189
def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
190+
Puppet.debug("MONGODB: in self.mongo_eval with cmd is #{cmd}")
158191
retry_count = retries
159192
retry_sleep = 3
193+
no_auth_cmd = cmd
160194
cmd = mongoshrc_file + cmd if mongoshrc_file
161195

162196
out = nil
163197
begin
198+
Puppet.debug("MONGODB: in self.mongo_eval in BEGIN")
164199
out = if host
165200
mongosh_cmd(db, host, cmd)
166201
else
167202
mongosh_cmd(db, conn_string, cmd)
168203
end
169204
rescue StandardError => e
170-
retry_count -= 1
171-
if retry_count.positive?
172-
Puppet.debug "Request failed: '#{e.message}' Retry: '#{retries - retry_count}'"
173-
sleep retry_sleep
174-
retry
205+
Puppet.debug("MONGODB: in self.mongo_eval rescue with error is #{e}")
206+
# When using the rc file, we get this eror because in most cases the admin user is not created yet
207+
# Can/must we move this out of the resue block ?
208+
if self.auth_enabled && e.message =~ %r{Authentication failed}
209+
out = if host
210+
mongosh_cmd(db, host, no_auth_cmd)
211+
else
212+
mongosh_cmd(db, conn_string, no_auth_cmd)
213+
end
214+
Puppet.debug("MONGODB: in self.mongo_eval rescue inside if with out is #{out}")
215+
else
216+
retry_count -= 1
217+
if retry_count.positive?
218+
Puppet.debug "Request failed: '#{e.message}' Retry: '#{retries - retry_count}'"
219+
sleep retry_sleep
220+
retry
221+
end
175222
end
176223
end
177224

178-
raise Puppet::ExecutionFailure, "Could not evaluate MongoDB shell command: #{cmd}" unless out
225+
# return also the error message, so caller can react on it
226+
raise Puppet::ExecutionFailure, "Could not evaluate MongoDB shell command: #{cmd} with #{e.message}" unless out
179227

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

lib/puppet/provider/mongodb_database/mongodb.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ def self.instances
1616
new(name: db['name'],
1717
ensure: :present)
1818
end
19+
rescue => e
20+
Puppet.warning("Getting instances of mongodb_database failed: #{e}")
21+
[]
1922
end
2023

2124
# Assign prefetched dbs based on name.

0 commit comments

Comments
 (0)