/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 at recompile
  • Date: 2019-12-04 23:52:20 UTC
  • Revision ID: teddy@recompile.se-20191204235220-yn88brp0scmqwy2t
Remove non-project-specific pattern from .bzrignore

Only project-specific patterns should be in the .bzrignore file.
Other generic patterns should be in the ~/.config/breezy/ignore and
~/.bazaar/ignore files.

* .bzrignore (.tramp_history): Remove.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
# -*- mode: python; coding: utf-8; after-save-hook: (lambda () (let ((command (if (and (boundp 'tramp-file-name-structure) (string-match (car tramp-file-name-structure) (buffer-file-name))) (tramp-file-name-localname (tramp-dissect-file-name (buffer-file-name))) (buffer-file-name)))) (if (= (shell-command (format "%s --check" (shell-quote-argument command)) "*Test*") 0) (let ((w (get-buffer-window "*Test*"))) (if w (delete-window w)) (kill-buffer "*Test*")) (display-buffer "*Test*")))); -*-
 
1
#!/usr/bin/python3 -bbI
 
2
# -*- after-save-hook: (lambda () (let ((command (if (fboundp 'file-local-name) (file-local-name (buffer-file-name)) (or (file-remote-p (buffer-file-name) 'localname) (buffer-file-name))))) (if (= (progn (if (get-buffer "*Test*") (kill-buffer "*Test*")) (process-file-shell-command (format "%s --check" (shell-quote-argument command)) nil "*Test*")) 0) (let ((w (get-buffer-window "*Test*"))) (if w (delete-window w))) (progn (with-current-buffer "*Test*" (compilation-mode)) (display-buffer "*Test*" '(display-buffer-in-side-window)))))); coding: utf-8 -*-
3
3
#
4
4
# Mandos Monitor - Control and monitor the Mandos server
5
5
#
46
46
import tempfile
47
47
import contextlib
48
48
 
 
49
if sys.version_info.major == 2:
 
50
    __metaclass__ = type
 
51
    str = unicode
 
52
 
49
53
try:
50
54
    import pydbus
51
55
    import gi
53
57
except ImportError:
54
58
    import dbus as dbus_python
55
59
    pydbus = None
56
 
    class gi(object):
 
60
    class gi:
57
61
        """Dummy gi module, for the tests"""
58
 
        class repository(object):
59
 
            class GLib(object):
 
62
        class repository:
 
63
            class GLib:
60
64
                class Error(Exception):
61
65
                    pass
62
66
 
72
76
logging.captureWarnings(True)   # Show warnings via the logging system
73
77
 
74
78
if sys.version_info.major == 2:
75
 
    str = unicode
76
79
    import StringIO
77
80
    io.StringIO = StringIO.StringIO
78
81
 
79
82
locale.setlocale(locale.LC_ALL, "")
80
83
 
81
 
version = "1.8.3"
 
84
version = "1.8.9"
82
85
 
83
86
 
