Skip to content

Commit 874cb79

Browse files
committed
Merge pull request voxpupuli#271 from tanadeau/2.6-ssl-support
MongoDB 2.6 SSL support
2 parents b24214d + 0dfde6e commit 874cb79

File tree

5 files changed

+84
-56
lines changed

5 files changed

+84
-56
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ For Red Hat family systems, the client can be installed in a similar fashion:
6767
class {'::mongodb::client':}
6868
```
6969

70-
Note that for Debian/Ubuntu family systems the client is installed with the
70+
Note that for Debian/Ubuntu family systems the client is installed with the
7171
server. Using the client class will by default install the server.
7272

7373
If one plans to configure sharding for a Mongo deployment, the module offer
@@ -427,11 +427,11 @@ class mongodb::server {
427427
Set to true to enable a simple REST interface. Default: false
428428

429429
#####`quiet`
430-
Runs the mongod or mongos instance in a quiet mode that attempts to limit the
430+
Runs the mongod or mongos instance in a quiet mode that attempts to limit the
431431
amount of output. This option suppresses : "output from database commands, including drop, dropIndexes, diagLogging, validate, and clean", "replication activity", "connection accepted events" and "connection closed events".
432432
Default: false
433433

434-
> For production systems this option is **not** recommended as it may make tracking
434+
> For production systems this option is **not** recommended as it may make tracking
435435
problems during particular connections much more difficult.
436436

437437
#####`slowms`
@@ -476,8 +476,9 @@ this slave instance will replicate. Default: <>
476476

477477
#####`ssl`
478478
Set to true to enable ssl. Default: <>
479-
*Important*: You need to have ssl_key and ssl_ca set as well and files
480-
need to pre-exist on node.
479+
*Important*: You need to have ssl_key set as well, and the file needs to
480+
pre-exist on node. If you wish to use certificate validation, ssl_ca must also
481+
be set.
481482

482483
#####`ssl_key`
483484
Default: <>

lib/puppet/provider/mongodb.rb

Lines changed: 66 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -28,55 +28,79 @@ def self.get_mongod_conf_file
2828
file
2929
end
3030

31-
def self.ipv6_is_enabled
31+
def self.get_mongo_conf
3232
file = get_mongod_conf_file
33+
# The mongo conf is probably a key-value store, even though 2.6 is
34+
# supposed to use YAML, because the config template is applied
35+
# based on $::mongodb::globals::version which is the user will not
36+
# necessarily set. This attempts to get the port from both types of
37+
# config files.
3338
config = YAML.load_file(file)
34-
if config.kind_of?(Hash)
35-
ipv6 = config['net.ipv6']
36-
else # It has to be a key-value store
39+
config_hash = Hash.new
40+
if config.kind_of?(Hash) # Using a valid YAML file for mongo 2.6
41+
config_hash['bindip'] = config['net.bindIp']
42+
config_hash['port'] = config['net.port']
43+
config_hash['ipv6'] = config['net.ipv6']
44+
config_hash['ssl'] = config['net.ssl.mode']
45+
config_hash['sslcert'] = config['net.ssl.PEMKeyFile']
46+
config_hash['sslca'] = config['net.ssl.CAFile']
47+
config_hash['auth'] = config['security.authorization']
48+
config_hash['shardsvr'] = config['sharding.clusterRole']
49+
config_hash['confsvr'] = config['sharding.clusterRole']
50+
else # It has to be a key-value config file
3751
config = {}
3852
File.readlines(file).collect do |line|
3953
k,v = line.split('=')
4054
config[k.rstrip] = v.lstrip.chomp if k and v
4155
end
42-
ipv6 = config['ipv6']
56+
config_hash['bindip'] = config['bind_ip']
57+
config_hash['port'] = config['port']
58+
config_hash['ipv6'] = config['ipv6']
59+
config_hash['ssl'] = config['sslMode']
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']
4365
end
44-
ipv6
66+
67+
config_hash
4568
end
4669

47-
def self.mongo_cmd(db, host, cmd)
48-
if ipv6_is_enabled
49-
out = mongo([db, '--quiet', '--ipv6', '--host', host, '--eval', cmd])
50-
else
51-
out = mongo([db, '--quiet', '--host', host, '--eval', cmd])
52-
end
70+
def self.ipv6_is_enabled(config=nil)
71+
config ||= get_mongo_conf
72+
config['ipv6']
5373
end
5474

55-
def self.get_conn_string
56-
file = get_mongod_conf_file
57-
# The mongo conf is probably a key-value store, even though 2.6 is
58-
# supposed to use YAML, because the config template is applied
59-
# based on $::mongodb::globals::version which is the user will not
60-
# necessarily set. This attempts to get the port from both types of
61-
# config files.
62-
config = YAML.load_file(file)
63-
if config.kind_of?(Hash) # Using a valid YAML file for mongo 2.6
64-
bindip = config['net.bindIp']
65-
port = config['net.port']
66-
shardsvr = config['sharding.clusterRole']
67-
confsvr = config['sharding.clusterRole']
68-
else # It has to be a key-value config file
69-
config = {}
70-
File.readlines(file).collect do |line|
71-
k,v = line.split('=')
72-
config[k.rstrip] = v.lstrip.chomp if k and v
75+
def self.ssl_is_enabled(config=nil)
76+
config ||= get_mongo_conf
77+
ssl_mode = config.fetch('ssl')
78+
ssl_mode.nil? ? false : ssl_mode != 'disabled'
79+
end
80+
81+
def self.mongo_cmd(db, host, cmd)
82+
config = get_mongo_conf
83+
84+
args = [db, '--quiet', '--host', host]
85+
args.push('--ipv6') if ipv6_is_enabled(config)
86+
87+
if ssl_is_enabled(config)
88+
args.push('--ssl')
89+
args += ['--sslPEMKeyFile', config['sslcert']]
90+
91+
ssl_ca = config['sslca']
92+
unless ssl_ca.nil?
93+
args += ['--sslCAFile', ssl_ca]
7394
end
74-
bindip = config['bind_ip']
75-
port = config['port']
76-
shardsvr = config['shardsvr']
77-
confsvr = config['confsvr']
7895
end
7996

97+
args += ['--eval', cmd]
98+
mongo(args)
99+
end
100+
101+
def self.get_conn_string
102+
config = get_mongo_conf
103+
bindip = config.fetch('bindip')
80104
if bindip
81105
first_ip_in_list = bindip.split(',').first
82106
case first_ip_in_list
@@ -89,6 +113,9 @@ def self.get_conn_string
89113
end
90114
end
91115

116+
port = config.fetch('port')
117+
shardsvr = config.fetch('shardsvr')
118+
confsvr = config.fetch('confsvr')
92119
if port
93120
port_real = port
94121
elsif !port and (confsvr.eql? 'configsvr' or confsvr.eql? 'true')
@@ -105,7 +132,7 @@ def self.get_conn_string
105132
def self.db_ismaster
106133
cmd_ismaster = 'printjson(db.isMaster())'
107134
if mongorc_file
108-
cmd_ismaster = mongorc_file + cmd_ismaster
135+
cmd_ismaster = mongorc_file + cmd_ismaster
109136
end
110137
db = 'admin'
111138
out = mongo_cmd(db, get_conn_string, cmd_ismaster)
@@ -121,29 +148,17 @@ def db_ismaster
121148
self.class.db_ismaster
122149
end
123150

124-
def self.auth_enabled
125-
auth_enabled = false
126-
file = get_mongod_conf_file
127-
config = YAML.load_file(file)
128-
if config.kind_of?(Hash)
129-
auth_enabled = config['security.authorization']
130-
else # It has to be a key-value store
131-
config = {}
132-
File.readlines(file).collect do |line|
133-
k,v = line.split('=')
134-
config[k.rstrip] = v.lstrip.chomp if k and v
135-
end
136-
auth_enabled = config['auth']
137-
end
138-
return auth_enabled
151+
def self.auth_enabled(config=nil)
152+
config ||= get_mongo_conf
153+
config['auth']
139154
end
140155

141156
# Mongo Command Wrapper
142157
def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
143158
retry_count = retries
144159
retry_sleep = 3
145160
if mongorc_file
146-
cmd = mongorc_file + cmd
161+
cmd = mongorc_file + cmd
147162
end
148163

149164
out = nil

manifests/server/config.pp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@
132132
# - $shardsvr
133133
# - $slowms
134134
# - $smallfiles
135+
# - $ssl
136+
# - $ssl_ca
137+
# - $ssl_key
135138
# - $syslog
136139
# - $verbose
137140
# - $verbositylevel

templates/mongodb.conf.2.6.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ net.maxIncomingConnections: <%= @maxconns %>
102102
<% if ! @nohttpinterface.nil? -%>
103103
net.http.enabled: <%= ! @nohttpinterface %>
104104
<% end -%>
105+
<% if @ssl -%>
106+
net.ssl.mode: requireSSL
107+
net.ssl.PEMKeyFile: <%= @ssl_key %>
108+
<% if @ssl_ca -%>
109+
net.ssl.CAFile: <%= @ssl_ca %>
110+
<% end -%>
111+
<% end -%>
105112

106113
#Replication
107114
<% if @replset -%>

templates/mongodb.conf.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,7 @@ quiet = <%= @quiet %>
186186
<% if @ssl -%>
187187
sslOnNormalPorts = true
188188
sslPEMKeyFile = <%= @ssl_key %>
189+
<% if @ssl_ca -%>
189190
sslCAFile = <%= @ssl_ca %>
190191
<% end -%>
192+
<% end -%>

0 commit comments

Comments
 (0)