From 17d8eea3bcd99f9045c1967c6d25683a15b7157f Mon Sep 17 00:00:00 2001 From: Mark Stemm Date: Tue, 1 Feb 2022 15:51:48 -0800 Subject: [PATCH] Add notion of falco application object/cmdline opts skeleton Add a notion of a falco application object. Eventually this will replace the bulk of falco_init and contain methods to: - Parse/validate command line options - Parse/validate falco config - Initialize prerequsites (inspector, falco engine, webserver, etc) - Load plugins - Load/validate rules - Command/subcommand execution (e.g. --list/--list-fields, or nothing specified to run "main" loop) For now, it is only responsible for command line options handling, which is stubbed out. Currently, the only public methods are init() to initialize everything and copts() to access command line options. Command line options are held in a different class falco::app::cmdline_opts. application::copts() returns a reference to that object, which allows access to parsed command line options bound to various public instance variables. Signed-off-by: Mark Stemm --- userspace/falco/CMakeLists.txt | 3 ++ userspace/falco/application.cpp | 61 +++++++++++++++++++++++++++++++++ userspace/falco/application.h | 53 ++++++++++++++++++++++++++++ userspace/falco/falco.cpp | 11 ++++++ 4 files changed, 128 insertions(+) create mode 100644 userspace/falco/application.cpp create mode 100644 userspace/falco/application.h diff --git a/userspace/falco/CMakeLists.txt b/userspace/falco/CMakeLists.txt index 86c35734..83584acb 100644 --- a/userspace/falco/CMakeLists.txt +++ b/userspace/falco/CMakeLists.txt @@ -15,6 +15,7 @@ configure_file(config_falco.h.in config_falco.h) set( FALCO_SOURCES + application.cpp configuration.cpp logger.cpp falco_outputs.cpp @@ -33,6 +34,7 @@ set( "${PROJECT_BINARY_DIR}/userspace/falco" "${PROJECT_BINARY_DIR}/driver/src" "${STRING_VIEW_LITE_INCLUDE}" + "${CXXOPTS_INCLUDE_DIR}" "${YAMLCPP_INCLUDE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}" "${DRAIOS_DEPENDENCIES_DIR}/yaml-${DRAIOS_YAML_VERSION}/target/include" @@ -46,6 +48,7 @@ set( luajit lpeg lyaml + cxxopts ) set( diff --git a/userspace/falco/application.cpp b/userspace/falco/application.cpp new file mode 100644 index 00000000..d9750a20 --- /dev/null +++ b/userspace/falco/application.cpp @@ -0,0 +1,61 @@ +/* +Copyright (C) 2022 The Falco Authors. + +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. +*/ + +// The falco "app" holds application-level configuration and contains +// the implementation of any subcommand-like behaviors like --list, -i +// (print_ignored_events), etc. + +// It also contains the code to initialize components like the +// inspector, falco engine, etc. + +#include "application.h" +#include "falco_common.h" + +namespace falco { +namespace app { + +application::application() + : m_initialized(false) +{ +} + +application::~application() +{ +} + +cmdline_options &application::options() +{ + if(!m_initialized) + { + throw falco_exception("App init() not called yet"); + } + + return m_cmdline_options; +} + +bool application::init(int argc, char **argv, std::string &errstr) +{ + if(!m_cmdline_options.parse(argc, argv, errstr)) + { + return false; + } + + m_initialized = true; + return true; +} + +}; // namespace app +}; // namespace falco diff --git a/userspace/falco/application.h b/userspace/falco/application.h new file mode 100644 index 00000000..b4e3ede9 --- /dev/null +++ b/userspace/falco/application.h @@ -0,0 +1,53 @@ +/* +Copyright (C) 2022 The Falco Authors. + +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. +*/ + +// The falco "app" will eventually replace the monolithic code in +// falco.cpp. We expect it will be responsible for the following: +// - Parsing/validating command line options +// - Parsing/validing falco config +// - Initialize prerequsites (inspector, falco engine, webserver, etc) +// - Loading plugins +// - Loading/validating rules +// - Command/subcommand execution (e.g. --list/--list-fields, or +// nothing specified to run "main" loop) + +// For now, it is only responsible for command line options. +#pragma once + +#include "app_cmdline_options.h" + +#include + +namespace falco { +namespace app { + +class application { +public: + + application(); + virtual ~application(); + + cmdline_options &options(); + bool init(int argc, char **argv, std::string &errstr); + +private: + + cmdline_options m_cmdline_options; + bool m_initialized; +}; + +}; // namespace app +}; // namespace falco diff --git a/userspace/falco/falco.cpp b/userspace/falco/falco.cpp index 4e9e288d..f1f3d34a 100644 --- a/userspace/falco/falco.cpp +++ b/userspace/falco/falco.cpp @@ -37,6 +37,7 @@ limitations under the License. #include #include +#include "application.h" #include "logger.h" #include "utils.h" #include "fields_info.h" @@ -507,6 +508,8 @@ static void list_source_fields(falco_engine *engine, bool verbose, bool names_on // int falco_init(int argc, char **argv) { + falco::application app; + int result = EXIT_SUCCESS; sinsp* inspector = NULL; sinsp_evt::param_fmt event_buffer_format = sinsp_evt::PF_NORMAL; @@ -608,6 +611,14 @@ int falco_init(int argc, char **argv) set disabled_rule_tags; set enabled_rule_tags; + std::string errstr; + bool successful = app.init(argc, argv, errstr); + + if(!successful) + { + throw falco_exception(string("Could not initialize: ") + errstr); + } + // // Parse the args //