Skip to content

Commit

Permalink
fix: system.tables table visibility (#17202)
Browse files Browse the repository at this point in the history
```sql
create user a identified by '123' with default_role='a';
create role a;
create database a;
grant ownership on a.* to role a;
grant role a to a;
create table a.b(id int);
create role b;
grant ownership on a.b to role b;

-- login with user a
-- this query should return right result
select name, owner from system.tables where database = 'a'and name = 'b'

```
  • Loading branch information
TCeason authored Jan 8, 2025
1 parent 64c53b2 commit 963e27a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/query/storages/system/src/tables_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ where TablesTable<WITH_HISTORY, WITHOUT_VIEW>: HistoryAware
}
}

let visibility_checker = ctx.get_visibility_checker(false).await?;
// from system.tables where database = 'db' and name = 'name'
// from system.tables where database = 'db' and table_id = 123
if db_name.len() == 1
Expand All @@ -336,7 +337,6 @@ where TablesTable<WITH_HISTORY, WITHOUT_VIEW>: HistoryAware
&& !invalid_tables_ids
&& !WITH_HISTORY
{
let visibility_checker = ctx.get_visibility_checker(true).await?;
for (ctl_name, ctl) in ctls.iter() {
for db in &db_name {
match ctl.get_database(&tenant, db.as_str()).await {
Expand Down Expand Up @@ -415,7 +415,6 @@ where TablesTable<WITH_HISTORY, WITHOUT_VIEW>: HistoryAware
}
}
} else {
let visibility_checker = ctx.get_visibility_checker(false).await?;
let catalog_dbs = visibility_checker.get_visibility_database();

for (ctl_name, ctl) in ctls.iter() {
Expand Down
2 changes: 2 additions & 0 deletions tests/suites/0_stateless/18_rbac/18_0003_db_visibility.result
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,5 @@ system
2
3
4
=== test system.tables ===
b b
22 changes: 22 additions & 0 deletions tests/suites/0_stateless/18_rbac/18_0003_db_visibility.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,25 @@ echo "select * from db1.t1" | $BENDSQL_CLIENT_CONNECT
echo "select * from db2.t2" | $BENDSQL_CLIENT_CONNECT
echo "select * from db_u3.t3" | $BENDSQL_CLIENT_CONNECT
echo "select * from db_root.t1" | $BENDSQL_CLIENT_CONNECT

echo "=== test system.tables ==="
echo "drop user if exists a;" | $BENDSQL_CLIENT_CONNECT
echo "drop user if exists b;" | $BENDSQL_CLIENT_CONNECT
echo "drop role if exists b;" | $BENDSQL_CLIENT_CONNECT
echo "drop role if exists a;" | $BENDSQL_CLIENT_CONNECT
echo "drop database if exists a;" | $BENDSQL_CLIENT_CONNECT
echo "create user a identified by '123' with default_role='a'" | $BENDSQL_CLIENT_CONNECT
echo "create role a" | $BENDSQL_CLIENT_CONNECT
echo "create database a" | $BENDSQL_CLIENT_CONNECT
echo "grant ownership on a.* to role a" | $BENDSQL_CLIENT_CONNECT
echo "grant role a to a" | $BENDSQL_CLIENT_CONNECT
echo "create table a.b(id int)" | $BENDSQL_CLIENT_CONNECT
echo "create role b" | $BENDSQL_CLIENT_CONNECT
echo "grant ownership on a.b to role b" | $BENDSQL_CLIENT_CONNECT
export TEST_A_CONNECT="bendsql --user=a --password=123 --host=${QUERY_MYSQL_HANDLER_HOST} --port ${QUERY_HTTP_HANDLER_PORT}"
echo "select name, owner from system.tables where database = 'a'and name = 'b'" | $TEST_A_CONNECT
echo "drop user if exists a;" | $BENDSQL_CLIENT_CONNECT
echo "drop user if exists b;" | $BENDSQL_CLIENT_CONNECT
echo "drop role if exists b;" | $BENDSQL_CLIENT_CONNECT
echo "drop role if exists a;" | $BENDSQL_CLIENT_CONNECT
echo "drop database if exists a;" | $BENDSQL_CLIENT_CONNECT

0 comments on commit 963e27a

Please sign in to comment.