/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2019-03-12 17:33:44 UTC
  • mto: This revision was merged to the branch mainline in revision 382.
  • Revision ID: teddy@recompile.se-20190312173344-13d0hhhkmwu3up78
mandos-ctl: Refactor

* mandos-ctl (domain): Rename to "dbus_busname_domain".  Delete after
                       use.
  (busname): Rename to "dbus_busname".  All users changed.
  (server_path): Rename to "server_dbus_path".  All users changed.
  (server_interface): Rename to "server_dbus_interface".  All users
                      changed.
  (client_interface): Rename to "client_dbus_interface".  All users
                      changed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
64
64
 
65
65
locale.setlocale(locale.LC_ALL, "")
66
66
 
67
 
domain = "se.recompile"
68
 
busname = domain + ".Mandos"
69
 
server_path = "/"
70
 
server_interface = domain + ".Mandos"
71
 
client_interface = domain + ".Mandos.Client"
 
67
dbus_busname_domain = "se.recompile"
 
68
dbus_busname = dbus_busname_domain + ".Mandos"
 
69
server_dbus_path = "/"
 
70
server_dbus_interface = dbus_busname_domain + ".Mandos"
 
71
client_dbus_interface = dbus_busname_domain + ".Mandos.Client"
 
72
del dbus_busname_domain
72
73
version = "1.8.3"
73
74
 
74
75
 
283
284
        self.mandos = mandos
284
285
        for clientpath, properties in clients.items():
285
286
            log.debug("D-Bus: Connect to: (busname=%r, path=%r)",
286
 
                      busname, str(clientpath))
287
 
            client = bus.get_object(busname, clientpath)
 
287
                      dbus_busname, str(clientpath))
 
288
            client = bus.get_object(dbus_busname, clientpath)
288
289
            self.run_on_one_client(client, properties)
289
290
 
290
291
class PrintCmd(Command):
305
306
    """Abstract class for Actions for setting one client property"""
306
307
    def run_on_one_client(self, client, properties):
307
308
        """Set the Client's D-Bus property"""
308
 
        log.debug("D-Bus: %s:%s:%s.Set(%r, %r, %r)", busname,
 
309
        log.debug("D-Bus: %s:%s:%s.Set(%r, %r, %r)", dbus_busname,
309
310
                  client.__dbus_object_path__,
310
 
                  dbus.PROPERTIES_IFACE, client_interface,
 
311
                  dbus.PROPERTIES_IFACE, client_dbus_interface,
311
312
                  self.propname, self.value_to_set
312
313
                  if not isinstance(self.value_to_set, dbus.Boolean)
313
314
                  else bool(self.value_to_set))
314
 
        client.Set(client_interface, self.propname, self.value_to_set,
 
315
        client.Set(client_dbus_interface, self.propname,
 
316
                   self.value_to_set,
315
317
                   dbus_interface=dbus.PROPERTIES_IFACE)
316
318
    @property
317
319
    def propname(self):
446
448
 
447
449
class RemoveCmd(Command):
448
450
    def run_on_one_client(self, client, properties):
449
 
        log.debug("D-Bus: %s:%s:%s.RemoveClient(%r)", busname,
450
 
                  server_path, server_interface,
 
451
        log.debug("D-Bus: %s:%s:%s.RemoveClient(%r)", dbus_busname,
 
452
                  server_dbus_path, server_dbus_interface,
451
453
                  str(client.__dbus_object_path__))
452
454
        self.mandos.RemoveClient(client.__dbus_object_path__)
453
455
 
454
456
class ApproveCmd(Command):
455
457
    def run_on_one_client(self, client, properties):
456
 
        log.debug("D-Bus: %s:%s:%s.Approve(True)", busname,
457
 
                  client.__dbus_object_path__, client_interface)
 
458
        log.debug("D-Bus: %s:%s:%s.Approve(True)", dbus_busname,
 
459
                  client.__dbus_object_path__, client_dbus_interface)
458
460
        client.Approve(dbus.Boolean(True),
459
 
                       dbus_interface=client_interface)
 
461
                       dbus_interface=client_dbus_interface)
460
462
 
461
463
class DenyCmd(Command):
462
464
    def run_on_one_client(self, client, properties):
463
 
        log.debug("D-Bus: %s:%s:%s.Approve(False)", busname,
464
 
                  client.__dbus_object_path__, client_interface)
 
465
        log.debug("D-Bus: %s:%s:%s.Approve(False)", dbus_busname,
 
466
                  client.__dbus_object_path__, client_dbus_interface)
465
467
        client.Approve(dbus.Boolean(False),
466
 
                       dbus_interface=client_interface)
 
468
                       dbus_interface=client_dbus_interface)
467
469
 
468
470
class EnableCmd(PropertyCmd):
469
471
    propname = "Enabled"
724
726
 
725
727
    try:
726
728
        bus = dbus.SystemBus()
727
 
        log.debug("D-Bus: Connect to: (busname=%r, path=%r)", busname,
728
 
                  server_path)
729
 
        mandos_dbus_objc = bus.get_object(busname, server_path)
 
729
        log.debug("D-Bus: Connect to: (busname=%r, path=%r)",
 
730
                  dbus_busname, server_dbus_path)
 
731
        mandos_dbus_objc = bus.get_object(dbus_busname,
 
732
                                          server_dbus_path)
730
733
    except dbus.exceptions.DBusException:
731
734
        log.critical("Could not connect to Mandos server")
732
735
        sys.exit(1)
733
736
 
734
737
    mandos_serv = dbus.Interface(mandos_dbus_objc,
735
 
                                 dbus_interface=server_interface)
 
738
                                 dbus_interface=server_dbus_interface)
