|
22 | 22 | #include "event.h" |
23 | 23 | #include "eventdescriptor.h" |
24 | 24 |
|
25 | | -#include <cJSON.h> |
26 | 25 | #include <logger/logger.h> |
27 | 26 | #include <memcached/isotime.h> |
28 | 27 | #include <memcached/server_cookie_iface.h> |
29 | 28 | #include <nlohmann/json.hpp> |
30 | 29 | #include <platform/dirutils.h> |
| 30 | +#include <utilities/json_utilities.h> |
31 | 31 | #include <utilities/logtags.h> |
| 32 | + |
32 | 33 | #include <algorithm> |
33 | 34 | #include <chrono> |
34 | 35 | #include <cstring> |
@@ -113,88 +114,66 @@ void AuditImpl::create_audit_event(uint32_t event_id, nlohmann::json& payload) { |
113 | 114 | "Audit::create_audit_event: Invalid event identifier specified"); |
114 | 115 | } |
115 | 116 |
|
116 | | -bool AuditImpl::add_event_descriptor(cJSON* event_ptr) { |
117 | | - if (event_ptr == nullptr) { |
118 | | - LOG_WARNING( |
119 | | - "Audit::add_event_descriptor: No JSON data " |
120 | | - "provided"); |
121 | | - return false; |
122 | | - } |
123 | | - |
| 117 | +bool AuditImpl::add_event_descriptor(const nlohmann::json& json) { |
124 | 118 | try { |
125 | | - auto entry = std::make_unique<EventDescriptor>(event_ptr); |
| 119 | + auto entry = std::make_unique<EventDescriptor>(json); |
126 | 120 | events.insert(std::pair<uint32_t, std::unique_ptr<EventDescriptor>>( |
127 | 121 | entry->getId(), std::move(entry))); |
128 | 122 | return true; |
129 | 123 | } catch (const std::bad_alloc&) { |
130 | 124 | LOG_WARNING( |
131 | 125 | "Audit::add_event_descriptor: Failed to allocate " |
132 | 126 | "memory"); |
133 | | - } catch (const std::logic_error& le) { |
134 | | - LOG_WARNING(R"(Audit::add_event_descriptor: JSON key "{}" error)", |
135 | | - le.what()); |
| 127 | + } catch (const nlohmann::json::exception& e) { |
| 128 | + LOG_WARNING( |
| 129 | + "Audit::add_event_descriptor: JSON parsing exception {}" |
| 130 | + " for event {}", |
| 131 | + cb::UserDataView(e.what()), |
| 132 | + cb::UserDataView(json.dump())); |
| 133 | + } catch (const std::invalid_argument& e) { |
| 134 | + LOG_WARNING( |
| 135 | + "Audit::add_event_descriptor: parsing exception {}" |
| 136 | + " for event {}", |
| 137 | + cb::UserDataView(e.what()), |
| 138 | + cb::UserDataView(json.dump())); |
136 | 139 | } |
137 | 140 |
|
138 | 141 | return false; |
139 | 142 | } |
140 | 143 |
|
141 | | -bool AuditImpl::process_module_data_structures(cJSON* module) { |
142 | | - if (module == NULL) { |
143 | | - LOG_WARNING( |
144 | | - "Audit::process_module_data_structures: No JSON data provided " |
145 | | - "for module"); |
146 | | - return false; |
147 | | - } |
148 | | - while (module != NULL) { |
149 | | - cJSON *mod_ptr = module->child; |
150 | | - if (mod_ptr == NULL) { |
151 | | - LOG_WARNING( |
152 | | - "Audit::process_module_data_structures: No JSON data " |
153 | | - "provided for child node"); |
| 144 | +bool AuditImpl::process_module_data_structures(const nlohmann::json& json) { |
| 145 | + for (const auto& event : json) { |
| 146 | + if (!add_event_descriptor(event)) { |
154 | 147 | return false; |
155 | 148 | } |
156 | | - while (mod_ptr != NULL) { |
157 | | - cJSON *event_ptr; |
158 | | - switch (mod_ptr->type) { |
159 | | - case cJSON_Number: |
160 | | - case cJSON_String: |
161 | | - break; |
162 | | - case cJSON_Array: |
163 | | - event_ptr = mod_ptr->child; |
164 | | - while (event_ptr != NULL) { |
165 | | - if (!add_event_descriptor(event_ptr)) { |
166 | | - return false; |
167 | | - } |
168 | | - event_ptr = event_ptr->next; |
169 | | - } |
170 | | - break; |
171 | | - default: |
172 | | - LOG_WARNING("Audit: JSON unknown field error"); |
173 | | - return false; |
174 | | - } |
175 | | - mod_ptr = mod_ptr->next; |
176 | | - } |
177 | | - module = module->next; |
178 | 149 | } |
179 | 150 | return true; |
180 | 151 | } |
181 | 152 |
|
182 | | -bool AuditImpl::process_module_descriptor(cJSON* module_descriptor) { |
| 153 | +bool AuditImpl::process_module_descriptor(const nlohmann::json& json) { |
183 | 154 | events.clear(); |
184 | | - while(module_descriptor != NULL) { |
185 | | - switch (module_descriptor->type) { |
186 | | - case cJSON_Number: |
187 | | - break; |
188 | | - case cJSON_Array: |
189 | | - if (!process_module_data_structures(module_descriptor->child)) { |
190 | | - return false; |
191 | | - } |
192 | | - break; |
193 | | - default: |
194 | | - LOG_WARNING("Audit: JSON unknown field error"); |
| 155 | + for (const auto& module_descriptor : json) { |
| 156 | + auto events = cb::getOptionalJsonObject(module_descriptor, "events"); |
| 157 | + if (!events.is_initialized()) { |
| 158 | + LOG_WARNING( |
| 159 | + "Audit::process_module_descriptor: \"events\"" |
| 160 | + " field missing"); |
| 161 | + return false; |
| 162 | + } |
| 163 | + switch (events->type()) { |
| 164 | + case nlohmann::json::value_t::number_integer: |
| 165 | + break; |
| 166 | + case nlohmann::json::value_t::array: |
| 167 | + if (!process_module_data_structures(*events)) { |
195 | 168 | return false; |
| 169 | + } |
| 170 | + break; |
| 171 | + default: |
| 172 | + LOG_WARNING( |
| 173 | + "Audit:process_module_descriptor \"events\" field is not" |
| 174 | + " integer or array"); |
| 175 | + return false; |
196 | 176 | } |
197 | | - module_descriptor = module_descriptor->next; |
198 | 177 | } |
199 | 178 | return true; |
200 | 179 | } |
@@ -253,20 +232,25 @@ bool AuditImpl::configure() { |
253 | 232 | if (str.empty()) { |
254 | 233 | return false; |
255 | 234 | } |
256 | | - cJSON *json_ptr = cJSON_Parse(str.c_str()); |
257 | | - if (json_ptr == NULL) { |
| 235 | + |
| 236 | + nlohmann::json events_json; |
| 237 | + try { |
| 238 | + events_json = nlohmann::json::parse(str); |
| 239 | + } catch (const nlohmann::json::exception& e) { |
258 | 240 | LOG_WARNING( |
259 | | - R"(Audit::configure: JSON parsing error of "{}" with the content: "{}")", |
| 241 | + R"(Audit::configure: JSON parsing error of "{}" with the |
| 242 | + content: "{}", e.what(): {})", |
260 | 243 | audit_events_file, |
261 | | - cb::UserDataView(str)); |
| 244 | + cb::UserDataView(str), |
| 245 | + cb::UserDataView(e.what())); |
262 | 246 | return false; |
263 | 247 | } |
264 | | - if (!process_module_descriptor(json_ptr->child)) { |
265 | | - cJSON_Delete(json_ptr); |
| 248 | + |
| 249 | + auto modules = cb::getOptionalJsonObject( |
| 250 | + events_json, "modules", nlohmann::json::value_t::array); |
| 251 | + if (!modules.is_initialized() || !process_module_descriptor(*modules)) { |
266 | 252 | return false; |
267 | 253 | } |
268 | | - cJSON_Delete(json_ptr); |
269 | | - |
270 | 254 | auditfile.reconfigure(config); |
271 | 255 |
|
272 | 256 | // iterate through the events map and update the sync and enabled flags |
|
0 commit comments