Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@
import org.apache.doris.catalog.TableIf.TableType;
import org.apache.doris.catalog.info.PartitionNamesInfo;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.ErrorCode;
import org.apache.doris.common.MetaNotFoundException;
import org.apache.doris.common.util.Util;
import org.apache.doris.datasource.InternalCatalog;
import org.apache.doris.mtmv.ivm.IvmUtil;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.planner.OlapScanNode;
import org.apache.doris.planner.PlanNodeId;
import org.apache.doris.planner.ScanContext;
Expand Down Expand Up @@ -126,6 +129,16 @@ public TableBinlogFunction(Map<String, String> params) throws AnalysisException
}
}

@Override
Comment thread
morrySnow marked this conversation as resolved.
Resolved
public void checkAuth(ConnectContext ctx) {
Comment thread
morrySnow marked this conversation as resolved.
Resolved
if (!Env.getCurrentEnv().getAccessManager().checkTblPriv(ctx, InternalCatalog.INTERNAL_CATALOG_NAME,
Comment thread
morrySnow marked this conversation as resolved.
Resolved
dbName, tableName, PrivPredicate.SELECT)) {
String message = ErrorCode.ERR_TABLE_ACCESS_DENIED_ERROR.formatErrorMsg(
PrivPredicate.SELECT.getPrivs().toString(), tableName);
throw new org.apache.doris.nereids.exceptions.AnalysisException(message);
}
}

@Override
public String getTableName() {
return "BinlogTableFunction";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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.

package org.apache.doris.tablefunction;

import org.apache.doris.catalog.Database;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.OlapTable;
import org.apache.doris.catalog.RowBinlogTableWrapper;
import org.apache.doris.catalog.TableIf.TableType;
import org.apache.doris.datasource.InternalCatalog;
import org.apache.doris.mysql.privilege.AccessControllerManager;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.qe.ConnectContext;

import com.google.common.collect.ImmutableMap;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

public class TableBinlogFunctionAuthTest {

@Test
public void testCheckAuthRequiresSelectPrivilege() throws Exception {
Env env = Mockito.mock(Env.class);
ConnectContext context = Mockito.mock(ConnectContext.class);
AccessControllerManager accessManager = Mockito.mock(AccessControllerManager.class);
InternalCatalog catalog = Mockito.mock(InternalCatalog.class);
Database database = Mockito.mock(Database.class);
OlapTable table = Mockito.mock(OlapTable.class);

Mockito.when(env.getAccessManager()).thenReturn(accessManager);
Mockito.when(env.getInternalCatalog()).thenReturn(catalog);
Mockito.when(catalog.getDbOrMetaException("test_db")).thenReturn(database);
Mockito.when(database.getTableOrMetaException("test_table", TableType.OLAP)).thenReturn(table);
Mockito.when(table.needRowBinlog()).thenReturn(true);
Mockito.when(accessManager.checkTblPriv(context, InternalCatalog.INTERNAL_CATALOG_NAME,
"test_db", "test_table", PrivPredicate.SELECT)).thenReturn(false);

try (MockedStatic<Env> mockedEnv = Mockito.mockStatic(Env.class);
MockedConstruction<RowBinlogTableWrapper> ignored =
Mockito.mockConstruction(RowBinlogTableWrapper.class)) {
mockedEnv.when(Env::getCurrentEnv).thenReturn(env);

TableBinlogFunction function = new TableBinlogFunction(ImmutableMap.of(
"db", "test_db", "table", "test_table"));
Mockito.verifyNoInteractions(accessManager);

AnalysisException exception = Assertions.assertThrows(AnalysisException.class,
() -> function.checkAuth(context));
Assertions.assertTrue(exception.getMessage().contains("Access denied"));
Mockito.verify(accessManager).checkTblPriv(context, InternalCatalog.INTERNAL_CATALOG_NAME,
"test_db", "test_table", PrivPredicate.SELECT);

Mockito.when(accessManager.checkTblPriv(context, InternalCatalog.INTERNAL_CATALOG_NAME,
"test_db", "test_table", PrivPredicate.SELECT)).thenReturn(true);
Assertions.assertDoesNotThrow(() -> function.checkAuth(context));
}
}
}
80 changes: 80 additions & 0 deletions regression-test/suites/auth_p0/test_binlog_tvf_auth.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// 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.

suite("test_binlog_tvf_auth", "p0,auth") {
String user = "test_binlog_tvf_auth_user"
String password = "C123_567p"

sql "DROP USER IF EXISTS '${user}'"
sql "DROP DATABASE IF EXISTS test_binlog_tvf_auth_db"
sql "CREATE DATABASE test_binlog_tvf_auth_db"
sql """
CREATE TABLE test_binlog_tvf_auth_db.test_binlog_tvf_auth_table (
k BIGINT,
v INT
) ENGINE=OLAP
DUPLICATE KEY(k)
DISTRIBUTED BY HASH(k) BUCKETS 1
PROPERTIES (
"replication_num" = "1",
"binlog.enable" = "true",
"binlog.format" = "ROW"
)
"""
sql "INSERT INTO test_binlog_tvf_auth_db.test_binlog_tvf_auth_table VALUES (1, 100)"
sql "SYNC"
sql "CREATE USER '${user}' IDENTIFIED BY '${password}'"

if (isCloudMode()) {
def clusters = sql "SHOW CLUSTERS"
assertFalse(clusters.isEmpty())
sql "GRANT USAGE_PRIV ON CLUSTER `${clusters[0][0]}` TO ${user}"
}
// The configured JDBC URL connects to regression_test before executing the query.
// Grant access to that default database while keeping the target table unauthorized.
sql "GRANT SELECT_PRIV ON regression_test TO ${user}"

connect(user, password, context.config.jdbcUrl) {
test {
sql "SELECT * FROM test_binlog_tvf_auth_db.test_binlog_tvf_auth_table"
exception "denied"
}
test {
sql """
SELECT k, v
FROM binlog(
"db" = "test_binlog_tvf_auth_db",
"table" = "test_binlog_tvf_auth_table"
)
LIMIT 1
"""
exception "denied"
}
}

sql "GRANT SELECT_PRIV ON test_binlog_tvf_auth_db.test_binlog_tvf_auth_table TO ${user}"
connect(user, password, context.config.jdbcUrl) {
sql """
SELECT k, v
FROM binlog(
"db" = "test_binlog_tvf_auth_db",
"table" = "test_binlog_tvf_auth_table"
)
LIMIT 1
"""
}
}
Loading