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

Ground work for implementation of CPE applicability language #7613

Merged
merged 31 commits into from
Nov 1, 2021

Conversation

vojtapolasek
Copy link
Collaborator

@vojtapolasek vojtapolasek commented Sep 22, 2021

Description:

  • create new classes for CPEAL platform-specification, platform, logical-test fact-ref
  • implement parsing mechanism for more complex platform specification (it will probably change in follow-up PR):
    • OR is done by using separate lines
    • and is done by using & in a line
    • negation is done with ! preceding the platform name
  • the new functionality is added into CPEProduct class
  • the XCCDF document now contains cpe-lang:platform-specification with all the platforms
  • platforms are currently using fact-ref, which points to the good old cpe name we used before. This is done partially to prevent big breakage, but mainly because xccdf 1.1 does not support high enough version of CPE applicability language to use check-fact-ref directly
  • groups and rules already use these new platforms, profiles and benchmarks not yet
  • the goal is to just move from cpe-names to CPE platforms, no changes to applicability

@vojtapolasek vojtapolasek added Infrastructure Our content build system usability Enhancements related to usability. Highlight This PR/Issue should make it to the featured changelog. labels Sep 22, 2021
@vojtapolasek vojtapolasek added this to the 0.1.59 milestone Sep 22, 2021
@openshift-ci
Copy link

openshift-ci bot commented Sep 22, 2021

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

@openshift-ci openshift-ci bot added the do-not-merge/work-in-progress Used by openshift-ci bot. label Sep 22, 2021
@pep8speaks
Copy link

pep8speaks commented Sep 22, 2021

Hello @vojtapolasek! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 141:38: E231 missing whitespace after ':'

Line 215:5: E303 too many blank lines (2)
Line 361:1: E302 expected 2 blank lines, found 1
Line 373:1: E302 expected 2 blank lines, found 1
Line 377:1: E302 expected 2 blank lines, found 1

Comment last updated at 2021-11-01 10:14:26 UTC

build-scripts/cpe_generate.py Outdated Show resolved Hide resolved
ssg/build_cpe.py Outdated Show resolved Hide resolved
ssg/build_cpe.py Outdated Show resolved Hide resolved
Copy link
Member

@yuumasato yuumasato left a comment

Choose a reason for hiding this comment

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

I know the PR is WIP, but just mentioning in case you are not aware:
When an XCCDF item references a CPEALPlatform, the ID in @id should be prefixed by #.
E.g.: <xccdf-1.2:platform idref="cpe_platform_machine"/>
Ref: XCCDF 6.2.5

ssg/build_cpe.py Outdated Show resolved Hide resolved
ssg/build_yaml.py Outdated Show resolved Hide resolved
ssg/build_cpe.py Outdated Show resolved Hide resolved
ssg/build_cpe.py Outdated
initial_test.add_object(self.parse_platform_line(platform_line))
return platform

def parse_platform_line(self, platform_line):
Copy link
Member

Choose a reason for hiding this comment

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

This function assumes that a platform_line has a single operator.
It should be possible to have platform_line like 'A & B & !C`.

Copy link
Contributor

Choose a reason for hiding this comment

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

uhm... instead of trying to come up with a custom parser function... have you thought about building a simple recursive descent parser? Having a simple grammar definition might also be nice documentation for this.

I found a simple parser here: https://gist.github.com/iafisher/6f53ce4df29c91ac9276c13a113ccf9f

And the grammar (in kinda EBNF form) could look something like:

rule → CPE & rule
rule → !CPE
rule → CPE

Doing it this way would allow for building more complex entries such as the ones Yuuma is describing in his comment... And since these types of parsers are fairly common, it might even help in readability/maintenance as folks would know what to expect.

Adding to my previous comment about readable operands, using and and not as opposed to & and ! would also not incur much complexity, as they would be merely tokens in the parser which you could easily swap.

Copy link
Member

Choose a reason for hiding this comment

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

BTW as you have already introduced the platform filter for profiles, I think that the CPE applicability may reuse that filter format.

@yuumasato
Copy link
Member

The prefix CPEAL seems a bit too long for me, how about shortening it to just CPE?

@JAORMX
Copy link
Contributor

JAORMX commented Sep 29, 2021

Will this PR include docs on the expression format? If not, could you post it as part of this PR's description? That would help me review this as well. I'm hoping to take this into use as soon as it lands.

@vojtapolasek
Copy link
Collaborator Author

Yes I plan to include documentation, I added it into the "what needs to be done" section.

JAORMX
JAORMX previously requested changes Oct 14, 2021
ssg/build_cpe.py Outdated
def _convert_platform_to_id(self, platform):
id = platform.replace(" ", "")
id = id.replace("&", "_and_")
id = id.replace("!", "not_")
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of using C-style operands, why not use human-readable ones already? As we're already doing with filter_rules? That would provide some consistency and also would make this easier to read for non-programmers.

ssg/build_cpe.py Outdated
initial_test.add_object(self.parse_platform_line(platform_line))
return platform

def parse_platform_line(self, platform_line):
Copy link
Contributor

Choose a reason for hiding this comment

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

uhm... instead of trying to come up with a custom parser function... have you thought about building a simple recursive descent parser? Having a simple grammar definition might also be nice documentation for this.

I found a simple parser here: https://gist.github.com/iafisher/6f53ce4df29c91ac9276c13a113ccf9f

And the grammar (in kinda EBNF form) could look something like:

rule → CPE & rule
rule → !CPE
rule → CPE

Doing it this way would allow for building more complex entries such as the ones Yuuma is describing in his comment... And since these types of parsers are fairly common, it might even help in readability/maintenance as folks would know what to expect.

Adding to my previous comment about readable operands, using and and not as opposed to & and ! would also not incur much complexity, as they would be merely tokens in the parser which you could easily swap.

@openshift-ci openshift-ci bot added the needs-rebase Used by openshift-ci bot. label Oct 15, 2021
@vojtapolasek vojtapolasek changed the title WIP: implement support for CPE applicability language Ground work for implementation of CPE applicability language Oct 19, 2021
@vojtapolasek
Copy link
Collaborator Author

Thank you everyone for suggestions. The grammar parser and more human readable syntax sound really good. Due to recent changes and overall complexity of the change, I decided to steer this PR in a bit different direction.
I plan to achieve the same applicability as we have now, but expressed with CPE platforms instead of cpe-names. This will be a base for further changes in follow-up pull requests.

@openshift-ci openshift-ci bot removed the needs-rebase Used by openshift-ci bot. label Oct 19, 2021
@vojtapolasek vojtapolasek marked this pull request as ready for review October 20, 2021 08:50
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Used by openshift-ci bot. label Oct 20, 2021
Copy link
Member

@yuumasato yuumasato left a comment

Choose a reason for hiding this comment

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

I have just a small request to de-duplicate code.

Otherwise looks good, tests are passing and I was able to do scan/remediation.

ns = PREFIX_TO_NS[prefix]

def __init__(self):
self.platforms = []
Copy link
Member

Choose a reason for hiding this comment

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

I think platforms could be a set().

ssg/build_yaml.py Outdated Show resolved Hide resolved
ssg/build_yaml.py Outdated Show resolved Hide resolved
ssg/build_yaml.py Outdated Show resolved Hide resolved
ssg/build_yaml.py Outdated Show resolved Hide resolved
Copy link
Member

@matejak matejak left a comment

Choose a reason for hiding this comment

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

One issue that I see with this PR is that it introduces changes to the build system in form of code that is not used with the current content, and it is not tested either.
I have noticed commits that suggest that they "fix" something, but how would one noticed that something was broken?

I think that unit tests of newly introduced classes are easy to write and they would also define the intended behavior.

@yuumasato
Copy link
Member

One issue that I see with this PR is that it introduces changes to the build system in form of code that is not used with the current content, and it is not tested either.

I think they are used, the Rule platforms now reference the CPE platforms <xccdf-1.2:platform idref="#cpe_platform_machine"/>, :)

I have noticed commits that suggest that they "fix" something, but how would one noticed that something was broken?

From what I understood most of the commits with "fix something" are addressing issues added by the PR itself.

I think that unit tests of newly introduced classes are easy to write and they would also define the intended behavior.

I agree

@vojtapolasek
Copy link
Collaborator Author

I will try to add unit tests as well. That is a good point. I think that the only introduced part of the code which is not used is the part which does some complex parsing of platform expressions. This will be changed in subsequent pull requests. So I don't plan to write tests for this part of the code.

@openshift-ci openshift-ci bot added the needs-rebase Used by openshift-ci bot. label Oct 21, 2021
@openshift-ci
Copy link

openshift-ci bot commented Oct 21, 2021

@vojtapolasek: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/e2e-aws-ocp4-pci-dss-node 6b4d852 link true /test e2e-aws-ocp4-pci-dss-node
ci/prow/e2e-aws-ocp4-pci-dss 6b4d852 link true /test e2e-aws-ocp4-pci-dss

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

they are not used now and they could cause confusion
replace cpe_al_* with cpe_*
substrings were not replaced
for profiles I am still waiting
rework comparing of platformspecifications, logicaltests and factrefs
when dumping logicaltest into xml, nested logicaltests must go first and then factrefs
this step was not done in all situations and it caused datastream contain references to not defined platforms
it is not dependent on ProductCPE class
the logic might come back in future improvements
@matejak
Copy link
Member

matejak commented Nov 1, 2021

This PR introduces the SCAP-side support for more complex rule applicability by means of the CPE Applicability Language.

I have proposed to the code that was originally present in this PR that bridged this new functionality with rule definitions. This is a problem that can be addressed separately, and that is, in itself, quite complex, as it has to appeal to content authors, and it has to be usable by remediations etc.

Therefore, the current PR doesn't contain code that is suitable for unit-testing - the ability to express a complex platform definition in XML can be tested by means of system tests using a scanner, whereas the ability to extract a platform definition from a rule yaml into its intermediate representation will need unit tests.

The machine platform applicability definition in rules/groups has changed,
so the test suite had to be extended accordingly.
@matejak
Copy link
Member

matejak commented Nov 1, 2021

I am in favor of merging the PR. One set of suggestions became not applicable because the scope has been reduced, while others are still somehow relevant, but the discussion around them got silent, and they can be addressed subsequently.
Thank you for the work!

@matejak matejak dismissed JAORMX’s stale review November 1, 2021 10:56

Suggestions became not applicable because the scope has been reduced. They will be relevant in another PRs.

@yuumasato yuumasato merged commit 94b58f5 into ComplianceAsCode:master Nov 1, 2021
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Highlight This PR/Issue should make it to the featured changelog. Infrastructure Our content build system usability Enhancements related to usability.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants