示例#1
0
文件: api.py 项目: dodopizza/superset
 def related_objects(self, pk: int) -> Response:
     """Get charts and dashboards count associated to a database
     ---
     get:
       description:
         Get charts and dashboards count associated to a database
       parameters:
       - in: path
         name: pk
         schema:
           type: integer
       responses:
         200:
           description: Query result
           content:
             application/json:
               schema:
                 $ref: "#/components/schemas/DatabaseRelatedObjectsResponse"
         401:
           $ref: '#/components/responses/401'
         404:
           $ref: '#/components/responses/404'
         500:
           $ref: '#/components/responses/500'
     """
     database = DatabaseDAO.find_by_id(pk)
     if not database:
         return self.response_404()
     data = DatabaseDAO.get_related_objects(pk)
     charts = [{
         "id": chart.id,
         "slice_name": chart.slice_name,
         "viz_type": chart.viz_type,
     } for chart in data["charts"]]
     dashboards = [{
         "id": dashboard.id,
         "json_metadata": dashboard.json_metadata,
         "slug": dashboard.slug,
         "title": dashboard.dashboard_title,
     } for dashboard in data["dashboards"]]
     sqllab_tab_states = [{
         "id": tab_state.id,
         "label": tab_state.label,
         "active": tab_state.active
     } for tab_state in data["sqllab_tab_states"]]
     return self.response(
         200,
         charts={
             "count": len(charts),
             "result": charts
         },
         dashboards={
             "count": len(dashboards),
             "result": dashboards
         },
         sqllab_tab_states={
             "count": len(sqllab_tab_states),
             "result": sqllab_tab_states,
         },
     )
示例#2
0
    def run(self) -> Model:
        self.validate()
        try:
            database = DatabaseDAO.create(self._properties, commit=False)
            database.set_sqlalchemy_uri(database.sqlalchemy_uri)

            try:
                TestConnectionDatabaseCommand(self._actor,
                                              self._properties).run()
            except Exception as ex:  # pylint: disable=broad-except
                db.session.rollback()
                event_logger.log_with_context(
                    action=f"db_creation_failed.{ex.__class__.__name__}",
                    engine=database.db_engine_spec.__name__,
                )
                raise DatabaseConnectionFailedError()

            # adding a new database we always want to force refresh schema list
            schemas = database.get_all_schema_names(cache=False)
            for schema in schemas:
                security_manager.add_permission_view_menu(
                    "schema_access",
                    security_manager.get_schema_perm(database, schema))
            security_manager.add_permission_view_menu("database_access",
                                                      database.perm)
            db.session.commit()
        except DAOCreateFailedError as ex:
            event_logger.log_with_context(
                action=f"db_creation_failed.{ex.__class__.__name__}",
                engine=database.db_engine_spec.__name__,
            )
            raise DatabaseCreateFailedError()
        return database
示例#3
0
    def run(self) -> None:
        self.validate()
        try:
            uri = self._properties.get("sqlalchemy_uri", "")
            if self._model and uri == self._model.safe_sqlalchemy_uri():
                uri = self._model.sqlalchemy_uri_decrypted

            database = DatabaseDAO.build_db_for_connection_test(
                server_cert=self._properties.get("server_cert", ""),
                extra=self._properties.get("extra", "{}"),
                impersonate_user=self._properties.get("impersonate_user",
                                                      False),
                encrypted_extra=self._properties.get("encrypted_extra", "{}"),
            )
            if database is not None:
                database.set_sqlalchemy_uri(uri)
                database.db_engine_spec.mutate_db_for_connection_test(database)
                username = self._actor.username if self._actor is not None else None
                engine = database.get_sqla_engine(user_name=username)
            with closing(engine.raw_connection()) as conn:
                if not engine.dialect.do_ping(conn):
                    raise DBAPIError(None, None, None)
        except DBSecurityException as ex:
            logger.warning(ex)
            raise DatabaseSecurityUnsafeError()
示例#4
0
 def function_names(self, pk: int) -> Response:
     """Get function names supported by a database
     ---
     get:
       description:
         Get function names supported by a database
       parameters:
       - in: path
         name: pk
         schema:
           type: integer
       responses:
         200:
           description: Query result
           content:
             application/json:
               schema:
                 $ref: "#/components/schemas/DatabaseFunctionNamesResponse"
         401:
           $ref: '#/components/responses/401'
         404:
           $ref: '#/components/responses/404'
         500:
           $ref: '#/components/responses/500'
     """
     database = DatabaseDAO.find_by_id(pk)
     if not database:
         return self.response_404()
     return self.response(
         200,
         function_names=database.function_names,
     )
