Skip to content

Commit a3fc533

Browse files
committed
Connect to localhost in provider when possible
The provider previously used the external IP to connect instead of the loopback. When using the loopback, the option `enableLocalhostAuthBypass` (true by default) allows to bypass authentication when a user doesn't yet exists. Which is exactly what would prevent us from executing administrative tasks (like setting up the replicaset) when auth is enabled, but not yet set up. We can only connect to localhost when MongoDB is listening on either the 'bind all' address (0.0.0.0 or ::0) or the local loopback address (127.0.0.1 or ::1).
1 parent 0a03dbf commit a3fc533

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/puppet/provider/mongodb.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ def self.conn_string
111111
first_ip_in_list = bindip.split(',').first
112112
ip_real = case first_ip_in_list
113113
when '0.0.0.0'
114-
Facter.value(:fqdn)
114+
'127.0.0.1'
115115
when %r{\[?::0\]?}
116-
Facter.value(:fqdn)
116+
'::1'
117117
else
118118
first_ip_in_list
119119
end
@@ -135,6 +135,10 @@ def self.conn_string
135135
"#{ip_real}:#{port_real}"
136136
end
137137

138+
def conn_string
139+
self.class.conn_string
140+
end
141+
138142
def self.db_ismaster
139143
cmd_ismaster = 'db.isMaster().ismaster'
140144
db = 'admin'

lib/puppet/provider/mongodb_replset/mongo.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,11 @@ def get_hosts_status(members)
154154
host = member['host']
155155
Puppet.debug "Checking replicaset member #{host} ..."
156156
begin
157-
status = rs_status(host)
157+
status = if host.split(':').first == Facter.value(:fqdn)
158+
rs_status(conn_string)
159+
else
160+
rs_status(host)
161+
end
158162

159163
if status.key?('set')
160164
raise Puppet::Error, "Can't configure replicaset #{name}, host #{host} is already part of another replicaset." if status['set'] != name

0 commit comments

Comments
 (0)