You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by hu...@apache.org on 2022/11/22 22:15:16 UTC

[superset] 01/02: write class in config

This is an automated email from the ASF dual-hosted git repository.

hugh pushed a commit to branch ssh-manager-config
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 0f6038f71de05d5185c59c9060249316a13ee5b1
Author: hughhhh <hu...@gmail.com>
AuthorDate: Tue Nov 22 17:02:08 2022 -0500

    write class in config
---
 superset/config.py                               | 38 ++++++++++++++++++++++++
 superset/databases/ssh_tunnel/commands/create.py |  6 ++++
 superset/databases/ssh_tunnel/commands/update.py |  8 +++++
 3 files changed, 52 insertions(+)

diff --git a/superset/config.py b/superset/config.py
index f163997c6e..23afbd1c6a 100644
--- a/superset/config.py
+++ b/superset/config.py
@@ -471,8 +471,46 @@ DEFAULT_FEATURE_FLAGS: Dict[str, bool] = {
     "DRILL_TO_DETAIL": False,
     "DATAPANEL_CLOSED_BY_DEFAULT": False,
     "HORIZONTAL_FILTER_BAR": False,
+    # Allow users to enable ssh tunneling when creating a DB.
+    # Users must check whether the DB engine supports SSH Tunnels
+    # otherwise enabling this flag won't have any effect on the DB.
+    "SSH_TUNNELING": False,
 }
 
+# ------------------------------
+# SSH Tunnel
+# ------------------------------
+# Allow users to set the host used when connecting to the SSH Tunnel
+# as localhost and any other alias (0.0.0.0)
+# ----------------------------------------------------------------------
+#                             |
+# -------------+              |    +----------+
+#     LOCAL    |              |    |  REMOTE  | :22 SSH
+#     CLIENT   | <== SSH ========> |  SERVER  | :8080 web service
+# -------------+              |    +----------+
+#                             |
+#                          FIREWALL (only port 22 is open)
+
+# ----------------------------------------------------------------------
+# class SSHManager:
+#     def validate(self):
+#         # validation on CREATE + UPDATE on SSHTunnel Model
+#         raise NotImplemented()
+
+#     def mutator(self):
+#         # override any ssh tunnel configuration object
+#         raise NotImplemented()
+
+#     @property
+#     def local_bind_address(self):
+#         # set the local binding address for the local client
+#         # the port will be dynamically configured by the sshtunnel.SSHTunnelForwarder
+#         # `server` return value
+#         return "127.0.0.1"
+
+
+SSH_TUNNEL_MANAGER = None
+
 # Feature flags may also be set via 'SUPERSET_FEATURE_' prefixed environment vars.
 DEFAULT_FEATURE_FLAGS.update(
     {
diff --git a/superset/databases/ssh_tunnel/commands/create.py b/superset/databases/ssh_tunnel/commands/create.py
index 1185d20bdf..8ae88a9f2d 100644
--- a/superset/databases/ssh_tunnel/commands/create.py
+++ b/superset/databases/ssh_tunnel/commands/create.py
@@ -20,12 +20,16 @@ from typing import Any, Dict, List, Optional
 from flask_appbuilder.models.sqla import Model
 from marshmallow import ValidationError
 
+from superset import app
 from superset.commands.base import BaseCommand
 from superset.dao.exceptions import DAOCreateFailedError
 from superset.databases.dao import DatabaseDAO
 from superset.databases.ssh_tunnel.commands.exceptions import SSHTunnelCreateFailedError
 from superset.databases.ssh_tunnel.dao import SSHTunnelDAO
 
+config = app.config
+ssh_tunnel_manager = config["SSH_TUNNEL_MANAGER"]
+
 logger = logging.getLogger(__name__)
 
 
@@ -47,4 +51,6 @@ class CreateSSHTunnelCommand(BaseCommand):
     def validate(self) -> None:
         # TODO(hughhh): check to make sure the server port is not localhost
         # using the config.SSH_TUNNEL_MANAGER
+        if ssh_tunnel_manager:
+            ssh_tunnel_manager.validate()
         return
diff --git a/superset/databases/ssh_tunnel/commands/update.py b/superset/databases/ssh_tunnel/commands/update.py
index a9d9a6fe6d..146cc34496 100644
--- a/superset/databases/ssh_tunnel/commands/update.py
+++ b/superset/databases/ssh_tunnel/commands/update.py
@@ -20,6 +20,7 @@ from typing import Any, Dict, List, Optional
 from flask_appbuilder.models.sqla import Model
 from marshmallow import ValidationError
 
+from superset import app
 from superset.commands.base import BaseCommand
 from superset.dao.exceptions import DAOUpdateFailedError
 from superset.databases.ssh_tunnel.commands.exceptions import (
@@ -33,6 +34,8 @@ from superset.databases.ssh_tunnel.models import SSHTunnel
 
 logger = logging.getLogger(__name__)
 
+config = app.config
+ssh_tunnel_manager = config["SSH_TUNNEL_MANAGER"]
 
 class UpdateSSHTunnelCommand(BaseCommand):
     def __init__(self, model_id: int, data: Dict[str, Any]):
@@ -53,3 +56,8 @@ class UpdateSSHTunnelCommand(BaseCommand):
         self._model = SSHTunnelDAO.find_by_id(self._model_id)
         if not self._model:
             raise SSHTunnelNotFoundError()
+        
+        # TODO(hughhh): check to make sure the server port is not localhost
+        # using the config.SSH_TUNNEL_MANAGER
+        if ssh_tunnel_manager:
+            ssh_tunnel_manager.validate()