Skip to content

Commit e70171e

Browse files
committed
Add connection option :ssl for all-defaults SSL connection
1 parent a0a9e4e commit e70171e

3 files changed

Lines changed: 32 additions & 13 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,13 @@ your MySQL client library and server have been compiled with SSL support.
203203
MySQL client library defaults will be used for any parameters that are left out
204204
or set to nil. Relative paths are allowed, and may be required by managed
205205
hosting providers such as Heroku. Set `:sslverify => true` to require that the
206-
server presents a valid certificate.
206+
server presents a valid certificate. Set `:ssl => true` to enable SSL using all
207+
default values.
207208

208209
``` ruby
209210
Mysql2::Client.new(
210211
# ...options as above...,
212+
:ssl => true,
211213
:sslkey => '/path/to/client-key.pem',
212214
:sslcert => '/path/to/client-cert.pem',
213215
:sslca => '/path/to/ca-cert.pem',

lib/mysql2/client.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ def initialize(opts = {})
4141
# force the encoding to utf8
4242
self.charset_name = opts[:encoding] || 'utf8'
4343

44+
# Enable SSL if any of the options are configured, or:
45+
# :ssl => true requests an all-defaults SSL connection
46+
# :ssl => false disables SSL even if configured by the other :ssl* options
4447
ssl_options = opts.values_at(:sslkey, :sslcert, :sslca, :sslcapath, :sslcipher)
45-
ssl_set(*ssl_options) if ssl_options.any?
48+
ssl_set(*ssl_options) if ssl_options.any? || (opts[:ssl] && opts[:ssl] != false)
4649

4750
# SSL verify is a connection flag rather than a mysql_ssl_set option
4851
flags = 0

spec/mysql2/client_spec.rb

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,28 @@ def connect *args
115115
expect(Mysql2::Client).to respond_to(:default_query_options)
116116
end
117117

118+
it "should be able to connect via SSL defaults" do
119+
ssl = @client.query "SHOW VARIABLES LIKE 'have_ssl'"
120+
ssl_uncompiled = ssl.any? {|x| x['Value'] == 'OFF'}
121+
pending("DON'T WORRY, THIS TEST PASSES - but SSL is not compiled into your MySQL daemon.") if ssl_uncompiled
122+
ssl_disabled = ssl.any? {|x| x['Value'] == 'DISABLED'}
123+
pending("DON'T WORRY, THIS TEST PASSES - but SSL is not enabled in your MySQL daemon.") if ssl_disabled
124+
125+
ssl_client = nil
126+
expect {
127+
ssl_client = Mysql2::Client.new(
128+
DatabaseCredentials['root'].merge(
129+
:ssl => true
130+
)
131+
)
132+
}.not_to raise_error
133+
134+
results = Hash[ ssl_client.query('SHOW STATUS WHERE Variable_name LIKE "Ssl_%"').map{ |x| x.values_at('Variable_name', 'Value') } ]
135+
expect(results['Ssl_cipher']).not_to be_empty
136+
expect(results['Ssl_version']).not_to be_empty
137+
ssl_client.close
138+
end
139+
118140
it "should be able to connect via SSL options" do
119141
ssl = @client.query "SHOW VARIABLES LIKE 'have_ssl'"
120142
ssl_uncompiled = ssl.any? {|x| x['Value'] == 'OFF'}
@@ -137,17 +159,9 @@ def connect *args
137159
)
138160
}.not_to raise_error
139161

140-
results = ssl_client.query("SHOW STATUS WHERE Variable_name = \"Ssl_version\" OR Variable_name = \"Ssl_cipher\"").to_a
141-
expect(results[0]['Variable_name']).to eql('Ssl_cipher')
142-
expect(results[0]['Value']).not_to be_nil
143-
expect(results[0]['Value']).to be_kind_of(String)
144-
expect(results[0]['Value']).not_to be_empty
145-
146-
expect(results[1]['Variable_name']).to eql('Ssl_version')
147-
expect(results[1]['Value']).not_to be_nil
148-
expect(results[1]['Value']).to be_kind_of(String)
149-
expect(results[1]['Value']).not_to be_empty
150-
162+
results = Hash[ ssl_client.query('SHOW STATUS WHERE Variable_name LIKE "Ssl_%"').map{ |x| x.values_at('Variable_name', 'Value') } ]
163+
expect(results['Ssl_cipher']).not_to be_empty
164+
expect(results['Ssl_version']).not_to be_empty
151165
ssl_client.close
152166
end
153167

0 commit comments

Comments
 (0)