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

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