new(userspace/falco): standaline utility for retrieving internal version numbers

Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
This commit is contained in:
Jason Dellaluce 2023-01-13 16:10:01 +00:00 committed by poiana
parent bb9edea666
commit 7724ad940a
3 changed files with 135 additions and 0 deletions

View File

@ -54,6 +54,7 @@ set(
outputs_syslog.cpp
event_drops.cpp
stats_writer.cpp
versions_info.cpp
falco.cpp
)

View File

@ -0,0 +1,80 @@
/*
Copyright (C) 2023 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.
*/
#include "versions_info.h"
#include "config_falco.h"
#include "falco_engine_version.h"
#include <plugin_manager.h>
// todo: move string conversion to scap
static std::string get_driver_api_version(const std::shared_ptr<sinsp>& s)
{
auto driver_api_version = s->get_scap_api_version();
unsigned long driver_api_major = PPM_API_VERSION_MAJOR(driver_api_version);
unsigned long driver_api_minor = PPM_API_VERSION_MINOR(driver_api_version);
unsigned long driver_api_patch = PPM_API_VERSION_PATCH(driver_api_version);
char driver_api_version_string[32];
snprintf(driver_api_version_string, sizeof(driver_api_version_string), "%lu.%lu.%lu", driver_api_major, driver_api_minor, driver_api_patch);
return std::string(driver_api_version_string);
}
// todo: move string conversion to scap
static inline std::string get_driver_schema_version(const std::shared_ptr<sinsp>& s)
{
auto driver_schema_version = s->get_scap_schema_version();
unsigned long driver_schema_major = PPM_API_VERSION_MAJOR(driver_schema_version);
unsigned long driver_schema_minor = PPM_API_VERSION_MINOR(driver_schema_version);
unsigned long driver_schema_patch = PPM_API_VERSION_PATCH(driver_schema_version);
char driver_schema_version_string[32];
snprintf(driver_schema_version_string, sizeof(driver_schema_version_string), "%lu.%lu.%lu", driver_schema_major, driver_schema_minor, driver_schema_patch);
return std::string(driver_schema_version_string);
}
falco::versions_info::versions_info(const std::shared_ptr<sinsp>& inspector)
{
falco_version = FALCO_VERSION;
engine_version = std::to_string(FALCO_ENGINE_VERSION);
libs_version = FALCOSECURITY_LIBS_VERSION;
plugin_api_version = inspector->get_plugin_api_version();
driver_api_version = get_driver_api_version(inspector);
driver_schema_version = get_driver_schema_version(inspector);
default_driver_version = DRIVER_VERSION;
for (const auto &p : inspector->get_plugin_manager()->plugins())
{
plugin_versions[p->name()] = p->plugin_version().as_string();
}
}
nlohmann::json falco::versions_info::as_json() const
{
nlohmann::json version_info;
version_info["falco_version"] = falco_version;
version_info["libs_version"] = libs_version;
version_info["plugin_api_version"] = plugin_api_version;
version_info["driver_api_version"] = driver_api_version;
version_info["driver_schema_version"] = driver_schema_version;
version_info["default_driver_version"] = default_driver_version;
version_info["engine_version"] = engine_version;
for (const auto& pv : plugin_versions)
{
version_info["plugin_versions"][pv.first] = pv.second;
}
return version_info;
}

View File

@ -0,0 +1,54 @@
/*
Copyright (C) 2023 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.
*/
#include <memory>
#include <string>
#include <unordered_map>
#include <sinsp.h>
#include <nlohmann/json.hpp>
namespace falco
{
/**
* @brief Container for the version of Falco components
*/
struct versions_info
{
/**
* @brief Construct a versions info by using an inspector to obtain
* versions about the drivers and the loaded plugins.
*/
versions_info(const std::shared_ptr<sinsp>& inspector);
versions_info(versions_info&&) = default;
versions_info& operator = (versions_info&&) = default;
versions_info(const versions_info& s) = default;
versions_info& operator = (const versions_info& s) = default;
/**
* @brief Encode the versions info as a JSON object
*/
nlohmann::json as_json() const;
std::string falco_version;
std::string engine_version;
std::string libs_version;
std::string plugin_api_version;
std::string driver_api_version;
std::string driver_schema_version;
std::string default_driver_version;
std::unordered_map<std::string, std::string> plugin_versions;
};
};