Skip to content

Commit 3de3a75

Browse files
authored
[fix](binlog) Require SELECT privilege for binlog TVF (apache#68088)
### What problem does this PR solve? Issue Number: N/A Related PR: N/A Problem Summary: The `binlog()` table-valued function did not implement the standard TVF authorization hook. As a result, the Nereids privilege-check phase could not enforce table privileges for the target OLAP table, and a user denied direct `SELECT` could still read its row-binlog contents through the TVF. This change overrides `TableBinlogFunction.checkAuth(ConnectContext)` and requires `SELECT` on the target table. The TVF is therefore authorized through the standard `CheckPrivileges` flow, independently of constructor and metadata initialization.
1 parent 0451443 commit 3de3a75

3 files changed

Lines changed: 170 additions & 0 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/tablefunction/TableBinlogFunction.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929
import org.apache.doris.catalog.TableIf.TableType;
3030
import org.apache.doris.catalog.info.PartitionNamesInfo;
3131
import org.apache.doris.common.AnalysisException;
32+
import org.apache.doris.common.ErrorCode;
3233
import org.apache.doris.common.MetaNotFoundException;
3334
import org.apache.doris.common.util.Util;
35+
import org.apache.doris.datasource.InternalCatalog;
3436
import org.apache.doris.mtmv.ivm.IvmUtil;
37+
import org.apache.doris.mysql.privilege.PrivPredicate;
3538
import org.apache.doris.planner.OlapScanNode;
3639
import org.apache.doris.planner.PlanNodeId;
3740
import org.apache.doris.planner.ScanContext;
@@ -126,6 +129,16 @@ public TableBinlogFunction(Map<String, String> params) throws AnalysisException
126129
}
127130
}
128131

