From aa8edadf684ec511e8bffaf78efc10b9264140bf Mon Sep 17 00:00:00 2001 From: Leonardo Grasso Date: Tue, 22 Sep 2020 17:44:57 +0200 Subject: [PATCH] new(userspace/falco): http output C++ impl Signed-off-by: Leonardo Grasso --- userspace/falco/falco_outputs_http.cpp | 54 ++++++++++++++++++++++++++ userspace/falco/falco_outputs_http.h | 35 +++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 userspace/falco/falco_outputs_http.cpp create mode 100644 userspace/falco/falco_outputs_http.h diff --git a/userspace/falco/falco_outputs_http.cpp b/userspace/falco/falco_outputs_http.cpp new file mode 100644 index 00000000..c32a113c --- /dev/null +++ b/userspace/falco/falco_outputs_http.cpp @@ -0,0 +1,54 @@ +/* +Copyright (C) 2020 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 "falco_outputs_http.h" +#include "logger.h" +#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, + 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; + CURLcode res = CURLE_FAILED_INIT; + struct curl_slist *slist1; + slist1 = NULL; + + curl = curl_easy_init(); + if(curl) + { + slist1 = curl_slist_append(slist1, "Content-Type: application/json"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist1); + 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_POSTFIELDSIZE, -1L); + + res = curl_easy_perform(curl); + + if(res != CURLE_OK) + { + falco_logger::log(LOG_ERR, "libcurl error: " + string(curl_easy_strerror(res))); + } + curl_easy_cleanup(curl); + curl = NULL; + curl_slist_free_all(slist1); + slist1 = NULL; + } +} \ No newline at end of file diff --git a/userspace/falco/falco_outputs_http.h b/userspace/falco/falco_outputs_http.h new file mode 100644 index 00000000..39d01511 --- /dev/null +++ b/userspace/falco/falco_outputs_http.h @@ -0,0 +1,35 @@ +/* +Copyright (C) 2020 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. +*/ + +#pragma once + +#include "falco_output.h" + +namespace falco +{ +namespace outputs +{ + +class output_http : public output +{ + void output_event(gen_event *evt, std::string &rule, std::string &source, + 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 falco