Backport: https://github.com/keylime/keylime/pull/1934 Resolves: RHEL-192222 Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
285 lines
14 KiB
Diff
285 lines
14 KiB
Diff
From 2d46c356abd86b2bbddea058a7ca9a0a0c5da386 Mon Sep 17 00:00:00 2001
|
|
From: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
|
|
Date: Tue, 14 Jul 2026 16:50:03 +0200
|
|
Subject: [PATCH 1/3] test: use per-case temp file instead of /dev/stdout in
|
|
test_sign_runtime_policy
|
|
|
|
The test was relying on the default --output value of /dev/stdout, which
|
|
is not writable in restricted container environments such as Konflux
|
|
build containers (podman with --network=none), causing the test to fail
|
|
with "Permission denied".
|
|
|
|
Fix by explicitly passing a per-case temp file via --output for each
|
|
test case that exercises sign_runtime_policy(). Using mkstemp() with a
|
|
case-indexed suffix avoids filename conflicts when tests run in
|
|
parallel.
|
|
|
|
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
|
|
Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
|
|
---
|
|
test/test_sign_runtime_policy.py | 8 ++++++--
|
|
1 file changed, 6 insertions(+), 2 deletions(-)
|
|
|
|
diff --git a/test/test_sign_runtime_policy.py b/test/test_sign_runtime_policy.py
|
|
index 7af03f8cf..02e7455a0 100644
|
|
--- a/test/test_sign_runtime_policy.py
|
|
+++ b/test/test_sign_runtime_policy.py
|
|
@@ -169,7 +169,7 @@ def test_sign_runtime_policy(self):
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
os.chdir(temp_dir)
|
|
|
|
- for case in test_cases:
|
|
+ for i, case in enumerate(test_cases):
|
|
expected = case["valid"]
|
|
del case["valid"]
|
|
missing_params = case["missing_params"]
|
|
@@ -184,7 +184,11 @@ def test_sign_runtime_policy(self):
|
|
with self.assertRaises(SystemExit):
|
|
args = parser.parse_args(cli_args)
|
|
else:
|
|
- args = parser.parse_args(cli_args)
|
|
+ # Use a per-case temp file instead of /dev/stdout to avoid
|
|
+ # permission issues in restricted container environments (e.g. Konflux).
|
|
+ fd, output_file = tempfile.mkstemp(suffix=f"-{i}.json", dir=temp_dir)
|
|
+ os.close(fd)
|
|
+ args = parser.parse_args(cli_args + ["--output", output_file])
|
|
self.assertTrue(args is not None)
|
|
|
|
signed = sign_runtime_policy.sign_runtime_policy(args)
|
|
|
|
From 1aa9cc6545e82ad509beff49466bae6e322b2cdc Mon Sep 17 00:00:00 2001
|
|
From: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
|
|
Date: Tue, 14 Jul 2026 16:55:06 +0200
|
|
Subject: [PATCH 2/3] test: use per-case temp file instead of /dev/stdout in
|
|
test_create_runtime_policy
|
|
|
|
test_digest_algorithm_priority and
|
|
test_digest_algorithm_priority_exceptions both call
|
|
create_runtime_policy() expecting a successful result, which causes the
|
|
function to write the output to the default --output value of
|
|
/dev/stdout. This fails with "Permission denied" in restricted container
|
|
environments such as Konflux build containers (podman with
|
|
--network=none).
|
|
|
|
Fix by passing a per-case temp file via --output for each test case.
|
|
Using mkstemp() with a case-indexed suffix avoids filename conflicts
|
|
when tests run in parallel.
|
|
|
|
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
|
|
Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
|
|
---
|
|
test/test_create_runtime_policy.py | 88 +++++++++++++++++-------------
|
|
1 file changed, 49 insertions(+), 39 deletions(-)
|
|
|
|
diff --git a/test/test_create_runtime_policy.py b/test/test_create_runtime_policy.py
|
|
index eb9c19a6e..3af1981d4 100644
|
|
--- a/test/test_create_runtime_policy.py
|
|
+++ b/test/test_create_runtime_policy.py
|
|
@@ -771,23 +771,28 @@ def test_digest_algorithm_priority(self):
|
|
subparser = main_parser.add_subparsers(title="actions")
|
|
parser = create_runtime_policy.get_arg_parser(subparser, parent_parser)
|
|
|
|
- for case in test_cases:
|
|
- cli_args = ["--verbose"]
|
|
- # Prepare argument input
|
|
- for k in ["algo_opt", "base_policy", "allowlist", "ima_log", "rootfs"]:
|
|
- cli_args.extend(case.get(k, []))
|
|
-
|
|
- args = parser.parse_args(cli_args)
|
|
- expected_algo = case["expected_algo"]
|
|
- expected_source = case["expected_source"]
|
|
-
|
|
- with keylimePolicyAssertLogs() as logs:
|
|
- _policy = create_runtime_policy.create_runtime_policy(args)
|
|
- self.assertIn(
|
|
- f"Using digest algorithm '{expected_algo}' obtained from the {expected_source}",
|
|
- logs.getvalue(),
|
|
- msg=f"ARGS: {' '.join(cli_args)}",
|
|
- )
|
|
+ with tempfile.TemporaryDirectory() as temp_dir:
|
|
+ for i, case in enumerate(test_cases):
|
|
+ cli_args = ["--verbose"]
|
|
+ # Prepare argument input
|
|
+ for k in ["algo_opt", "base_policy", "allowlist", "ima_log", "rootfs"]:
|
|
+ cli_args.extend(case.get(k, []))
|
|
+
|
|
+ # Use a per-case temp file instead of /dev/stdout to avoid
|
|
+ # permission issues in restricted container environments (e.g. Konflux).
|
|
+ fd, output_file = tempfile.mkstemp(suffix=f"-{i}.json", dir=temp_dir)
|
|
+ os.close(fd)
|
|
+ args = parser.parse_args(cli_args + ["--output", output_file])
|
|
+ expected_algo = case["expected_algo"]
|
|
+ expected_source = case["expected_source"]
|
|
+
|
|
+ with keylimePolicyAssertLogs() as logs:
|
|
+ _policy = create_runtime_policy.create_runtime_policy(args)
|
|
+ self.assertIn(
|
|
+ f"Using digest algorithm '{expected_algo}' obtained from the {expected_source}",
|
|
+ logs.getvalue(),
|
|
+ msg=f"ARGS: {' '.join(cli_args)}",
|
|
+ )
|
|
|
|
def test_digest_algorithm_priority_exceptions(self):
|
|
"""Test priority algorithms exceptions"""
|
|
@@ -839,28 +844,33 @@ def test_digest_algorithm_priority_exceptions(self):
|
|
subparser = main_parser.add_subparsers(title="actions")
|
|
parser = create_runtime_policy.get_arg_parser(subparser, parent_parser)
|
|
|
|
- for case in test_cases:
|
|
- cli_args = ["--verbose"]
|
|
- # Prepare argument input
|
|
- for k in ["base_policy", "allowlist", "ima_log"]:
|
|
- cli_args.extend(case.get(k, []))
|
|
-
|
|
- args = parser.parse_args(cli_args)
|
|
- expected_algo = case["expected_algo"]
|
|
- expected_source = case["expected_source"]
|
|
-
|
|
- with keylimePolicyAssertLogs() as logs:
|
|
- _policy = create_runtime_policy.create_runtime_policy(args)
|
|
- if case["expected_mismatch"]:
|
|
- self.assertIn(
|
|
- f"The digest algorithm in the IMA measurement list does not match the previously set '{expected_algo}' algorithm",
|
|
- logs.getvalue(),
|
|
- )
|
|
- else:
|
|
- self.assertIn(
|
|
- f"Using digest algorithm '{expected_algo}' obtained from the {expected_source}",
|
|
- logs.getvalue(),
|
|
- )
|
|
+ with tempfile.TemporaryDirectory() as temp_dir:
|
|
+ for i, case in enumerate(test_cases):
|
|
+ cli_args = ["--verbose"]
|
|
+ # Prepare argument input
|
|
+ for k in ["base_policy", "allowlist", "ima_log"]:
|
|
+ cli_args.extend(case.get(k, []))
|
|
+
|
|
+ # Use a per-case temp file instead of /dev/stdout to avoid
|
|
+ # permission issues in restricted container environments (e.g. Konflux).
|
|
+ fd, output_file = tempfile.mkstemp(suffix=f"-{i}.json", dir=temp_dir)
|
|
+ os.close(fd)
|
|
+ args = parser.parse_args(cli_args + ["--output", output_file])
|
|
+ expected_algo = case["expected_algo"]
|
|
+ expected_source = case["expected_source"]
|
|
+
|
|
+ with keylimePolicyAssertLogs() as logs:
|
|
+ _policy = create_runtime_policy.create_runtime_policy(args)
|
|
+ if case["expected_mismatch"]:
|
|
+ self.assertIn(
|
|
+ f"The digest algorithm in the IMA measurement list does not match the previously set '{expected_algo}' algorithm",
|
|
+ logs.getvalue(),
|
|
+ )
|
|
+ else:
|
|
+ self.assertIn(
|
|
+ f"Using digest algorithm '{expected_algo}' obtained from the {expected_source}",
|
|
+ logs.getvalue(),
|
|
+ )
|
|
|
|
def test_mixed_algorithms_sources(self):
|
|
"""Test that mixing digests from different algorithms is not allowed"""
|
|
|
|
From c107900aa92aa1b63b5adf6ecb9cf768cc5a79d2 Mon Sep 17 00:00:00 2001
|
|
From: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
|
|
Date: Wed, 15 Jul 2026 14:11:56 +0200
|
|
Subject: [PATCH 3/3] test: use temp files in test_mixed and test_unknown
|
|
algorithm tests
|
|
|
|
Apply the same mkstemp + --output pattern to
|
|
test_mixed_algorithms_sources and test_unknown_algorithm_sources for
|
|
consistency. These tests currently work because create_runtime_policy()
|
|
returns early before reaching the open(args.output) call, but a future
|
|
refactor that moves the file write earlier would break them in
|
|
restricted container environments.
|
|
|
|
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
|
|
Signed-off-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
|
|
---
|
|
test/test_create_runtime_policy.py | 70 +++++++++++++++++-------------
|
|
1 file changed, 40 insertions(+), 30 deletions(-)
|
|
|
|
diff --git a/test/test_create_runtime_policy.py b/test/test_create_runtime_policy.py
|
|
index 3af1981d4..e77974014 100644
|
|
--- a/test/test_create_runtime_policy.py
|
|
+++ b/test/test_create_runtime_policy.py
|
|
@@ -917,21 +917,26 @@ def test_mixed_algorithms_sources(self):
|
|
subparser = main_parser.add_subparsers(title="actions")
|
|
parser = create_runtime_policy.get_arg_parser(subparser, parent_parser)
|
|
|
|
- for case in test_cases:
|
|
- cli_args = []
|
|
- # Prepare argument input
|
|
- for k in ["algo_opt", "base policy", "allowlist", "IMA measurement list", "rootfs"]:
|
|
- cli_args.extend(case.get(k, []))
|
|
-
|
|
- args = parser.parse_args(cli_args)
|
|
-
|
|
- with keylimePolicyAssertLogs() as logs:
|
|
- policy = create_runtime_policy.create_runtime_policy(args)
|
|
- self.assertIn(
|
|
- f"The digest algorithm in the {case['source']} does not match the previously set 'sha1' algorithm",
|
|
- logs.getvalue(),
|
|
- )
|
|
- self.assertEqual(policy, None)
|
|
+ with tempfile.TemporaryDirectory() as temp_dir:
|
|
+ for i, case in enumerate(test_cases):
|
|
+ cli_args = []
|
|
+ # Prepare argument input
|
|
+ for k in ["algo_opt", "base policy", "allowlist", "IMA measurement list", "rootfs"]:
|
|
+ cli_args.extend(case.get(k, []))
|
|
+
|
|
+ # Use a per-case temp file instead of /dev/stdout to avoid
|
|
+ # permission issues in restricted container environments (e.g. Konflux).
|
|
+ fd, output_file = tempfile.mkstemp(suffix=f"-{i}.json", dir=temp_dir)
|
|
+ os.close(fd)
|
|
+ args = parser.parse_args(cli_args + ["--output", output_file])
|
|
+
|
|
+ with keylimePolicyAssertLogs() as logs:
|
|
+ policy = create_runtime_policy.create_runtime_policy(args)
|
|
+ self.assertIn(
|
|
+ f"The digest algorithm in the {case['source']} does not match the previously set 'sha1' algorithm",
|
|
+ logs.getvalue(),
|
|
+ )
|
|
+ self.assertEqual(policy, None)
|
|
|
|
def test_unknown_algorithm_sources(self):
|
|
"""Test that input with digests from unknown algorithms are not allowed"""
|
|
@@ -978,18 +983,23 @@ def test_unknown_algorithm_sources(self):
|
|
subparser = main_parser.add_subparsers(title="actions")
|
|
parser = create_runtime_policy.get_arg_parser(subparser, parent_parser)
|
|
|
|
- for case in test_cases:
|
|
- cli_args = ["--verbose"]
|
|
- # Prepare argument input
|
|
- for k in ["algo_opt", "base policy", "allowlist", "IMA measurement list", "rootfs"]:
|
|
- cli_args.extend(case.get(k, []))
|
|
-
|
|
- args = parser.parse_args(cli_args)
|
|
-
|
|
- with keylimePolicyAssertLogs() as logs:
|
|
- policy = create_runtime_policy.create_runtime_policy(args)
|
|
- self.assertIn(
|
|
- f"Invalid digest algorithm found in the {case['source']}",
|
|
- logs.getvalue(),
|
|
- )
|
|
- self.assertEqual(policy, None)
|
|
+ with tempfile.TemporaryDirectory() as temp_dir:
|
|
+ for i, case in enumerate(test_cases):
|
|
+ cli_args = ["--verbose"]
|
|
+ # Prepare argument input
|
|
+ for k in ["algo_opt", "base policy", "allowlist", "IMA measurement list", "rootfs"]:
|
|
+ cli_args.extend(case.get(k, []))
|
|
+
|
|
+ # Use a per-case temp file instead of /dev/stdout to avoid
|
|
+ # permission issues in restricted container environments (e.g. Konflux).
|
|
+ fd, output_file = tempfile.mkstemp(suffix=f"-{i}.json", dir=temp_dir)
|
|
+ os.close(fd)
|
|
+ args = parser.parse_args(cli_args + ["--output", output_file])
|
|
+
|
|
+ with keylimePolicyAssertLogs() as logs:
|
|
+ policy = create_runtime_policy.create_runtime_policy(args)
|
|
+ self.assertIn(
|
|
+ f"Invalid digest algorithm found in the {case['source']}",
|
|
+ logs.getvalue(),
|
|
+ )
|
|
+ self.assertEqual(policy, None)
|