Blame SOURCES/oscap-anaconda-addon-1.3.0-fix_content_paths-PR_225.patch

ad1461
From 08d3da5640e5c16cda4e79cc13ac7921f1ebd964 Mon Sep 17 00:00:00 2001
ad1461
From: Matej Tyc <matyc@redhat.com>
ad1461
Date: Tue, 15 Nov 2022 15:37:28 +0100
ad1461
Subject: [PATCH 1/2] Fix handling of content paths
ad1461
ad1461
Archives and ready-to-use content use paths differently.
ad1461
ad1461
Archives get unpacked into a directory, where they need to be unpacked,
ad1461
analyzed, and cross-checked with e.g. the supplied content path,
ad1461
whereas ready-to-use content can be used directly.
ad1461
ad1461
As the current codebase doesn't untangle all possible ways how to obtain
ad1461
existing content in a way of decomposing those into layers, this change
ad1461
just makes the current code working at the expense of making it worse to
ad1461
maintain.
ad1461
---
ad1461
 org_fedora_oscap/content_discovery.py | 34 ++++++++++++++++++---------
ad1461
 org_fedora_oscap/ks/oscap.py          |  6 ++++-
ad1461
 tests/test_content_discovery.py       | 21 +++++++++++++++++
ad1461
 3 files changed, 49 insertions(+), 12 deletions(-)
ad1461
ad1461
diff --git a/org_fedora_oscap/content_discovery.py b/org_fedora_oscap/content_discovery.py
ad1461
index e9cf34a..2b71b1f 100644
ad1461
--- a/org_fedora_oscap/content_discovery.py
ad1461
+++ b/org_fedora_oscap/content_discovery.py
ad1461
@@ -25,6 +25,14 @@ def is_network(scheme):
ad1461
         for net_prefix in data_fetch.NET_URL_PREFIXES)
ad1461
 
ad1461
 
ad1461
+def path_is_present_among_paths(path, paths):
ad1461
+    absolute_path = os.path.abspath(path)
ad1461
+    for second_path in paths:
ad1461
+        if absolute_path == os.path.abspath(second_path):
ad1461
+            return True
ad1461
+    return False
ad1461
+
ad1461
+
ad1461
 class ContentBringer:
ad1461
     CONTENT_DOWNLOAD_LOCATION = pathlib.Path(common.INSTALLATION_CONTENT_DIR)
ad1461
     DEFAULT_SSG_DATA_STREAM_PATH = f"{common.SSG_DIR}/{common.SSG_CONTENT}"
ad1461
@@ -170,7 +178,7 @@ def _verify_fingerprint(self, dest_filename, fingerprint=""):
ad1461
             raise content_handling.ContentCheckError(msg)
ad1461
 
ad1461
     def allow_one_expected_tailoring_or_no_tailoring(self, labelled_files):
ad1461
-        expected_tailoring = self._addon_data.tailoring_path
ad1461
+        expected_tailoring = self._addon_data.preinst_tailoring_path
ad1461
         tailoring_label = CONTENT_TYPES["TAILORING"]
ad1461
         if expected_tailoring:
ad1461
             labelled_files = self.reduce_files(labelled_files, expected_tailoring, [tailoring_label])
ad1461
@@ -182,7 +190,7 @@ def allow_one_expected_tailoring_or_no_tailoring(self, labelled_files):
ad1461
         return labelled_files
ad1461
 
ad1461
     def filter_discovered_content(self, labelled_files):
ad1461
-        expected_path = self._addon_data.content_path
ad1461
+        expected_path = self._addon_data.preinst_content_path
ad1461
         categories = (CONTENT_TYPES["DATASTREAM"], CONTENT_TYPES["XCCDF_CHECKLIST"])
ad1461
         if expected_path:
ad1461
             labelled_files = self.reduce_files(labelled_files, expected_path, categories)
ad1461
@@ -198,7 +206,7 @@ def filter_discovered_content(self, labelled_files):
ad1461
 
ad1461
     def reduce_files(self, labelled_files, expected_path, categories):
ad1461
         reduced_files = dict()
ad1461
-        if expected_path not in labelled_files:
ad1461
+        if not path_is_present_among_paths(expected_path, labelled_files.keys()):
ad1461
             msg = (
ad1461
                 f"Expected a file {expected_path} to be part of the supplied content, "
ad1461
                 f"but it was not the case, got only {list(labelled_files.keys())}"
ad1461
@@ -225,13 +233,9 @@ def _finish_actual_fetch(self, wait_for, fingerprint, report_callback, dest_file
ad1461
             structured_content.add_content_archive(dest_filename)
ad1461
 
ad1461
         labelled_filenames = content_handling.identify_files(fpaths)
ad1461
-        labelled_relative_filenames = {
ad1461
-            os.path.relpath(path, self.CONTENT_DOWNLOAD_LOCATION): label
ad1461
-            for path, label in labelled_filenames.items()}
ad1461
-        labelled_relative_filenames = self.filter_discovered_content(labelled_relative_filenames)
ad1461
+        labelled_filenames = self.filter_discovered_content(labelled_filenames)
ad1461
 
ad1461
-        for rel_fname, label in labelled_relative_filenames.items():
ad1461
-            fname = self.CONTENT_DOWNLOAD_LOCATION / rel_fname
ad1461
+        for fname, label in labelled_filenames.items():
ad1461
             structured_content.add_file(str(fname), label)
ad1461
 
ad1461
         if fingerprint and dest_filename:
ad1461
@@ -274,11 +278,18 @@ def use_downloaded_content(self, content):
ad1461
         # We know that we have ended up with a datastream-like content,
ad1461
         # but if we can't convert an archive to a datastream.
ad1461
         # self._addon_data.content_type = "datastream"
ad1461
-        self._addon_data.content_path = str(preferred_content.relative_to(content.root))
ad1461
+        content_type = self._addon_data.content_type
ad1461
+        if content_type in ("archive", "rpm"):
ad1461
+            self._addon_data.content_path = str(preferred_content.relative_to(content.root))
ad1461
+        else:
ad1461
+            self._addon_data.content_path = str(preferred_content)
ad1461
 
ad1461
         preferred_tailoring = self.get_preferred_tailoring(content)
ad1461
         if content.tailoring:
ad1461
-            self._addon_data.tailoring_path = str(preferred_tailoring.relative_to(content.root))
ad1461
+            if content_type in ("archive", "rpm"):
ad1461
+                self._addon_data.tailoring_path = str(preferred_tailoring.relative_to(content.root))
ad1461
+            else:
ad1461
+                self._addon_data.tailoring_path = str(preferred_tailoring)
ad1461
 
ad1461
     def use_system_content(self, content=None):
ad1461
         self._addon_data.clear_all()
ad1461
@@ -372,6 +383,7 @@ def _xccdf_content(self):
ad1461
 
ad1461
     def find_expected_usable_content(self, relative_expected_content_path):
ad1461
         content_path = self.root / relative_expected_content_path
ad1461
+        content_path = content_path.resolve()
ad1461
         eligible_main_content = (self._datastream_content(), self._xccdf_content())
ad1461
 
ad1461
         if content_path in eligible_main_content:
ad1461
diff --git a/org_fedora_oscap/ks/oscap.py b/org_fedora_oscap/ks/oscap.py
ad1461
index dac273d..7d4a131 100644
ad1461
--- a/org_fedora_oscap/ks/oscap.py
ad1461
+++ b/org_fedora_oscap/ks/oscap.py
ad1461
@@ -179,7 +179,11 @@ def _parse_profile_id(self, value):
ad1461
         self.profile_id = value
ad1461
 
ad1461
     def _parse_content_path(self, value):
ad1461
-        # need to be checked?
ad1461
+        if self.content_type in ("archive", "rpm") and os.path.isabs(self.content_path):
ad1461
+            msg = (
ad1461
+                "When using archives-like content input, the corresponding content path "
ad1461
+                "has to be relative, but got '{self.content_path}'.")
ad1461
+            raise KickstartValueError(msg)
ad1461
         self.content_path = value
ad1461
 
ad1461
     def _parse_cpe_path(self, value):
ad1461
diff --git a/tests/test_content_discovery.py b/tests/test_content_discovery.py
ad1461
index 5463c9a..d6e14d9 100644
ad1461
--- a/tests/test_content_discovery.py
ad1461
+++ b/tests/test_content_discovery.py
ad1461
@@ -1,3 +1,5 @@
ad1461
+import os
ad1461
+
ad1461
 import pytest
ad1461
 
ad1461
 import org_fedora_oscap.content_discovery as tested_module
ad1461
@@ -46,3 +48,22 @@ def test_reduce(labelled_files):
ad1461
 
ad1461
     reduced = bringer.reduce_files(labelled_files, "cpe", ["C"])
ad1461
     assert reduced == labelled_files
ad1461
+
ad1461
+
ad1461
+def test_path_presence_detection():
ad1461
+    list_of_paths = ["file1", os.path.abspath("file2"), os.path.abspath("dir///file3")]
ad1461
+
ad1461
+    list_of_paths_in_list = [
ad1461
+        "file1", os.path.abspath("file1"), "./file1",
ad1461
+        "file2", "dir/..//file2",
ad1461
+        "dir/../dir/file3", "dir/file3",
ad1461
+    ]
ad1461
+    list_of_paths_not_in_list = [
ad1461
+        "../file1", "file3"
ad1461
+    ]
ad1461
+
ad1461
+    for path in list_of_paths_in_list:
ad1461
+        assert tested_module.path_is_present_among_paths(path, list_of_paths)
ad1461
+
ad1461
+    for path in list_of_paths_not_in_list:
ad1461
+        assert not tested_module.path_is_present_among_paths(path, list_of_paths)
ad1461
ad1461
From 786ec5d90d12a1321fbff86f5d8d4a534059ad22 Mon Sep 17 00:00:00 2001
ad1461
From: Matej Tyc <matyc@redhat.com>
ad1461
Date: Wed, 16 Nov 2022 15:35:09 +0100
ad1461
Subject: [PATCH 2/2] Compare paths according to their equivalence
ad1461
ad1461
not according their arbitrary string form
ad1461
---
ad1461
 org_fedora_oscap/content_discovery.py | 8 ++++++--
ad1461
 1 file changed, 6 insertions(+), 2 deletions(-)
ad1461
ad1461
diff --git a/org_fedora_oscap/content_discovery.py b/org_fedora_oscap/content_discovery.py
ad1461
index 2b71b1f..42c61e0 100644
ad1461
--- a/org_fedora_oscap/content_discovery.py
ad1461
+++ b/org_fedora_oscap/content_discovery.py
ad1461
@@ -25,10 +25,14 @@ def is_network(scheme):
ad1461
         for net_prefix in data_fetch.NET_URL_PREFIXES)
ad1461
 
ad1461
 
ad1461
+def paths_are_equivalent(p1, p2):
ad1461
+    return os.path.abspath(p1) == os.path.abspath(p2)
ad1461
+
ad1461
+
ad1461
 def path_is_present_among_paths(path, paths):
ad1461
     absolute_path = os.path.abspath(path)
ad1461
     for second_path in paths:
ad1461
-        if absolute_path == os.path.abspath(second_path):
ad1461
+        if paths_are_equivalent(path, second_path):
ad1461
             return True
ad1461
     return False
ad1461
 
ad1461
@@ -213,7 +217,7 @@ def reduce_files(self, labelled_files, expected_path, categories):
ad1461
             )
ad1461
             raise RuntimeError(msg)
ad1461
         for path, label in labelled_files.items():
ad1461
-            if label in categories and path != expected_path:
ad1461
+            if label in categories and not paths_are_equivalent(path, expected_path):
ad1461
                 continue
ad1461
             reduced_files[path] = label
ad1461
         return reduced_files