new(falco): add --version-json to print version information in json format

Signed-off-by: Luca Guerra <luca@guerra.sh>
This commit is contained in:
Luca Guerra 2022-12-16 16:24:48 +00:00 committed by poiana
parent 280fcfe5d3
commit 1b2c7ef7d9
5 changed files with 63 additions and 28 deletions

View File

@ -48,23 +48,10 @@ application::run_result application::print_support()
support["version"] = FALCO_VERSION;
support["libs_version"] = FALCOSECURITY_LIBS_VERSION;
support["plugin_api_version"] = s->get_plugin_api_version();
support["plugin_api_version"] = application::get_plugin_api_version();
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);
snprintf(driver_api_version_string, sizeof(driver_api_version_string), "%lu.%lu.%lu", driver_api_major, driver_api_minor, driver_api_patch);
support["driver_api_version"] = driver_api_version_string;
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);
snprintf(driver_schema_version_string, sizeof(driver_schema_version_string), "%lu.%lu.%lu", driver_schema_major, driver_schema_minor, driver_schema_patch);
support["driver_schema_version"] = driver_schema_version_string;
support["driver_api_version"] = application::get_driver_api_version();
support["driver_schema_version"] = application::get_driver_schema_version();
support["default_driver_version"] = DRIVER_VERSION;
support["system_info"]["sysname"] = sysinfo.sysname;

View File

@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
#include <nlohmann/json.hpp>
#include "config_falco.h"
#include "application.h"
#include "falco_engine_version.h"
@ -27,24 +29,33 @@ application::run_result application::print_version()
std::unique_ptr<sinsp> s(new sinsp());
printf("Falco version: %s\n", FALCO_VERSION);
printf("Libs version: %s\n", FALCOSECURITY_LIBS_VERSION);
printf("Plugin API: %s\n", s->get_plugin_api_version());
printf("Plugin API: %s\n", application::get_plugin_api_version().c_str());
printf("Engine: %d\n", FALCO_ENGINE_VERSION);
// todo(leogr): move string conversion to scap
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);
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);
printf("Driver:\n");
printf(" API version: %lu.%lu.%lu\n", driver_api_major, driver_api_minor, driver_api_patch);
printf(" Schema version: %lu.%lu.%lu\n", driver_schema_major, driver_schema_minor, driver_schema_patch);
printf(" API version: %s\n", application::get_driver_api_version().c_str());
printf(" Schema version: %s\n", application::get_driver_api_version().c_str());
printf(" Default driver: %s\n", DRIVER_VERSION);
return run_result::exit();
}
if(m_options.print_version_info_json)
{
nlohmann::json version_info;
version_info["falco_version"] = FALCO_VERSION;
version_info["libs_version"] = FALCOSECURITY_LIBS_VERSION;
version_info["plugin_api_version"] = application::get_plugin_api_version();
version_info["driver_api_version"] = application::get_driver_api_version();
version_info["driver_schema_version"] = application::get_driver_schema_version();
version_info["default_driver_version"] = DRIVER_VERSION;
version_info["engine_version"] = std::to_string(FALCO_ENGINE_VERSION);
printf("%s\n", version_info.dump().c_str());
return run_result::exit();
}
return run_result::ok();
}

View File

@ -211,6 +211,7 @@ void cmdline_options::define()
("V,validate", "Read the contents of the specified rules(s) file and exit. This option can be passed multiple times to validate multiple files.", cxxopts::value(validate_rules_filenames), "<rules_file>")
("v", "Verbose output.", cxxopts::value(verbose)->default_value("false"))
("version", "Print version number.", cxxopts::value(print_version_info)->default_value("false"))
("version-json", "Print version information in JSON format", cxxopts::value(print_version_info_json)->default_value("false"))
("page-size", "Print the system page size (may help you to choose the right syscall ring-buffer size).", cxxopts::value(print_page_size)->default_value("false"));

View File

@ -78,6 +78,7 @@ public:
std::vector<std::string> validate_rules_filenames;
bool verbose;
bool print_version_info;
bool print_version_info_json;
bool print_page_size;
bool modern_bpf;

View File

@ -400,6 +400,41 @@ private:
return g_reopen_outputs.load(std::memory_order_seq_cst) != APP_SIGNAL_NOT_SET;
}
static inline std::string get_plugin_api_version()
{
std::unique_ptr<sinsp> s(new sinsp());
return std::string(s->get_plugin_api_version());
}
// TODO: move string conversion to scap
static inline std::string get_driver_api_version()
{
char driver_api_version_string[32];
std::unique_ptr<sinsp> s(new sinsp());
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);
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);
}
static inline std::string get_driver_schema_version()
{
char driver_schema_version_string[32];
std::unique_ptr<sinsp> s(new sinsp());
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);
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);
}
std::unique_ptr<state> m_state;
cmdline_options m_options;
bool m_initialized;