Skip to content

Fix broken k8s_object_rules tests.#302

Merged
yashtewari merged 1 commit into
elastic:mainfrom
yashtewari:fix-k8s-object-rules-tests
Jul 28, 2022
Merged

Fix broken k8s_object_rules tests.#302
yashtewari merged 1 commit into
elastic:mainfrom
yashtewari:fix-k8s-object-rules-tests

Conversation

@yashtewari

@yashtewari yashtewari commented Jul 26, 2022

Copy link
Copy Markdown
Contributor

Most of the problems occur in resource-matching logic that tries to find the right ES event to compare the expected evaluation against, which is harder to do for K8S resources than other resource types.

Inserting a per-testcase UUID into the K8S object's metadata that is being tested allows for much simpler resource-matching than the recursion logic in the dict_contains utility.

Resolves: https://github.com/elastic/security-team/issues/4312

@yashtewari yashtewari requested a review from a team as a code owner July 26, 2022 07:08
@mergify

mergify Bot commented Jul 26, 2022

Copy link
Copy Markdown
Contributor

This pull request does not have a backport label. Could you fix it @yashtewari? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v./d./d./d is the label to automatically backport to the 8./d branch. /d is the digit
    NOTE: backport-skip has been added to this pull request.

@yashtewari yashtewari force-pushed the fix-k8s-object-rules-tests branch 2 times, most recently from fd13092 to 70bc263 Compare July 28, 2022 07:27
@yashtewari yashtewari requested a review from oren-zohar July 28, 2022 07:54
@yashtewari yashtewari force-pushed the fix-k8s-object-rules-tests branch 3 times, most recently from 7d518cd to 61623c0 Compare July 28, 2022 08:00

@oren-zohar oren-zohar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inserting a per-testcase UUID into the K8S object's metadata that is being tested allows for much simpler resource-matching than the recursion logic in the dict_contains utility.

🚀 🚀 🚀 🚀

Looks good, one comment

Comment thread tests/product/tests/test_k8s_objects_rules.py
Comment on lines +75 to +78
test_resource_id = str(uuid.uuid4())
labels = resource_body['metadata'].get('labels', {})
labels['test_resource_id'] = test_resource_id
resource_body['metadata']['labels'] = labels

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test_resource_id = str(uuid.uuid4())
labels = resource_body['metadata'].get('labels', {})
labels['test_resource_id'] = test_resource_id
resource_body['metadata']['labels'] = labels
metadata.setdefault('labels', {})
test_resource_id = str(uuid.uuid4())
metadata['labels']['test_resource_id'] = test_resource_id

Comment thread tests/product/tests/test_k8s_objects_rules.py Outdated
@yashtewari yashtewari force-pushed the fix-k8s-object-rules-tests branch from 61623c0 to 8741308 Compare July 28, 2022 13:28
@yashtewari yashtewari enabled auto-merge (squash) July 28, 2022 13:59
@github-actions

Copy link
Copy Markdown

@gurevichdmitry gurevichdmitry left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, great work!

@yashtewari yashtewari merged commit 678e826 into elastic:main Jul 28, 2022
@yashtewari yashtewari deleted the fix-k8s-object-rules-tests branch July 28, 2022 14:28
mergify Bot pushed a commit that referenced this pull request Aug 26, 2022
Most of the problems occur in resource-matching logic that tries to find the right ES event to compare the expected evaluation against, which is harder to do for K8S resources than other resource types.

Inserting a per-testcase UUID into the K8S object's metadata that is being tested allows for much simpler resource-matching than the recursion logic in the `dict_contains` utility.

(cherry picked from commit 678e826)
DaveSys911 pushed a commit that referenced this pull request Sep 5, 2022
Fix broken k8s_object_rules tests. (#302)

Most of the problems occur in resource-matching logic that tries to find the right ES event to compare the expected evaluation against, which is harder to do for K8S resources than other resource types.

Inserting a per-testcase UUID into the K8S object's metadata that is being tested allows for much simpler resource-matching than the recursion logic in the `dict_contains` utility.

(cherry picked from commit 678e826)

Co-authored-by: Yash Tewari <yashtewari1996@gmail.com>
Co-authored-by: David Natachanny <david.natachanny@elastic.co>
orouz pushed a commit to orouz/cloudbeat that referenced this pull request Sep 16, 2023
orestisfl pushed a commit to orestisfl/cloudbeat that referenced this pull request Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants