diff --git a/src/odbc-test/src/configuration_test.cpp b/src/odbc-test/src/configuration_test.cpp index 3942aa578..827852c86 100644 --- a/src/odbc-test/src/configuration_test.cpp +++ b/src/odbc-test/src/configuration_test.cpp @@ -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()), diff --git a/src/odbc/include/documentdb/odbc/config/configuration.h b/src/odbc/include/documentdb/odbc/config/configuration.h index 931649471..1eceae0f3 100644 --- a/src/odbc/include/documentdb/odbc/config/configuration.h +++ b/src/odbc/include/documentdb/odbc/config/configuration.h @@ -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 { @@ -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; diff --git a/src/odbc/src/config/configuration.cpp b/src/odbc/src/config/configuration.cpp index c3d0f11f8..41d42fdff 100644 --- a/src/odbc/src/config/configuration.cpp +++ b/src/odbc/src/config/configuration.cpp @@ -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); @@ -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); @@ -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 diff --git a/src/odbc/src/connection.cpp b/src/odbc/src/connection.cpp index 41f5f4957..d6a501974 100644 --- a/src/odbc/src/connection.cpp +++ b/src/odbc/src/connection.cpp @@ -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); }