Skip to content

Commit 630e174

Browse files
rsomla1silvakid
authored andcommitted
MYCPP-284: TLS Options implementation on DevAPI
1 parent 03f4a0f commit 630e174

File tree

6 files changed

+128
-15
lines changed

6 files changed

+128
-15
lines changed

cdk/core/tests/session-t.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,11 @@ TEST_F(Session_core, tls_options)
10321032
}
10331033
}
10341034

1035+
if (ssl_ca.find('\\') == string::npos && ssl_ca.find('/') == string::npos)
1036+
{ //not full path
1037+
ssl_ca = datadir + ssl_ca;
1038+
}
1039+
10351040
cout << "Setting CA to: " << ssl_ca << endl;
10361041

10371042
tls_options.set_ca(ssl_ca);

cdk/include/mysql/cdk/data_source.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,16 @@ class TCPIP::Options : public ds::Options
131131
public:
132132

133133
Options()
134+
#ifdef WITH_SSL
134135
: m_tls_options(false)
136+
#endif
135137
{}
136138

137139
Options(const string &usr, const std::string *pwd =NULL)
138140
: ds::Options(usr, pwd)
139-
, m_tls_options(false)
141+
#ifdef WITH_SSL
142+
,m_tls_options(false)
143+
#endif
140144
{}
141145

142146
#ifdef WITH_SSL

cdk/include/mysql/cdk/foundation/connection_yassl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class TLS::Options
7575
: m_use_tls(use_tls)
7676
{}
7777

78+
void set_use_tls(bool use_tls) { m_use_tls = use_tls; }
7879
bool use_tls() const { return m_use_tls; }
7980

8081
void set_key(const string &key) { m_key = key; }

devapi/session.cc

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,18 @@ struct URI_parser
129129
, private endpoint::TCPIP
130130
, public parser::URI_processor
131131
{
132+
133+
#ifdef WITH_SSL
134+
// tls off by default on URI connection
135+
cdk::connection::TLS::Options m_tls_opt = false;
136+
#endif
137+
132138
URI_parser(const std::string &uri)
133139
{
140+
parser::parse_conn_str(uri, *this);
134141
#ifdef WITH_SSL
135-
// TLS OFF by default on URI
136-
set_tls(false);
142+
set_tls(m_tls_opt);
137143
#endif
138-
parser::parse_conn_str(uri, *this);
139144
}
140145

141146

@@ -173,14 +178,36 @@ struct URI_parser
173178
void key_val(const std::string &key) override
174179
{
175180
if (key == "ssl-enable")
181+
{
182+
#ifdef WITH_SSL
183+
m_tls_opt.set_use_tls(true);
184+
#else
185+
throw_error(
186+
"Can not create TLS session - this connector is built"
187+
" without TLS support."
188+
);
189+
#endif
190+
}
191+
}
192+
193+
void key_val(const std::string &key, const std::string &val) override
194+
{
195+
if (key == "ssl-ca")
196+
{
176197
#ifdef WITH_SSL
177-
set_tls(true);
198+
m_tls_opt.set_ca(val);
178199
#else
179200
throw_error(
180-
"Can not create TLS session - this connector is built"
181-
" without TLS support."
182-
);
201+
"Can not create TLS session - this connector is built"
202+
" without TLS support."
203+
);
183204
#endif
205+
} else
206+
{
207+
std::stringstream err;
208+
err << "Unexpected key " << key << "=" << val << " on URI";
209+
throw_error(err.str().c_str());
210+
}
184211
}
185212

186213
};
@@ -197,8 +224,8 @@ internal::XSession_base::XSession_base(SessionSettings settings)
197224
);
198225

199226
m_impl = new Impl(
200-
static_cast<endpoint::TCPIP&>(parser.get_endpoint()),
201-
static_cast<XSession_base::Options&>(parser));
227+
static_cast<endpoint::TCPIP&>(parser.get_endpoint()),
228+
static_cast<XSession_base::Options&>(parser));
202229
}
203230
else
204231
{
@@ -246,16 +273,25 @@ internal::XSession_base::XSession_base(SessionSettings settings)
246273
);
247274

