@@ -125,6 +125,43 @@ MySQLConnectionParameters MySQLUtils::ParseConnectionParameters(const string &ds
125
125
} else {
126
126
result.client_flag &= ~CLIENT_COMPRESS;
127
127
}
128
+ } else if (key == " ssl_mode" ) {
129
+ set_options.insert (" ssl_mode" );
130
+ auto val = StringUtil::Lower (value);
131
+ if (val == " disabled" ) {
132
+ result.ssl_mode = SSL_MODE_DISABLED;
133
+ } else if (val == " required" ) {
134
+ result.ssl_mode = SSL_MODE_REQUIRED;
135
+ } else if (val == " verify_ca" ) {
136
+ result.ssl_mode = SSL_MODE_VERIFY_CA;
137
+ } else if (val == " verify_identity" ) {
138
+ result.ssl_mode = SSL_MODE_VERIFY_IDENTITY;
139
+ } else if (val == " preferred" ) {
140
+ result.ssl_mode = SSL_MODE_PREFERRED;
141
+ } else {
142
+ throw InvalidInputException (" Invalid dsn - ssl mode must be either disabled, required, verify_ca, verify_identity or preferred - got %s" , value);
143
+ }
144
+ } else if (key == " ssl_ca" ) {
145
+ set_options.insert (" ssl_ca" );
146
+ result.ssl_ca = value;
147
+ } else if (key == " ssl_capath" ) {
148
+ set_options.insert (" ssl_capath" );
149
+ result.ssl_ca_path = value;
150
+ } else if (key == " ssl_cert" ) {
151
+ set_options.insert (" ssl_cert" );
152
+ result.ssl_cert = value;
153
+ } else if (key == " ssl_cipher" ) {
154
+ set_options.insert (" ssl_cipher" );
155
+ result.ssl_cipher = value;
156
+ } else if (key == " ssl_crl" ) {
157
+ set_options.insert (" ssl_crl" );
158
+ result.ssl_crl = value;
159
+ } else if (key == " ssl_crlpath" ) {
160
+ set_options.insert (" ssl_crlpath" );
161
+ result.ssl_crl_path = value;
162
+ } else if (key == " ssl_key" ) {
163
+ set_options.insert (" ssl_key" );
164
+ result.ssl_key = value;
128
165
} else {
129
166
throw InvalidInputException (" Unrecognized configuration parameter \" %s\" "
130
167
" - expected options are host, "
@@ -167,13 +204,36 @@ MySQLConnectionParameters MySQLUtils::ParseConnectionParameters(const string &ds
167
204
return result;
168
205
}
169
206
207
+ void SetMySQLOption (MYSQL *mysql, enum mysql_option option, const string &value) {
208
+ if (value.empty ()) {
209
+ return ;
210
+ }
211
+ int rc = mysql_options (mysql, option, value.c_str ());
212
+ if (rc != 0 ) {
213
+ throw InternalException (" Failed to set MySQL option" );
214
+ }
215
+ }
216
+
170
217
MYSQL *MySQLUtils::Connect (const string &dsn) {
171
218
MYSQL *mysql = mysql_init (NULL );
172
219
if (!mysql) {
173
220
throw IOException (" Failure in mysql_init" );
174
221
}
175
222
MYSQL *result;
176
223
auto config = ParseConnectionParameters (dsn);
224
+ // set SSL options (if any)
225
+ if (config.ssl_mode != SSL_MODE_PREFERRED) {
226
+ mysql_options (mysql, MYSQL_OPT_SSL_MODE, &config.ssl_mode );
227
+ }
228
+ SetMySQLOption (mysql, MYSQL_OPT_SSL_CA, config.ssl_ca );
229
+ SetMySQLOption (mysql, MYSQL_OPT_SSL_CAPATH, config.ssl_ca_path );
230
+ SetMySQLOption (mysql, MYSQL_OPT_SSL_CERT, config.ssl_cert );
231
+ SetMySQLOption (mysql, MYSQL_OPT_SSL_CIPHER, config.ssl_cipher );
232
+ SetMySQLOption (mysql, MYSQL_OPT_SSL_CRL, config.ssl_crl );
233
+ SetMySQLOption (mysql, MYSQL_OPT_SSL_CRLPATH, config.ssl_crl_path );
234
+ SetMySQLOption (mysql, MYSQL_OPT_SSL_KEY, config.ssl_key );
235
+
236
+ // get connection options
177
237
const char *host = config.host .empty () ? nullptr : config.host .c_str ();
178
238
const char *user = config.user .empty () ? nullptr : config.user .c_str ();
179
239
const char *passwd = config.passwd .empty () ? nullptr : config.passwd .c_str ();
0 commit comments