-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Enhancement] (nereids)implement showRolesCommand in nereids
- Loading branch information
Showing
7 changed files
with
189 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowRolesCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// 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.nereids.trees.plans.commands; | ||
|
||
import org.apache.doris.catalog.Column; | ||
import org.apache.doris.catalog.Env; | ||
import org.apache.doris.catalog.ScalarType; | ||
import org.apache.doris.common.ErrorCode; | ||
import org.apache.doris.common.ErrorReport; | ||
import org.apache.doris.mysql.privilege.PrivPredicate; | ||
import org.apache.doris.nereids.trees.plans.PlanType; | ||
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; | ||
import org.apache.doris.qe.ConnectContext; | ||
import org.apache.doris.qe.ShowResultSet; | ||
import org.apache.doris.qe.ShowResultSetMetaData; | ||
import org.apache.doris.qe.StmtExecutor; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* show roles command | ||
*/ | ||
public class ShowRolesCommand extends ShowCommand { | ||
public static final Logger LOG = LogManager.getLogger(ShowRolesCommand.class); | ||
private static final ShowResultSetMetaData META_DATA; | ||
|
||
static { | ||
ShowResultSetMetaData.Builder builder = ShowResultSetMetaData.builder(); | ||
|
||
builder.addColumn(new Column("Name", ScalarType.createVarchar(100))); | ||
builder.addColumn(new Column("Comment", ScalarType.createVarchar(100))); | ||
builder.addColumn(new Column("Users", ScalarType.createVarchar(100))); | ||
builder.addColumn(new Column("GlobalPrivs", ScalarType.createVarchar(300))); | ||
builder.addColumn(new Column("CatalogPrivs", ScalarType.createVarchar(300))); | ||
builder.addColumn(new Column("DatabasePrivs", ScalarType.createVarchar(300))); | ||
builder.addColumn(new Column("TablePrivs", ScalarType.createVarchar(300))); | ||
builder.addColumn(new Column("ResourcePrivs", ScalarType.createVarchar(300))); | ||
builder.addColumn(new Column("CloudClusterPrivs", ScalarType.createVarchar(300))); | ||
builder.addColumn(new Column("CloudStagePrivs", ScalarType.createVarchar(300))); | ||
builder.addColumn(new Column("StorageVaultPrivs", ScalarType.createVarchar(300))); | ||
builder.addColumn(new Column("WorkloadGroupPrivs", ScalarType.createVarchar(300))); | ||
builder.addColumn(new Column("ComputeGroupPrivs", ScalarType.createVarchar(300))); | ||
|
||
META_DATA = builder.build(); | ||
} | ||
|
||
/** | ||
* constructor | ||
*/ | ||
|
||
public ShowRolesCommand() { | ||
super(PlanType.SHOW_ROLE_COMMAND); | ||
} | ||
|
||
@Override | ||
public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exception { | ||
if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), PrivPredicate.GRANT)) { | ||
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "GRANT"); | ||
} | ||
|
||
List<List<String>> infos = Env.getCurrentEnv().getAuth().getRoleInfo(); | ||
return new ShowResultSet(META_DATA, infos); | ||
} | ||
|
||
@Override | ||
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) { | ||
return visitor.visitShowRolesCommand(this, context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
regression-test/suites/nereids_p0/ddl/account/test_nereids_role.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// 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. | ||
|
||
import org.junit.Assert; | ||
|
||
suite("test_nereids_role") { | ||
def role= 'nereids_account_role_test' | ||
def user = 'acount_role_user_test' | ||
def dbName = 'nereids_account_role_test_db' | ||
def pwd = 'C123_567p' | ||
|
||
try_sql("DROP ROLE ${role}") | ||
try_sql("DROP USER ${user}") | ||
sql """DROP DATABASE IF EXISTS ${dbName}""" | ||
sql """CREATE DATABASE ${dbName}""" | ||
|
||
sql """CREATE ROLE ${role}""" | ||
sql """GRANT SELECT_PRIV ON ${context.config.defaultDb} TO ROLE '${role}'""" | ||
sql """GRANT SELECT_PRIV ON ${dbName} TO ROLE '${role}'""" | ||
sql """CREATE USER '${user}' IDENTIFIED BY '${pwd}' DEFAULT ROLE '${role}'""" | ||
def result1 = connect(user=user, password="${pwd}", url=context.config.jdbcUrl) { | ||
sql "show databases like '${dbName}'" | ||
} | ||
assertEquals(result1.size(), 1) | ||
|
||
sql """REVOKE SELECT_PRIV ON ${dbName} FROM ROLE '${role}'""" | ||
def result2 = connect(user=user, password="${pwd}", url=context.config.jdbcUrl) { | ||
sql "show databases like '${dbName}'" | ||
} | ||
assertEquals(result2.size(), 0) | ||
|
||
sql """DROP USER ${user}""" | ||
sql """DROP ROLE ${role}""" | ||
sql """DROP DATABASE ${dbName}""" | ||
|
||
// test comment | ||
// create role with comment | ||
sql """CREATE ROLE ${role} comment 'account_p0_nereids_account_role_test_comment_create'""" | ||
def roles_create = checkNereidsExecuteWithResult("show roles;"); | ||
logger.info("roles_create: " + roles_create.toString()) | ||
assertTrue(roles_create.toString().contains("account_p0_nereids_account_role_test_comment_create")) | ||
// alter role with comment | ||
sql """ALTER ROLE ${role} comment 'account_p0_nereids_account_role_test_comment_alter'""" | ||
def roles_alter = checkNereidsExecuteWithResult("show roles;"); | ||
logger.info("roles_alter: " + roles_alter.toString()) | ||
assertTrue(roles_alter.toString().contains("account_p0_nereids_account_role_test_comment_alter")) | ||
// drop role | ||
sql """DROP ROLE ${role}""" | ||
def roles_drop = checkNereidsExecuteWithResult("show roles;"); | ||
logger.info("roles_drop: " + roles_drop.toString()) | ||
assertFalse(roles_drop.toString().contains("account_p0_nereids_account_role_test_comment_alter")) | ||
} | ||
|