From 1c3bed135f5daf6ae51d6a0e04c89197196a04ef Mon Sep 17 00:00:00 2001 From: Mark Stemm Date: Mon, 9 Sep 2019 16:45:03 -0700 Subject: [PATCH] Add improved field outputting Support the notion of a message for all fields in a single class, and making sure it's wrapped as well as the other fields. This is used to display a single message about how indexing working for ka.* filter fields and what IDX_ALLOWED/IDX_NUMERIC/IDX_KEY means, rather than repeating the same text over and over in every field. The wrapping is handled by a function falco::utils::wrap_text. Signed-off-by: Mark Stemm --- userspace/engine/CMakeLists.txt | 1 + userspace/engine/falco_engine.cpp | 42 ++++++++++++----------- userspace/engine/falco_utils.cpp | 55 +++++++++++++++++++++++++++++++ userspace/engine/falco_utils.h | 34 +++++++++++++++++++ 4 files changed, 113 insertions(+), 19 deletions(-) create mode 100644 userspace/engine/falco_utils.cpp create mode 100644 userspace/engine/falco_utils.h diff --git a/userspace/engine/CMakeLists.txt b/userspace/engine/CMakeLists.txt index 65028244..bb7852c2 100644 --- a/userspace/engine/CMakeLists.txt +++ b/userspace/engine/CMakeLists.txt @@ -22,6 +22,7 @@ set(FALCO_ENGINE_SOURCE_FILES rules.cpp falco_common.cpp falco_engine.cpp + falco_utils.cpp json_evt.cpp ruleset.cpp token_bucket.cpp diff --git a/userspace/engine/falco_engine.cpp b/userspace/engine/falco_engine.cpp index 6c4bb171..6b5ccbb4 100644 --- a/userspace/engine/falco_engine.cpp +++ b/userspace/engine/falco_engine.cpp @@ -20,6 +20,7 @@ limitations under the License. #include #include "falco_engine.h" +#include "falco_utils.h" #include "falco_engine_version.h" #include "config_falco_engine.h" @@ -91,12 +92,15 @@ void falco_engine::list_fields(bool names_only) { printf("\n----------------------\n"); printf("Field Class: %s (%s)\n\n", chk_field.m_name.c_str(), chk_field.m_desc.c_str()); + if(chk_field.m_class_info != "") + { + std::string str = falco::utils::wrap_text(chk_field.m_class_info, 0, 0, CONSOLE_LINE_LEN); + printf("%s\n", str.c_str()); + } } for(auto &field : chk_field.m_fields) { - uint32_t l, m; - printf("%s", field.m_name.c_str()); if(names_only) @@ -112,29 +116,29 @@ void falco_engine::list_fields(bool names_only) namelen = 0; } - for(l = 0; l < DESCRIPTION_TEXT_START - namelen; l++) + for(uint32_t l = 0; l < DESCRIPTION_TEXT_START - namelen; l++) { printf(" "); } - size_t desclen = field.m_desc.size(); - - for(l = 0; l < desclen; l++) + std::string desc = field.m_desc; + switch(field.m_idx_mode) { - if(l % (CONSOLE_LINE_LEN - DESCRIPTION_TEXT_START) == 0 && l != 0) - { - printf("\n"); + case json_event_filter_check::IDX_REQUIRED: + case json_event_filter_check::IDX_ALLOWED: + desc += " ("; + desc += json_event_filter_check::s_index_mode_strs[field.m_idx_mode]; + desc += ", "; + desc += json_event_filter_check::s_index_type_strs[field.m_idx_type]; + desc += ")"; + break; + case json_event_filter_check::IDX_NONE: + default: + break; + }; - for(m = 0; m < DESCRIPTION_TEXT_START; m++) - { - printf(" "); - } - } - - printf("%c", field.m_desc.at(l)); - } - - printf("\n"); + std::string str = falco::utils::wrap_text(desc, namelen, DESCRIPTION_TEXT_START, CONSOLE_LINE_LEN); + printf("%s\n", str.c_str()); } } } diff --git a/userspace/engine/falco_utils.cpp b/userspace/engine/falco_utils.cpp new file mode 100644 index 00000000..101deab0 --- /dev/null +++ b/userspace/engine/falco_utils.cpp @@ -0,0 +1,55 @@ +/* +Copyright (C) 2016-2018 Draios Inc dba Sysdig. + +This file is part of falco. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +*/ + +#include "falco_utils.h" + +namespace falco +{ + +namespace utils +{ + +std::string wrap_text(const std::string &str, uint32_t initial_pos, uint32_t indent, uint32_t line_len) +{ + std::string ret; + + size_t len = str.size(); + + for(uint32_t l = 0; l < len; l++) + { + if(l % (line_len - indent) == 0 && l != 0) + { + ret += "\n"; + + for(uint32_t m = 0; m < indent; m++) + { + ret += " "; + } + } + ret += str.at(l); + } + + ret += "\n"; + + return ret; +} + +} // namespace utils + +} // namespace falco diff --git a/userspace/engine/falco_utils.h b/userspace/engine/falco_utils.h new file mode 100644 index 00000000..62afd6e8 --- /dev/null +++ b/userspace/engine/falco_utils.h @@ -0,0 +1,34 @@ +/* +Copyright (C) 2016-2018 Draios Inc dba Sysdig. + +This file is part of falco. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +*/ + +#include + +#pragma once + +namespace falco +{ + +namespace utils +{ + +std::string wrap_text(const std::string &str, uint32_t initial_pos, uint32_t indent, uint32_t line_len); + +} // namespace utils + +} // namespace falco