跳到主要内容

配置 Superset

superset_config.py

Superset 通过其 config.py 模块公开了数百个可配置参数。公开的变量和对象作为您可能希望配置、更改和交互的大部分内容的公共接口。在这个 Python 模块中,您将找到所有这些参数、合理的默认值以及以注释形式呈现的丰富文档。

要配置您的应用程序,您需要创建自己的配置模块,这将允许您覆盖其中一些或许多参数。您不需要更改核心模块,而是要定义自己的模块(通常是一个名为 superset_config.py 的文件)。将此文件添加到您的 PYTHONPATH 中,或者创建一个环境变量 SUPERSET_CONFIG_PATH 来指定 superset_config.py 的完整路径。

例如,如果直接在基于 Linux 的系统上部署 Superset,并且您的 superset_config.py 位于 /app 目录下,您可以运行

export SUPERSET_CONFIG_PATH=/app/superset_config.py

如果您正在使用自己的自定义 Dockerfile,并以官方 Superset 镜像作为基础镜像,那么您可以按如下所示添加您的覆盖设置

COPY --chown=superset superset_config.py /app/
ENV SUPERSET_CONFIG_PATH /app/superset_config.py

Docker Compose 部署使用特定约定以不同方式处理应用程序配置。有关详细信息,请参阅Docker Compose 技巧和配置

以下是在 superset_config.py 文件中可以设置的几个参数的示例

# Superset specific config
ROW_LIMIT = 5000

# Flask App Builder configuration
# Your App secret key will be used for securely signing the session cookie
# and encrypting sensitive information on the database
# Make sure you are changing this key for your deployment with a strong key.
# Alternatively you can set it with `SUPERSET_SECRET_KEY` environment variable.
# You MUST set this for production environments or the server will refuse
# to start and you will see an error in the logs accordingly.
SECRET_KEY = 'YOUR_OWN_RANDOM_GENERATED_SECRET_KEY'

# The SQLAlchemy connection string to your database backend
# This connection defines the path to the database that stores your
# superset metadata (slices, connections, tables, dashboards, ...).
# Note that the connection information to connect to the datasources
# you want to explore are managed directly in the web UI
# The check_same_thread=false property ensures the sqlite client does not attempt
# to enforce single-threaded access, which may be problematic in some edge cases
SQLALCHEMY_DATABASE_URI = 'sqlite:////path/to/superset.db?check_same_thread=false'

# Flask-WTF flag for CSRF
WTF_CSRF_ENABLED = True
# Add endpoints that need to be exempt from CSRF protection
WTF_CSRF_EXEMPT_LIST = []
# A CSRF token that expires in 1 year
WTF_CSRF_TIME_LIMIT = 60 * 60 * 24 * 365

# Set this API key to enable Mapbox visualizations
MAPBOX_API_KEY = ''
提示

请注意,通常的做法是只复制核心 superset/config.py 中您想要修改的部分,连同相关注释,粘贴到您自己的 superset_config.py 文件中。

superset/config.py 中定义的所有参数和默认值都可以在您本地的 superset_config.py 中更改。管理员应仔细阅读该文件,以了解哪些内容可以在本地配置以及当前的默认值。

由于 superset_config.py 充当 Flask 配置模块,因此它可用于更改 Flask 本身以及 Superset 捆绑的 Flask 扩展(如 flask-wtfflask-cachingflask-migrateflask-appbuilder)的设置。每个这些扩展都提供了复杂的配置能力。Superset 使用的 Web 框架 Flask App Builder 也提供了许多配置设置。有关如何配置的更多信息,请查阅 Flask App Builder 文档

至少,您需要更改 SECRET_KEYSQLALCHEMY_DATABASE_URI。请继续阅读以了解更多信息。

指定 SECRET_KEY

添加初始 SECRET_KEY

Superset 启动时需要用户指定的 SECRET_KEY。此要求在 2.1.0 版本中添加,以强制进行安全配置。在您的 superset_config.py 文件中添加一个强 SECRET_KEY,例如

SECRET_KEY = 'YOUR_OWN_RANDOM_GENERATED_SECRET_KEY'

您可以使用 openssl rand -base64 42 生成一个强安全的密钥。

使用强秘密密钥

此密钥将用于安全地签署会话 cookie 以及加密存储在 Superset 应用程序元数据数据库中的敏感信息。您的部署必须使用一个复杂且唯一的密钥。

轮换到新的 SECRET_KEY

如果您希望更改现有的 SECRET_KEY,请将现有 SECRET_KEY 添加到您的 superset_config.py 文件中,格式为 PREVIOUS_SECRET_KEY =,并提供您的新密钥,格式为 SECRET_KEY =。您可以使用以下命令找到当前的 SECRET_KEY - 如果使用 Docker 运行 Superset,请在 Superset 应用程序容器内执行

superset shell
from flask import current_app; print(current_app.config["SECRET_KEY"])

保存您的 superset_config.py 文件,包含这些值,然后运行 superset re-encrypt-secrets

设置生产元数据数据库

Superset 需要一个数据库来存储它管理的信息,例如图表、仪表板的定义以及许多其他内容。

默认情况下,Superset 配置为使用 SQLite,这是一种独立的单文件数据库,提供了一种简单快速的入门方式(无需任何安装)。然而,对于生产环境,强烈不建议使用 SQLite,原因在于安全性、可扩展性和数据完整性。重要的是仅使用受支持的数据库引擎,并考虑在单独的主机或容器上使用不同的数据库引擎。

Superset 支持以下数据库引擎/版本

数据库引擎支持的版本
PostgreSQL10.X, 11.X, 12.X, 13.X, 14.X, 15.X, 16.X
MySQL5.7, 8.X

使用以下数据库驱动程序和连接字符串

数据库PyPI 包连接字符串
PostgreSQLpip install psycopg2postgresql://<用户名>:<数据库密码>@<数据库主机>/<数据库名称>
MySQLpip install mysqlclientmysql://<用户名>:<数据库密码>@<数据库主机>/<数据库名称>
提示

正确设置元数据存储超出了本文档的范围。我们建议使用托管服务,例如 Amazon RDSGoogle Cloud Databases,以处理服务、支持基础设施和备份策略。

要配置 Superset 元数据存储,请在 superset_config 中将 SQLALCHEMY_DATABASE_URI 配置键设置为适当的连接字符串。

在 WSGI HTTP 服务器上运行

虽然您可以在 NGINX 或 Apache 上运行 Superset,但我们建议使用异步模式下的 Gunicorn。这可以实现令人印象深刻的并发性,并且安装和配置相当容易。请参阅您首选技术文档,以在您的环境中良好运行的方式设置此 Flask WSGI 应用程序。以下是一个在生产环境中已知运行良好的异步设置示例

      -w 10 \
-k gevent \
--worker-connections 1000 \
--timeout 120 \
-b 0.0.0.0:6666 \
--limit-request-line 0 \
--limit-request-field_size 0 \
--statsd-host localhost:8125 \
"superset.app:create_app()"

有关更多信息,请参阅Gunicorn 文档。*请注意,开发网络服务器(superset runflask run)不适用于生产环境。*

如果您不使用 Gunicorn,您可能需要通过在 superset_config.py 中设置 COMPRESS_REGISTER = False 来禁用 flask-compress 的使用。

目前,Google BigQuery Python SDK 与 gevent 不兼容,原因是 gevent 对 Python 核心库进行了一些动态猴子补丁。因此,当您在 Superset 上使用 BigQuery 数据源时,除了 gevent 之外,您必须使用 gunicorn 工作器类型。

HTTPS 配置

您可以通过负载均衡器或反向代理(如 nginx)配置 HTTPS 上游,并在流量到达 Superset 应用程序之前进行 SSL/TLS 卸载。在此设置中,来自 Celery 工作器的本地流量(用于为警报和报告拍摄图表快照)可以从入口点后面以 http:// URL 访问 Superset。如果您使用的是官方 Superset Docker 镜像,您还可以在 Gunicorn 中配置 SSL(Python Web 服务器)。

负载均衡器后的配置

如果您在负载均衡器或反向代理(例如 NGINX 或 AWS 上的 ELB)后面运行 Superset,您可能需要使用健康检查端点,以便您的负载均衡器知道您的 Superset 实例是否正在运行。该端点位于 /health,如果 Web 服务器正在运行,它将返回包含“OK”的 200 响应。

如果负载均衡器正在插入 X-Forwarded-For/X-Forwarded-Proto 头部,您应该在 Superset 配置文件(superset_config.py)中设置 ENABLE_PROXY_FIX = True 以提取和使用这些头部。

如果反向代理用于提供 SSL 加密,则可能需要明确定义 X-Forwarded-Proto。对于 Apache Web 服务器,可以按如下方式设置

RequestHeader set X-Forwarded-Proto "https"

