Skip to content

Commit 6049da7

Browse files
committed
Use new policy admin exceptions
For: QubesOS/qubes-issues#10746 Requires: QubesOS/qubes-core-qrexec#223
1 parent d56bb9e commit 6049da7

4 files changed

Lines changed: 60 additions & 9 deletions

File tree

qubes_config/global_config/policy_manager.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,18 @@
2121
import subprocess
2222
from typing import Optional, List, Tuple
2323

24+
from qubes_config.widgets.utils import compare_rule_lists
2425
from qrexec.policy.admin_client import PolicyClient
2526
from qrexec.policy.parser import StringPolicy, Rule
26-
from qubes_config.widgets.utils import compare_rule_lists
27+
28+
try:
29+
from qrexec.policy.admin import (
30+
PolicyAdminException,
31+
PolicyAdminFileNotFoundException,
32+
)
33+
except ImportError:
34+
PolicyAdminException = subprocess.CalledProcessError
35+
PolicyAdminFileNotFoundException = subprocess.CalledProcessError
2736

2837
import gettext
2938

@@ -51,7 +60,7 @@ def get_all_policy_files(self, service: str) -> List[str]:
5160
"""Just get a straightforward list of all relevant policy files."""
5261
try:
5362
return self.policy_client.policy_get_files(service)
54-
except subprocess.CalledProcessError:
63+
except (PolicyAdminException, subprocess.CalledProcessError):
5564
return []
5665

5766
def get_conflicting_policy_files(self, service: str, own_file: str) -> List[str]:
@@ -83,7 +92,7 @@ def get_rules_from_filename(
8392
for the file."""
8493
try:
8594
rules_text, token = self.policy_client.policy_get(filename)
86-
except subprocess.CalledProcessError:
95+
except (PolicyAdminFileNotFoundException, subprocess.CalledProcessError):
8796
if not default_policy:
8897
return [], None
8998
rules_text, token = default_policy, None

qubes_config/policy_editor/policy_editor.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@
3131
from qrexec.policy.parser import StringPolicy
3232
from qrexec.exc import PolicySyntaxError
3333

34+
try:
35+
from qrexec.policy.admin import (
36+
PolicyAdminException,
37+
PolicyAdminFileNotFoundException,
38+
)
39+
except ImportError:
40+
PolicyAdminException = subprocess.CalledProcessError
41+
PolicyAdminFileNotFoundException = subprocess.CalledProcessError
42+
3443
from qubes_config.widgets.gtk_utils import (
3544
load_theme,
3645
show_error,
@@ -450,9 +459,9 @@ def _save(self, *_args):
450459
self.policy_client.policy_replace(
451460
self.filename, self.policy_text, self.token
452461
)
453-
except subprocess.CalledProcessError as ex:
454-
err_msg = "An error occurred while trying to save the policy file:\n"
455-
if ex.stdout:
462+
except (PolicyAdminException, subprocess.CalledProcessError) as ex:
463+
err_msg = f"Failed to replace policy {self.filename!r}: "
464+
if hasattr(ex, "stdout"):
456465
err_msg += ex.stdout.decode()
457466
else:
458467
err_msg += str(ex)
@@ -533,8 +542,8 @@ def open_policy_file(self, name: Optional[str]):
533542
else:
534543
try:
535544
text, self.token = self.policy_client.policy_get(name)
536-
except subprocess.CalledProcessError as ex:
537-
if ex.returncode == 126:
545+
except (PolicyAdminFileNotFoundException, subprocess.CalledProcessError) as ex:
546+
if getattr(ex, "returncode", None) == 126:
538547
show_error(
539548
self.main_window,
540549
"Access denied",

qubes_config/tests/conftest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@
4646
MockDevice,
4747
)
4848

49+
try:
50+
from qrexec.policy.admin import (
51+
PolicyAdminFileNotFoundException,
52+
PolicyAdminTokenException,
53+
)
54+
55+
admin_exc = True
56+
except ImportError:
57+
admin_exc = False
58+
4959

5060
@pytest.fixture
5161
def test_qapp():
@@ -291,6 +301,8 @@ def policy_get(self, file_name):
291301
"""Get file contents; takes into account policy_replace."""
292302
if file_name in self.files:
293303
return self.files[file_name], self.file_tokens[file_name]
304+
if admin_exc:
305+
raise PolicyAdminFileNotFoundException
294306
raise subprocess.CalledProcessError(2, "test")
295307

296308
def policy_include_get(self, file_name):
@@ -300,15 +312,21 @@ def policy_include_get(self, file_name):
300312
self.include_files[file_name],
301313
self.include_file_tokens[file_name],
302314
)
315+
if admin_exc:
316+
raise PolicyAdminFileNotFoundException
303317
raise subprocess.CalledProcessError(2, "test")
304318

305319
def policy_replace(self, filename, policy_text, token="any"):
306320
"""Replace file contents with provided contents."""
307321
if token == "new":
308322
if filename in self.file_tokens:
323+
if admin_exc:
324+
raise PolicyAdminTokenException
309325
raise subprocess.CalledProcessError(2, "test")
310326
elif token != "any":
311327
if token != self.file_tokens.get(filename, ""):
328+
if admin_exc:
329+
raise PolicyAdminTokenException
312330
raise subprocess.CalledProcessError(2, "test")
313331
self.files[filename] = policy_text
314332
self.file_tokens[filename] = str(len(policy_text))
@@ -317,6 +335,8 @@ def policy_include_replace(self, filename, policy_text, token="any"):
317335
"""Replace file contents with provided contents."""
318336
if token != "any":
319337
if token != self.include_file_tokens.get(filename, ""):
338+
if admin_exc:
339+
raise PolicyAdminTokenException
320340
raise subprocess.CalledProcessError(2, "test")
321341
self.include_files[filename] = policy_text
322342
self.include_file_tokens[filename] = str(len(policy_text))

qubes_config/tests/test_policy_manager.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
from ..global_config.policy_manager import PolicyManager
2626
from qrexec.policy.parser import Rule
2727

28+
try:
29+
from qrexec.policy.admin import PolicyAdminFileNotFoundException
30+
31+
admin_exc = True
32+
except ImportError:
33+
admin_exc = False
34+
2835

2936
def test_conflict_files():
3037
def return_files(service_name):
@@ -58,10 +65,14 @@ def test_get_policy_from_file_new_no_default(mock_replace, mock_get):
5865
manager = PolicyManager()
5966

6067
mock_get.side_effect = subprocess.CalledProcessError(2, "test")
61-
6268
assert manager.get_rules_from_filename("test", "") == ([], None)
6369
assert not mock_replace.mock_calls
6470

71+
if admin_exc:
72+
mock_get.side_effect = PolicyAdminFileNotFoundException
73+
assert manager.get_rules_from_filename("test", "") == ([], None)
74+
assert not mock_replace.mock_calls
75+
6576

6677
def test_get_policy_from_file_new():
6778
class MockPolicy:
@@ -71,6 +82,8 @@ def __init__(self):
7182
def policy_get(self, filename):
7283
if filename in self.files:
7384
return self.files[filename], filename
85+
if admin_exc:
86+
raise PolicyAdminFileNotFoundException
7487
raise subprocess.CalledProcessError(2, "test")
7588

7689
def policy_replace(self, filename, text):

0 commit comments

Comments
 (0)