Skip to content

Commit 4e18d79

Browse files
authored
Support get_server_public_key option (#1377)
* caching_sha2_password requests secure connection if cache is not ready on server-side. get_server_public_key option enables clients to create secure connection automatically even if connection is not SSL. * return error if get_server_public_key option is not supported
1 parent 16c40a5 commit 4e18d79

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ Mysql2::Client.new(
281281
:reconnect = true/false,
282282
:local_infile = true/false,
283283
:secure_auth = true/false,
284+
:get_server_public_key = true/false,
284285
:default_file = '/path/to/my.cfg',
285286
:default_group = 'my.cfg section',
286287
:default_auth = 'authentication_windows_client'

ext/mysql2/client.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,13 @@ static VALUE _mysql_client_options(VALUE self, int opt, VALUE value) {
996996
retval = charval;
997997
break;
998998

999+
#ifdef HAVE_CONST_MYSQL_OPT_GET_SERVER_PUBLIC_KEY
1000+
case MYSQL_OPT_GET_SERVER_PUBLIC_KEY:
1001+
boolval = (value == Qfalse ? 0 : 1);
1002+
retval = &boolval;
1003+
break;
1004+
#endif
1005+
9991006
#ifdef HAVE_MYSQL_DEFAULT_AUTH
10001007
case MYSQL_DEFAULT_AUTH:
10011008
charval = (const char *)StringValueCStr(value);
@@ -1485,6 +1492,14 @@ static VALUE set_init_command(VALUE self, VALUE value) {
14851492
return _mysql_client_options(self, MYSQL_INIT_COMMAND, value);
14861493
}
14871494

1495+
static VALUE set_get_server_public_key(VALUE self, VALUE value) {
1496+
#ifdef HAVE_CONST_MYSQL_OPT_GET_SERVER_PUBLIC_KEY
1497+
return _mysql_client_options(self, MYSQL_OPT_GET_SERVER_PUBLIC_KEY, value);
1498+
#else
1499+
rb_raise(cMysql2Error, "get-server-public-key is not available, you may need a newer MySQL client library");
1500+
#endif
1501+
}
1502+
14881503
static VALUE set_default_auth(VALUE self, VALUE value) {
14891504
#ifdef HAVE_MYSQL_DEFAULT_AUTH
14901505
return _mysql_client_options(self, MYSQL_DEFAULT_AUTH, value);
@@ -1596,6 +1611,7 @@ void init_mysql2_client() {
15961611
rb_define_private_method(cMysql2Client, "default_file=", set_read_default_file, 1);
15971612
rb_define_private_method(cMysql2Client, "default_group=", set_read_default_group, 1);
15981613
rb_define_private_method(cMysql2Client, "init_command=", set_init_command, 1);
1614+
rb_define_private_method(cMysql2Client, "get_server_public_key=", set_get_server_public_key, 1);
15991615
rb_define_private_method(cMysql2Client, "default_auth=", set_default_auth, 1);
16001616
rb_define_private_method(cMysql2Client, "ssl_set", set_ssl_options, 5);
16011617
rb_define_private_method(cMysql2Client, "ssl_mode=", rb_set_ssl_mode_option, 1);

ext/mysql2/extconf.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def add_ssl_defines(header)
159159
have_const('SERVER_QUERY_WAS_SLOW', mysql_h)
160160
have_const('MYSQL_OPTION_MULTI_STATEMENTS_ON', mysql_h)
161161
have_const('MYSQL_OPTION_MULTI_STATEMENTS_OFF', mysql_h)
162+
have_const('MYSQL_OPT_GET_SERVER_PUBLIC_KEY', mysql_h)
162163

163164
# my_bool is replaced by C99 bool in MySQL 8.0, but we want
164165
# to retain compatibility with the typedef in earlier MySQLs.

lib/mysql2/client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ def initialize(opts = {})
3232
opts[:connect_timeout] = 120 unless opts.key?(:connect_timeout)
3333

3434
# TODO: stricter validation rather than silent massaging
35-
%i[reconnect connect_timeout local_infile read_timeout write_timeout default_file default_group secure_auth init_command automatic_close enable_cleartext_plugin default_auth].each do |key|
35+
%i[reconnect connect_timeout local_infile read_timeout write_timeout default_file default_group secure_auth init_command automatic_close enable_cleartext_plugin default_auth get_server_public_key].each do |key|
3636
next unless opts.key?(key)
3737

3838
case key
39-
when :reconnect, :local_infile, :secure_auth, :automatic_close, :enable_cleartext_plugin
39+
when :reconnect, :local_infile, :secure_auth, :automatic_close, :enable_cleartext_plugin, :get_server_public_key
4040
send(:"#{key}=", !!opts[key]) # rubocop:disable Style/DoubleNegation
4141
when :connect_timeout, :read_timeout, :write_timeout
4242
send(:"#{key}=", Integer(opts[key])) unless opts[key].nil?

0 commit comments

Comments
 (0)