Repository navigation
fix(charts): support saved_query datasource in chart create/update #43499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
ba7e005
f14142d
b73caca
a45ee1b
0053e08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,9 @@ | |
| from flask import g | ||
| from marshmallow import ValidationError | ||
|
|
||
| from superset import security_manager | ||
| from superset.commands.chart.exceptions import ( | ||
| ChartForbiddenError, | ||
| ChartQueryContextDatasourceMismatchValidationError, | ||
| ) | ||
| from superset.commands.exceptions import DatasourceTypeInvalidError | ||
|
|
@@ -36,18 +38,56 @@ | |
| def validate_chart_datasource_type(datasource_type: str) -> None: | ||
| """Accept persistent chart sources with names and type-aware access policies. | ||
|
|
||
| SQL Lab queries are registered datasource types but are not persistent | ||
| chart sources; accepting all DatasourceDAO registrations would include them. | ||
| A raw SQL Lab ``query`` is a registered datasource type but not a | ||
| persistent chart source, so it's excluded here; accepting all | ||
| DatasourceDAO registrations would include it. A ``saved_query`` is | ||
| explicitly persisted and named by the user, so it's accepted alongside | ||
| ``table``/``semantic_view``. | ||
| """ | ||
| if ( | ||
| datasource_type == DatasourceType.SEMANTIC_VIEW | ||
| and not is_semantic_layers_enabled() | ||
| ): | ||
| raise ValidationError(SemanticLayersDisabledError.message) | ||
| if datasource_type not in (DatasourceType.TABLE, DatasourceType.SEMANTIC_VIEW): | ||
| if datasource_type not in ( | ||
| DatasourceType.TABLE, | ||
| DatasourceType.SEMANTIC_VIEW, | ||
| DatasourceType.SAVEDQUERY, | ||
|
rusackas marked this conversation as resolved.
Resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once creation works, a non-editor whose only grant is
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one's legit, but it's a visibility gap, not an access-escalation one: a database-only grant just means these charts won't surface yet, not that the wrong people can see them. Feels like a reasonable follow-up (mirroring table_backed_slice_join for SavedQuery) rather than a blocker, happy to be told otherwise.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed it is a visibility gap rather than an escalation, but the same user would see create succeed and then get a 404 on the chart they just made, which this PR introduces. Could the follow-up issue be linked here, or
rusackas marked this conversation as resolved.
Resolved
rusackas marked this conversation as resolved.
Resolved
|
||
| ): | ||
|
rusackas marked this conversation as resolved.
Resolved
|
||
| raise DatasourceTypeInvalidError() | ||
|
|
||
|
|
||
| def raise_for_saved_query_ownership(datasource: Any) -> None: | ||
| """ | ||
| Refuse a saved query the caller could not read through the saved-query API. | ||
|
|
||
| ``raise_for_access`` accepts a schema grant without looking at the creator, | ||
| so without this check a user could bind a chart to another user's private | ||
| saved query. Mirrors ``SavedQueryFilter``: only the creator, or a user with | ||
| ``all_query_access``, may reference a saved query. | ||
| """ | ||
| if getattr(datasource, "type", None) != DatasourceType.SAVEDQUERY: | ||
| return | ||
| if security_manager.can_access_all_queries(): | ||
| return | ||
| if getattr(datasource, "created_by", None) != g.user: | ||
| raise ChartForbiddenError() | ||
|
|
||
|
|
||
| def raise_for_chart_datasource_access(datasource: Any) -> None: | ||
| """ | ||
| Authorize binding a chart to ``datasource``. | ||
|
|
||
| A saved query is authored through an API that does not validate its | ||
| database, so editorship of one (being its creator) must not stand in for a | ||
| database, catalog, or schema grant: require the real grant. | ||
| """ | ||
| if getattr(datasource, "type", None) == DatasourceType.SAVEDQUERY: | ||
| security_manager.raise_for_access(datasource=datasource, allow_editorship=False) | ||
| return | ||
| security_manager.raise_for_access(datasource=datasource) | ||
|
|
||
|
|
||
| def validate_query_context_datasource( | ||
| raw_query_context: Any, | ||
| expected_datasource_id: Any, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ | |
|
|
||
| from flask_appbuilder.models.sqla import Model | ||
|
|
||
| from superset import db | ||
| from superset import db, security_manager | ||
| from superset.commands.base import BaseCommand | ||
| from superset.commands.database.exceptions import ( | ||
| DatabaseConnectionFailedError, | ||
|
|
@@ -272,6 +272,13 @@ def _update_catalog_attribute( | |
| update = {"catalog": new_catalog} | ||
| db.session.query(model).filter_by(**predicate).update(update) | ||
|
|
||
| # The bulk update above bypasses ``saved_query_before_update``, so | ||
| # re-copy the saved queries' perm strings onto their dependent charts. | ||
| if database := db.session.query(Database).get(database_id): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothing exercises this resync: |
||
| security_manager.sync_database_saved_query_chart_perms( | ||
| db.session.connection(), database | ||
| ) | ||
|
|
||
| def validate(self) -> None: | ||
| if database_name := self._properties.get("database_name"): | ||
| if not DatabaseDAO.validate_update_uniqueness( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # 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. | ||
| """allow saved_query chart datasource | ||
|
|
||
| Revision ID: dfa683969f48 | ||
| Revises: f72b6e9c104a | ||
| Create Date: 2026-10-13 00:00:00.000000 | ||
|
|
||
| """ | ||
|
|
||
| import logging | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
| from sqlalchemy.orm import declarative_base | ||
|
|
||
| from superset import db | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "dfa683969f48" | ||
| down_revision = "f72b6e9c104a" | ||
|
|
||
| Base = declarative_base() | ||
|
|
||
| logger = logging.getLogger("alembic.env") | ||
|
|
||
|
|
||
| class Slice(Base): # type: ignore | ||
| __tablename__ = "slices" | ||
|
|
||
| id = sa.Column(sa.Integer, primary_key=True) | ||
| datasource_type = sa.Column(sa.String(200)) | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| # Update chart datasource constraint to allow saved_query, matching | ||
| # CreateChartCommand/UpdateChartCommand.validate_chart_datasource_type | ||
| # (apache/superset#29697), which now accepts a saved_query as a valid, | ||
| # persistent chart datasource alongside table and semantic_view. | ||
| with op.batch_alter_table("slices") as batch_op: | ||
| batch_op.drop_constraint("ck_chart_datasource", type_="check") | ||
| batch_op.create_check_constraint( | ||
| "ck_chart_datasource", | ||
| "datasource_type in ('table', 'semantic_view', 'saved_query')", | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| # Any chart persisted with datasource_type == saved_query would violate | ||
| # the stricter constraint being restored below, so reassign those rows | ||
| # first (mirrors the cleanup in 7e67aecbf3f1_chart_ds_constraint.py). | ||
| bind = op.get_bind() | ||
| session = db.Session(bind=bind) | ||
| for slc in session.query(Slice).filter(Slice.datasource_type == "saved_query"): | ||
| logger.warning( | ||
| "downgrading slice.id = %s from datasource_type saved_query to table", | ||
| slc.id, | ||
| ) | ||
| slc.datasource_type = "table" | ||
| session.add(slc) | ||
|
rusackas marked this conversation as resolved.
Resolved
|
||
| session.commit() | ||
| session.close() | ||
|
|
||
| with op.batch_alter_table("slices") as batch_op: | ||
| batch_op.drop_constraint("ck_chart_datasource", type_="check") | ||
| batch_op.create_check_constraint( | ||
|
rusackas marked this conversation as resolved.
Resolved
|
||
| "ck_chart_datasource", | ||
| "datasource_type in ('table', 'semantic_view')", | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Charts created with this newly accepted type cannot round-trip through export/import:
Slice.tableisNone, so chart export omitsdataset_uuid, butImportV1ChartSchemarequires it. Could the saved-query export/import contract be implemented before allowing these charts to be persisted?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The importer only resolves a chart's datasource via
dataset_uuid(anything that doesnt match an imported dataset gets skipped outright), so saved_query round-tripping needs its own importer path, not just a schema tweak. Same scope call as the ExploreMixin ask, bigger than #29697's create/update fix.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That importer limitation means a chart newly accepted here produces an export that cannot be reimported, so the create/update fix introduces a persisted compatibility failure. If saved-query import support is outside this change, could this type keep returning a controlled validation error until that contract is implemented?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get the concern, but I don't think a chart that can't execute yet should also be blocked from being created. Export/import support feels like its own follow-up, not a blocker here, same call as the ExploreMixin thread above. Fair thing to flag either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Charts of this type become creatable as soon as this merges, and each one exports without
dataset_uuid, so the bundle failsImportV1ChartSchemaon reimport. Is there a tracked issue for the importer path, and could export at least skip or flag these charts so users are not handed a bundle that cannot be imported?