Skip to content

Commit ddee57e

Browse files
committed
WL14711: Support OCI IAM authentication
- Make sure pluginDir is set before other options. - Improve error messages when setting plugin options.
1 parent 4f473e8 commit ddee57e

File tree

3 files changed

+73
-34
lines changed

3 files changed

+73
-34
lines changed

driver/mysql_connection.cpp

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,13 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
506506
bool secure_auth= true;
507507
#endif
508508

509+
sql::ConnectOptionsMap::const_iterator it;
510+
509511
/* Port from options must be set as default for all hosts where port
510512
is not specified */
511513
{
512-
sql::ConnectOptionsMap::const_iterator it = properties.find("port");
514+
it = properties.find("port");
515+
513516
if (it != properties.end()) {
514517
try {
515518
p_i = (it->second).get< int >();
@@ -526,31 +529,56 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
526529

527530
/* Values set in properties individually should have priority over those
528531
we restore from Uri */
529-
sql::ConnectOptionsMap::const_iterator it = properties.find("hostName");
532+
{
533+
it = properties.find("hostName");
530534

531-
if (it != properties.end()) {
532-
try {
533-
p_s = (it->second).get< sql::SQLString >();
534-
} catch (sql::InvalidArgumentException&) {
535-
throw sql::InvalidArgumentException("Wrong type passed for userName expected sql::SQLString");
536-
}
537-
if (p_s) {
538-
/*
539-
Parsing uri prior to processing all parameters, so indivudually
540-
specified parameters precede over those in the uri
541-
*/
542-
if(!parseUri(*p_s, uri))
543-
throw sql::InvalidArgumentException("Invalid hostname URI");
535+
if (it != properties.end()) {
536+
try {
537+
p_s = (it->second).get< sql::SQLString >();
538+
} catch (sql::InvalidArgumentException&) {
539+
throw sql::InvalidArgumentException("Wrong type passed for userName expected sql::SQLString");
540+
}
541+
if (p_s) {
542+
/*
543+
Parsing uri prior to processing all parameters, so indivudually
544+
specified parameters precede over those in the uri
545+
*/
546+
if(!parseUri(*p_s, uri))
547+
throw sql::InvalidArgumentException("Invalid hostname URI");
544548

545-
} else {
546-
throw sql::InvalidArgumentException("No string value passed for hostName");
549+
} else {
550+
throw sql::InvalidArgumentException("No string value passed for hostName");
551+
}
547552
}
548553
}
549554

555+
/*
556+
Note: We set pluginDir option early because other options that are plugin
557+
specific might require loading plugins before they can be set.
558+
*/
550559

551-
#define PROCESS_CONN_OPTION(option_type, options_map) process_connection_option< option_type >(it, options_map, sizeof(options_map)/sizeof(String2IntMap), proxy)
560+
{
561+
it = properties.find(OPT_PLUGIN_DIR);
552562

553-
for (it = properties.begin(); it != properties.end(); ++it) {
563+
if (it != properties.end()) {
564+
try {
565+
p_s = (it->second).get< sql::SQLString >();
566+
} catch (sql::InvalidArgumentException&) {
567+
throw sql::InvalidArgumentException("Wrong type passed for pluginDir expected sql::SQLString");
568+
}
569+
if (p_s) {
570+
proxy->options(sql::mysql::MYSQL_PLUGIN_DIR, *p_s);
571+
}
572+
else {
573+
throw sql::InvalidArgumentException("No string value passed for pluginDir");
574+
}
575+
}
576+
}
577+
578+
#define PROCESS_CONN_OPTION(option_type, options_map) \
579+
process_connection_option< option_type >(it, options_map, sizeof(options_map)/sizeof(String2IntMap), proxy)
580+
581+
for (it = properties.begin(); it != properties.end(); ++it) {
554582
if (!it->first.compare(OPT_USERNAME)) {
555583
try {
556584
p_s = (it->second).get< sql::SQLString >();
@@ -751,7 +779,7 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
751779
intern->defaultStatementResultType = static_cast< sql::ResultSet::enum_type >(*p_i);
752780
/* The connector is not ready for unbuffered as we need to refetch */
753781
} else if (!it->first.compare("defaultPreparedStatementResultType")) {
754-
#if WE_SUPPORT_USE_RESULT_WITH_PS
782+
#if WE_SUPPORT_USE_RESULT_WITH_PS
755783
try {
756784
p_i = (it->second).get< int >();
757785
} catch (sql::InvalidArgumentException&) {
@@ -774,10 +802,10 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
774802
throw sql::InvalidArgumentException(msg.str());
775803
} while (0);
776804
intern->defaultPreparedStatementResultType = static_cast< sql::ResultSet::enum_type >(*p_i);
777-
#else
805+
#else
778806
throw SQLException("defaultPreparedStatementResultType parameter still not implemented");
779807

780-
#endif
808+
#endif
781809
} else if (!it->first.compare(OPT_METADATA_INFO_SCHEMA)) {
782810
try {
783811
p_b = (it->second).get<bool>();
@@ -867,13 +895,13 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
867895
} catch (sql::InvalidArgumentException&) {
868896
throw sql::InvalidArgumentException("Wrong type passed for useLegacyAuth expected sql::SQLString");
869897
}
870-
#if MYCPPCONN_STATIC_MYSQL_VERSION_ID < 80000
898+
#if MYCPPCONN_STATIC_MYSQL_VERSION_ID < 80000
871899
if (p_b) {
872900
secure_auth= !*p_b;
873901
} else {
874902
throw sql::InvalidArgumentException("No bool value passed for useLegacyAuth");
875903
}
876-
#endif
904+
#endif
877905
} else if (!it->first.compare(OPT_CONNECT_ATTR_ADD)) {
878906
const std::map< sql::SQLString, sql::SQLString > *conVal;
879907
try {
@@ -909,22 +937,22 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
909937
} else if (!it->first.compare(OPT_CONNECT_ATTR_RESET)) {
910938
proxy->options(MYSQL_OPT_CONNECT_ATTR_RESET, 0);
911939

912-
#if MYCPPCONN_STATIC_MYSQL_VERSION_ID > 80000
940+
#if MYCPPCONN_STATIC_MYSQL_VERSION_ID > 80000
913941

914942
//Deprecated
915943
} else if (!it->first.compare("sslVerify")) {
916944

917945
ssl_mode ssl_mode_val = (it->second).get< bool >() ? SSL_MODE_VERIFY_CA
918-
: SSL_MODE_PREFERRED;
946+
: SSL_MODE_PREFERRED;
919947
proxy->options(MYSQL_OPT_SSL_MODE, &ssl_mode_val);
920948

921949
//Deprecated
922950
} else if (!it->first.compare("sslEnforce")) {
923951
ssl_mode ssl_mode_val = (it->second).get< bool >() ? SSL_MODE_REQUIRED
924-
: SSL_MODE_PREFERRED;
952+
: SSL_MODE_PREFERRED;
925953
proxy->options(MYSQL_OPT_SSL_MODE, &ssl_mode_val);
926954

927-
#endif
955+
#endif
928956
} else if (!it->first.compare(OPT_OCI_CONFIG_FILE)) {
929957
try {
930958
p_s= (it->second).get<sql::SQLString>();
@@ -938,12 +966,19 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
938966
"oci-config-file",
939967
*p_s);
940968
} catch (sql::InvalidArgumentException &e) {
941-
throw ::sql::SQLUnsupportedOptionException(e.what(), OPT_OCI_CONFIG_FILE);
969+
throw ::sql::SQLUnsupportedOptionException(
970+
"Failed to set config file for authentication_oci_client plugin",
971+
OPT_OCI_CONFIG_FILE
972+
);
942973
}
943974

975+
}
976+
else if (!it->first.compare(OPT_PLUGIN_DIR)) {
977+
// Nothing to do here: this option was handeld before the loop
978+
944979
/* If you need to add new integer connection option that should result in
945-
calling mysql_optiong - add its mapping to the intOptions array
946-
*/
980+
calling mysql_optiong - add its mapping to the intOptions array
981+
*/
947982
} else if (PROCESS_CONN_OPTION(int, intOptions)) {
948983
// Nothing to do here
949984

driver/nativeapi/libmysql_dynamic_proxy.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,12 +482,14 @@ LibmysqlDynamicProxy::plugin_options(st_mysql_client_plugin *plugin, const char
482482
static ptr2mysql_plugin_options ptr2_plugin_options = symbol_safe_cast<ptr2mysql_plugin_options>(GetProcAddr("mysql_plugin_options"));
483483
if (ptr2_plugin_options != NULL) {
484484
if (((*ptr2_plugin_options)(plugin, option, value))) {
485-
throw sql::InvalidArgumentException("Unsupported option provided to mysql_plugin_options()");
485+
std::string err("Failed to set plugin option");
486+
err += " '" + std::string(option) + "'";
487+
throw sql::InvalidArgumentException(err);
486488
} else {
487489
return 0;
488490
}
489491
} else {
490-
throw ::sql::MethodNotImplementedException("::mysql_options4()");
492+
throw ::sql::MethodNotImplementedException("::mysql_plugin_options");
491493
}
492494
}
493495
/* }}} */

driver/nativeapi/libmysql_static_proxy.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,9 @@ int
359359
LibmysqlStaticProxy::plugin_options(st_mysql_client_plugin *plugin, const char *option, const void *value)
360360
{
361361
if (::mysql_plugin_options(plugin, option, value)) {
362-
throw sql::InvalidArgumentException("Unsupported option provided to mysql_plugin_options()");
362+
std::string err("Failed to set plugin option");
363+
err += " '" + std::string(option) + "'";
364+
throw sql::InvalidArgumentException(err);
363365
} else {
364366
return 0;
365367
}

0 commit comments

Comments
 (0)