-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongobackend.cpp
More file actions
354 lines (312 loc) · 11.2 KB
/
Copy pathmongobackend.cpp
File metadata and controls
354 lines (312 loc) · 11.2 KB
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
347
348
349
350
351
352
353
354
#include <string>
#include <iostream>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include <boost/tokenizer.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/exception/exception.hpp>
#include <bsoncxx/builder/basic/array.hpp>
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>
#include <bsoncxx/types.hpp>
#include <bsoncxx/json.hpp>
#include "loguru.hpp"
#include "exceptions.h"
#include "ldapproto.h"
#include "storage.h"
namespace Storage {
namespace Mongo {
using bsoncxx::builder::basic::document;
using bsoncxx::builder::basic::array;
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::sub_document;
using bsoncxx::builder::basic::sub_array;
std::list<std::string> dnToList(std::string dn) {
std::list<std::string> dnParts;
using tokenizer = boost::tokenizer<boost::escaped_list_separator<char>>;
tokenizer tok(dn);
for (tokenizer::iterator dnIt = tok.begin(); dnIt != tok.end(); ++dnIt)
{
auto part = std::string{*dnIt};
auto eqPos = part.find("=");
if (eqPos == std::string::npos || part.size() <= eqPos + 1) {
LOG_S(ERROR) << "Error parsing DN for \"" << dn << "\'";
throw Ldap::Exception(Ldap::ErrorCode::invalidDNSyntax);
}
auto varName = part.substr(0, eqPos);
auto varValue = part.substr(eqPos + 1);
boost::to_lower(varName);
boost::trim(varName);
boost::trim(varValue);
std::stringstream valBuf;
valBuf << varName << "=" << varValue;
dnParts.push_back(valBuf.str());
}
return dnParts;
}
std::string dnPartsToId(const std::list<std::string>& parts) {
std::list<std::string> reversedList(parts.rbegin(), parts.rend());
return boost::algorithm::join(reversedList, ",");
}
MongoCursor::iterator& MongoCursor::iterator::operator++() {
++_cursorIt;
return *this;
}
void MongoCursor::iterator::refreshDocument() {
bsoncxx::document::view resultDoc;
try {
resultDoc = *_cursorIt;
} catch (const mongocxx::exception& e) {
LOG_S(ERROR) << "Error fetching next document: " << e.what();
throw Ldap::Exception(Ldap::ErrorCode::operationsError);
}
std::string dn{ resultDoc["_id"].get_utf8().value };
dn = dnPartsToId(dnToList(dn));
curEntry = Ldap::Entry { dn };
for (bsoncxx::document::element el: resultDoc) {
// We've already parsed the _id above
std::string key{ el.key() };
if (key == "_id") {
continue;
}
switch(el.type()) {
case bsoncxx::type::k_utf8:
curEntry.appendValue(key, std::string{ el.get_utf8().value });
break;
case bsoncxx::type::k_array: {
bsoncxx::array::view values{el.get_array().value};
for (bsoncxx::array::element subEl: values) {
curEntry.appendValue(key, std::string { subEl.get_utf8().value });
}
}
break;
default:
break;
}
}
}
MongoCursor::iterator MongoCursor::begin() {
try {
return iterator{_cursor.begin()};
} catch (const mongocxx::exception& e) {
LOG_S(ERROR) << "Error getting beginning of search results: " << e.what();
throw Ldap::Exception(Ldap::ErrorCode::operationsError, e.what());
}
}
MongoCursor::iterator MongoCursor::end() {
try {
return iterator{_cursor.end()};
} catch (const mongocxx::exception& e) {
LOG_S(ERROR) << "Error getting end of search results: " << e.what();
throw Ldap::Exception(Ldap::ErrorCode::operationsError, e.what());
}
}
MongoBackend::MongoBackend(
std::string connectURI,
std::string db,
std::string collection,
std::string rootDN
) :
_client { mongocxx::uri { connectURI } },
_collection { _client[db][collection] },
_rootdn { rootDN }
{}
void MongoBackend::saveEntry(Ldap::Entry e, bool insert) {
std::string dnId = dnPartsToId(dnToList(e.dn));
auto updateDoc = document{};
updateDoc.append(kvp("_id", dnId));
for (auto && attr: e.attributes) {
auto values = attr.second;
if (values.size() > 1) {
updateDoc.append(kvp(attr.first, [values](sub_array subArray) {
for(auto && v: values) {
subArray.append(v);
}
}));
} else {
updateDoc.append(kvp(attr.first, values[0]));
}
}
try {
if (insert) {
_collection.insert_one(updateDoc.view());
} else {
auto opts = mongocxx::options::update();
opts.upsert(true);
auto filterDoc = document{};
filterDoc.append(kvp("_id", dnId));
_collection.replace_one(filterDoc.view(), updateDoc.view(), opts);
}
} catch (const mongocxx::exception) {
LOG_S(ERROR) << "Error " << (insert ? "inserting" : "updating") << " document for "
<< "dn " << e.dn;
throw Ldap::Exception(Ldap::ErrorCode::operationsError);
}
}
std::unique_ptr<Ldap::Entry> MongoBackend::findEntry(std::string dn) {
auto e = std::unique_ptr<Ldap::Entry>{new Ldap::Entry{dn}};
auto searchDoc = document{};
searchDoc.append(kvp("_id", dnPartsToId(dnToList(dn))));
mongocxx::stdx::optional<bsoncxx::document::value> resultDoc;
try {
resultDoc = _collection.find_one(searchDoc.view());
} catch (const mongocxx::exception& e) {
LOG_S(ERROR) << "Error finding " << dn << ": " << e.what();
throw Ldap::Exception(Ldap::ErrorCode::operationsError);
}
if (!resultDoc)
throw Ldap::Exception(Ldap::ErrorCode::noSuchObject);
for (bsoncxx::document::element el: resultDoc->view()) {
// We've already parsed the _id above
std::string key{ el.key() };
if (key == "_id") {
continue;
}
switch(el.type()) {
case bsoncxx::type::k_utf8:
e->appendValue(key, std::string{ el.get_utf8().value });
break;
case bsoncxx::type::k_array: {
bsoncxx::array::view values{el.get_array().value};
for (bsoncxx::array::element subEl: values) {
e->appendValue(key, std::string { subEl.get_utf8().value });
}
}
break;
default:
break;
}
}
return e;
}
void processFilter(Ldap::Search::Filter filter, sub_document & searchDoc) {
using Type = Ldap::Search::Filter::Type;
switch (filter.type) {
case Type::And:
searchDoc.append(kvp("$and", [filter](sub_array arr) {
for (auto && c: filter.children) {
arr.append([c](sub_document subDoc) {
processFilter(c, subDoc);
});
}
}));
break;
case Type::Or:
searchDoc.append(kvp("$or", [filter](sub_array arr) {
for (auto && c: filter.children) {
arr.append([c](sub_document subDoc) {
processFilter(c, subDoc);
});
}
}));
break;
case Type::Not:
searchDoc.append(kvp("$not", [filter](sub_document subDoc) {
processFilter(filter.children[0], subDoc);
}));
break;
case Type::Eq:
searchDoc.append(kvp(filter.attributeName, filter.value));
break;
case Type::Sub: {
using SubType = Ldap::Search::SubFilter::Type;
std::stringstream subBuffer;
for (auto && c: filter.subChildren) {
switch(c.type) {
case SubType::Initial:
subBuffer << "^" << c.value;
break;
case SubType::Any:
subBuffer << ".+" << c.value;
break;
case SubType::Final:
subBuffer << ".+" << c.value << "$";
break;
}
}
searchDoc.append(kvp(filter.attributeName,
bsoncxx::types::b_regex{ subBuffer.str(), "" }));
}
break;
case Type::Gte:
searchDoc.append(kvp(filter.attributeName, [filter](sub_document gteDoc) {
gteDoc.append(kvp("$gte", filter.value));
}));
break;
case Type::Lte:
searchDoc.append(kvp(filter.attributeName, [filter](sub_document lteDoc) {
lteDoc.append(kvp("$lte", filter.value));
}));
break;
case Type::Present:
searchDoc.append(kvp(filter.attributeName, [](sub_document lteDoc) {
lteDoc.append(kvp("$exists", true));
}));
break;
case Type::Approx:
case Type::Extensible:
// TODO implement these!
throw Ldap::Exception(Ldap::ErrorCode::unavailableCriticalExtension);
break;
}
}
std::unique_ptr<MongoCursor> MongoBackend::findEntries(Ldap::Search::Request req) {
auto searchDocument = document{};
auto baseDnId = dnPartsToId(dnToList(req.base));
using Scope = Ldap::Search::Request::Scope;
std::stringstream regexBuf;
regexBuf << "^" << baseDnId;
switch(req.scope) {
case Scope::One:
regexBuf << ",?[^,]+";
break;
case Scope::Sub:
regexBuf << ",?.+";
break;
case Scope::Base:
regexBuf << "$";
break;
}
searchDocument.append(kvp("_id", bsoncxx::types::b_regex{ regexBuf.str(), "" }));
processFilter(req.filter, searchDocument);
mongocxx::options::find opts;
if (req.sizeLimit > 0) {
opts.limit(req.sizeLimit);
}
if (req.timeLimit > 0) {
opts.max_time(std::chrono::milliseconds{req.timeLimit * 1000});
}
if (req.attributes.size() > 0) {
auto projection = document{};
if (req.attributes[0] == "1.1") {
projection.append(kvp("_id", 1));
}
else if(req.attributes[0] != "*") {
for (auto && attr: req.attributes) {
projection.append(kvp(attr, 1));
}
}
opts.projection(projection.extract());
}
LOG_S(INFO) << "Executing search for " << bsoncxx::to_json(searchDocument);
auto view = searchDocument.view();
auto cursor = _collection.find(view, opts);
return std::unique_ptr<MongoCursor>(new MongoCursor{ std::move(cursor) });
}
void MongoBackend::deleteEntry(std::string dn) {
auto searchDoc = document{};
std::stringstream regexBuf;
regexBuf << "^" << dnPartsToId(dnToList(dn)) << ",?.+";
searchDoc.append(kvp("_id", bsoncxx::types::b_regex{ regexBuf.str(), "" }));
try {
_collection.delete_many(searchDoc.view());
} catch (const mongocxx::exception& e) {
LOG_S(ERROR) << "Error deleting sub-tree " << dn << ": " << e.what();
throw Ldap::Exception(Ldap::ErrorCode::operationsError, e.what());
}
}
} // namespace Mongo
} // namespace Storage