You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/05/09 08:21:14 UTC
[shardingsphere] branch master updated: Refactor AuthorityProviderAlgorithm (#17476)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 1f9eee80c18 Refactor AuthorityProviderAlgorithm (#17476)
1f9eee80c18 is described below
commit 1f9eee80c18d7e98cbf90badb33bdb136c2dc134
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon May 9 16:21:07 2022 +0800
Refactor AuthorityProviderAlgorithm (#17476)
---
.../AuthorityProviderAlgorithmFactory.java | 3 +-
.../AuthorityRegistry.java} | 30 +++----------------
.../authority/spi/AuthorityProviderAlgorithm.java | 25 +++-------------
.../natived/NativeAuthorityProviderAlgorithm.java | 22 +++-----------
...vilegesPermittedAuthorityProviderAlgorithm.java | 22 +++-----------
...vilegesPermittedAuthorityProviderAlgorithm.java | 18 ++++-------
.../registry/AllPermittedAuthorityRegistry.java} | 26 ++++++++--------
.../UserPrivilegeMapAuthorityRegistry.java} | 29 +++++++++---------
.../authority/rule/AuthorityRule.java | 35 ++++++++++++----------
.../NativeAuthorityProviderAlgorithmTest.java | 19 ++----------
...esPermittedAuthorityProviderAlgorithmTest.java} | 16 +++++-----
...gesPermittedAuthorityProviderAlgorithmTest.java | 22 ++++----------
.../MySQLAuthenticationHandlerTest.java | 26 +++++++---------
13 files changed, 94 insertions(+), 199 deletions(-)
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/AuthorityProviderAlgorithmFactory.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/factory/AuthorityProviderAlgorithmFactory.java
similarity index 93%
rename from shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/AuthorityProviderAlgorithmFactory.java
rename to shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/factory/AuthorityProviderAlgorithmFactory.java
index edddfdc2069..f046067e893 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/AuthorityProviderAlgorithmFactory.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/factory/AuthorityProviderAlgorithmFactory.java
@@ -15,10 +15,11 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.authority.spi;
+package org.apache.shardingsphere.authority.factory;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.authority.spi.AuthorityProviderAlgorithm;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmFactory;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/AuthorityProviderAlgorithm.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/model/AuthorityRegistry.java
similarity index 52%
copy from shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/AuthorityProviderAlgorithm.java
copy to shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/model/AuthorityRegistry.java
index 16949b0e45c..4bdb833e8c1 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/AuthorityProviderAlgorithm.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/model/AuthorityRegistry.java
@@ -15,38 +15,16 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.authority.spi;
+package org.apache.shardingsphere.authority.model;
-import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
-import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithm;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
-import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
-import java.util.Collection;
-import java.util.Map;
import java.util.Optional;
/**
- * Authority provide algorithm.
-*/
-public interface AuthorityProviderAlgorithm extends ShardingSphereAlgorithm {
-
- /**
- * Initialize authority.
- *
- * @param metaDataMap meta data map
- * @param users users
- */
- void init(Map<String, ShardingSphereMetaData> metaDataMap, Collection<ShardingSphereUser> users);
-
- /**
- * Refresh authority.
- *
- * @param metaDataMap meta data map
- * @param users users
- */
- void refresh(Map<String, ShardingSphereMetaData> metaDataMap, Collection<ShardingSphereUser> users);
+ * Authority registry.
+ */
+public interface AuthorityRegistry {
/**
* Find Privileges.
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/AuthorityProviderAlgorithm.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/AuthorityProviderAlgorithm.java
index 16949b0e45c..d0361f32728 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/AuthorityProviderAlgorithm.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/AuthorityProviderAlgorithm.java
@@ -17,15 +17,13 @@
package org.apache.shardingsphere.authority.spi;
-import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
+import org.apache.shardingsphere.authority.model.AuthorityRegistry;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithm;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import java.util.Collection;
import java.util.Map;
-import java.util.Optional;
/**
* Authority provide algorithm.
@@ -33,26 +31,11 @@ import java.util.Optional;
public interface AuthorityProviderAlgorithm extends ShardingSphereAlgorithm {
/**
- * Initialize authority.
+ * Build authority registry.
*
* @param metaDataMap meta data map
* @param users users
+ * @return authority registry
*/
- void init(Map<String, ShardingSphereMetaData> metaDataMap, Collection<ShardingSphereUser> users);
-
- /**
- * Refresh authority.
- *
- * @param metaDataMap meta data map
- * @param users users
- */
- void refresh(Map<String, ShardingSphereMetaData> metaDataMap, Collection<ShardingSphereUser> users);
-
- /**
- * Find Privileges.
- *
- * @param grantee grantee
- * @return found privileges
- */
- Optional<ShardingSpherePrivileges> findPrivileges(Grantee grantee);
+ AuthorityRegistry buildAuthorityRegistry(Map<String, ShardingSphereMetaData> metaDataMap, Collection<ShardingSphereUser> users);
}
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/NativeAuthorityProviderAlgorithm.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/NativeAuthorityProviderAlgorithm.java
index 5ccc7388035..82ad14af27c 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/NativeAuthorityProviderAlgorithm.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/NativeAuthorityProviderAlgorithm.java
@@ -18,19 +18,17 @@
package org.apache.shardingsphere.authority.provider.natived;
import lombok.Getter;
-import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
+import org.apache.shardingsphere.authority.model.AuthorityRegistry;
import org.apache.shardingsphere.authority.provider.natived.builder.StoragePrivilegeBuilder;
+import org.apache.shardingsphere.authority.registry.UserPrivilegeMapAuthorityRegistry;
import org.apache.shardingsphere.authority.spi.AuthorityProviderAlgorithm;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
-import java.util.Optional;
import java.util.Properties;
-import java.util.concurrent.ConcurrentHashMap;
/**
* Native authority provide algorithm.
@@ -43,26 +41,14 @@ public final class NativeAuthorityProviderAlgorithm implements AuthorityProvider
@Getter
private Properties props;
- private final Map<ShardingSphereUser, ShardingSpherePrivileges> userPrivilegeMap = new ConcurrentHashMap<>();
-
@Override
public void init(final Properties props) {
this.props = props;
}
@Override
- public void init(final Map<String, ShardingSphereMetaData> metaDataMap, final Collection<ShardingSphereUser> users) {
- userPrivilegeMap.putAll(StoragePrivilegeBuilder.build(new LinkedList<>(metaDataMap.values()), users));
- }
-
- @Override
- public void refresh(final Map<String, ShardingSphereMetaData> metaDataMap, final Collection<ShardingSphereUser> users) {
- userPrivilegeMap.putAll(StoragePrivilegeBuilder.build(new LinkedList<>(metaDataMap.values()), users));
- }
-
- @Override
- public Optional<ShardingSpherePrivileges> findPrivileges(final Grantee grantee) {
- return userPrivilegeMap.keySet().stream().filter(each -> each.getGrantee().equals(grantee)).findFirst().map(userPrivilegeMap::get);
+ public AuthorityRegistry buildAuthorityRegistry(final Map<String, ShardingSphereMetaData> metaDataMap, final Collection<ShardingSphereUser> users) {
+ return new UserPrivilegeMapAuthorityRegistry(StoragePrivilegeBuilder.build(new LinkedList<>(metaDataMap.values()), users));
}
@Override
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/schema/SchemaPrivilegesPermittedAuthorityProviderAlgorithm.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/schema/SchemaPrivilegesPermittedAuthorityProviderAlgorithm.java
index 720728f14cd..523ec2b5c67 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/schema/SchemaPrivilegesPermittedAuthorityProviderAlgorithm.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/schema/SchemaPrivilegesPermittedAuthorityProviderAlgorithm.java
@@ -18,18 +18,16 @@
package org.apache.shardingsphere.authority.provider.schema;
import lombok.Getter;
-import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
+import org.apache.shardingsphere.authority.model.AuthorityRegistry;
import org.apache.shardingsphere.authority.provider.schema.builder.SchemaPrivilegeBuilder;
+import org.apache.shardingsphere.authority.registry.UserPrivilegeMapAuthorityRegistry;
import org.apache.shardingsphere.authority.spi.AuthorityProviderAlgorithm;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import java.util.Collection;
import java.util.Map;
-import java.util.Optional;
import java.util.Properties;
-import java.util.concurrent.ConcurrentHashMap;
/**
* Schema privileges permitted authority provider algorithm.
@@ -41,26 +39,14 @@ public final class SchemaPrivilegesPermittedAuthorityProviderAlgorithm implement
@Getter
private Properties props;
- private final Map<ShardingSphereUser, ShardingSpherePrivileges> userPrivilegeMap = new ConcurrentHashMap<>();
-
@Override
public void init(final Properties props) {
this.props = props;
}
@Override
- public void init(final Map<String, ShardingSphereMetaData> metaDataMap, final Collection<ShardingSphereUser> users) {
- userPrivilegeMap.putAll(SchemaPrivilegeBuilder.build(users, props));
- }
-
- @Override
- public void refresh(final Map<String, ShardingSphereMetaData> metaDataMap, final Collection<ShardingSphereUser> users) {
- userPrivilegeMap.putAll(SchemaPrivilegeBuilder.build(users, props));
- }
-
- @Override
- public Optional<ShardingSpherePrivileges> findPrivileges(final Grantee grantee) {
- return userPrivilegeMap.keySet().stream().filter(each -> each.getGrantee().equals(grantee)).findFirst().map(userPrivilegeMap::get);
+ public AuthorityRegistry buildAuthorityRegistry(final Map<String, ShardingSphereMetaData> metaDataMap, final Collection<ShardingSphereUser> users) {
+ return new UserPrivilegeMapAuthorityRegistry(SchemaPrivilegeBuilder.build(users, props));
}
@Override
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/simple/AllPrivilegesPermittedAuthorityProviderAlgorithm.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/simple/AllPrivilegesPermittedAuthorityProviderAlgorithm.java
index 8c40c53d0d7..1704843483c 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/simple/AllPrivilegesPermittedAuthorityProviderAlgorithm.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/simple/AllPrivilegesPermittedAuthorityProviderAlgorithm.java
@@ -19,16 +19,16 @@ package org.apache.shardingsphere.authority.provider.simple;
import lombok.Getter;
import org.apache.shardingsphere.authority.model.AccessSubject;
+import org.apache.shardingsphere.authority.model.AuthorityRegistry;
import org.apache.shardingsphere.authority.model.PrivilegeType;
import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
+import org.apache.shardingsphere.authority.registry.AllPermittedAuthorityRegistry;
import org.apache.shardingsphere.authority.spi.AuthorityProviderAlgorithm;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import java.util.Collection;
import java.util.Map;
-import java.util.Optional;
import java.util.Properties;
/**
@@ -47,16 +47,8 @@ public final class AllPrivilegesPermittedAuthorityProviderAlgorithm implements A
}
@Override
- public void init(final Map<String, ShardingSphereMetaData> metaDataMap, final Collection<ShardingSphereUser> users) {
- }
-
- @Override
- public void refresh(final Map<String, ShardingSphereMetaData> metaDataMap, final Collection<ShardingSphereUser> users) {
- }
-
- @Override
- public Optional<ShardingSpherePrivileges> findPrivileges(final Grantee grantee) {
- return Optional.of(INSTANCE);
+ public AuthorityRegistry buildAuthorityRegistry(final Map<String, ShardingSphereMetaData> metaDataMap, final Collection<ShardingSphereUser> users) {
+ return new AllPermittedAuthorityRegistry();
}
@Override
@@ -64,7 +56,7 @@ public final class AllPrivilegesPermittedAuthorityProviderAlgorithm implements A
return "ALL_PRIVILEGES_PERMITTED";
}
- private static final class AllPrivilegesPermittedShardingSpherePrivileges implements ShardingSpherePrivileges {
+ public static final class AllPrivilegesPermittedShardingSpherePrivileges implements ShardingSpherePrivileges {
@Override
public void setSuperPrivilege() {
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPrivilegesProviderAlgorithmTest.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/registry/AllPermittedAuthorityRegistry.java
similarity index 54%
copy from shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPrivilegesProviderAlgorithmTest.java
copy to shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/registry/AllPermittedAuthorityRegistry.java
index 7f09f5fb791..f01a7292310 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPrivilegesProviderAlgorithmTest.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/registry/AllPermittedAuthorityRegistry.java
@@ -15,26 +15,24 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.authority.provider.simple;
+package org.apache.shardingsphere.authority.registry;
import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
-import org.apache.shardingsphere.authority.provider.natived.model.subject.SchemaAccessSubject;
+import org.apache.shardingsphere.authority.provider.simple.AllPrivilegesPermittedAuthorityProviderAlgorithm.AllPrivilegesPermittedShardingSpherePrivileges;
+import org.apache.shardingsphere.authority.model.AuthorityRegistry;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
-import org.junit.Test;
-import java.util.Collections;
import java.util.Optional;
-import static org.junit.Assert.assertTrue;
-
-public final class AllPrivilegesProviderAlgorithmTest {
+/**
+ * All permitted authority registry.
+ */
+public final class AllPermittedAuthorityRegistry implements AuthorityRegistry {
+
+ private static final ShardingSpherePrivileges INSTANCE = new AllPrivilegesPermittedShardingSpherePrivileges();
- @Test
- public void assertFindPrivileges() {
- Optional<ShardingSpherePrivileges> actual = new AllPrivilegesPermittedAuthorityProviderAlgorithm().findPrivileges(new Grantee("TestUser", "testHost"));
- assertTrue(actual.isPresent());
- assertTrue(actual.get().hasPrivileges("testSchema"));
- assertTrue(actual.get().hasPrivileges(Collections.emptyList()));
- assertTrue(actual.get().hasPrivileges(new SchemaAccessSubject("testSchema"), Collections.emptyList()));
+ @Override
+ public Optional<ShardingSpherePrivileges> findPrivileges(final Grantee grantee) {
+ return Optional.of(INSTANCE);
}
}
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPrivilegesProviderAlgorithmTest.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/registry/UserPrivilegeMapAuthorityRegistry.java
similarity index 54%
copy from shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPrivilegesProviderAlgorithmTest.java
copy to shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/registry/UserPrivilegeMapAuthorityRegistry.java
index 7f09f5fb791..979a00e078f 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPrivilegesProviderAlgorithmTest.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/registry/UserPrivilegeMapAuthorityRegistry.java
@@ -15,26 +15,27 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.authority.provider.simple;
+package org.apache.shardingsphere.authority.registry;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.authority.model.AuthorityRegistry;
import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
-import org.apache.shardingsphere.authority.provider.natived.model.subject.SchemaAccessSubject;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
-import org.junit.Test;
+import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
-import java.util.Collections;
+import java.util.Map;
import java.util.Optional;
-import static org.junit.Assert.assertTrue;
-
-public final class AllPrivilegesProviderAlgorithmTest {
+/**
+ * User privilege map authority registry.
+ */
+@RequiredArgsConstructor
+public final class UserPrivilegeMapAuthorityRegistry implements AuthorityRegistry {
+
+ private final Map<ShardingSphereUser, ? extends ShardingSpherePrivileges> userPrivilegeMap;
- @Test
- public void assertFindPrivileges() {
- Optional<ShardingSpherePrivileges> actual = new AllPrivilegesPermittedAuthorityProviderAlgorithm().findPrivileges(new Grantee("TestUser", "testHost"));
- assertTrue(actual.isPresent());
- assertTrue(actual.get().hasPrivileges("testSchema"));
- assertTrue(actual.get().hasPrivileges(Collections.emptyList()));
- assertTrue(actual.get().hasPrivileges(new SchemaAccessSubject("testSchema"), Collections.emptyList()));
+ @Override
+ public Optional<ShardingSpherePrivileges> findPrivileges(final Grantee grantee) {
+ return userPrivilegeMap.keySet().stream().filter(each -> each.getGrantee().equals(grantee)).findFirst().map(userPrivilegeMap::get);
}
}
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
index 34a9399d773..de45655d158 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
@@ -19,8 +19,9 @@ package org.apache.shardingsphere.authority.rule;
import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
+import org.apache.shardingsphere.authority.model.AuthorityRegistry;
import org.apache.shardingsphere.authority.spi.AuthorityProviderAlgorithm;
-import org.apache.shardingsphere.authority.spi.AuthorityProviderAlgorithmFactory;
+import org.apache.shardingsphere.authority.factory.AuthorityProviderAlgorithmFactory;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
@@ -35,14 +36,25 @@ import java.util.Optional;
*/
public final class AuthorityRule implements GlobalRule {
+ private final Collection<ShardingSphereUser> users;
+
private final AuthorityProviderAlgorithm provider;
- private final Collection<ShardingSphereUser> users;
+ private volatile AuthorityRegistry authorityRegistry;
public AuthorityRule(final AuthorityRuleConfiguration config, final Map<String, ShardingSphereMetaData> metaDataMap) {
- provider = AuthorityProviderAlgorithmFactory.newInstance(config.getProvider());
- provider.init(metaDataMap, config.getUsers());
users = config.getUsers();
+ provider = AuthorityProviderAlgorithmFactory.newInstance(config.getProvider());
+ authorityRegistry = provider.buildAuthorityRegistry(metaDataMap, config.getUsers());
+ }
+
+ /**
+ * Find user.
+ * @param grantee grantee user
+ * @return user
+ */
+ public Optional<ShardingSphereUser> findUser(final Grantee grantee) {
+ return users.stream().filter(each -> each.getGrantee().equals(grantee)).findFirst();
}
/**
@@ -52,7 +64,7 @@ public final class AuthorityRule implements GlobalRule {
* @return found privileges
*/
public Optional<ShardingSpherePrivileges> findPrivileges(final Grantee grantee) {
- return provider.findPrivileges(grantee);
+ return authorityRegistry.findPrivileges(grantee);
}
/**
@@ -61,17 +73,8 @@ public final class AuthorityRule implements GlobalRule {
* @param metaDataMap meta data map
* @param users users
*/
- public void refresh(final Map<String, ShardingSphereMetaData> metaDataMap, final Collection<ShardingSphereUser> users) {
- provider.refresh(metaDataMap, users);
- }
-
- /**
- * Find user.
- * @param grantee grantee user
- * @return user
- */
- public Optional<ShardingSphereUser> findUser(final Grantee grantee) {
- return users.stream().filter(each -> each.getGrantee().equals(grantee)).findFirst();
+ public synchronized void refresh(final Map<String, ShardingSphereMetaData> metaDataMap, final Collection<ShardingSphereUser> users) {
+ authorityRegistry = provider.buildAuthorityRegistry(metaDataMap, users);
}
@Override
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/NativeAuthorityProviderAlgorithmTest.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/NativeAuthorityProviderAlgorithmTest.java
index a9fa3b3a52f..01f7e8b5ea5 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/NativeAuthorityProviderAlgorithmTest.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/NativeAuthorityProviderAlgorithmTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.authority.provider.natived;
+import org.apache.shardingsphere.authority.model.AuthorityRegistry;
import org.apache.shardingsphere.authority.model.PrivilegeType;
import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
@@ -47,26 +48,12 @@ public final class NativeAuthorityProviderAlgorithmTest {
ShardingSphereUser root = new ShardingSphereUser("root", "", "localhost");
users.add(root);
ShardingSphereMetaData metaData = mockShardingSphereMetaData(users);
- algorithm.init(Collections.singletonMap("db0", metaData), users);
- Optional<ShardingSpherePrivileges> privileges = algorithm.findPrivileges(new Grantee("root", "localhost"));
+ AuthorityRegistry authorityRegistry = algorithm.buildAuthorityRegistry(Collections.singletonMap("db0", metaData), users);
+ Optional<ShardingSpherePrivileges> privileges = authorityRegistry.findPrivileges(new Grantee("root", "localhost"));
assertTrue(privileges.isPresent());
assertPrivilege(privileges.get());
}
- @Test
- public void assertRefreshPrivileges() throws SQLException {
- NativeAuthorityProviderAlgorithm algorithm = new NativeAuthorityProviderAlgorithm();
- Collection<ShardingSphereUser> users = Collections.singletonList(new ShardingSphereUser("root", "", "localhost"));
- algorithm.init(Collections.emptyMap(), users);
- Optional<ShardingSpherePrivileges> privileges1 = algorithm.findPrivileges(new Grantee("root", "localhost"));
- assertTrue(privileges1.isPresent());
- assertTrue(privileges1.get().hasPrivileges(Collections.singletonList(PrivilegeType.SUPER)));
- algorithm.refresh(Collections.singletonMap("db0", mockShardingSphereMetaData(users)), users);
- Optional<ShardingSpherePrivileges> privileges2 = algorithm.findPrivileges(new Grantee("root", "localhost"));
- assertTrue(privileges2.isPresent());
- assertPrivilege(privileges2.get());
- }
-
private void assertPrivilege(final ShardingSpherePrivileges privileges) {
Collection<PrivilegeType> expected = new LinkedList<>();
expected.add(PrivilegeType.SUPER);
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPrivilegesProviderAlgorithmTest.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPrivilegesPermittedAuthorityProviderAlgorithmTest.java
similarity index 66%
rename from shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPrivilegesProviderAlgorithmTest.java
rename to shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPrivilegesPermittedAuthorityProviderAlgorithmTest.java
index 7f09f5fb791..e6a4e81ca15 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPrivilegesProviderAlgorithmTest.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/simple/AllPrivilegesPermittedAuthorityProviderAlgorithmTest.java
@@ -19,22 +19,20 @@ package org.apache.shardingsphere.authority.provider.simple;
import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
import org.apache.shardingsphere.authority.provider.natived.model.subject.SchemaAccessSubject;
-import org.apache.shardingsphere.infra.metadata.user.Grantee;
+import org.apache.shardingsphere.authority.provider.simple.AllPrivilegesPermittedAuthorityProviderAlgorithm.AllPrivilegesPermittedShardingSpherePrivileges;
import org.junit.Test;
import java.util.Collections;
-import java.util.Optional;
import static org.junit.Assert.assertTrue;
-public final class AllPrivilegesProviderAlgorithmTest {
+public final class AllPrivilegesPermittedAuthorityProviderAlgorithmTest {
@Test
public void assertFindPrivileges() {
- Optional<ShardingSpherePrivileges> actual = new AllPrivilegesPermittedAuthorityProviderAlgorithm().findPrivileges(new Grantee("TestUser", "testHost"));
- assertTrue(actual.isPresent());
- assertTrue(actual.get().hasPrivileges("testSchema"));
- assertTrue(actual.get().hasPrivileges(Collections.emptyList()));
- assertTrue(actual.get().hasPrivileges(new SchemaAccessSubject("testSchema"), Collections.emptyList()));
- }
+ ShardingSpherePrivileges actual = new AllPrivilegesPermittedShardingSpherePrivileges();
+ assertTrue(actual.hasPrivileges("testSchema"));
+ assertTrue(actual.hasPrivileges(Collections.emptyList()));
+ assertTrue(actual.hasPrivileges(new SchemaAccessSubject("testSchema"), Collections.emptyList()));
+ }
}
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/simple/SchemaPrivilegesPermittedAuthorityProviderAlgorithmTest.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/simple/SchemaPrivilegesPermittedAuthorityProviderAlgorithmTest.java
index f6040759b29..c0b0ff4e35a 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/simple/SchemaPrivilegesPermittedAuthorityProviderAlgorithmTest.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/simple/SchemaPrivilegesPermittedAuthorityProviderAlgorithmTest.java
@@ -17,9 +17,10 @@
package org.apache.shardingsphere.authority.provider.simple;
+import org.apache.shardingsphere.authority.factory.AuthorityProviderAlgorithmFactory;
+import org.apache.shardingsphere.authority.model.AuthorityRegistry;
import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
import org.apache.shardingsphere.authority.provider.schema.SchemaPrivilegesPermittedAuthorityProviderAlgorithm;
-import org.apache.shardingsphere.authority.spi.AuthorityProviderAlgorithmFactory;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
@@ -34,27 +35,14 @@ import static org.junit.Assert.assertTrue;
public final class SchemaPrivilegesPermittedAuthorityProviderAlgorithmTest {
@Test
- public void assertFindPrivileges() {
+ public void assertBuildAuthorityRegistry() {
SchemaPrivilegesPermittedAuthorityProviderAlgorithm algorithm = createAuthorityProviderAlgorithm();
- algorithm.init(Collections.emptyMap(), Collections.singletonList(new ShardingSphereUser("user1", "", "127.0.0.2")));
- Optional<ShardingSpherePrivileges> privileges = algorithm.findPrivileges(new Grantee("user1", "127.0.0.2"));
+ AuthorityRegistry actual = algorithm.buildAuthorityRegistry(Collections.emptyMap(), Collections.singletonList(new ShardingSphereUser("user1", "", "127.0.0.2")));
+ Optional<ShardingSpherePrivileges> privileges = actual.findPrivileges(new Grantee("user1", "127.0.0.2"));
assertTrue(privileges.isPresent());
assertTrue(privileges.get().hasPrivileges("test"));
}
- @Test
- public void assertRefreshPrivileges() {
- SchemaPrivilegesPermittedAuthorityProviderAlgorithm algorithm = createAuthorityProviderAlgorithm();
- algorithm.init(Collections.emptyMap(), Collections.singletonList(new ShardingSphereUser("root", "", "localhost")));
- Optional<ShardingSpherePrivileges> privileges1 = algorithm.findPrivileges(new Grantee("root", "localhost"));
- assertTrue(privileges1.isPresent());
- assertTrue(privileges1.get().hasPrivileges("test"));
- algorithm.refresh(Collections.emptyMap(), Collections.singletonList(new ShardingSphereUser("user1", "", "127.0.0.1")));
- Optional<ShardingSpherePrivileges> privileges2 = algorithm.findPrivileges(new Grantee("user1", "127.0.0.1"));
- assertTrue(privileges2.isPresent());
- assertTrue(privileges2.get().hasPrivileges("test1"));
- }
-
private SchemaPrivilegesPermittedAuthorityProviderAlgorithm createAuthorityProviderAlgorithm() {
return (SchemaPrivilegesPermittedAuthorityProviderAlgorithm) AuthorityProviderAlgorithmFactory.newInstance(
new ShardingSphereAlgorithmConfiguration("SCHEMA_PRIVILEGES_PERMITTED", createProperties()));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationHandlerTest.java
index 51f7f1dde7f..9369f4407e3 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationHandlerTest.java
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.proxy.frontend.mysql.authentication;
import com.google.common.primitives.Bytes;
import lombok.SneakyThrows;
import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
-import org.apache.shardingsphere.authority.provider.natived.NativeAuthorityProviderAlgorithm;
+import org.apache.shardingsphere.authority.model.AuthorityRegistry;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.authority.rule.builder.AuthorityRuleBuilder;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLAuthenticationMethod;
@@ -85,28 +85,28 @@ public final class MySQLAuthenticationHandlerTest {
@Test
public void assertLoginWithPassword() {
- setAuthority(new ShardingSphereUser("root", "root", ""));
+ initProxyContext(new ShardingSphereUser("root", "root", ""), true);
byte[] authResponse = {-27, 89, -20, -27, 65, -120, -64, -101, 86, -100, -108, -100, 6, -125, -37, 117, 14, -43, 95, -113};
assertFalse(authenticationHandler.login("root", "", authResponse, "db1").isPresent());
}
@Test
public void assertLoginWithAbsentUser() {
- setAuthority(new ShardingSphereUser("root", "root", ""));
+ initProxyContext(new ShardingSphereUser("root", "root", ""), true);
byte[] authResponse = {-27, 89, -20, -27, 65, -120, -64, -101, 86, -100, -108, -100, 6, -125, -37, 117, 14, -43, 95, -113};
assertThat(authenticationHandler.login("root1", "", authResponse, "db1").orElse(null), is(MySQLServerErrorCode.ER_ACCESS_DENIED_ERROR));
}
@Test
public void assertLoginWithIncorrectPassword() {
- setAuthority(new ShardingSphereUser("root", "root", ""));
+ initProxyContext(new ShardingSphereUser("root", "root", ""), true);
byte[] authResponse = {0, 89, -20, -27, 65, -120, -64, -101, 86, -100, -108, -100, 6, -125, -37, 117, 14, -43, 95, -113};
assertThat(authenticationHandler.login("root", "", authResponse, "db1").orElse(null), is(MySQLServerErrorCode.ER_ACCESS_DENIED_ERROR));
}
@Test
public void assertLoginWithoutPassword() {
- setAuthority(new ShardingSphereUser("root", null, ""));
+ initProxyContext(new ShardingSphereUser("root", null, ""), true);
byte[] authResponse = {};
assertFalse(authenticationHandler.login("root", "", authResponse, "db1").isPresent());
}
@@ -130,12 +130,6 @@ public final class MySQLAuthenticationHandlerTest {
assertThat(authenticator.getAuthenticationMethodName(), is(MySQLAuthenticationMethod.SECURE_PASSWORD_AUTHENTICATION.getMethodName()));
}
- private void setAuthority(final ShardingSphereUser user) {
- NativeAuthorityProviderAlgorithm algorithm = new NativeAuthorityProviderAlgorithm();
- algorithm.init(Collections.emptyMap(), Collections.emptyList());
- initProxyContext(user, true);
- }
-
@SneakyThrows(ReflectiveOperationException.class)
private void initProxyContext(final ShardingSphereUser user, final boolean isNeedSuper) {
Field contextManagerField = ProxyContext.getInstance().getClass().getDeclaredField("contextManager");
@@ -165,11 +159,11 @@ public final class MySQLAuthenticationHandlerTest {
AuthorityRuleConfiguration ruleConfig = new AuthorityRuleConfiguration(Collections.singletonList(user), new ShardingSphereAlgorithmConfiguration("NATIVE", new Properties()));
AuthorityRule rule = new AuthorityRuleBuilder().build(ruleConfig, Collections.emptyMap());
if (!isNeedSuper) {
- Field providerField = AuthorityRule.class.getDeclaredField("provider");
- NativeAuthorityProviderAlgorithm nativeAuthorityProviderAlgorithm = mock(NativeAuthorityProviderAlgorithm.class, RETURNS_DEEP_STUBS);
- when(nativeAuthorityProviderAlgorithm.findPrivileges(user.getGrantee())).thenReturn(Optional.empty());
- providerField.setAccessible(true);
- providerField.set(rule, nativeAuthorityProviderAlgorithm);
+ Field authorityRegistryField = AuthorityRule.class.getDeclaredField("authorityRegistry");
+ AuthorityRegistry authorityRegistry = mock(AuthorityRegistry.class);
+ when(authorityRegistry.findPrivileges(user.getGrantee())).thenReturn(Optional.empty());
+ authorityRegistryField.setAccessible(true);
+ authorityRegistryField.set(rule, authorityRegistry);
}
return new ShardingSphereRuleMetaData(Collections.singletonList(ruleConfig), Collections.singletonList(rule));
}