Skip to content

Commit

Permalink
[BugFix] use database should check privilege of pipe (#34999)
Browse files Browse the repository at this point in the history
Signed-off-by: Murphy <mofei@starrocks.com>
  • Loading branch information
murphyatwork authored Nov 15, 2023
1 parent a2015c1 commit f89f616
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
58 changes: 35 additions & 23 deletions fe/fe-core/src/main/java/com/starrocks/sql/analyzer/Authorizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.starrocks.sql.analyzer;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.starrocks.analysis.Expr;
import com.starrocks.analysis.TableName;
import com.starrocks.catalog.Column;
Expand All @@ -24,6 +25,7 @@
import com.starrocks.catalog.Table;
import com.starrocks.common.Config;
import com.starrocks.privilege.AccessControlProvider;
import com.starrocks.privilege.AccessController;
import com.starrocks.privilege.AccessDeniedException;
import com.starrocks.privilege.NativeAccessController;
import com.starrocks.privilege.ObjectType;
Expand All @@ -36,6 +38,7 @@
import com.starrocks.sql.ast.StatementBase;
import com.starrocks.sql.ast.UserIdentity;
import com.starrocks.sql.ast.pipe.PipeName;
import org.apache.commons.collections4.ListUtils;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -278,39 +281,48 @@ public static void checkActionInDb(UserIdentity currentUser, Set<Long> roleIds,
.checkActionInDb(currentUser, roleIds, db, privilegeType);
}

/**
* A lambda function that throws AccessDeniedException
*/
@FunctionalInterface
public interface AccessControlChecker {
void check() throws AccessDeniedException;
}

/**
* Check whether current user has any privilege action on the db or objects(table/view/mv) in the db.
* Currently, it's used by `show databases` or `use database`.
*/
public static void checkAnyActionOnOrInDb(UserIdentity currentUser, Set<Long> roleIds, String catalogName, String db)
throws AccessDeniedException {
Preconditions.checkNotNull(db, "db should not null");

try {
getInstance().getAccessControlOrDefault(catalogName).checkAnyActionOnDb(currentUser, roleIds, catalogName, db);
} catch (AccessDeniedException e1) {
AccessController controller = getInstance().getAccessControlOrDefault(catalogName);

List<AccessControlChecker> basicCheckers = ImmutableList.of(
() -> controller.checkAnyActionOnDb(currentUser, roleIds, catalogName, db),
() -> controller.checkAnyActionOnAnyTable(currentUser, roleIds, catalogName, db)
);
List<AccessControlChecker> extraCheckers = ImmutableList.of(
() -> controller.checkAnyActionOnAnyView(currentUser, roleIds, db),
() -> controller.checkAnyActionOnAnyMaterializedView(currentUser, roleIds, db),
() -> controller.checkAnyActionOnAnyFunction(currentUser, roleIds, db),
() -> controller.checkAnyActionOnPipe(currentUser, roleIds, new PipeName("*", "*"))
);
List<AccessControlChecker> appliedCheckers = CatalogMgr.isInternalCatalog(catalogName) ?
ListUtils.union(basicCheckers, extraCheckers) : basicCheckers;

AccessDeniedException lastExcepton = null;
for (AccessControlChecker checker : appliedCheckers) {
try {
getInstance().getAccessControlOrDefault(catalogName)
.checkAnyActionOnAnyTable(currentUser, roleIds, catalogName, db);
} catch (AccessDeniedException e2) {
if (CatalogMgr.isInternalCatalog(catalogName)) {
try {
getInstance().getAccessControlOrDefault(InternalCatalog.DEFAULT_INTERNAL_CATALOG_NAME)
.checkAnyActionOnAnyView(currentUser, roleIds, db);
} catch (AccessDeniedException e3) {
try {
getInstance().getAccessControlOrDefault(InternalCatalog.DEFAULT_INTERNAL_CATALOG_NAME)
.checkAnyActionOnAnyMaterializedView(currentUser, roleIds, db);
} catch (AccessDeniedException e4) {
getInstance().getAccessControlOrDefault(InternalCatalog.DEFAULT_INTERNAL_CATALOG_NAME)
.checkAnyActionOnAnyFunction(currentUser, roleIds, db);
}
}
} else {
throw new AccessDeniedException();
}
checker.check();
return;
} catch (AccessDeniedException e) {
lastExcepton = e;
}
}
if (lastExcepton != null) {
throw lastExcepton;
}
}

public static void checkResourceAction(UserIdentity currentUser, Set<Long> roleIds, String name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3355,6 +3355,21 @@ public void testPipePEntryObject() throws Exception {
;
}

@Test
public void testPipeUseDatabase() throws Exception {
starRocksAssert.withDatabase("test_implicit_priv");
ctxToTestUser();
verifyGrantRevoke(
"use test_implicit_priv",
"grant ALL on ALL PIPES in ALL DATABASES to test",
"revoke ALL on ALL PIPES in ALL DATABASES from test",
"Access denied; you need (at least one of) the ANY privilege(s) on DATABASE " +
"test_implicit_priv for this operation"
);
ctxToRoot();
starRocksAssert.dropDatabase("test_implicit_priv");
}

@Test
public void testPolicyRewrite() throws Exception {
Config.access_control = "ranger";
Expand Down

0 comments on commit f89f616

Please sign in to comment.