示例#5
0
 def run(self) -> None:
     self.validate()
     uri = self._properties.get("sqlalchemy_uri", "")
     if self._model and uri == self._model.safe_sqlalchemy_uri():
         uri = self._model.sqlalchemy_uri_decrypted
     try:
         database = DatabaseDAO.build_db_for_connection_test(
             server_cert=self._properties.get("server_cert", ""),
             extra=self._properties.get("extra", "{}"),
             impersonate_user=self._properties.get("impersonate_user", False),
             encrypted_extra=self._properties.get("encrypted_extra", "{}"),
         )
         if database is not None:
             database.set_sqlalchemy_uri(uri)
             database.db_engine_spec.mutate_db_for_connection_test(database)
             username = self._actor.username if self._actor is not None else None
             engine = database.get_sqla_engine(user_name=username)
         with closing(engine.raw_connection()) as conn:
             if not engine.dialect.do_ping(conn):
                 raise DBAPIError(None, None, None)
     except (NoSuchModuleError, ModuleNotFoundError):
         driver_name = make_url(uri).drivername
         raise DatabaseTestConnectionDriverError(
             message=_("Could not load database driver: {}").format(driver_name),
         )
     except DBAPIError:
         raise DatabaseTestConnectionFailedError()
     except SupersetSecurityException as ex:
         raise DatabaseSecurityUnsafeError(message=str(ex))
     except Exception:
         raise DatabaseTestConnectionUnexpectedError()
示例#6
0
    def run(self) -> Model:
        self.validate()

        try:
            # Test connection before starting create transaction
            TestConnectionDatabaseCommand(self._properties).run()
        except Exception as ex:
            event_logger.log_with_context(
                action=f"db_creation_failed.{ex.__class__.__name__}",
                engine=self._properties.get("sqlalchemy_uri", "").split(":")[0],
            )
            raise DatabaseConnectionFailedError() from ex

        try:
            database = DatabaseDAO.create(self._properties, commit=False)
            database.set_sqlalchemy_uri(database.sqlalchemy_uri)

            # adding a new database we always want to force refresh schema list
            schemas = database.get_all_schema_names(cache=False)
            for schema in schemas:
                security_manager.add_permission_view_menu(
                    "schema_access", security_manager.get_schema_perm(database, schema)
                )
            db.session.commit()
        except DAOCreateFailedError as ex:
            db.session.rollback()
            event_logger.log_with_context(
                action=f"db_creation_failed.{ex.__class__.__name__}",
                engine=database.db_engine_spec.__name__,
            )
            raise DatabaseCreateFailedError() from ex
        return database
示例#7
0
 def validate(self) -> None:
     exceptions: List[ValidationError] = []
     # Validate/populate model exists
     self._model = DatabaseDAO.find_by_id(self._model_id)
     if not self._model:
         raise DatabaseNotFoundError()
     database_name: Optional[str] = self._properties.get("database_name")
     if database_name:
         # Check database_name uniqueness
         if not DatabaseDAO.validate_update_uniqueness(
                 self._model_id, database_name):
             exceptions.append(DatabaseExistsValidationError())
     if exceptions:
         exception = DatabaseInvalidError()
         exception.add_list(exceptions)
         raise exception
示例#8
0
    def run(self) -> Model:
        self.validate()
        try:
            database = DatabaseDAO.update(self._model,
                                          self._properties,
                                          commit=False)
            database.set_sqlalchemy_uri(database.sqlalchemy_uri)
            security_manager.add_permission_view_menu("database_access",
                                                      database.perm)
            # adding a new database we always want to force refresh schema list
            # TODO Improve this simplistic implementation for catching DB conn fails
            try:
                schemas = database.get_all_schema_names()
            except Exception as ex:
                db.session.rollback()
                raise DatabaseConnectionFailedError() from ex
            for schema in schemas:
                security_manager.add_permission_view_menu(
                    "schema_access",
                    security_manager.get_schema_perm(database, schema))
            db.session.commit()

        except DAOUpdateFailedError as ex:
            logger.exception(ex.exception)
            raise DatabaseUpdateFailedError() from ex
        return database
