Skip to content

Commit

Permalink
[irods#95] Migrate from rodsLog to irods::experimental::log
Browse files Browse the repository at this point in the history
  • Loading branch information
SwooshyCueb committed Nov 7, 2022
1 parent c4259b8 commit 36f4014
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 55 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ find_package(nlohmann_json "3.6.1" REQUIRED)

add_library(${PLUGIN} MODULE ${CMAKE_SOURCE_DIR}/libirods_rule_engine_plugin-audit_amqp.cpp)
target_compile_options(${PLUGIN} PRIVATE -Wno-write-strings)
target_compile_definitions(${PLUGIN} PRIVATE ${IRODS_COMPILE_DEFINITIONS} ${IRODS_COMPILE_DEFINITIONS_PRIVATE})
target_compile_definitions(${PLUGIN} PRIVATE ${IRODS_COMPILE_DEFINITIONS} ${IRODS_COMPILE_DEFINITIONS_PRIVATE} IRODS_ENABLE_SYSLOG)
target_include_directories(
${PLUGIN}
PRIVATE
${IRODS_INCLUDE_DIRS}
${IRODS_EXTERNALS_FULLPATH_ARCHIVE}/include
${IRODS_EXTERNALS_FULLPATH_BOOST}/include
${IRODS_EXTERNALS_FULLPATH_FMT}/include
${IRODS_EXTERNALS_FULLPATH_SPDLOG}/include
${IRODS_EXTERNALS_FULLPATH_QPID_PROTON}/include
)

Expand Down
147 changes: 93 additions & 54 deletions libirods_rule_engine_plugin-audit_amqp.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// irods includes
#include <irods/irods_logger.hpp>
#include <irods/irods_re_plugin.hpp>
#include <irods/irods_re_serialization.hpp>
#include <irods/irods_server_properties.hpp>
Expand Down Expand Up @@ -38,7 +39,6 @@
#include <cstdint>
#include <version>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <chrono>
Expand All @@ -61,6 +61,10 @@ namespace
std::mutex audit_plugin_mutex;
// NOLINTEND(cert-err58-cpp, cppcoreguidelines-avoid-non-const-global-variables)

const char* const rule_engine_name = "audit_amqp";

using log_re = irods::experimental::log::rule_engine;

#if __cpp_lib_chrono >= 201907
// we use millisecond precision in our timestamps, so we want to use a clock
// that does not implement leap seconds as repeated non-leap seconds, if we can.
Expand Down Expand Up @@ -117,8 +121,27 @@ namespace
bool _message_sent;
}; // class send_handler

BOOST_FORCEINLINE void
insert_as_string_or_base64(nlohmann::json& json_obj, const std::string& key, const std::string& val)
template <class T>
BOOST_FORCEINLINE void log_exception(
const T& exception,
const std::string& log_message,
const irods::experimental::log::key_value& context_info)
{
// clang-format off
log_re::info({
{"rule_engine_plugin", rule_engine_name},
{"log_message", log_message},
context_info,
{"exception", exception.what()},
});
// clang-format on
}

BOOST_FORCEINLINE void insert_as_string_or_base64(
nlohmann::json& json_obj,
const std::string& key,
const std::string& val,
const std::uint64_t& time_ms)
{
try {
json_obj[key] = nlohmann::json::parse("\"" + val + "\"");
Expand All @@ -136,13 +159,15 @@ namespace

json_obj[key_b64] = val_b64;

irods::log(
LOG_DEBUG,
fmt::format(
"[AUDIT] - Message with timestamp:[{}] had invalid UTF-8 in key:[{}] and was stored as base64 in key:[{}].",
json_obj.at("@timestamp").get_ref<const std::uint64_t&>(),
key,
key_b64)); // irods::log
// clang-format off
log_re::debug({
{"rule_engine_plugin", rule_engine_name},
{"log_message", "Invalid UTF-8 encountered when adding element to message; added as base64"},
{"element_original_key", key},
{"element_key", key_b64},
{"message_timestamp", std::to_string(time_ms)},
});
// clang-format on
}
}

Expand Down Expand Up @@ -178,12 +203,13 @@ namespace
}
}
else {
rodsLog(
LOG_DEBUG,
"%s - using default configuration: regex - %s, topic - %s, location - %s",
audit_pep_regex_to_match.c_str(),
audit_amqp_topic.c_str(),
audit_amqp_location.c_str());
// clang-format off
log_re::debug({
{"rule_engine_plugin", rule_engine_name},
{"log_message", "Using default plugin configuration"},
{"instance_name", _instance_name},
});
// clang-format on
}

return SUCCESS();
Expand All @@ -194,19 +220,16 @@ namespace
return ERROR(KEY_NOT_FOUND, e.what());
}
catch (const nlohmann::json::exception& e) {
return ERROR(SYS_LIBRARY_ERROR, fmt::format("[{}:{}] - [{}]", __func__, __LINE__, e.what()));
return ERROR(SYS_LIBRARY_ERROR, e.what());
}
catch (const std::exception& e) {
return ERROR(SYS_INTERNAL_ERR, fmt::format("[{}:{}] - [{}]", __func__, __LINE__, e.what()));
return ERROR(SYS_INTERNAL_ERR, e.what());
}
catch (...) {
return ERROR(SYS_UNKNOWN_ERROR, fmt::format("[{}:{}] - an unknown error occurred", __func__, __LINE__));
return ERROR(SYS_UNKNOWN_ERROR, "an unknown error occurred");
}

std::stringstream msg;
msg << "failed to find configuration for audit_amqp plugin [" << _instance_name << "]";
rodsLog(LOG_ERROR, "%s", msg.str().c_str());
return ERROR(SYS_INVALID_INPUT_PARAM, msg.str());
return ERROR(SYS_INVALID_INPUT_PARAM, "failed to find plugin configuration");
}

auto start([[maybe_unused]] irods::default_re_ctx& _re_ctx, const std::string& _instance_name) -> irods::error
Expand All @@ -215,7 +238,14 @@ namespace

irods::error ret = get_re_configs(_instance_name);
if (!ret.ok()) {
irods::log(PASS(ret));
// clang-format off
log_re::error({
{"rule_engine_plugin", rule_engine_name},
{"log_message", "Error loading plugin configuration"},
{"instance_name", _instance_name},
{"error_result", ret.result()},
});
// clang-format on
}

nlohmann::json json_obj;
Expand All @@ -242,20 +272,19 @@ namespace
}
}
catch (const irods::exception& e) {
rodsLog(LOG_ERROR, e.client_display_what());
return ERROR(e.code(), fmt::format("[{}:{}] - [{}]", __func__, __LINE__, e.what()));
log_exception(e, "Caught iRODS exception", {"instance_name", _instance_name});
return ERROR(e.code(), e.what());
}
catch (const nlohmann::json::exception& e) {
const std::string msg = fmt::format("[{}:{}] - [{}]", __func__, __LINE__, e.what());
rodsLog(LOG_ERROR, msg.data());
return ERROR(SYS_LIBRARY_ERROR, msg);
log_exception(e, "Caught nlohmann-json exception", {"instance_name", _instance_name});
return ERROR(SYS_LIBRARY_ERROR, e.what());
}
catch (const std::exception& e) {
rodsLog(LOG_ERROR, e.what());
return ERROR(SYS_INTERNAL_ERR, fmt::format("[{}:{}] - [{}]", __func__, __LINE__, e.what()));
log_exception(e, "Caught exception", {"instance_name", _instance_name});
return ERROR(SYS_INTERNAL_ERR, e.what());
}
catch (...) {
return ERROR(SYS_UNKNOWN_ERROR, fmt::format("[{}:{}] - unknown error occurred", __func__, __LINE__));
return ERROR(SYS_UNKNOWN_ERROR, "an unknown error occurred");
}

msg_str = json_obj.dump();
Expand All @@ -270,8 +299,7 @@ namespace
return SUCCESS();
}

