/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2019-03-16 00:10:31 UTC
  • mto: This revision was merged to the branch mainline in revision 382.
  • Revision ID: teddy@recompile.se-20190316001031-c5v8it0q3qbvpskx
mandos-ctl: Refactor

* mandos-ctl (main): Call get_managed_objects() to get all client
                     paths and properties.
  (get_managed_objects): New.
  (Test_get_managed_objects): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
102
102
    mandos_serv_object_manager = dbus.Interface(
103
103
        mandos_dbus_object, dbus_interface=dbus.OBJECT_MANAGER_IFACE)
104
104
 
105
 
    log.debug("D-Bus: %s:%s:%s.GetManagedObjects()", dbus_busname,
106
 
              server_dbus_path, dbus.OBJECT_MANAGER_IFACE)
107
 
    try:
108
 
        with SilenceLogger("dbus.proxies"):
109
 
            managed_objects = (mandos_serv_object_manager
110
 
                               .GetManagedObjects())
111
 
    except dbus.exceptions.DBusException as e:
112
 
        log.critical("Failed to access Mandos server through D-Bus:"
113
 
                     "\n%s", e)
114
 
        sys.exit(1)
 
105
    managed_objects = get_managed_objects(mandos_serv_object_manager)
115
106
 
116
107
    all_clients = {}
117
108
    for path, ifs_and_props in managed_objects.items():
446
437
    return mandos_dbus_object
447
438
 
448
439
 
 
440
def get_managed_objects(object_manager):
 
441
    log.debug("D-Bus: %s:%s:%s.GetManagedObjects()", dbus_busname,
 
442
              server_dbus_path, dbus.OBJECT_MANAGER_IFACE)
 
443
    try:
 
444
        with SilenceLogger("dbus.proxies"):
 
445
            managed_objects = object_manager.GetManagedObjects()
 
446
    except dbus.exceptions.DBusException as e:
 
447
        log.critical("Failed to access Mandos server through D-Bus:"
 
448
                     "\n%s", e)
 
449
        sys.exit(1)
 
450
    return managed_objects
 
451
 
 
452
 
449
453
class SilenceLogger(object):
450
454
    "Simple context manager to silence a particular logger"
451
455
    def __init__(self, loggername):
1047
1051
            return not is_critical
1048
1052
 
1049
1053
 
 
1054
class Test_get_managed_objects(unittest.TestCase):
 
1055
    def test_calls_and_returns_GetManagedObjects(self):
 
1056
        managed_objects = {"/clients/foo": { "Name": "foo"}}
 
1057
        class MockObjectManager(object):
 
1058
            @staticmethod
 
1059
            def GetManagedObjects():
 
1060
                return managed_objects
 
1061
        retval = get_managed_objects(MockObjectManager())
 
1062
        self.assertDictEqual(managed_objects, retval)
 
1063
 
 
1064
    def test_logs_and_exits_on_dbus_error(self):
 
1065
        class MockObjectManagerFailing(object):
 
1066
            @staticmethod
 
1067
            def GetManagedObjects():
 
1068
                raise dbus.exceptions.DBusException("Test")
 
1069
 
 
1070
        if hasattr(self, "assertLogs"):
 
1071
            with self.assertLogs(log, logging.CRITICAL):
 
1072
                with self.assertRaises(SystemExit):
 
1073
                    get_managed_objects(MockObjectManagerFailing())
 
1074
        else:
 
1075
            critical_filter = self.CriticalFilter()
 
1076
            log.addFilter(critical_filter)
 
1077
            try:
 
1078
                with self.assertRaises(SystemExit) as e:
 
1079
                    get_managed_objects(MockObjectManagerFailing())
 
1080
            finally:
 
1081
                log.removeFilter(critical_filter)
 
1082
            self.assertTrue(critical_filter.found)
 
1083
        if isinstance(e.exception.code, int):
 
1084
            self.assertNotEqual(e.exception.code, 0)
 
1085
        else:
 
1086
            self.assertIsNotNone(e.exception.code)
 
1087
 
 
1088
    class CriticalFilter(logging.Filter):
 
1089
        """Don't show, but register, critical messages"""
 
1090
        found = False
 
1091
        def filter(self, record):
 
1092
            is_critical = record.levelno >= logging.CRITICAL
 
1093
            self.found = is_critical or self.found
 
1094
            return not is_critical
 
1095
 
 
1096
 
1050
1097
class Test_SilenceLogger(unittest.TestCase):
1051
1098
    loggername = "mandos-ctl.Test_SilenceLogger"
1052
1099
    log = logging.getLogger(loggername)