示例#9
0
    def validate(self) -> None:
        # Validate/populate model exists
        self._model = DatabaseDAO.find_by_id(self._model_id)
        if not self._model:
            raise DatabaseNotFoundError()

        spec = self._model.db_engine_spec
        validators_by_engine = current_app.config["SQL_VALIDATORS_BY_ENGINE"]
        if not validators_by_engine or spec.engine not in validators_by_engine:
            raise NoValidatorConfigFoundError(
                SupersetError(
                    message=__("no SQL validator is configured for {}".format(
                        spec.engine)),
                    error_type=SupersetErrorType.GENERIC_DB_ENGINE_ERROR,
                    level=ErrorLevel.ERROR,
                ), )
        validator_name = validators_by_engine[spec.engine]
        self._validator = get_validator_by_name(validator_name)
        if not self._validator:
            raise NoValidatorFoundError(
                SupersetError(
                    message=__("No validator named {} found "
                               "(configured for the {} engine)".format(
                                   validator_name, spec.engine)),
                    error_type=SupersetErrorType.GENERIC_DB_ENGINE_ERROR,
                    level=ErrorLevel.ERROR,
                ), )
示例#10
0
 def run(self) -> Model:
     self.validate()
     try:
         database = DatabaseDAO.delete(self._model)
     except DAODeleteFailedError as ex:
         logger.exception(ex.exception)
         raise DatabaseDeleteFailedError()
     return database
示例#11
0
 def validate(self) -> None:
     # Validate/populate model exists
     self._model = DatabaseDAO.find_by_id(self._model_id)
     if not self._model:
         raise DatabaseNotFoundError()
     # Check if there are datasets for this database
     if self._model.tables:
         raise DatabaseDeleteDatasetsExistFailedError()
示例#12
0
    def validate(self) -> None:
        exceptions: List[ValidationError] = []
        owner_ids: Optional[List[int]] = self._properties.get("owners")
        name = self._properties.get("name", "")
        report_type = self._properties.get("type")
        creation_method = self._properties.get("creation_method")
        chart_id = self._properties.get("chart")
        dashboard_id = self._properties.get("dashboard")
        user_id = self._actor.id

        # Validate type is required
        if not report_type:
            exceptions.append(ReportScheduleRequiredTypeValidationError())

        # Validate name type uniqueness
        if report_type and not ReportScheduleDAO.validate_update_uniqueness(
            name, report_type
        ):
            exceptions.append(ReportScheduleNameUniquenessValidationError())

        # validate relation by report type
        if report_type == ReportScheduleType.ALERT:
            database_id = self._properties.get("database")
            if not database_id:
                exceptions.append(ReportScheduleAlertRequiredDatabaseValidationError())
            else:
                database = DatabaseDAO.find_by_id(database_id)
                if not database:
                    exceptions.append(DatabaseNotFoundValidationError())
                self._properties["database"] = database

        # Validate chart or dashboard relations
        self.validate_chart_dashboard(exceptions)

        # Validate that each chart or dashboard only has one report with
        # the respective creation method.
        if (
            creation_method != ReportCreationMethodType.ALERTS_REPORTS
            and not ReportScheduleDAO.validate_unique_creation_method(
                user_id, dashboard_id, chart_id
            )
        ):
            raise ReportScheduleCreationMethodUniquenessValidationError()

        if "validator_config_json" in self._properties:
            self._properties["validator_config_json"] = json.dumps(
                self._properties["validator_config_json"]
            )

        try:
            owners = self.populate_owners(self._actor, owner_ids)
            self._properties["owners"] = owners
        except ValidationError as ex:
            exceptions.append(ex)
        if exceptions:
            exception = ReportScheduleInvalidError()
            exception.add_list(exceptions)
            raise exception
