-
Notifications
You must be signed in to change notification settings - Fork 42
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
Adding capability of running automated process rules tests #127
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4551115
Process rules tests addition
jeniawhite f547345
Refactor and add kubelet tests
jeniawhite cc31542
Adding Lease and altering test cases
jeniawhite 2ec9fe7
Change test cases of api server
jeniawhite 29fa798
Addition of ETCD cert happy cases
jeniawhite d09a84f
Review fixes
jeniawhite 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
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,62 @@ | ||
""" | ||
uri-weisman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Kubernetes CIS rules verification. | ||
This module verifies correctness of retrieved findings by manipulating audit and remediation actions | ||
""" | ||
from datetime import datetime | ||
|
||
import pytest | ||
import time | ||
|
||
from commonlib.utils import get_evaluation | ||
from product.tests.tests.process.process_test_cases import * | ||
|
||
|
||
@pytest.mark.rules | ||
@pytest.mark.parametrize( | ||
("rule_tag", "dictionary", "resource", "expected"), | ||
api_server_rules, | ||
) | ||
def test_process_api_server(config_node_pre_test, | ||
rule_tag, | ||
dictionary, | ||
resource, | ||
expected): | ||
""" | ||
This data driven test verifies rules and findings return by cloudbeat agent. | ||
In order to add new cases @pytest.mark.parameterize section shall be updated. | ||
Setup and teardown actions are defined in data method. | ||
This test creates cloudbeat agent instance, changes node resources (modes, users, groups) and verifies, | ||
that cloudbeat returns correct finding. | ||
@param rule_tag: Name of rule to be verified. | ||
@param dictionary: Set and Unset dictionary | ||
@param resource: Full path to resource / file | ||
@param expected: Result to be found in finding evaluation field. | ||
@return: None - Test Pass / Fail result is generated. | ||
""" | ||
k8s_client, api_client, cloudbeat_agent = config_node_pre_test | ||
|
||
if not "edit_process_file" in dir(api_client): | ||
pytest.skip("skipping process rules run in non-containerized api_client") | ||
|
||
# Currently, single node is used, in the future may be extended for all nodes. | ||
node = k8s_client.get_cluster_nodes()[0] | ||
pods = k8s_client.get_agent_pod_instances(agent_name=cloudbeat_agent.name, namespace=cloudbeat_agent.namespace) | ||
|
||
api_client.edit_process_file(container_name=node.metadata.name, | ||
dictionary=dictionary, | ||
resource=resource) | ||
|
||
# Wait for process reboot | ||
# TODO: Implement a more optimal way of waiting | ||
time.sleep(60) | ||
|
||
evaluation = get_evaluation( | ||
k8s=k8s_client, | ||
timeout=cloudbeat_agent.findings_timeout, | ||
pod_name=pods[0].metadata.name, | ||
namespace=cloudbeat_agent.namespace, | ||
rule_tag=rule_tag, | ||
exec_timestamp=datetime.utcnow() | ||
) | ||
|
||
assert evaluation == expected, f"Rule {rule_tag} verification failed." |
62 changes: 62 additions & 0 deletions
62
tests/product/tests/test_process_controller_manager_rules.py
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,62 @@ | ||
""" | ||
Kubernetes CIS rules verification. | ||
This module verifies correctness of retrieved findings by manipulating audit and remediation actions | ||
""" | ||
from datetime import datetime | ||
|
||
import pytest | ||
import time | ||
|
||
from commonlib.utils import get_evaluation | ||
from product.tests.tests.process.process_test_cases import * | ||
|
||
|
||
@pytest.mark.rules | ||
@pytest.mark.parametrize( | ||
("rule_tag", "dictionary", "resource", "expected"), | ||
controller_manager_rules, | ||
) | ||
def test_process_controller_manager(config_node_pre_test, | ||
rule_tag, | ||
dictionary, | ||
resource, | ||
expected): | ||
""" | ||
This data driven test verifies rules and findings return by cloudbeat agent. | ||
In order to add new cases @pytest.mark.parameterize section shall be updated. | ||
Setup and teardown actions are defined in data method. | ||
This test creates cloudbeat agent instance, changes node resources (modes, users, groups) and verifies, | ||
that cloudbeat returns correct finding. | ||
@param rule_tag: Name of rule to be verified. | ||
@param dictionary: Set and Unset dictionary | ||
@param resource: Full path to resource / file | ||
@param expected: Result to be found in finding evaluation field. | ||
@return: None - Test Pass / Fail result is generated. | ||
""" | ||
k8s_client, api_client, cloudbeat_agent = config_node_pre_test | ||
|
||
if not "edit_process_file" in dir(api_client): | ||
pytest.skip("skipping process rules run in non-containerized api_client") | ||
|
||
# Currently, single node is used, in the future may be extended for all nodes. | ||
node = k8s_client.get_cluster_nodes()[0] | ||
pods = k8s_client.get_agent_pod_instances(agent_name=cloudbeat_agent.name, namespace=cloudbeat_agent.namespace) | ||
|
||
api_client.edit_process_file(container_name=node.metadata.name, | ||
dictionary=dictionary, | ||
resource=resource) | ||
|
||
# Wait for process reboot | ||
# TODO: Implement a more optimal way of waiting | ||
time.sleep(60) | ||
|
||
evaluation = get_evaluation( | ||
k8s=k8s_client, | ||
timeout=cloudbeat_agent.findings_timeout, | ||
pod_name=pods[0].metadata.name, | ||
namespace=cloudbeat_agent.namespace, | ||
rule_tag=rule_tag, | ||
exec_timestamp=datetime.utcnow() | ||
) | ||
|
||
assert evaluation == expected, f"Rule {rule_tag} verification failed." |
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,62 @@ | ||
""" | ||
Kubernetes CIS rules verification. | ||
This module verifies correctness of retrieved findings by manipulating audit and remediation actions | ||
""" | ||
from datetime import datetime | ||
|
||
import pytest | ||
import time | ||
|
||
from commonlib.utils import get_evaluation | ||
from product.tests.tests.process.process_test_cases import * | ||
|
||
|
||
@pytest.mark.rules | ||
@pytest.mark.parametrize( | ||
("rule_tag", "dictionary", "resource", "expected"), | ||
etcd_rules, | ||
) | ||
def test_process_etcd(config_node_pre_test, | ||
rule_tag, | ||
dictionary, | ||
resource, | ||
expected): | ||
""" | ||
This data driven test verifies rules and findings return by cloudbeat agent. | ||
In order to add new cases @pytest.mark.parameterize section shall be updated. | ||
Setup and teardown actions are defined in data method. | ||
This test creates cloudbeat agent instance, changes node resources (modes, users, groups) and verifies, | ||
that cloudbeat returns correct finding. | ||
@param rule_tag: Name of rule to be verified. | ||
@param dictionary: Set and Unset dictionary | ||
@param resource: Full path to resource / file | ||
@param expected: Result to be found in finding evaluation field. | ||
@return: None - Test Pass / Fail result is generated. | ||
""" | ||
k8s_client, api_client, cloudbeat_agent = config_node_pre_test | ||
|
||
if not "edit_process_file" in dir(api_client): | ||
pytest.skip("skipping process rules run in non-containerized api_client") | ||
|
||
# Currently, single node is used, in the future may be extended for all nodes. | ||
node = k8s_client.get_cluster_nodes()[0] | ||
pods = k8s_client.get_agent_pod_instances(agent_name=cloudbeat_agent.name, namespace=cloudbeat_agent.namespace) | ||
|
||
api_client.edit_process_file(container_name=node.metadata.name, | ||
dictionary=dictionary, | ||
resource=resource) | ||
|
||
# Wait for process reboot | ||
# TODO: Implement a more optimal way of waiting | ||
time.sleep(60) | ||
|
||
evaluation = get_evaluation( | ||
k8s=k8s_client, | ||
timeout=cloudbeat_agent.findings_timeout, | ||
pod_name=pods[0].metadata.name, | ||
namespace=cloudbeat_agent.namespace, | ||
rule_tag=rule_tag, | ||
exec_timestamp=datetime.utcnow() | ||
) | ||
|
||
assert evaluation == expected, f"Rule {rule_tag} verification failed." |
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.
Can we plz add some description to the function?