aminghadersohi commented on code in PR #40356: URL: https://github.com/apache/superset/pull/40356#discussion_r3326700444
########## superset/mcp_service/rls/schemas.py: ########## @@ -0,0 +1,138 @@ +# 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. + +""" +Pydantic schemas for RLS (row-level security) MCP tools. +""" + +from __future__ import annotations + +from typing import Literal + +from pydantic import BaseModel, ConfigDict, Field + + +class CreateRLSFilterRequest(BaseModel): + model_config = ConfigDict(populate_by_name=True) + + name: str = Field(..., min_length=1, description="Name for the RLS filter rule.") + filter_type: Literal["Regular", "Base"] = Field( + ..., + description=( + 'Type of filter. "Regular" hides rows from the specified roles ' + 'unless the clause matches. "Base" shows only rows where the ' + "clause matches to the specified roles." + ), + ) + tables: list[int] = Field( + ..., + min_length=1, + description=( + "List of table IDs this filter applies to. Use list_datasets to find IDs." + ), + ) + roles: list[int] = Field( + ..., + description="List of role IDs that see this filter applied.", + ) Review Comment: Fixed. The `roles` field description now clarifies the semantics per filter type: for Regular filters these roles are restricted; for Base filters these roles are exempt from the filter. ########## superset/mcp_service/rls/tool/create_rls_filter.py: ########## @@ -0,0 +1,131 @@ +# 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 fastmcp import Context +from superset_core.mcp.decorators import tool, ToolAnnotations + +from superset.extensions import event_logger +from superset.mcp_service.rls.schemas import ( + CreateRLSFilterRequest, + CreateRLSFilterResponse, +) + +logger = logging.getLogger(__name__) + + +@tool( + tags=["mutate"], + class_permission_name="Row Level Security", + method_permission_name="write", + annotations=ToolAnnotations( + title="Create RLS filter", + readOnlyHint=False, + destructiveHint=False, + ), +) +async def create_rls_filter( + request: CreateRLSFilterRequest, ctx: Context +) -> CreateRLSFilterResponse: + """Create a row-level security (RLS) filter rule. + + RLS filters restrict which rows users can see based on their role membership. + Use this to enforce data access policies at the row level. + + Filter types: + - "Regular": Hides rows from the specified roles unless the clause matches. + - "Base": Shows only rows where the clause matches to the specified roles. + + Workflow: + 1. Use list_datasets to find the table IDs to protect. + 2. Use find_users to locate role IDs to apply the filter to. + 3. Call this tool with the SQL WHERE clause to enforce. Review Comment: Fixed. The docstring now correctly describes Base filters (applies to ALL users EXCEPT those with the specified roles), and the misleading `find_users` workflow step has been replaced with a reference to the Superset Roles admin page. ########## superset/mcp_service/rls/schemas.py: ########## @@ -0,0 +1,138 @@ +# 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. + +""" +Pydantic schemas for RLS (row-level security) MCP tools. +""" + +from __future__ import annotations + +from typing import Literal + +from pydantic import BaseModel, ConfigDict, Field + + +class CreateRLSFilterRequest(BaseModel): + model_config = ConfigDict(populate_by_name=True) + + name: str = Field(..., min_length=1, description="Name for the RLS filter rule.") + filter_type: Literal["Regular", "Base"] = Field( + ..., + description=( + 'Type of filter. "Regular" hides rows from the specified roles ' + 'unless the clause matches. "Base" shows only rows where the ' + "clause matches to the specified roles." + ), + ) + tables: list[int] = Field( + ..., + min_length=1, + description=( + "List of table IDs this filter applies to. Use list_datasets to find IDs." + ), + ) + roles: list[int] = Field( + ..., + description="List of role IDs that see this filter applied.", + ) + clause: str = Field( + ..., + min_length=1, + description=( + "SQL WHERE clause applied to matching tables (e.g. \"region = 'EMEA'\")." + ), + ) + group_key: str | None = Field( + None, + description="Optional group key for grouping related filters.", + ) + description: str | None = Field( + None, + description="Optional human-readable description of the filter.", + ) + + +class CreateRLSFilterResponse(BaseModel): + id: int | None = Field( + None, + description="RLS filter ID. None if the operation failed.", + ) + name: str | None = Field(None, description="Name of the RLS filter.") + filter_type: str | None = Field(None, description="Filter type: Regular or Base.") + clause: str | None = Field(None, description="SQL WHERE clause of the filter.") + tables: list[int] = Field( + default_factory=list, + description="Table IDs this filter applies to.", + ) + roles: list[int] = Field( + default_factory=list, + description="Role IDs affected by this filter.", + ) + group_key: str | None = Field(None, description="Group key for the filter.") + description: str | None = Field(None, description="Description of the filter.") + error: str | None = Field( + None, + description="Error message if the operation failed, otherwise null.", + ) + + +class UpdateRLSFilterRequest(BaseModel): + model_config = ConfigDict(populate_by_name=True) + + id: int = Field(..., description="ID of the RLS filter rule to update.") + name: str | None = Field( + None, + min_length=1, + description="New name for the RLS filter rule. Omit to keep existing.", + ) + filter_type: Literal["Regular", "Base"] | None = Field( + None, + description=( + "New filter type. Omit to keep existing. " + '"Regular" hides rows from the specified roles unless the clause matches. ' + '"Base" shows only rows where the clause matches to the specified roles.' + ), + ) Review Comment: Fixed. The `filter_type` description in `UpdateRLSFilterRequest` now reflects the correct semantics for both Regular and Base filters. ########## superset/mcp_service/rls/schemas.py: ########## @@ -0,0 +1,138 @@ +# 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. + +""" +Pydantic schemas for RLS (row-level security) MCP tools. +""" + +from __future__ import annotations + +from typing import Literal + +from pydantic import BaseModel, ConfigDict, Field + + +class CreateRLSFilterRequest(BaseModel): + model_config = ConfigDict(populate_by_name=True) + + name: str = Field(..., min_length=1, description="Name for the RLS filter rule.") + filter_type: Literal["Regular", "Base"] = Field( + ..., + description=( + 'Type of filter. "Regular" hides rows from the specified roles ' + 'unless the clause matches. "Base" shows only rows where the ' + "clause matches to the specified roles." + ), + ) + tables: list[int] = Field( + ..., + min_length=1, + description=( + "List of table IDs this filter applies to. Use list_datasets to find IDs." + ), + ) + roles: list[int] = Field( + ..., + description="List of role IDs that see this filter applied.", + ) + clause: str = Field( + ..., + min_length=1, + description=( + "SQL WHERE clause applied to matching tables (e.g. \"region = 'EMEA'\")." + ), + ) + group_key: str | None = Field( + None, + description="Optional group key for grouping related filters.", + ) + description: str | None = Field( + None, + description="Optional human-readable description of the filter.", + ) + + +class CreateRLSFilterResponse(BaseModel): + id: int | None = Field( + None, + description="RLS filter ID. None if the operation failed.", + ) + name: str | None = Field(None, description="Name of the RLS filter.") + filter_type: str | None = Field(None, description="Filter type: Regular or Base.") + clause: str | None = Field(None, description="SQL WHERE clause of the filter.") + tables: list[int] = Field( + default_factory=list, + description="Table IDs this filter applies to.", + ) + roles: list[int] = Field( + default_factory=list, + description="Role IDs affected by this filter.", + ) + group_key: str | None = Field(None, description="Group key for the filter.") + description: str | None = Field(None, description="Description of the filter.") + error: str | None = Field( + None, + description="Error message if the operation failed, otherwise null.", + ) + + +class UpdateRLSFilterRequest(BaseModel): + model_config = ConfigDict(populate_by_name=True) + + id: int = Field(..., description="ID of the RLS filter rule to update.") + name: str | None = Field( + None, + min_length=1, + description="New name for the RLS filter rule. Omit to keep existing.", + ) + filter_type: Literal["Regular", "Base"] | None = Field( + None, + description=( + "New filter type. Omit to keep existing. " + '"Regular" hides rows from the specified roles unless the clause matches. ' + '"Base" shows only rows where the clause matches to the specified roles.' + ), + ) + tables: list[int] | None = Field( + None, + description=( + "New list of table IDs this filter applies to. " + "Omit to keep existing. Pass [] to remove all tables." + ), + ) + roles: list[int] | None = Field( + None, + description=( + "New list of role IDs that see this filter applied. " + "Omit to keep existing. Pass [] to remove all roles." + ), + ) Review Comment: Fixed. The `roles` description in `UpdateRLSFilterRequest` now clarifies that for Base filters the listed roles are exempt from the filter. ########## superset/mcp_service/rls/schemas.py: ########## @@ -0,0 +1,138 @@ +# 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. + +""" +Pydantic schemas for RLS (row-level security) MCP tools. +""" + +from __future__ import annotations + +from typing import Literal + +from pydantic import BaseModel, ConfigDict, Field + + +class CreateRLSFilterRequest(BaseModel): + model_config = ConfigDict(populate_by_name=True) + + name: str = Field(..., min_length=1, description="Name for the RLS filter rule.") + filter_type: Literal["Regular", "Base"] = Field( + ..., + description=( + 'Type of filter. "Regular" hides rows from the specified roles ' + 'unless the clause matches. "Base" shows only rows where the ' + "clause matches to the specified roles." + ), + ) + tables: list[int] = Field( + ..., + min_length=1, + description=( + "List of table IDs this filter applies to. Use list_datasets to find IDs." + ), + ) + roles: list[int] = Field( + ..., + description="List of role IDs that see this filter applied.", + ) + clause: str = Field( + ..., + min_length=1, + description=( + "SQL WHERE clause applied to matching tables (e.g. \"region = 'EMEA'\")." + ), + ) + group_key: str | None = Field( + None, + description="Optional group key for grouping related filters.", + ) + description: str | None = Field( + None, + description="Optional human-readable description of the filter.", + ) + + +class CreateRLSFilterResponse(BaseModel): + id: int | None = Field( + None, + description="RLS filter ID. None if the operation failed.", + ) + name: str | None = Field(None, description="Name of the RLS filter.") + filter_type: str | None = Field(None, description="Filter type: Regular or Base.") + clause: str | None = Field(None, description="SQL WHERE clause of the filter.") Review Comment: Fixed. Renamed `CreateRLSFilterResponse` to `RLSFilterResponse` since it is the shared response type for both create and update operations. ########## superset/mcp_service/app.py: ########## @@ -656,6 +656,10 @@ def create_mcp_app( from superset.mcp_service.explore.tool import ( # noqa: F401, E402 generate_explore_link, ) +from superset.mcp_service.rls.tool import ( # noqa: F401, E402 + create_rls_filter, + update_rls_filter, +) Review Comment: Added. `tests/unit_tests/mcp_service/rls/tool/test_create_rls_filter.py` and `test_update_rls_filter.py` now cover: success path, role-not-found, table-not-found, rule-not-found (update only), partial update (update only), and Base filter type. -- 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]
