This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 6a3ac495769 Add AuthorityRule.getGrantees() (#33084)
6a3ac495769 is described below
commit 6a3ac4957693505ebd2092806cc190a918dd1401
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Oct 1 16:08:36 2024 +0800
Add AuthorityRule.getGrantees() (#33084)
* Refactor AuthorityRule
* Add AuthorityRule.getGrantees()
* Add AuthorityRule.getGrantees()
* Add AuthorityRule.getGrantees()
* Add AuthorityRule.getGrantees()
---
.../authority/rule/AuthorityRule.java | 41 +++++++++++++++-------
.../authority/rule/AuthorityRuleTest.java | 15 ++++++++
.../handler/query/ShowAuthorityRuleExecutor.java | 3 +-
.../executor/utils/StatisticsAssembleUtils.java | 6 ++--
4 files changed, 48 insertions(+), 17 deletions(-)
diff --git
a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
index 0dc6a54be17..f468877d4b7 100644
---
a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
+++
b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
@@ -28,7 +28,7 @@ import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import java.util.Collection;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
@@ -41,20 +41,16 @@ public final class AuthorityRule implements GlobalRule {
@Getter
private final AuthorityRuleConfiguration configuration;
- @Getter
- private final Collection<ShardingSphereUser> users;
-
- private final Map<Grantee, ShardingSpherePrivileges> privileges;
+ private final Map<ShardingSphereUser, ShardingSpherePrivileges> privileges;
public AuthorityRule(final AuthorityRuleConfiguration ruleConfig) {
configuration = ruleConfig;
- users = ruleConfig.getUsers().stream()
+ Collection<ShardingSphereUser> users = ruleConfig.getUsers().stream()
.map(each -> new ShardingSphereUser(each.getUsername(),
each.getPassword(), each.getHostname(), each.getAuthenticationMethodName(),
each.isAdmin())).collect(Collectors.toList());
- privileges = new HashMap<>();
- for (ShardingSphereUser each : users) {
- privileges.put(each.getGrantee(), TypedSPILoader.getService(
- PrivilegeProvider.class,
ruleConfig.getPrivilegeProvider().getType(),
ruleConfig.getPrivilegeProvider().getProps()).build(ruleConfig,
each.getGrantee()));
- }
+ privileges = users.stream().collect(Collectors.toMap(each -> each,
+ each -> TypedSPILoader.getService(PrivilegeProvider.class,
ruleConfig.getPrivilegeProvider().getType(),
ruleConfig.getPrivilegeProvider().getProps())
+ .build(ruleConfig, each.getGrantee()),
+ (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
}
/**
@@ -73,6 +69,15 @@ public final class AuthorityRule implements GlobalRule {
return "";
}
+ /**
+ * Get grantees.
+ *
+ * @return grantees
+ */
+ public Collection<Grantee> getGrantees() {
+ return
privileges.keySet().stream().map(ShardingSphereUser::getGrantee).collect(Collectors.toList());
+ }
+
/**
* Find user.
*
@@ -81,7 +86,12 @@ public final class AuthorityRule implements GlobalRule {
*/
@HighFrequencyInvocation
public Optional<ShardingSphereUser> findUser(final Grantee grantee) {
- return users.stream().filter(each ->
each.getGrantee().accept(grantee)).findFirst();
+ for (ShardingSphereUser each : privileges.keySet()) {
+ if (each.getGrantee().accept(grantee)) {
+ return Optional.of(each);
+ }
+ }
+ return Optional.empty();
}
/**
@@ -92,6 +102,11 @@ public final class AuthorityRule implements GlobalRule {
*/
@HighFrequencyInvocation
public Optional<ShardingSpherePrivileges> findPrivileges(final Grantee
grantee) {
- return privileges.keySet().stream().filter(each ->
each.accept(grantee)).findFirst().map(privileges::get);
+ for (ShardingSphereUser each : privileges.keySet()) {
+ if (each.getGrantee().accept(grantee)) {
+ return Optional.of(each).map(privileges::get);
+ }
+ }
+ return Optional.empty();
}
}
diff --git
a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/rule/AuthorityRuleTest.java
b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/rule/AuthorityRuleTest.java
index 436d4645870..6ac21da0183 100644
---
a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/rule/AuthorityRuleTest.java
+++
b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/rule/AuthorityRuleTest.java
@@ -24,9 +24,11 @@ import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.junit.jupiter.api.Test;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.List;
import java.util.Optional;
import java.util.Properties;
@@ -55,6 +57,14 @@ class AuthorityRuleTest {
assertThat(createAuthorityRule(null).getAuthenticatorType(user),
is(""));
}
+ @Test
+ void assertGetGrantees() {
+ List<Grantee> actual = new
ArrayList<>(createAuthorityRule(null).getGrantees());
+ assertThat(actual.size(), is(2));
+ assertThat(actual.get(0), is(new Grantee("root", "localhost")));
+ assertThat(actual.get(1), is(new Grantee("admin", "localhost")));
+ }
+
@Test
void assertFindUser() {
Grantee toBefoundGrantee = new Grantee("admin", "localhost");
@@ -73,6 +83,11 @@ class AuthorityRuleTest {
assertTrue(createAuthorityRule(null).findPrivileges(new
Grantee("admin", "localhost")).isPresent());
}
+ @Test
+ void assertNotFoundPrivileges() {
+ assertFalse(createAuthorityRule(null).findPrivileges(new
Grantee("not_found", "")).isPresent());
+ }
+
private AuthorityRule createAuthorityRule(final String
defaultAuthenticator) {
Collection<UserConfiguration> userConfigs = Arrays.asList(
new UserConfiguration("root", "root", "localhost", null,
false),
diff --git
a/kernel/authority/distsql/handler/src/main/java/org/apache/shardingsphere/authority/distsql/handler/query/ShowAuthorityRuleExecutor.java
b/kernel/authority/distsql/handler/src/main/java/org/apache/shardingsphere/authority/distsql/handler/query/ShowAuthorityRuleExecutor.java
index d0c12321c6f..56870376088 100644
---
a/kernel/authority/distsql/handler/src/main/java/org/apache/shardingsphere/authority/distsql/handler/query/ShowAuthorityRuleExecutor.java
+++
b/kernel/authority/distsql/handler/src/main/java/org/apache/shardingsphere/authority/distsql/handler/query/ShowAuthorityRuleExecutor.java
@@ -23,6 +23,7 @@ import org.apache.shardingsphere.authority.rule.AuthorityRule;
import
org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorRuleAware;
import
org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecutor;
import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
+import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.mode.manager.ContextManager;
import java.util.Arrays;
@@ -46,7 +47,7 @@ public final class ShowAuthorityRuleExecutor implements
DistSQLQueryExecutor<Sho
@Override
public Collection<LocalDataQueryResultRow> getRows(final
ShowAuthorityRuleStatement sqlStatement, final ContextManager contextManager) {
- String users = rule.getUsers().stream().map(each ->
each.getGrantee().toString()).collect(Collectors.joining("; "));
+ String users =
rule.getGrantees().stream().map(Grantee::toString).collect(Collectors.joining(";
"));
String provider =
rule.getConfiguration().getPrivilegeProvider().getType();
Properties props =
rule.getConfiguration().getPrivilegeProvider().getProps().isEmpty() ? new
Properties() : rule.getConfiguration().getPrivilegeProvider().getProps();
return Collections.singleton(new LocalDataQueryResultRow(users,
provider, props));
diff --git
a/kernel/sql-federation/executor/src/main/java/org/apache/shardingsphere/sqlfederation/executor/utils/StatisticsAssembleUtils.java
b/kernel/sql-federation/executor/src/main/java/org/apache/shardingsphere/sqlfederation/executor/utils/StatisticsAssembleUtils.java
index 7385a74916a..5c4b0f43074 100644
---
a/kernel/sql-federation/executor/src/main/java/org/apache/shardingsphere/sqlfederation/executor/utils/StatisticsAssembleUtils.java
+++
b/kernel/sql-federation/executor/src/main/java/org/apache/shardingsphere/sqlfederation/executor/utils/StatisticsAssembleUtils.java
@@ -26,7 +26,7 @@ import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSp
import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
import
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
-import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
+import org.apache.shardingsphere.infra.metadata.user.Grantee;
import
org.apache.shardingsphere.sqlfederation.executor.constant.EnumerableConstants;
import java.util.Arrays;
@@ -83,9 +83,9 @@ public final class StatisticsAssembleUtils {
}
private static void assembleOpenGaussRoleData(final
ShardingSphereTableData tableData, final ShardingSphereMetaData metaData) {
- for (ShardingSphereUser each :
metaData.getGlobalRuleMetaData().getSingleRule(AuthorityRule.class).getUsers())
{
+ for (Grantee each :
metaData.getGlobalRuleMetaData().getSingleRule(AuthorityRule.class).getGrantees())
{
Object[] rows = new Object[27];
- rows[0] = each.getGrantee().getUsername();
+ rows[0] = each.getUsername();
tableData.getRows().add(new
ShardingSphereRowData(Arrays.asList(rows)));
}
}