294
293
"ApprovalDuration", "Checker", "ExtendedTimeout",
295
294
"Expires", "LastCheckerStatus")
296
295
def run(self, mandos, clients):
297
print(self.output(clients.values()))
298
def output(self, clients):
299
raise NotImplementedError()
296
print(self.output(clients))
301
298
class PropertyCmd(Command):
302
299
"""Abstract class for Actions for setting one client property"""
303
300
def run_on_one_client(self, client, properties):
304
301
"""Set the Client's D-Bus property"""
305
log.debug("D-Bus: %s:%s:%s.Set(%r, %r, %r)", busname,
306
client.__dbus_object_path__,
307
dbus.PROPERTIES_IFACE, client_interface,
308
self.propname, self.value_to_set
309
if not isinstance(self.value_to_set, dbus.Boolean)
310
else bool(self.value_to_set))
311
client.Set(client_interface, self.propname, self.value_to_set,
302
client.Set(client_interface, self.property, self.value_to_set,
312
303
dbus_interface=dbus.PROPERTIES_IFACE)
315
raise NotImplementedError()
317
305
class ValueArgumentMixIn(object):
318
306
"""Mixin class for commands taking a value as argument"""
440
428
def is_enabled(self, client, properties):
441
log.debug("D-Bus: %s:%s:%s.Get(%r, %r)", busname,
442
client.__dbus_object_path__,
443
dbus.PROPERTIES_IFACE, client_interface,
445
return bool(client.Get(client_interface, "Enabled",
446
dbus_interface=dbus.PROPERTIES_IFACE))
429
return bool(properties["Enabled"])
448
431
class RemoveCmd(Command):
449
432
def run_on_one_client(self, client, properties):
450
log.debug("D-Bus: %s:%s:%s.RemoveClient(%r)", busname,
451
server_path, server_interface,
452
str(client.__dbus_object_path__))
453
433
self.mandos.RemoveClient(client.__dbus_object_path__)
455
435
class ApproveCmd(Command):
456
436
def run_on_one_client(self, client, properties):
457
log.debug("D-Bus: %s:%s.Approve(True)",
458
client.__dbus_object_path__, client_interface)
459
437
client.Approve(dbus.Boolean(True),
460
438
dbus_interface=client_interface)
462
440
class DenyCmd(Command):
463
441
def run_on_one_client(self, client, properties):
464
log.debug("D-Bus: %s:%s.Approve(False)",
465
client.__dbus_object_path__, client_interface)
466
442
client.Approve(dbus.Boolean(False),
467
443
dbus_interface=client_interface)
469
445
class EnableCmd(PropertyCmd):
471
447
value_to_set = dbus.Boolean(True)
473
449
class DisableCmd(PropertyCmd):
475
451
value_to_set = dbus.Boolean(False)
477
453
class BumpTimeoutCmd(PropertyCmd):
478
propname = "LastCheckedOK"
454
property = "LastCheckedOK"
479
455
value_to_set = ""
481
457
class StartCheckerCmd(PropertyCmd):
482
propname = "CheckerRunning"
458
property = "CheckerRunning"
483
459
value_to_set = dbus.Boolean(True)
485
461
class StopCheckerCmd(PropertyCmd):
486
propname = "CheckerRunning"
462
property = "CheckerRunning"
487
463
value_to_set = dbus.Boolean(False)
489
465
class ApproveByDefaultCmd(PropertyCmd):
490
propname = "ApprovedByDefault"
466
property = "ApprovedByDefault"
491
467
value_to_set = dbus.Boolean(True)
493
469
class DenyByDefaultCmd(PropertyCmd):
494
propname = "ApprovedByDefault"
470
property = "ApprovedByDefault"
495
471
value_to_set = dbus.Boolean(False)
497
473
class SetCheckerCmd(PropertyCmd, ValueArgumentMixIn):
500
476
class SetHostCmd(PropertyCmd, ValueArgumentMixIn):
503
479
class SetSecretCmd(PropertyCmd, ValueArgumentMixIn):
506
481
def value_to_set(self):
510
485
"""When setting, read data from supplied file object"""
511
486
self._vts = value.read()
514
490
class SetTimeoutCmd(PropertyCmd, MillisecondsValueArgumentMixIn):
517
493
class SetExtendedTimeoutCmd(PropertyCmd,
518
494
MillisecondsValueArgumentMixIn):
519
propname = "ExtendedTimeout"
495
property = "ExtendedTimeout"
521
497
class SetIntervalCmd(PropertyCmd, MillisecondsValueArgumentMixIn):
522
propname = "Interval"
498
property = "Interval"
524
500
class SetApprovalDelayCmd(PropertyCmd,
525
501
MillisecondsValueArgumentMixIn):
526
propname = "ApprovalDelay"
502
property = "ApprovalDelay"
528
504
class SetApprovalDurationCmd(PropertyCmd,
529
505
MillisecondsValueArgumentMixIn):
530
propname = "ApprovalDuration"
506
property = "ApprovalDuration"
532
508
def add_command_line_options(parser):
533
509
parser.add_argument("--version", action="version",
846
802
self.attributes = attributes
847
803
self.attributes["Name"] = name
849
def Set(self, interface, propname, value, dbus_interface):
850
testcase.assertEqual(interface, client_interface)
851
testcase.assertEqual(dbus_interface,
852
dbus.PROPERTIES_IFACE)
853
self.attributes[propname] = value
854
def Get(self, interface, propname, dbus_interface):
855
testcase.assertEqual(interface, client_interface)
856
testcase.assertEqual(dbus_interface,
857
dbus.PROPERTIES_IFACE)
858
return self.attributes[propname]
805
def Set(self, interface, property, value, dbus_interface):
806
testcase.assertEqual(interface, client_interface)
807
testcase.assertEqual(dbus_interface,
808
dbus.PROPERTIES_IFACE)
809
self.attributes[property] = value
810
def Get(self, interface, property, dbus_interface):
811
testcase.assertEqual(interface, client_interface)
812
testcase.assertEqual(dbus_interface,
813
dbus.PROPERTIES_IFACE)
814
return self.attributes[property]
859
815
def Approve(self, approve, dbus_interface):
860
816
testcase.assertEqual(dbus_interface, client_interface)
861
817
self.calls.append(("Approve", (approve,
1098
1053
class TestBumpTimeoutCmd(TestPropertyCmd):
1099
1054
command = BumpTimeoutCmd
1100
propname = "LastCheckedOK"
1055
property = "LastCheckedOK"
1101
1056
values_to_set = [""]
1103
1058
class TestStartCheckerCmd(TestPropertyCmd):
1104
1059
command = StartCheckerCmd
1105
propname = "CheckerRunning"
1060
property = "CheckerRunning"
1106
1061
values_to_set = [dbus.Boolean(True)]
1108
1063
class TestStopCheckerCmd(TestPropertyCmd):
1109
1064
command = StopCheckerCmd
1110
propname = "CheckerRunning"
1065
property = "CheckerRunning"
1111
1066
values_to_set = [dbus.Boolean(False)]
1113
1068
class TestApproveByDefaultCmd(TestPropertyCmd):
1114
1069
command = ApproveByDefaultCmd
1115
propname = "ApprovedByDefault"
1070
property = "ApprovedByDefault"
1116
1071
values_to_set = [dbus.Boolean(True)]
1118
1073
class TestDenyByDefaultCmd(TestPropertyCmd):
1119
1074
command = DenyByDefaultCmd
1120
propname = "ApprovedByDefault"
1075
property = "ApprovedByDefault"
1121
1076
values_to_set = [dbus.Boolean(False)]
1123
1078
class TestValueArgumentPropertyCmd(TestPropertyCmd):
1375
1330
def test_is_enabled_short(self):
1376
1331
self.assert_command_from_args(["-V", "foo"], IsEnabledCmd)
1378
def test_deny_before_remove(self):
1379
options = self.parser.parse_args(["--deny", "--remove", "foo"])
1380
check_option_syntax(self.parser, options)
1381
commands = commands_from_options(options)
1382
self.assertEqual(len(commands), 2)
1383
self.assertIsInstance(commands[0], DenyCmd)
1384
self.assertIsInstance(commands[1], RemoveCmd)
1386
def test_deny_before_remove_reversed(self):
1387
options = self.parser.parse_args(["--remove", "--deny", "--all"])
1388
check_option_syntax(self.parser, options)
1389
commands = commands_from_options(options)
1390
self.assertEqual(len(commands), 2)
1391
self.assertIsInstance(commands[0], DenyCmd)
1392
self.assertIsInstance(commands[1], RemoveCmd)
1395
class Test_check_option_syntax(unittest.TestCase):
1396
# This mostly corresponds to the definition from has_actions() in
1397
# check_option_syntax()
1399
# The actual values set here are not that important, but we do
1400
# at least stick to the correct types, even though they are
1404
"bump_timeout": True,
1405
"start_checker": True,
1406
"stop_checker": True,
1410
"timeout": datetime.timedelta(),
1411
"extended_timeout": datetime.timedelta(),
1412
"interval": datetime.timedelta(),
1413
"approved_by_default": True,
1414
"approval_delay": datetime.timedelta(),
1415
"approval_duration": datetime.timedelta(),
1417
"secret": io.BytesIO(b"x"),
1423
self.parser = argparse.ArgumentParser()
1424
add_command_line_options(self.parser)
1426
@contextlib.contextmanager
1427
def assertParseError(self):
1428
with self.assertRaises(SystemExit) as e:
1429
with self.temporarily_suppress_stderr():
1431
# Exit code from argparse is guaranteed to be "2". Reference:
1432
# https://docs.python.org/3/library/argparse.html#exiting-methods
1433
self.assertEqual(e.exception.code, 2)
1436
@contextlib.contextmanager
1437
def temporarily_suppress_stderr():
1438
null = os.open(os.path.devnull, os.O_RDWR)
1439
stderrcopy = os.dup(sys.stderr.fileno())
1440
os.dup2(null, sys.stderr.fileno())
1446
os.dup2(stderrcopy, sys.stderr.fileno())
1447
os.close(stderrcopy)
1449
def check_option_syntax(self, options):
1450
check_option_syntax(self.parser, options)
1452
def test_actions_requires_client_or_all(self):
1453
for action, value in self.actions.items():
1454
options = self.parser.parse_args()
1455
setattr(options, action, value)
1456
with self.assertParseError():
1457
self.check_option_syntax(options)
1459
def test_actions_conflicts_with_verbose(self):
1460
for action, value in self.actions.items():
1461
options = self.parser.parse_args()
1462
setattr(options, action, value)
1463
options.verbose = True
1464
with self.assertParseError():
1465
self.check_option_syntax(options)
1467
def test_dump_json_conflicts_with_verbose(self):
1468
options = self.parser.parse_args()
1469
options.dump_json = True
1470
options.verbose = True
1471
with self.assertParseError():
1472
self.check_option_syntax(options)
1474
def test_dump_json_conflicts_with_action(self):
1475
for action, value in self.actions.items():
1476
options = self.parser.parse_args()
1477
setattr(options, action, value)
1478
options.dump_json = True
1479
with self.assertParseError():
1480
self.check_option_syntax(options)
1482
def test_all_can_not_be_alone(self):
1483
options = self.parser.parse_args()
1485
with self.assertParseError():
1486
self.check_option_syntax(options)
1488
def test_all_is_ok_with_any_action(self):
1489
for action, value in self.actions.items():
1490
options = self.parser.parse_args()
1491
setattr(options, action, value)
1493
self.check_option_syntax(options)
1495
def test_is_enabled_fails_without_client(self):
1496
options = self.parser.parse_args()
1497
options.is_enabled = True
1498
with self.assertParseError():
1499
self.check_option_syntax(options)
1501
def test_is_enabled_works_with_one_client(self):
1502
options = self.parser.parse_args()
1503
options.is_enabled = True
1504
options.client = ["foo"]
1505
self.check_option_syntax(options)
1507
def test_is_enabled_fails_with_two_clients(self):
1508
options = self.parser.parse_args()
1509
options.is_enabled = True
1510
options.client = ["foo", "barbar"]
1511
with self.assertParseError():
1512
self.check_option_syntax(options)
1514
def test_remove_can_only_be_combined_with_action_deny(self):
1515
for action, value in self.actions.items():
1516
if action in {"remove", "deny"}:
1518
options = self.parser.parse_args()
1519
setattr(options, action, value)
1521
options.remove = True
1522
with self.assertParseError():
1523
self.check_option_syntax(options)
1527
1335
def should_only_run_tests():