736
739
    mandos_serv_object_manager = dbus.Interface(
737
740
        mandos_dbus_objc, dbus_interface=dbus.OBJECT_MANAGER_IFACE)
738
741
 
744
747
    dbus_filter = NullFilter()
745
748
    try:
746
749
        dbus_logger.addFilter(dbus_filter)
747
 
        log.debug("D-Bus: %s:%s:%s.GetManagedObjects()", busname,
748
 
                  server_path, dbus.OBJECT_MANAGER_IFACE)
749
 
        mandos_clients = {path: ifs_and_props[client_interface]
 
750
        log.debug("D-Bus: %s:%s:%s.GetManagedObjects()", dbus_busname,
 
751
                  server_dbus_path, dbus.OBJECT_MANAGER_IFACE)
 
752
        mandos_clients = {path: ifs_and_props[client_dbus_interface]
750
753
                          for path, ifs_and_props in
751
754
                          mandos_serv_object_manager
752
755
                          .GetManagedObjects().items()
753
 
                          if client_interface in ifs_and_props}
 
756
                          if client_dbus_interface in ifs_and_props}
754
757
    except dbus.exceptions.DBusException as e:
755
758
        log.critical("Failed to access Mandos server through D-Bus:"
756
759
                     "\n%s", e)
838
841
                self.attributes["Name"] = name
839
842
                self.calls = []
840
843
            def Set(self, interface, propname, value, dbus_interface):
841
 
                testcase.assertEqual(interface, client_interface)
 
844
                testcase.assertEqual(interface, client_dbus_interface)
842
845
                testcase.assertEqual(dbus_interface,
843
846
                                     dbus.PROPERTIES_IFACE)
844
847
                self.attributes[propname] = value
845
848
            def Get(self, interface, propname, dbus_interface):
846
 
                testcase.assertEqual(interface, client_interface)
 
849
                testcase.assertEqual(interface, client_dbus_interface)
847
850
                testcase.assertEqual(dbus_interface,
848
851
                                     dbus.PROPERTIES_IFACE)
849
852
                return self.attributes[propname]
850
853
            def Approve(self, approve, dbus_interface):
851
 
                testcase.assertEqual(dbus_interface, client_interface)
 
854
                testcase.assertEqual(dbus_interface,
 
855
                                     client_dbus_interface)
852
856
                self.calls.append(("Approve", (approve,
853
857
                                               dbus_interface)))
854
858
        self.client = MockClient(
910
914
        class Bus(object):
911
915
            @staticmethod
912
916
            def get_object(client_bus_name, path):
913
 
                self.assertEqual(client_bus_name, busname)
 
917
                self.assertEqual(client_bus_name, dbus_busname)
914
918
                return {
915
919
                    "/clients/foo": self.client,
916
920
                    "/clients/barbar": self.other_client,
1044
1048
    def test_approve(self):
1045
1049
        ApproveCmd().run(self.clients, self.bus)
1046
1050
        for clientpath in self.clients:
1047
 
            client = self.bus.get_object(busname, clientpath)
1048
 
            self.assertIn(("Approve", (True, client_interface)),
 
1051
            client = self.bus.get_object(dbus_busname, clientpath)
 
1052
            self.assertIn(("Approve", (True, client_dbus_interface)),
1049
1053
                          client.calls)
1050
1054
 
1051
1055
class TestDenyCmd(TestCmd):
1052
1056
    def test_deny(self):
1053
1057
        DenyCmd().run(self.clients, self.bus)
1054
1058
        for clientpath in self.clients:
1055
 
            client = self.bus.get_object(busname, clientpath)
1056
 
            self.assertIn(("Approve", (False, client_interface)),
 
1059
            client = self.bus.get_object(dbus_busname, clientpath)
 
1060
            self.assertIn(("Approve", (False, client_dbus_interface)),
1057
1061
                          client.calls)
1058
1062
 
1059
1063
class TestEnableCmd(TestCmd):
1060
1064
    def test_enable(self):
1061
1065
        for clientpath in self.clients:
1062
 
            client = self.bus.get_object(busname, clientpath)
 
1066
            client = self.bus.get_object(dbus_busname, clientpath)
1063
1067
            client.attributes["Enabled"] = False
1064
1068
 
1065
1069
        EnableCmd().run(self.clients, self.bus)
1066
1070
 
1067
1071
        for clientpath in self.clients:
1068
 
            client = self.bus.get_object(busname, clientpath)
 
1072
            client = self.bus.get_object(dbus_busname, clientpath)
1069
1073
            self.assertTrue(client.attributes["Enabled"])
1070
1074
 
1071
1075
class TestDisableCmd(TestCmd):
1072
1076
    def test_disable(self):
1073
1077
        DisableCmd().run(self.clients, self.bus)
1074
1078
        for clientpath in self.clients:
1075
 
            client = self.bus.get_object(busname, clientpath)
 
1079
            client = self.bus.get_object(dbus_busname, clientpath)
1076
1080
            self.assertFalse(client.attributes["Enabled"])
1077
1081
 
1078
1082
class Unique(object):
1089
1093
        for value_to_set, value_to_get in zip(self.values_to_set,
1090
1094
                                              values_to_get):
1091
1095
            for clientpath in self.clients:
1092
 
                client = self.bus.get_object(busname, clientpath)
 
1096
                client = self.bus.get_object(dbus_busname, clientpath)
1093
1097
                old_value = client.attributes[self.propname]
1094
1098
                self.assertNotIsInstance(old_value, Unique)
1095
1099
                client.attributes[self.propname] = Unique()
1096
1100
            self.run_command(value_to_set, self.clients)
1097
1101
            for clientpath in self.clients:
1098
 
                client = self.bus.get_object(busname, clientpath)
 
1102
                client = self.bus.get_object(dbus_busname, clientpath)
1099
1103
                value = client.attributes[self.propname]
1100
1104
                self.assertNotIsInstance(value, Unique)
1101
1105
                self.assertEqual(value, value_to_get)