102
102
mandos_serv_object_manager = dbus.Interface(
103
103
mandos_dbus_object, dbus_interface=dbus.OBJECT_MANAGER_IFACE)
106
log.debug("D-Bus: %s:%s:%s.GetManagedObjects()", dbus_busname,
107
server_dbus_path, dbus.OBJECT_MANAGER_IFACE)
108
with SilenceLogger("dbus.proxies"):
109
all_clients = {path: ifs_and_props[client_dbus_interface]
110
for path, ifs_and_props in
111
mandos_serv_object_manager
112
.GetManagedObjects().items()
113
if client_dbus_interface in ifs_and_props}
114
except dbus.exceptions.DBusException as e:
115
log.critical("Failed to access Mandos server through D-Bus:"
119
# Compile dict of (clients: properties) to process
105
managed_objects = get_managed_objects(mandos_serv_object_manager)
108
for path, ifs_and_props in managed_objects.items():
110
all_clients[path] = ifs_and_props[client_dbus_interface]
114
# Compile dict of (clientpath: properties) to process
122
115
if not clientnames:
123
116
clients = all_clients
125
119
for name in clientnames:
126
120
for objpath, properties in all_clients.items():
127
121
if properties["Name"] == name:
433
427
def get_mandos_dbus_object(bus):
435
log.debug("D-Bus: Connect to: (busname=%r, path=%r)",
436
dbus_busname, server_dbus_path)
428
log.debug("D-Bus: Connect to: (busname=%r, path=%r)",
429
dbus_busname, server_dbus_path)
430
with if_dbus_exception_log_with_exception_and_exit(
431
"Could not connect to Mandos server: %s"):
437
432
mandos_dbus_object = bus.get_object(dbus_busname,
438
433
server_dbus_path)
439
except dbus.exceptions.DBusException:
440
log.critical("Could not connect to Mandos server")
443
434
return mandos_dbus_object
437
@contextlib.contextmanager
438
def if_dbus_exception_log_with_exception_and_exit(*args, **kwargs):
441
except dbus.exceptions.DBusException as e:
442
log.critical(*(args + (e,)), **kwargs)
446
def get_managed_objects(object_manager):
447
log.debug("D-Bus: %s:%s:%s.GetManagedObjects()", dbus_busname,
448
server_dbus_path, dbus.OBJECT_MANAGER_IFACE)
449
with if_dbus_exception_log_with_exception_and_exit(
450
"Failed to access Mandos server through D-Bus:\n%s"):
451
with SilenceLogger("dbus.proxies"):
452
managed_objects = object_manager.GetManagedObjects()
453
return managed_objects
446
456
class SilenceLogger(object):
447
457
"Simple context manager to silence a particular logger"
448
458
def __init__(self, loggername):
832
class Test_string_to_delta(unittest.TestCase):
842
class TestCaseWithAssertLogs(unittest.TestCase):
843
"""unittest.TestCase.assertLogs only exists in Python 3.4"""
845
if not hasattr(unittest.TestCase, "assertLogs"):
846
@contextlib.contextmanager
847
def assertLogs(self, logger, level=logging.INFO):
848
capturing_handler = self.CapturingLevelHandler(level)
849
old_level = logger.level
850
old_propagate = logger.propagate
851
logger.addHandler(capturing_handler)
852
logger.setLevel(level)
853
logger.propagate = False
855
yield capturing_handler.watcher
857
logger.propagate = old_propagate
858
logger.removeHandler(capturing_handler)
859
logger.setLevel(old_level)
860
self.assertGreater(len(capturing_handler.watcher.records),
863
class CapturingLevelHandler(logging.Handler):
864
def __init__(self, level, *args, **kwargs):
865
logging.Handler.__init__(self, *args, **kwargs)
866
self.watcher = self.LoggingWatcher([], [])
867
def emit(self, record):
868
self.watcher.records.append(record)
869
self.watcher.output.append(self.format(record))
871
LoggingWatcher = collections.namedtuple("LoggingWatcher",
875
class Test_string_to_delta(TestCaseWithAssertLogs):
833
876
def test_handles_basic_rfc3339(self):
834
877
self.assertEqual(string_to_delta("PT0S"),
835
878
datetime.timedelta())
841
884
datetime.timedelta(0, 7200))
843
886
def test_falls_back_to_pre_1_6_1_with_warning(self):
844
# assertLogs only exists in Python 3.4
845
if hasattr(self, "assertLogs"):
846
with self.assertLogs(log, logging.WARNING):
847
value = string_to_delta("2h")
849
class WarningFilter(logging.Filter):
850
"""Don't show, but record the presence of, warnings"""
851
def filter(self, record):
852
is_warning = record.levelno >= logging.WARNING
853
self.found = is_warning or getattr(self, "found",
855
return not is_warning
856
warning_filter = WarningFilter()
857
log.addFilter(warning_filter)
859
value = string_to_delta("2h")
861
log.removeFilter(warning_filter)
862
self.assertTrue(getattr(warning_filter, "found", False))
887
with self.assertLogs(log, logging.WARNING):
888
value = string_to_delta("2h")
863
889
self.assertEqual(value, datetime.timedelta(0, 7200))
928
954
def check_option_syntax(self, options):
929
955
check_option_syntax(self.parser, options)
931
def test_actions_conflicts_with_verbose(self):
932
for action, value in self.actions.items():
933
options = self.parser.parse_args()
934
setattr(options, action, value)
935
options.verbose = True
957
def test_actions_all_conflicts_with_verbose(self):
958
for action, value in self.actions.items():
959
options = self.parser.parse_args()
960
setattr(options, action, value)
962
options.verbose = True
963
with self.assertParseError():
964
self.check_option_syntax(options)
966
def test_actions_with_client_conflicts_with_verbose(self):
967
for action, value in self.actions.items():
968
options = self.parser.parse_args()
969
setattr(options, action, value)
970
options.verbose = True
971
options.client = ["foo"]
936
972
with self.assertParseError():
937
973
self.check_option_syntax(options)
964
1000
options.all = True
965
1001
self.check_option_syntax(options)
1003
def test_any_action_is_ok_with_one_client(self):
1004
for action, value in self.actions.items():
1005
options = self.parser.parse_args()
1006
setattr(options, action, value)
1007
options.client = ["foo"]
1008
self.check_option_syntax(options)
1010
def test_actions_except_is_enabled_are_ok_with_two_clients(self):
1011
for action, value in self.actions.items():
1012
if action == "is_enabled":
1014
options = self.parser.parse_args()
1015
setattr(options, action, value)
1016
options.client = ["foo", "barbar"]
1017
self.check_option_syntax(options)
967
1019
def test_is_enabled_fails_without_client(self):
968
1020
options = self.parser.parse_args()
969
1021
options.is_enabled = True
970
1022
with self.assertParseError():
971
1023
self.check_option_syntax(options)
973
def test_is_enabled_works_with_one_client(self):
974
options = self.parser.parse_args()
975
options.is_enabled = True
976
options.client = ["foo"]
977
self.check_option_syntax(options)
979
1025
def test_is_enabled_fails_with_two_clients(self):
980
1026
options = self.parser.parse_args()
981
1027
options.is_enabled = True
1016
1062
def get_object(self, busname, dbus_path):
1017
1063
raise dbus.exceptions.DBusException("Test")
1019
# assertLogs only exists in Python 3.4
1020
if hasattr(self, "assertLogs"):
1021
with self.assertLogs(log, logging.CRITICAL):
1022
with self.assertRaises(SystemExit) as e:
1023
bus = get_mandos_dbus_object(bus=MockBus())
1025
critical_filter = self.CriticalFilter()
1026
log.addFilter(critical_filter)
1028
with self.assertRaises(SystemExit) as e:
1029
get_mandos_dbus_object(bus=MockBusFailing())
1031
log.removeFilter(critical_filter)
1032
self.assertTrue(critical_filter.found)
1065
with self.assertLogs(log, logging.CRITICAL):
1066
with self.assertRaises(SystemExit) as e:
1067
bus = get_mandos_dbus_object(bus=MockBusFailing())
1033
1069
if isinstance(e.exception.code, int):
1034
1070
self.assertNotEqual(e.exception.code, 0)
1036
1072
self.assertIsNotNone(e.exception.code)
1038
class CriticalFilter(logging.Filter):
1039
"""Don't show, but register, critical messages"""
1041
def filter(self, record):
1042
is_critical = record.levelno >= logging.CRITICAL
1043
self.found = is_critical or self.found
1044
return not is_critical
1047
class Test_SilenceLogger(unittest.TestCase):
1048
loggername = "mandos-ctl.Test_SilenceLogger"
1049
log = logging.getLogger(loggername)
1050
log.propagate = False
1051
log.addHandler(logging.NullHandler())
1054
self.counting_filter = self.CountingFilter()
1056
class CountingFilter(logging.Filter):
1057
"Count number of records"
1059
def filter(self, record):
1063
def test_should_filter_records_only_when_active(self):
1075
class Test_get_managed_objects(TestCaseWithAssertLogs):
1076
def test_calls_and_returns_GetManagedObjects(self):
1077
managed_objects = {"/clients/foo": { "Name": "foo"}}
1078
class MockObjectManager(object):
1079
def GetManagedObjects(self):
1080
return managed_objects
1081
retval = get_managed_objects(MockObjectManager())
1082
self.assertDictEqual(managed_objects, retval)
1084
def test_logs_and_exits_on_dbus_error(self):
1085
dbus_logger = logging.getLogger("dbus.proxies")
1087
class MockObjectManagerFailing(object):
1088
def GetManagedObjects(self):
1089
dbus_logger.error("Test")
1090
raise dbus.exceptions.DBusException("Test")
1092
class CountingHandler(logging.Handler):
1094
def emit(self, record):
1097
counting_handler = CountingHandler()
1099
dbus_logger.addHandler(counting_handler)
1065
with SilenceLogger(self.loggername):
1066
self.log.addFilter(self.counting_filter)
1067
self.log.info("Filtered log message 1")
1068
self.log.info("Non-filtered message 2")
1069
self.log.info("Non-filtered message 3")
1102
with self.assertLogs(log, logging.CRITICAL) as watcher:
1103
with self.assertRaises(SystemExit) as e:
1104
get_managed_objects(MockObjectManagerFailing())
1071
self.log.removeFilter(self.counting_filter)
1072
self.assertEqual(self.counting_filter.count, 2)
1106
dbus_logger.removeFilter(counting_handler)
1108
# Make sure the dbus logger was suppressed
1109
self.assertEqual(counting_handler.count, 0)
1111
# Test that the dbus_logger still works
1112
with self.assertLogs(dbus_logger, logging.ERROR):
1113
dbus_logger.error("Test")
1115
if isinstance(e.exception.code, int):
1116
self.assertNotEqual(e.exception.code, 0)
1118
self.assertIsNotNone(e.exception.code)
1075
1121
class Test_commands_from_options(unittest.TestCase):