/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:30:17 UTC
  • mfrom: (1045.1.3 trunk)
  • Revision ID: teddy@recompile.se-20190309183017-5z0892yksb1rpulx
mandos-ctl: Merge refactoring changes and debug output

Show diffs side-by-side

added added

removed removed

Lines of Context:
294
294
                    "ApprovalDuration", "Checker", "ExtendedTimeout",
295
295
                    "Expires", "LastCheckerStatus")
296
296
    def run(self, mandos, clients):
297
 
        print(self.output(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"""
336
336
        keywords = default_keywords
337
337
        if self.verbose:
338
338
            keywords = self.all_keywords
339
 
        return str(self.TableOfClients(clients.values(), keywords))
 
339
        return str(self.TableOfClients(clients, keywords))
340
340
 
341
341
    class TableOfClients(object):
342
342
        tableheaders = {
433
433
            sys.exit(0)
434
434
        sys.exit(1)
435
435
    def is_enabled(self, client, properties):
436
 
        return bool(properties["Enabled"])
 
436
        log.debug("D-Bus: %s:%s:%s.Get(%r, %r)", busname,
 
437
                  client.__dbus_object_path__,
 
438
                  dbus.PROPERTIES_IFACE, client_interface,
 
439
                  "Enabled")
 
440
        return bool(client.Get(client_interface, "Enabled",
 
441
                               dbus_interface=dbus.PROPERTIES_IFACE))
437
442
 
438
443
class RemoveCmd(Command):
439
444
    def run_on_one_client(self, client, properties):
611
616
    if options.is_enabled:
612
617
        commands.append(IsEnabledCmd())
613
618
 
614
 
    if options.remove:
615
 
        commands.append(RemoveCmd())
616
 
 
617
619
    if options.checker is not None:
618
620
        commands.append(SetCheckerCmd(options.checker))
619
621
 
652
654
    if options.deny:
653
655
        commands.append(DenyCmd())
654
656
 
 
657
    if options.remove:
 
658
        commands.append(RemoveCmd())
 
659
 
655
660
    # If no command option has been given, show table of clients,
656
661
    # optionally verbosely
657
662
    if not commands:
695
700
        parser.error("--all requires an action.")
696
701
    if options.is_enabled and len(options.client) > 1:
697
702
        parser.error("--is-enabled requires exactly one client")
 
703
    if options.remove:
 
704
        options.remove = False
 
705
        if has_actions(options) and not options.deny:
 
706
            parser.error("--remove can only be combined with --deny")
 
707
        options.remove = True
698
708
 
699
709
 
700
710
def main():
752
762
    clients = {}
753
763
 
754
764
    if not clientnames:
755
 
        clients = {bus.get_object(busname, path): properties
 
765
        clients = {(log.debug("D-Bus: Connect to: (name=%r, path=%r)",
 
766
                              busname, str(path)) and False) or
 
767
                   bus.get_object(busname, path): properties
756
768
                   for path, properties in mandos_clients.items()}
757
769
    else:
758
770
        for name in clientnames:
759
771
            for path, client in mandos_clients.items():
760
772
                if client["Name"] == name:
 
773
                    log.debug("D-Bus: Connect to: (name=%r, path=%r)",
 
774
                              busname, str(path))
761
775
                    client_objc = bus.get_object(busname, path)
762
776
                    clients[client_objc] = client
763
777
                    break
898
912
 
899
913
class TestPrintTableCmd(TestCmd):
900
914
    def test_normal(self):
901
 
        output = PrintTableCmd().output(self.clients)
 
915
        output = PrintTableCmd().output(self.clients.values())
902
916
        expected_output = """
903
917
Name   Enabled Timeout  Last Successful Check
904
918
foo    Yes     00:05:00 2019-02-03T00:00:00  
906
920
"""[1:-1]
907
921
        self.assertEqual(output, expected_output)
908
922
    def test_verbose(self):
909
 
        output = PrintTableCmd(verbose=True).output(self.clients)
 
923
        output = PrintTableCmd(verbose=True).output(
 
924
            self.clients.values())
910
925
        expected_output = """
911
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
912
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                  
914
929
"""[1:-1]
915
930
        self.assertEqual(output, expected_output)
916
931
    def test_one_client(self):
917
 
        output = PrintTableCmd().output(self.one_client)
 
932
        output = PrintTableCmd().output(self.one_client.values())
918
933
        expected_output = """
919
934
Name Enabled Timeout  Last Successful Check
920
935
foo  Yes     00:05:00 2019-02-03T00:00:00  
1355
1370
    def test_is_enabled_short(self):
1356
1371
        self.assert_command_from_args(["-V", "foo"], IsEnabledCmd)
1357
1372
 
 
1373
    def test_deny_before_remove(self):
 
1374
        options = self.parser.parse_args(["--deny", "--remove", "foo"])
 
1375
        check_option_syntax(self.parser, options)
 
1376
        commands = commands_from_options(options)
 
1377
        self.assertEqual(len(commands), 2)
 
1378
        self.assertIsInstance(commands[0], DenyCmd)
 
1379
        self.assertIsInstance(commands[1], RemoveCmd)
 
1380
 
 
1381
    def test_deny_before_remove_reversed(self):
 
1382
        options = self.parser.parse_args(["--remove", "--deny", "--all"])
 
1383
        check_option_syntax(self.parser, options)
 
1384
        commands = commands_from_options(options)
 
1385
        self.assertEqual(len(commands), 2)
 
1386
        self.assertIsInstance(commands[0], DenyCmd)
 
1387
        self.assertIsInstance(commands[1], RemoveCmd)
 
1388
 
1358
1389
 
1359
1390
class Test_check_option_syntax(unittest.TestCase):
1360
1391
    # This mostly corresponds to the definition from has_actions() in
1475
1506
        with self.assertParseError():
1476
1507
            self.check_option_syntax(options)
1477
1508
 
 
1509
    def test_remove_can_only_be_combined_with_action_deny(self):
 
1510
        for action, value in self.actions.items():
 
1511
            if action in {"remove", "deny"}:
 
1512
                continue
 
1513
            options = self.parser.parse_args()
 
1514
            setattr(options, action, value)
 
1515
            options.all = True
 
1516
            options.remove = True
 
1517
            with self.assertParseError():
 
1518
                self.check_option_syntax(options)
 
1519
 
1478
1520
 
1479
1521
 
1480
1522
def should_only_run_tests():