示例#13
0
    def run(self) -> None:
        self.validate()
        uri = self._properties.get("sqlalchemy_uri", "")
        if self._model and uri == self._model.safe_sqlalchemy_uri():
            uri = self._model.sqlalchemy_uri_decrypted
        try:
            database = DatabaseDAO.build_db_for_connection_test(
                server_cert=self._properties.get("server_cert", ""),
                extra=self._properties.get("extra", "{}"),
                impersonate_user=self._properties.get("impersonate_user",
                                                      False),
                encrypted_extra=self._properties.get("encrypted_extra", "{}"),
            )

            database.set_sqlalchemy_uri(uri)
            database.db_engine_spec.mutate_db_for_connection_test(database)
            username = self._actor.username if self._actor is not None else None
            engine = database.get_sqla_engine(user_name=username)
            with closing(engine.raw_connection()) as conn:
                if not engine.dialect.do_ping(conn):
                    raise DBAPIError(None, None, None)

            # Log succesful connection test with engine
            event_logger.log_with_context(
                action="test_connection_success",
                engine=database.db_engine_spec.__name__,
            )

        except (NoSuchModuleError, ModuleNotFoundError) as ex:
            event_logger.log_with_context(
                action=f"test_connection_error.{ex.__class__.__name__}",
                engine=database.db_engine_spec.__name__,
            )
            raise DatabaseTestConnectionDriverError(
                message=_("Could not load database driver: {}").format(
                    database.db_engine_spec.__name__), )
        except DBAPIError as ex:
            event_logger.log_with_context(
                action=f"test_connection_error.{ex.__class__.__name__}",
                engine=database.db_engine_spec.__name__,
            )
            # check if we have connectivity to the host, and if the port is open
            self._diagnose(uri)
            raise DatabaseTestConnectionFailedError()
        except SupersetSecurityException as ex:
            event_logger.log_with_context(
                action=f"test_connection_error.{ex.__class__.__name__}",
                engine=database.db_engine_spec.__name__,
            )
            raise DatabaseSecurityUnsafeError(message=str(ex))
        except Exception as ex:  # pylint: disable=broad-except
            event_logger.log_with_context(
                action=f"test_connection_error.{ex.__class__.__name__}",
                engine=database.db_engine_spec.__name__,
            )
            raise DatabaseTestConnectionUnexpectedError(str(ex))
示例#14
0
    def validate(self) -> None:
        exceptions: List[ValidationError] = list()
        owner_ids: Optional[List[int]] = self._properties.get("owners")
        report_type = self._properties.get("type", ReportScheduleType.ALERT)

        name = self._properties.get("name", "")
        self._model = ReportScheduleDAO.find_by_id(self._model_id)

        # Does the report exist?
        if not self._model:
            raise ReportScheduleNotFoundError()

        # Validate name uniqueness
        if not ReportScheduleDAO.validate_update_uniqueness(
                name, report_schedule_id=self._model_id):
            exceptions.append(ReportScheduleNameUniquenessValidationError())

        # validate relation by report type
        if not report_type:
            report_type = self._model.type
        if report_type == ReportScheduleType.ALERT:
            database_id = self._properties.get("database")
            # If database_id was sent let's validate it exists
            if database_id:
                database = DatabaseDAO.find_by_id(database_id)
                if not database:
                    exceptions.append(DatabaseNotFoundValidationError())
                self._properties["database"] = database

        # Validate chart or dashboard relations
        self.validate_chart_dashboard(exceptions, update=True)

        if "validator_config_json" in self._properties:
            self._properties["validator_config_json"] = json.dumps(
                self._properties["validator_config_json"])

        # Check ownership
        try:
            check_ownership(self._model)
        except SupersetSecurityException:
            raise ReportScheduleForbiddenError()

        # Validate/Populate owner
        if owner_ids is None:
            owner_ids = [owner.id for owner in self._model.owners]
        try:
            owners = populate_owners(self._actor, owner_ids)
            self._properties["owners"] = owners
        except ValidationError as ex:
            exceptions.append(ex)
        if exceptions:
            exception = ReportScheduleInvalidError()
            exception.add_list(exceptions)
            raise exception
示例#15
0
    def validate(self) -> None:
        # Validate/populate model exists
        self._model = DatabaseDAO.find_by_id(self._model_id)
        if not self._model:
            raise DatabaseNotFoundError()
        # Check there are no associated ReportSchedules
        reports = ReportScheduleDAO.find_by_database_id(self._model_id)

        if reports:
            report_names = [report.name for report in reports]
            raise DatabaseDeleteFailedReportsExistError(
                _("There are associated alerts or reports: %s" % ",".join(report_names))
            )
        # Check if there are datasets for this database
        if self._model.tables:
            raise DatabaseDeleteDatasetsExistFailedError()
示例#16
0
    def validate(self) -> None:
        exceptions: List[ValidationError] = list()
        sqlalchemy_uri: Optional[str] = self._properties.get("sqlalchemy_uri")
        database_name: Optional[str] = self._properties.get("database_name")

        if not sqlalchemy_uri:
            exceptions.append(DatabaseRequiredFieldValidationError("sqlalchemy_uri"))
        if not database_name:
            exceptions.append(DatabaseRequiredFieldValidationError("database_name"))
        else:
            # Check database_name uniqueness
            if not DatabaseDAO.validate_uniqueness(database_name):
                exceptions.append(DatabaseExistsValidationError())

        if exceptions:
            exception = DatabaseInvalidError()
            exception.add_list(exceptions)
            raise exception
