/mandos/trunk

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

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2019-03-15 19:54:19 UTC
  • Revision ID: teddy@recompile.se-20190315195419-d58e5t1gofoh24x5
mandos-ctl: Refactor

* mandos-ctl (main): Use SilenceLogger().
  (SilenceLogger): New.
  (Test_SilenceLogger): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
108
108
    mandos_serv_object_manager = dbus.Interface(
109
109
        mandos_dbus_objc, dbus_interface=dbus.OBJECT_MANAGER_IFACE)
110
110
 
111
 
    # Filter out log message from dbus module
112
 
    dbus_logger = logging.getLogger("dbus.proxies")
113
 
    class NullFilter(logging.Filter):
114
 
        def filter(self, record):
115
 
            return False
116
 
    dbus_filter = NullFilter()
117
111
    try:
118
 
        dbus_logger.addFilter(dbus_filter)
119
112
        log.debug("D-Bus: %s:%s:%s.GetManagedObjects()", dbus_busname,
120
113
                  server_dbus_path, dbus.OBJECT_MANAGER_IFACE)
121
 
        mandos_clients = {path: ifs_and_props[client_dbus_interface]
122
 
                          for path, ifs_and_props in
123
 
                          mandos_serv_object_manager
124
 
                          .GetManagedObjects().items()
125
 
                          if client_dbus_interface in ifs_and_props}
 
114
        with SilenceLogger("dbus.proxies"):
 
115
            all_clients = {path: ifs_and_props[client_dbus_interface]
 
116
                              for path, ifs_and_props in
 
117
                              mandos_serv_object_manager
 
118
                              .GetManagedObjects().items()
 
119
                              if client_dbus_interface in ifs_and_props}
126
120
    except dbus.exceptions.DBusException as e:
127
121
        log.critical("Failed to access Mandos server through D-Bus:"
128
122
                     "\n%s", e)
129
123
        sys.exit(1)
130
 
    finally:
131
 
        # restore dbus logger
132
 
        dbus_logger.removeFilter(dbus_filter)
133
124
 
134
125
    # Compile dict of (clients: properties) to process
135
126
    clients = {}
136
127
 
137
128
    if not clientnames:
138
 
        clients = {objpath: properties
139
 
                   for objpath, properties in mandos_clients.items()}
 
129
        clients = all_clients
140
130
    else:
141
131
        for name in clientnames:
142
 
            for objpath, properties in mandos_clients.items():
 
132
            for objpath, properties in all_clients.items():
143
133
                if properties["Name"] == name:
144
134
                    clients[objpath] = properties
145
135
                    break
446
436
        options.remove = True
447
437
 
448
438
 
 
439
class SilenceLogger(object):
 
440
    "Simple context manager to silence a particular logger"
 
441
    def __init__(self, loggername):
 
442
        self.logger = logging.getLogger(loggername)
 
443
 
 
444
    def __enter__(self):
 
445
        self.logger.addFilter(self.nullfilter)
 
446
        return self
 
447
 
 
448
    class NullFilter(logging.Filter):
 
449
        def filter(self, record):
 
450
            return False
 
451
 
 
452
    nullfilter = NullFilter()
 
453
 
 
454
    def __exit__(self, exc_type, exc_val, exc_tb):
 
455
        self.logger.removeFilter(self.nullfilter)
 
456
 
 
457
 
449
458
def commands_from_options(options):
450
459
 
451
460
    commands = []
636
645
            "LastCheckerStatus": "Last Checker Status",
637
646
        }
638
647
 
639
 
        def __init__(self, clients, keywords, tableheaders=None):
 
648
        def __init__(self, clients, keywords):
640
649
            self.clients = clients
641
650
            self.keywords = keywords
642
 
            if tableheaders is not None:
643
 
                self.tableheaders = tableheaders
644
651
 
645
652
        def __str__(self):
646
653
            return "\n".join(self.rows())
981
988
                self.check_option_syntax(options)
982
989
 
983
990
 
 
991
class Test_SilenceLogger(unittest.TestCase):
 
992
    loggername = "mandos-ctl.Test_SilenceLogger"
 
993
    log = logging.getLogger(loggername)
 
994
    log.propagate = False
 
995
    log.addHandler(logging.NullHandler())
 
996
 
 
997
    def setUp(self):
 
998
        self.counting_filter = self.CountingFilter()
 
999
 
 
1000
    class CountingFilter(logging.Filter):
 
1001
        "Count number of records"
 
1002
        count = 0
 
1003
        def filter(self, record):
 
1004
            self.count += 1
 
1005
            return True
 
1006
 
 
1007
    def test_should_filter_records_only_when_active(self):
 
1008
        try:
 
1009
            with SilenceLogger(self.loggername):
 
1010
                self.log.addFilter(self.counting_filter)
 
1011
                self.log.info("Filtered log message 1")
 
1012
            self.log.info("Non-filtered message 2")
 
1013
            self.log.info("Non-filtered message 3")
 
1014
        finally:
 
1015
            self.log.removeFilter(self.counting_filter)
 
1016
        self.assertEqual(self.counting_filter.count, 2)
 
1017
 
 
1018
 
984
1019
class Test_commands_from_options(unittest.TestCase):
985
1020
    def setUp(self):
986
1021
        self.parser = argparse.ArgumentParser()