betodealmeida commented on code in PR #14225: URL: https://github.com/apache/superset/pull/14225#discussion_r875068304
########## superset/db_engine_specs/superset.py: ########## @@ -0,0 +1,343 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +import datetime +import operator +import urllib.parse +from functools import wraps +from typing import ( + Any, + Callable, + cast, + Dict, + Iterator, + List, + Optional, + Tuple, + Type, + TypeVar, +) + +from flask import g +from shillelagh.adapters.base import Adapter +from shillelagh.backends.apsw.dialect import APSWDialect +from shillelagh.exceptions import ProgrammingError +from shillelagh.fields import ( + Blob, + Boolean, + Date, + DateTime, + Field, + Float, + Integer, + Order, + String, + Time, +) +from shillelagh.filters import Equal, Filter, Range +from shillelagh.types import RequestedOrder, Row +from sqlalchemy import MetaData, Table +from sqlalchemy.engine.url import URL +from sqlalchemy.exc import NoSuchTableError +from sqlalchemy.pool.base import _ConnectionFairy +from sqlalchemy.sql import Select, select + +from superset import db, security_manager, sql_parse +from superset.db_engine_specs.sqlite import SqliteEngineSpec + + +class SupersetEngineSpec(SqliteEngineSpec): + """ + Internal engine for Superset + + This DB engine spec is a meta-database. It uses the shillelagh library + to build a DB that can operate across different Superset databases. + """ + + engine = "superset" + engine_name = "Superset" + + @classmethod + def modify_url_for_impersonation( + cls, url: URL, impersonate_user: bool, username: Optional[str] + ) -> None: + if impersonate_user: + url.username = username + + +# pylint: disable=abstract-method +class SupersetAPSWDialect(APSWDialect): + + """ + A SQLAlchemy dialect for an internal Superset engine. + + This dialect allows query to be executed across different Superset + databases. For example, to read data from the `birth_names` table in the + `examples` databases: + + >>> engine = create_engine('superset://') + >>> conn = engine.connect() + >>> results = conn.execute('SELECT * FROM "superset.examples.birth_names"') + + Queries can also join data across different Superset databases. + + The dialect is built in top of the shillelagh library, leveraging SQLite to + create virtual tables on-the-fly proxying Superset tables. The + `SupersetShillelaghAdapter` adapter is responsible for returning data when a + Superset table is accessed. + """ + + name = "superset" + + # pylint: disable=unused-argument + def create_connect_args(self, url: URL) -> Tuple[Tuple[()], Dict[str, Any]]: + return ( + (), + { + "path": ":memory:", + "adapters": ["superset"], + "adapter_args": {"supersetshillelaghadapter": (url.username,)}, + "safe": True, + "isolation_level": self.isolation_level, + }, + ) + + # pylint: disable=unused-argument, no-self-use + def get_schema_names( + self, connection: _ConnectionFairy, **kwargs: Any + ) -> List[str]: + return [] + + +# pylint: disable=invalid-name +F = TypeVar("F", bound=Callable[..., Any]) + + +def check_dml(method: F) -> F: + @wraps(method) + def wrapper(self: "SupersetShillelaghAdapter", *args: Any, **kwargs: Any) -> Any: + # pylint: disable=protected-access + if not self._allow_dml: + raise ProgrammingError(f'DML not enabled in database "{self.database}"') + return method(self, *args, **kwargs) + + return cast(F, wrapper) + + +def has_rowid(method: F) -> F: + @wraps(method) + def wrapper(self: "SupersetShillelaghAdapter", *args: Any, **kwargs: Any) -> Any: + # pylint: disable=protected-access + if not self._rowid: + raise ProgrammingError( + "Can only modify data in a table with a single, integer, primary key" + ) + return method(self, *args, **kwargs) + + return cast(F, wrapper) + + +# pylint: disable=too-many-instance-attributes +class SupersetShillelaghAdapter(Adapter): + + """ + A shillelagh adapter for Superset tables. + + Shillelagh adapters are responsible for fetching data from a given resource, + allowing it to be represented as a virtual table in SQLite. This one works + as a proxy to Superset tables. + """ + + safe = True + + type_map: Dict[Any, Type[Field]] = { + bool: Boolean, + float: Float, + int: Integer, + str: String, + datetime.date: Date, + datetime.datetime: DateTime, + datetime.time: Time, + } + + @staticmethod + def supports(uri: str) -> bool: + # An URL for a table has the format superset.database[.catalog][.schema].table, + # eg, superset.examples.birth_names + parsed = urllib.parse.urlparse(uri) + parts = parsed.path.split(".") + return 3 <= len(parts) <= 5 and parts[0] == "superset" + + @staticmethod + def parse_uri(uri: str) -> Tuple[str, Optional[str], Optional[str], str]: + parsed = urllib.parse.urlparse(uri) + parts = parsed.path.split(".") + if len(parts) == 3: + return parts[1], None, None, parts[2] + if len(parts) == 4: + return parts[1], None, parts[2], parts[3] + return tuple(parts[1:]) # type: ignore + + def __init__( + self, + database: str, + catalog: Optional[str], + schema: Optional[str], + table: str, + username: str, + ): + self.database = database + self.catalog = catalog + self.schema = schema + self.table = table + self.username = username + + self._rowid: Optional[str] = None + self._allow_dml: bool = False + self._set_columns() + + @classmethod + def get_field(cls, python_type: Any) -> Field: + class_ = cls.type_map.get(python_type, Blob) + return class_(filters=[Equal, Range], order=Order.ANY, exact=True) + + def _set_columns(self) -> None: + from superset.models.core import Database + + database = ( + db.session.query(Database).filter_by(database_name=self.database).first() + ) + if database is None: + raise ProgrammingError(f"Database not found: {self.database}") + self._allow_dml = database.allow_dml + + # verify permissions + # set user since the adapter runs in a different thread + g.user = security_manager.get_user_by_username(self.username) Review Comment: I used the same approach from #19999 and passed username, which is then used by the security manager iff `g.user` is undefined. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org