update(userspace/engine): address jasondellaluce comments for maintainability

Signed-off-by: Lorenzo Susini <susinilorenzo1@gmail.com>
This commit is contained in:
Lorenzo Susini
2023-09-27 09:22:38 +00:00
committed by poiana
parent 9bbf9716b6
commit 1326ca356e
9 changed files with 70 additions and 33 deletions

View File

@@ -97,7 +97,10 @@ jobs:
- name: Check Engine version
run: |
base_hash=$(grep CHECKSUM "./userspace/engine/falco_engine_version.h" | awk '{print $3}' | sed -e 's/"//g')
base_engine_ver=$(grep ENGINE_VERSION "./userspace/engine/falco_engine_version.h" | awk '{print $3}' | sed -e 's/(//g' -e 's/)//g')
base_engine_ver_major=$(grep ENGINE_VERSION_MAJOR "./userspace/engine/falco_engine_version.h" | head -n 1 | awk '{print $3}' | sed -e 's/(//g' -e 's/)//g')
base_engine_ver_minor=$(grep ENGINE_VERSION_MINOR "./userspace/engine/falco_engine_version.h" | head -n 1 | awk '{print $3}' | sed -e 's/(//g' -e 's/)//g')
base_engine_ver_patch=$(grep ENGINE_VERSION_PATCH "./userspace/engine/falco_engine_version.h" | head -n 1 | awk '{print $3}' | sed -e 's/(//g' -e 's/)//g')
base_engine_ver="${base_engine_ver_major}.${base_engine_ver_minor}.${base_engine_ver_patch}"
cur_hash=$(echo "${{ needs.build-dev.outputs.cmdout }}" | cut -d ' ' -f 2)
cur_engine_ver=$(echo "${{ needs.build-dev.outputs.cmdout }}" | cut -d ' ' -f 1)

View File

