-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
registry_factory.cpp
285 lines (247 loc) · 8.82 KB
/
registry_factory.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
/**
* Copyright (c) 2014-present, The osquery authors
*
* This source code is licensed as defined by the LICENSE file found in the
* root directory of this source tree.
*
* SPDX-License-Identifier: (Apache-2.0 OR GPL-2.0-only)
*/
#include <osquery/core/core.h>
#include <osquery/registry/registry_factory.h>
#include <cstdlib>
#include <sstream>
#include <osquery/extensions/extensions.h>
#include <osquery/logger/logger.h>
#include <osquery/process/process.h>
#include <osquery/registry/registry.h>
#include <osquery/utils/conversions/split.h>
#include <osquery/utils/json/json.h>
namespace osquery {
/* NOTE: the default is false, because it's easier to enable it in one place
when starting osquery, instead of having each test enable them,
so that they are seen and fixed. */
HIDDEN_FLAG(bool,
ignore_registry_exceptions,
false,
"Ignore exceptions thrown by the registry. osquery and extensions "
"default to true.");
RegistryFactory& RegistryFactory::get() {
static RegistryFactory instance;
return instance;
}
void RegistryFactory::add(const std::string& name, RegistryInterfaceRef reg) {
if (exists(name)) {
throw std::runtime_error("Cannot add duplicate registry: " + name);
}
registries_[name] = std::move(reg);
}
RegistryInterfaceRef RegistryFactory::registry(const std::string& t) const {
if (!exists(t)) {
throw std::runtime_error("Unknown registry requested: " + t);
}
return registries_.at(t);
}
std::map<std::string, RegistryInterfaceRef> RegistryFactory::all() const {
return registries_;
}
std::map<std::string, PluginRef> RegistryFactory::plugins(
const std::string& registry_name) const {
return registry(registry_name)->plugins();
}
PluginRef RegistryFactory::plugin(const std::string& registry_name,
const std::string& item_name) const {
return registry(registry_name)->plugin(item_name);
}
RegistryBroadcast RegistryFactory::getBroadcast() {
RegistryBroadcast broadcast;
for (const auto& registry : registries_) {
broadcast[registry.first] = registry.second->getRoutes();
}
return broadcast;
}
Status RegistryFactory::addBroadcast(const RouteUUID& uuid,
const RegistryBroadcast& broadcast) {
{
ReadLock lock(mutex_);
if (extensions_.count(uuid) > 0) {
return Status(1, "Duplicate extension UUID: " + std::to_string(uuid));
}
}
// Make sure the extension does not broadcast conflicting registry items.
if (!allowDuplicates()) {
for (const auto& registry : broadcast) {
for (const auto& item : registry.second) {
if (exists(registry.first, item.first)) {
VLOG(1) << "Extension " << uuid
<< " has duplicate plugin name: " << item.first
<< " in registry: " << registry.first;
return Status(1,
"Registry item " + item.first +
" conflicts with another item of the same name.");
}
}
}
}
// Once duplication is satisfied call each registry's addExternal.
Status status;
for (const auto& registry : broadcast) {
if (!exists(registry.first)) {
VLOG(1) << "Extension " << uuid
<< "contains unknown registry: " << registry.first;
return Status(1, "Unknown registry: " + registry.first);
}
status = this->registry(registry.first)->addExternal(uuid, registry.second);
if (!status.ok()) {
// If any registry fails to add the set of external routes, stop.
break;
}
for (const auto& plugin : registry.second) {
VLOG(1) << "Extension " << uuid << " registered " << registry.first
<< " plugin " << plugin.first;
}
}
// If any registry failed, remove each (assume a broadcast is atomic).
{
if (!status.ok()) {
for (const auto& registry : broadcast) {
this->registry(registry.first)->removeExternal(uuid);
}
}
WriteLock lock(mutex_);
extensions_.insert(uuid);
}
return status;
}
Status RegistryFactory::removeBroadcast(const RouteUUID& uuid) {
{
ReadLock lock(mutex_);
if (extensions_.count(uuid) == 0) {
return Status(1, "Unknown extension UUID: " + std::to_string(uuid));
}
}
for (const auto& registry : registries_) {
registry.second->removeExternal(uuid);
}
WriteLock lock(mutex_);
extensions_.erase(uuid);
return Status::success();
}
/// Adds an alias for an internal registry item. This registry will only
/// broadcast the alias name.
Status RegistryFactory::addAlias(const std::string& registry_name,
const std::string& item_name,
const std::string& alias) {
if (!exists(registry_name)) {
return Status(1, "Unknown registry: " + registry_name);
}
return registries_.at(registry_name)->addAlias(item_name, alias);
}
/// Returns the item_name or the item alias if an alias exists.
std::string RegistryFactory::getAlias(const std::string& registry_name,
const std::string& alias) const {
if (!exists(registry_name)) {
return alias;
}
return registries_.at(registry_name)->getAlias(alias);
}
Status RegistryFactory::call(const std::string& registry_name,
const std::string& item_name,
const PluginRequest& request,
PluginResponse& response) {
// Forward factory call to the registry.
try {
if (item_name.find(',') != std::string::npos) {
// Call is multiplexing plugins (usually for multiple loggers).
for (const auto& item : osquery::split(item_name, ",")) {
get().registry(registry_name)->call(item, request, response);
}
// All multiplexed items are called without regard for statuses.
return Status(0);
}
return get().registry(registry_name)->call(item_name, request, response);
} catch (const std::exception& e) {
LOG(ERROR) << registry_name << " registry " << item_name
<< " plugin caused exception: " << e.what();
if (!FLAGS_ignore_registry_exceptions) {
throw;
}
return Status(1, e.what());
} catch (...) {
LOG(ERROR) << registry_name << " registry " << item_name
<< " plugin caused unknown exception";
if (!FLAGS_ignore_registry_exceptions) {
throw std::runtime_error(registry_name + ": " + item_name + " failed");
}
return Status(2, "Unknown exception");
}
}
Status RegistryFactory::call(const std::string& registry_name,
const std::string& item_name,
const PluginRequest& request) {
PluginResponse response;
// Wrapper around a call expecting a response.
return call(registry_name, item_name, request, response);
}
Status RegistryFactory::call(const std::string& registry_name,
const PluginRequest& request,
PluginResponse& response) {
auto plugin = get().registry(registry_name)->getActive();
return call(registry_name, plugin, request, response);
}
Status RegistryFactory::call(const std::string& registry_name,
const PluginRequest& request) {
PluginResponse response;
return call(registry_name, request, response);
}
Status RegistryFactory::setActive(const std::string& registry_name,
const std::string& item_name) {
WriteLock lock(mutex_);
return registry(registry_name)->setActive(item_name);
}
std::string RegistryFactory::getActive(const std::string& registry_name) const {
return registry(registry_name)->getActive();
}
void RegistryFactory::setUp() {
for (const auto& registry : get().all()) {
registry.second->setUp();
}
}
bool RegistryFactory::exists(const std::string& registry_name,
const std::string& item_name,
bool local) const {
if (!exists(registry_name)) {
return false;
}
// Check the registry.
return registry(registry_name)->exists(item_name, local);
}
std::vector<std::string> RegistryFactory::names() const {
std::vector<std::string> names;
for (const auto& registry : all()) {
names.push_back(registry.second->getName());
}
return names;
}
std::vector<std::string> RegistryFactory::names(
const std::string& registry_name) const {
if (registries_.at(registry_name) == nullptr) {
std::vector<std::string> names;
return names;
}
return registry(registry_name)->names();
}
std::vector<RouteUUID> RegistryFactory::routeUUIDs() const {
ReadLock lock(mutex_);
std::vector<RouteUUID> uuids;
for (const auto& extension : extensions_) {
uuids.push_back(extension);
}
return uuids;
}
size_t RegistryFactory::count(const std::string& registry_name) const {
if (!exists(registry_name)) {
return 0;
}
return registry(registry_name)->count();
}
} // namespace osquery