Skip to content

Commit

Permalink
[fix](auth)fix create table like need create_priv of existed table (#… (
Browse files Browse the repository at this point in the history
#38570)

…37879)

pick: #37879
  • Loading branch information
zddr authored Aug 1, 2024
1 parent a4e7937 commit 60091f0
Showing 1 changed file with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -1166,6 +1167,8 @@ public boolean createTable(CreateTableStmt stmt) throws UserException {
}

public void createTableLike(CreateTableLikeStmt stmt) throws DdlException {
ConnectContext ctx = ConnectContext.get();
Objects.requireNonNull(ctx, "ConnectContext.get() can not be null.");
try {
DatabaseIf db = getDbOrDdlException(stmt.getExistedDbName());
TableIf table = db.getTableOrDdlException(stmt.getExistedTableName());
Expand Down Expand Up @@ -1199,14 +1202,23 @@ public void createTableLike(CreateTableLikeStmt stmt) throws DdlException {
} finally {
table.readUnlock();
}
CreateTableStmt parsedCreateTableStmt = (CreateTableStmt) SqlParserUtils.parseAndAnalyzeStmt(
createTableStmt.get(0), ConnectContext.get());
parsedCreateTableStmt.setTableName(stmt.getTableName());
parsedCreateTableStmt.setIfNotExists(stmt.isIfNotExists());
createTable(parsedCreateTableStmt);

try {
// analyze CreateTableStmt will check create_priv of existedTable, create table like only need
// create_priv of newTable, and select_priv of existedTable, and priv check has done in
// CreateTableStmt/CreateTableCommand, so we skip it
ctx.setSkipAuth(true);
CreateTableStmt parsedCreateTableStmt = (CreateTableStmt) SqlParserUtils.parseAndAnalyzeStmt(
createTableStmt.get(0), ctx);
parsedCreateTableStmt.setTableName(stmt.getTableName());
parsedCreateTableStmt.setIfNotExists(stmt.isIfNotExists());
createTable(parsedCreateTableStmt);
} finally {
ctx.setSkipAuth(false);
}
} catch (UserException e) {
throw new DdlException("Failed to execute CREATE TABLE LIKE " + stmt.getExistedTableName() + ". Reason: "
+ e.getMessage());
+ e.getMessage(), e);
}
}

Expand Down

0 comments on commit 60091f0

Please sign in to comment.