示例#17
0
    def validate(self) -> None:
        exceptions: List[ValidationError] = list()
        owner_ids: Optional[List[int]] = self._properties.get("owners")
        name = self._properties.get("name", "")
        report_type = self._properties.get("type")

        # Validate type is required
        if not report_type:
            exceptions.append(ReportScheduleRequiredTypeValidationError())

        # Validate name type uniqueness
        if report_type and not ReportScheduleDAO.validate_update_uniqueness(
            name, report_type
        ):
            exceptions.append(ReportScheduleNameUniquenessValidationError())

        # validate relation by report type
        if report_type == ReportScheduleType.ALERT:
            database_id = self._properties.get("database")
            if not database_id:
                exceptions.append(ReportScheduleAlertRequiredDatabaseValidationError())
            else:
                database = DatabaseDAO.find_by_id(database_id)
                if not database:
                    exceptions.append(DatabaseNotFoundValidationError())
                self._properties["database"] = database

        # Validate chart or dashboard relations
        self.validate_chart_dashboard(exceptions)

        if "validator_config_json" in self._properties:
            self._properties["validator_config_json"] = json.dumps(
                self._properties["validator_config_json"]
            )

        try:
            owners = self.populate_owners(self._actor, owner_ids)
            self._properties["owners"] = owners
        except ValidationError as ex:
            exceptions.append(ex)
        if exceptions:
            exception = ReportScheduleInvalidError()
            exception.add_list(exceptions)
            raise exception
示例#18
0
文件: create.py 项目: ws1993/superset
 def validate(self) -> None:
     exceptions: List[ValidationError] = []
     sqlalchemy_uri: Optional[str] = self._properties.get("sqlalchemy_uri")
     database_name: Optional[str] = self._properties.get("database_name")
     if not sqlalchemy_uri:
         exceptions.append(DatabaseRequiredFieldValidationError("sqlalchemy_uri"))
     if not database_name:
         exceptions.append(DatabaseRequiredFieldValidationError("database_name"))
     else:
         # Check database_name uniqueness
         if not DatabaseDAO.validate_uniqueness(database_name):
             exceptions.append(DatabaseExistsValidationError())
     if exceptions:
         exception = DatabaseInvalidError()
         exception.add_list(exceptions)
         event_logger.log_with_context(
             action=f"db_connection_failed.{exception.__class__.__name__}"
         )
         raise exception
示例#19
0
    def run(self) -> Model:
        self.validate()
        if not self._model:
            raise DatabaseNotFoundError()
        old_database_name = self._model.database_name

        try:
            database = DatabaseDAO.update(self._model,
                                          self._properties,
                                          commit=False)
            database.set_sqlalchemy_uri(database.sqlalchemy_uri)
            # adding a new database we always want to force refresh schema list
            # TODO Improve this simplistic implementation for catching DB conn fails
            try:
                schemas = database.get_all_schema_names()
            except Exception as ex:
                db.session.rollback()
                raise DatabaseConnectionFailedError() from ex
            # Update database schema permissions
            new_schemas: List[str] = []
            for schema in schemas:
                old_view_menu_name = security_manager.get_schema_perm(
                    old_database_name, schema)
                new_view_menu_name = security_manager.get_schema_perm(
                    database.database_name, schema)
                schema_pvm = security_manager.find_permission_view_menu(
                    "schema_access", old_view_menu_name)
                # Update the schema permission if the database name changed
                if schema_pvm and old_database_name != database.database_name:
                    schema_pvm.view_menu.name = new_view_menu_name
                else:
                    new_schemas.append(schema)
            for schema in new_schemas:
                security_manager.add_permission_view_menu(
                    "schema_access",
                    security_manager.get_schema_perm(database, schema))
            db.session.commit()

        except DAOUpdateFailedError as ex:
            logger.exception(ex.exception)
            raise DatabaseUpdateFailedError() from ex
        return database
