/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-15 19:54:19 UTC
  • mto: This revision was merged to the branch mainline in revision 382.
  • 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
 
        all_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 = {}
445
436
        options.remove = True
446
437
 
447
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
 
448
458
def commands_from_options(options):
449
459
 
450
460
    commands = []
978
988
                self.check_option_syntax(options)
979
989
 
980
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
 
981
1019
class Test_commands_from_options(unittest.TestCase):
982
1020
    def setUp(self):
983
1021
        self.parser = argparse.ArgumentParser()