[improvement](auth) support show all properties (#25645)

support `show all properties`
This commit is contained in:
zhangdong
2023-10-24 17:27:59 +08:00
committed by GitHub
parent 091cb0ce37
commit 4cd0dae4b3
7 changed files with 177 additions and 18 deletions

View File

@ -30,39 +30,55 @@ import org.apache.doris.common.PatternMatcherWrapper;
import org.apache.doris.common.UserException;
import org.apache.doris.common.proc.UserPropertyProcNode;
import org.apache.doris.mysql.privilege.PrivPredicate;
import org.apache.doris.persist.gson.GsonUtils;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.ShowResultSetMetaData;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
import java.util.Map;
import java.util.Set;
// Show Property Stmt
// syntax:
// SHOW PROPERTY [FOR user] [LIKE key pattern]
// SHOW [ALL] PROPERTY [FOR user] [LIKE key pattern]
public class ShowUserPropertyStmt extends ShowStmt {
private static final Logger LOG = LogManager.getLogger(ShowUserPropertyStmt.class);
private String user;
private String pattern;
private boolean isAll;
public ShowUserPropertyStmt(String user, String pattern) {
public ShowUserPropertyStmt(String user, String pattern, boolean isAll) {
this.user = user;
this.pattern = pattern;
this.isAll = isAll;
}
@Override
public void analyze(Analyzer analyzer) throws AnalysisException, UserException {
super.analyze(analyzer);
if (Strings.isNullOrEmpty(user)) {
user = analyzer.getQualifiedUser();
// user can see itself's property, no need to check privs
} else {
boolean needCheckAuth = true;
if (!Strings.isNullOrEmpty(user)) {
if (isAll) {
throw new AnalysisException("Can not specified keyword ALL when specified user");
}
user = ClusterNamespace.getFullName(getClusterName(), user);
} else {
if (!isAll) {
// self
user = analyzer.getQualifiedUser();
// user can see itself's property, no need to check privs
needCheckAuth = false;
}
}
if (needCheckAuth) {
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.GRANT)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "GRANT");
}
@ -72,6 +88,10 @@ public class ShowUserPropertyStmt extends ShowStmt {
}
public List<List<String>> getRows() throws AnalysisException {
return isAll ? getRowsForAllUser() : getRowsForOneUser();
}
public List<List<String>> getRowsForOneUser() throws AnalysisException {
List<List<String>> rows = Env.getCurrentEnv().getAuth().getUserProperties(user);
if (pattern == null) {
@ -91,10 +111,42 @@ public class ShowUserPropertyStmt extends ShowStmt {
return result;
}
public List<List<String>> getRowsForAllUser() throws AnalysisException {
Set<String> allUser = Env.getCurrentEnv().getAuth().getAllUser();
List<List<String>> result = Lists.newArrayListWithCapacity(allUser.size());
for (String user : allUser) {
List<String> row = Lists.newArrayListWithCapacity(2);
row.add(user);
row.add(GsonUtils.GSON.toJson(getRowsForUser(user)));
result.add(row);
}
return result;
}
private Map<String, String> getRowsForUser(String user) throws AnalysisException {
Map<String, String> result = Maps.newHashMap();
List<List<String>> userProperties = Env.getCurrentEnv().getAuth()
.getUserProperties(ClusterNamespace.getFullName(getClusterName(), user));
PatternMatcher matcher = null;
if (pattern != null) {
matcher = PatternMatcherWrapper.createMysqlPattern(pattern,
CaseSensibility.USER.getCaseSensibility());
}
for (List<String> row : userProperties) {
String key = row.get(0).split("\\" + SetUserPropertyVar.DOT_SEPARATOR)[0];
if (matcher == null || matcher.match(key)) {
result.put(row.get(0), row.get(1));
}
}
return result;
}
@Override
public ShowResultSetMetaData getMetaData() {
ShowResultSetMetaData.Builder builder = ShowResultSetMetaData.builder();
for (String col : UserPropertyProcNode.TITLE_NAMES) {
for (String col : isAll ? UserPropertyProcNode.ALL_USER_TITLE_NAMES : UserPropertyProcNode.TITLE_NAMES) {
builder.addColumn(new Column(col, ScalarType.createVarchar(30)));
}
return builder.build();
@ -103,10 +155,14 @@ public class ShowUserPropertyStmt extends ShowStmt {
@Override
public String toSql() {
StringBuilder sb = new StringBuilder();
sb.append("SHOW PROPERTY FOR '");
sb.append(user);
sb.append("'");
sb.append("SHOW ");
if (isAll) {
sb.append("ALL PROPERTIES");
} else {
sb.append("PROPERTY FOR '");
sb.append(user);
sb.append("'");
}
if (pattern != null) {
sb.append(" LIKE '");
sb.append(pattern);

View File

@ -31,6 +31,10 @@ public class UserPropertyProcNode implements ProcNodeInterface {
.add("Key").add("Value")
.build();
public static final ImmutableList<String> ALL_USER_TITLE_NAMES = new ImmutableList.Builder<String>()
.add("User").add("Properties")
.build();
private Auth auth;
private UserIdentity userIdent;

View File

@ -1581,6 +1581,9 @@ public class Auth implements Writable {
userRoleManager.addUserRole(userIdent, roleManager.getUserDefaultRoleName(userIdent));
}
public Set<String> getAllUser() {
return userManager.getNameToUsers().keySet();
}
/**
* This is a bug that if created a normal user and grant it with ADMIN_PRIV/RESOURCE_PRIV/NODE_PRIV