Beispiel #1
0
 def test_09_get_data_from_params(self):
     config_description = {
         "local": {
             'cakey': 'string',
             'cacert': 'string',
             'openssl.cnf': 'string',
             'WorkingDir': 'string',
             'CSRDir': 'sting',
             'CertificateDir': 'string',
             'CRLDir': 'string',
             'CRL_Validity_Period': 'int',
             'CRL_Overlap_Period': 'int'
         }
     }
     params = {
         "type": "local",
         "cakey": "key",
         "cacert": "cert",
         "bindpw": "secret",
         "type.bindpw": "password"
     }
     data, types, desc = get_data_from_params(params,
                                              ["caconnector", "type"],
                                              config_description,
                                              "CA connector", "local")
     self.assertEqual(data.get("cakey"), "key")
     self.assertEqual(data.get("bindpw"), "secret")
     self.assertEqual(types.get("bindpw"), "password")
 def test_09_get_data_from_params(self):
     config_description = {
         "local":  {
             'cakey': 'string',
             'cacert': 'string',
             'openssl.cnf': 'string',
             'WorkingDir': 'string',
             'CSRDir': 'sting',
             'CertificateDir': 'string',
             'CRLDir': 'string',
             'CRL_Validity_Period': 'int',
             'CRL_Overlap_Period': 'int'}}
     params = {"type": "local", "cakey": "key", "cacert": "cert",
               "bindpw": "secret", "type.bindpw": "password"}
     data, types, desc = get_data_from_params(params,
                                              ["caconnector", "type"],
                                              config_description,
                                              "CA connector",
                                              "local")
     self.assertEqual(data.get("cakey"), "key")
     self.assertEqual(data.get("bindpw"), "secret")
     self.assertEqual(types.get("bindpw"), "password")
