aminghadersohi commented on code in PR #40356: URL: https://github.com/apache/superset/pull/40356#discussion_r3328066171
########## 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.", + ) Review Comment: Fixed. Added `max_length=255` to the `name` field in both `CreateRLSFilterRequest` and `UpdateRLSFilterRequest`. Overlong names now fail Pydantic validation before reaching the database layer. ########## superset/mcp_service/rls/tool/update_rls_filter.py: ########## @@ -0,0 +1,147 @@ +# 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 + +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 ( + CreateRLSFilterResponse, + UpdateRLSFilterRequest, +) + +logger = logging.getLogger(__name__) + + +def _build_update_properties( + request: UpdateRLSFilterRequest, existing: Any +) -> dict[str, object]: + """Build the properties dict for UpdateRLSRuleCommand from a partial request. + + Omitted fields default to their current values from ``existing`` so the + caller only needs to specify what changed. + """ + set_fields = request.model_fields_set + properties: dict[str, object] = {} + + for field in ("name", "filter_type", "clause"): + value = getattr(request, field) + if field in set_fields and value is not None: + properties[field] = value Review Comment: Fixed. Added a `@model_validator(mode="after")` to `UpdateRLSFilterRequest` that raises a `ValidationError` when any of `name`, `filter_type`, or `clause` are explicitly set to `null`. This matches the REST API behavior (`allow_none=False`) and turns what was a silent no-op into a clear validation error. ########## superset/mcp_service/rls/tool/update_rls_filter.py: ########## @@ -0,0 +1,147 @@ +# 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 + +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 ( + CreateRLSFilterResponse, + UpdateRLSFilterRequest, +) + +logger = logging.getLogger(__name__) + + +def _build_update_properties( + request: UpdateRLSFilterRequest, existing: Any +) -> dict[str, object]: + """Build the properties dict for UpdateRLSRuleCommand from a partial request. + + Omitted fields default to their current values from ``existing`` so the + caller only needs to specify what changed. + """ + set_fields = request.model_fields_set + properties: dict[str, object] = {} + + for field in ("name", "filter_type", "clause"): + value = getattr(request, field) + if field in set_fields and value is not None: + properties[field] = value + + for field in ("group_key", "description"): + if field in set_fields: + properties[field] = getattr(request, field) + + if "tables" in set_fields and request.tables is not None: + properties["tables"] = request.tables + else: + properties["tables"] = [t.id for t in getattr(existing, "tables", [])] + + if "roles" in set_fields and request.roles is not None: + properties["roles"] = request.roles + else: + properties["roles"] = [r.id for r in getattr(existing, "roles", [])] Review Comment: Fixed. The same `@model_validator` added for thread 9 also covers `tables` and `roles` — explicitly passing `null` for either field now raises a `ValidationError` with a message directing callers to omit the field to keep existing associations, or pass `[]` to clear them. -- 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]