示例#20
0
    def run(self) -> Model:
        self.validate()
        try:
            database = DatabaseDAO.create(self._properties, commit=False)
            database.set_sqlalchemy_uri(database.sqlalchemy_uri)

            try:
                TestConnectionDatabaseCommand(self._actor, self._properties).run()
            except Exception:
                db.session.rollback()
                raise DatabaseConnectionFailedError()

            # adding a new database we always want to force refresh schema list
            schemas = database.get_all_schema_names(cache=False)
            for schema in schemas:
                security_manager.add_permission_view_menu(
                    "schema_access", security_manager.get_schema_perm(database, schema)
                )
            security_manager.add_permission_view_menu("database_access", database.perm)
            db.session.commit()
        except DAOCreateFailedError as ex:
            logger.exception(ex.exception)
            raise DatabaseCreateFailedError()
        return database
示例#21
0
 def validate(self) -> None:
     database_name = self._properties.get("database_name")
     if database_name is not None:
         self._model = DatabaseDAO.get_database_by_name(database_name)
    def run(self) -> None:
        self.validate()
        uri = self._properties.get("sqlalchemy_uri", "")
        if self._model and uri == self._model.safe_sqlalchemy_uri():
            uri = self._model.sqlalchemy_uri_decrypted

        # context for error messages
        url = make_url(uri)
        context = {
            "hostname": url.host,
            "password": url.password,
            "port": url.port,
            "username": url.username,
            "database": url.database,
        }

        try:
            database = DatabaseDAO.build_db_for_connection_test(
                server_cert=self._properties.get("server_cert", ""),
                extra=self._properties.get("extra", "{}"),
                impersonate_user=self._properties.get("impersonate_user",
                                                      False),
                encrypted_extra=self._properties.get("encrypted_extra", "{}"),
            )

            database.set_sqlalchemy_uri(uri)
            database.db_engine_spec.mutate_db_for_connection_test(database)
            username = self._actor.username if self._actor is not None else None
            engine = database.get_sqla_engine(user_name=username)
            event_logger.log_with_context(
                action="test_connection_attempt",
                engine=database.db_engine_spec.__name__,
            )
            with closing(engine.raw_connection()) as conn:
                try:
                    alive = func_timeout(
                        int(app.config["TEST_DATABASE_CONNECTION_TIMEOUT"].
                            total_seconds()),
                        engine.dialect.do_ping,
                        args=(conn, ),
                    )
                except sqlite3.ProgrammingError:
                    # SQLite can't run on a separate thread, so ``func_timeout`` fails
                    alive = engine.dialect.do_ping(conn)
                except FunctionTimedOut as ex:
                    raise SupersetTimeoutException(
                        error_type=SupersetErrorType.
                        CONNECTION_DATABASE_TIMEOUT,
                        message=
                        ("Please check your connection details and database settings, "
                         "and ensure that your database is accepting connections, "
                         "then try connecting again."),
                        level=ErrorLevel.ERROR,
                        extra={"sqlalchemy_uri": database.sqlalchemy_uri},
                    ) from ex
                except Exception:  # pylint: disable=broad-except
                    alive = False
                if not alive:
                    raise DBAPIError(None, None, None)

            # Log succesful connection test with engine
            event_logger.log_with_context(
                action="test_connection_success",
                engine=database.db_engine_spec.__name__,
            )

        except (NoSuchModuleError, ModuleNotFoundError) as ex:
            event_logger.log_with_context(
                action=f"test_connection_error.{ex.__class__.__name__}",
                engine=database.db_engine_spec.__name__,
            )
            raise DatabaseTestConnectionDriverError(
                message=_("Could not load database driver: {}").format(
                    database.db_engine_spec.__name__), ) from ex
        except DBAPIError as ex:
            event_logger.log_with_context(
                action=f"test_connection_error.{ex.__class__.__name__}",
                engine=database.db_engine_spec.__name__,
            )
            # check for custom errors (wrong username, wrong password, etc)
            errors = database.db_engine_spec.extract_errors(ex, context)
            raise DatabaseTestConnectionFailedError(errors) from ex
        except SupersetSecurityException as ex:
            event_logger.log_with_context(
                action=f"test_connection_error.{ex.__class__.__name__}",
                engine=database.db_engine_spec.__name__,
            )
            raise DatabaseSecurityUnsafeError(message=str(ex)) from ex
        except SupersetTimeoutException as ex:
            event_logger.log_with_context(
                action=f"test_connection_error.{ex.__class__.__name__}",
                engine=database.db_engine_spec.__name__,
            )
            # bubble up the exception to return a 408
            raise ex
        except Exception as ex:
            event_logger.log_with_context(
                action=f"test_connection_error.{ex.__class__.__name__}",
                engine=database.db_engine_spec.__name__,
            )
            errors = database.db_engine_spec.extract_errors(ex, context)
            raise DatabaseTestConnectionUnexpectedError(errors) from ex
