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

Refactor handling of content #148

Merged
merged 16 commits into from
Jun 17, 2021

Conversation

matejak
Copy link
Contributor

@matejak matejak commented Jun 4, 2021

This PR aims to process content in a way that is backend-agnostic, i.e. it can work for the automated kickstart installations as well as interactive ones. It is separated into multiple commits - most of them are small, but the main one is quite extensive.

The fetch is performed in a two-step manner that was present in the GUI code, when it is started in a separate thread, and then the processing code then waits for the fetching thread - this may be an overkill for a plain kickstart installation, but it doesn't cause any problems.
Interactions with the backend are performed by callbacks - at this time it is a possibility to report status, definition of what to do after fetching stops, and finally definition of how to handle errors by means of a function that accepts an exception instance.
The unified pipeline includes content fetching, fingerprint verification, file extraction and file identification.

A class containing a content set has been introduced - it contains a list of discovered files (fetched, present on the system, or extracted from an archive) that have been identified by the oscap scanner. That class allows to work with the content more easily.

@matejak matejak added this to the 1.2 milestone Jun 4, 2021
@pep8speaks
Copy link

pep8speaks commented Jun 4, 2021

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

Line 123:100: E501 line too long (101 > 99 characters)
Line 145:100: E501 line too long (114 > 99 characters)

Line 403:13: E125 continuation line with same indent as next logical line
Line 403:13: E128 continuation line under-indented for visual indent
Line 441:100: E501 line too long (103 > 99 characters)

Line 32:100: E501 line too long (101 > 99 characters)

Comment last updated at 2021-06-17 13:57:45 UTC

@openscap-ci
Copy link

Build finished.

1 similar comment
@openscap-ci
Copy link

Build finished.

@matejak matejak force-pushed the gathering_refactoring branch from 51b522f to 7abf7eb Compare June 7, 2021 14:35
@openscap-ci
Copy link

Build finished.

for net_prefix in data_fetch.NET_URL_PREFIXES)


class Model:
Copy link
Member

Choose a reason for hiding this comment

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

Please add documentation strings on classes and public methods.

self.content_uri_path = path
self.content_uri_scheme = scheme

def fetch_content(self, cert, what_if_fail):
Copy link
Member

Choose a reason for hiding this comment

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

What about merging it with fetch_files?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense now, but what if you would like to introduce support for fetching tailoring? Then, it would make sense to keep those separately.

hash_obj)
if digest != fingerprint:
log.error(
"File {dest_filename} failed integrity check - assumed a "
Copy link
Member

Choose a reason for hiding this comment

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

Missing f.

return content


class ObtainedContent:
Copy link
Member

Choose a reason for hiding this comment

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

I feel that it somehow overlaps with OSCAPData class (forms ks/oscap.py).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one aims to assist the gathered files discovery - files can be downloaded directly, or they can be extracted. This class enables a user to quickly understand what is available, and whether the current set of contents is usable for further processing.

def test_fetch_local(tmp_path):
source_path = pathlib.Path(__file__).absolute()
dest_path = tmp_path / "dest"
data_fetch.fetch_data("file://" + str(source_path), dest_path)
Copy link
Member

Choose a reason for hiding this comment

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

So if I am an user when I run the installation where do I put the files or how do I pass files to Anaconda?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can use this only if those files are already there. I can imagine usage when the content filename is not guessed properly (e.g. RHEL8 vs RHV), or when the installation image contains the content as a specific location.

for net_prefix in data_fetch.NET_URL_PREFIXES)


class Model:
Copy link
Member

Choose a reason for hiding this comment

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

Can we think of a better name?

"Couldn't find a valid datastream or a valid XCCDF-OVAL file tuple "
"among the available content")
raise content_handling.ContentHandlingError(msg)
if elligible_main_content[0]:
Copy link
Member

Choose a reason for hiding this comment

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

this perhaps require a comment or better unpacking the tuple on line into 2 variables that would have good names and changing the condition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

or one can avoid the tuple completely.

@openscap-ci
Copy link

Build finished.

@matejak matejak force-pushed the gathering_refactoring branch from 78b0c76 to 15db68c Compare June 9, 2021 14:43
@matejak matejak marked this pull request as ready for review June 9, 2021 14:43
@openscap-ci
Copy link

Build finished.

@jan-cerny jan-cerny self-assigned this Jun 10, 2021
@matejak matejak force-pushed the gathering_refactoring branch from 15db68c to f2a7550 Compare June 10, 2021 11:29
@openscap-ci
Copy link

Build finished.

1 similar comment
@openscap-ci
Copy link

Build finished.

@matejak matejak force-pushed the gathering_refactoring branch from d3d3802 to bd4d62e Compare June 10, 2021 15:06
@openscap-ci
Copy link

Build finished.

Copy link
Member

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

