Skip to content

Commit

Permalink
check db extra and metadata params preemptively (apache#6004)
Browse files Browse the repository at this point in the history
* check db extra and metadata params preemptively

* flake8

* make use of inspect
  • Loading branch information
youngyjd authored and bipinsoniguavus committed Dec 24, 2018
1 parent a116f84 commit f227375
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,8 @@ def fetch_metadata(self):
"""Fetches the metadata for the table and merges it in"""
try:
table = self.get_sqla_table_object()
except Exception:
except Exception as e:
logging.exception(e)
raise Exception(_(
"Table [{}] doesn't seem to exist in the specified database, "
"couldn't fetch column information").format(self.table_name))
Expand Down
19 changes: 18 additions & 1 deletion superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from __future__ import unicode_literals

from datetime import datetime, timedelta
import inspect
import logging
import os
import re
Expand All @@ -26,7 +27,7 @@
from six import text_type
from six.moves.urllib import parse
import sqlalchemy as sqla
from sqlalchemy import and_, create_engine, or_, update
from sqlalchemy import and_, create_engine, MetaData, or_, update
from sqlalchemy.engine.url import make_url
from sqlalchemy.exc import IntegrityError
from unidecode import unidecode
Expand Down Expand Up @@ -259,6 +260,7 @@ class DatabaseView(SupersetModelView, DeleteMixin, YamlExportMixin): # noqa
}

def pre_add(self, db):
self.check_extra(db)
db.set_sqlalchemy_uri(db.sqlalchemy_uri)
security_manager.merge_perm('database_access', db.perm)
# adding a new database we always want to force refresh schema list
Expand All @@ -279,6 +281,21 @@ def pre_delete(self, obj):
def _delete(self, pk):
DeleteMixin._delete(self, pk)

def check_extra(self, db):
# this will check whether json.loads(extra) can succeed
try:
extra = db.get_extra()
except Exception as e:
raise Exception('Extra field cannot be decoded by JSON. {}'.format(str(e)))

# this will check whether 'metadata_params' is configured correctly
metadata_signature = inspect.signature(MetaData)
for key in extra.get('metadata_params', {}):
if key not in metadata_signature.parameters:
raise Exception('The metadata_params in Extra field '
'is not configured correctly. The key '
'{} is invalid.'.format(key))


appbuilder.add_link(
'Import Dashboards',
Expand Down

0 comments on commit f227375

Please sign in to comment.