Exemple #1
0
    def get_enabled_user(self, username, password):
        """
        Busca o usuário de acordo com o login e a senha.

        Retorna apenas usuário ativo.
        """
        bypass = 0
        try:
            try:
                use_ldap = convert_string_or_int_to_boolean(
                    get_value('use_ldap'))
                if use_ldap:
                    ldap_param = get_value('ldap_config')
                    ldap_server = get_value('ldap_server')
                    return_user = self.get_by_ldap_user(username, True)
                else:
                    bypass = 1
            except exceptions.VariableDoesNotExistException, e:
                self.log.error(
                    'Error getting LDAP config variables (use_ldap). Trying local authentication'
                )
                bypass = 1
            except UsuarioNotFoundError, e:
                self.log.debug("Using local authentication for user \'%s\'" %
                               username)
                bypass = 1
Exemple #2
0
    def get_enabled_user(self, username, password):
        """
        Busca o usuário de acordo com o login e a senha.

        Retorna apenas usuário ativo.
        """
        bypass = 0
        try:
            try:
                use_ldap = convert_string_or_int_to_boolean(
                    get_value('use_ldap'))
                if use_ldap:
                    ldap_param = get_value('ldap_config')
                    ldap_server = get_value('ldap_server')
                    return_user = self.get_by_ldap_user(username, True)
                else:
                    bypass = 1
            except exceptions.VariableDoesNotExistException, e:
                self.log.error(
                    'Error getting LDAP config variables (use_ldap). Trying local authentication')
                bypass = 1
            except UsuarioNotFoundError, e:
                self.log.debug(
                    "Using local authentication for user \'%s\'" % username)
                bypass = 1
Exemple #3
0
def cache_enabled():
    try:
        if int(get_value('use_cache')):
            return 1
        return 0
    except Exception as e:
        return 0
def cache_enabled():
    try:
        if int(get_value('use_cache')):
            return 1
        return 0
    except Exception as ERROR:
        log.error(ERROR)
        return 0
    def get_enabled_user(self, username, password):
        """
        Busca o usuário de acordo com o login e a senha.

        Retorna apenas usuário ativo.
        """
        bypass = 0
        try:
            try:
                use_ldap = get_value("use_ldap")
                if use_ldap:
                    ldap_param = get_value("ldap_config")
                    ldap_server = get_value("ldap_server")
                    return_user = self.get_by_ldap_user(username, True)
                else:
                    bypass = 1
            except exceptions.VariableDoesNotExistException, e:
                self.log.error("Error getting LDAP config variables (use_ldap). Trying local authentication")
                bypass = 1
            except UsuarioNotFoundError, e:
                self.log.error("Using local authentication for user '%s'" % username)
                bypass = 1
    def check_configuration_file_exists(self, file_path):

        """
        This function try to find and build (if necessary) the configuration file path. The priorities are:
        (1) build the full path from system variable base and relative file path ('file_path'); or
        (2) build the full path from static variable base and relative file path ('file_path'); or
        (3) return the relative path it self ('file_path')

        :param str file_path: Relative path, examples:
            'networkapi/plugins/Juniper/JUNOS/samples/sample_command.txt' or
            'networkapi/generated_config/interface/int-d_24823_config_ROR9BX3ATQG93TALJAMO2G'

        :return: Return a valid configuration file path string. Ex.:
        'networkapi/plugins/Juniper/JUNOS/samples/sample_command.txt' or
        '/mnt/scripts/tftpboot/networkapi/generated_config/interface/int-d_24823_config_ROR9BX3ATQG93TALJAMO2G'
        """

        log.info("Checking configuration file exist: {}".format(file_path))

        # Check in system variables
        for variable in self.alternative_variable_base_path_list:
            try:
                base_path = get_value(variable)
                if base_path != "":
                    result_path = base_path + file_path
                    if os.path.isfile(result_path):
                        log.info("Configuration file {} was found by system variable {}!".format(result_path, variable))
                        return result_path
            except (DatabaseError, VariableDoesNotExistException):
                # DatabaseError means that variable table do not exist
                pass
            except Exception as e:
                log.warning("Unknown error while calling networkapi.system.facade.get_value({}): {} {} ".format(
                    variable, e.__class__, e))

        # Check possible static variables
        for static_path in self.alternative_static_base_path_list:
            result_path = static_path + file_path
            if os.path.isfile(result_path):
                log.info("Configuration file {} was found by static variable {}!".format(result_path, static_path))
                return result_path

        # Check if relative path is valid (for dev tests)
        if os.path.isfile(file_path):
            log.info("Configuration file {} was found by relative path".format(file_path))
            return file_path

        message = "An error occurred while finding configuration file."
        log.error("{} Could not find in: relative path ('{}'), system variables ({}) or static paths ({})".format(
            message, file_path, self.alternative_variable_base_path_list, self.alternative_static_base_path_list))
        raise exceptions.APIException(message)