84
87
def main():
250
253
def rfc3339_duration_to_delta(duration):
251
254
    """Parse an RFC 3339 "duration" and return a datetime.timedelta
252
255
 
253
 
    >>> rfc3339_duration_to_delta("P7D")
254
 
    datetime.timedelta(7)
255
 
    >>> rfc3339_duration_to_delta("PT60S")
256
 
    datetime.timedelta(0, 60)
257
 
    >>> rfc3339_duration_to_delta("PT60M")
258
 
    datetime.timedelta(0, 3600)
259
 
    >>> rfc3339_duration_to_delta("P60M")
260
 
    datetime.timedelta(1680)
261
 
    >>> rfc3339_duration_to_delta("PT24H")
262
 
    datetime.timedelta(1)
263
 
    >>> rfc3339_duration_to_delta("P1W")
264
 
    datetime.timedelta(7)
265
 
    >>> rfc3339_duration_to_delta("PT5M30S")
266
 
    datetime.timedelta(0, 330)
267
 
    >>> rfc3339_duration_to_delta("P1DT3M20S")
268
 
    datetime.timedelta(1, 200)
 
256
    >>> rfc3339_duration_to_delta("P7D") == datetime.timedelta(7)
 
257
    True
 
258
    >>> rfc3339_duration_to_delta("PT60S") == datetime.timedelta(0, 60)
 
259
    True
 
260
    >>> rfc3339_duration_to_delta("PT60M") == datetime.timedelta(hours=1)
 
261
    True
 
262
    >>> # 60 months
 
263
    >>> rfc3339_duration_to_delta("P60M") == datetime.timedelta(1680)
 
264
    True
 
265
    >>> rfc3339_duration_to_delta("PT24H") == datetime.timedelta(1)
 
266
    True
 
267
    >>> rfc3339_duration_to_delta("P1W") == datetime.timedelta(7)
 
268
    True
 
269
    >>> rfc3339_duration_to_delta("PT5M30S") == datetime.timedelta(0, 330)
 
270
    True
 
271
    >>> rfc3339_duration_to_delta("P1DT3M20S") == datetime.timedelta(1, 200)
 
272
    True
269
273
    >>> # Can not be empty:
270
274
    >>> rfc3339_duration_to_delta("")
271
275
    Traceback (most recent call last):
381
385
    """Parse an interval string as documented by Mandos before 1.6.1,
382
386
    and return a datetime.timedelta
383
387
 
384
 
    >>> parse_pre_1_6_1_interval('7d')
385
 
    datetime.timedelta(7)
386
 
    >>> parse_pre_1_6_1_interval('60s')
387
 
    datetime.timedelta(0, 60)
388
 
    >>> parse_pre_1_6_1_interval('60m')
389
 
    datetime.timedelta(0, 3600)
390
 
    >>> parse_pre_1_6_1_interval('24h')
391
 
    datetime.timedelta(1)
392
 
    >>> parse_pre_1_6_1_interval('1w')
393
 
    datetime.timedelta(7)
394
 
    >>> parse_pre_1_6_1_interval('5m 30s')
395
 
    datetime.timedelta(0, 330)
396
 
    >>> parse_pre_1_6_1_interval('')
397
 
    datetime.timedelta(0)
 
388
    >>> parse_pre_1_6_1_interval('7d') == datetime.timedelta(days=7)
 
389
    True
 
390
    >>> parse_pre_1_6_1_interval('60s') == datetime.timedelta(0, 60)
 
391
    True
 
392
    >>> parse_pre_1_6_1_interval('60m') == datetime.timedelta(hours=1)
 
393
    True
 
394
    >>> parse_pre_1_6_1_interval('24h') == datetime.timedelta(days=1)
 
395
    True
 
396
    >>> parse_pre_1_6_1_interval('1w') == datetime.timedelta(days=7)
 
397
    True
 
398
    >>> parse_pre_1_6_1_interval('5m 30s') == datetime.timedelta(0, 330)
 
399
    True
 
400
    >>> parse_pre_1_6_1_interval('') == datetime.timedelta(0)
 
401
    True
398
402
    >>> # Ignore unknown characters, allow any order and repetitions
399
 
    >>> parse_pre_1_6_1_interval('2dxy7zz11y3m5m')
400
 
    datetime.timedelta(2, 480, 18000)
 
403
    >>> parse_pre_1_6_1_interval('2dxy7zz11y3m5m') == datetime.timedelta(2, 480, 18000)
 
404
    True
401
405
 
