def get_resolver_class(resolver_type):
    """
    return the class object for a resolver type
    :param resolver_type: string specifying the resolver
                          fully qualified or abbreviated
    :return: resolver object class
    """
    ret = None
    
    (resolver_clazzes, resolver_types) = get_machine_resolver_class_dict()

    if resolver_type in resolver_types.values():
        for k, v in resolver_types.items():
            if v == resolver_type:
                ret = resolver_clazzes.get(k, None)
                break
    return ret
Example #2
0
def get_resolver_class(resolver_type):
    """
    return the class object for a resolver type
    :param resolver_type: string specifying the resolver
                          fully qualified or abbreviated
    :return: resolver object class
    """
    ret = None

    (resolver_clazzes, resolver_types) = get_machine_resolver_class_dict()

    if resolver_type in resolver_types.values():
        for k, v in resolver_types.items():
            if v == resolver_type:
                ret = resolver_clazzes.get(k, None)
                break
    return ret
def save_resolver(params):
    """
    create a new machine resolver from request parameters
    and save the machine resolver in the database.

    If the machine resolver already exist, it is updated.
    If you update machine resolver, you do not need to provide all parameters.
    Parameters you do not provide are left untouched.
    When updating a resolver you must not change the type!
    You do not need to specify the type, but if you specify a wrong type,
    it will produce an error.

    :param params: request parameters like "name" and "type" and the
                   configuration parameters of the resolver config.
    :type params: dict
    :return: the database ID of the resolver
    :rtype: int
    """
    # before we create the resolver in the database, we need to check
    # for the name and type.
    resolvername = getParam(params, 'name', required)
    resolvertype = getParam(params, 'type', required)
    update_resolver = False
    # check the name
    sanity_name_check(resolvername)
    # check the type
    (class_dict, type_dict) = get_machine_resolver_class_dict()
    if resolvertype not in type_dict.values():
            raise Exception("machine resolver type : %s not in %s" %
                            (resolvertype, type_dict.values()))

    # check the name
    resolvers = get_resolver_list(filter_resolver_name=resolvername)
    for r_name, resolver in resolvers.items():
        if resolver.get("type") == resolvertype:
            # We found the resolver with the same name and the same type,
            # So we will update this resolver
            update_resolver = True
        else:  # pragma: no cover
            raise Exception("machine resolver with similar name and other type "
                            "already exists: %s" % r_name)

    # create a dictionary for the ResolverConfig
    resolver_config = get_resolver_config_description(resolvertype)
    config_description = resolver_config.get(resolvertype,
                                             {}).get('config',
                                                     {})
    data, types, desc = get_data_from_params(params,
                                             ["name", "type"],
                                             config_description,
                                             "machine resolver",
                                             resolvertype)

    # Everything passed. So lets actually create the resolver in the DB
    if update_resolver:
        resolver_id = MachineResolver.query.filter(func.lower(
            MachineResolver.name) == resolvername.lower()).first().id
    else:
        resolver = MachineResolver(params.get("name"), params.get("type"))
        resolver_id = resolver.save()
    # create the config
    for key, value in data.items():
        if types.get(key) == "password":
            value = encryptPassword(value)
        MachineResolverConfig(resolver_id=resolver_id,
                              Key=key,
                              Value=value,
                              Type=types.get(key, ""),
                              Description=desc.get(key, "")).save()
    return resolver_id
 def test_05_machine_resolvers(self):
     (classes, types) = get_machine_resolver_class_dict()
     self.assertTrue("hosts" in types.values(), types.values())
     self.assertTrue("privacyidea.lib.machines.hosts.HostsMachineResolver"
                     in classes.keys(), classes)
 def test_05_machine_resolvers(self):
     (classes, types) = get_machine_resolver_class_dict()
     self.assertTrue("hosts" in types.values(), list(types.values()))
     self.assertTrue("privacyidea.lib.machines.hosts.HostsMachineResolver"
                     in classes, classes)
Example #6
0
def save_resolver(params):
    """
    create a new machine resolver from request parameters
    and save the machine resolver in the database.

    If the machine resolver already exist, it is updated.
    If you update machine resolver, you do not need to provide all parameters.
    Parameters you do not provide are left untouched.
    When updating a resolver you must not change the type!
    You do not need to specify the type, but if you specify a wrong type,
    it will produce an error.

    :param params: request parameters like "name" and "type" and the
                   configuration parameters of the resolver config.
    :type params: dict
    :return: the database ID of the resolver
    :rtype: int
    """
    # before we create the resolver in the database, we need to check
    # for the name and type.
    resolvername = getParam(params, 'name', required)
    resolvertype = getParam(params, 'type', required)
    update_resolver = False
    # check the name
    sanity_name_check(resolvername)
    # check the type
    (class_dict, type_dict) = get_machine_resolver_class_dict()
    if resolvertype not in type_dict.values():
        raise Exception("machine resolver type : %s not in %s" %
                        (resolvertype, type_dict.values()))

    # check the name
    resolvers = get_resolver_list(filter_resolver_name=resolvername)
    for r_name, resolver in resolvers.items():
        if resolver.get("type") == resolvertype:
            # We found the resolver with the same name and the same type,
            # So we will update this resolver
            update_resolver = True
        else:  # pragma: no cover
            raise Exception(
                "machine resolver with similar name and other type "
                "already exists: %s" % r_name)

    # create a dictionary for the ResolverConfig
    resolver_config = get_resolver_config_description(resolvertype)
    config_description = resolver_config.get(resolvertype,
                                             {}).get('config', {})
    data, types, desc = get_data_from_params(params, ["name", "type"],
                                             config_description,
                                             "machine resolver", resolvertype)

    # Everything passed. So lets actually create the resolver in the DB
    if update_resolver:
        resolver_id = MachineResolver.query.filter(
            func.lower(MachineResolver.name) ==
            resolvername.lower()).first().id
    else:
        resolver = MachineResolver(params.get("name"), params.get("type"))
        resolver_id = resolver.save()
    # create the config
    for key, value in data.items():
        if types.get(key) == "password":
            value = encryptPassword(value)
        MachineResolverConfig(resolver_id=resolver_id,
                              Key=key,
                              Value=value,
                              Type=types.get(key, ""),
                              Description=desc.get(key, "")).save()
    return resolver_id