forked from netboxlabs/pktvisor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaps.cpp
More file actions
241 lines (211 loc) · 8.96 KB
/
Copy pathTaps.cpp
File metadata and controls
241 lines (211 loc) · 8.96 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "Taps.h"
#include "CoreRegistry.h"
#include "InputStream.h"
#include "Policies.h"
#include <algorithm>
#include <fmt/core.h>
#include <spdlog/spdlog.h>
namespace visor {
std::vector<Tap *> TapManager::load_from_str(const std::string &str)
{
if (str.empty()) {
throw TapException("empty data");
}
YAML::Node node = YAML::Load(str);
if (!node.IsMap() || !node["visor"]) {
throw TapException("invalid schema");
}
if (!node["version"]) {
spdlog::get("visor")->info("missing version, using version \"1.0\"");
} else if (!node["version"].IsScalar() || node["version"].as<std::string>() != "1.0") {
throw PolicyException("unsupported version");
}
if (node["visor"]["taps"] && node["visor"]["taps"].IsMap()) {
return load(node["visor"]["taps"], true, true);
} else {
throw TapException("no taps found in schema");
}
}
// needs to be thread safe and transactional: any errors mean resources get cleaned up with no side effects
std::vector<Tap *> TapManager::load(const YAML::Node &tap_yaml, bool strict, bool single)
{
assert(tap_yaml.IsMap());
assert(spdlog::get("visor"));
if (single && tap_yaml.size() > 1) {
throw TapException(fmt::format("only a single tap expected but got {}", tap_yaml.size()));
}
std::vector<Tap *> result;
for (YAML::const_iterator it = tap_yaml.begin(); it != tap_yaml.end(); ++it) {
if (!it->first.IsScalar()) {
throw TapException("expecting tap identifier");
}
auto tap_name = it->first.as<std::string>();
spdlog::get("visor")->info("tap [{}]: parsing", tap_name);
if (!it->second.IsMap()) {
throw TapException("expecting tap configuration map");
}
if (!it->second["input_type"] || !it->second["input_type"].IsScalar()) {
throw TapException("missing or invalid tap type key 'input_type'");
}
auto input_type = it->second["input_type"].as<std::string>();
auto input_plugin = _registry->input_plugins().find(std::make_pair(input_type, CoreRegistry::DEFAULT_INPUT_PLUGIN_VERSION));
if (input_plugin == _registry->input_plugins().end()) {
if (strict) {
throw TapException(fmt::format("Tap '{}' requires input stream type '{}' which is not available", tap_name, input_type));
} else {
spdlog::get("visor")->warn("Tap '{}' requires input stream type '{}' which is not available; skipping", tap_name, input_type);
continue;
}
}
auto tap_module = std::make_unique<Tap>(tap_name, input_plugin->second.get());
if (it->second["config"]) {
if (!it->second["config"].IsMap()) {
throw TapException("tap configuration is not a map");
}
tap_module->config_set_yaml(it->second["config"]);
}
if (it->second["tags"]) {
if (!it->second["tags"].IsMap()) {
throw TapException("tap tags is not a map");
}
tap_module->tags_set_yaml(it->second["tags"]);
}
// will throw if it already exists. nothing else to clean up
result.push_back(tap_module.get());
module_add(std::move(tap_module));
spdlog::get("visor")->info("tap [{}]: loaded, type {}", tap_name, input_type);
}
return result;
}
std::vector<std::string> TapManager::get_input_taps_name(const YAML::Node &input_node)
{
if (input_node["tap"] && input_node["tap_selector"]) {
throw TapException("input can have only key 'input.tap' or key 'input.tap_selector'");
} else if (!input_node["tap"] && !input_node["tap_selector"]) {
throw TapException("missing key 'input.tap' or key 'input.tap_selector'");
}
std::vector<std::string> taps_name;
if (input_node["tap"]) {
if (!input_node["tap"].IsScalar()) {
throw PolicyException("invalid tap at key 'input.tap'");
}
auto tap_name = input_node["tap"].as<std::string>();
if (!_registry->tap_manager()->module_exists(tap_name)) {
throw TapException(fmt::format("tap '{}' does not exist", tap_name));
}
taps_name.push_back(tap_name);
} else if (input_node["tap_selector"]) {
auto tap_selector = input_node["tap_selector"];
if (!tap_selector.IsMap()) {
throw TapException("'input.tap_selector' is not a map");
}
if (tap_selector["all"] && tap_selector["any"]) {
throw TapException("input can have only key 'input.tap_selector.all' or key 'input.tap_selector.any'");
} else if (!tap_selector["all"] && !tap_selector["any"]) {
throw TapException("missing key 'input.tap_selector.all' or key 'input.tap_selector.any'");
}
std::string binary_op{"all"};
if (tap_selector["all"]) {
if (!tap_selector["all"].IsSequence()) {
throw TapException("'input.tap_selector.all' is not a sequence");
}
} else if (tap_selector["any"]) {
if (!tap_selector["any"].IsSequence()) {
throw TapException("'input.tap_selector.any' is not a sequence");
}
binary_op = "any";
}
auto [tap_modules, hm_lock] = module_get_all_locked();
bool match{false};
for (auto &[name, mod] : tap_modules) {
auto tmod = dynamic_cast<Tap *>(mod.get());
if (tmod && tmod->tags_match_selector_yaml(tap_selector[binary_op], (binary_op == "all"))) {
taps_name.push_back(tmod->name());
match = true;
}
}
if (!match) {
spdlog::get("visor")->info("no tap match found for specified 'input.tap_selector' tags");
throw std::invalid_argument("no tap match found for specified 'input.tap_selector' tags");
}
}
return taps_name;
}
std::string Tap::get_input_name(const Configurable &config, const Configurable &filter)
{
Config c;
c.config_merge(dynamic_cast<const Configurable &>(*this));
c.config_merge(config);
return _input_plugin->generate_input_name(name(), c, filter);
}
std::unique_ptr<InputStream> Tap::instantiate(const Configurable *config, const Configurable *filter, std::string input_name)
{
Config c;
c.config_merge(dynamic_cast<const Configurable &>(*this));
c.config_merge(*config);
auto module = _input_plugin->instantiate(input_name, &c, filter);
return module;
}
bool Tap::tags_match_selector_yaml(const YAML::Node &tag_yaml, bool all)
{
bool any_match = false;
for (YAML::const_iterator it = tag_yaml.begin(); it != tag_yaml.end(); ++it) {
// sequence
if (it->size() > 1) {
throw TapException("selector tag must contain only one key/value pair per sequence index");
}
const auto it_module = it->begin();
const auto &key = it_module->first.as<std::string>();
if (!it_module->first.IsScalar()) {
throw TapException(fmt::format("tag key '{}' have be scalar", key));
}
if (!it_module->second.IsScalar()) {
throw TapException(fmt::format("tag key '{}' must have scalar value", key));
}
if (!_tags->config_exists(key)) {
if (all) {
return false;
} else {
continue;
}
}
// the yaml library doesn't discriminate between scalar types, so we have to do that ourselves
auto value = it_module->second.as<std::string>();
if (std::regex_match(value, std::regex("[0-9]+"))) {
uint64_t tag_value;
try {
tag_value = _tags->config_get<uint64_t>(key);
} catch (ConfigException &e) {
throw TapException(fmt::format("Tap [{}]: tag and selector value types are different for key '{}'", name(), key));
}
if (tag_value == it_module->second.as<uint64_t>()) {
any_match = true;
} else if (all) {
return false;
}
} else if (std::regex_match(value, std::regex("true|false", std::regex_constants::icase))) {
bool tag_value;
try {
tag_value = _tags->config_get<bool>(key);
} catch (ConfigException &e) {
throw TapException(fmt::format("Tap [{}]: tag and selector value types are different for key '{}'", name(), key));
}
if (tag_value == it_module->second.as<bool>()) {
any_match = true;
} else if (all) {
return false;
}
} else {
if (_tags->config_get<std::string>(key) == value) {
any_match = true;
} else if (all) {
return false;
}
}
}
return any_match;
}
}