Skip to content

Commit

Permalink
Add -dbURI option
Browse files Browse the repository at this point in the history
  • Loading branch information
ctc-nakatsuka committed Jul 21, 2023
1 parent 083f032 commit e4ef003
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 41 deletions.
5 changes: 4 additions & 1 deletion src/app/contextBroker/contextBroker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ char authMech[64];
char authDb[64];
bool dbSSL;
bool dbDisableRetryWrites;
char dbURI[1024];
char pidPath[256];
bool harakiri;
bool useOnlyIPv4;
Expand Down Expand Up @@ -276,6 +277,7 @@ bool logDeprecate;
#define NGSIV1_AUTOCAST_DESC "automatic cast for number, booleans and dates in NGSIv1 update/create attribute operations"
#define MQTT_MAX_AGE_DESC "max time (in minutes) that an unused MQTT connection is kept, default: 60"
#define LOG_DEPRECATE_DESC "log deprecation usages as warnings"
#define DBURI_DESC "complete URI for database connection"



Expand All @@ -295,6 +297,7 @@ PaArgument paArgs[] =
{ "-port", &port, "PORT", PaInt, PaOpt, 1026, 1, 65535, PORT_DESC },
{ "-pidpath", pidPath, "PID_PATH", PaString, PaOpt, PIDPATH, PaNL, PaNL, PIDPATH_DESC },

{ "-dbURI", dbURI, "MONGO_URI", PaString, PaOpt, _i "", PaNL, PaNL, DBURI_DESC },
{ "-dbhost", dbHost, "MONGO_HOST", PaString, PaOpt, LOCALHOST, PaNL, PaNL, DBHOST_DESC },
{ "-rplSet", rplSet, "MONGO_REPLICA_SET", PaString, PaOpt, _i "", PaNL, PaNL, RPLSET_DESC },
{ "-dbuser", user, "MONGO_USER", PaString, PaOpt, _i "", PaNL, PaNL, DBUSER_DESC },
Expand Down Expand Up @@ -1213,7 +1216,7 @@ int main(int argC, char* argV[])
alarmMgr.init(relogAlarms);
mqttMgr.init(mqttTimeout);
orionInit(orionExit, ORION_VERSION, policy, statCounters, statSemWait, statTiming, statNotifQueue, strictIdv1);
mongoInit(dbHost, rplSet, dbName, user, pwd, authMech, authDb, dbSSL, dbDisableRetryWrites, mtenant, dbTimeout, writeConcern, dbPoolSize, statSemWait);
mongoInit(dbURI, dbHost, rplSet, dbName, user, pwd, authMech, authDb, dbSSL, dbDisableRetryWrites, mtenant, dbTimeout, writeConcern, dbPoolSize, statSemWait);
metricsMgr.init(!disableMetrics, statSemWait);
logSummaryInit(&lsPeriod);

