Skip to content

Commit

Permalink
Merge pull request #4385 from ctc-nakatsuka/add-dburi-option
Browse files Browse the repository at this point in the history
Add the CLI param "-dbURI"
  • Loading branch information
fgalan authored Aug 24, 2023
2 parents 9b520ae + 8d0617c commit 19d0f95
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Add: servicePath field to builtin attributes (#2877)
- Fix: logDeprecate not working correctly (`geo:json` wrongly considered as deprecated)
- Fix: improve error traces (#4387)
- Add: CLI parameter -dbUri / env var ORION_MONGO_URI (#3794)
4 changes: 4 additions & 0 deletions doc/manuals.jp/admin/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ broker はデフォルトでバックグラウンドで実行されるため、
- **-ipv6** : broker を IPv6 専用モードで実行します。デフォルトでは、broker は IPv4 と IPv6 の両方で動作します。-ipv4 と同時に使用することはできません。
- **-multiservice** : マルチサービス/マルチテナントモードを有効にします。[マルチ・テナンシーのセクション](../orion-api.md#multi-tenancy)を参照してください
- **-db <db>** : 使用する MogoDB データベース、または (`-multiservice` を使用している場合) サービス単位/テナント単位のデータベースのプレフィックス ([マルチ・テナンシー](../orion-api.md#multi-tenancy)のセクションを参照してください) です。このフィールドは最大10文字までです
- **-dbURI <uri>** : 使用する MongoDB を URI で指定します。
URI に文字列 `${PWD}` がある場合は `-dbpwd` または環境変数 `ORION_MONGO_PASSWORD` で指定したパスワードで置き換えられます。
このオプションは `-dbhost`, `-rplSet`, `-dbTimeout`, `-dbuser`, `-dbAuthMech`, `-dbAuthDb`, `-dbSSL`, `-dbDisableRetryWrites` と組み合わせできません。(組み合わせた場合、Orion は起動時にエラーで終了します)
- **-dbhost <host>** : 使用する MongoDB のホストとポートです。たとえば、`-dbhost localhost:12345` です
- **-rplSet <replicat_set>** : 指定すれば、Orion CB が MongoDB レプリカセット (スタンドアロン MongoDB インスタンスではなく) に接続されます。使用するレプリカセットの名前は、パラメータの値です。この場合、-dbhost パラメーターは、レプリカ・セットのシードとして使用されるホスト ("," で区切られた) のリストにすることができます
- **-dbTimeout <interval>** : レプリカセット (-rplSet) を使用する場合にのみ使用され、それ以外の場合は無視されます。レプリカセットへの接続のタイムアウトをミリ秒単位で指定します
Expand Down Expand Up @@ -139,6 +142,7 @@ Orion は、環境変数を使用した引数の受け渡しをサポートし
| ORION_LOCALIP | localIp |
| ORION_PORT | port |
| ORION_PID_PATH | pidpath |
| ORION_MONGO_URI | dbURI |
| ORION_MONGO_HOST | dbhost |
| ORION_MONGO_REPLICA_SET | rplSet |
| ORION_MONGO_USER | dbuser |
Expand Down
7 changes: 7 additions & 0 deletions doc/manuals/admin/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ The list of available options is the following:
[service/tenant database
separation](../orion-api.md#multi-tenancy). This field is restricted to 10 characters
max length.
- **-dbURI <uri>** : The URI to use the MongoDB.
If the URI contains the string `${PWD}`, it will be replaced with the password
specified in `-dbpwd` or the environment variable `ORION_MONGO_PASSWORD`.
This option cannot be combined with `-dbhost`, `-rplSet`, `-dbTimeout`, `-dbuser`,
`-dbAuthMech`, `-dbAuthDb`, `-dbSSL` and `-dbDisableRetryWrites` (if you attempt to do that
Orion will exit with an error on startup).
- **-dbhost <host>**. The MongoDB host and port to use, e.g. `-dbhost
localhost:12345`.
- **-rplSet <replicat_set>**. If used, Orion CB connnects to a
Expand Down Expand Up @@ -211,6 +217,7 @@ Two facts have to be taken into account:
| ORION_LOCALIP | localIp |
| ORION_PORT | port |
| ORION_PID_PATH | pidpath |
| ORION_MONGO_URI | dbURI |
| ORION_MONGO_HOST | dbhost |
| ORION_MONGO_REPLICA_SET | rplSet |
| ORION_MONGO_USER | dbuser |
Expand Down
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
106 changes: 68 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,82 @@ 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 + "@";
}
if (strlen(username) != 0 || strlen(authDb) != 0 || strlen(rplSet) != 0 || strlen(mechanism) != 0 || dbSSL || dbDisableRetryWrites || timeout > 0)
{
LM_X(1, ("Invalid Command Line Options: -dbURI cannot be combined with -dbhost, -rplSet, -dbTimeout, -dbuser, -dbAuthMech, -dbAuthDb, -dbSSL and -dbDisableRetryWrites"));
}

uri += host + std::string("/");
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"));
}

if (strlen(authDb) != 0)
{
uri += authDb;
// +6 is the length of the "${PWD}"
uri = std::string(dbURI, pwd - dbURI) + passwd + (pwd + 6);
}
else
{
uri = dbURI;
}
}
else
{
uri = "mongodb://";

// First option prefix is '?' symbol
std::string optionPrefix = "?";
// Add auth parameter if included
if (strlen(username) != 0 && strlen(passwd) != 0)
{
uri += username + std::string(":") + passwd + "@";
}

if (strlen(rplSet) != 0)
{
uri += optionPrefix + "replicaSet=" + rplSet;
optionPrefix = "&";
}
uri += host + std::string("/");

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

if (dbSSL)
{
uri += optionPrefix + "tls=true&tlsAllowInvalidCertificates=true";
optionPrefix = "&";
}
// First option prefix is '?' symbol
std::string optionPrefix = "?";

if (dbDisableRetryWrites)
{
uri += optionPrefix + "retryWrites=false";
optionPrefix = "&";
}
if (strlen(rplSet) != 0)
{
uri += optionPrefix + "replicaSet=" + rplSet;
optionPrefix = "&";
}

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

if (dbSSL)
{
uri += optionPrefix + "tls=true&tlsAllowInvalidCertificates=true";
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 +421,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 +451,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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Usage: contextBroker [option '-U' (extended usage)]
[option '-localIp' <IP to receive new connections>]
[option '-port' <port to receive new connections>]
[option '-pidpath' <pid file path>]
[option '-dbURI' <complete URI for database connection>]
[option '-dbhost' <database host>]
[option '-rplSet' <replica set>]
[option '-dbuser' <database user>]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Usage: contextBroker [option '-U' (extended usage)]
[option '-localIp' <IP to receive new connections>]
[option '-port' <port to receive new connections>]
[option '-pidpath' <pid file path>]
[option '-dbURI' <complete URI for database connection>]
[option '-dbhost' <database host>]
[option '-rplSet' <replica set>]
[option '-dbuser' <database user>]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Usage: contextBroker [option '-U' (extended usage)]
[option '-localIp' <IP to receive new connections>]
[option '-port' <port to receive new connections>]
[option '-pidpath' <pid file path>]
[option '-dbURI' <complete URI for database connection>]
[option '-dbhost' <database host>]
[option '-rplSet' <replica set>]
[option '-dbuser' <database user>]
Expand Down
1 change: 1 addition & 0 deletions test/functionalTest/cases/3658_env_vars/env_vars.test
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Extended Usage: contextBroker [option '-U' (extended usage)]
[option '-localIp' <IP to receive new connections>] ORION_LOCALIP '0.0.0.0' /'0.0.0.0'/
[option '-port' <port to receive new connections>] ORION_PORT 1 <= 1026 /1026/ <= 65535
[option '-pidpath' <pid file path>] ORION_PID_PATH '/tmp/contextBroker.pid' /'/tmp/contextBroker/
[option '-dbURI' <complete URI for database connection>] ORION_MONGO_URI '' /''/
[option '-dbhost' <database host>] ORION_MONGO_HOST 'localhost' /'localhost'/
[option '-rplSet' <replica set>] ORION_MONGO_REPLICA_SET '' /''/
[option '-dbuser' <database user>] ORION_MONGO_USER '' /''/
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 19d0f95

Please sign in to comment.