Skip to content

Commit fa66548

Browse files
committed
Drop support for mongodb versions before 2.6
1 parent ea3feb2 commit fa66548

7 files changed

Lines changed: 92 additions & 434 deletions

File tree

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ When `manage_package_repo` is set to true, this setting indicates if it will
219219
use the Community Edition (false, the default) or the Enterprise one (true).
220220

221221
##### `version`
222-
The version of MonogDB to install/manage. This is a simple way of providing
223-
a specific version such as '2.2' or '2.4' for example. If not specified,
224-
the module will use the default for your OS distro.
222+
The version of MonogDB to install/manage. This is needed when managing
223+
repositories. If not specified, the module will use the default for your OS
224+
distro.
225225

226226
##### `repo_location`
227227
This setting can be used to override the default MongoDB repository location.
@@ -340,7 +340,6 @@ Default: None
340340
##### `objcheck`
341341
Forces the mongod to validate all requests from clients upon receipt to ensure
342342
that clients never insert invalid documents into the database.
343-
Default: on v2.4 default to true and on earlier version to false
344343

345344
##### `quota`
346345
Set to true to enable a maximum limit for the number of data files each database

lib/puppet/provider/mongodb.rb

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,44 +27,19 @@ def self.mongod_conf_file
2727
end
2828

2929
def self.mongo_conf
30-
file = mongod_conf_file
31-
# The mongo conf is probably a key-value store, even though 2.6 is
32-
# supposed to use YAML, because the config template is applied
33-
# based on $mongodb::globals::version which is the user will not
34-
# necessarily set. This attempts to get the port from both types of
35-
# config files.
36-
config = YAML.load_file(file)
37-
config_hash = {}
38-
if config.is_a?(Hash) # Using a valid YAML file for mongo 2.6
39-
config_hash['bindip'] = config['net.bindIp']
40-
config_hash['port'] = config['net.port']
41-
config_hash['ipv6'] = config['net.ipv6']
42-
config_hash['allowInvalidHostnames'] = config['net.ssl.allowInvalidHostnames']
43-
config_hash['ssl'] = config['net.ssl.mode']
44-
config_hash['sslcert'] = config['net.ssl.PEMKeyFile']
45-
config_hash['sslca'] = config['net.ssl.CAFile']
46-
config_hash['auth'] = config['security.authorization']
47-
config_hash['shardsvr'] = config['sharding.clusterRole']
48-
config_hash['confsvr'] = config['sharding.clusterRole']
49-
else # It has to be a key-value config file
50-
config = {}
51-
File.readlines(file).map do |line|
52-
k, v = line.split('=')
53-
config[k.rstrip] = v.lstrip.chomp if k && v
54-
end
55-
config_hash['bindip'] = config['bind_ip']
56-
config_hash['port'] = config['port']
57-
config_hash['ipv6'] = config['ipv6']
58-
config_hash['ssl'] = config['sslOnNormalPorts']
59-
config_hash['allowInvalidHostnames'] = config['allowInvalidHostnames']
60-
config_hash['sslcert'] = config['sslPEMKeyFile']
61-
config_hash['sslca'] = config['sslCAFile']
62-
config_hash['auth'] = config['auth']
63-
config_hash['shardsvr'] = config['shardsvr']
64-
config_hash['confsvr'] = config['confsvr']
65-
end
66-
67-
config_hash
30+
config = YAML.load_file(mongod_conf_file) || {}
31+
{
32+
'bindip' => config['net.bindIp'],
33+
'port' => config['net.port'],
34+
'ipv6' => config['net.ipv6'],
35+
'allowInvalidHostnames' => config['net.ssl.allowInvalidHostnames'],
36+
'ssl' => config['net.ssl.mode'],
37+
'sslcert' => config['net.ssl.PEMKeyFile'],
38+
'sslca' => config['net.ssl.CAFile'],
39+
'auth' => config['security.authorization'],
40+
'shardsvr' => config['sharding.clusterRole'],
41+
'confsvr' => config['sharding.clusterRole']
42+
}
6843
end
6944