配置应用程序根路径

请注意,此功能处于测试版 (BETA) 阶段。

Superset 支持在非根路径下运行应用程序。根路径前缀可以通过以下两种方式之一指定

  • SUPERSET_APP_ROOT 环境变量设置为所需的前缀。
  • 通过传递 superset_app_root 变量来自定义 Flask 入口点

请注意,前缀应以 / 开头。

自定义 Flask 入口点

要配置前缀,例如 /analytics,在通过 FLASK_APP 环境变量调用 flask run 时,将 superset_app_root 参数传递给 create_app

FLASK_APP="superset:create_app(superset_app_root='/analytics')"

或作为 flask run 命令的 --app 参数的一部分

flask --app "superset.app:create_app(superset_app_root='/analytics')"

Docker 构建

Docker Compose 开发人员配置包含一个额外的环境变量 SUPERSET_APP_ROOT,以简化在各个服务中设置非默认根路径的过程。

docker/.env-local 中将 SUPERSET_APP_ROOT 设置为所需前缀,然后使用 docker compose up --detach 启动服务。

自定义 OAuth2 配置

Superset 基于 Flask-AppBuilder (FAB) 构建,它开箱即用地支持许多提供商(GitHub、Twitter、LinkedIn、Google、Azure 等)。除此之外,Superset 还可以配置为连接支持“代码”授权的其他 OAuth2 授权服务器实现。

请确保在 Web 服务器上安装了 pip 包 Authlib

首先,在 Superset 的 superset_config.py 中配置授权。

from flask_appbuilder.security.manager import AUTH_OAUTH

# Set the authentication type to OAuth
AUTH_TYPE = AUTH_OAUTH

OAUTH_PROVIDERS = [
{ 'name':'egaSSO',
'token_key':'access_token', # Name of the token in the response of access_token_url
'icon':'fa-address-card', # Icon for the provider
'remote_app': {
'client_id':'myClientId', # Client Id (Identify Superset application)
'client_secret':'MySecret', # Secret for this Client Id (Identify Superset application)
'client_kwargs':{
'scope': 'read' # Scope for the Authorization
},
'access_token_method':'POST', # HTTP Method to call access_token_url
'access_token_params':{ # Additional parameters for calls to access_token_url
'client_id':'myClientId'
},
'jwks_uri':'https://myAuthorizationServe/adfs/discovery/keys', # may be required to generate token
'access_token_headers':{ # Additional headers for calls to access_token_url
'Authorization': 'Basic Base64EncodedClientIdAndSecret'
},
'api_base_url':'https://myAuthorizationServer/oauth2AuthorizationServer/',
'access_token_url':'https://myAuthorizationServer/oauth2AuthorizationServer/token',
'authorize_url':'https://myAuthorizationServer/oauth2AuthorizationServer/authorize'
}
}
]

# Will allow user self registration, allowing to create Flask users from Authorized User
AUTH_USER_REGISTRATION = True

# The default user self registration role
AUTH_USER_REGISTRATION_ROLE = "Public"

如果您想在新用户注册时分配 Admin 角色,可以按如下方式分配

AUTH_USER_REGISTRATION_ROLE = "Admin"

如果您遇到无法从 Superset 主页设置中列出用户的问题,即使新注册的用户具有 Admin 角色,请重新运行 superset init 以同步所需权限。以下是使用 Docker Compose 重新运行 superset init 的命令。

docker-compose exec superset superset init

然后,创建一个 CustomSsoSecurityManager,它扩展 SupersetSecurityManager 并覆盖 oauth_user_info

import logging
from superset.security import SupersetSecurityManager

class CustomSsoSecurityManager(SupersetSecurityManager):

def oauth_user_info(self, provider, response=None):
logging.debug("Oauth2 provider: {0}.".format(provider))
if provider == 'egaSSO':
# As example, this line request a GET to base_url + '/' + userDetails with Bearer Authentication,
# and expects that authorization server checks the token, and response with user details
me = self.appbuilder.sm.oauth_remotes[provider].get('userDetails').data
logging.debug("user_data: {0}".format(me))
return { 'name' : me['name'], 'email' : me['email'], 'id' : me['user_name'], 'username' : me['user_name'], 'first_name':'', 'last_name':''}
...

此文件必须与 superset_config.py 位于同一目录中,文件名为 custom_sso_security_manager.py。最后,将以下 2 行添加到 superset_config.py