Beispiel #3
0
def save_caconnector(params):
    """
    Create a new CA connector from the given parameters and save it to the
    database.

    If the CA Connector already exists, it is updated.
    For updating some attributes of an existing CA connector you do not need
    to pass all attributes again, but only those, which should be changed.

    When updating the CA connector the type must not be changed,
    since another type might require different attributes.

    :param params: request parameters like "caconnector" (name) and "type"
        and the specific attributes of the ca connector.
    :type params: dict
    :return: the database ID of the CA connector
    :rtype: int
    """
    # before we create the resolver in the database, we need to check
    # for the name and type thing...
    connector_name = getParam(params, 'caconnector', required)
    connector_type = getParam(params, 'type', required)
    update_connector = False
    sanity_name_check(connector_name)
    # check the type
    if connector_type not in get_caconnector_types():
        raise Exception("connector type : {0!s} not in {1!s}".format(
            connector_type, unicode(get_caconnector_types())))

    # check the name
    connectors = get_caconnector_list(filter_caconnector_name=connector_name)
    for connector in connectors:
        if connector.get("type") == connector_type:
            # There is a CA connector with the same name, so we will update
            # the CA Connector config
            update_connector = True
        else:  # pragma: no cover
            raise Exception("CA Connector with similar name and other type "
                            "already exists: %s" % connector_name)

    # create a dictionary for the ResolverConfig
    connector_config = get_caconnector_config_description(connector_type)
    config_description = connector_config.get(connector_type, {})

    data, types, desc = get_data_from_params(params, ["caconnector", "type"],
                                             config_description,
                                             "CA connector", connector_type)

    # Everything passed. So lets actually create the CA Connector in the DB
    if update_connector:
        connector_id = CAConnector.query.filter(
            func.lower(CAConnector.name) == connector_name.lower()).first().id
    else:
        db_connector = CAConnector(params.get("caconnector"),
                                   params.get("type"))
        connector_id = db_connector.save()
    # create the config
    for key, value in data.items():
        if types.get(key) == "password":
            value = encryptPassword(value)
        CAConnectorConfig(caconnector_id=connector_id,
                          Key=key,
                          Value=value,
                          Type=types.get(key, ""),
                          Description=desc.get(key, "")).save()
    return connector_id
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
Beispiel #5
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
def save_resolver(params):
    """
    create a new resolver from request parameters
    and save the resolver in the database.

    If the resolver already exist, it is updated.
    If you update a 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 thing...
    resolvername = getParam(params, 'resolver', required)
    resolvertype = getParam(params, 'type', required)
    update_resolver = False
    # check the name
    sanity_name_check(resolvername)
    # check the type
    resolvertypes = get_resolver_types()
    if resolvertype not in resolvertypes:
        raise Exception("resolver type : {0!s} not in {1!s}".format(
            resolvertype, str(resolvertypes)))

    # 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:
            raise Exception(
                "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, ['resolver', 'type'],
                                             config_description, resolvertype,
                                             resolvername)

    # Everything passed. So lets actually create the resolver in the DB
    if update_resolver:
        resolver_id = Resolver.query.filter(
            func.lower(Resolver.name) == resolvername.lower()).first().id
    else:
        resolver = Resolver(params.get("resolver"), params.get("type"))
        resolver_id = resolver.save()
    # create the config
    for key, value in data.items():
        if types.get(key) == "password":
            if value == CENSORED:
                continue
            else:
                value = encryptPassword(value)

        ResolverConfig(resolver_id=resolver_id,
                       Key=key,
                       Value=value,
                       Type=types.get(key, ""),
                       Description=desc.get(key, "")).save()

    # Remove corresponding entries from the user cache
    delete_user_cache(resolver=resolvername)

    return resolver_id
Beispiel #7
0
def save_caconnector(params):
    """
    Create a new CA connector from the given parameters and save it to the
    database.

    If the CA Connector already exists, it is updated.
    For updating some attributes of an existing CA connector you do not need
    to pass all attributes again, but only those, which should be changed.

    When updating the CA connector the type must not be changed,
    since another type might require different attributes.

    :param params: request parameters like "caconnector" (name) and "type"
        and the specific attributes of the ca connector.
    :type params: dict
    :return: the database ID of the CA connector
    :rtype: int
    """
    # before we create the resolver in the database, we need to check
    # for the name and type thing...
    connector_name = getParam(params, 'caconnector', required)
    connector_type = getParam(params, 'type', required)
    update_connector = False
    sanity_name_check(connector_name)
    # check the type
    if connector_type not in get_caconnector_types():
        raise Exception("connector type : %s not in %s" %
                        (connector_type, unicode(get_caconnector_types())))

    # check the name
    connectors = get_caconnector_list(filter_caconnector_name=connector_name)
    for connector in connectors:
        if connector.get("type") == connector_type:
            # There is a CA connector with the same name, so we will update
            # the CA Connector config
            update_connector = True
        else:  # pragma: no cover
            raise Exception("CA Connector with similar name and other type "
                            "already exists: %s" % connector_name)

    # create a dictionary for the ResolverConfig
    connector_config = get_caconnector_config_description(connector_type)
    config_description = connector_config.get(connector_type, {})

    data, types, desc = get_data_from_params(params,
                                             ["caconnector", "type"],
                                             config_description,
                                             "CA connector",
                                             connector_type)

    # Everything passed. So lets actually create the CA Connector in the DB
    if update_connector:
        connector_id = CAConnector.query.filter(func.lower(CAConnector.name)
                                                == connector_name.lower()).first().id
    else:
        db_connector = CAConnector(params.get("caconnector"),
                                   params.get("type"))
        connector_id = db_connector.save()
    # create the config
    for key, value in data.items():
        if types.get(key) == "password":
            value = encryptPassword(value)
        CAConnectorConfig(caconnector_id=connector_id,
                        Key=key,
                        Value=value,
                        Type=types.get(key, ""),
                        Description=desc.get(key, "")).save()
    return connector_id