/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-21 18:37:29 UTC
  • Revision ID: teddy@recompile.se-20190321183729-6okcpelqgalk1p6k
mandos-ctl: Refactor

* mandos-ctl (main): Remove a couple of blank lines.
  (SilenceLogger.__enter__): Remove unnecessary return statement.
  (commands.Property): Rename to "PropertySetter".
  (commands.PropertyValue): Rename to "PropertySetterValue".
  (commands.MillisecondsPropertyValueArgument): Rename to
                                    "PropertySetterValueMilliseconds".
  (TestPropertyCmd): Rename to "TestPropertySetterCmd".
  (TestPropertyValueCmd): Rename to "TestPropertySetterValueCmd".

Show diffs side-by-side

added added

removed removed

Lines of Context:
83
83
 
84
84
def main():
85
85
    parser = argparse.ArgumentParser()
86
 
 
87
86
    add_command_line_options(parser)
88
87
 
89
88
    options = parser.parse_args()
90
 
 
91
89
    check_option_syntax(parser, options)
92
90
 
93
91
    clientnames = options.client
462
460
 
463
461
    def __enter__(self):
464
462
        self.logger.addFilter(self.nullfilter)
465
 
        return self
466
463
 
467
464
    class NullFilter(logging.Filter):
468
465
        def filter(self, record):
728
725
                                seconds=td.seconds % 60))
729
726
 
730
727
 
731
 
    class Property(Base):
 
728
    class PropertySetter(Base):
732
729
        "Abstract class for Actions for setting one client property"
733
730
 
734
731
        def run_on_one_client(self, client, properties):
749
746
            raise NotImplementedError()
750
747
 
751
748
 
752
 
    class Enable(Property):
 
749
    class Enable(PropertySetter):
753
750
        propname = "Enabled"
754
751
        value_to_set = dbus.Boolean(True)
755
752
 
756
753
 
757
 
    class Disable(Property):
 
754
    class Disable(PropertySetter):
758
755
        propname = "Enabled"
759
756
        value_to_set = dbus.Boolean(False)
760
757
 
761
758
 
762
 
    class BumpTimeout(Property):
 
759
    class BumpTimeout(PropertySetter):
763
760
        propname = "LastCheckedOK"
764
761
        value_to_set = ""
765
762
 
766
763
 
767
 
    class StartChecker(Property):
768
 
        propname = "CheckerRunning"
769
 
        value_to_set = dbus.Boolean(True)
770
 
 
771
 
 
772
 
    class StopChecker(Property):
773
 
        propname = "CheckerRunning"
774
 
        value_to_set = dbus.Boolean(False)
775
 
 
776
 
 
777
 
    class ApproveByDefault(Property):
778
 
        propname = "ApprovedByDefault"
779
 
        value_to_set = dbus.Boolean(True)
780
 
 
781
 
 
782
 
    class DenyByDefault(Property):
783
 
        propname = "ApprovedByDefault"
784
 
        value_to_set = dbus.Boolean(False)
785
 
 
786
 
 
787
 
    class PropertyValue(Property):
788
 
        "Abstract class for Property recieving a value as argument"
 
764
    class StartChecker(PropertySetter):
 
765
        propname = "CheckerRunning"
 
766
        value_to_set = dbus.Boolean(True)
 
767
 
 
768
 
 
769
    class StopChecker(PropertySetter):
 
770
        propname = "CheckerRunning"
 
771
        value_to_set = dbus.Boolean(False)
 
772
 
 
773
 
 
774
    class ApproveByDefault(PropertySetter):
 
775
        propname = "ApprovedByDefault"
 
776
        value_to_set = dbus.Boolean(True)
 
777
 
 
778
 
 
779
    class DenyByDefault(PropertySetter):
 
780
        propname = "ApprovedByDefault"
 
781
        value_to_set = dbus.Boolean(False)
 
782
 
 
783
 
 
784
    class PropertySetterValue(PropertySetter):
 
785
        """Abstract class for PropertySetter recieving a value as
 
786
constructor argument instead of a class attribute."""
789
787
        def __init__(self, value):
790
788
            self.value_to_set = value
791
789
 
792
790
 
793
 
    class SetChecker(PropertyValue):
 
791
    class SetChecker(PropertySetterValue):
794
792
        propname = "Checker"
795
793
 
796
794
 
797
 
    class SetHost(PropertyValue):
 
795
    class SetHost(PropertySetterValue):
798
796
        propname = "Host"
799
797
 
800
798
 
801
 
    class SetSecret(PropertyValue):
 
799
    class SetSecret(PropertySetterValue):
802
800
        propname = "Secret"
803
801
 
804
802
        @property
812
810
            value.close()
813
811
 
814
812
 
815
 
    class MillisecondsPropertyValueArgument(PropertyValue):
816
 
        """Abstract class for PropertyValue taking a value argument as
817
 
a datetime.timedelta() but should store it as milliseconds."""
 
813
    class PropertySetterValueMilliseconds(PropertySetterValue):
 
814
        """Abstract class for PropertySetterValue taking a value
 
815
argument as a datetime.timedelta() but should store it as
 
816
milliseconds."""
818
817
 
819
818
        @property
820
819
        def value_to_set(self):
826
825
            self._vts = int(round(value.total_seconds() * 1000))
827
826
 
828
827
 
829
 
    class SetTimeout(MillisecondsPropertyValueArgument):
 
828
    class SetTimeout(PropertySetterValueMilliseconds):
830
829
        propname = "Timeout"
831
830
 
832
831
 
833
 
    class SetExtendedTimeout(MillisecondsPropertyValueArgument):
 
832
    class SetExtendedTimeout(PropertySetterValueMilliseconds):
834
833
        propname = "ExtendedTimeout"
835
834
 
836
835
 
837
 
    class SetInterval(MillisecondsPropertyValueArgument):
 
836
    class SetInterval(PropertySetterValueMilliseconds):
838
837
        propname = "Interval"
839
838
 
840
839
 
841
 
    class SetApprovalDelay(MillisecondsPropertyValueArgument):
 
840
    class SetApprovalDelay(PropertySetterValueMilliseconds):
842
841
        propname = "ApprovalDelay"
843
842
 
844
843
 
845
 
    class SetApprovalDuration(MillisecondsPropertyValueArgument):
 
844
    class SetApprovalDuration(PropertySetterValueMilliseconds):
846
845
        propname = "ApprovalDuration"
847
846
 
848
847
 
1699
1698
        self.assertEqual(expected_output, buffer.getvalue())
1700
1699
 
1701
1700
 
1702
 
class TestPropertyCmd(TestCommand):
1703
 
    """Abstract class for tests of command.Property classes"""
 
1701
class TestPropertySetterCmd(TestCommand):
 
1702
    """Abstract class for tests of command.PropertySetter classes"""
1704
1703
    def runTest(self):
1705
1704
        if not hasattr(self, "command"):
1706
1705
            return
1727
1726
        self.command().run(clients, self.bus)
1728
1727
 
1729
1728
 
1730
 
class TestEnableCmd(TestPropertyCmd):
 
1729
class TestEnableCmd(TestPropertySetterCmd):
1731
1730
    command = command.Enable
1732
1731
    propname = "Enabled"
1733
1732
    values_to_set = [dbus.Boolean(True)]
1734
1733
 
1735
1734
 
1736
 
class TestDisableCmd(TestPropertyCmd):
 
1735
class TestDisableCmd(TestPropertySetterCmd):
1737
1736
    command = command.Disable
1738
1737
    propname = "Enabled"
1739
1738
    values_to_set = [dbus.Boolean(False)]
1740
1739
 
1741
1740
 
1742
 
class TestBumpTimeoutCmd(TestPropertyCmd):
 
1741
class TestBumpTimeoutCmd(TestPropertySetterCmd):
1743
1742
    command = command.BumpTimeout
1744
1743
    propname = "LastCheckedOK"
1745
1744
    values_to_set = [""]
1746
1745
 
1747
1746
 
1748
 
class TestStartCheckerCmd(TestPropertyCmd):
 
1747
class TestStartCheckerCmd(TestPropertySetterCmd):
1749
1748
    command = command.StartChecker
1750
1749
    propname = "CheckerRunning"
1751
1750
    values_to_set = [dbus.Boolean(True)]
1752
1751
 
1753
1752
 
1754
 
class TestStopCheckerCmd(TestPropertyCmd):
 
1753
class TestStopCheckerCmd(TestPropertySetterCmd):
1755
1754
    command = command.StopChecker
1756
1755
    propname = "CheckerRunning"
1757
1756
    values_to_set = [dbus.Boolean(False)]
1758
1757
 
1759
1758
 
1760
 
class TestApproveByDefaultCmd(TestPropertyCmd):
 
1759
class TestApproveByDefaultCmd(TestPropertySetterCmd):
1761
1760
    command = command.ApproveByDefault
1762
1761
    propname = "ApprovedByDefault"
1763
1762
    values_to_set = [dbus.Boolean(True)]
1764
1763
 
1765
1764
 
1766
 
class TestDenyByDefaultCmd(TestPropertyCmd):
 
1765
class TestDenyByDefaultCmd(TestPropertySetterCmd):
1767
1766
    command = command.DenyByDefault
1768
1767
    propname = "ApprovedByDefault"
1769
1768
    values_to_set = [dbus.Boolean(False)]
1770
1769
 
1771
1770
 
1772
 
class TestPropertyValueCmd(TestPropertyCmd):
1773
 
    """Abstract class for tests of PropertyValueCmd classes"""
 
1771
class TestPropertySetterValueCmd(TestPropertySetterCmd):
 
1772
    """Abstract class for tests of PropertySetterValueCmd classes"""
1774
1773
 
1775
1774
    def runTest(self):
1776
 
        if type(self) is TestPropertyValueCmd:
 
1775
        if type(self) is TestPropertySetterValueCmd:
1777
1776
            return
1778
 
        return super(TestPropertyValueCmd, self).runTest()
 
1777
        return super(TestPropertySetterValueCmd, self).runTest()
1779
1778
 
1780
1779
    def run_command(self, value, clients):
1781
1780
        self.command(value).run(clients, self.bus)
1782
1781
 
1783
1782
 
1784
 
class TestSetCheckerCmd(TestPropertyValueCmd):
 
1783
class TestSetCheckerCmd(TestPropertySetterValueCmd):
1785
1784
    command = command.SetChecker
1786
1785
    propname = "Checker"
1787
1786
    values_to_set = ["", ":", "fping -q -- %s"]
1788
1787
 
1789
1788
 
1790
 
class TestSetHostCmd(TestPropertyValueCmd):
 
1789
class TestSetHostCmd(TestPropertySetterValueCmd):
1791
1790
    command = command.SetHost
1792
1791
    propname = "Host"
1793
1792
    values_to_set = ["192.0.2.3", "client.example.org"]
1794
1793
 
1795
1794
 
1796
 
class TestSetSecretCmd(TestPropertyValueCmd):
 
1795
class TestSetSecretCmd(TestPropertySetterValueCmd):
1797
1796
    command = command.SetSecret
1798
1797
    propname = "Secret"
1799
1798
    values_to_set = [io.BytesIO(b""),
1801
1800
    values_to_get = [f.getvalue() for f in values_to_set]
1802
1801
 
1803
1802
 
1804
 
class TestSetTimeoutCmd(TestPropertyValueCmd):
 
1803
class TestSetTimeoutCmd(TestPropertySetterValueCmd):
1805
1804
    command = command.SetTimeout
1806
1805
    propname = "Timeout"
1807
1806
    values_to_set = [datetime.timedelta(),
1812
1811
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]
1813
1812
 
1814
1813
 
1815
 
class TestSetExtendedTimeoutCmd(TestPropertyValueCmd):
 
1814
class TestSetExtendedTimeoutCmd(TestPropertySetterValueCmd):
1816
1815
    command = command.SetExtendedTimeout
1817
1816
    propname = "ExtendedTimeout"
1818
1817
    values_to_set = [datetime.timedelta(),
1823
1822
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]
1824
1823
 
1825
1824
 
1826
 
class TestSetIntervalCmd(TestPropertyValueCmd):
 
1825
class TestSetIntervalCmd(TestPropertySetterValueCmd):
1827
1826
    command = command.SetInterval
1828
1827
    propname = "Interval"
1829
1828
    values_to_set = [datetime.timedelta(),
1834
1833
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]
1835
1834
 
1836
1835
 
1837
 
class TestSetApprovalDelayCmd(TestPropertyValueCmd):
 
1836
class TestSetApprovalDelayCmd(TestPropertySetterValueCmd):
1838
1837
    command = command.SetApprovalDelay
1839
1838
    propname = "ApprovalDelay"
1840
1839
    values_to_set = [datetime.timedelta(),
1845
1844
    values_to_get = [dt.total_seconds()*1000 for dt in values_to_set]
1846
1845
 
1847
1846
 
1848
 
class TestSetApprovalDurationCmd(TestPropertyValueCmd):
 
1847
class TestSetApprovalDurationCmd(TestPropertySetterValueCmd):
1849
1848
    command = command.SetApprovalDuration
1850
1849
    propname = "ApprovalDuration"
1851
1850
    values_to_set = [datetime.timedelta(),