402
406
    """
403
407
 
466
470
        parser.error("--remove can only be combined with --deny")
467
471
 
468
472
 
469
 
class dbus(object):
 
473
class dbus:
470
474
 
471
 
    class SystemBus(object):
 
475
    class SystemBus:
472
476
 
473
477
        object_manager_iface = "org.freedesktop.DBus.ObjectManager"
474
478
        def get_managed_objects(self, busname, objectpath):
520
524
        pass
521
525
 
522
526
 
523
 
class dbus_python_adapter(object):
 
527
class dbus_python_adapter:
524
528
 
525
529
    class SystemBus(dbus.MandosBus):
526
530
        """Use dbus-python"""
571
575
                        for key, subval in value.items()}
572
576
            return value
573
577
 
 
578
        def set_client_property(self, objectpath, key, value):
 
579
            if key == "Secret":
 
580
                if not isinstance(value, bytes):
 
581
                    value = value.encode("utf-8")
 
582
                value = self.dbus_python.ByteArray(value)
 
583
            return self.set_property(self.busname, objectpath,
 
584
                                     self.client_interface, key,
 
585
                                     value)
574
586
 
575
 
    class SilenceLogger(object):
 
587
    class SilenceLogger:
576
588
        "Simple context manager to silence a particular logger"
577
589
        def __init__(self, loggername):
578
590
            self.logger = logging.getLogger(loggername)
607
619
                return new_object
608
620
 
609
621
 
610
 
class pydbus_adapter(object):
 
622
class pydbus_adapter:
611
623
    class SystemBus(dbus.MandosBus):
612
624
        def __init__(self, module=pydbus):
613
625
            self.pydbus = module
703
715
    return commands
704
716
 
705
717
 
706
 
class command(object):
 
718
class command:
707
719
    """A namespace for command classes"""
708
720
 
709
 
    class Base(object):
 
721
    class Base:
710
722
        """Abstract base class for commands"""
711
723
        def run(self, clients, bus=None):
712
724
            """Normal commands should implement run_on_one_client(),
775
787
                keywords = self.all_keywords
776
788
            print(self.TableOfClients(clients.values(), keywords))
777
789
 
778
 
        class TableOfClients(object):
 
790
        class TableOfClients:
779
791
            tableheaders = {
780
792
                "Name": "Name",
781
793
                "Enabled": "Enabled",
1012
1024
                                                     "output"))
1013
1025
 
1014
1026
 
1015
 
class Unique(object):
 
1027
class Unique:
1016
1028
    """Class for objects which exist only to be unique objects, since
1017
1029
unittest.mock.sentinel only exists in Python 3.3"""
1018
1030
 
1302
1314
class Test_dbus_python_adapter_SystemBus(TestCaseWithAssertLogs):
1303
1315
 
1304
1316
    def MockDBusPython_func(self, func):
1305
 
        class mock_dbus_python(object):
 
1317
        class mock_dbus_python:
1306
1318
            """mock dbus-python module"""
1307
 
            class exceptions(object):
 
1319
            class exceptions:
1308
1320
                """Pseudo-namespace"""
1309
1321
                class DBusException(Exception):
1310
1322
                    pass
1311
 
            class SystemBus(object):
 
1323
            class SystemBus:
1312
1324
                @staticmethod
1313
1325
                def get_object(busname, objectpath):
1314
1326
                    DBusObject = collections.namedtuple(
1315
 
                        "DBusObject", ("methodname",))
 
1327
                        "DBusObject", ("methodname", "Set"))
1316
1328
                    def method(*args, **kwargs):
1317
1329
                        self.assertEqual({"dbus_interface":
1318
1330
                                          "interface"},
1319
1331
                                         kwargs)
1320
1332
                        return func(*args)
1321
 
                    return DBusObject(methodname=method)
1322
 
            class Boolean(object):
 
1333
                    def set_property(interface, key, value,
 
1334
                                     dbus_interface=None):
 
1335
                        self.assertEqual(
 
1336
                            "org.freedesktop.DBus.Properties",
 
1337
                            dbus_interface)
 
1338
                        self.assertEqual("Secret", key)
 
1339
                        return func(interface, key, value,
 
1340
                                    dbus_interface=dbus_interface)
 
1341
                    return DBusObject(methodname=method,
 
1342
                                      Set=set_property)
 
1343
            class Boolean:
1323
1344
                def __init__(self, value):
1324
1345
                    self.value = bool(value)
1325
1346
                def __bool__(self):
1330
1351
                pass
1331
1352
            class Dictionary(dict):
1332
1353
                pass
 
1354
            class ByteArray(bytes):
 
1355
                pass
1333
1356
        return mock_dbus_python
1334
1357
 