7045
def self.ipv6_is_enabled(config = nil)
@@ -192,15 +167,6 @@ def mongo_version
192167
self.class.mongo_version
193168
end
194169

195-
def self.mongo_24?
196-
v = mongo_version
197-
!v[%r{^2\.4\.}].nil?
198-
end
199-
200-
def mongo_24?
201-
self.class.mongo_24?
202-
end
203-
204170
def self.mongo_26?
205171
v = mongo_version
206172
!v[%r{^2\.6\.}].nil?

lib/puppet/provider/mongodb_user/mongodb.rb

Lines changed: 36 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,16 @@ def self.instances
88
require 'json'
99

1010
if db_ismaster
11-
if mongo_24?
12-
dbs = JSON.parse mongo_eval('printjson(db.getMongo().getDBs()["databases"].map(function(db){return db["name"]}))') || 'admin'
13-
14-
allusers = []
15-
16-
dbs.each do |db|
17-
users = JSON.parse mongo_eval('printjson(db.system.users.find().toArray())', db)
18-
19-
allusers += users.map do |user|
20-
new(name: user['_id'],
21-
ensure: :present,
22-
username: user['user'],
23-
database: db,
24-
roles: user['roles'].sort,
25-
password_hash: user['pwd'])
26-
end
27-
end
28-
return allusers
29-
else
30-
users = JSON.parse mongo_eval('printjson(db.system.users.find().toArray())')
31-
32-
users.map do |user|
33-
new(name: user['_id'],
34-
ensure: :present,
35-
username: user['user'],
36-
database: user['db'],
37-
roles: from_roles(user['roles'], user['db']),
38-
password_hash: user['credentials']['MONGODB-CR'],
39-
scram_credentials: user['credentials']['SCRAM-SHA-1'])
40-
end
11+
users = JSON.parse mongo_eval('printjson(db.system.users.find().toArray())')
12+
13+
users.map do |user|
14+
new(name: user['_id'],
15+
ensure: :present,
16+
username: user['user'],
17+
database: user['db'],
18+
roles: from_roles(user['roles'], user['db']),
19+
password_hash: user['credentials']['MONGODB-CR'],
20+
scram_credentials: user['credentials']['SCRAM-SHA-1'])
4121
end
4222
else
4323
Puppet.warning 'User info is available only from master host'
@@ -58,36 +38,23 @@ def self.prefetch(resources)
5838

5939
def create
6040
if db_ismaster
61-
if mongo_24?
62-
if @resource[:password_hash]
63-
raise Puppet::Error, "password_hash can't be set on MongoDB older than 3.0; use password instead"
64-
end
65-
user = {
66-
user: @resource[:username],
67-
pwd: @resource[:password],
68-
roles: @resource[:roles]
69-
}
70-
71-
mongo_eval("db.addUser(#{user.to_json})", @resource[:database])
72-
else
73-
password_hash = @resource[:password_hash]
74-
75-
if password_hash
76-
elsif @resource[:password]
77-
password_hash = Puppet::Util::MongodbMd5er.md5(@resource[:username], @resource[:password])
78-
end
79-
cmd_json = <<-EOS.gsub(%r{^\s*}, '').gsub(%r{$\n}, '')
80-
{
81-
"createUser": "#{@resource[:username]}",
82-
"pwd": "#{password_hash}",
83-
"customData": {"createdBy": "Puppet Mongodb_user['#{@resource[:name]}']"},
84-
"roles": #{@resource[:roles].to_json},
85-
"digestPassword": false
86-
}
87-
EOS
88-
89-
mongo_eval("db.runCommand(#{cmd_json})", @resource[:database])
41+
password_hash = @resource[:password_hash]
42+
43+
if password_hash
44+
elsif @resource[:password]
45+
password_hash = Puppet::Util::MongodbMd5er.md5(@resource[:username], @resource[:password])
9046
end
47+
cmd_json = <<-EOS.gsub(%r{^\s*}, '').gsub(%r{$\n}, '')
48+
{
49+
"createUser": "#{@resource[:username]}",
50+
"pwd": "#{password_hash}",
51+
"customData": {"createdBy": "Puppet Mongodb_user['#{@resource[:name]}']"},
52+
"roles": #{@resource[:roles].to_json},
53+
"digestPassword": false
54+
}
55+
EOS
56+
57+
mongo_eval("db.runCommand(#{cmd_json})", @resource[:database])
9158
else
9259
Puppet.warning 'User creation is available only from master host'
9360

