mirror of
https://github.com/falcosecurity/falco.git
synced 2025-07-21 09:59:40 +00:00
update(userspace/falco): introduce message struct for outputs
Signed-off-by: Leonardo Grasso <me@leonardograsso.com>
This commit is contained in:
parent
3b78cda716
commit
8eb7d83ee8
@ -37,6 +37,21 @@ struct config
|
|||||||
std::map<std::string, std::string> options;
|
std::map<std::string, std::string> options;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
// The message to be outputted. It can either refer to:
|
||||||
|
// - an event that has matched some rule,
|
||||||
|
// - or a generic message (e.g., a drop alert).
|
||||||
|
//
|
||||||
|
struct message
|
||||||
|
{
|
||||||
|
uint64_t ts;
|
||||||
|
falco_common::priority_type priority;
|
||||||
|
std::string msg;
|
||||||
|
std::string rule;
|
||||||
|
std::string source;
|
||||||
|
map<std::string, std::string> fields;
|
||||||
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
// This class acts as the primary interface for implementing
|
// This class acts as the primary interface for implementing
|
||||||
// a Falco output class.
|
// a Falco output class.
|
||||||
@ -52,15 +67,13 @@ public:
|
|||||||
m_hostname = hostname;
|
m_hostname = hostname;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output an event that has matched some rule.
|
// Output a message.
|
||||||
virtual void output_event(gen_event *evt, std::string &rule, std::string &source,
|
virtual void output(const message *msg) = 0;
|
||||||
falco_common::priority_type priority, std::string &format, std::string &msg) = 0;
|
|
||||||
|
|
||||||
// Output a generic message. Not necessarily associated with any event.
|
|
||||||
virtual void output_msg(falco_common::priority_type priority, std::string &msg) = 0;
|
|
||||||
|
|
||||||
|
// Possibly close the output and open it again.
|
||||||
virtual void reopen() {}
|
virtual void reopen() {}
|
||||||
|
|
||||||
|
// Possibly flush the output.
|
||||||
virtual void cleanup() {}
|
virtual void cleanup() {}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -31,16 +31,10 @@ void falco::outputs::output_file::open_file()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void falco::outputs::output_file::output_event(gen_event *evt, std::string &rule, std::string &source,
|
void falco::outputs::output_file::output(const message *msg)
|
||||||
falco_common::priority_type priority, std::string &format, std::string &msg)
|
|
||||||
{
|
|
||||||
output_msg(priority, msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
void falco::outputs::output_file::output_msg(falco_common::priority_type priority, std::string &msg)
|
|
||||||
{
|
{
|
||||||
open_file();
|
open_file();
|
||||||
m_outfile << msg + "\n";
|
m_outfile << msg->msg + "\n";
|
||||||
|
|
||||||
if(m_oc.options["keep_alive"] != "true")
|
if(m_oc.options["keep_alive"] != "true")
|
||||||
{
|
{
|
||||||
|
@ -27,10 +27,7 @@ namespace outputs
|
|||||||
|
|
||||||
class output_file : public abstract_output
|
class output_file : public abstract_output
|
||||||
{
|
{
|
||||||
void output_event(gen_event *evt, std::string &rule, std::string &source,
|
void output(const message *msg);
|
||||||
falco_common::priority_type priority, std::string &format, std::string &msg);
|
|
||||||
|
|
||||||
void output_msg(falco_common::priority_type priority, std::string &msg);
|
|
||||||
|
|
||||||
void cleanup();
|
void cleanup();
|
||||||
|
|
||||||
|
@ -21,23 +21,21 @@ limitations under the License.
|
|||||||
#include "formats.h"
|
#include "formats.h"
|
||||||
#include "banned.h" // This raises a compilation error when certain functions are used
|
#include "banned.h" // This raises a compilation error when certain functions are used
|
||||||
|
|
||||||
void falco::outputs::output_grpc::output_event(gen_event *evt, std::string &rule, std::string &source,
|
void falco::outputs::output_grpc::output(const message *msg)
|
||||||
falco_common::priority_type priority, std::string &format,
|
|
||||||
std::string &msg)
|
|
||||||
{
|
{
|
||||||
falco::outputs::response grpc_res;
|
falco::outputs::response grpc_res;
|
||||||
|
|
||||||
// time
|
// time
|
||||||
auto timestamp = grpc_res.mutable_time();
|
auto timestamp = grpc_res.mutable_time();
|
||||||
*timestamp = google::protobuf::util::TimeUtil::NanosecondsToTimestamp(evt->get_ts());
|
*timestamp = google::protobuf::util::TimeUtil::NanosecondsToTimestamp(msg->ts);
|
||||||
|
|
||||||
// rule
|
// rule
|
||||||
auto r = grpc_res.mutable_rule();
|
auto r = grpc_res.mutable_rule();
|
||||||
*r = rule;
|
*r = msg->rule;
|
||||||
|
|
||||||
// source
|
// source
|
||||||
falco::schema::source s = falco::schema::source::SYSCALL;
|
falco::schema::source s = falco::schema::source::SYSCALL;
|
||||||
if(!falco::schema::source_Parse(source, &s))
|
if(!falco::schema::source_Parse(msg->source, &s))
|
||||||
{
|
{
|
||||||
throw falco_exception("Unknown source passed to output_grpc::output_event()");
|
throw falco_exception("Unknown source passed to output_grpc::output_event()");
|
||||||
}
|
}
|
||||||
@ -45,7 +43,7 @@ void falco::outputs::output_grpc::output_event(gen_event *evt, std::string &rule
|
|||||||
|
|
||||||
// priority
|
// priority
|
||||||
falco::schema::priority p = falco::schema::priority::EMERGENCY;
|
falco::schema::priority p = falco::schema::priority::EMERGENCY;
|
||||||
if(!falco::schema::priority_Parse(falco_common::priority_names[priority], &p))
|
if(!falco::schema::priority_Parse(falco_common::priority_names[msg->priority], &p))
|
||||||
{
|
{
|
||||||
throw falco_exception("Unknown priority passed to output_grpc::output_event()");
|
throw falco_exception("Unknown priority passed to output_grpc::output_event()");
|
||||||
}
|
}
|
||||||
@ -53,12 +51,11 @@ void falco::outputs::output_grpc::output_event(gen_event *evt, std::string &rule
|
|||||||
|
|
||||||
// output
|
// output
|
||||||
auto output = grpc_res.mutable_output();
|
auto output = grpc_res.mutable_output();
|
||||||
*output = msg;
|
*output = msg->msg;
|
||||||
|
|
||||||
// output fields
|
// output fields
|
||||||
auto &fields = *grpc_res.mutable_output_fields();
|
auto &fields = *grpc_res.mutable_output_fields();
|
||||||
auto resolvedTkns = falco_formats::resolve_tokens(evt, source, format);
|
for(const auto &kv : msg->fields)
|
||||||
for(const auto &kv : resolvedTkns)
|
|
||||||
{
|
{
|
||||||
fields[kv.first] = kv.second;
|
fields[kv.first] = kv.second;
|
||||||
}
|
}
|
||||||
@ -69,8 +66,3 @@ void falco::outputs::output_grpc::output_event(gen_event *evt, std::string &rule
|
|||||||
|
|
||||||
falco::grpc::queue::get().push(grpc_res);
|
falco::grpc::queue::get().push(grpc_res);
|
||||||
}
|
}
|
||||||
|
|
||||||
void falco::outputs::output_grpc::output_msg(falco_common::priority_type priority, std::string &msg)
|
|
||||||
{
|
|
||||||
// todo(fntlnz, leodido, leogr) > gRPC does not support subscribing to dropped events yet
|
|
||||||
}
|
|
@ -25,10 +25,7 @@ namespace outputs
|
|||||||
|
|
||||||
class output_grpc : public abstract_output
|
class output_grpc : public abstract_output
|
||||||
{
|
{
|
||||||
void output_event(gen_event *evt, std::string &rule, std::string &source,
|
void output(const message *msg);
|
||||||
falco_common::priority_type priority, std::string &format, std::string &msg);
|
|
||||||
|
|
||||||
void output_msg(falco_common::priority_type priority, std::string &msg);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace outputs
|
} // namespace outputs
|
||||||
|
@ -18,13 +18,7 @@ limitations under the License.
|
|||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
#include "banned.h" // This raises a compilation error when certain functions are used
|
#include "banned.h" // This raises a compilation error when certain functions are used
|
||||||
|
|
||||||
void falco::outputs::output_http::output_event(gen_event *evt, std::string &rule, std::string &source,
|
void falco::outputs::output_http::output(const message *msg)
|
||||||
falco_common::priority_type priority, std::string &format, std::string &msg)
|
|
||||||
{
|
|
||||||
output_msg(priority, msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
void falco::outputs::output_http::output_msg(falco_common::priority_type priority, std::string &msg)
|
|
||||||
{
|
{
|
||||||
CURL *curl = NULL;
|
CURL *curl = NULL;
|
||||||
CURLcode res = CURLE_FAILED_INIT;
|
CURLcode res = CURLE_FAILED_INIT;
|
||||||
@ -37,7 +31,7 @@ void falco::outputs::output_http::output_msg(falco_common::priority_type priorit
|
|||||||
slist1 = curl_slist_append(slist1, "Content-Type: application/json");
|
slist1 = curl_slist_append(slist1, "Content-Type: application/json");
|
||||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist1);
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist1);
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, m_oc.options["url"].c_str());
|
curl_easy_setopt(curl, CURLOPT_URL, m_oc.options["url"].c_str());
|
||||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, msg.c_str());
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, msg->msg.c_str());
|
||||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1L);
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1L);
|
||||||
|
|
||||||
res = curl_easy_perform(curl);
|
res = curl_easy_perform(curl);
|
||||||
|
@ -25,10 +25,7 @@ namespace outputs
|
|||||||
|
|
||||||
class output_http : public abstract_output
|
class output_http : public abstract_output
|
||||||
{
|
{
|
||||||
void output_event(gen_event *evt, std::string &rule, std::string &source,
|
void output(const message *msg);
|
||||||
falco_common::priority_type priority, std::string &format, std::string &msg);
|
|
||||||
|
|
||||||
void output_msg(falco_common::priority_type priority, std::string &msg);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace outputs
|
} // namespace outputs
|
||||||
|
@ -31,17 +31,11 @@ void falco::outputs::output_program::open_pfile()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void falco::outputs::output_program::output_event(gen_event *evt, std::string &rule, std::string &source,
|
void falco::outputs::output_program::output(const message *msg)
|
||||||
falco_common::priority_type priority, std::string &format, std::string &msg)
|
|
||||||
{
|
|
||||||
output_msg(priority, msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
void falco::outputs::output_program::output_msg(falco_common::priority_type priority, std::string &msg)
|
|
||||||
{
|
{
|
||||||
open_pfile();
|
open_pfile();
|
||||||
|
|
||||||
fprintf(m_pfile, "%s\n", msg.c_str());
|
fprintf(m_pfile, "%s\n", msg->msg.c_str());
|
||||||
|
|
||||||
if(m_oc.options["keep_alive"] != "true")
|
if(m_oc.options["keep_alive"] != "true")
|
||||||
{
|
{
|
||||||
|
@ -25,10 +25,7 @@ namespace outputs
|
|||||||
|
|
||||||
class output_program : public abstract_output
|
class output_program : public abstract_output
|
||||||
{
|
{
|
||||||
void output_event(gen_event *evt, std::string &rule, std::string &source,
|
void output(const message *msg);
|
||||||
falco_common::priority_type priority, std::string &format, std::string &msg);
|
|
||||||
|
|
||||||
void output_msg(falco_common::priority_type priority, std::string &msg);
|
|
||||||
|
|
||||||
void cleanup();
|
void cleanup();
|
||||||
|
|
||||||
|
@ -18,13 +18,7 @@ limitations under the License.
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "banned.h" // This raises a compilation error when certain functions are used
|
#include "banned.h" // This raises a compilation error when certain functions are used
|
||||||
|
|
||||||
void falco::outputs::output_stdout::output_event(gen_event *evt, std::string &rule, std::string &source,
|
void falco::outputs::output_stdout::output(const message *msg)
|
||||||
falco_common::priority_type priority, std::string &format, std::string &msg)
|
|
||||||
{
|
|
||||||
output_msg(priority, msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
void falco::outputs::output_stdout::output_msg(falco_common::priority_type priority, std::string &msg)
|
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
// By default, the stdout stream is fully buffered or line buffered
|
// By default, the stdout stream is fully buffered or line buffered
|
||||||
@ -36,7 +30,7 @@ void falco::outputs::output_stdout::output_msg(falco_common::priority_type prior
|
|||||||
{
|
{
|
||||||
std::cout << std::unitbuf;
|
std::cout << std::unitbuf;
|
||||||
}
|
}
|
||||||
std::cout << msg + "\n";
|
std::cout << msg->msg + "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void falco::outputs::output_stdout::cleanup()
|
void falco::outputs::output_stdout::cleanup()
|
||||||
|
@ -25,10 +25,7 @@ namespace outputs
|
|||||||
|
|
||||||
class output_stdout : public abstract_output
|
class output_stdout : public abstract_output
|
||||||
{
|
{
|
||||||
void output_event(gen_event *evt, std::string &rule, std::string &source,
|
void output(const message *msg);
|
||||||
falco_common::priority_type priority, std::string &format, std::string &msg);
|
|
||||||
|
|
||||||
void output_msg(falco_common::priority_type priority, std::string &msg);
|
|
||||||
|
|
||||||
void cleanup();
|
void cleanup();
|
||||||
};
|
};
|
||||||
|
@ -18,14 +18,8 @@ limitations under the License.
|
|||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
#include "banned.h" // This raises a compilation error when certain functions are used
|
#include "banned.h" // This raises a compilation error when certain functions are used
|
||||||
|
|
||||||
void falco::outputs::output_syslog::output_event(gen_event *evt, std::string &rule, std::string &source,
|
void falco::outputs::output_syslog::output(const message *msg)
|
||||||
falco_common::priority_type priority, std::string &format, std::string &msg)
|
|
||||||
{
|
|
||||||
output_msg(priority, msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
void falco::outputs::output_syslog::output_msg(falco_common::priority_type priority, std::string &msg)
|
|
||||||
{
|
{
|
||||||
// Syslog output should not have any trailing newline
|
// Syslog output should not have any trailing newline
|
||||||
::syslog(priority, "%s", msg.c_str());
|
::syslog(msg->priority, "%s", msg->msg.c_str());
|
||||||
}
|
}
|
||||||
|
@ -25,10 +25,7 @@ namespace outputs
|
|||||||
|
|
||||||
class output_syslog : public abstract_output
|
class output_syslog : public abstract_output
|
||||||
{
|
{
|
||||||
void output_event(gen_event *evt, std::string &rule, std::string &source,
|
void output(const message *msg);
|
||||||
falco_common::priority_type priority, std::string &format, std::string &msg);
|
|
||||||
|
|
||||||
void output_msg(falco_common::priority_type priority, std::string &msg);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace outputs
|
} // namespace outputs
|
||||||
|
Loading…
Reference in New Issue
Block a user