-
Notifications
You must be signed in to change notification settings - Fork 910
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
new(falco): add append_output configuration option with fields and format #3308
Merged
poiana
merged 7 commits into
falcosecurity:master
from
LucaGuerra:new/append_output_config
Sep 9, 2024
+701
−103
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d87df4a
new(app): add append_output configuration option with fields and format
LucaGuerra ad81dd2
new(falco): add json schema for append_output
LucaGuerra 606f2d9
new(falco): add append_output explanation to falco.yaml
LucaGuerra 69a23be
update(tests): add message for failing configuration schema test
LucaGuerra 766b459
fix(falco): update json schema
LucaGuerra f6057b3
cleanup(falco): apply review suggestion about extra_output_field_t
LucaGuerra c481491
update(libsinsp/tests): add CLI options test for append output
LucaGuerra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/* | ||
Copyright (C) 2024 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 <gtest/gtest.h> | ||
|
||
#include "../test_falco_engine.h" | ||
|
||
TEST_F(test_falco_engine, extra_format_all) | ||
{ | ||
std::string rules_content = R"END( | ||
- rule: legit_rule | ||
desc: legit rule description | ||
condition: evt.type=open | ||
output: user=%user.name command=%proc.cmdline file=%fd.name | ||
priority: INFO | ||
)END"; | ||
|
||
m_engine->add_extra_output_format("evt.type=%evt.type", "", "", "", false); | ||
ASSERT_TRUE(load_rules(rules_content, "legit_rules.yaml")) << m_load_result_string; | ||
|
||
EXPECT_EQ(get_compiled_rule_output("legit_rule"),"user=%user.name command=%proc.cmdline file=%fd.name evt.type=%evt.type"); | ||
} | ||
|
||
TEST_F(test_falco_engine, extra_format_by_rule) | ||
{ | ||
std::string rules_content = R"END( | ||
- rule: legit_rule | ||
desc: legit rule description | ||
condition: evt.type=open | ||
output: out 1 | ||
priority: INFO | ||
|
||
- rule: another_rule | ||
desc: legit rule description | ||
condition: evt.type=open | ||
output: out 2 | ||
priority: INFO | ||
)END"; | ||
|
||
m_engine->add_extra_output_format("evt.type=%evt.type", "", "", "legit_rule", false); | ||
ASSERT_TRUE(load_rules(rules_content, "legit_rules.yaml")) << m_load_result_string; | ||
|
||
EXPECT_EQ(get_compiled_rule_output("legit_rule"),"out 1 evt.type=%evt.type"); | ||
EXPECT_EQ(get_compiled_rule_output("another_rule"),"out 2"); | ||
} | ||
|
||
TEST_F(test_falco_engine, extra_format_by_tag_rule) | ||
{ | ||
std::string rules_content = R"END( | ||
- rule: legit_rule | ||
desc: legit rule description | ||
condition: evt.type=open | ||
output: out 1 | ||
priority: INFO | ||
tags: [tag1] | ||
|
||
- rule: another_rule | ||
desc: legit rule description | ||
condition: evt.type=open | ||
output: out 2 | ||
priority: INFO | ||
tags: [tag1] | ||
)END"; | ||
|
||
m_engine->add_extra_output_format("extra 1", "", "tag1", "", false); | ||
m_engine->add_extra_output_format("extra 2", "", "", "another_rule", false); | ||
|
||
ASSERT_TRUE(load_rules(rules_content, "legit_rules.yaml")) << m_load_result_string; | ||
|
||
EXPECT_EQ(get_compiled_rule_output("legit_rule"),"out 1 extra 1"); | ||
EXPECT_EQ(get_compiled_rule_output("another_rule"),"out 2 extra 1 extra 2"); | ||
} | ||
|
||
TEST_F(test_falco_engine, extra_format_replace_container_info) | ||
{ | ||
std::string rules_content = R"END( | ||
- rule: legit_rule | ||
desc: legit rule description | ||
condition: evt.type=open | ||
output: out 1 (%container.info) | ||
priority: INFO | ||
tags: [tag1] | ||
|
||
- rule: another_rule | ||
desc: legit rule description | ||
condition: evt.type=open | ||
output: out 2 | ||
priority: INFO | ||
tags: [tag1] | ||
)END"; | ||
|
||
m_engine->add_extra_output_format("extra 1", "", "", "", true); | ||
|
||
ASSERT_TRUE(load_rules(rules_content, "legit_rules.yaml")) << m_load_result_string; | ||
|
||
EXPECT_EQ(get_compiled_rule_output("legit_rule"), "out 1 (extra 1)"); | ||
EXPECT_EQ(get_compiled_rule_output("another_rule"), "out 2 extra 1"); | ||
} | ||
|
||
TEST_F(test_falco_engine, extra_format_do_not_replace_container_info) | ||
{ | ||
std::string rules_content = R"END( | ||
- rule: legit_rule | ||
desc: legit rule description | ||
condition: evt.type=open | ||
output: out 1 (%container.info) | ||
priority: INFO | ||
tags: [tag1] | ||
)END"; | ||
|
||
ASSERT_TRUE(load_rules(rules_content, "legit_rules.yaml")) << m_load_result_string; | ||
|
||
auto output = get_compiled_rule_output("legit_rule"); | ||
EXPECT_TRUE(output.find("%container.info") == output.npos); | ||
} | ||
|
||
TEST_F(test_falco_engine, extra_fields_all) | ||
{ | ||
std::string rules_content = R"END( | ||
- rule: legit_rule | ||
desc: legit rule description | ||
condition: evt.type=open | ||
output: user=%user.name command=%proc.cmdline file=%fd.name | ||
priority: INFO | ||
)END"; | ||
|
||
std::unordered_map<std::string, std::string> extra_formatted_fields = {{"my_field", "hello %evt.num"}}; | ||
for (auto const& f : extra_formatted_fields) | ||
{ | ||
m_engine->add_extra_output_formatted_field(f.first, f.second, "", "", ""); | ||
} | ||
|
||
ASSERT_TRUE(load_rules(rules_content, "legit_rules.yaml")) << m_load_result_string; | ||
|
||
EXPECT_EQ(get_compiled_rule_formatted_fields("legit_rule"), extra_formatted_fields); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/* | ||
Copyright (C) 2024 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 <gtest/gtest.h> | ||
#include <falco/configuration.h> | ||
|
||
TEST(ConfigurationRuleOutputOptions, parse_yaml) | ||
{ | ||
falco_configuration falco_config; | ||
ASSERT_NO_THROW(falco_config.init_from_content(R"( | ||
append_output: | ||
- source: syscall | ||
tag: persistence | ||
rule: some rule name | ||
format: "gparent=%proc.aname[2] ggparent=%proc.aname[3] gggparent=%proc.aname[4]" | ||
|
||
- tag: persistence | ||
fields: | ||
- proc.aname[2]: "%proc.aname[2]" | ||
- proc.aname[3]: "%proc.aname[3]" | ||
- proc.aname[4]: "%proc.aname[4]" | ||
format: "gparent=%proc.aname[2] ggparent=%proc.aname[3] gggparent=%proc.aname[4]" | ||
|
||
- source: k8s_audit | ||
fields: | ||
- ka.verb | ||
- static_field: "static content" | ||
|
||
)", {})); | ||
|
||
EXPECT_EQ(falco_config.m_append_output.size(), 3); | ||
|
||
EXPECT_EQ(falco_config.m_append_output[0].m_source, "syscall"); | ||
EXPECT_EQ(falco_config.m_append_output[0].m_tag, "persistence"); | ||
EXPECT_EQ(falco_config.m_append_output[0].m_rule, "some rule name"); | ||
EXPECT_EQ(falco_config.m_append_output[0].m_formatted_fields.size(), 0); | ||
EXPECT_EQ(falco_config.m_append_output[0].m_format, "gparent=%proc.aname[2] ggparent=%proc.aname[3] gggparent=%proc.aname[4]"); | ||
|
||
EXPECT_EQ(falco_config.m_append_output[1].m_tag, "persistence"); | ||
EXPECT_EQ(falco_config.m_append_output[1].m_format, "gparent=%proc.aname[2] ggparent=%proc.aname[3] gggparent=%proc.aname[4]"); | ||
|
||
EXPECT_EQ(falco_config.m_append_output[1].m_formatted_fields.size(), 3); | ||
EXPECT_EQ(falco_config.m_append_output[1].m_formatted_fields["proc.aname[2]"], "%proc.aname[2]"); | ||
EXPECT_EQ(falco_config.m_append_output[1].m_formatted_fields["proc.aname[3]"], "%proc.aname[3]"); | ||
EXPECT_EQ(falco_config.m_append_output[1].m_formatted_fields["proc.aname[4]"], "%proc.aname[4]"); | ||
|
||
EXPECT_EQ(falco_config.m_append_output[2].m_source, "k8s_audit"); | ||
|
||
EXPECT_EQ(falco_config.m_append_output[2].m_formatted_fields.size(), 1); | ||
EXPECT_EQ(falco_config.m_append_output[2].m_formatted_fields["static_field"], "static content"); | ||
|
||
EXPECT_EQ(falco_config.m_append_output[2].m_raw_fields.size(), 1); | ||
EXPECT_EQ(falco_config.m_append_output[2].m_raw_fields.count("ka.verb"), 1); | ||
} | ||
|
||
TEST(ConfigurationRuleOutputOptions, cli_options) | ||
{ | ||
falco_configuration falco_config; | ||
|
||
ASSERT_NO_THROW(falco_config.init_from_content("", | ||
std::vector<std::string>{ | ||
R"(append_output[]={"source": "syscall", "tag": "persistence", "rule": "some rule name", "format": "gparent=%proc.aname[2] ggparent=%proc.aname[3] gggparent=%proc.aname[4]"})", | ||
R"(append_output[]={"tag": "persistence", "fields": [{"proc.aname[2]": "%proc.aname[2]"}, {"proc.aname[3]": "%proc.aname[3]"}, {"proc.aname[4]": "%proc.aname[4]"}], "format": "gparent=%proc.aname[2] ggparent=%proc.aname[3] gggparent=%proc.aname[4]"})", | ||
R"(append_output[]={"source": "k8s_audit", "fields": ["ka.verb", {"static_field": "static content"}]})"})); | ||
|
||
EXPECT_EQ(falco_config.m_append_output.size(), 3); | ||
|
||
EXPECT_EQ(falco_config.m_append_output[0].m_source, "syscall"); | ||
EXPECT_EQ(falco_config.m_append_output[0].m_tag, "persistence"); | ||
EXPECT_EQ(falco_config.m_append_output[0].m_rule, "some rule name"); | ||
EXPECT_EQ(falco_config.m_append_output[0].m_formatted_fields.size(), 0); | ||
EXPECT_EQ(falco_config.m_append_output[0].m_format, "gparent=%proc.aname[2] ggparent=%proc.aname[3] gggparent=%proc.aname[4]"); | ||
|
||
EXPECT_EQ(falco_config.m_append_output[1].m_tag, "persistence"); | ||
EXPECT_EQ(falco_config.m_append_output[1].m_format, "gparent=%proc.aname[2] ggparent=%proc.aname[3] gggparent=%proc.aname[4]"); | ||
|
||
EXPECT_EQ(falco_config.m_append_output[1].m_formatted_fields.size(), 3); | ||
EXPECT_EQ(falco_config.m_append_output[1].m_formatted_fields["proc.aname[2]"], "%proc.aname[2]"); | ||
EXPECT_EQ(falco_config.m_append_output[1].m_formatted_fields["proc.aname[3]"], "%proc.aname[3]"); | ||
EXPECT_EQ(falco_config.m_append_output[1].m_formatted_fields["proc.aname[4]"], "%proc.aname[4]"); | ||
|
||
EXPECT_EQ(falco_config.m_append_output[2].m_source, "k8s_audit"); | ||
|
||
EXPECT_EQ(falco_config.m_append_output[2].m_formatted_fields.size(), 1); | ||
EXPECT_EQ(falco_config.m_append_output[2].m_formatted_fields["static_field"], "static content"); | ||
|
||
EXPECT_EQ(falco_config.m_append_output[2].m_raw_fields.size(), 1); | ||
EXPECT_EQ(falco_config.m_append_output[2].m_raw_fields.count("ka.verb"), 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.