mirror of
https://github.com/falcosecurity/falco.git
synced 2025-09-07 17:54:07 +00:00
refactor(userspace/engine): turn falco_common into a namespace containing common static utilities
Signed-off-by: Jason Dellaluce <jasondellaluce@gmail.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Copyright (C) 2019 The Falco Authors.
|
Copyright (C) 2022 The Falco Authors.
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
@@ -16,61 +16,39 @@ limitations under the License.
|
|||||||
|
|
||||||
#include "falco_common.h"
|
#include "falco_common.h"
|
||||||
|
|
||||||
std::vector<std::string> falco_common::priority_names = {
|
vector<string> falco_common::priority_names = {
|
||||||
"Emergency",
|
"Emergency",
|
||||||
"Alert",
|
"Alert",
|
||||||
"Critical",
|
"Critical",
|
||||||
"Error",
|
"Error",
|
||||||
"Warning",
|
"Warning",
|
||||||
"Notice",
|
"Notice",
|
||||||
"Informational",
|
"Info",
|
||||||
"Debug"};
|
"Debug"
|
||||||
|
};
|
||||||
|
|
||||||
falco_common::falco_common()
|
bool falco_common::parse_priority(string v, priority_type& out)
|
||||||
{
|
{
|
||||||
m_ls = lua_open();
|
transform(v.begin(), v.end(), v.begin(), [](int c){return tolower(c);});
|
||||||
if(!m_ls)
|
for (size_t i = 0; i < priority_names.size(); i++)
|
||||||
{
|
{
|
||||||
throw falco_exception("Cannot open lua");
|
auto p = priority_names[i];
|
||||||
}
|
transform(p.begin(), p.end(), p.begin(), [](int c){return tolower(c);});
|
||||||
luaL_openlibs(m_ls);
|
if (p.compare(0, v.size(), v) == 0)
|
||||||
}
|
|
||||||
|
|
||||||
falco_common::~falco_common()
|
|
||||||
{
|
|
||||||
if(m_ls)
|
|
||||||
{
|
|
||||||
lua_close(m_ls);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void falco_common::init()
|
|
||||||
{
|
|
||||||
// Strings in the list lua_module_strings need to be loaded as
|
|
||||||
// lua modules, which also involves adding them to the
|
|
||||||
// package.module table.
|
|
||||||
for(const auto &pair : lua_module_strings)
|
|
||||||
{
|
|
||||||
lua_getglobal(m_ls, "package");
|
|
||||||
lua_getfield(m_ls, -1, "preload");
|
|
||||||
|
|
||||||
if(luaL_loadstring(m_ls, pair.first))
|
|
||||||
{
|
{
|
||||||
throw falco_exception("Failed to load embedded lua code " +
|
out = (priority_type) i;
|
||||||
string(pair.second) + ": " + lua_tostring(m_ls, -1));
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
lua_setfield(m_ls, -2, pair.second);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Strings in the list lua_code_strings need to be loaded and
|
|
||||||
// evaluated so any public functions can be directly called.
|
|
||||||
for(const auto &str : lua_code_strings)
|
|
||||||
{
|
|
||||||
if(luaL_loadstring(m_ls, str) || lua_pcall(m_ls, 0, 0, 0))
|
|
||||||
{
|
|
||||||
throw falco_exception("Failed to load + evaluate embedded lua code " +
|
|
||||||
string(str) + ": " + lua_tostring(m_ls, -1));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool falco_common::format_priority(priority_type v, string& out)
|
||||||
|
{
|
||||||
|
if ((size_t) v < priority_names.size())
|
||||||
|
{
|
||||||
|
out = priority_names[(size_t) v];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Copyright (C) 2019 The Falco Authors.
|
Copyright (C) 2022 The Falco Authors.
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
@@ -50,22 +50,12 @@ struct falco_exception : std::exception
|
|||||||
std::string m_error_str;
|
std::string m_error_str;
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
namespace falco_common
|
||||||
// This is the base class of falco_engine/falco_output. It is
|
|
||||||
// responsible for managing a lua state and associated inspector and
|
|
||||||
// loading a single "main" lua file into that state.
|
|
||||||
//
|
|
||||||
|
|
||||||
class falco_common
|
|
||||||
{
|
{
|
||||||
public:
|
const string syscall_source = "syscall";
|
||||||
falco_common();
|
|
||||||
virtual ~falco_common();
|
|
||||||
|
|
||||||
void init();
|
// Priority levels, as a vector of strings
|
||||||
|
extern std::vector<std::string> priority_names;
|
||||||
// Priority levels, as a vector of strings
|
|
||||||
static std::vector<std::string> priority_names;
|
|
||||||
|
|
||||||
// Same as numbers/indices into the above vector
|
// Same as numbers/indices into the above vector
|
||||||
enum priority_type
|
enum priority_type
|
||||||
@@ -79,9 +69,7 @@ public:
|
|||||||
PRIORITY_INFORMATIONAL = 6,
|
PRIORITY_INFORMATIONAL = 6,
|
||||||
PRIORITY_DEBUG = 7
|
PRIORITY_DEBUG = 7
|
||||||
};
|
};
|
||||||
|
|
||||||
protected:
|
bool parse_priority(std::string v, priority_type& out);
|
||||||
lua_State *m_ls;
|
bool format_priority(priority_type v, std::string& out);
|
||||||
|
|
||||||
std::mutex m_ls_semaphore;
|
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user