-
Notifications
You must be signed in to change notification settings - Fork 18k
feat: add SSL certificate validation for Druid #9396
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
Changes from 3 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 |
|---|---|---|
|
|
@@ -14,15 +14,20 @@ | |
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| from typing import TYPE_CHECKING | ||
| import json | ||
| import logging | ||
| from typing import Any, Dict, TYPE_CHECKING | ||
|
|
||
| from superset.db_engine_specs.base import BaseEngineSpec | ||
| from superset.utils import core as utils | ||
|
|
||
| if TYPE_CHECKING: | ||
| from superset.connectors.sqla.models import ( # pylint: disable=unused-import | ||
| TableColumn, | ||
| ) | ||
|
|
||
| logger = logging.getLogger() | ||
|
|
||
|
|
||
| class DruidEngineSpec(BaseEngineSpec): # pylint: disable=abstract-method | ||
| """Engine spec for Druid.io""" | ||
|
|
@@ -47,3 +52,26 @@ class DruidEngineSpec(BaseEngineSpec): # pylint: disable=abstract-method | |
| def alter_new_orm_column(cls, orm_col: "TableColumn") -> None: | ||
| if orm_col.column_name == "__time": | ||
| orm_col.is_dttm = True | ||
|
|
||
| @staticmethod | ||
| def get_extra_params(database: "Database") -> Dict[str, Any]: | ||
| """ | ||
| For Druid, the path to a SSL certificate is placed in `connect_args`. | ||
|
|
||
| :param database: database instance from which to extract extras | ||
|
Member
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. nit: it's nice to add a :raises: on the docstring when a method can raise an exception |
||
| """ | ||
| try: | ||
| extra = json.loads(database.extra or "{}") | ||
| except json.JSONDecodeError as e: | ||
| logger.error(e) | ||
| raise e | ||
|
|
||
| if database.server_cert: | ||
| engine_params = extra.get("engine_params", {}) | ||
| connect_args = engine_params.get("connect_args", {}) | ||
| connect_args["scheme"] = "https" | ||
| path = utils.create_ssl_cert_file(database.server_cert) | ||
| connect_args["ssl_verify_cert"] = path | ||
|
villebro marked this conversation as resolved.
|
||
| engine_params["connect_args"] = connect_args | ||
| extra["engine_params"] = engine_params | ||
| return extra | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # 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. | ||
| """add certificate to dbs | ||
|
|
||
| Revision ID: b5998378c225 | ||
| Revises: 72428d1ea401 | ||
| Create Date: 2020-03-25 10:49:10.883065 | ||
|
|
||
| """ | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "b5998378c225" | ||
| down_revision = "72428d1ea401" | ||
|
|
||
| from typing import Dict | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
| from sqlalchemy.dialects.postgresql.base import PGDialect | ||
| from sqlalchemy_utils import EncryptedType | ||
|
|
||
|
|
||
| def upgrade(): | ||
| kwargs: Dict[str, str] = {} | ||
| bind = op.get_bind() | ||
| if isinstance(bind.dialect, PGDialect): | ||
| kwargs["postgresql_using"] = "encrypted_extra::bytea" | ||
| op.add_column( | ||
| "dbs", | ||
| sa.Column("server_cert", EncryptedType(sa.Text()), nullable=True, **kwargs), | ||
|
Member
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. Would it make sense, while we're at it, to create a client_cert field also?
Member
Author
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. I did but took it out, as |
||
| ) | ||
|
|
||
|
|
||
| def downgrade(): | ||
| op.drop_column("dbs", "server_cert") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,7 @@ class DatabaseMixin: | |
| "allow_multi_schema_metadata_fetch", | ||
| "extra", | ||
| "encrypted_extra", | ||
| "server_cert", | ||
| ] | ||
| search_exclude_columns = ( | ||
| "password", | ||
|
|
@@ -74,6 +75,7 @@ class DatabaseMixin: | |
| "queries", | ||
| "saved_queries", | ||
| "encrypted_extra", | ||
| "server_cert", | ||
| ) | ||
| edit_columns = add_columns | ||
| show_columns = [ | ||
|
|
@@ -149,6 +151,11 @@ class DatabaseMixin: | |
| "syntax normally used by SQLAlchemy.", | ||
| True, | ||
| ), | ||
| "server_cert": utils.markdown( | ||
| "Optional CA_BUNDLE contents to validate HTTPS requests. Only available " | ||
| "on certain database engines.", | ||
| True, | ||
| ), | ||
| "impersonate_user": _( | ||
| "If Presto, all the queries in SQL Lab are going to be executed as the " | ||
| "currently logged on user who must have permission to run them.<br/>" | ||
|
|
@@ -183,6 +190,7 @@ class DatabaseMixin: | |
| "cache_timeout": _("Chart Cache Timeout"), | ||
| "extra": _("Extra"), | ||
| "encrypted_extra": _("Secure Extra"), | ||
| "server_cert": _("Root certificate"), | ||
| "allow_run_async": _("Asynchronous Query Execution"), | ||
| "impersonate_user": _("Impersonate the logged on user"), | ||
| "allow_csv_upload": _("Allow Csv Upload"), | ||
|
|
@@ -196,6 +204,9 @@ def _pre_add_update(self, database): | |
| check_sqlalchemy_uri(database.sqlalchemy_uri) | ||
| self.check_extra(database) | ||
| self.check_encrypted_extra(database) | ||
| database.server_cert = ( | ||
| database.server_cert.strip() if database.server_cert else "" | ||
| ) | ||
|
Member
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. I think we should validate the certificate here also, it would give a better user experience, instead of failing when a connection is made
Member
Author
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. Good idea, I'll break it out into a separate util function.
Member
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. "server_cert" is not being validated before persisting to the DB, are we relying on |
||
| 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 | ||
|
|
||
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.
nit: it's nice to add a
:raises:on the docstring when a method can raise an exception