Exemple #7
0
    def set_detailed_junos_log_level(self, loggers):

        """
        Used to disable logs from detailed and specific libs used in Junos plugin, for example:
        If 'ERROR' is defined, higher log messages will be disabled, like 'WARNING', 'INFO' and 'DEBUG'.

        :param str loggers: Array of logger objects
        :return: A valid level error ('ERROR', 'INFO', etc.), otherwise None
        """

        default_log_level = 'ERROR'
        log_level = None

        # Trying to get the variable from networkapi.
        try:
            log_level = get_value(self.detailed_log_level_var)
        except DatabaseError as e:
            log.warning("Database error while getting '{}' variable: {}".format(
                self.detailed_log_level_var, e))
        except VariableDoesNotExistException as e:
            log.warning("Variable '{}' does not exist: {}".format(
                self.detailed_log_level_var, e))
        except Exception as e:
            log.warning("Unknown error while getting '{}' variable: {} ".format(
                self.detailed_log_level_var, e))

        # If could not get the value earlier, a default value will be used
        if log_level is None or log_level == "":
            log.warning("Could not get '{}' variable from networkapi. ".format(
                self.detailed_log_level_var, default_log_level))
            log_level = default_log_level

        # Trying to set the level to each log object.
        for logger in loggers:
            try:
                logger.setLevel(log_level)
            except Exception as e:
                log.error("Invalid log level '{}'. The default {} will be used: {}".format(
                    log_level, default_log_level, e))
                log_level = default_log_level  # to be used at next iteration too
                logger.setLevel(log_level)

        return log_level
Exemple #8
0
def get_environment_cache_time():
    try:
        return int(get_value('ENVIRONMENT_CACHE_TIMEOUT'))
    except Exception as e:
        return DEFAULT_CACHE_TIMEOUT
Exemple #9
0
    def get_enabled_user(self, username, password):
        """
        Busca o usuário de acordo com o login e a senha.

        Retorna apenas usuário ativo.
        """
        bypass = 0
        try:
            try:
                use_cache_user = convert_string_or_int_to_boolean(
                    get_value('use_cache_user'))

                if use_cache_user:
                    salt = get_cache('salt_key')

                    if salt:
                        self.log.debug(
                            'The encrypt key was taken successfully!')

                        hash_text = str(username + password)
                        encrypted_hash_text = encrypt_key(hash_text, salt)
                        cached_hash_text = get_cache(
                            b64encode(encrypted_hash_text))

                        if cached_hash_text:
                            self.log.debug(
                                'This authentication is using cached user')
                            pswd = Usuario.encode_password(password)
                            return Usuario.objects.prefetch_related(
                                'grupos').get(user=username, pwd=pswd, ativo=1)

                        else:
                            set_cache(b64encode(encrypted_hash_text), True,
                                      int(get_value('time_cache_user')))
                            self.log.debug('The user was cached successfully!')

                    else:
                        salt_key = generate_key()
                        set_cache('salt_key', salt_key,
                                  int(get_value('time_cache_salt_key')))
                        self.log.debug(
                            'The encrypt token was generated and cached successfully!'
                        )

            except Exception as ERROR:
                self.log.error(ERROR)

            try:
                use_ldap = convert_string_or_int_to_boolean(
                    get_value('use_ldap'))
                if use_ldap:
                    ldap_param = get_value('ldap_config')
                    ldap_server = get_value('ldap_server')
                    return_user = self.get_by_ldap_user(username, True)
                else:
                    bypass = 1
            except exceptions.VariableDoesNotExistException, e:
                self.log.error(
                    'Error getting LDAP config variables (use_ldap). Trying local authentication'
                )
                bypass = 1
            except UsuarioNotFoundError, e:
                self.log.debug("Using local authentication for user \'%s\'" %
                               username)
                bypass = 1