forked from telefonicaid/fiware-orion
-
Notifications
You must be signed in to change notification settings - Fork 43
/
mongocInit.cpp
346 lines (302 loc) · 11.4 KB
/
mongocInit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
*
* Copyright 2019 FIWARE Foundation e.V.
*
* This file is part of Orion-LD Context Broker.
*
* Orion-LD Context Broker is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Orion-LD Context Broker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Orion-LD Context Broker. If not, see http://www.gnu.org/licenses/.
*
* For those usages not covered by this license please contact with
* orionld at fiware dot org
*
* Author: Ken Zangelin
*/
#include <stdlib.h> // calloc, free
#include <string.h> // strchr
#include <semaphore.h> // sem_init
#include <mongoc/mongoc.h> // MongoDB C Client Driver
#include "logMsg/logMsg.h" // LM_*
#include "logMsg/traceLevels.h" // LmtMongoc
#include "orionld/common/orionldState.h" // orionldState, mongocPool, ...
#include "orionld/common/tenantList.h" // tenant0 - the default tenant
#include "orionld/mongoc/mongocTenantsGet.h" // mongocTenantsGet
#include "orionld/mongoc/mongocGeoIndexInit.h" // mongocGeoIndexInit
#include "orionld/mongoc/mongocIdIndexCreate.h" // mongocIdIndexCreate
#include "orionld/mongoc/mongocInit.h" // Own interface
// -----------------------------------------------------------------------------
//
// mongocLog -
//
static void mongocLog
(
mongoc_log_level_t level,
const char* domain,
const char* msg,
void* userData
)
{
if (level == MONGOC_LOG_LEVEL_CRITICAL)
LM_E(("MONGOC[%s]:critical %s", domain, msg)); // Perhaps even LM_X ?
else if (level == MONGOC_LOG_LEVEL_ERROR)
LM_E(("MONGOC[%s]:error: %s", domain, msg));
else if (level == MONGOC_LOG_LEVEL_WARNING)
LM_W(("MONGOC[%s]:warning: %s", domain, msg));
else if (level == MONGOC_LOG_LEVEL_MESSAGE)
LM_M(("MONGOC[%s]:message: %s", domain, msg)); // This LM_M is OK
else if (level == MONGOC_LOG_LEVEL_INFO)
LM_I(("MONGOC[%s]:info: %s", domain, msg));
else if ((level == MONGOC_LOG_LEVEL_DEBUG) || (level == MONGOC_LOG_LEVEL_TRACE))
{
if (lmTraceIsSet(LmtMongoc) == true)
LM_M(("MONGOC[%s]: %s", domain, msg));
}
}
// -----------------------------------------------------------------------------
//
// uriCompose -
//
// All options for connection to the built-in mongoc connection pool are CLI options for Orion-LD
// and thus global variables in orionldState.h/cpp:
// - dbHost
// - dbUser
// - dbPwd
// - dbAuthDb
// - rplSet
// - dbAuthMech
// - dbSSL
//
// The URI for the connection looks like this:
// mongodb://[dbUser:dbPwd@]dbHost/[dbAuthDb]? [replicaSet=dbReplicaSet] [&authMechanism=dbAuthMech] [&tls=true&tlsAllowInvalidCertificates=true]
//
// Note that dbHost can be a comma-separated list of hostnames (and ports)
//
//
// About Connecting to a server over TLS
//
// Orion does this:
// if (dbSSL)
// uri += optionPrefix + "tls=true&tlsAllowInvalidCertificates=true";
//
// So, no certificate is used.
// Not sure that's the best idea though ...
//
// http://mongoc.org/libmongoc/current/advanced-connections.html:
// -----------------------------------------------------------------------------------------------------------------------------------------------
// MongoDB requires client certificates by default, unless the --tlsAllowConnectionsWithoutCertificates is provided.
//
// The C Driver can be configured to present a client certificate using the URI option tlsCertificateKeyFile,
// which may be referenced through the constant MONGOC_URI_TLSCERTIFICATEKEYFILE:
//
// mongoc_client_t* client = NULL;
// mongoc_uri_t* uri = mongoc_uri_new("mongodb://localhost:27017/?tls=true");
// mongoc_uri_set_option_as_utf8(uri, MONGOC_URI_TLSCERTIFICATEKEYFILE, "client.pem"); // "client.pem" would be a CLI for Orion-LD
// client = mongoc_client_new_from_uri(uri);
// -----------------------------------------------------------------------------------------------------------------------------------------------
//
//
// Apart from what I've implemented so far:
// * Connecting to a UNIX Domain Socket: mongoc_uri_new("mongodb://%2Ftmp%2Fmongodb-27017.sock");
// * Compressing data to and from MongoDB: mongoc_client_new("mongodb://localhost:27017/?compressors=snappy,zlib,zstd");
// * Disable Retry Writes mongodb://localhost/?retryWrites=false
// * Connection timeout mongodb://localhost/?connectTimeoutMS=<millisecs>
// * SRV record: mongodb+srv://localhost ...
// * And another 100 options or so: See complete list here: http://mongoc.org/libmongoc/current/mongoc_uri_t.html
//
// So, perhaps I should just NOT try to fix the URI myself, just use it as is and let the users define the URI ...
// Makes no sense to implement that myself, and check for everything - will just add bugs, for nothing
// Default would be "mongodb://localhost" (port 27017 is already default) and if a user wants something else,
// just pass the URI when starting the broker.
//
// BTW, Orion's tlsallowinvalidcertificates is DEPRECATED.
//
static char* uriCompose
(
char* dbURI,
char* dbHost,
char* dbUser,
char* dbPwd,
char* dbAuthDb,
char* dbReplicaSet,
char* dbAuthMechanism,
bool dbSSL,
char* tlsCertificateFilePath
)
{
char* compV[50];
int compNo = 0;
LM_T(LmtMongoc, ("dbURI: '%s'", dbURI));
LM_T(LmtMongoc, ("dbHost: '%s'", dbHost));
LM_T(LmtMongoc, ("dbUser: '%s'", dbUser));
if (dbPwd != NULL)
LM_T(LmtMongoc, ("dbPwd: '****'"));
LM_T(LmtMongoc, ("dbAuthDb: '%s'", dbAuthDb));
LM_T(LmtMongoc, ("dbReplicaSet: '%s'", dbReplicaSet));
LM_T(LmtMongoc, ("dbAuthMechanism: '%s'", dbAuthMechanism));
LM_T(LmtMongoc, ("dbSSL: '%s'", (dbSSL == true)? "true" : "false"));
LM_T(LmtMongoc, ("tlsCertificate: '%s'", tlsCertificateFilePath));
if (dbURI[0] != 0)
{
//
// OK, the complete URI comes in dbURI
//
// Is "${PWD}" present?
// If so, split dbURI into two parts - before and after "${PWD}" and add dbPwd in between
//
compV[0] = dbURI;
compNo = 1;
char* pwdP = strstr(dbURI, "${PWD}");
if (pwdP != NULL)
{
if (dbPwd[0] == 0)
LM_X(1, ("Invalid Command Line Options: -dbURI is used with a password substitution, but no password (-dbPwd) is supplied"));
*pwdP = 0;
compV[1] = dbPwd;
compV[2] = &pwdP[6];
compNo = 3;
LM_T(LmtMongoc, ("dbURI: '%s****%s'", compV[0], compV[2]));
}
}
else
{
compV[compNo++] = (char*) "mongodb://";
if ((dbUser != NULL) && (dbUser[0] != 0))
{
compV[compNo++] = dbUser;
compV[compNo++] = (char*) ":";
compV[compNo++] = dbPwd;
compV[compNo++] = (char*) "@";
}
compV[compNo++] = dbHost;
//
// If dbHost is a list, the list must end with a slash
// Assuming it's a list if there's a comma in the string
//
// Correction: seems like the slash is needed ALWAYS
//
// if (strchr(dbHost, ',') != NULL)
compV[compNo++] = (char*) "/";
bool dbAuthDbPresent = (dbAuthDb != NULL) && (dbAuthDb[0] != 0);
bool dbReplicaSetPresent = (dbReplicaSet != NULL) && (dbReplicaSet[0] != 0);
bool dbAuthMechanismPresent = (dbAuthMechanism != NULL) && (dbAuthMechanism[0] != 0);
if ((dbAuthDbPresent == true) || (dbReplicaSetPresent == true) || (dbAuthMechanismPresent == true) || (dbSSL == true))
compV[compNo++] = (char*) "?";
if (dbAuthDbPresent == true)
compV[compNo++] = dbAuthDb;
if (dbReplicaSetPresent == true)
{
compV[compNo++] = (char*) "replicaSet=";
compV[compNo++] = dbReplicaSet;
compV[compNo++] = (char*) "&";
}
if (dbAuthMechanismPresent == true)
{
compV[compNo++] = (char*) "authMechanism=";
compV[compNo++] = dbAuthMechanism;
compV[compNo++] = (char*) "&";
}
if (dbSSL == true)
{
if (tlsCertificateFilePath == NULL)
compV[compNo++] = (char*) "&tls=true&tlsAllowConnectionsWithoutCertificates=true&";
else
{
compV[compNo++] = (char*) "&tls=true&tlsCertificateKeyFile=";
compV[compNo++] = tlsCertificateFilePath;
}
}
}
//
// Count all string length and get the total length of the URI
//
int uriLen = 0;
for (int ix = 0; ix < compNo; ix++)
{
uriLen += strlen(compV[ix]);
}
char* uri = (char*) calloc(1, uriLen + 1);
if (uri == NULL)
LM_X(1, ("Out of memory allocating a mongo connection URI of length %d", uriLen + 1));
int len = 0;
for (int ix = 0; ix < compNo; ix++)
{
strncpy(&uri[len], compV[ix], uriLen - len);
len += strlen(compV[ix]);
}
return uri;
}
// -----------------------------------------------------------------------------
//
// mongocInit -
//
void mongocInit
(
char* dbURI,
char* dbHost,
char* dbUser,
char* dbPwd,
char* dbAuthDb,
char* dbReplicaSet,
char* dbAuthMechanism,
bool dbSSL,
char* tlsCertificateFilePath
)
{
//
// Redirect mongoc log messages from stdout to OrionLD's own log file
//
mongoc_log_set_handler(mongocLog, NULL);
// mongoc_log_trace_enable();
//
// Initialize libmongoc's internals
//
mongoc_init();
//
// Create a MongoDB URI object from the given connection parameters
//
char* mongoUri = uriCompose(dbURI, dbHost, dbUser, dbPwd, dbAuthDb, dbReplicaSet, dbAuthMechanism, dbSSL, tlsCertificateFilePath);
bson_error_t mongoError;
LM_K(("Connecting to mongo for the C driver (URI: %s)", mongoUri));
mongocUri = mongoc_uri_new_with_error(mongoUri, &mongoError);
if (mongocUri == NULL)
LM_X(1, ("Unable to connect to mongo(URI: %s): %s", mongoUri, mongoError.message));
//
// Initialize the connection pool
//
mongocPool = mongoc_client_pool_new(mongocUri);
//
// We want the newer, better, error handling
//
mongoc_client_pool_set_error_api(mongocPool, 2);
//
// Semaphore for the 'contexts' collection on DB 'orionld' - hopefully not needed in the end ...
//
sem_init(&mongocContextsSem, 0, 1); // 0: shared between threads of the same process. 1: free to be taken
//
// Semaphore for getting a connection/collection for mongo
//
sem_init(&mongocConnectionSem, 0, 1); // 0: shared between threads of the same process. 1: free to be taken
int tenants = 1;
if (mongocTenantsGet(&tenants) == false)
LM_X(1, ("Unable to extract tenants from the database - fatal error"));
LM_T(LmtMongoPool, ("No of tenants: %d", tenants));
extern int dbPoolSize;
if (dbPoolSize < tenants)
dbPoolSize = tenants + 5;
if (mongocGeoIndexInit() == false)
LM_X(1, ("Unable to initialize geo indices in database - fatal error"));
if (mongocIdIndexCreate(&tenant0) == false)
LM_W(("Unable to create the index on Entity ID on the default database"));
// Free the uri, allocated by uriCompose
free(mongoUri);
}