示例#23
0
 def validate(self) -> None:
     self._models = DatabaseDAO.find_by_ids(self.database_ids)
     if len(self._models) != len(self.database_ids):
         raise DatabaseNotFoundError()
示例#24
0
    def run(self) -> None:
        self.validate()
        uri = self._properties.get("sqlalchemy_uri", "")
        if self._model and uri == self._model.safe_sqlalchemy_uri():
            uri = self._model.sqlalchemy_uri_decrypted

        # context for error messages
        url = make_url(uri)
        context = {
            "hostname": url.host,
            "password": url.password,
            "port": url.port,
            "username": url.username,
            "database": url.database,
        }

        try:
            database = DatabaseDAO.build_db_for_connection_test(
                server_cert=self._properties.get("server_cert", ""),
                extra=self._properties.get("extra", "{}"),
                impersonate_user=self._properties.get("impersonate_user",
                                                      False),
                encrypted_extra=self._properties.get("encrypted_extra", "{}"),
            )

            database.set_sqlalchemy_uri(uri)
            database.db_engine_spec.mutate_db_for_connection_test(database)
            username = self._actor.username if self._actor is not None else None
            engine = database.get_sqla_engine(user_name=username)
            event_logger.log_with_context(
                action="test_connection_attempt",
                engine=database.db_engine_spec.__name__,
            )
            with closing(engine.raw_connection()) as conn:
                try:
                    alive = engine.dialect.do_ping(conn)
                except Exception:  # pylint: disable=broad-except
                    alive = False
                if not alive:
                    raise DBAPIError(None, None, None)

            # Log succesful connection test with engine
            event_logger.log_with_context(
                action="test_connection_success",
                engine=database.db_engine_spec.__name__,
            )

        except (NoSuchModuleError, ModuleNotFoundError) as ex:
            event_logger.log_with_context(
                action=f"test_connection_error.{ex.__class__.__name__}",
                engine=database.db_engine_spec.__name__,
            )
            raise DatabaseTestConnectionDriverError(
                message=_("Could not load database driver: {}").format(
                    database.db_engine_spec.__name__), ) from ex
        except DBAPIError as ex:
            event_logger.log_with_context(
                action=f"test_connection_error.{ex.__class__.__name__}",
                engine=database.db_engine_spec.__name__,
            )
            # check for custom errors (wrong username, wrong password, etc)
            errors = database.db_engine_spec.extract_errors(ex, context)
            raise DatabaseTestConnectionFailedError(errors) from ex
        except SupersetSecurityException as ex:
            event_logger.log_with_context(
                action=f"test_connection_error.{ex.__class__.__name__}",
                engine=database.db_engine_spec.__name__,
            )
            raise DatabaseSecurityUnsafeError(message=str(ex)) from ex
        except Exception as ex:
            event_logger.log_with_context(
                action=f"test_connection_error.{ex.__class__.__name__}",
                engine=database.db_engine_spec.__name__,
            )
            errors = database.db_engine_spec.extract_errors(ex, context)
            raise DatabaseTestConnectionUnexpectedError(errors) from ex