@@ -102,15 +69,7 @@ def create
10269
end
10370

10471
def destroy
105-
if db_ismaster
106-
if mongo_24?
107-
mongo_eval("db.removeUser('#{@resource[:username]}')")
108-
else
109-
mongo_eval("db.dropUser('#{@resource[:username]}')")
110-
end
111-
else
112-
mongo_eval("db.dropUser('#{@resource[:username]}')")
113-
end
72+
mongo_eval("db.dropUser('#{@resource[:username]}')")
11473
end
11574

11675
def exists?
@@ -133,7 +92,7 @@ def password_hash=(_value)
13392
end
13493

13594
def password=(value)
136-
if mongo_24? || mongo_26?
95+
if mongo_26?
13796
mongo_eval("db.changeUserPassword('#{@resource[:username]}','#{value}')", @resource[:database])
13897
else
13998
cmd_json = <<-EOS.gsub(%r{^\s*}, '').gsub(%r{$\n}, '')
@@ -150,18 +109,14 @@ def password=(value)
150109

151110
def roles=(roles)
152111
if db_ismaster
153-
if mongo_24?
154-
mongo_eval("db.system.users.update({user:'#{@resource[:username]}'}, { $set: {roles: #{@resource[:roles].to_json}}})")
155-
else
156-
grant = roles - @property_hash[:roles]
157-
unless grant.empty?
158-
mongo_eval("db.getSiblingDB('#{@resource[:database]}').grantRolesToUser('#{@resource[:username]}', #{grant. to_json})")
159-
end
160-
161-
revoke = @property_hash[:roles] - roles
162-
unless revoke.empty?
163-
mongo_eval("db.getSiblingDB('#{@resource[:database]}').revokeRolesFromUser('#{@resource[:username]}', #{revoke.to_json})")
164-
end
112+
grant = roles - @property_hash[:roles]
113+
unless grant.empty?
114+
mongo_eval("db.getSiblingDB('#{@resource[:database]}').grantRolesToUser('#{@resource[:username]}', #{grant. to_json})")
115+
end
116+
117+
revoke = @property_hash[:roles] - roles
118+
unless revoke.empty?
119+
mongo_eval("db.getSiblingDB('#{@resource[:database]}').revokeRolesFromUser('#{@resource[:username]}', #{revoke.to_json})")
165120
end
166121
else
167122
Puppet.warning 'User roles operations are available only from master host'

manifests/server/config.pp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
$ssl_invalid_hostnames = $mongodb::server::ssl_invalid_hostnames
6868
$ssl_mode = $mongodb::server::ssl_mode
6969
$storage_engine = $mongodb::server::storage_engine
70-
$version = $mongodb::server::version
7170

7271
File {
7372
owner => $user,
@@ -108,16 +107,10 @@
108107
# Template has available user-supplied data
109108
# - $config_data
110109
$cfg_content = template($config_template)
111-
} elsif $version and (versioncmp($version, '2.6.0') >= 0) {
112-
# Template has available user-supplied data
113-
# - $config_data
114-
$cfg_content = template('mongodb/mongodb.conf.2.6.erb')
115110
} else {
116-
# Fall back to oldest most basic config
117-
#
118111
# Template has available user-supplied data
119112
# - $config_data
120-
$cfg_content = template('mongodb/mongodb.conf.erb')
113+
$cfg_content = template('mongodb/mongodb.conf.2.6.erb')
121114
}
122115

123116
file { $config:

0 commit comments

Comments
 (0)