-
Notifications
You must be signed in to change notification settings - Fork 14.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(databases): test connection api (#10723)
* test connection api on databases * update test connection tests * update database api test and open api description * moved test connection to commands * update error message * fix isort * fix mypy * fix black * fix mypy pre commit
- Loading branch information
Lily Kuang
authored
Sep 9, 2020
1 parent
9a59bdd
commit 8a3ac70
Showing
10 changed files
with
316 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# 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 logging | ||
from contextlib import closing | ||
from typing import Any, Dict, Optional | ||
|
||
import simplejson as json | ||
from flask_appbuilder.security.sqla.models import User | ||
from sqlalchemy import select | ||
|
||
from superset.commands.base import BaseCommand | ||
from superset.databases.commands.exceptions import DatabaseSecurityUnsafeError | ||
from superset.databases.dao import DatabaseDAO | ||
from superset.models.core import Database | ||
from superset.security.analytics_db_safety import DBSecurityException | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class TestConnectionDatabaseCommand(BaseCommand): | ||
def __init__(self, user: User, data: Dict[str, Any]): | ||
self._actor = user | ||
self._properties = data.copy() | ||
self._model: Optional[Database] = None | ||
|
||
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=json.dumps(self._properties.get("extra", {})), | ||
impersonate_user=self._properties.get("impersonate_user", False), | ||
encrypted_extra=json.dumps(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.connect()) as conn: | ||
conn.scalar(select([1])) | ||
except DBSecurityException as ex: | ||
logger.warning(ex) | ||
raise DatabaseSecurityUnsafeError() | ||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.