108
108
mandos_serv_object_manager = dbus.Interface(
109
109
mandos_dbus_objc, dbus_interface=dbus.OBJECT_MANAGER_IFACE)
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):
116
dbus_filter = NullFilter()
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:"
131
# restore dbus logger
132
dbus_logger.removeFilter(dbus_filter)
134
125
# Compile dict of (clients: properties) to process
137
128
if not clientnames:
138
clients = {objpath: properties
139
for objpath, properties in mandos_clients.items()}
129
clients = all_clients
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
446
436
options.remove = True
439
class SilenceLogger(object):
440
"Simple context manager to silence a particular logger"
441
def __init__(self, loggername):
442
self.logger = logging.getLogger(loggername)
445
self.logger.addFilter(self.nullfilter)
448
class NullFilter(logging.Filter):
449
def filter(self, record):
452
nullfilter = NullFilter()
454
def __exit__(self, exc_type, exc_val, exc_tb):
455
self.logger.removeFilter(self.nullfilter)
449
458
def commands_from_options(options):
636
645
"LastCheckerStatus": "Last Checker Status",
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
645
652
def __str__(self):
646
653
return "\n".join(self.rows())
981
988
self.check_option_syntax(options)
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())
998
self.counting_filter = self.CountingFilter()
1000
class CountingFilter(logging.Filter):
1001
"Count number of records"
1003
def filter(self, record):
1007
def test_should_filter_records_only_when_active(self):
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")
1015
self.log.removeFilter(self.counting_filter)
1016
self.assertEqual(self.counting_filter.count, 2)
984
1019
class Test_commands_from_options(unittest.TestCase):
985
1020
def setUp(self):
986
1021
self.parser = argparse.ArgumentParser()