Expand Down
4 changes: 3 additions & 1 deletion src/lib/mongoBackend/MongoGlobal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ bool mongoMultitenant(void)
*/
void mongoInit
(
const char* dbURI,
const char* dbHost,
const char* rplSet,
std::string dbName,
Expand All @@ -123,7 +124,8 @@ void mongoInit
// Set the global multitenant variable
multitenant = mtenant;

if (orion::mongoConnectionPoolInit(dbHost,
if (orion::mongoConnectionPoolInit(dbURI,
dbHost,
dbName.c_str(),
rplSet,
user,
Expand Down
1 change: 1 addition & 0 deletions src/lib/mongoBackend/MongoGlobal.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ extern bool mongoMultitenant(void);
*/
void mongoInit
(
const char* dbURI,
const char* dbHost,
const char* rplSet,
std::string dbName,
Expand Down
100 changes: 62 additions & 38 deletions src/lib/mongoDriver/mongoConnectionPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ static void mongoDriverLogger
*/
static std::string composeMongoUri
(
const char* dbURI,
const char* host,
const char* rplSet,
const char* username,
Expand All @@ -329,54 +330,76 @@ static std::string composeMongoUri
{
// Compose the mongoUri, taking into account all information

std::string uri = "mongodb://";
std::string uri;

// Add auth parameter if included
if (strlen(username) != 0 && strlen(passwd) != 0)
if (strlen(dbURI) != 0)
{
uri += username + std::string(":") + passwd + "@";
const char* pwd = strstr(dbURI, "${PWD}");
if (pwd != NULL)
{
if (strlen(passwd) == 0)
{
LM_X(1, ("Invalid Command Line Options: -dbURI is used with a password substitution, but no password (-dbpwd) is supplied"));
}

uri = std::string(dbURI, pwd - dbURI) + passwd + (pwd + 6);
}
else
{
uri = dbURI;
}
}
else
{
uri = "mongodb://";

uri += host + std::string("/");
// Add auth parameter if included
if (strlen(username) != 0 && strlen(passwd) != 0)
{
uri += username + std::string(":") + passwd + "@";
}

if (strlen(authDb) != 0)
{
uri += authDb;
}
uri += host + std::string("/");

// First option prefix is '?' symbol
std::string optionPrefix = "?";
if (strlen(authDb) != 0)
{
uri += authDb;
}

if (strlen(rplSet) != 0)
{
uri += optionPrefix + "replicaSet=" + rplSet;
optionPrefix = "&";
}
// First option prefix is '?' symbol
std::string optionPrefix = "?";

if (strlen(mechanism) != 0)
{
uri += optionPrefix + "authMechanism=" + mechanism;
optionPrefix = "&";
}
if (strlen(rplSet) != 0)
{
uri += optionPrefix + "replicaSet=" + rplSet;
optionPrefix = "&";
}

if (dbSSL)
{
uri += optionPrefix + "tls=true&tlsAllowInvalidCertificates=true";
optionPrefix = "&";
}
if (strlen(mechanism) != 0)
{
uri += optionPrefix + "authMechanism=" + mechanism;
optionPrefix = "&";
}

if (dbDisableRetryWrites)
{
uri += optionPrefix + "retryWrites=false";
optionPrefix = "&";
}
if (dbSSL)
{
uri += optionPrefix + "tls=true&tlsAllowInvalidCertificates=true";
optionPrefix = "&";
}

if (timeout > 0)
{
char buf[STRING_SIZE_FOR_LONG];
i2s(timeout, buf, sizeof(buf));
uri += optionPrefix + "connectTimeoutMS=" + buf;
optionPrefix = "&";
if (dbDisableRetryWrites)
{
uri += optionPrefix + "retryWrites=false";
optionPrefix = "&";
}

if (timeout > 0)
{
char buf[STRING_SIZE_FOR_LONG];
i2s(timeout, buf, sizeof(buf));
uri += optionPrefix + "connectTimeoutMS=" + buf;
optionPrefix = "&";
}
}

LM_T(LmtMongo, ("MongoDB connection URI: '%s'", offuscatePassword(uri, passwd).c_str()));
Expand All @@ -392,6 +415,7 @@ static std::string composeMongoUri
*/
int orion::mongoConnectionPoolInit
(
const char* dbURI,
const char* host,
const char* db,
const char* rplSet,
Expand Down Expand Up @@ -421,7 +445,7 @@ int orion::mongoConnectionPoolInit
atexit(shutdownClient);

// Set mongo Uri to connect
std::string uri = composeMongoUri(host, rplSet, username, passwd, mechanism, authDb, dbSSL, dbDisableRetryWrites, timeout);
std::string uri = composeMongoUri(dbURI, host, rplSet, username, passwd, mechanism, authDb, dbSSL, dbDisableRetryWrites, timeout);

#ifdef UNIT_TEST
/* Basically, we are mocking all the DB pool with a single connection. The getMongoConnection() and mongoReleaseConnection() methods
Expand Down
1 change: 1 addition & 0 deletions src/lib/mongoDriver/mongoConnectionPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ extern void mongoVersionGet(int* mayor, int* minor);
*/
extern int mongoConnectionPoolInit
(
const char* dbURI,
const char* host,
const char* db,
const char* rplSet,
Expand Down
4 changes: 3 additions & 1 deletion test/unittests/main_UnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ unsigned long logLineMaxSize = 32 * 1024;

bool logDeprecate = false;

char dbURI[1024];
char dbHost[256];
char rplSet[64];
char dbName[64];
Expand Down Expand Up @@ -107,6 +108,7 @@ unsigned long fcMaxInterval = 0;
*/
PaArgument paArgs[] =
{
{ "-dbURI", dbURI, "DB_URI", PaString, PaOpt, (int64_t) "", PaNL, PaNL, "" },
{ "-dbhost", dbHost, "DB_HOST", PaString, PaOpt, (int64_t) "localhost", PaNL, PaNL, "" },
{ "-rplSet", rplSet, "RPL_SET", PaString, PaOpt, (int64_t) "", PaNL, PaNL, "" },
{ "-dbuser", user, "DB_USER", PaString, PaOpt, (int64_t) "", PaNL, PaNL, "" },
Expand Down Expand Up @@ -158,7 +160,7 @@ int main(int argC, char** argV)
LM_M(("Init tests"));
orionInit(exitFunction, orionUnitTestVersion, SemReadWriteOp, false, false, false, false, false);
// Note that disableRetryTries, multitenancy and mutex time stats are disabled for unit test mongo init
mongoInit(dbHost, rplSet, dbName, user, pwd, authMech, authDb, dbSSL, false, false, dbTimeout, writeConcern, dbPoolSize, false);
mongoInit(dbURI, dbHost, rplSet, dbName, user, pwd, authMech, authDb, dbSSL, false, false, dbTimeout, writeConcern, dbPoolSize, false);
alarmMgr.init(false);
logSummaryInit(&lsPeriod);
// setupDatabase(); FIXME #3775: pending on mongo unit test re-enabling
Expand Down

0 comments on commit e4ef003

Please sign in to comment.