from custom_sso_security_manager import CustomSsoSecurityManager
CUSTOM_SECURITY_MANAGER = CustomSsoSecurityManager

备注

  • 如果需要配置 OAuth2 授权提供商,重定向 URL 将是 https://<superset-webserver>/oauth-authorized/<provider-name>。例如,对于上述配置,重定向 URL 将是 https://<superset-webserver>/oauth-authorized/egaSSO

  • 如果 OAuth2 授权服务器支持 OpenID Connect 1.0,您只需配置其配置文档 URL,而无需提供 api_base_urlaccess_token_urlauthorize_url 以及用户 info endpoint、jwks uri 等其他必需选项。例如

    OAUTH_PROVIDERS = [
    { 'name':'egaSSO',
    'token_key':'access_token', # Name of the token in the response of access_token_url
    'icon':'fa-address-card', # Icon for the provider
    'remote_app': {
    'client_id':'myClientId', # Client Id (Identify Superset application)
    'client_secret':'MySecret', # Secret for this Client Id (Identify Superset application)
    'server_metadata_url': 'https://myAuthorizationServer/.well-known/openid-configuration'
    }
    }
    ]

使用 Flask-OIDC 的 Keycloak 特有配置

如果您使用 Keycloak 作为 OpenID Connect 1.0 提供商,基于 Authlib 的上述配置可能无法工作。在这种情况下,使用 Flask-OIDC 是一个可行的选择。

请确保在 Web 服务器上安装了 pip 包 Flask-OIDC。该包已成功通过 2.2.0 版本测试。此包需要 Flask-OpenID 作为依赖项。

以下代码定义了一个新的安全管理器。将其添加到名为 keycloak_security_manager.py 的新文件中,该文件应与您的 superset_config.py 文件位于同一目录中。

from flask_appbuilder.security.manager import AUTH_OID
from superset.security import SupersetSecurityManager
from flask_oidc import OpenIDConnect
from flask_appbuilder.security.views import AuthOIDView
from flask_login import login_user
from urllib.parse import quote
from flask_appbuilder.views import ModelView, SimpleFormView, expose
from flask import (
redirect,
request
)
import logging

class OIDCSecurityManager(SupersetSecurityManager):

def __init__(self, appbuilder):
super(OIDCSecurityManager, self).__init__(appbuilder)
if self.auth_type == AUTH_OID:
self.oid = OpenIDConnect(self.appbuilder.get_app)
self.authoidview = AuthOIDCView

class AuthOIDCView(AuthOIDView):

@expose('/login/', methods=['GET', 'POST'])
def login(self, flag=True):
sm = self.appbuilder.sm
oidc = sm.oid

@self.appbuilder.sm.oid.require_login
def handle_login():
user = sm.auth_user_oid(oidc.user_getfield('email'))

if user is None:
info = oidc.user_getinfo(['preferred_username', 'given_name', 'family_name', 'email'])
user = sm.add_user(info.get('preferred_username'), info.get('given_name'), info.get('family_name'),
info.get('email'), sm.find_role('Gamma'))

login_user(user, remember=False)
return redirect(self.appbuilder.get_url_for_index)

return handle_login()

@expose('/logout/', methods=['GET', 'POST'])
def logout(self):
oidc = self.appbuilder.sm.oid

oidc.logout()
super(AuthOIDCView, self).logout()
redirect_url = request.url_root.strip('/') + self.appbuilder.get_url_for_login

return redirect(
oidc.client_secrets.get('issuer') + '/protocol/openid-connect/logout?redirect_uri=' + quote(redirect_url))

然后添加到您的 superset_config.py 文件中

from keycloak_security_manager import OIDCSecurityManager
from flask_appbuilder.security.manager import AUTH_OID, AUTH_REMOTE_USER, AUTH_DB, AUTH_LDAP, AUTH_OAUTH
import os

AUTH_TYPE = AUTH_OID
SECRET_KEY: 'SomethingNotEntirelySecret'
OIDC_CLIENT_SECRETS = '/path/to/client_secret.json'
OIDC_ID_TOKEN_COOKIE_SECURE = False
OIDC_OPENID_REALM: '<myRealm>'
OIDC_INTROSPECTION_AUTH_METHOD: 'client_secret_post'
CUSTOM_SECURITY_MANAGER = OIDCSecurityManager

# Will allow user self registration, allowing to create Flask users from Authorized User
AUTH_USER_REGISTRATION = True

# The default user self registration role
AUTH_USER_REGISTRATION_ROLE = 'Public'

