dpgaspar commented on code in PR #33976: URL: https://github.com/apache/superset/pull/33976#discussion_r2225792592
########## superset/mcp_service/auth.py: ########## @@ -0,0 +1,96 @@ +# 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 typing import Any, Optional + +logger = logging.getLogger(__name__) + + +def get_user_from_request() -> Any: + """ + Extract user info from the request context (e.g., from Bearer token, headers, etc.). + By default, returns admin user. Override for OIDC/OAuth/Okta integration. + """ + from flask import current_app + + from superset.extensions import security_manager + + admin_username = current_app.config.get("MCP_ADMIN_USERNAME", "admin") + return security_manager.get_user_by_username(admin_username) + + +def impersonate_user(user: Any, run_as: Optional[str] = None) -> Any: + """ + Optionally impersonate another user if allowed. By default, returns the same user. + Override to enforce impersonation rules. + """ + return user + + +def has_permission(user: Any, tool_func: Any) -> bool: + """ + Check if the user has permission to run the tool. By default, always True. + Override for RBAC. + """ + return True + + +def log_access(user: Any, tool_name: str, args: Any, kwargs: Any) -> None: + """ + Log access/action for observability/audit. By default, does nothing. + Override to log to your system. + """ + pass + + +def mcp_auth_hook(tool_func: Any) -> Any: + """ + Decorator for MCP tool functions to enforce auth, impersonation, RBAC, and logging. + Also sets up Flask user context (g.user) for downstream DAO/model code. + All logic is overridable for enterprise integration. + """ + import functools + + from flask import current_app, g + from flask_login import AnonymousUserMixin + + from superset.extensions import security_manager + + @functools.wraps(tool_func) + def wrapper(*args: Any, **kwargs: Any) -> Any: + # --- Setup user context (was _setup_user_context) --- + admin_username = current_app.config.get("MCP_ADMIN_USERNAME", "admin") + admin_user = security_manager.get_user_by_username(admin_username) Review Comment: To my understanding, by default full admin is given as a `sudoer` also, no auth process is inplace, for example JWT verification, using JWT claims to get user info. Can you give an example on how can this be implemented? ########## superset/mcp_service/mcp_app.py: ########## @@ -0,0 +1,90 @@ +# 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. + +""" +FastMCP app instance and initialization for Superset MCP service. +This file provides the global FastMCP instance (mcp) and a function to initialize +middleware. All tool modules should import mcp from here and use @mcp.tool and +@mcp_auth_hook decorators. +""" + +import logging + +from fastmcp import FastMCP + +from superset.mcp_service.middleware import LoggingMiddleware, PrivateToolMiddleware + +mcp = FastMCP( + "Superset MCP Server", + instructions=""" Review Comment: how can we hook auth for example: `BearerAuthProvider` ? is it possible to use `add_middleware` later on `init_fastmcp_server`? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
