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

Populate _rule_id virtual template parameter in Automatus #11943

Merged
merged 1 commit into from
May 6, 2024

Conversation

jan-cerny
Copy link
Collaborator

Templates have an implicit (virtual) parameter _rule_id that contains rule ID of the currently processed rule. This parameter is added by the build system and is passed to template preprocessing function. However, this parameter isn't set in when invoking the templating subsystem Automatus when resolving templated TSs. This causes some of the template preprocessing functions to traceback when testing templated rules in Automatus.

Addressing:

Traceback (most recent call last):
  File "/home/jcerny/work/git/scap-security-guide/tests/automatus.py", line 518, in <module>
    main()
  File "/home/jcerny/work/git/scap-security-guide/tests/automatus.py", line 514, in main
    options.func(options)
  File "/home/jcerny/work/git/scap-security-guide/tests/ssg_test_suite/rule.py", line 676, in perform_rule_check
    checker.test_target()
  File "/home/jcerny/work/git/scap-security-guide/tests/ssg_test_suite/oscap.py", line 685, in test_target
    self._test_target()
  File "/home/jcerny/work/git/scap-security-guide/tests/ssg_test_suite/rule.py", line 444, in _test_target
    test_content_by_rule_id = self._get_test_content_by_rule_id(
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jcerny/work/git/scap-security-guide/tests/ssg_test_suite/rule.py", line 427, in _get_test_content_by_rule_id
    rule_test_content = self._get_rule_test_content(rule)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jcerny/work/git/scap-security-guide/tests/ssg_test_suite/rule.py", line 409, in _get_rule_test_content
    all_tests = self._load_all_tests(rule)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jcerny/work/git/scap-security-guide/tests/ssg_test_suite/rule.py", line 399, in _load_all_tests
    templated_tests = common.load_templated_tests(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jcerny/work/git/scap-security-guide/tests/ssg_test_suite/common.py", line 494, in load_templated_tests
    test = load_test(path, template, local_env_yaml)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jcerny/work/git/scap-security-guide/tests/ssg_test_suite/common.py", line 506, in load_test
    template_parameters = maybe_template.preprocess(template_vars, "tests")
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jcerny/work/git/scap-security-guide/tests/../ssg/templates.py", line 95, in preprocess
    parameters = self._preprocess_with_template_module(parameters, lang)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jcerny/work/git/scap-security-guide/tests/../ssg/templates.py", line 112, in _preprocess_with_template_module
    parameters = preprocess_mod.preprocess(parameters.copy(), lang)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jcerny/work/git/scap-security-guide/shared/templates/audit_rules_watch/template.py", line 3, in preprocess
    data["key"] = data["_rule_id"]
                  ~~~~^^^^^^^^^^^^
KeyError: '_rule_id'

Fixes: #11940

Templates have an implicit (virtual) parameter `_rule_id` that
contains rule ID of the currently processed rule. This parameter
is added by the build system and is passed to template preprocessing
function. However, this parameter isn't set in when invoking the
templating subsystem Automatus when resolving templated TSs.
This causes some of the template preprocessing functions to traceback
when testing templated rules in Automatus.

Addressing:

```
Traceback (most recent call last):
  File "/home/jcerny/work/git/scap-security-guide/tests/automatus.py", line 518, in <module>
    main()
  File "/home/jcerny/work/git/scap-security-guide/tests/automatus.py", line 514, in main
    options.func(options)
  File "/home/jcerny/work/git/scap-security-guide/tests/ssg_test_suite/rule.py", line 676, in perform_rule_check
    checker.test_target()
  File "/home/jcerny/work/git/scap-security-guide/tests/ssg_test_suite/oscap.py", line 685, in test_target
    self._test_target()
  File "/home/jcerny/work/git/scap-security-guide/tests/ssg_test_suite/rule.py", line 444, in _test_target
    test_content_by_rule_id = self._get_test_content_by_rule_id(
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jcerny/work/git/scap-security-guide/tests/ssg_test_suite/rule.py", line 427, in _get_test_content_by_rule_id
    rule_test_content = self._get_rule_test_content(rule)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jcerny/work/git/scap-security-guide/tests/ssg_test_suite/rule.py", line 409, in _get_rule_test_content
    all_tests = self._load_all_tests(rule)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jcerny/work/git/scap-security-guide/tests/ssg_test_suite/rule.py", line 399, in _load_all_tests
    templated_tests = common.load_templated_tests(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jcerny/work/git/scap-security-guide/tests/ssg_test_suite/common.py", line 494, in load_templated_tests
    test = load_test(path, template, local_env_yaml)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jcerny/work/git/scap-security-guide/tests/ssg_test_suite/common.py", line 506, in load_test
    template_parameters = maybe_template.preprocess(template_vars, "tests")
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jcerny/work/git/scap-security-guide/tests/../ssg/templates.py", line 95, in preprocess
    parameters = self._preprocess_with_template_module(parameters, lang)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jcerny/work/git/scap-security-guide/tests/../ssg/templates.py", line 112, in _preprocess_with_template_module
    parameters = preprocess_mod.preprocess(parameters.copy(), lang)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jcerny/work/git/scap-security-guide/shared/templates/audit_rules_watch/template.py", line 3, in preprocess
    data["key"] = data["_rule_id"]
                  ~~~~^^^^^^^^^^^^
KeyError: '_rule_id'
```

Fixes: ComplianceAsCode#11940
@jan-cerny jan-cerny added the productization-issue Issue found in upstream stabilization process. label May 6, 2024
Copy link

github-actions bot commented May 6, 2024

Start a new ephemeral environment with changes proposed in this pull request:

Fedora Environment
Open in Gitpod

Oracle Linux 8 Environment
Open in Gitpod

Copy link

github-actions bot commented May 6, 2024

🤖 A k8s content image for this PR is available at:
ghcr.io/complianceascode/k8scontent:11943
This image was built from commit: 8df9cbc

Click here to see how to deploy it

If you alread have Compliance Operator deployed:
utils/build_ds_container.py -i ghcr.io/complianceascode/k8scontent:11943

Otherwise deploy the content and operator together by checking out ComplianceAsCode/compliance-operator and:
CONTENT_IMAGE=ghcr.io/complianceascode/k8scontent:11943 make deploy-local

Copy link

codeclimate bot commented May 6, 2024

Code Climate has analyzed commit 8df9cbc and detected 0 issues on this pull request.

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 59.4% (0.0% change).

View more on Code Climate.

@jan-cerny jan-cerny added the Test Suite Update in Test Suite. label May 6, 2024
@Mab879 Mab879 self-assigned this May 6, 2024
Copy link
Member

@Mab879 Mab879 left a comment

Choose a reason for hiding this comment

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

Thanks for this PR.

I confirmed that this PR fixes the issue.

@Mab879 Mab879 merged commit b241dc8 into ComplianceAsCode:master May 6, 2024
113 checks passed
@Mab879 Mab879 added this to the 0.1.74 milestone May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
productization-issue Issue found in upstream stabilization process. Test Suite Update in Test Suite.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

audit_rules_networkconfig_modification_network_scripts is broken in Automatus
2 participants