def test_13_machine_resolver(self): # create the machineresolver and a config entry mr = MachineResolver("mr1", "mrtype1") mr_id = mr.save() self.assertTrue(mr_id > 0, mr_id) mrc = MachineResolverConfig(resolver="mr1", Key="key1", Value="value1") mrc.save() self.assertTrue(mrc > 0) # check that the config entry exist db_mrconf = MachineResolverConfig.query.filter( MachineResolverConfig.resolver_id == mr_id).first() self.assertTrue(db_mrconf is not None) # add a config value by ID mrc = MachineResolverConfig(resolver_id=mr_id, Key="key2", Value="v2") mrc.save() self.assertTrue(mrc > 0) # update config MachineResolverConfig(resolver_id=mr_id, Key="key2", Value="new value").save() # check if the value is updated. new_config = MachineResolverConfig.query.filter( MachineResolverConfig.Key == "key2").first() self.assertTrue(new_config.Value == "new value", new_config.Value) # Connect a machine to a token mt_id = MachineToken(machineresolver_id=mr_id, machine_id="client1", serial="serial1123", application="SSH").save() self.assertTrue(mt_id > 0, mt_id) # Connect another machine to a token token_id = Token.query.filter_by(serial="serial1123").first().id mt_id2 = MachineToken(machineresolver="mr1", machine_id="client2", token_id=token_id, application="LUKS").save() self.assertTrue(mt_id2 > mt_id, (mt_id2, mt_id)) # get the token that contains the machines db_token = Token.query.filter_by(serial="serial1123").first() # check the length of the machine list of the token self.assertTrue(len(db_token.machine_list) == 2, db_token.machine_list) machine2 = db_token.machine_list[1].machine_id self.assertTrue(machine2 == "client2", (machine2, db_token.machine_list)) # delete the machine resolver db_mr = MachineResolver.query.filter( MachineResolver.name == "mr1").first() db_mr.delete() # check that there is no machine resolver and no config entry db_mr = MachineResolver.query.filter( MachineResolver.name == "mr1").first() self.assertTrue(db_mr is None) db_mrconf = MachineResolverConfig.query.filter( MachineResolverConfig.resolver_id == mr_id).first() self.assertTrue(db_mrconf is None)
def attach_token(serial, application, hostname=None, machine_id=None, resolver_name=None, options=None): """ Attach a token with an application to a machine. You need to provide either the hostname or the (machine_id, resolver_name) of the machine, to which you want to attach the token. :param serial: The serial number of the token :type serial: string :param application: The name of the application - something like ssh or luks :type application: basestring :param hostname: The hostname of the machine, to which your want to attach the token. If the hostname is not unique, an exception is raised. :type hostname: basestring :param machine_id: The machine ID of the machine, you want to attach the token to. :type machine_id: basestring :param resolver_name: The resolver_name of the machine you want attach the token to. :type resolver_name: basestring :param options: additional options :return: the new MachineToken Object """ if hostname or machine_id or resolver_name: machine_id, resolver_name = _get_host_identifier( hostname, machine_id, resolver_name) # Now we have all data to create the MachineToken machinetoken = MachineToken(machineresolver=resolver_name, machine_id=machine_id, serial=serial, application=application) machinetoken.save() # Add options to the machine token if options: add_option(machinetoken_id=machinetoken.id, options=options) return machinetoken
def attach_token(serial, application, hostname=None, machine_id=None, resolver_name=None, options=None): """ Attach a token with an application to a machine. You need to provide either the hostname or the (machine_id, resolver_name) of the machine, to which you want to attach the token. :param serial: The serial number of the token :type serial: string :param application: The name of the application - something like ssh or luks :type application: basestring :param hostname: The hostname of the machine, to which your want to attach the token. If the hostname is not unique, an exception is raised. :type hostname: basestring :param machine_id: The machine ID of the machine, you want to attach the token to. :type machine_id: basestring :param resolver_name: The resolver_name of the machine you want attach the token to. :type resolver_name: basestring :param options: addtional options :return: the new MachineToken Object """ machine_id, resolver_name = _get_host_identifier(hostname, machine_id, resolver_name) # Now we have all data to create the MachineToken machinetoken = MachineToken(machineresolver=resolver_name, machine_id=machine_id, serial=serial, application=application) machinetoken.save() # Add options to the machine token if options: add_option(machinetoken_id=machinetoken.id, options=options) return machinetoken