Skip to content

Commit

Permalink
[AD-898] Ensure appName is set correctly (#112)
Browse files Browse the repository at this point in the history
* [AD-845] Update user and password if passed to SQLConnect.

* [AD-845] Add tests for SQLConnect.

* [AD-845] Refactor Windows-specific code.

* [AD-845] Attempt to resolve MacOS test error for writing to ODBC.INI

* [AD-845] Attempt to resolve MacOS test error for writing to ODBC.INI

* [AD-845] Fix Linux ODBC compile error.

* [AD-845] Fix MacOS ODBC compile error.

* [AD-845] Attempt to resolve Linux test error for writing to ODBC.INI

* [AD-845] Attempt to resolve Linux test error for prefixed text in error message.

* [AD-845] Refactor location to ODBC.INI

* [AD-845] Attempt to resolve MacOS test error for writing to ODBC.INI

* [AD-845] Code review suggestions.

* [AD-864] Improve UI/UX of DSN dialog.

* [AD-864] Add/update screen-shots and documentation for new DSN dialog layout.

* [AD-864] Correct validation for login timeout to allow zero.

* Update src/odbc/os/win/include/documentdb/odbc/system/ui/dsn_configuration_window.h

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

* Update src/odbc/os/win/src/system/ui/dsn_configuration_window.cpp

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

* [AD-864] Review comment improvments. User/password handling improvements.

* [AD-864] Review comment improvements. Fix rename bug. Improve dialog error handling.

* [AD-864] Review comment improvements. Replace tab with spaces.

* [AD-864] Review comment improvements. Remove commented code.

* [AD-864] Review comment improvements. Use Cmake project version to set the driver version.

* [AD-864] Review comment improvements. Set optional invalid values to default.

* [AD-864] Review comment improvements. Handle port=0 validation. Improve balloon behaviour.

* Update src/odbc/os/win/src/system/ui/custom_window.cpp

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

* [AD-898] Initial commit to add appName option on MongoCXX connection.

* [AD-864] Fix identifier name bug.

* [AD-898] Add unit test. Refactored some code.

* [AD-898] Improvements via code review comments.

* Update src/odbc-test/src/configuration_test.cpp

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

* Update src/odbc-test/src/configuration_test.cpp

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

* [AD-898] Improvements via code review comments.

Co-authored-by: Alina (Xi) Li <[email protected]>
  • Loading branch information
Bruce Irschick and alinaliBQ authored Aug 26, 2022
1 parent a390420 commit b4482f3
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 43 deletions.
10 changes: 10 additions & 0 deletions src/odbc-test/src/configuration_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,19 @@ void CheckConnectionConfig(const Configuration& cfg) {
<< "&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);
const std::string& expectedMongoDbStr = mongodbConstructor.str();

BOOST_CHECK_EQUAL(documentdb::odbc::common::ToLower(cfg.ToConnectString()),
documentdb::odbc::common::ToLower(expectedStr));
BOOST_CHECK_EQUAL(cfg.ToJdbcConnectionString(), expectedJdbcStr);
BOOST_CHECK_EQUAL(cfg.ToMongoDbConnectionString(0), expectedMongoDbStr);
}

void CheckDsnConfig(const Configuration& cfg) {
Expand Down
50 changes: 50 additions & 0 deletions src/odbc/include/documentdb/odbc/config/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,50 @@
#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_CANONICALIZEHOSTNAME "canonicalizehostname"
#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_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_SAFE "safe"
#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_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_TLSDISABLECERTIFICATEREVOCATIONCHECK \
"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"

namespace documentdb {
namespace odbc {
namespace config {
Expand Down Expand Up @@ -776,6 +820,12 @@ class Configuration {
*/
std::string ToJdbcConnectionString() const;

/**
* Formats the MongoDB connection string from configuration values.
* @return the MongoDB connection string.
*/
std::string ToMongoDbConnectionString(int32_t localSSHTunnelPort) const;

/**
* Gets the driver version properly formated.
*/
Expand Down
7 changes: 0 additions & 7 deletions src/odbc/include/documentdb/odbc/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,6 @@ class Connection : public diagnostic::DiagnosableAdapter {
*/
bool ConnectCPPDocumentDB(int32_t localSSHTunnelPort, DocumentDbError& err);

/**
* Formats the Mongo connection string from configuration values.
*
* @return the JDBC connection string.
*/
std::string FormatMongoCppConnectionString(int32_t localSSHTunnelPort) const;

/**
* Helper function to get internall SSH tunnel Port
*
Expand Down
41 changes: 41 additions & 0 deletions src/odbc/src/config/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,47 @@ void Configuration::ToJdbcOptionsMap(ArgumentMap& res) const {
AddToMap(res, "defaultFetchSize", defaultFetchSize);
}

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

// localSSHTunnelPort == 0 means that internal SSH tunnel option was not set
if (localSSHTunnelPort == 0) {
host = GetHostname();
port = common::LexicalCast< std::string >(GetPort());
}
std::stringstream mongoConnectionString;
const char* INIT_OPT = "?";
const char* SUBS_OPT = "&";

mongoConnectionString << "mongodb:";
mongoConnectionString << "//" << EncodeURIComponent(GetUser());
mongoConnectionString << ":" << EncodeURIComponent(GetPassword());
mongoConnectionString << "@" << host;
mongoConnectionString << ":" << port;
mongoConnectionString << "/admin";
mongoConnectionString << INIT_OPT << MONGO_URI_AUTHMECHANISM
<< "=SCRAM-SHA-1";
mongoConnectionString << SUBS_OPT << MONGO_URI_APPNAME << "="
<< EncodeURIComponent(GetApplicationName());
if (IsTls()) {
mongoConnectionString << SUBS_OPT << MONGO_URI_TLSALLOWINVALIDHOSTNAMES
<< "=true";
}
if (GetLoginTimeoutSeconds()) {
std::chrono::milliseconds connectionTimeoutMS =
std::chrono::seconds(GetLoginTimeoutSeconds());
mongoConnectionString << SUBS_OPT << MONGO_URI_CONNECTTIMEOUTMS << "="
<< std::to_string(connectionTimeoutMS.count());
}

// tls configuration is handled using tls_options in connectionCPP
// TODO handle the other DSN configuration
// https://bitquill.atlassian.net/browse/AD-599

return mongoConnectionString.str();
}

std::string Configuration::GetFormatedDriverVersion() {
std::stringstream formattedVersion;
formattedVersion << std::setfill('0') << std::setw(2) << DRIVER_VERSION_MAJOR;
Expand Down
37 changes: 1 addition & 36 deletions src/odbc/src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,41 +497,6 @@ bool Connection::GetInternalSSHTunnelPort(int32_t& localSSHTunnelPort,
return true;
}

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

// localSSHTunnelPort == 0 means that internal SSH tunnel option was not set
if (localSSHTunnelPort == 0) {
host = config_.GetHostname();
port = common::LexicalCast< std::string >(config_.GetPort());
}
std::string mongoConnectionString;

mongoConnectionString = "mongodb:";
mongoConnectionString.append("//" + config_.GetUser());
mongoConnectionString.append(":" + config_.GetPassword());
mongoConnectionString.append("@" + host);
mongoConnectionString.append(":" + port);
mongoConnectionString.append("/admin");
mongoConnectionString.append("?authMechanism=SCRAM-SHA-1");
if (config_.IsTls()) {
mongoConnectionString.append("&tlsAllowInvalidHostnames=true");
}
if (config_.GetLoginTimeoutSeconds()) {
std::chrono::milliseconds connectionTimeoutMS = std::chrono::seconds(config_.GetLoginTimeoutSeconds());
mongoConnectionString.append("&connectTimeoutMS="
+ std::to_string(connectionTimeoutMS.count()));
}

// tls configuration is handled using tls_options in connectionCPP
// TODO handle the other DSN configuration
// https://bitquill.atlassian.net/browse/AD-599

return mongoConnectionString;
}

SharedPointer< JniContext > Connection::GetJniContext(JniErrorInfo& errInfo) {
if (!jniContext_.IsValid()) {
// Resolve DOCUMENTDB_HOME.
Expand Down Expand Up @@ -657,7 +622,7 @@ bool Connection::ConnectCPPDocumentDB(int32_t localSSHTunnelPort,
DriverInstance::getInstance().initialize();
try {
std::string mongoCPPConnectionString =
FormatMongoCppConnectionString(localSSHTunnelPort);
config_.ToMongoDbConnectionString(localSSHTunnelPort);
mongocxx::options::client client_options;
mongocxx::options::tls tls_options;
if (config_.IsTls()) {
Expand Down

0 comments on commit b4482f3

Please sign in to comment.