132+
@Override
133+
public void checkAuth(ConnectContext ctx) {
134+
if (!Env.getCurrentEnv().getAccessManager().checkTblPriv(ctx, InternalCatalog.INTERNAL_CATALOG_NAME,
135+
dbName, tableName, PrivPredicate.SELECT)) {
136+
String message = ErrorCode.ERR_TABLE_ACCESS_DENIED_ERROR.formatErrorMsg(
137+
PrivPredicate.SELECT.getPrivs().toString(), tableName);
138+
throw new org.apache.doris.nereids.exceptions.AnalysisException(message);
139+
}
140+
}
141+
129142
@Override
130143
public String getTableName() {
131144
return "BinlogTableFunction";
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.tablefunction;
19+
20+
import org.apache.doris.catalog.Database;
21+
import org.apache.doris.catalog.Env;
22+
import org.apache.doris.catalog.OlapTable;
23+
import org.apache.doris.catalog.RowBinlogTableWrapper;
24+
import org.apache.doris.catalog.TableIf.TableType;
25+
import org.apache.doris.datasource.InternalCatalog;
26+
import org.apache.doris.mysql.privilege.AccessControllerManager;
27+
import org.apache.doris.mysql.privilege.PrivPredicate;
28+
import org.apache.doris.nereids.exceptions.AnalysisException;
29+
import org.apache.doris.qe.ConnectContext;
30+
31+
import com.google.common.collect.ImmutableMap;
32+
import org.junit.jupiter.api.Assertions;
33+
import org.junit.jupiter.api.Test;
34+
import org.mockito.MockedConstruction;
35+
import org.mockito.MockedStatic;
36+
import org.mockito.Mockito;
37+
38+
public class TableBinlogFunctionAuthTest {
39+
40+
@Test
41+
public void testCheckAuthRequiresSelectPrivilege() throws Exception {
42+
Env env = Mockito.mock(Env.class);
43+
ConnectContext context = Mockito.mock(ConnectContext.class);
44+
AccessControllerManager accessManager = Mockito.mock(AccessControllerManager.class);
45+
InternalCatalog catalog = Mockito.mock(InternalCatalog.class);
46+
Database database = Mockito.mock(Database.class);
47+
OlapTable table = Mockito.mock(OlapTable.class);
48+
49+
Mockito.when(env.getAccessManager()).thenReturn(accessManager);
50+
Mockito.when(env.getInternalCatalog()).thenReturn(catalog);
51+
Mockito.when(catalog.getDbOrMetaException("test_db")).thenReturn(database);
52+
Mockito.when(database.getTableOrMetaException("test_table", TableType.OLAP)).thenReturn(table);
53+
Mockito.when(table.needRowBinlog()).thenReturn(true);
54+
Mockito.when(accessManager.checkTblPriv(context, InternalCatalog.INTERNAL_CATALOG_NAME,
55+
"test_db", "test_table", PrivPredicate.SELECT)).thenReturn(false);
56+
57+
try (MockedStatic<Env> mockedEnv = Mockito.mockStatic(Env.class);
58+
MockedConstruction<RowBinlogTableWrapper> ignored =
59+
Mockito.mockConstruction(RowBinlogTableWrapper.class)) {
60+
mockedEnv.when(Env::getCurrentEnv).thenReturn(env);
61+
62+
TableBinlogFunction function = new TableBinlogFunction(ImmutableMap.of(
63+
"db", "test_db", "table", "test_table"));
64+
Mockito.verifyNoInteractions(accessManager);
65+
66+
AnalysisException exception = Assertions.assertThrows(AnalysisException.class,
67+
() -> function.checkAuth(context));
68+
Assertions.assertTrue(exception.getMessage().contains("Access denied"));
69+
Mockito.verify(accessManager).checkTblPriv(context, InternalCatalog.INTERNAL_CATALOG_NAME,
70+
"test_db", "test_table", PrivPredicate.SELECT);
71+
72+
Mockito.when(accessManager.checkTblPriv(context, InternalCatalog.INTERNAL_CATALOG_NAME,
73+
"test_db", "test_table", PrivPredicate.SELECT)).thenReturn(true);
74+
Assertions.assertDoesNotThrow(() -> function.checkAuth(context));
75+
}
76+
}
77+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
suite("test_binlog_tvf_auth", "p0,auth") {
19+
String user = "test_binlog_tvf_auth_user"
20+
String password = "C123_567p"
21+
22+
sql "DROP USER IF EXISTS '${user}'"
23+
sql "DROP DATABASE IF EXISTS test_binlog_tvf_auth_db"
24+
sql "CREATE DATABASE test_binlog_tvf_auth_db"
25+
sql """
26+
CREATE TABLE test_binlog_tvf_auth_db.test_binlog_tvf_auth_table (
27+
k BIGINT,
28+
v INT
29+
) ENGINE=OLAP
30+
DUPLICATE KEY(k)
31+
DISTRIBUTED BY HASH(k) BUCKETS 1
32+
PROPERTIES (
33+
"replication_num" = "1",
34+
"binlog.enable" = "true",
35+
"binlog.format" = "ROW"
36+
)
37+
"""
38+
sql "INSERT INTO test_binlog_tvf_auth_db.test_binlog_tvf_auth_table VALUES (1, 100)"
39+
sql "SYNC"
40+
sql "CREATE USER '${user}' IDENTIFIED BY '${password}'"
41+
42+
if (isCloudMode()) {
43+
def clusters = sql "SHOW CLUSTERS"
44+
assertFalse(clusters.isEmpty())
45+
sql "GRANT USAGE_PRIV ON CLUSTER `${clusters[0][0]}` TO ${user}"
46+
}
47+
// The configured JDBC URL connects to regression_test before executing the query.
48+
// Grant access to that default database while keeping the target table unauthorized.
49+
sql "GRANT SELECT_PRIV ON regression_test TO ${user}"
50+
51+
connect(user, password, context.config.jdbcUrl) {
52+
test {
53+
sql "SELECT * FROM test_binlog_tvf_auth_db.test_binlog_tvf_auth_table"
54+
exception "denied"
55+
}
56+
test {
57+
sql """
58+
SELECT k, v
59+
FROM binlog(
60+
"db" = "test_binlog_tvf_auth_db",
61+
"table" = "test_binlog_tvf_auth_table"
62+
)
63+
LIMIT 1
64+
"""
65+
exception "denied"
66+
}
67+
}
68+
69+
sql "GRANT SELECT_PRIV ON test_binlog_tvf_auth_db.test_binlog_tvf_auth_table TO ${user}"
70+
connect(user, password, context.config.jdbcUrl) {
71+
sql """
72+
SELECT k, v
73+
FROM binlog(
74+
"db" = "test_binlog_tvf_auth_db",
75+
"table" = "test_binlog_tvf_auth_table"
76+
)
77+
LIMIT 1
78+
"""
79+
}
80+
}

0 commit comments

Comments
 (0)