This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 d5a7575ff30 Refactor AuthorityRuleConfiguration (#29580)
d5a7575ff30 is described below
commit d5a7575ff307be3729d6302ac6424116bad77e4e
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Dec 28 15:34:45 2023 +0800
Refactor AuthorityRuleConfiguration (#29580)
* Add SingletonSPI for AuthorityRegistryProvider
* Refactor AllPermittedAuthorityRegistryProvider
* Refactor AuthorityRuleConfiguration
---
.../authority/config/AuthorityRuleConfiguration.java | 5 ++---
.../builder/DefaultAuthorityRuleConfigurationBuilder.java | 3 ++-
.../swapper/NewYamlAuthorityRuleConfigurationSwapper.java | 13 +++++++------
.../yaml/swapper/YamlAuthorityRuleConfigurationSwapper.java | 13 +++++++------
.../authority/checker/AuthorityCheckerTest.java | 4 ++--
.../shardingsphere/authority/rule/AuthorityRuleTest.java | 3 ++-
.../NewYamlAuthorityRuleConfigurationSwapperTest.java | 2 +-
.../swapper/YamlAuthorityRuleConfigurationSwapperTest.java | 2 +-
.../distsql/handler/ShowAuthorityRuleExecutorTest.java | 2 +-
.../subscriber/ConfigurationChangedSubscriberTest.java | 2 +-
.../authentication/OpenGaussAuthenticationEngineTest.java | 3 ++-
.../authentication/PostgreSQLAuthenticationEngineTest.java | 3 ++-
12 files changed, 30 insertions(+), 25 deletions(-)
diff --git
a/kernel/authority/api/src/main/java/org/apache/shardingsphere/authority/config/AuthorityRuleConfiguration.java
b/kernel/authority/api/src/main/java/org/apache/shardingsphere/authority/config/AuthorityRuleConfiguration.java
index 7f359b794fd..8b7224d549e 100644
---
a/kernel/authority/api/src/main/java/org/apache/shardingsphere/authority/config/AuthorityRuleConfiguration.java
+++
b/kernel/authority/api/src/main/java/org/apache/shardingsphere/authority/config/AuthorityRuleConfiguration.java
@@ -24,7 +24,6 @@ import
org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import java.util.Collection;
-import java.util.LinkedHashMap;
import java.util.Map;
/**
@@ -38,7 +37,7 @@ public final class AuthorityRuleConfiguration implements
GlobalRuleConfiguration
private final AlgorithmConfiguration authorityProvider;
- private final String defaultAuthenticator;
+ private final Map<String, AlgorithmConfiguration> authenticators;
- private final Map<String, AlgorithmConfiguration> authenticators = new
LinkedHashMap<>();
+ private final String defaultAuthenticator;
}
diff --git
a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/rule/builder/DefaultAuthorityRuleConfigurationBuilder.java
b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/rule/builder/DefaultAuthorityRuleConfigurationBuilder.java
index 0e73e333018..5d2cb239e85 100644
---
a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/rule/builder/DefaultAuthorityRuleConfigurationBuilder.java
+++
b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/rule/builder/DefaultAuthorityRuleConfigurationBuilder.java
@@ -24,6 +24,7 @@ import
org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import
org.apache.shardingsphere.infra.rule.builder.global.DefaultGlobalRuleConfigurationBuilder;
import java.util.Collection;
+import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Properties;
@@ -34,7 +35,7 @@ public final class DefaultAuthorityRuleConfigurationBuilder
implements DefaultGl
@Override
public AuthorityRuleConfiguration build() {
- return new AuthorityRuleConfiguration(createDefaultUsers(), new
AlgorithmConfiguration("ALL_PERMITTED", new Properties()), null);
+ return new AuthorityRuleConfiguration(createDefaultUsers(), new
AlgorithmConfiguration("ALL_PERMITTED", new Properties()), new
LinkedHashMap<>(), null);
}
private Collection<ShardingSphereUser> createDefaultUsers() {
diff --git
a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapper.java
b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapper.java
index 6e2d0408dd5..62f61af847d 100644
---
a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapper.java
+++
b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapper.java
@@ -32,7 +32,10 @@ import
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlGlobalRul
import java.util.Collection;
import java.util.Collections;
+import java.util.Map;
+import java.util.Map.Entry;
import java.util.Optional;
+import java.util.stream.Collectors;
/**
* TODO Rename YamlAuthorityRuleConfigurationSwapper when metadata structure
adjustment completed. #25485
@@ -52,9 +55,7 @@ public final class NewYamlAuthorityRuleConfigurationSwapper
implements NewYamlGl
result.setPrivilege(algorithmSwapper.swapToYamlConfiguration(data.getAuthorityProvider()));
result.setUsers(YamlUsersConfigurationConverter.convertToYamlUserConfiguration(data.getUsers()));
result.setDefaultAuthenticator(data.getDefaultAuthenticator());
- if (!data.getAuthenticators().isEmpty()) {
- data.getAuthenticators().forEach((key, value) ->
result.getAuthenticators().put(key,
algorithmSwapper.swapToYamlConfiguration(value)));
- }
+ data.getAuthenticators().forEach((key, value) ->
result.getAuthenticators().put(key,
algorithmSwapper.swapToYamlConfiguration(value)));
return result;
}
@@ -76,9 +77,9 @@ public final class NewYamlAuthorityRuleConfigurationSwapper
implements NewYamlGl
if (null == provider) {
provider = new
DefaultAuthorityRuleConfigurationBuilder().build().getAuthorityProvider();
}
- AuthorityRuleConfiguration result = new
AuthorityRuleConfiguration(users, provider,
yamlConfig.getDefaultAuthenticator());
- yamlConfig.getAuthenticators().forEach((key, value) ->
result.getAuthenticators().put(key, algorithmSwapper.swapToObject(value)));
- return result;
+ Map<String, AlgorithmConfiguration> authenticators =
yamlConfig.getAuthenticators().entrySet().stream()
+ .collect(Collectors.toMap(Entry::getKey, entry ->
algorithmSwapper.swapToObject(entry.getValue())));
+ return new AuthorityRuleConfiguration(users, provider, authenticators,
yamlConfig.getDefaultAuthenticator());
}
@Override
diff --git
a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/YamlAuthorityRuleConfigurationSwapper.java
b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/YamlAuthorityRuleConfigurationSwapper.java
index ed2c0bf7d2f..4ede2fe52c4 100644
---
a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/YamlAuthorityRuleConfigurationSwapper.java
+++
b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/yaml/swapper/YamlAuthorityRuleConfigurationSwapper.java
@@ -28,6 +28,9 @@ import
org.apache.shardingsphere.infra.yaml.config.swapper.algorithm.YamlAlgorit
import
org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper;
import java.util.Collection;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.stream.Collectors;
/**
* YAML Authority rule configuration swapper.
@@ -42,9 +45,7 @@ public final class YamlAuthorityRuleConfigurationSwapper
implements YamlRuleConf
result.setPrivilege(algorithmSwapper.swapToYamlConfiguration(data.getAuthorityProvider()));
result.setUsers(YamlUsersConfigurationConverter.convertToYamlUserConfiguration(data.getUsers()));
result.setDefaultAuthenticator(data.getDefaultAuthenticator());
- if (!data.getAuthenticators().isEmpty()) {
- data.getAuthenticators().forEach((key, value) ->
result.getAuthenticators().put(key,
algorithmSwapper.swapToYamlConfiguration(value)));
- }
+ data.getAuthenticators().forEach((key, value) ->
result.getAuthenticators().put(key,
algorithmSwapper.swapToYamlConfiguration(value)));
return result;
}
@@ -55,9 +56,9 @@ public final class YamlAuthorityRuleConfigurationSwapper
implements YamlRuleConf
if (null == provider) {
provider = new
DefaultAuthorityRuleConfigurationBuilder().build().getAuthorityProvider();
}
- AuthorityRuleConfiguration result = new
AuthorityRuleConfiguration(users, provider,
yamlConfig.getDefaultAuthenticator());
- yamlConfig.getAuthenticators().forEach((key, value) ->
result.getAuthenticators().put(key, algorithmSwapper.swapToObject(value)));
- return result;
+ Map<String, AlgorithmConfiguration> authenticators =
yamlConfig.getAuthenticators().entrySet().stream()
+ .collect(Collectors.toMap(Entry::getKey, entry ->
algorithmSwapper.swapToObject(entry.getValue())));
+ return new AuthorityRuleConfiguration(users, provider, authenticators,
yamlConfig.getDefaultAuthenticator());
}
@Override
diff --git
a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/checker/AuthorityCheckerTest.java
b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/checker/AuthorityCheckerTest.java
index 6cb66c929bb..fa7131afaa8 100644
---
a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/checker/AuthorityCheckerTest.java
+++
b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/checker/AuthorityCheckerTest.java
@@ -39,14 +39,14 @@ class AuthorityCheckerTest {
@Test
void assertCheckIsAuthorizedDatabase() {
Collection<ShardingSphereUser> users = Collections.singleton(new
ShardingSphereUser("root", "", "localhost"));
- AuthorityRuleConfiguration ruleConfig = new
AuthorityRuleConfiguration(users, new AlgorithmConfiguration("FIXTURE", new
Properties()), null);
+ AuthorityRuleConfiguration ruleConfig = new
AuthorityRuleConfiguration(users, new AlgorithmConfiguration("FIXTURE", new
Properties()), Collections.emptyMap(), null);
assertTrue(new AuthorityChecker(new AuthorityRule(ruleConfig), new
Grantee("root", "localhost")).isAuthorized("db0"));
}
@Test
void assertCheckPrivileges() {
Collection<ShardingSphereUser> users = Collections.singleton(new
ShardingSphereUser("root", "", "localhost"));
- AuthorityRule rule = new AuthorityRule(new
AuthorityRuleConfiguration(users, new AlgorithmConfiguration("FIXTURE", new
Properties()), null));
+ AuthorityRule rule = new AuthorityRule(new
AuthorityRuleConfiguration(users, new AlgorithmConfiguration("FIXTURE", new
Properties()), Collections.emptyMap(), null));
AuthorityChecker authorityChecker = new AuthorityChecker(rule, new
Grantee("root", "localhost"));
authorityChecker.checkPrivileges(null, mock(SelectStatement.class));
authorityChecker.checkPrivileges(null, mock(InsertStatement.class));
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 b5cdd02cb46..39186ae0373 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,6 +24,7 @@ import
org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.junit.jupiter.api.Test;
import java.util.Collection;
+import java.util.Collections;
import java.util.LinkedList;
import java.util.Optional;
import java.util.Properties;
@@ -58,7 +59,7 @@ class AuthorityRuleTest {
Collection<ShardingSphereUser> users = new LinkedList<>();
users.add(new ShardingSphereUser("root", "root", "localhost"));
users.add(new ShardingSphereUser("admin", "123456", "localhost"));
- AuthorityRuleConfiguration ruleConfig = new
AuthorityRuleConfiguration(users, new AlgorithmConfiguration("FIXTURE", new
Properties()), null);
+ AuthorityRuleConfiguration ruleConfig = new
AuthorityRuleConfiguration(users, new AlgorithmConfiguration("FIXTURE", new
Properties()), Collections.emptyMap(), null);
return new AuthorityRule(ruleConfig);
}
}
diff --git
a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapperTest.java
b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapperTest.java
index 8b890df82da..2c5ef8f5112 100644
---
a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapperTest.java
+++
b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/NewYamlAuthorityRuleConfigurationSwapperTest.java
@@ -38,7 +38,7 @@ class NewYamlAuthorityRuleConfigurationSwapperTest {
@Test
void assertSwapToDataNodes() {
Collection<ShardingSphereUser> users = Collections.singleton(new
ShardingSphereUser("root", "", "localhost"));
- Collection<YamlDataNode> actual = swapper.swapToDataNodes(new
AuthorityRuleConfiguration(users, new AlgorithmConfiguration("ALL_PERMITTED",
new Properties()), null));
+ Collection<YamlDataNode> actual = swapper.swapToDataNodes(new
AuthorityRuleConfiguration(users, new AlgorithmConfiguration("ALL_PERMITTED",
new Properties()), Collections.emptyMap(), null));
assertThat(actual.iterator().next().getKey(), is("authority"));
}
}
diff --git
a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/YamlAuthorityRuleConfigurationSwapperTest.java
b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/YamlAuthorityRuleConfigurationSwapperTest.java
index 2203e49020d..008f5a887e6 100644
---
a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/YamlAuthorityRuleConfigurationSwapperTest.java
+++
b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/yaml/swapper/YamlAuthorityRuleConfigurationSwapperTest.java
@@ -38,7 +38,7 @@ class YamlAuthorityRuleConfigurationSwapperTest {
@Test
void assertSwapToYamlConfiguration() {
- AuthorityRuleConfiguration authorityRuleConfig = new
AuthorityRuleConfiguration(Collections.emptyList(), new
AlgorithmConfiguration("type", new Properties()), null);
+ AuthorityRuleConfiguration authorityRuleConfig = new
AuthorityRuleConfiguration(Collections.emptyList(), new
AlgorithmConfiguration("type", new Properties()), Collections.emptyMap(), null);
YamlAuthorityRuleConfiguration actual =
swapper.swapToYamlConfiguration(authorityRuleConfig);
assertTrue(actual.getUsers().isEmpty());
assertNotNull(actual.getPrivilege());
diff --git
a/kernel/authority/distsql/handler/src/test/java/org/apache/shardingsphere/authority/distsql/handler/ShowAuthorityRuleExecutorTest.java
b/kernel/authority/distsql/handler/src/test/java/org/apache/shardingsphere/authority/distsql/handler/ShowAuthorityRuleExecutorTest.java
index 14e458c146a..e670463a2e1 100644
---
a/kernel/authority/distsql/handler/src/test/java/org/apache/shardingsphere/authority/distsql/handler/ShowAuthorityRuleExecutorTest.java
+++
b/kernel/authority/distsql/handler/src/test/java/org/apache/shardingsphere/authority/distsql/handler/ShowAuthorityRuleExecutorTest.java
@@ -75,6 +75,6 @@ class ShowAuthorityRuleExecutorTest {
private AuthorityRuleConfiguration createAuthorityRuleConfiguration() {
ShardingSphereUser root = new ShardingSphereUser("root", "",
"localhost");
- return new AuthorityRuleConfiguration(Collections.singleton(root), new
AlgorithmConfiguration("ALL_PERMITTED", new Properties()), null);
+ return new AuthorityRuleConfiguration(Collections.singleton(root), new
AlgorithmConfiguration("ALL_PERMITTED", new Properties()),
Collections.emptyMap(), null);
}
}
diff --git
a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java
index 148f0ff8ca1..0b295e9302b 100644
---
a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java
+++
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ConfigurationChangedSubscriberTest.java
@@ -158,7 +158,7 @@ class ConfigurationChangedSubscriberTest {
}
private Collection<RuleConfiguration> getChangedGlobalRuleConfigurations()
{
- RuleConfiguration authorityRuleConfig = new
AuthorityRuleConfiguration(getShardingSphereUsers(), new
AlgorithmConfiguration("ALL_PERMITTED", new Properties()), null);
+ RuleConfiguration authorityRuleConfig = new
AuthorityRuleConfiguration(getShardingSphereUsers(), new
AlgorithmConfiguration("ALL_PERMITTED", new Properties()),
Collections.emptyMap(), null);
return Collections.singleton(authorityRuleConfig);
}
diff --git
a/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngineTest.java
b/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngineTest.java
index e00539957d8..38586f42028 100644
---
a/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngineTest.java
+++
b/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngineTest.java
@@ -81,7 +81,8 @@ class OpenGaussAuthenticationEngineTest {
}
private RuleMetaData buildGlobalRuleMetaData(final ShardingSphereUser
user) {
- AuthorityRuleConfiguration ruleConfig = new
AuthorityRuleConfiguration(Collections.singleton(user), new
AlgorithmConfiguration("ALL_PERMITTED", new Properties()), null);
+ AuthorityRuleConfiguration ruleConfig = new AuthorityRuleConfiguration(
+ Collections.singleton(user), new
AlgorithmConfiguration("ALL_PERMITTED", new Properties()),
Collections.emptyMap(), null);
return new RuleMetaData(Collections.singleton(new
AuthorityRuleBuilder().build(ruleConfig, Collections.emptyMap(),
mock(ConfigurationProperties.class))));
}
diff --git
a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java
b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java
index 8230d11f6d4..e4d818a12a5 100644
---
a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java
+++
b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java
@@ -204,7 +204,8 @@ class PostgreSQLAuthenticationEngineTest {
}
private RuleMetaData buildGlobalRuleMetaData(final ShardingSphereUser
user) {
- AuthorityRuleConfiguration ruleConfig = new
AuthorityRuleConfiguration(Collections.singletonList(user), new
AlgorithmConfiguration("ALL_PERMITTED", new Properties()), null);
+ AuthorityRuleConfiguration ruleConfig = new AuthorityRuleConfiguration(
+ Collections.singleton(user), new
AlgorithmConfiguration("ALL_PERMITTED", new Properties()),
Collections.emptyMap(), null);
AuthorityRule rule = new AuthorityRuleBuilder().build(ruleConfig,
Collections.emptyMap(), mock(ConfigurationProperties.class));
return new RuleMetaData(Collections.singleton(rule));
}