def _handle_runtime_exception(self, p_status_code, p_exception, p_artifact_path='UNKNOWN', p_login='******',
                                  p_result_document=None, p_key=None):

        error_code = p_status_code
        error_message = None

        if p_artifact_path is not None:
            p_artifact_path = tools.anonymize_url(p_artifact_path)

        if error_code is None:
            try:
                error_json = json.loads(str(p_result_document))
                error_code = int(error_json['errors'][0]['status'])
                error_message = error_json['errors'][0]['message']

            except Exception:
                pass

        if error_code is None:
            try:
                error_json = json.loads(str(p_exception))
                error_code = int(error_json['errors'][0]['status'])
                error_message = error_json['errors'][0]['message']

            except Exception:
                pass

        if error_code is None:
            try:
                error_code = int(str(p_exception))

            except Exception:
                pass

        # print("error_message", error_message)

        if p_result_document is not None:
            fmt = "Result document: %s" % p_result_document
            self._logger.info(fmt)

        if error_code == 400 and error_message is not None and "key already exists" in error_message:

            fmt = "Objekt with key '%s' already exists" % p_key
            self._logger.error(fmt)
            raise exceptions.DuplicateKeyException(p_key=p_key, p_error_code=error_code)

        elif error_code == 401:

            if p_login is None:
                fmt = "Access to '{artifact_path}' was not authorized"

            else:
                fmt = "Access to '{artifact_path}' using login '{p_login}' was not authorized"

            self._logger.error(fmt.format(artifact_path=p_artifact_path, login=p_login))
            raise exceptions.UnauthorizedException(p_artifact_path=p_artifact_path, p_login=p_login,
                                                   p_error_code=error_code)

        elif error_code == 403:

            fmt = "Access to '%s' using login '%s' was not successful (too many attempts)" % (p_artifact_path, p_login)
            self._logger.error(fmt)
            raise exceptions.UnauthorizedException(p_artifact_path=p_artifact_path, p_login=p_login,
                                                   p_error_code=error_code)

        elif error_code == 404:

            fmt = "Artifact '%s' not found" % p_artifact_path
            self._logger.error(fmt)
            raise exceptions.ArtifactNotFoundException(p_artifact_path=p_artifact_path, p_error_code=error_code,
                                                       p_result_document=p_result_document)

        elif error_code == 416:

            fmt = "Range not satisfiable in '%s'" % p_artifact_path
            self._logger.error(fmt)
            raise exceptions.RangeNotSatisfiableException(p_artifact_path=p_artifact_path, p_error_code=error_code)

        elif error_code == 500 or error_code == 423:

            fmt = "Artifact '%s' is locked" % p_artifact_path
            self._logger.error(fmt)
            raise exceptions.ArtifactBlockedException(p_artifact_path=p_artifact_path, p_error_code=error_code)

        elif error_code == 504:

            fmt = "Timeout while accessing artifact '%s'" % p_artifact_path
            self._logger.error(fmt)
            raise exceptions.TimeoutException(p_artifact_path=p_artifact_path, p_error_code=error_code)

        else:

            fmt = "Unknown error code '%s' with message '%s' while accessing artifact '%s'" % (
                error_code, error_message, p_artifact_path)
            self._logger.error(fmt)

            if p_result_document is not None:
                fmt = "Result document: %s" % p_result_document
                self._logger.info(fmt)

            raise p_exception
    def execute_api_call(self, p_url, p_requires_authorization=False,
                         p_requires_admin=False,
                         p_mime_type=None,
                         p_method="GET",
                         p_data=None,
                         p_parameters=None,
                         p_jsonify=False):

        result = None
        headers = {}
        login = None
        r = None

        try:

            if p_requires_authorization:
                auth = self._get_auth(p_requires_admin)
                login = auth[0]

            else:
                auth = None

            if p_mime_type:
                headers['Content-type'] = p_mime_type

            fmt = "Executing %s API call '%s'" % (p_method, tools.anonymize_url(p_url))
            self._logger.debug(fmt)

            if p_method == "GET":
                r = requests.get(p_url, auth=auth, stream=False, headers=headers, params=p_parameters,
                                 timeout=self._config.request_timeout)

            elif p_method == "PUT":
                r = requests.put(p_url, auth=auth, data=p_data, headers=headers, params=p_parameters,
                                 timeout=self._config.request_timeout)

            elif p_method == "POST":
                r = requests.post(p_url, auth=auth, data=p_data, headers=headers, params=p_parameters,
                                  timeout=self._config.request_timeout)

            elif p_method == "DELETE":
                r = requests.delete(p_url, auth=auth, data=p_data, headers=headers, params=p_parameters,
                                    timeout=self._config.request_timeout)

            else:
                raise NotImplementedError("Invalid method '%s'" % p_method)

            try:
                if p_jsonify:
                    result = json.loads(r.content.decode("UTF-8"))

                else:
                    result = r.content.decode("UTF-8")
                # print ("result", result)

            except Exception:
                pass

            r.raise_for_status()

            r.close()


        except Exception as e:

            self._handle_runtime_exception(
                p_status_code=r.status_code if r is not None else None,
                p_exception=e,
                p_artifact_path=tools.anonymize_url(p_url),
                p_key=tools.anonymize_url(p_url),
                p_result_document=result,
                p_login=login)

        return result
    def __init__(self, p_config, p_reuse_session=True):

        self._logger = log_handling.get_logger(self.__class__.__name__)
        self._config = p_config
        self._session_used = False
        self._admin_session = None
        self._create_table_session = None
        self._admin_engine = None
        self._reuse_session = p_reuse_session

        if self._config.database_user is not None:
            tools.check_config_value(p_config=self._config, p_config_attribute_name="database_password")

        if self._config.database_admin is not None and self._config.database_admin_password is not None:

            url = urllib.parse.urlunsplit(
                (
                    self._config.database_driver,
                    "%s:%s@%s:%d" % (
                        self._config.database_admin, self._config.database_admin_password,
                        self._config.database_host, self._config.database_port),
                    '',
                    None,
                    None
                ))

            fmt = "Database URL for administrative access: '%s'" % tools.anonymize_url(url)
            self._logger.info(fmt)

            if DATABASE_DRIVER_POSTGRESQL in self._config.database_driver:
                isolation_level = 'AUTOCOMMIT'

            else:
                isolation_level = None

            if isolation_level is not None:
                self._create_table_engine = sqlalchemy.create_engine(url, isolation_level=isolation_level)

            else:
                self._create_table_engine = sqlalchemy.create_engine(url)

            self._admin_engine = sqlalchemy.create_engine(url, pool_recycle=self._config.pool_recycle)

        if self._config.database_user is not None:
            url = urllib.parse.urlunsplit(
                (
                    self._config.database_driver,
                    "%s:%s@%s:%d" % (
                        self._config.database_user, self._config.database_password,
                        self._config.database_host, self._config.database_port),
                    self._config.database_name,
                    None,
                    None
                ))
        else:
            url = "{driver}://".format(driver=self._config.database_driver)

        fmt = "Database URL for normal access: '%s'" % tools.anonymize_url(url)
        self._logger.info(fmt)

        self._engine = sqlalchemy.create_engine(url, pool_recycle=self._config.pool_recycle)