将您的客户端特定的 OpenID 信息存储在一个名为 client_secret.json 的文件中。在与 superset_config.py 相同的目录中创建此文件。

{
"<myOpenIDProvider>": {
"issuer": "https://<myKeycloakDomain>/realms/<myRealm>",
"auth_uri": "https://<myKeycloakDomain>/realms/<myRealm>/protocol/openid-connect/auth",
"client_id": "https://<myKeycloakDomain>",
"client_secret": "<myClientSecret>",
"redirect_uris": [
"https://<SupersetWebserver>/oauth-authorized/<myOpenIDProvider>"
],
"userinfo_uri": "https://<myKeycloakDomain>/realms/<myRealm>/protocol/openid-connect/userinfo",
"token_uri": "https://<myKeycloakDomain>/realms/<myRealm>/protocol/openid-connect/token",
"token_introspection_uri": "https://<myKeycloakDomain>/realms/<myRealm>/protocol/openid-connect/token/introspect"
}
}

LDAP 认证

FAB 支持针对 LDAP 服务器验证用户凭据。要使用 LDAP,您必须安装 python-ldap 包。有关详细信息,请参阅 FAB 的 LDAP 文档

将 LDAP 或 OAUTH 组映射到 Superset 角色

Flask-AppBuilder 中的 AUTH_ROLES_MAPPING 是一个字典,用于将 LDAP/OAUTH 组名映射到 FAB 角色。它用于为使用 LDAP 或 OAuth 进行身份验证的用户分配角色。

将 OAUTH 组映射到 Superset 角色

以下 AUTH_ROLES_MAPPING 字典会将 OAUTH 组“superset_users”映射到 Superset 角色“Gamma”和“Alpha”,并将 OAUTH 组“superset_admins”映射到 Superset 角色“Admin”。

AUTH_ROLES_MAPPING = {
"superset_users": ["Gamma","Alpha"],
"superset_admins": ["Admin"],
}

将 LDAP 组映射到 Superset 角色

以下 AUTH_ROLES_MAPPING 字典会将 LDAP DN “cn=superset_users,ou=groups,dc=example,dc=com”映射到 Superset 角色“Gamma”和“Alpha”,并将 LDAP DN “cn=superset_admins,ou=groups,dc=example,dc=com”映射到 Superset 角色“Admin”。

AUTH_ROLES_MAPPING = {
"cn=superset_users,ou=groups,dc=example,dc=com": ["Gamma","Alpha"],
"cn=superset_admins,ou=groups,dc=example,dc=com": ["Admin"],
}

注意:这需要设置 AUTH_LDAP_SEARCH。有关更多详细信息,请参阅 FAB 安全文档

登录时同步角色

您还可以使用 AUTH_ROLES_SYNC_AT_LOGIN 配置变量来控制 Flask-AppBuilder 同步用户角色与 LDAP/OAUTH 组的频率。如果 AUTH_ROLES_SYNC_AT_LOGIN 设置为 True,Flask-AppBuilder 将在用户每次登录时同步其角色。如果 AUTH_ROLES_SYNC_AT_LOGIN 设置为 False,Flask-AppBuilder 只会在用户首次注册时同步其角色。

Flask 应用程序配置钩子

FLASK_APP_MUTATOR 是一个可以在您的环境中提供的配置函数,它接收 app 对象并可以以任何方式对其进行修改。例如,将 FLASK_APP_MUTATOR 添加到您的 superset_config.py 中以将会话 cookie 过期时间设置为 24 小时

from flask import session
from flask import Flask


def make_session_permanent():
'''
Enable maxAge for the cookie 'session'
'''
session.permanent = True

# Set up max age of session to 24 hours
PERMANENT_SESSION_LIFETIME = timedelta(hours=24)
def FLASK_APP_MUTATOR(app: Flask) -> None:
app.before_request_funcs.setdefault(None, []).append(make_session_permanent)

功能标志

为支持不同类型的用户,Superset 默认禁用了一些功能。例如,一些用户有更强的安全限制,而另一些用户则可能没有。因此,Superset 允许用户通过配置来启用或禁用某些功能。对于功能所有者,您可以在 Superset 中添加可选功能,但这些功能只会影响一部分用户。

您可以通过 superset_config.py 中的标志来启用或禁用功能

FEATURE_FLAGS = {
'PRESTO_EXPAND_DATA': False,
}

当前的功能标志列表可以在 RESOURCES/FEATURE_FLAGS.md 中找到。