This is an automated email from the ASF dual-hosted git repository.
sunnianjun 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 8f696c21601 Refactor PrivilegeProvider (#30077)
8f696c21601 is described below
commit 8f696c216011506e7d6e1ff525171b8d07514a67
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Feb 8 17:07:20 2024 +0800
Refactor PrivilegeProvider (#30077)
---
.../apache/shardingsphere/authority/spi/PrivilegeProvider.java | 7 +++----
.../org/apache/shardingsphere/authority/rule/AuthorityRule.java | 2 +-
.../authority/fixture/PrivilegeProviderFixture.java | 6 +++---
.../provider/database/DatabasePermittedPrivilegeProvider.java | 5 +++--
.../database/DatabasePermittedPrivilegeProviderTest.java | 9 +++++++--
.../authority/provider/simple/AllPermittedPrivilegeProvider.java | 5 +++--
.../provider/simple/AllPermittedPrivilegeProviderTest.java | 7 ++++++-
7 files changed, 26 insertions(+), 15 deletions(-)
diff --git
a/kernel/authority/api/src/main/java/org/apache/shardingsphere/authority/spi/PrivilegeProvider.java
b/kernel/authority/api/src/main/java/org/apache/shardingsphere/authority/spi/PrivilegeProvider.java
index c4ff64ce9e5..2b6794ef00c 100644
---
a/kernel/authority/api/src/main/java/org/apache/shardingsphere/authority/spi/PrivilegeProvider.java
+++
b/kernel/authority/api/src/main/java/org/apache/shardingsphere/authority/spi/PrivilegeProvider.java
@@ -17,13 +17,12 @@
package org.apache.shardingsphere.authority.spi;
+import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
-import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;
-import java.util.Collection;
import java.util.Map;
/**
@@ -35,8 +34,8 @@ public interface PrivilegeProvider extends TypedSPI {
/**
* Build grantee and privileges map.
*
- * @param users users
+ * @param ruleConfig authority rule configuration
* @return grantee and privileges map
*/
- Map<Grantee, ShardingSpherePrivileges>
build(Collection<ShardingSphereUser> users);
+ Map<Grantee, ShardingSpherePrivileges> build(AuthorityRuleConfiguration
ruleConfig);
}
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 419dfe8ba77..318669f61e6 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
@@ -42,7 +42,7 @@ public final class AuthorityRule implements GlobalRule {
public AuthorityRule(final AuthorityRuleConfiguration ruleConfig) {
configuration = ruleConfig;
PrivilegeProvider provider =
TypedSPILoader.getService(PrivilegeProvider.class,
ruleConfig.getPrivilegeProvider().getType(),
ruleConfig.getPrivilegeProvider().getProps());
- privileges = provider.build(ruleConfig.getUsers());
+ privileges = provider.build(ruleConfig);
}
/**
diff --git
a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/fixture/PrivilegeProviderFixture.java
b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/fixture/PrivilegeProviderFixture.java
index e41e8251355..6a61c8b04cd 100644
---
a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/fixture/PrivilegeProviderFixture.java
+++
b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/fixture/PrivilegeProviderFixture.java
@@ -17,13 +17,13 @@
package org.apache.shardingsphere.authority.fixture;
+import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
import org.apache.shardingsphere.authority.spi.PrivilegeProvider;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.mockito.Answers;
-import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;
@@ -33,9 +33,9 @@ import static org.mockito.Mockito.withSettings;
public final class PrivilegeProviderFixture implements PrivilegeProvider {
@Override
- public Map<Grantee, ShardingSpherePrivileges> build(final
Collection<ShardingSphereUser> users) {
+ public Map<Grantee, ShardingSpherePrivileges> build(final
AuthorityRuleConfiguration ruleConfig) {
ShardingSpherePrivileges privileges = mockPrivileges();
- return
users.stream().collect(Collectors.toMap(ShardingSphereUser::getGrantee, each ->
privileges));
+ return
ruleConfig.getUsers().stream().collect(Collectors.toMap(ShardingSphereUser::getGrantee,
each -> privileges));
}
private ShardingSpherePrivileges mockPrivileges() {
diff --git
a/kernel/authority/provider/database/src/main/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProvider.java
b/kernel/authority/provider/database/src/main/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProvider.java
index f190ce48698..c51f4b7a183 100644
---
a/kernel/authority/provider/database/src/main/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProvider.java
+++
b/kernel/authority/provider/database/src/main/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProvider.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.authority.provider.database;
import com.google.common.base.Preconditions;
+import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
import org.apache.shardingsphere.authority.spi.PrivilegeProvider;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
@@ -55,9 +56,9 @@ public final class DatabasePermittedPrivilegeProvider
implements PrivilegeProvid
}
@Override
- public Map<Grantee, ShardingSpherePrivileges> build(final
Collection<ShardingSphereUser> users) {
+ public Map<Grantee, ShardingSpherePrivileges> build(final
AuthorityRuleConfiguration ruleConfig) {
Map<ShardingSphereUser, Collection<String>> userDatabaseMappings =
convertUserDatabases();
- return
users.stream().collect(Collectors.toMap(ShardingSphereUser::getGrantee, each ->
new DatabasePermittedPrivileges(getUserDatabases(each, userDatabaseMappings))));
+ return
ruleConfig.getUsers().stream().collect(Collectors.toMap(ShardingSphereUser::getGrantee,
each -> new DatabasePermittedPrivileges(getUserDatabases(each,
userDatabaseMappings))));
}
private Map<ShardingSphereUser, Collection<String>> convertUserDatabases()
{
diff --git
a/kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProviderTest.java
b/kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProviderTest.java
index 8eb81a2b2da..ebba3dcbcd7 100644
---
a/kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProviderTest.java
+++
b/kernel/authority/provider/database/src/test/java/org/apache/shardingsphere/authority/provider/database/DatabasePermittedPrivilegeProviderTest.java
@@ -17,8 +17,10 @@
package org.apache.shardingsphere.authority.provider.database;
+import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
import org.apache.shardingsphere.authority.spi.PrivilegeProvider;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
@@ -27,6 +29,7 @@ import
org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
+import java.util.Collections;
import java.util.Map;
import java.util.Properties;
@@ -34,6 +37,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
class DatabasePermittedPrivilegeProviderTest {
@@ -41,11 +45,12 @@ class DatabasePermittedPrivilegeProviderTest {
void assertBuild() {
Properties props = PropertiesBuilder.build(new
Property("user-database-mappings", "root@localhost=*, [email protected]=sys_db,
user1@=foo_db, user1@=bar_db, user2@=*"));
PrivilegeProvider provider =
TypedSPILoader.getService(PrivilegeProvider.class, "DATABASE_PERMITTED", props);
- Map<Grantee, ShardingSpherePrivileges> actual =
provider.build(Arrays.asList(
+ AuthorityRuleConfiguration ruleConfig = new
AuthorityRuleConfiguration(Arrays.asList(
new ShardingSphereUser("root", "", "localhost"),
new ShardingSphereUser("user1", "", "127.0.0.1"),
new ShardingSphereUser("user1", "", "%"),
- new ShardingSphereUser("user3", "", "%")));
+ new ShardingSphereUser("user3", "", "%")),
mock(AlgorithmConfiguration.class), Collections.emptyMap(), null);
+ Map<Grantee, ShardingSpherePrivileges> actual =
provider.build(ruleConfig);
assertThat(actual.size(), is(4));
assertTrue(actual.get(new Grantee("root",
"localhost")).hasPrivileges("sys_db"));
assertTrue(actual.get(new Grantee("user1",
"127.0.0.1")).hasPrivileges("sys_db"));
diff --git
a/kernel/authority/provider/simple/src/main/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProvider.java
b/kernel/authority/provider/simple/src/main/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProvider.java
index 2785373e5f2..f1f00f05208 100644
---
a/kernel/authority/provider/simple/src/main/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProvider.java
+++
b/kernel/authority/provider/simple/src/main/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProvider.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.authority.provider.simple;
+import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
import org.apache.shardingsphere.authority.spi.PrivilegeProvider;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
@@ -33,8 +34,8 @@ import java.util.stream.Collectors;
public final class AllPermittedPrivilegeProvider implements PrivilegeProvider {
@Override
- public Map<Grantee, ShardingSpherePrivileges> build(final
Collection<ShardingSphereUser> users) {
- return
users.stream().collect(Collectors.toMap(ShardingSphereUser::getGrantee, each ->
new AllPermittedPrivileges()));
+ public Map<Grantee, ShardingSpherePrivileges> build(final
AuthorityRuleConfiguration ruleConfig) {
+ return
ruleConfig.getUsers().stream().collect(Collectors.toMap(ShardingSphereUser::getGrantee,
each -> new AllPermittedPrivileges()));
}
@Override
diff --git
a/kernel/authority/provider/simple/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProviderTest.java
b/kernel/authority/provider/simple/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProviderTest.java
index e40135d95e7..ccfb80aebda 100644
---
a/kernel/authority/provider/simple/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProviderTest.java
+++
b/kernel/authority/provider/simple/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPermittedPrivilegeProviderTest.java
@@ -17,8 +17,10 @@
package org.apache.shardingsphere.authority.provider.simple;
+import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
import org.apache.shardingsphere.authority.spi.PrivilegeProvider;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
@@ -30,12 +32,15 @@ import java.util.Map;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
class AllPermittedPrivilegeProviderTest {
@Test
void assertBuild() {
- Map<Grantee, ShardingSpherePrivileges> actual =
TypedSPILoader.getService(PrivilegeProvider.class,
"ALL_PERMITTED").build(Collections.singleton(new ShardingSphereUser("root@%")));
+ AuthorityRuleConfiguration ruleConfig = new AuthorityRuleConfiguration(
+ Collections.singleton(new ShardingSphereUser("root@%")),
mock(AlgorithmConfiguration.class), Collections.emptyMap(), null);
+ Map<Grantee, ShardingSpherePrivileges> actual =
TypedSPILoader.getService(PrivilegeProvider.class,
"ALL_PERMITTED").build(ruleConfig);
assertThat(actual.size(), is(1));
assertThat(actual.get(new Grantee("root", "%")),
instanceOf(AllPermittedPrivileges.class));
}