1335
1358
    def call_method(self, bus, methodname, busname, objectpath,
1512
1535
        # Make sure the dbus logger was suppressed
1513
1536
        self.assertEqual(0, counting_handler.count)
1514
1537
 
 
1538
    def test_Set_Secret_sends_bytearray(self):
 
1539
        ret = [None]
 
1540
        def func(*args, **kwargs):
 
1541
            ret[0] = (args, kwargs)
 
1542
        mock_dbus_python = self.MockDBusPython_func(func)
 
1543
        bus = dbus_python_adapter.SystemBus(mock_dbus_python)
 
1544
        bus.set_client_property("objectpath", "Secret", "value")
 
1545
        expected_call = (("se.recompile.Mandos.Client", "Secret",
 
1546
                          mock_dbus_python.ByteArray(b"value")),
 
1547
                         {"dbus_interface":
 
1548
                          "org.freedesktop.DBus.Properties"})
 
1549
        self.assertEqual(expected_call, ret[0])
 
1550
        if sys.version_info.major == 2:
 
1551
            self.assertIsInstance(ret[0][0][-1],
 
1552
                                  mock_dbus_python.ByteArray)
 
1553
 
1515
1554
    def test_get_object_converts_to_correct_exception(self):
1516
1555
        bus = dbus_python_adapter.SystemBus(
1517
1556
            self.fake_dbus_python_raises_exception_on_connect)
1519
1558
            self.call_method(bus, "methodname", "busname",
1520
1559
                             "objectpath", "interface")
1521
1560
 
1522
 
    class fake_dbus_python_raises_exception_on_connect(object):
 
1561
    class fake_dbus_python_raises_exception_on_connect:
1523
1562
        """fake dbus-python module"""
1524
 
        class exceptions(object):
 
1563
        class exceptions:
1525
1564
            """Pseudo-namespace"""
1526
1565
            class DBusException(Exception):
1527
1566
                pass
1535
1574
 
1536
1575
 
1537
1576
class Test_dbus_python_adapter_CachingBus(unittest.TestCase):
1538
 
    class mock_dbus_python(object):
 
1577
    class mock_dbus_python:
1539
1578
        """mock dbus-python modules"""
1540
 
        class SystemBus(object):
 
1579
        class SystemBus:
1541
1580
            @staticmethod
1542
1581
            def get_object(busname, objectpath):
1543
1582
                return Unique()
1589
1628
class Test_pydbus_adapter_SystemBus(TestCaseWithAssertLogs):
1590
1629
 
1591
1630
    def Stub_pydbus_func(self, func):
1592
 
        class stub_pydbus(object):
 
1631
        class stub_pydbus:
1593
1632
            """stub pydbus module"""
1594
 
            class SystemBus(object):
 
1633
            class SystemBus:
1595
1634
                @staticmethod
1596
1635
                def get(busname, objectpath):
1597
1636
                    DBusObject = collections.namedtuple(
1643
1682
            self.call_method(bus, "methodname", "busname",
1644
1683
                             "objectpath", "interface")
1645
1684
 
1646
 
    class fake_pydbus_raises_exception_on_connect(object):
 
1685
    class fake_pydbus_raises_exception_on_connect:
1647
1686
        """fake dbus-python module"""
1648
1687
        @classmethod
1649
1688
        def SystemBus(cls):
1653
1692
            return Bus(get=get)
1654
1693
 
1655
1694
    def test_set_property_uses_setattr(self):
1656
 
        class Object(object):
 
1695
        class Object:
1657
1696
            pass
1658
1697
        obj = Object()
1659
 
        class pydbus_spy(object):
1660
 
            class SystemBus(object):
 
1698
        class pydbus_spy:
 
1699
            class SystemBus:
1661
1700
                @staticmethod
1662
1701
                def get(busname, objectpath):
1663
1702
                    return {"interface": obj}
1670
1709
    def test_get_suppresses_xml_deprecation_warning(self):
1671
1710
        if sys.version_info.major >= 3:
1672
1711
            return
1673
 
        class stub_pydbus_get(object):
1674
 
            class SystemBus(object):
 
1712
        class stub_pydbus_get:
 
1713
            class SystemBus:
1675
1714
                @staticmethod
1676
1715
                def get(busname, objectpath):
1677
1716
                    warnings.warn_explicit(
1685
1724
 
1686
1725
 
1687
1726
class Test_pydbus_adapter_CachingBus(unittest.TestCase):
1688
 
    class stub_pydbus(object):
 
1727
    class stub_pydbus:
1689
1728
        """stub pydbus module"""
1690
 
        class SystemBus(object):
 
1729
        class SystemBus:
1691
1730
            @staticmethod
1692
1731
            def get(busname, objectpath):
1693
1732
                return Unique()
1745
1784
        self.assert_command_from_args(["--is-enabled", "client"],
1746
1785
                                      command.IsEnabled)
1747
1786
 
1748
 
    def assert_command_from_args(self, args, command_cls,
1749
 
                                 **cmd_attrs):
 
1787
    def assert_command_from_args(self, args, command_cls, length=1,
 
1788
                                 clients=None, **cmd_attrs):
1750
1789
        """Assert that parsing ARGS should result in an instance of
1751
1790
COMMAND_CLS with (optionally) all supplied attributes (CMD_ATTRS)."""
1752
1791
        options = self.parser.parse_args(args)
1753
1792
        check_option_syntax(self.parser, options)
1754
1793
        commands = commands_from_options(options)
1755
 
        self.assertEqual(1, len(commands))
1756
 
        command = commands[0]
1757
 
        self.assertIsInstance(command, command_cls)
 
1794
        self.assertEqual(length, len(commands))
 
1795
        for command in commands:
 
1796
            if isinstance(command, command_cls):
 
1797
                break
 
1798
        else:
 
1799
            self.assertIsInstance(command, command_cls)
 
1800
        if clients is not None:
 
1801
            self.assertEqual(clients, options.client)
1758
1802
        for key, value in cmd_attrs.items():
1759
1803
            self.assertEqual(value, getattr(command, key))
1760
1804
 
 
1805
    def assert_commands_from_args(self, args, commands, clients=None):
 
1806
        for cmd in commands:
 
1807
            self.assert_command_from_args(args, cmd,
 
1808
                                          length=len(commands),
 
1809
                                          clients=clients)
 
1810
 
1761
1811
    def test_is_enabled_short(self):
1762
1812
        self.assert_command_from_args(["-V", "client"],
1763
1813
                                      command.IsEnabled)
1954
2004
                                      verbose=True)
1955
2005
 
1956
2006
 
 
2007
    def test_manual_page_example_1(self):
 
2008
        self.assert_command_from_args("",
 
2009
                                      command.PrintTable,
 
2010
                                      clients=[],
 
2011
                                      verbose=False)
 
2012
 
 
2013
    def test_manual_page_example_2(self):
 
2014
        self.assert_command_from_args(
 
2015
            "--verbose foo1.example.org foo2.example.org".split(),
 
2016
            command.PrintTable, clients=["foo1.example.org",
 
2017
                                         "foo2.example.org"],
 
2018
            verbose=True)
 
2019
 
 
2020
    def test_manual_page_example_3(self):
 
2021
        self.assert_command_from_args("--enable --all".split(),
 
2022
                                      command.Enable,
 
2023
                                      clients=[])
 
2024
 
 
2025
    def test_manual_page_example_4(self):
 
2026
        self.assert_commands_from_args(
 
2027
            ("--timeout=PT5M --interval=PT1M foo1.example.org"
 
2028
             " foo2.example.org").split(),
 
2029
            [command.SetTimeout, command.SetInterval],
 
2030
            clients=["foo1.example.org", "foo2.example.org"])
 
2031
 
 
2032
    def test_manual_page_example_5(self):
 
2033
        self.assert_command_from_args("--approve --all".split(),
 
2034
                                      command.Approve,
 
2035
                                      clients=[])
 
2036
 
 
2037
 
1957
2038
class TestCommand(unittest.TestCase):
1958
2039
    """Abstract class for tests of command classes"""
1959
2040