From 2bc3a5caac17743e2927788a37f8c64332087f4e Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Thu, 6 Feb 2025 16:09:33 +0100 Subject: [PATCH] ScanCryptoPolicies: Adjust to Python2 compatibility Even though it will never run on RHEL 7. This can be safely reverted after we will drop support for Python2 Signed-off-by: Jakub Jelen --- .../libraries/scancryptopolicies.py | 32 ++++----- .../tests/unit_test_scancryptopolicies.py | 66 ++++++++++++------- 2 files changed, 57 insertions(+), 41 deletions(-) diff --git a/repos/system_upgrade/common/actors/scancryptopolicies/libraries/scancryptopolicies.py b/repos/system_upgrade/common/actors/scancryptopolicies/libraries/scancryptopolicies.py index b456cb44de..30bb172e17 100644 --- a/repos/system_upgrade/common/actors/scancryptopolicies/libraries/scancryptopolicies.py +++ b/repos/system_upgrade/common/actors/scancryptopolicies/libraries/scancryptopolicies.py @@ -11,23 +11,23 @@ '/usr/share/crypto-policies/policies/modules',) -def read_current_policy(file): - if not os.path.exists(file): +def read_current_policy(filename): + if not os.path.exists(filename): # NOTE(pstodulk) just seatbelt, I do not expect the file is not present # skipping tests raise StopActorExecutionError( - 'File not found: {}'.format(file), + 'File not found: {}'.format(filename), details={'details:': 'Cannot check the current set crypto policies.'} ) current = 'DEFAULT' - with open(file) as fp: + with open(filename) as fp: current = fp.read().strip() return current -def _get_name_from_file(file): +def _get_name_from_file(filename): """This is just stripping the path and the extension""" - base = os.path.basename(file) + base = os.path.basename(filename) return os.path.splitext(base)[0] @@ -44,10 +44,10 @@ def find_rpm_untracked(files): # return only untracked files from the list out = [] - for file in files: - exp = "file {} is not owned by any package".format(file) + for filename in files: + exp = "file {} is not owned by any package".format(filename) if exp in res['stdout']: - out.append(file) + out.append(filename) return out @@ -56,17 +56,17 @@ def read_policy_dirs(dirs, obj, extension): files = [] # find all policy files for d in dirs: - for file in os.listdir(d): - file = os.path.join(d, file) - if not os.path.isfile(file) or not file.endswith(extension): + for filename in os.listdir(d): + filepath = os.path.join(d, filename) + if not os.path.isfile(filepath) or not filepath.endswith(extension): continue - files.append(file) + files.append(filepath) # now, check which are not tracked by RPM: files = find_rpm_untracked(files) out = [] - for file in files: - name = _get_name_from_file(file) - out.append(obj(name=name, path=file)) + for filename in files: + name = _get_name_from_file(filename) + out.append(obj(name=name, path=filename)) return out diff --git a/repos/system_upgrade/common/actors/scancryptopolicies/tests/unit_test_scancryptopolicies.py b/repos/system_upgrade/common/actors/scancryptopolicies/tests/unit_test_scancryptopolicies.py index d2fa71813d..01d7f2da34 100644 --- a/repos/system_upgrade/common/actors/scancryptopolicies/tests/unit_test_scancryptopolicies.py +++ b/repos/system_upgrade/common/actors/scancryptopolicies/tests/unit_test_scancryptopolicies.py @@ -1,4 +1,5 @@ import os +import shutil import tempfile import pytest @@ -30,21 +31,26 @@ def test_find_rpm_untracked(current_actor_context): files = ["/etc/crypto-policies/config"] assert find_rpm_untracked(files) == [] - # the tempfile is not tracked by RPM - with tempfile.NamedTemporaryFile(delete=False) as f: - files = [f.name] - assert find_rpm_untracked(files) == [f.name] + # python2 compatibility :/ + dirpath = tempfile.mkdtemp() + + try: + # the tempfile is not tracked by RPM + files = [dirpath] + assert find_rpm_untracked(files) == [dirpath] # not existing files are ignored files = [NOFILE] assert find_rpm_untracked(files) == [] # combinations should yield expected results too - files = ["/tmp", f.name, NOFILE] - assert find_rpm_untracked(files) == [f.name] + files = ["/tmp", dirpath, NOFILE] + assert find_rpm_untracked(files) == [dirpath] # regardless the order - files = [NOFILE, f.name, "/tmp"] - assert find_rpm_untracked(files) == [f.name] + files = [NOFILE, dirpath, "/tmp"] + assert find_rpm_untracked(files) == [dirpath] + finally: + shutil.rmtree(dirpath) def test_read_current_policy(): @@ -63,43 +69,53 @@ def test_read_current_policy(): def test_read_policy_dirs(current_actor_context): - with tempfile.TemporaryDirectory() as dir1: + # python2 compatibility :/ + dirpath = tempfile.mkdtemp() + + try: # empty - files = read_policy_dirs([dir1], CustomCryptoPolicy, ".pol") + files = read_policy_dirs([dirpath], CustomCryptoPolicy, ".pol") assert files == [] # first policy module - path1 = os.path.join(dir1, "policy.mpol") - with open(path1, "x") as f: + path1 = os.path.join(dirpath, "policy.mpol") + with open(path1, "w") as f: f.write('test') - files = read_policy_dirs([dir1], CustomCryptoPolicy, ".pol") + files = read_policy_dirs([dirpath], CustomCryptoPolicy, ".pol") assert files == [] - files = read_policy_dirs([dir1], CustomCryptoPolicyModule, ".mpol") + files = read_policy_dirs([dirpath], CustomCryptoPolicyModule, ".mpol") assert files == [CustomCryptoPolicyModule(name="policy", path=path1)] - with tempfile.TemporaryDirectory() as dir2: - files = read_policy_dirs([dir1], CustomCryptoPolicy, ".pol") + # python2 compatibility :/ + dirpath2 = tempfile.mkdtemp() + + try: + files = read_policy_dirs([dirpath], CustomCryptoPolicy, ".pol") assert files == [] - files = read_policy_dirs([dir1, dir2], CustomCryptoPolicyModule, ".mpol") + files = read_policy_dirs([dirpath, dirpath2], CustomCryptoPolicyModule, ".mpol") assert files == [CustomCryptoPolicyModule(name="policy", path=path1)] # first policy file - path2 = os.path.join(dir2, "mypolicy.pol") - with open(path2, "x") as f: + path2 = os.path.join(dirpath2, "mypolicy.pol") + with open(path2, "w") as f: f.write('test2') # second policy file - path3 = os.path.join(dir2, "other.pol") - with open(path3, "x") as f: + path3 = os.path.join(dirpath2, "other.pol") + with open(path3, "w") as f: f.write('test3') - files = read_policy_dirs([dir1, dir2], dict, ".pol") + files = read_policy_dirs([dirpath, dirpath2], dict, ".pol") assert len(files) == 2 assert dict(name="mypolicy", path=path2) in files assert dict(name="other", path=path3) in files - files = read_policy_dirs([dir1, dir2], CustomCryptoPolicyModule, ".mpol") + files = read_policy_dirs([dirpath, dirpath2], CustomCryptoPolicyModule, ".mpol") assert files == [CustomCryptoPolicyModule(name="policy", path=path1)] + finally: + shutil.rmtree(dirpath2) - files = read_policy_dirs([dir1], CustomCryptoPolicy, ".pol") + files = read_policy_dirs([dirpath], CustomCryptoPolicy, ".pol") assert files == [] - files = read_policy_dirs([dir1], CustomCryptoPolicyModule, ".mpol") + files = read_policy_dirs([dirpath], CustomCryptoPolicyModule, ".mpol") assert files == [CustomCryptoPolicyModule(name="policy", path=path1)] + finally: + shutil.rmtree(dirpath)