248275
if (settings.has_option(SessionSettings::SSL_ENABLE))
276+
{
249277
#ifdef WITH_SSL
250-
opt.set_tls(settings[SessionSettings::SSL_ENABLE].get<bool>());
278+
cdk::connection::TLS::Options opt_ssl(settings[SessionSettings::SSL_ENABLE]);
279+
280+
281+
if (settings.has_option(SessionSettings::SSL_CA))
282+
opt_ssl.set_ca(settings[SessionSettings::SSL_ENABLE].get<string>());
283+
284+
opt.set_tls(opt_ssl);
251285
#else
252286
throw_error(
253-
"Can not create TLS session - this connector is built"
254-
" without TLS support."
255-
);
287+
"Can not create TLS session - this connector is built"
288+
" without TLS support."
289+
);
256290
#endif
291+
}
257292

258293
m_impl = new Impl(ep, opt);
294+
259295
}
260296
}
261297
CATCH_AND_WRAP

devapi/tests/session-t.cc

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,72 @@ TEST_F(Sess, ssl_session)
431431
EXPECT_FALSE(cipher.empty());
432432
}
433433

434+
435+
//using wrong ssl-ca and ssl-ca-path as SessionSettings
436+
{
437+
EXPECT_THROW(
438+
mysqlx::XSession sess(SessionSettings::PORT, get_port(),
439+
SessionSettings::USER,get_user(),
440+
SessionSettings::PWD, get_password() ? get_password() : NULL ,
441+
SessionSettings::SSL_ENABLE, true,
442+
SessionSettings::SSL_CA, "unknown")
443+
, mysqlx::Error);
444+
445+
446+
}
447+
448+
//using wrong ssl-ca and ssl-ca-path on URI
449+
{
450+
std::stringstream bad_uri;
451+
bad_uri << uri.str() << "&ssl-ca=" << "unknown.file" << "&ssl-ca-path=" << "unknown.path";
452+
453+
EXPECT_THROW(mysqlx::XSession sess(bad_uri.str()), mysqlx::Error);
454+
}
455+
456+
string ssl_ca;
457+
string datadir;
458+
459+
{
460+
mysqlx::XSession sess(uri.str());
461+
462+
SqlResult res = sess.bindToDefaultShard()
463+
.sql("show global variables like 'ssl_ca'")
464+
.execute();
465+
466+
ssl_ca = res.fetchOne().get(1);
467+
468+
res = sess.bindToDefaultShard()
469+
.sql("show global variables like 'datadir'")
470+
.execute();
471+
472+
datadir = res.fetchOne().get(1);
473+
474+
}
475+
476+
std::cout << "ssl-ca:" << ssl_ca
477+
<< " datadir:" << datadir
478+
<< std::endl;
479+
480+
if (ssl_ca.find('\\') == string::npos && ssl_ca.find('/') == string::npos)
481+
{ //not full path
482+
ssl_ca = datadir + ssl_ca;
483+
}
484+
485+
uri << "&ssl-ca=" << ssl_ca;
486+
487+
{
488+
mysqlx::XSession sess(uri.str());
489+
490+
SqlResult res = sess.bindToDefaultShard().sql("SHOW STATUS LIKE 'mysqlx_ssl_cipher'").execute();
491+
492+
auto row = res.fetchOne();
493+
cout << row[0] << ":" << row[1] << endl;
494+
495+
string cipher = row[1];
496+
497+
EXPECT_FALSE(cipher.empty());
498+
}
499+
434500
}
435501

436502
TEST_F(Sess, ipv6)

include/mysql_devapi.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,8 @@ class PUBLIC_API SessionSettings
516516
USER,
517517
PWD,
518518
DB,
519-
SSL_ENABLE
519+
SSL_ENABLE,
520+
SSL_CA
520521
};
521522

522523

0 commit comments

Comments
 (0)