auto stop([[maybe_unused]] irods::default_re_ctx& _re_ctx, [[maybe_unused]] const std::string& _instance_name)
-> irods::error
auto stop([[maybe_unused]] irods::default_re_ctx& _re_ctx, const std::string& _instance_name) -> irods::error
{
std::lock_guard<std::mutex> lock(audit_plugin_mutex);

Expand All @@ -298,20 +326,19 @@ namespace
}
}
catch (const irods::exception& e) {
rodsLog(LOG_ERROR, e.client_display_what());
return ERROR(e.code(), fmt::format("[{}:{}] - [{}]", __func__, __LINE__, e.what()));
log_exception(e, "Caught iRODS exception", {"instance_name", _instance_name});
return ERROR(e.code(), e.what());
}
catch (const nlohmann::json::exception& e) {
const std::string msg = fmt::format("[{}:{}] - [{}]", __func__, __LINE__, e.what());
rodsLog(LOG_ERROR, msg.data());
return ERROR(SYS_LIBRARY_ERROR, msg);
log_exception(e, "Caught nlohmann-json exception", {"instance_name", _instance_name});
return ERROR(SYS_LIBRARY_ERROR, e.what());
}
catch (const std::exception& e) {
rodsLog(LOG_ERROR, e.what());
return ERROR(SYS_INTERNAL_ERR, fmt::format("[{}:{}] - [{}]", __func__, __LINE__, e.what()));
log_exception(e, "Caught exception", {"instance_name", _instance_name});
return ERROR(SYS_INTERNAL_ERR, e.what());
}
catch (...) {
return ERROR(SYS_UNKNOWN_ERROR, fmt::format("[{}:{}] - unknown error occurred", __func__, __LINE__));
return ERROR(SYS_UNKNOWN_ERROR, "an unknown error occurred");
}

msg_str = json_obj.dump();
Expand Down Expand Up @@ -386,15 +413,28 @@ namespace
// the entirety of the contents of files. These could be very big and cause the
// message broker to explode.
if (std::type_index(typeid(BytesBuf*)) == std::type_index(itr.type())) {
rodsLog(LOG_DEBUG9, "[{}:{}] - skipping serialization of BytesBuf parameter", __FILE__, __LINE__);
// clang-format off
log_re::trace({
{"rule_engine_plugin", rule_engine_name},
{"log_message", "skipping serialization of BytesBuf parameter"},
{"rule_name", _rn},
});
// clang-format on
continue;
}

// serialize the parameter to a map
irods::re_serialization::serialized_parameter_t param;
irods::error ret = irods::re_serialization::serialize_parameter(itr, param);
if (!ret.ok()) {
rodsLog(LOG_ERROR, "unsupported argument for calling re rules from the rule language");
// clang-format off
log_re::error({
{"rule_engine_plugin", rule_engine_name},
{"log_message", "failed to serialize argument"},
{"rule_name", _rn},
{"error_result", ret.result()},
});
// clang-format on
continue;
}

Expand All @@ -414,29 +454,28 @@ namespace

if (ctr > 1) {
const std::string key = fmt::format(FMT_COMPILE("{0:s}__{1:d}"), arg, ctr);
insert_as_string_or_base64(json_obj, key, elem.second);
insert_as_string_or_base64(json_obj, key, elem.second, time_ms);
}
else {
insert_as_string_or_base64(json_obj, arg, elem.second);
insert_as_string_or_base64(json_obj, arg, elem.second, time_ms);
}
}
}
}
catch (const irods::exception& e) {
rodsLog(LOG_ERROR, e.client_display_what());
return ERROR(e.code(), fmt::format("[{}:{}] - [{}]", __func__, __LINE__, e.what()));
log_exception(e, "Caught iRODS exception", {"rule_name", _rn});
return ERROR(e.code(), e.what());
}
catch (const nlohmann::json::exception& e) {
const std::string msg = fmt::format("[{}:{}] - [{}]", __func__, __LINE__, e.what());
rodsLog(LOG_ERROR, msg.data());
return ERROR(SYS_LIBRARY_ERROR, msg);
log_exception(e, "Caught nlohmann-json exception", {"rule_name", _rn});
return ERROR(SYS_LIBRARY_ERROR, e.what());
}
catch (const std::exception& e) {
rodsLog(LOG_ERROR, e.what());
return ERROR(SYS_INTERNAL_ERR, fmt::format("[{}:{}] - [{}]", __func__, __LINE__, e.what()));
log_exception(e, "Caught exception", {"rule_name", _rn});
return ERROR(SYS_INTERNAL_ERR, e.what());
}
catch (...) {
return ERROR(SYS_UNKNOWN_ERROR, fmt::format("[{}:{}] - unknown error occurred", __func__, __LINE__));
return ERROR(SYS_UNKNOWN_ERROR, "an unknown error occurred");
}

msg_str = json_obj.dump();
Expand Down

0 comments on commit 36f4014

Please sign in to comment.