Skip to content
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

fix: table schema permissions #23356

Merged
merged 7 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion superset/security/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1823,8 +1823,14 @@ def raise_for_access(
return

if query:
# if no schema is specified, use the database default; in the future we
# should pass the query schema to the SQLAlchemy URI so that it's set as
# the default one.
with database.get_sqla_engine_with_context() as engine:
betodealmeida marked this conversation as resolved.
Show resolved Hide resolved
default_schema = inspect(engine).default_schema_name

tables = {
Table(table_.table, table_.schema or query.schema)
Table(table_.table, table_.schema or default_schema)
for table_ in sql_parse.ParsedQuery(query.sql).tables
}
elif table:
Expand Down
18 changes: 9 additions & 9 deletions tests/integration_tests/sqllab_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,15 @@ def test_sql_json_schema_access(self):
)
self.assertEqual(1, len(data["data"]))

# postgres needs a schema as a part of the table name.
if db_backend == "mysql":
data = self.run_sql(
"SELECT * FROM test_table",
"5",
username="SchemaUser",
schema=CTAS_SCHEMA_NAME,
)
self.assertEqual(1, len(data["data"]))
# without a schema the query will fail, since it uses the default schema instead
# of CTAS_SCHEMA_NAME
data = self.run_sql(
"SELECT * FROM test_table",
"5",
username="SchemaUser",
schema=CTAS_SCHEMA_NAME,
)
self.assertTrue("errors" in data)

db.session.query(Query).delete()
with get_example_database().get_sqla_engine_with_context() as engine:
Expand Down
21 changes: 13 additions & 8 deletions tests/unit_tests/explore/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,20 @@ def test_query_no_access(mocker: MockFixture, client) -> None:
from superset.models.core import Database
from superset.models.sql_lab import Query

inspect = mocker.patch("superset.security.manager.inspect")
inspect().default_schema_name = "public"

database = mocker.MagicMock()
mocker.patch(
query_find_by_id,
return_value=Query(database=database, sql="select * from foo"),
)
mocker.patch(query_datasources_by_name, return_value=[SqlaTable()])
mocker.patch(is_admin, return_value=False)
mocker.patch(is_owner, return_value=False)
mocker.patch(can_access, return_value=False)

with raises(SupersetSecurityException):
mocker.patch(
query_find_by_id,
return_value=Query(database=Database(), sql="select * from foo"),
)
mocker.patch(query_datasources_by_name, return_value=[SqlaTable()])
mocker.patch(is_admin, return_value=False)
mocker.patch(is_owner, return_value=False)
mocker.patch(can_access, return_value=False)
check_datasource_access(
datasource_id=1,
datasource_type=DatasourceType.QUERY,
Expand Down
16 changes: 16 additions & 0 deletions tests/unit_tests/security/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.
91 changes: 91 additions & 0 deletions tests/unit_tests/security/manager_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# 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 pytest
from pytest_mock import MockFixture

from superset.exceptions import SupersetSecurityException
from superset.extensions import appbuilder
from superset.security.manager import SupersetSecurityManager


def test_security_manager(app_context: None) -> None:
"""
Test that the security manager can be built.
"""
sm = SupersetSecurityManager(appbuilder)
assert sm


def test_raise_for_access_query_default_schema(
mocker: MockFixture,
app_context: None,
) -> None:
"""
Test that the DB default schema is used in non-qualified table names.

For example, in Postgres, for the following query:

> SELECT * FROM foo;

We should check that the user has access to the `public` schema, regardless of the
schema set in the query.
"""
sm = SupersetSecurityManager(appbuilder)
mocker.patch.object(sm, "can_access_database", return_value=False)
mocker.patch.object(sm, "get_schema_perm", return_value="[PostgreSQL].[public]")
SqlaTable = mocker.patch("superset.connectors.sqla.models.SqlaTable")
SqlaTable.query_datasources_by_name.return_value = []

inspect = mocker.patch("superset.security.manager.inspect")
inspect().default_schema_name = "public"
database = mocker.MagicMock()
query = mocker.MagicMock()
query.database = database
query.sql = "SELECT * FROM ab_user"

# user has access to `public` schema
mocker.patch.object(sm, "can_access", return_value=True)
assert (
sm.raise_for_access( # type: ignore
database=None,
datasource=None,
query=query,
query_context=None,
table=None,
viz=None,
)
is None
)
sm.can_access.assert_called_with("schema_access", "[PostgreSQL].[public]") # type: ignore

# user has only access to `secret` schema
mocker.patch.object(sm, "can_access", return_value=False)
with pytest.raises(SupersetSecurityException) as excinfo:
sm.raise_for_access(
database=None,
datasource=None,
query=query,
query_context=None,
table=None,
viz=None,
)
assert (
str(excinfo.value)
== """You need access to the following tables: `public.ab_user`,
`all_database_access` or `all_datasource_access` permission"""
)