-
Notifications
You must be signed in to change notification settings - Fork 14.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: validate_parameters for GSheets #15578
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,4 @@ pylint | |
pytest | ||
pytest-cov | ||
statsd | ||
pytest-mock |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,14 +17,17 @@ | |
import json | ||
import re | ||
from contextlib import closing | ||
from typing import Any, Dict, Optional, Pattern, Tuple, TYPE_CHECKING | ||
from typing import Any, Dict, List, Optional, Pattern, Tuple, TYPE_CHECKING | ||
|
||
from flask import g | ||
from flask_babel import gettext as __ | ||
from sqlalchemy.engine import create_engine | ||
from sqlalchemy.engine.url import URL | ||
from typing_extensions import TypedDict | ||
|
||
from superset import security_manager | ||
from superset.db_engine_specs.sqlite import SqliteEngineSpec | ||
from superset.errors import SupersetErrorType | ||
from superset.errors import ErrorLevel, SupersetError, SupersetErrorType | ||
|
||
if TYPE_CHECKING: | ||
from superset.models.core import Database | ||
|
@@ -33,6 +36,12 @@ | |
SYNTAX_ERROR_REGEX = re.compile('SQLError: near "(?P<server_error>.*?)": syntax error') | ||
|
||
|
||
class GSheetsParametersType(TypedDict): | ||
credentials_info: Dict[str, Any] | ||
query: Dict[str, Any] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gshees doesn't need query There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But it does! The user can definitely pass options to it via the query string. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the query string for each individual url in the catalog? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it's to pass parameters to configure the engine, eg:
This makes the dropdown show not only sheets in the catalog, but all of the user's sheets. This can be done in the "extra" session as well, to be clear. |
||
table_catalog: Dict[str, str] | ||
|
||
|
||
class GSheetsEngineSpec(SqliteEngineSpec): | ||
"""Engine for Google spreadsheets""" | ||
|
||
|
@@ -45,7 +54,7 @@ class GSheetsEngineSpec(SqliteEngineSpec): | |
SYNTAX_ERROR_REGEX: ( | ||
__( | ||
'Please check your query for syntax errors near "%(server_error)s". ' | ||
"Then, try running your query again." | ||
"Then, try running your query again.", | ||
), | ||
SupersetErrorType.SYNTAX_ERROR, | ||
{}, | ||
|
@@ -54,7 +63,7 @@ class GSheetsEngineSpec(SqliteEngineSpec): | |
|
||
@classmethod | ||
def modify_url_for_impersonation( | ||
cls, url: URL, impersonate_user: bool, username: Optional[str] | ||
cls, url: URL, impersonate_user: bool, username: Optional[str], | ||
) -> None: | ||
if impersonate_user and username is not None: | ||
user = security_manager.find_user(username=username) | ||
|
@@ -77,3 +86,41 @@ def extra_table_metadata( | |
metadata = {} | ||
|
||
return {"metadata": metadata["extra"]} | ||
|
||
@classmethod | ||
def validate_parameters( | ||
cls, parameters: GSheetsParametersType, | ||
) -> List[SupersetError]: | ||
errors: List[SupersetError] = [] | ||
|
||
credentials_info = parameters.get("credentials_info") | ||
table_catalog = parameters.get("table_catalog", {}) | ||
|
||
if not table_catalog: | ||
return errors | ||
|
||
# We need a subject in case domain wide delegation is set, otherwise the | ||
# check will fail. This means that the admin will be able to add sheets | ||
# that only they have access, even if later users are not able to access | ||
# them. | ||
subject = g.user.email if g.user else None | ||
|
||
engine = create_engine( | ||
"gsheets://", service_account_info=credentials_info, subject=subject, | ||
) | ||
conn = engine.connect() | ||
for name, url in table_catalog.items(): | ||
try: | ||
results = conn.execute(f'SELECT * FROM "{url}" LIMIT 1') | ||
results.fetchall() | ||
except Exception: # pylint: disable=broad-except | ||
errors.append( | ||
SupersetError( | ||
message=f"Unable to connect to spreadsheet {name} at {url}", | ||
error_type=SupersetErrorType.TABLE_DOES_NOT_EXIST_ERROR, | ||
level=ErrorLevel.WARNING, | ||
extra={"name": name, "url": url}, | ||
), | ||
) | ||
|
||
return errors |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# 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 pytest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @betodealmeida we could look at using pytest-flask. |
||
|
||
|
||
@pytest.fixture | ||
def app_context(): | ||
""" | ||
A fixture for running the test inside an app context. | ||
""" | ||
from superset.app import create_app | ||
|
||
app = create_app() | ||
with app.app_context(): | ||
yield |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
be careful, TypedDict added in 3.8 so ... maybe the name should be changed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, this is a backport so we can use it in 3.7: https://github.com/python/typing/blob/master/typing_extensions/README.rst