Skip to content

Commit

Permalink
tests: check that there are no selinux denials
Browse files Browse the repository at this point in the history
  • Loading branch information
mvo5 committed Dec 6, 2023
1 parent 52157c0 commit 66cb1d2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
40 changes: 35 additions & 5 deletions test/test_smoke.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import pathlib
import re
import subprocess

import pytest
Expand Down Expand Up @@ -36,6 +37,28 @@ def config_json_fixture(output_path):
return config_json_path


def log_has_osbuild_selinux_denials(log):
OSBUID_SELINUX_DENIALS_RE = re.compile(r"(?ms)avc:\ +denied.*osbuild")
return re.search(OSBUID_SELINUX_DENIALS_RE, log)


def test_osbuild_selinux_denails_re_works():
fake_log = (
'Dec 05 07:19:39 other log msg\n'
'Dec 05 07:19:39 fedora audit: SELINUX_ERR'
' op=security_bounded_transition seresult=denied'
' oldcontext=system_u:system_r:install_t:s0:c42,c355'
' newcontext=system_u:system_r:mount_t:s0:c42,c355\n'
'Dec 06 16:00:54 internal audit[14368]: AVC avc: denied '
'{ nnp_transition nosuid_transition } for pid=14368 '
'comm="org.osbuild.ost" scontext=system_u:system_r:install_t:s0:'
'c516,c631 tcontext=system_u:system_r:mount_t:s0:c516,c631 '
'tclass=process2 permissive=0'
)
assert log_has_osbuild_selinux_denials(fake_log)
assert not log_has_osbuild_selinux_denials("some\nrandom\nlogs")


@pytest.mark.skipif(os.getuid() != 0, reason="needs root")
@pytest.mark.skipif(not testutil.has_executable("podman"), reason="need podman")
def test_smoke(output_path, config_json):
Expand All @@ -56,12 +79,19 @@ def test_smoke(output_path, config_json):
"quay.io/centos-bootc/centos-bootc:stream9",
"--config", "/output/config.json",
])
# check that there are no denials
# TODO: actually check this once https://github.com/osbuild/images/pull/287
# is merged
journal_output = testutil.journal_after_cursor(cursor)
assert journal_output != ""
generated_img = pathlib.Path(output_path) / "qcow2/disk.qcow2"
assert generated_img.exists(), f"output file missing, dir content: {os.listdir(os.fspath(output_path))}"

# check that there are no selinux denials
journal_output = testutil.journal_after_cursor(cursor)
assert journal_output != ""
assert generated_img.exists()
if testutil.has_executable("selinuxenabled") and subprocess.run("selinuxenabled").returncode == 0:
# log example:
assert not log_has_osbuild_selinux_denials(journal_output), f"denials in log {journal_output}"
# print(f"DEBUG: journal output:\n{journal_output}")
else:
print("WARNING: selinux not enabled, cannot check for denials")

# TODO: boot and do basic checks, see
# https://github.com/osbuild/osbuild-deploy-container/compare/main...mvo5:integration-test?expand=1
2 changes: 1 addition & 1 deletion test/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def journal_cursor():


def journal_after_cursor(cursor):
output = subprocess.check_output(["journalctl", f"--after-cursor={cursor}"])
output = subprocess.check_output(["journalctl", f"--after-cursor={cursor}"], encoding="utf8")
return output


Expand Down

0 comments on commit 66cb1d2

Please sign in to comment.