Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix webserver errors #759

Merged
merged 6 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test/traces-info
test/job-results
test/.phoronix-test-suite
test/results*.json.*
test/build

userspace/falco/lua/re.lua
userspace/falco/lua/lpeg.so
Expand Down
7 changes: 5 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# License for the specific language governing permissions and limitations under
# the License.
#
set(FALCO_TESTS_SOURCES test_base.cpp engine/test_token_bucket.cpp)
set(FALCO_TESTS_SOURCES test_base.cpp engine/test_token_bucket.cpp falco/test_webserver.cpp)

set(FALCO_TESTED_LIBRARIES falco_engine)

Expand All @@ -38,7 +38,10 @@ if(FALCO_BUILD_TESTS)
falco_test
PUBLIC "${CATCH2_INCLUDE}"
"${FAKEIT_INCLUDE}"
"${PROJECT_SOURCE_DIR}/userspace/engine")
"${PROJECT_SOURCE_DIR}/userspace/engine"
"${YAMLCPP_INCLUDE_DIR}"
"${CIVETWEB_INCLUDE_DIR}"
"${PROJECT_SOURCE_DIR}/userspace/falco")

include(CMakeParseArguments)
include(CTest)
Expand Down
31 changes: 31 additions & 0 deletions tests/falco/test_webserver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright (C) 2016-2019 Draios Inc dba Sysdig.

This file is part of falco.

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 "webserver.h"
#include <catch.hpp>

TEST_CASE("webserver must accept invalid data", "[!hide][webserver][k8s_audit_handler][accept_data]")
{
// falco_engine* engine = new falco_engine();
// falco_outputs* outputs = new falco_outputs(engine);
// std::string errstr;
// std::string input("{\"kind\": 0}");
//k8s_audit_handler::accept_data(engine, outputs, input, errstr);

REQUIRE(1 == 1);
}
56 changes: 32 additions & 24 deletions userspace/engine/falco_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,46 +365,54 @@ unique_ptr<falco_engine::rule_result> falco_engine::process_k8s_audit_event(json

bool falco_engine::parse_k8s_audit_json(nlohmann::json &j, std::list<json_event> &evts)
{
// If the Kind is EventList, split it into individual events.
if(j.value("kind", "<NA>") == "EventList")
// Note that nlohmann::basic_json::value can throw nlohmann::basic_json::type_error (302, 306)
try
{
for(auto &je : j["items"])
// If the kind is EventList, split it into individual events
if(j.value("kind", "<NA>") == "EventList")
{
evts.emplace_back();
je["kind"] = "Event";
for(auto &je : j["items"])
{
evts.emplace_back();
je["kind"] = "Event";

uint64_t ns = 0;
if(!sinsp_utils::parse_iso_8601_utc_string(je.value(k8s_audit_time, "<NA>"), ns))
{
return false;
}

std::string tmp;
sinsp_utils::ts_to_string(ns, &tmp, false, true);

evts.back().set_jevt(je, ns);
}

return true;
}
else if(j.value("kind", "<NA>") == "Event")
leodido marked this conversation as resolved.
Show resolved Hide resolved
{
evts.emplace_back();
uint64_t ns = 0;
if(!sinsp_utils::parse_iso_8601_utc_string(je.value(k8s_audit_time, "<NA>"), ns))
if(!sinsp_utils::parse_iso_8601_utc_string(j.value(k8s_audit_time, "<NA>"), ns))
{
return false;
}

std::string tmp;
sinsp_utils::ts_to_string(ns, &tmp, false, true);

evts.back().set_jevt(je, ns);
evts.back().set_jevt(j, ns);
return true;
}

return true;
}
else if(j.value("kind", "<NA>") == "Event")
{
evts.emplace_back();
uint64_t ns = 0;
if(!sinsp_utils::parse_iso_8601_utc_string(j.value(k8s_audit_time, "<NA>"), ns))
else
{
return false;
}


evts.back().set_jevt(j, ns);
return true;
}
else
catch(exception &e)
{
// Propagate the exception
rethrow_exception(current_exception());
return false;
}

}

unique_ptr<falco_engine::rule_result> falco_engine::process_k8s_audit_event(json_event *ev)
Expand Down
21 changes: 18 additions & 3 deletions userspace/falco/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,31 @@ bool k8s_audit_handler::accept_data(falco_engine *engine,
std::list<json_event> jevts;
json j;

try {
try
{
j = json::parse(data);
}
catch (json::parse_error& e)
catch(json::parse_error &e)
{
errstr = string("Could not parse data: ") + e.what();
return false;
}
catch(json::out_of_range &e)
{
errstr = string("Could not parse data: ") + e.what();
return false;
}

if(!engine->parse_k8s_audit_json(j, jevts))
bool ok;
try
{
ok = engine->parse_k8s_audit_json(j, jevts);
}
catch(json::type_error &e)
{
ok = false;
}
if(!ok)
{
errstr = string("Data not recognized as a k8s audit event");
return false;
Expand Down