Skip to content

Commit

Permalink
[AD-599] Adapt to use all DSN configuration on mongo cxx driver conne…
Browse files Browse the repository at this point in the history
…ction (#118)

* adapted all dsn configuration to be used on mongo cxx driver

* fixed local test connection and unit tests

* [AD-599] Set the allow_invalid_certificates option.

* Apply suggestions from code review

Co-authored-by: Alina (Xi) Li <[email protected]>

* Apply suggestions from code review

Co-authored-by: Bruce Irschick <[email protected]>
Co-authored-by: Alina (Xi) Li <[email protected]>
  • Loading branch information
3 people authored Sep 16, 2022
1 parent 956dddd commit 254bb83
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 54 deletions.
15 changes: 11 additions & 4 deletions src/odbc-test/src/configuration_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,16 +280,23 @@ void CheckConnectionConfig(const Configuration& cfg) {
<< "&sshUser=" << testSshUser << "&tls=" << BoolToStr(testTlsFlag)
<< "&tlsAllowInvalidHostnames="
<< BoolToStr(testTlsAllowInvalidHostnamesFlag)
<< "&tlsCaFile=" << EncodeURIComponent(testTlsCaFile);
<< "&tlsCAFile=" << EncodeURIComponent(testTlsCaFile);
const std::string& expectedJdbcStr = jdbcConstructor.str();

std::stringstream mongodbConstructor;
mongodbConstructor
<< "mongodb://" << testUsername << ":" << testPassword << "@"
<< testHostname << ':' << testServerPort << "/admin"
<< "?authmechanism=SCRAM-SHA-1"
<< "&appname=" << testAppName
<< "&connecttimeoutms=" << (testLoginTimeoutSec * 1000);
<< "?appName=" << testAppName
<< "&authMechanism=SCRAM-SHA-1"
<< "&connectTimeoutMS=" << (testLoginTimeoutSec * 1000)
<< "&readPreference=" << ReadPreference::ToJdbcString(testReadPreference)
<< "&replicaSet=" << testReplicaSet
<< "&retryReads=" << BoolToStr(testRetryReads)
<< "&tls=" << BoolToStr(testTlsFlag)
<< "&tlsAllowInvalidHostnames="
<< BoolToStr(testTlsAllowInvalidHostnamesFlag)
<< "&tlsCAFile=" << EncodeURIComponent(testTlsCaFile);
const std::string& expectedMongoDbStr = mongodbConstructor.str();

BOOST_CHECK_EQUAL(documentdb::odbc::common::ToLower(cfg.ToConnectString()),
Expand Down
73 changes: 40 additions & 33 deletions src/odbc/include/documentdb/odbc/config/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,49 +36,49 @@
#define DRIVER_VERSION_MINOR PROJECT_VERSION_MINOR
#define DRIVER_VERSION_PATCH PROJECT_VERSION_PATCH

#define MONGO_URI_APPNAME "appname"
#define MONGO_URI_AUTHMECHANISM "authmechanism"
#define MONGO_URI_AUTHMECHANISMPROPERTIES "authmechanismproperties"
#define MONGO_URI_AUTHSOURCE "authsource"
#define MONGO_URI_APPNAME "appName"
#define MONGO_URI_AUTHMECHANISM "authMechanism"
#define MONGO_URI_AUTHMECHANISMPROPERTIES "authMechanismProperties"
#define MONGO_URI_AUTHSOURCE "authSource"
#define MONGO_URI_CANONICALIZEHOSTNAME "canonicalizehostname"
#define MONGO_URI_CONNECTTIMEOUTMS "connecttimeoutms"
#define MONGO_URI_CONNECTTIMEOUTMS "connectTimeoutMS"
#define MONGO_URI_COMPRESSORS "compressors"
#define MONGO_URI_DIRECTCONNECTION "directconnection"
#define MONGO_URI_GSSAPISERVICENAME "gssapiservicename"
#define MONGO_URI_HEARTBEATFREQUENCYMS "heartbeatfrequencyms"
#define MONGO_URI_GSSAPISERVICENAME "gssapiServiceName"
#define MONGO_URI_HEARTBEATFREQUENCYMS "heartbeatFrequencyMS"
#define MONGO_URI_JOURNAL "journal"
#define MONGO_URI_LOCALTHRESHOLDMS "localthresholdms"
#define MONGO_URI_MAXIDLETIMEMS "maxidletimems"
#define MONGO_URI_MAXPOOLSIZE "maxpoolsize"
#define MONGO_URI_MAXSTALENESSSECONDS "maxstalenessseconds"
#define MONGO_URI_MINPOOLSIZE "minpoolsize"
#define MONGO_URI_READCONCERNLEVEL "readconcernlevel"
#define MONGO_URI_READPREFERENCE "readpreference"
#define MONGO_URI_READPREFERENCETAGS "readpreferencetags"
#define MONGO_URI_REPLICASET "replicaset"
#define MONGO_URI_RETRYREADS "retryreads"
#define MONGO_URI_RETRYWRITES "retrywrites"
#define MONGO_URI_LOCALTHRESHOLDMS "localThresholdMS"
#define MONGO_URI_MAXIDLETIMEMS "maxIdleTimeMS"
#define MONGO_URI_MAXPOOLSIZE "maxPoolSize"
#define MONGO_URI_MAXSTALENESSSECONDS "maxStalenessSeconds"
#define MONGO_URI_MINPOOLSIZE "minPoolSize"
#define MONGO_URI_READCONCERNLEVEL "readConcernLevel"
#define MONGO_URI_READPREFERENCE "readPreference"
#define MONGO_URI_READPREFERENCETAGS "readPreferenceTags"
#define MONGO_URI_REPLICASET "replicaSet"
#define MONGO_URI_RETRYREADS "retryReads"
#define MONGO_URI_RETRYWRITES "retryWrites"
#define MONGO_URI_SAFE "safe"
#define MONGO_URI_SERVERSELECTIONTIMEOUTMS "serverselectiontimeoutms"
#define MONGO_URI_SERVERSELECTIONTRYONCE "serverselectiontryonce"
#define MONGO_URI_SERVERSELECTIONTIMEOUTMS "serverSelectionTimeoutMS"
#define MONGO_URI_SERVERSELECTIONTRYONCE "serverSelectionTryOnce"
#define MONGO_URI_SLAVEOK "slaveok"
#define MONGO_URI_SOCKETCHECKINTERVALMS "socketcheckintervalms"
#define MONGO_URI_SOCKETTIMEOUTMS "sockettimeoutms"
#define MONGO_URI_SOCKETTIMEOUTMS "socketTimeoutMS"
#define MONGO_URI_TLS "tls"
#define MONGO_URI_TLSCERTIFICATEKEYFILE "tlscertificatekeyfile"
#define MONGO_URI_TLSCERTIFICATEKEYFILEPASSWORD "tlscertificatekeyfilepassword"
#define MONGO_URI_TLSCAFILE "tlscafile"
#define MONGO_URI_TLSALLOWINVALIDCERTIFICATES "tlsallowinvalidcertificates"
#define MONGO_URI_TLSALLOWINVALIDHOSTNAMES "tlsallowinvalidhostnames"
#define MONGO_URI_TLSINSECURE "tlsinsecure"
#define MONGO_URI_TLSCERTIFICATEKEYFILE "tlsCertificateKeyFile"
#define MONGO_URI_TLSCERTIFICATEKEYFILEPASSWORD "tlsCertificateKeyFilePassword"
#define MONGO_URI_TLSCAFILE "tlsCAFile"
#define MONGO_URI_TLSALLOWINVALIDCERTIFICATES "tlsAllowInvalidCertificates"
#define MONGO_URI_TLSALLOWINVALIDHOSTNAMES "tlsAllowInvalidHostnames"
#define MONGO_URI_TLSINSECURE "tlsInsecure"
#define MONGO_URI_TLSDISABLECERTIFICATEREVOCATIONCHECK \
"tlsdisablecertificaterevocationcheck"
#define MONGO_URI_TLSDISABLEOCSPENDPOINTCHECK "tlsdisableocspendpointcheck"
"tlsDisableCertificateRevocationCheck"
#define MONGO_URI_TLSDISABLEOCSPENDPOINTCHECK "tlsDisableOCSPEndpointCheck"
#define MONGO_URI_W "w"
#define MONGO_URI_WAITQUEUEMULTIPLE "waitqueuemultiple"
#define MONGO_URI_WAITQUEUETIMEOUTMS "waitqueuetimeoutms"
#define MONGO_URI_WTIMEOUTMS "wtimeoutms"
#define MONGO_URI_ZLIBCOMPRESSIONLEVEL "zlibcompressionlevel"
#define MONGO_URI_WAITQUEUEMULTIPLE "waitQueueMultiple"
#define MONGO_URI_WAITQUEUETIMEOUTMS "waitQueueTimeoutMS"
#define MONGO_URI_WTIMEOUTMS "wtimeoutMS"
#define MONGO_URI_ZLIBCOMPRESSIONLEVEL "zlibCompressionLevel"

namespace documentdb {
namespace odbc {
Expand Down Expand Up @@ -863,6 +863,13 @@ class Configuration {
*/
void ToJdbcOptionsMap(ArgumentMap& res) const;

/**
* Get argument map.
*
* @param res Resulting argument map.
*/
void ToMongoCommonOptionsMap(ArgumentMap& res) const;

/** DSN. */
SettableValue< std::string > dsn = DefaultValue::dsn;

Expand Down
51 changes: 36 additions & 15 deletions src/odbc/src/config/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,8 @@ std::string Configuration::ToJdbcConnectionString() const {
}

void Configuration::ToJdbcOptionsMap(ArgumentMap& res) const {
ToMongoCommonOptionsMap(res);
AddToMap(res, "loginTimeoutSec", loginTimeoutSec);
AddToMap(res, "readPreference", readPreference, true);
AddToMap(res, "replicaSet", replicaSet);
AddToMap(res, "retryReads", retryReads);
AddToMap(res, "tls", tls);
AddToMap(res, "tlsAllowInvalidHostnames", tlsAllowInvalidHostnames);
AddToMap(res, "tlsCaFile", tlsCaFile);
AddToMap(res, "sshUser", sshUser);
AddToMap(res, "sshHost", sshHost);
AddToMap(res, "sshPrivateKeyFile", sshPrivateKeyFile);
Expand All @@ -563,9 +558,22 @@ void Configuration::ToJdbcOptionsMap(ArgumentMap& res) const {
AddToMap(res, "schemaName", schemaName);
AddToMap(res, "refreshSchema", refreshSchema);
AddToMap(res, "defaultFetchSize", defaultFetchSize);
//TODO expose defaultAuthDB on the DSN config
//https://bitquill.atlassian.net/browse/AD-935

}

std::string Configuration::ToMongoDbConnectionString(int32_t localSSHTunnelPort) const {
void Configuration::ToMongoCommonOptionsMap(ArgumentMap& res) const {
AddToMap(res, MONGO_URI_READPREFERENCE, readPreference, true);
AddToMap(res, MONGO_URI_REPLICASET, replicaSet);
AddToMap(res, MONGO_URI_RETRYREADS, retryReads);
AddToMap(res, MONGO_URI_TLS, tls);
AddToMap(res, MONGO_URI_TLSALLOWINVALIDHOSTNAMES, tlsAllowInvalidHostnames);
AddToMap(res, MONGO_URI_TLSCAFILE, tlsCaFile);
}

std::string Configuration::ToMongoDbConnectionString(
int32_t localSSHTunnelPort) const {
std::string host = "localhost";
std::string port = std::to_string(localSSHTunnelPort);

Expand All @@ -583,21 +591,34 @@ std::string Configuration::ToMongoDbConnectionString(int32_t localSSHTunnelPort)
mongoConnectionString << ":" << EncodeURIComponent(GetPassword());
mongoConnectionString << "@" << host;
mongoConnectionString << ":" << port;
//TODO expose defaultAuthDB on the DSN config
//https://bitquill.atlassian.net/browse/AD-935
mongoConnectionString << "/admin";
mongoConnectionString << INIT_OPT << MONGO_URI_AUTHMECHANISM
<< "=SCRAM-SHA-1";
mongoConnectionString << SUBS_OPT << MONGO_URI_APPNAME << "="
mongoConnectionString << INIT_OPT << MONGO_URI_APPNAME << "="
<< EncodeURIComponent(GetApplicationName());
if (IsTls()) {
mongoConnectionString << SUBS_OPT << MONGO_URI_TLSALLOWINVALIDHOSTNAMES
<< "=true";
}
mongoConnectionString << SUBS_OPT << MONGO_URI_AUTHMECHANISM
<< "=SCRAM-SHA-1";
if (GetLoginTimeoutSeconds()) {
std::chrono::milliseconds connectionTimeoutMS =
std::chrono::seconds(GetLoginTimeoutSeconds());
mongoConnectionString << SUBS_OPT << MONGO_URI_CONNECTTIMEOUTMS << "="
<< std::to_string(connectionTimeoutMS.count());
<< EncodeURIComponent(
std::to_string(connectionTimeoutMS.count()));
}

config::Configuration::ArgumentMap arguments;
ToMongoCommonOptionsMap(arguments);
std::stringstream options;
for (config::Configuration::ArgumentMap::const_iterator it =
arguments.begin();
it != arguments.end(); ++it) {
const std::string& key = it->first;
const std::string& value = it->second;
if (!value.empty()) {
options << SUBS_OPT << key << "=" << EncodeURIComponent(value);
}
}
mongoConnectionString << options.str();

// tls configuration is handled using tls_options in connectionCPP
// TODO handle the other DSN configuration
Expand Down
4 changes: 2 additions & 2 deletions src/odbc/src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,8 @@ bool Connection::ConnectCPPDocumentDB(int32_t localSSHTunnelPort,
mongocxx::options::client client_options;
mongocxx::options::tls tls_options;
if (config_.IsTls()) {
// TO-DO Adapt to use certificates
// https://bitquill.atlassian.net/browse/AD-598
// TODO: Enable use of Amazon RDS CA certificate in driver
// [AD-941](https://bitquill.atlassian.net/browse/AD-941)
tls_options.allow_invalid_certificates(true);
client_options.tls_opts(tls_options);
}
Expand Down

0 comments on commit 254bb83

Please sign in to comment.