It mostly works well.

See the comments below.

Please check the unit tests, they don't pass for me.


class ContentBringer:
CONTENT_DOWNLOAD_LOCATION = pathlib.Path(common.INSTALLATION_CONTENT_DIR)
DEFAULT_CONTENT = f"{common.SSG_DIR}/{common.SSG_CONTENT}"
Copy link
Member

Choose a reason for hiding this comment

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

I would rename it to make it clear that it's the path to the datastream from the scap-security-guide RPM, eg. DEFAULT_SSG_DATA_STREAM_PATH.

Args:
what_if_fail: Callback accepting exception as an argument that
should handle them in the calling layer.
cert: HTTPS certificates
Copy link
Member

Choose a reason for hiding this comment

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

certificates paths? names?

msg = f"Couldn't find '{content_path}' among the available content"
else:
msg = (
"File '{content_path}' is not a valid datastream "
Copy link
Member

Choose a reason for hiding this comment

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

missing f

Comment on lines +290 to +291
f"When dealing with {attribute_name}, "
f"there was already the {old_value.name} when setting the new {new_value.name}")
Copy link
Member

Choose a reason for hiding this comment

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

If I provide an RPM containing multiple SCAP source data streams this causes the UI to crash. It's expected to fail in this situation, but it shouldn't crash.

However, providing https://github.com/OpenSCAP/oscap-anaconda-addon/blob/rhel8-branch/testing_files/separate-scap-files.zip causes the addon to crash as well because of CPE OVAL (ssg-rhel8-cpe-oval.xml) gets into a conflict with check OVAL (ssg-rhel8-oval.xml). I think it shouldn't happen, CPE OVAL checks can be a valid part of oscap evaluation.



def test_init_invalid_file_path():
with pytest.raises(ch.DataStreamHandlingError) as excinfo:
Copy link
Member

Choose a reason for hiding this comment

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

The DataStreamHandlingError has been removed by #147 together with some other stuff from content_handling.py which were replaced by SCAPContentHandler class. It looks like this file had conflicts with #147 which should be resolved in a different way,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ouch.

progressQ.send_quit(1)
while True:
time.sleep(100000)
msg = ["Wrong configuration detected!"]
Copy link
Member

Choose a reason for hiding this comment

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

msgs would be a more descriptive name because this is a list

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another weird thing is the absence of translations.

org_fedora_oscap/gui/spokes/oscap.py Outdated Show resolved Hide resolved
@matejak matejak force-pushed the gathering_refactoring branch from 4ae7ab0 to cea80b8 Compare June 16, 2021 15:45
matejak added 11 commits June 17, 2021 11:01
That function has the same interface as the remote fetching one.
sudo is only needed when RPMs are installed.
Notably introduced the _terminate function that extracts
all the installation termination-related boilerplate.
The class unifies the content workflow between kickstart and GUI installations.
It provides methods to work with the content in non-interactive and interactive modes
using a system of callbacks.
Anaconda will want to perform fetch, but if we install via GUI,
a fetch has already been performed, and the addon should be able to
acknowledge the work that has been done on the GUI side.
- Added docstringsu
- Improved naming.
- Removed redundant arguments.
- Split large method into two smaller.
@matejak matejak force-pushed the gathering_refactoring branch from cea80b8 to 01e7afd Compare June 17, 2021 11:40
Copy link
Member

@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 have tried multiple different content from the testing_files directory and it seems to work. Great improvement.

I have only one small nitpick.


try:
if actually_fetched_content:
self.content_bringer.use_downloaded_content(content)
log.info(f"Opening SCAP content at {self._addon_data.preinst_content_path} with tailoring {self._addon_data.preinst_tailoring_path}")
Copy link
Member

Choose a reason for hiding this comment

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

I'm sorry but now I realized that self._addon_data.preinst_tailoring_path isn't set when tailoring isn't present so you would get: "Opening SCAP content at /tmp/bzfn with tailoring" in that situation which would be confusing for people reading the log.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed by a rebase.

matejak and others added 5 commits June 17, 2021 15:57
Co-authored-by: Jan Černý <jcerny@redhat.com>
Tests became obsolete because of earlier refactoring.
- Multiple OVAL files in an archive cause no longer an error.
- Multiple instances of other filetypes result in a content-related error.
- Unexpected errors don't crash the addon.
- mock is part of the Python standard library.
- scanner is used in unit tests.
@matejak matejak force-pushed the gathering_refactoring branch from 3c32b4d to 1732af6 Compare June 17, 2021 13:57
@scrutinizer-notifier
Copy link

A new inspection was created.

@scrutinizer-notifier
Copy link

The inspection completed: 41 updated code elements

Copy link
Member

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

amazing

@jan-cerny jan-cerny merged commit b7f2374 into OpenSCAP:rhel8-branch Jun 17, 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