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

Replace DataStreamHandler and BenchmarkHandler #147

Merged
merged 12 commits into from
Jun 9, 2021

Conversation

jan-cerny
Copy link
Member

The new class SCAPContentHandler can handle SCAP source data
streams, XCCDF files and tailoring files using common code.
The classes DataStreamHandler and BenchmarkHandler are removed.
The purpose of the new class is the same as of the removed
classes. The purpose is to get a list of profiles (their IDs
and descriptions) to be able to show it in the GUI. It is
used only in the GUI.

The new class SCAPContentHandler can handle SCAP source data
streams, XCCDF files and tailoring files using common code.
The classes DataStreamHandler and BenchmarkHandler are removed.
The purpose of the new class is the same as of the removed
classes. The purpose is to get a list of profiles (their IDs
and descriptions) to be able to show it in the GUI. It is
used only in the GUI.
@pep8speaks
Copy link

pep8speaks commented Jun 4, 2021

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

Line 71:1: E302 expected 2 blank lines, found 1

Comment last updated at 2021-06-09 13:53:09 UTC

@openscap-ci
Copy link

Build finished.

@jan-cerny
Copy link
Member Author

jan-cerny commented Jun 7, 2021

I'm thinking how to test it and I have already found multiple combinations that we need to test:

a. scap-security-guide
b. data stream
c. RPM cotaining data stream
d. RPM cotaining data stream + tailoring
e. rpm containing XCCDF
f. rpm containing XCCDF + tailoring
g. archive containing data stream
h. archive containing data stream + tailoring
i. archive containing XCCDF
j. archive containing XCCDF + tailoring

all of the situations need to be tested in both with and without kickstart

These classes are used only in GUI so I think we don't need to test the text mode.

jan-cerny added 3 commits June 7, 2021 17:52
Let users choose from profiles from both tailoring and the main
SCAP content file. This is useful for content from an archive
or a RPM file.
@openscap-ci
Copy link

Build finished.

@jan-cerny jan-cerny marked this pull request as ready for review June 8, 2021 11:25
Copy link
Contributor

@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.

I have pointed out small improvements, and an issue when the profile update goes wrong.

elif benchmark.tag.startswith(f"{{{ns['xccdf-1.2']}}}"):
xccdf_ns_prefix = "xccdf-1.2"
else:
raise SCAPContentHandlerError("Unsupported XML namespace")
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be nice to indicate what namespace is unsupported.

self._data_stream_id = data_stream_id
self._checklist_id = checklist_id

def get_profiles(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Those nested ifs are confusing. I propose to restructure it in the following way:

Firs identify all pathological cases, throw all the easy errors - bad XCCDF, standalone tailoring or unknown content.
Then, there is the trivial benchmark extraction from XCCDF, and multi-step benchmark extraction from a datastream, that I propose to extract to another method.

self.file_path = file_path
tree = ET.parse(file_path)
self.root = tree.getroot()
if tailoring_file_path is not None:
Copy link
Contributor

Choose a reason for hiding this comment

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

You can reduce to a Pythonic if not tailoring_file_path, which would handle empty strings as well, and you could therefore streamline some of the higher-level code.

Comment on lines 442 to 445
if self._addon_data.preinst_tailoring_path:
tailoring_path = self._addon_data.preinst_tailoring_path
else:
tailoring_path = None
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be streamlined - see another comment below.

title="Default",
description="The implicit XCCDF profile. Usually, the default profile "
"contains no rules.")
assert pinfo1 in profiles
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a newline, text files that don't end by a newline are considered dubious.

else:
# pylint: disable-msg=E1103
profiles = self._content_handler.profiles
profiles = self._content_handler.get_profiles()
Copy link
Contributor

Choose a reason for hiding this comment

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

This can lead to exceptions, so what about dispatching a logging message that something went wrong with the update of profiles, and pass the exception on?
The previous behavior was equally bad about this, and any exception would probably kill the addon, so I wouldn't concentrate on this aspect in this PR.

jan-cerny added 6 commits June 9, 2021 14:30
This way will allow us to emit e more descriptive error message.
The code that extracts the pointer to root element of an XCCDF
Benchmark has been extracted out to a new private method
`_parse_profiles_from_xccdf`. Also, the conditions in get_profiles()
have been reorganized so that the function now checks for all possible
errors and proceeds only when the checks succeed.
@openscap-ci
Copy link

Build finished.

Addressing:
The variable benchmark does not seem to be defined for all execution paths.
@openscap-ci
Copy link

Build finished.

@scrutinizer-notifier
Copy link

The inspection completed: 23 updated code elements

@matejak
Copy link
Contributor

matejak commented Jun 9, 2021

The handler works as expected, and it is also much faster than the previous implementation. Great work, thanks!

@matejak matejak merged commit ff1c74d into OpenSCAP:rhel8-branch Jun 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants