Skip to content

Commit

Permalink
ScanCryptoPolicies: Adjust to Python2 compatibility
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Jakuje committed Feb 6, 2025
1 parent 73099e2 commit 2bc3a5c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand All @@ -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


Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shutil
import tempfile

import pytest
Expand Down Expand Up @@ -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():
Expand All @@ -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)

0 comments on commit 2bc3a5c

Please sign in to comment.