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

Prepare OVAL object model for integration #11206

Merged

Conversation

Honny1
Copy link
Collaborator

@Honny1 Honny1 commented Oct 16, 2023

This PR adds and enhances the OVAL object model capabilities for integration into combine_ovals.py.

New capabilities:

  • Finding references to OVAL components
  • Remove OVAL definitions with all referenced OVAL components (preserves components used in other OVAL components).
  • Check if the OVAL definition is applicable to the product.
  • Check that the Affected attribute in the metadata is not empty.
  • Add error logging or other warnings

Enhanced capabilities:

  • Load from shorthands has been redesigned.
  • Fixed fields in the element generator in the generated OVAL document.

Review Hints:

Test script

test.py

Execution of test script:

python3 test.py

This test script generates one document for each OVAL definition with all referenced OVAL components and then performs validation using oscap oval validate FILE_PATH, but ignores validation for the ocp and eks products.

Note: the test script processes all OVAL documents in the build directory.

Unit tests

Run unit tests:

tox

@openshift-ci openshift-ci bot added the do-not-merge/work-in-progress Used by openshift-ci bot. label Oct 16, 2023
@openshift-ci
Copy link

openshift-ci bot commented Oct 16, 2023

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@github-actions
Copy link

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

Fedora Environment
Open in Gitpod

Oracle Linux 8 Environment
Open in Gitpod

@Honny1 Honny1 force-pushed the integration-of-OVAL-Object-mode branch from 4b54a04 to 853d46b Compare October 16, 2023 16:41
@Honny1 Honny1 added enhancement General enhancements to the project. OVAL OVAL update. Related to the systems assessments. New Feature Issues or pull requests related to new Features. labels Oct 16, 2023
@Honny1 Honny1 force-pushed the integration-of-OVAL-Object-mode branch 4 times, most recently from f3913d4 to f6fbece Compare October 16, 2023 17:14
@jan-cerny
Copy link
Collaborator

@Honny1 What is the intention behind OVALContainer and OVALShorthand classes?

@Honny1
Copy link
Collaborator Author

Honny1 commented Oct 17, 2023

@jan-cerny OVALContainer is the parent class of the OVALDocument and OVALShorthand classes. OVALShorthand represents a shorthand with OVAL definitions.

@jan-cerny
Copy link
Collaborator

@Honny1 Cool, that sounds natural. But, I'm curious about their purpose. Won't the shorthand format be parsed and converted to objects?

@Honny1 Honny1 force-pushed the integration-of-OVAL-Object-mode branch 9 times, most recently from 5ccd6e8 to d71e097 Compare October 17, 2023 20:03
@jan-cerny jan-cerny self-assigned this Oct 18, 2023
@Honny1
Copy link
Collaborator Author

Honny1 commented Oct 18, 2023

/test all

@Honny1 Honny1 force-pushed the integration-of-OVAL-Object-mode branch 3 times, most recently from c31c2e6 to a6cdb3f Compare October 18, 2023 14:03
@Honny1
Copy link
Collaborator Author

Honny1 commented Oct 18, 2023

@jan-cerny Yes, OVALShorthand is an object representation for shorthand.

@jan-cerny
Copy link
Collaborator

@Honny1 And why do we need this representation? Feel free to reply in a more detailed way.

@Honny1
Copy link
Collaborator Author

Honny1 commented Oct 23, 2023

@jan-cerny When loading shortcuts, you can load a shortcut containing several OVAL definitions. One of them may be applicable for a given platform and the other may not. For this reason, I decided to load the entire shortcut into an OVALShorthand object and then remove the unusable OVAL definition. The OVALShordhand inherits capabilities from OVALContainer. For example, look up references in an OVAL definition and then just preserve all referenced components of the OVAL definition. These capabilities will also be used to slim down OVAL documents for lightweight DS.

@Honny1 Honny1 force-pushed the integration-of-OVAL-Object-mode branch from a6cdb3f to 8efe315 Compare October 23, 2023 16:30
return "external_variable" in component.tag


def _handle_existing_id(component, component_dict):
Copy link
Collaborator

Choose a reason for hiding this comment

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

This function looks very similar to a part of append function in ssg/build_ovals.py. You should first extract the common code out to a function and then call that function both here and there to prevent unwanted code duplication and responsibility duplication.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In the next PR, I plan to rework ssg/build_ovals.py and combine_ovals.py and remove some duplicated and unused code. So this duplication will be resolved.



def _handle_existing_id(component, component_dict):
# ID is identical, but OVAL entities are semantically difference =>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# ID is identical, but OVAL entities are semantically difference =>
# ID is identical, but OVAL entities are semantically different =>

Comment on lines 37 to 39
if value is None:
return True
return False
Copy link
Collaborator

Choose a reason for hiding this comment

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

This could be simplified to return value is None.

Comment on lines 163 to 164
"ERROR: A definition that does not match the rule ID '{}'"
" is not present in the shorthand."
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm confused by this. Should the "does not" be there?

@@ -0,0 +1,51 @@
import pytest

from test_load_and_store import _load_oval_document, OVAL_DOCUMENT_PATH
Copy link
Collaborator

Choose a reason for hiding this comment

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

A small nit pick: I think that the tests are somehow exceptional and it actually could be more readable if the path is redefined here in this file instead of importing the constant from other unit test file.

@Honny1 Honny1 force-pushed the integration-of-OVAL-Object-mode branch 2 times, most recently from 265ea7a to 7ccbeea Compare October 24, 2023 12:41
@Honny1
Copy link
Collaborator Author

Honny1 commented Oct 25, 2023

/packit retest-failed

@Honny1 Honny1 force-pushed the integration-of-OVAL-Object-mode branch from 7ccbeea to 51c55f5 Compare October 25, 2023 09:08
@Honny1 Honny1 force-pushed the integration-of-OVAL-Object-mode branch from 51c55f5 to 5eb2824 Compare October 25, 2023 14:04
@codeclimate
Copy link

codeclimate bot commented Oct 25, 2023

Code Climate has analyzed commit 5eb2824 and detected 0 issues on this pull request.

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

This pull request will bring the total coverage in the repository to 58.4%.

View more on Code Climate.

@Honny1 Honny1 marked this pull request as ready for review October 25, 2023 14:23
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Used by openshift-ci bot. label Oct 25, 2023
@Honny1 Honny1 requested a review from jan-cerny October 25, 2023 14:23
@jan-cerny jan-cerny added this to the 0.1.71 milestone Oct 25, 2023
Copy link
Collaborator

@jan-cerny jan-cerny left a comment

Choose a reason for hiding this comment

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

I hav found that the feedback from previous drive-by's has been addressed, but I'm sad that there are no PR comments that would make this situation transparent for everyone.

I can see that almost all the changes are in the oval_object_model module and the unit tests for the oval_object_model module. At this moment, the oval_object_model module isn't used anywhere in the build system. The only changes outside of this that indeed affect our build system are small refactoring changes in ssg/build_ovals.py and ssg/utils.py that is the extraction of code to the get_fixed_product_version function. From this point of view, the changes are safe for the project. However, I'm missing this information in the PR description. Also, I would prefer if this specific refactoring change would be submitted as a separate PR.

I have played with the attached test.py script. I have noticed that it assumes the existence of the ./build/single_ovals/ directory which isn't created by that. But if I create this directory manually, it creates the expected OVAL files.

Overall, I evaluate the contents of this PR as a good preparation for further steps. Thanks for having the unit tests!

@jan-cerny jan-cerny merged commit 246f43d into ComplianceAsCode:master Oct 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement General enhancements to the project. New Feature Issues or pull requests related to new Features. OVAL OVAL update. Related to the systems assessments.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants