/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-09 18:17:28 UTC
  • mto: This revision was merged to the branch mainline in revision 1046.
  • Revision ID: teddy@recompile.se-20190309181728-29wuri960s9xl8n0
mandos-ctl: Refactor; mostly revert commit 1046

* mandos-ctl (Command.run): No longer take "mandos_objmgr" argument.
                            All callers changed.
  (PrintTableCmd.output): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
276
276
# Abstract classes first
277
277
class Command(object):
278
278
    """Abstract class for commands"""
279
 
    def run(self, mandos, mandos_objmgr, clients):
 
279
    def run(self, mandos, clients):
280
280
        """Normal commands should implement run_on_one_client(), but
281
281
        commands which want to operate on all clients at the same time
282
282
        can override this run() method instead."""
293
293
                    "LastApprovalRequest", "ApprovalDelay",
294
294
                    "ApprovalDuration", "Checker", "ExtendedTimeout",
295
295
                    "Expires", "LastCheckerStatus")
296
 
    def run(self, mandos, mandos_objmgr, clients):
297
 
        print(self.output(mandos_objmgr, clients.values()))
 
296
    def run(self, mandos, clients):
 
297
        print(self.output(clients.values()))
298
298
 
299
299
class PropertyCmd(Command):
300
300
    """Abstract class for Actions for setting one client property"""
331
331
    def __init__(self, verbose=False):
332
332
        self.verbose = verbose
333
333
 
334
 
    def output(self, mandos_objmgr, clients):
 
334
    def output(self, clients):
335
335
        default_keywords = ("Name", "Enabled", "Timeout", "LastCheckedOK")
336
336
        keywords = default_keywords
337
337
        if self.verbose:
415
415
 
416
416
 
417
417
class DumpJSONCmd(PrintCmd):
418
 
    def output(self, mandos_objmgr, clients):
 
418
    def output(self, clients):
419
419
        data = {client["Name"]:
420
420
                {key: self.dbus_boolean_to_bool(client[key])
421
421
                 for key in self.all_keywords}
782
782
    # Run all commands on clients
783
783
    commands = commands_from_options(options)
784
784
    for command in commands:
785
 
        command.run(mandos_serv, mandos_serv_object_manager, clients)
 
785
        command.run(mandos_serv, clients)
786
786
 
787
787
 
788
788
class Test_milliseconds_to_string(unittest.TestCase):
912
912
 
913
913
class TestPrintTableCmd(TestCmd):
914
914
    def test_normal(self):
915
 
        output = PrintTableCmd().output(None, self.clients.values())
 
915
        output = PrintTableCmd().output(self.clients.values())
916
916
        expected_output = """
917
917
Name   Enabled Timeout  Last Successful Check
918
918
foo    Yes     00:05:00 2019-02-03T00:00:00  
921
921
        self.assertEqual(output, expected_output)
922
922
    def test_verbose(self):
923
923
        output = PrintTableCmd(verbose=True).output(
924
 
            None, self.clients.values())
 
924
            self.clients.values())
925
925
        expected_output = """
926
926
Name   Enabled Timeout  Last Successful Check Created             Interval Host            Key ID                                                           Fingerprint                              Check Is Running Last Enabled        Approval Is Pending Approved By Default Last Approval Request Approval Delay Approval Duration Checker              Extended Timeout Expires             Last Checker Status
927
927
foo    Yes     00:05:00 2019-02-03T00:00:00   2019-01-02T00:00:00 00:02:00 foo.example.org 92ed150794387c03ce684574b1139a6594a34f895daaaf09fd8ea90a27cddb12 778827225BA7DE539C5A7CFA59CFF7CDBD9A5920 No               2019-01-03T00:00:00 No                  Yes                                       00:00:00       00:00:01          fping -q -- %(host)s 00:15:00         2019-02-04T00:00:00 0                  
929
929
"""[1:-1]
930
930
        self.assertEqual(output, expected_output)
931
931
    def test_one_client(self):
932
 
        output = PrintTableCmd().output(None, self.one_client.values())
 
932
        output = PrintTableCmd().output(self.one_client.values())
933
933
        expected_output = """
934
934
Name Enabled Timeout  Last Successful Check
935
935
foo  Yes     00:05:00 2019-02-03T00:00:00  
990
990
        }
991
991
        return super(TestDumpJSONCmd, self).setUp()
992
992
    def test_normal(self):
993
 
        json_data = json.loads(DumpJSONCmd().output(None,
994
 
                                                    self.clients))
 
993
        json_data = json.loads(DumpJSONCmd().output(self.clients))
995
994
        self.assertDictEqual(json_data, self.expected_json)
996
995
    def test_one_client(self):
997
996
        clients = self.one_client
998
 
        json_data = json.loads(DumpJSONCmd().output(None, clients))
 
997
        json_data = json.loads(DumpJSONCmd().output(clients))
999
998
        expected_json = {"foo": self.expected_json["foo"]}
1000
999
        self.assertDictEqual(json_data, expected_json)
1001
1000
 
1005
1004
                            for client, properties in self.clients.items()))
1006
1005
    def test_is_enabled_run_exits_successfully(self):
1007
1006
        with self.assertRaises(SystemExit) as e:
1008
 
            IsEnabledCmd().run(None, None, self.one_client)
 
1007
            IsEnabledCmd().run(None, self.one_client)
1009
1008
        if e.exception.code is not None:
1010
1009
            self.assertEqual(e.exception.code, 0)
1011
1010
        else:
1013
1012
    def test_is_enabled_run_exits_with_failure(self):
1014
1013
        self.client.attributes["Enabled"] = dbus.Boolean(False)
1015
1014
        with self.assertRaises(SystemExit) as e:
1016
 
            IsEnabledCmd().run(None, None, self.one_client)
 
1015
            IsEnabledCmd().run(None, self.one_client)
1017
1016
        if isinstance(e.exception.code, int):
1018
1017
            self.assertNotEqual(e.exception.code, 0)
1019
1018
        else:
1028
1027
                self.calls.append(("RemoveClient", (dbus_path,)))
1029
1028
        mandos = MockMandos()
1030
1029
        super(TestRemoveCmd, self).setUp()
1031
 
        RemoveCmd().run(mandos, None, self.clients)
 
1030
        RemoveCmd().run(mandos, self.clients)
1032
1031
        self.assertEqual(len(mandos.calls), 2)
1033
1032
        for client in self.clients:
1034
1033
            self.assertIn(("RemoveClient",
1037
1036
 
1038
1037
class TestApproveCmd(TestCmd):
1039
1038
    def test_approve(self):
1040
 
        ApproveCmd().run(None, None, self.clients)
 
1039
        ApproveCmd().run(None, self.clients)
1041
1040
        for client in self.clients:
1042
1041
            self.assertIn(("Approve", (True, client_interface)),
1043
1042
                          client.calls)
1044
1043
 
1045
1044
class TestDenyCmd(TestCmd):
1046
1045
    def test_deny(self):
1047
 
        DenyCmd().run(None, None, self.clients)
 
1046
        DenyCmd().run(None, self.clients)
1048
1047
        for client in self.clients:
1049
1048
            self.assertIn(("Approve", (False, client_interface)),
1050
1049
                          client.calls)
1054
1053
        for client in self.clients:
1055
1054
            client.attributes["Enabled"] = False
1056
1055
 
1057
 
        EnableCmd().run(None, None, self.clients)
 
1056
        EnableCmd().run(None, self.clients)
1058
1057
 
1059
1058
        for client in self.clients:
1060
1059
            self.assertTrue(client.attributes["Enabled"])
1061
1060
 
1062
1061
class TestDisableCmd(TestCmd):
1063
1062
    def test_disable(self):
1064
 
        DisableCmd().run(None, None, self.clients)
 
1063
        DisableCmd().run(None, self.clients)
1065
1064
 
1066
1065
        for client in self.clients:
1067
1066
            self.assertFalse(client.attributes["Enabled"])
1089
1088
                self.assertNotIsInstance(value, Unique)
1090
1089
                self.assertEqual(value, value_to_get)
1091
1090
    def run_command(self, value, clients):
1092
 
        self.command().run(None, None, clients)
 
1091
        self.command().run(None, clients)
1093
1092
 
1094
1093
class TestBumpTimeoutCmd(TestPropertyCmd):
1095
1094
    command = BumpTimeoutCmd
1124
1123
            return
1125
1124
        return super(TestValueArgumentPropertyCmd, self).runTest()
1126
1125
    def run_command(self, value, clients):
1127
 
        self.command(value).run(None, None, clients)
 
1126
        self.command(value).run(None, clients)
1128
1127
 
1129
1128
class TestSetCheckerCmd(TestValueArgumentPropertyCmd):
1130
1129
    command = SetCheckerCmd