Exemplo n.º 4
0
def test_anonymize_url():
    assert 'http://*****:*****@somehost/someurl' == tools.anonymize_url(
        p_url='http://*****:*****@somehost/someurl')
Exemplo n.º 5
0
    def __init__(self, p_config, p_reuse_session=True):

        self._logger = log_handling.get_logger(self.__class__.__name__)
        self._config = p_config
        self._session_used = False
        self._admin_session = None
        self._create_table_session = None
        self._admin_engine = None
        self._create_table_engine = None
        self._reuse_session = p_reuse_session
        self._users = None
        self._devices = None
        self._users_session = None
        self._devices_session = None
        self._cache_entities = True

        if self._config.database_user is not None:
            tools.check_config_value(
                p_config=self._config,
                p_config_attribute_name="database_password")

        if self._config.database_admin is not None and self._config.database_admin_password is not None:

            url = urllib.parse.urlunsplit(
                (self._config.database_driver, "%s:%s@%s:%d" %
                 (self._config.database_admin,
                  self._config.database_admin_password,
                  self._config.database_host, self._config.database_port), '',
                 None, None))

            fmt = "Database URL for administrative access: '%s'" % tools.anonymize_url(
                url)
            self._logger.info(fmt)

            if DATABASE_DRIVER_POSTGRESQL in self._config.database_driver:
                isolation_level = 'AUTOCOMMIT'

            else:
                isolation_level = None

            if isolation_level is not None:
                self._create_table_engine = sqlalchemy.create_engine(
                    url, isolation_level=isolation_level)

            else:
                self._create_table_engine = sqlalchemy.create_engine(url)

            self._admin_engine = sqlalchemy.create_engine(
                url, pool_recycle=self._config.pool_recycle)

        url = self.build_url()

        fmt = "Database URL for normal access: '%s'" % tools.anonymize_url(url)
        self._logger.info(fmt)

        options = {'pool_recycle': self._config.pool_recycle}

        if (DATABASE_DRIVER_POSTGRESQL in self._config.database_driver
                or DATABASE_DRIVER_MYSQL in self._config.database_driver):
            options['pool_size'] = self._config.pool_size
            options['max_overflow'] = self._config.max_overflow

        # if DATABASE_DRIVER_SQLITE in self._config.database_driver:
        #     options['check_same_thread'] = False

        self._engine = sqlalchemy.create_engine(url, **options)