@@ -75,9 +75,9 @@ falco_engine::~falco_engine()
m_sources.clear();
}
std::string falco_engine::engine_version()
sinsp_version falco_engine::engine_version()
{
return FALCO_ENGINE_VERSION;
return sinsp_version(FALCO_ENGINE_VERSION);
}
const falco_source* falco_engine::find_source(const std::string& name) const
@@ -567,7 +567,7 @@ void falco_engine::describe_rule(std::string *rule, const std::vector<std::share
// Store required engine version
auto required_engine_version = m_rule_collector.required_engine_version();
output["required_engine_version"] = required_engine_version.version;
output["required_engine_version"] = required_engine_version.version.as_string();
// Store required plugin versions
Json::Value plugin_versions = Json::arrayValue;
@@ -1007,14 +1007,14 @@ static bool check_plugin_requirement_alternatives(
{
sinsp_version req_version(req.version);
sinsp_version plugin_version(plugin.version);
if(!plugin_version.m_valid)
if(!plugin_version.is_valid())
{
err = "Plugin '" + plugin.name
+ "' has invalid version string '"
+ plugin.version + "'";
return false;
}
if (!plugin_version.check(req_version))
if (!plugin_version.compatible_with(req_version))
{
err = "Plugin '" + plugin.name
+ "' version '" + plugin.version

View File

@@ -39,6 +39,7 @@ limitations under the License.
#include "falco_source.h"
#include "falco_load_result.h"
#include "filter_details_resolver.h"
#include "rule_loader_reader.h"
//
// This class acts as the primary interface between a program and the
@@ -56,7 +57,16 @@ public:
// and rules file format it supports. This version will change
// any time the code that handles rules files, expression
// fields, etc, changes.
static std::string engine_version();
static sinsp_version engine_version();
// Engine version used to be represented as a simple progressive
// number. With the new semver schema, the number now represents
// the semver minor number. This function converts the legacy version
// number to the new semver schema.
static inline sinsp_version get_implicit_version(uint32_t minor)
{
return rule_loader::reader::get_implicit_engine_version(minor);
}
// Print to stdout (using printf) a description of each field supported by this engine.
// If source is non-empty, only fields for the provided source are printed.

View File

@@ -15,12 +15,18 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// The version of this Falco engine.
#define FALCO_ENGINE_VERSION "0.26.0"
#define __FALCO_ENGINE_STRINGIFY1(str) #str
#define __FALCO_ENGINE_STRINGIFY(str) __FALCO_ENGINE_STRINGIFY1(str)
// Default version values assumed when parsing numeric required_engine_version
#define FALCO_DEFAULT_ENGINE_MAJOR 0
#define FALCO_DEFAULT_ENGINE_PATCH 0
// The version of this Falco engine
#define FALCO_ENGINE_VERSION_MAJOR 0
#define FALCO_ENGINE_VERSION_MINOR 26
#define FALCO_ENGINE_VERSION_PATCH 0
#define FALCO_ENGINE_VERSION \
__FALCO_ENGINE_STRINGIFY(FALCO_ENGINE_VERSION_MAJOR) "." \
__FALCO_ENGINE_STRINGIFY(FALCO_ENGINE_VERSION_MINOR) "." \
__FALCO_ENGINE_STRINGIFY(FALCO_ENGINE_VERSION_PATCH)
// This is the result of running the following command:
// FALCO="falco -c ./falco.yaml"

View File

@@ -24,6 +24,7 @@ limitations under the License.
#include "falco_source.h"
#include "falco_load_result.h"
#include "indexed_vector.h"
#include "version.h"
namespace rule_loader
{
@@ -298,7 +299,7 @@ namespace rule_loader
*/
struct engine_version_info
{
engine_version_info() : ctx("no-filename-given") { };
engine_version_info() : ctx("no-filename-given"), version("0.0.0") { };
engine_version_info(context &ctx);
~engine_version_info() = default;
engine_version_info(engine_version_info&&) = default;
@@ -307,7 +308,7 @@ namespace rule_loader
engine_version_info& operator = (const engine_version_info&) = default;
context ctx;
std::string version;
sinsp_version version;
};
/*!

View File

@@ -145,19 +145,13 @@ const indexed_vector<rule_loader::rule_info>& rule_loader::collector::rules() co
void rule_loader::collector::define(configuration& cfg, engine_version_info& info)
{
auto engine_version = sinsp_version(falco_engine::engine_version());
sinsp_version required_engine_version(info.version);
THROW(!required_engine_version.m_valid, "Unable to parse " + info.version
+ " as a semver string. Expected \"x.y.z\" semver format.", info.ctx);
THROW(!engine_version.check(required_engine_version), "Rules require engine version "
+ required_engine_version.as_string() + " but engine version is "
+ engine_version.as_string(), info.ctx);
sinsp_version current_required_engine_version(m_required_engine_version.version);
auto v = falco_engine::engine_version();
THROW(!v.compatible_with(info.version), "Rules require engine version "
+ info.version.as_string() + ", but engine version is " + v.as_string(),
info.ctx);
// Store max required_engine_version
if(current_required_engine_version.check(required_engine_version))
if(m_required_engine_version.version < info.version)
{
m_required_engine_version = info;
}
@@ -169,7 +163,7 @@ void rule_loader::collector::define(configuration& cfg, plugin_version_info& inf
for (const auto& req : info.alternatives)
{
sinsp_version plugin_version(req.version);
THROW(!plugin_version.m_valid,
THROW(!plugin_version.is_valid(),
"Invalid required version '" + req.version
+ "' for plugin '" + req.name + "'",
info.ctx);

View File

@@ -20,6 +20,7 @@ limitations under the License.
#include "rule_loader_reader.h"
#include "falco_engine_version.h"
#include "logger.h"
#define THROW(cond, err, ctx) { if ((cond)) { throw rule_loader::rule_load_exception(falco::load_result::LOAD_ERR_YAML_VALIDATE, (err), (ctx)); } }
@@ -264,12 +265,17 @@ static void read_item(
decode_val(item, "required_engine_version", ver, ctx);
// Build proper semver representation
v.version = std::to_string(FALCO_DEFAULT_ENGINE_MAJOR) + "." + std::to_string(ver) + "." + std::to_string(FALCO_DEFAULT_ENGINE_PATCH);
v.version = rule_loader::reader::get_implicit_engine_version(ver);
}
catch(std::exception& e)
{
// Convert to string
decode_val(item, "required_engine_version", v.version, ctx);
std::string ver;
decode_val(item, "required_engine_version", ver, ctx);
v.version = sinsp_version(ver);
THROW(!v.version.is_valid(), "Unable to parse engine version '" + ver + "' as a semver string. Expected \"x.y.z\" semver format.", ctx);
}
collector.define(cfg, v);

View File

@@ -19,6 +19,9 @@ limitations under the License.
#include "rule_loader.h"
#include "rule_loader_collector.h"
#include "logger.h"
#include "version.h"
#include "falco_engine_version.h"
namespace rule_loader
{
@@ -41,6 +44,19 @@ public:
thew new definitions
*/
virtual bool read(configuration& cfg, collector& loader);
/*!
\brief Engine version used to be represented as a simple progressive
number. With the new semver schema, the number now represents
the semver minor number. This function converts the legacy version
number to the new semver schema.
*/
static inline sinsp_version get_implicit_engine_version(uint32_t minor)
{
return sinsp_version(std::to_string(FALCO_ENGINE_VERSION_MAJOR) + "."
+ std::to_string(minor) + "."
+ std::to_string(FALCO_ENGINE_VERSION_PATCH));
}
};
}; // namespace rule_loader

View File

@@ -16,6 +16,7 @@ limitations under the License.
*/
#include "config_falco.h"
#include "falco_engine.h"
#include "falco_engine_version.h"
#include "grpc_server_impl.h"
#include "grpc_queue.h"
@@ -79,10 +80,10 @@ void falco::grpc::server_impl::version(const context& ctx, const version::reques
res.set_engine_version(FALCO_ENGINE_VERSION);
res.set_engine_fields_checksum(FALCO_ENGINE_CHECKSUM);
auto engine_version = sinsp_version(FALCO_ENGINE_VERSION);
res.set_engine_major(engine_version.m_version_major);
res.set_engine_minor(engine_version.m_version_minor);
res.set_engine_patch(engine_version.m_version_patch);
auto engine_version = falco_engine::engine_version();
res.set_engine_major(engine_version.major());
res.set_engine_minor(engine_version.minor());
res.set_engine_patch(engine_version.patch());
res.set_major(FALCO_VERSION_MAJOR);
res.set_minor(FALCO_VERSION_MINOR);