3

I'm trying to connect remotely to a mysql db over SSL, with the server's certificate verified to match the DNS domain used to connect to the server.

Using the command-line mysql tool, I can make such a connection using mysql --ssl-ca=/path/to/cacert.pem --ssl-verify-server-cert.

Using rails mysql2, I set sslca:¹, which causes a not-fully-verified SSL connection like mysql --ssl-ca= does. How do I do the equivalent of --ssl-verify-server-cert so that the connection fails if the server cert's domain is wrong?

I've tried adding the following which had no effect on this issue: flags: SSL_VERIFY_SERVER_CERT, flags: CLIENT_SSL_VERIFY_SERVER_CERT, flags: 1073741824, and secure_auth: true.

¹ either sslca: /path/to/cacert.pem in config/database.yml, or ?sslca=/path/to/cacert.pem in a mysql2:// URL

2 Answers 2

5

With mysql2>=0.4.0, you can set sslverify: true and sslca: path/to/cert_chain.pem in your adapter configs to make the client verify the server identity.

2
4

This is not one of the default connection flags in the Mysql2 gem, but the constant is available and can be bitwise OR-ed into the connection flags field before making a connection.

You can set the global default like this:

Mysql2::Client::default_query_options[:connect_flags] |=
    Mysql2::Client::SSL_VERIFY_SERVER_CERT

Or set the flags per connection:

client = Mysql2::Client.new(
    :connect_flags => (Mysql2::Client::default_query_options[:connect_flags]
                     | Mysql2::Client::SSL_VERIFY_SERVER_CERT)
    )

Hope that helps!

1
  • Thanks! Using it with Rails (4.1), we set the global default in a new file config/initializers/ssl_verify_server_cert.rb and it is verifying correctly. (Would a feature request or pull request be welcome to make it possible to specify this flag everywhere sslca can be specified?)
    – idupree
    Jun 16, 2014 at 19:23

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.