示例#25
0
    def validate(self) -> None:
        exceptions: List[ValidationError] = list()
        owner_ids: Optional[List[int]] = self._properties.get("owners")
        report_type = self._properties.get("type", ReportScheduleType.ALERT)

        name = self._properties.get("name", "")
        self._model = ReportScheduleDAO.find_by_id(self._model_id)

        # Does the report exist?
        if not self._model:
            raise ReportScheduleNotFoundError()

        # Change the state to not triggered when the user deactivates
        # A report that is currently in a working state. This prevents
        # an alert/report from being kept in a working state if activated back
        if (self._model.last_state == ReportState.WORKING
                and "active" in self._properties
                and not self._properties["active"]):
            self._properties["last_state"] = ReportState.NOOP

        # validate relation by report type
        if not report_type:
            report_type = self._model.type

        # Validate name type uniqueness
        if not ReportScheduleDAO.validate_update_uniqueness(
                name, report_type, report_schedule_id=self._model_id):
            exceptions.append(ReportScheduleNameUniquenessValidationError())

        if report_type == ReportScheduleType.ALERT:
            database_id = self._properties.get("database")
            # If database_id was sent let's validate it exists
            if database_id:
                database = DatabaseDAO.find_by_id(database_id)
                if not database:
                    exceptions.append(DatabaseNotFoundValidationError())
                self._properties["database"] = database

        # Validate chart or dashboard relations
        self.validate_chart_dashboard(exceptions, update=True)

        if "validator_config_json" in self._properties:
            self._properties["validator_config_json"] = json.dumps(
                self._properties["validator_config_json"])

        # Check ownership
        try:
            check_ownership(self._model)
        except SupersetSecurityException:
            raise ReportScheduleForbiddenError()

        # Validate/Populate owner
        if owner_ids is None:
            owner_ids = [owner.id for owner in self._model.owners]
        try:
            owners = populate_owners(self._actor, owner_ids)
            self._properties["owners"] = owners
        except ValidationError as ex:
            exceptions.append(ex)
        if exceptions:
            exception = ReportScheduleInvalidError()
            exception.add_list(exceptions)
            raise exception
示例#26
0
    def run(self) -> None:
        engine = self._properties["engine"]
        engine_specs = get_engine_specs()

        if engine in BYPASS_VALIDATION_ENGINES:
            # Skip engines that are only validated onCreate
            return

        if engine not in engine_specs:
            raise InvalidEngineError(
                SupersetError(
                    message=__(
                        'Engine "%(engine)s" is not a valid engine.',
                        engine=engine,
                    ),
                    error_type=SupersetErrorType.GENERIC_DB_ENGINE_ERROR,
                    level=ErrorLevel.ERROR,
                    extra={
                        "allowed": list(engine_specs),
                        "provided": engine
                    },
                ), )
        engine_spec = engine_specs[engine]
        if not issubclass(engine_spec, BasicParametersMixin):
            raise InvalidEngineError(
                SupersetError(
                    message=__(
                        'Engine "%(engine)s" cannot be configured through parameters.',
                        engine=engine,
                    ),
                    error_type=SupersetErrorType.GENERIC_DB_ENGINE_ERROR,
                    level=ErrorLevel.ERROR,
                    extra={
                        "allowed": [
                            name for name, engine_spec in engine_specs.items()
                            if issubclass(engine_spec, BasicParametersMixin)
                        ],
                        "provided":
                        engine,
                    },
                ), )

        # perform initial validation
        errors = engine_spec.validate_parameters(
            self._properties.get("parameters", {}))
        if errors:
            raise InvalidParametersError(errors)

        serialized_encrypted_extra = self._properties.get(
            "encrypted_extra", "{}")
        try:
            encrypted_extra = json.loads(serialized_encrypted_extra)
        except json.decoder.JSONDecodeError:
            encrypted_extra = {}

        # try to connect
        sqlalchemy_uri = engine_spec.build_sqlalchemy_uri(
            self._properties.get("parameters", None),  # type: ignore
            encrypted_extra,
        )
        if self._model and sqlalchemy_uri == self._model.safe_sqlalchemy_uri():
            sqlalchemy_uri = self._model.sqlalchemy_uri_decrypted
        database = DatabaseDAO.build_db_for_connection_test(
            server_cert=self._properties.get("server_cert", ""),
            extra=self._properties.get("extra", "{}"),
            impersonate_user=self._properties.get("impersonate_user", False),
            encrypted_extra=serialized_encrypted_extra,
        )
        database.set_sqlalchemy_uri(sqlalchemy_uri)
        database.db_engine_spec.mutate_db_for_connection_test(database)
        username = self._actor.username if self._actor is not None else None
        engine = database.get_sqla_engine(user_name=username)
        try:
            with closing(engine.raw_connection()) as conn:
                alive = engine.dialect.do_ping(conn)
        except Exception as ex:  # pylint: disable=broad-except
            url = make_url(sqlalchemy_uri)
            context = {
                "hostname": url.host,
                "password": url.password,
                "port": url.port,
                "username": url.username,
                "database": url.database,
            }
            errors = database.db_engine_spec.extract_errors(ex, context)
            raise DatabaseTestConnectionFailedError(errors)

        if not alive:
            raise DatabaseOfflineError(
                SupersetError(
                    message=__("Database is offline."),
                    error_type=SupersetErrorType.GENERIC_DB_ENGINE_ERROR,
                    level=ErrorLevel.ERROR,
                ), )