You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2021/03/26 08:37:29 UTC

[shardingsphere] branch master updated: Add Authentication.init() (#9835)

This is an automated email from the ASF dual-hosted git repository.

menghaoran 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 853a34e  Add Authentication.init() (#9835)
853a34e is described below

commit 853a34ec745687bdd34b38ed7d383fb0665494da
Author: Liang Zhang <te...@163.com>
AuthorDate: Fri Mar 26 16:36:58 2021 +0800

    Add Authentication.init() (#9835)
    
    * Add Authentication.init()
    
    * Refactor Authentication.init()
    
    * Remove useless constructor
---
 .../metadata/GovernanceMetaDataContexts.java       | 23 ++++++++++++----------
 .../infra/metadata/auth/Authentication.java        |  9 +++++++++
 .../auth/builtin/DefaultAuthentication.java        |  8 ++++----
 .../yaml/swapper/UserRuleYamlSwapperTest.java      |  3 +--
 .../context/metadata/MetaDataContextsBuilder.java  |  2 +-
 .../impl/GovernanceBootstrapInitializerTest.java   |  8 ++++++--
 .../impl/StandardBootstrapInitializerTest.java     | 17 +++++++++-------
 .../swapper/YamlProxyConfigurationSwapperTest.java |  4 +++-
 8 files changed, 47 insertions(+), 27 deletions(-)

diff --git a/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/metadata/GovernanceMetaDataContexts.java b/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/metadata/GovernanceMetaDataContexts.java
index 890af4a..50953d7 100644
--- a/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/metadata/GovernanceMetaDataContexts.java
+++ b/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/metadata/GovernanceMetaDataContexts.java
@@ -211,11 +211,8 @@ public final class GovernanceMetaDataContexts implements MetaDataContexts {
      */
     @Subscribe
     public synchronized void renew(final UserRuleChangedEvent event) {
-        Collection<ShardingSphereUser> users = event.getUsers();
-        DefaultAuthentication authentication = new DefaultAuthentication(getNewUsers(users));
-        authentication.getAuthentication().putAll(getModifiedUsers(users));
-        metaDataContexts = new StandardMetaDataContexts(metaDataContexts.getMetaDataMap(), metaDataContexts.getExecutorEngine(), authentication, metaDataContexts.getProps());
-        reloadPrivilege(users);
+        metaDataContexts = new StandardMetaDataContexts(metaDataContexts.getMetaDataMap(), metaDataContexts.getExecutorEngine(), createAuthentication(event.getUsers()), metaDataContexts.getProps());
+        reloadPrivilege(event.getUsers());
     }
     
     /**
@@ -311,6 +308,12 @@ public final class GovernanceMetaDataContexts implements MetaDataContexts {
         }
     }
     
+    private DefaultAuthentication createAuthentication(final Collection<ShardingSphereUser> users) {
+        DefaultAuthentication result = new DefaultAuthentication();
+        result.init(getNewUsers(users), getModifiedUsers(users));
+        return result;
+    }
+    
     private ShardingSphereMetaData buildMetaData(final MetaDataPersistedEvent event) throws SQLException {
         String schemaName = event.getSchemaName();
         if (!governanceFacade.getRegistryCenter().hasDataSourceConfiguration(schemaName)) {
@@ -416,12 +419,12 @@ public final class GovernanceMetaDataContexts implements MetaDataContexts {
             return;
         }
         Map<ShardingSphereUser, ShardingSpherePrivilege> result = PrivilegeBuilder.build(metaDataContexts.getMetaDataMap().values(), users, metaDataContexts.getProps());
-        for (Entry<ShardingSphereUser, ShardingSpherePrivilege> each : result.entrySet()) {
+        for (Entry<ShardingSphereUser, ShardingSpherePrivilege> entry : result.entrySet()) {
             Optional<ShardingSphereUser> user = metaDataContexts.getAuthentication().getAuthentication().keySet().stream().filter(t -> t.getGrantee().equals(t.getGrantee())).findFirst();
-            if (user.isPresent() && null != result.get(each.getKey())) {
-                metaDataContexts.getAuthentication().getAuthentication().put(user.get(), each.getValue());
-            } else if (!user.isPresent() && null != result.get(each.getKey())) {
-                metaDataContexts.getAuthentication().getAuthentication().put(each.getKey(), each.getValue());
+            if (user.isPresent() && null != result.get(entry.getKey())) {
+                metaDataContexts.getAuthentication().getAuthentication().put(user.get(), entry.getValue());
+            } else if (!user.isPresent() && null != result.get(entry.getKey())) {
+                metaDataContexts.getAuthentication().getAuthentication().put(entry.getKey(), entry.getValue());
             }
         }
     }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/Authentication.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/Authentication.java
index a6c496b..f2530ea 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/Authentication.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/Authentication.java
@@ -21,6 +21,7 @@ import org.apache.shardingsphere.infra.metadata.auth.model.privilege.ShardingSph
 import org.apache.shardingsphere.infra.metadata.auth.model.user.Grantee;
 import org.apache.shardingsphere.infra.metadata.auth.model.user.ShardingSphereUser;
 
+import java.util.Collection;
 import java.util.Map;
 import java.util.Optional;
 
@@ -30,6 +31,14 @@ import java.util.Optional;
 public interface Authentication {
     
     /**
+     * Initialize authentication.
+     * 
+     * @param initializedUsers initialized users
+     * @param loadedPrivileges loaded privileges
+     */
+    void init(Collection<ShardingSphereUser> initializedUsers, Map<ShardingSphereUser, ShardingSpherePrivilege> loadedPrivileges);
+    
+    /**
      * Get authentication.
      *
      * @return Authentication
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builtin/DefaultAuthentication.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builtin/DefaultAuthentication.java
index 4dac150..348a190 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builtin/DefaultAuthentication.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builtin/DefaultAuthentication.java
@@ -18,7 +18,6 @@
 package org.apache.shardingsphere.infra.metadata.auth.builtin;
 
 import lombok.Getter;
-import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.infra.metadata.auth.Authentication;
 import org.apache.shardingsphere.infra.metadata.auth.model.privilege.ShardingSpherePrivilege;
 import org.apache.shardingsphere.infra.metadata.auth.model.user.Grantee;
@@ -32,16 +31,17 @@ import java.util.concurrent.ConcurrentHashMap;
 /**
  * Default authentication.
 */
-@NoArgsConstructor
 @Getter
 public final class DefaultAuthentication implements Authentication {
     
     private final Map<ShardingSphereUser, ShardingSpherePrivilege> authentication = new ConcurrentHashMap<>();
     
-    public DefaultAuthentication(final Collection<ShardingSphereUser> users) {
-        for (ShardingSphereUser each : users) {
+    @Override
+    public void init(final Collection<ShardingSphereUser> initializedUsers, final Map<ShardingSphereUser, ShardingSpherePrivilege> loadedPrivileges) {
+        for (ShardingSphereUser each : initializedUsers) {
             authentication.put(each, createShardingSpherePrivilege());
         }
+        authentication.putAll(loadedPrivileges);
     }
     
     private ShardingSpherePrivilege createShardingSpherePrivilege() {
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserRuleYamlSwapperTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserRuleYamlSwapperTest.java
index 5f11286..d5cc037 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserRuleYamlSwapperTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserRuleYamlSwapperTest.java
@@ -28,7 +28,6 @@ import org.junit.Test;
 
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.LinkedHashSet;
 import java.util.Map;
 import java.util.Optional;
 
@@ -40,7 +39,7 @@ public final class UserRuleYamlSwapperTest {
     
     @Test
     public void assertSwapToYaml() {
-        DefaultAuthentication authentication = new DefaultAuthentication(new LinkedHashSet<>(2, 1));
+        DefaultAuthentication authentication = new DefaultAuthentication();
         authentication.getAuthentication().put(new ShardingSphereUser("user1", "pwd1", "127.0.0.1"), new ShardingSpherePrivilege());
         authentication.getAuthentication().put(new ShardingSphereUser("user2", "pwd2", "127.0.0.2"), new ShardingSpherePrivilege());
         YamlUserRuleConfiguration actual = new UserRuleYamlSwapper().swapToYamlConfiguration(authentication.getAuthentication().keySet());
diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilder.java b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilder.java
index a660edd..f8fc4a6 100644
--- a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilder.java
+++ b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilder.java
@@ -158,7 +158,7 @@ public final class MetaDataContextsBuilder {
     
     private Authentication buildAuthentication(final Collection<ShardingSphereUser> users, final Map<String, ShardingSphereMetaData> metaDataMap) {
         DefaultAuthentication result = new DefaultAuthentication();
-        result.getAuthentication().putAll(PrivilegeBuilder.build(metaDataMap.values(), users, props));
+        result.init(Collections.emptyList(), PrivilegeBuilder.build(metaDataMap.values(), users, props));
         return result;
     }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializerTest.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializerTest.java
index 894f79d..9ff3c02 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializerTest.java
@@ -27,6 +27,7 @@ import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
 import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
 import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
 import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
+import org.apache.shardingsphere.infra.metadata.auth.Authentication;
 import org.apache.shardingsphere.infra.metadata.auth.builtin.DefaultAuthentication;
 import org.apache.shardingsphere.infra.metadata.auth.model.user.Grantee;
 import org.apache.shardingsphere.infra.metadata.auth.model.user.ShardingSphereUser;
@@ -47,6 +48,7 @@ import java.net.URISyntaxException;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Properties;
@@ -112,7 +114,9 @@ public final class GovernanceBootstrapInitializerTest extends AbstractBootstrapI
         assertNotNull(actual);
         assertSchemaDataSources(actual.getSchemaDataSources());
         assertSchemaRules(actual.getSchemaRules());
-        assertAuthentication(new DefaultAuthentication(actual.getUsers()));
+        Authentication authentication = new DefaultAuthentication();
+        authentication.init(actual.getUsers(), Collections.emptyMap());
+        assertAuthentication(authentication);
         assertProps(actual.getProps());
     }
     
@@ -192,7 +196,7 @@ public final class GovernanceBootstrapInitializerTest extends AbstractBootstrapI
         assertThat(props.getProperty("algorithm-expression"), is(expectedAlgorithmExpr));
     }
     
-    private void assertAuthentication(final DefaultAuthentication actual) {
+    private void assertAuthentication(final Authentication actual) {
         Optional<ShardingSphereUser> rootUser = actual.findUser(new Grantee("root", ""));
         assertTrue(rootUser.isPresent());
         assertThat(rootUser.get().getPassword(), is("root"));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/StandardBootstrapInitializerTest.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/StandardBootstrapInitializerTest.java
index 47813db..924258e 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/StandardBootstrapInitializerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/initializer/impl/StandardBootstrapInitializerTest.java
@@ -17,14 +17,15 @@
 
 package org.apache.shardingsphere.proxy.initializer.impl;
 
-import org.apache.shardingsphere.infra.metadata.auth.model.user.Grantee;
-import org.apache.shardingsphere.infra.metadata.auth.builtin.DefaultAuthentication;
-import org.apache.shardingsphere.infra.metadata.auth.model.user.ShardingSphereUser;
-import org.apache.shardingsphere.infra.metadata.auth.builtin.yaml.config.YamlUserRuleConfiguration;
-import org.apache.shardingsphere.infra.metadata.auth.builtin.yaml.config.YamlUserConfiguration;
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
 import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
+import org.apache.shardingsphere.infra.metadata.auth.Authentication;
+import org.apache.shardingsphere.infra.metadata.auth.builtin.DefaultAuthentication;
+import org.apache.shardingsphere.infra.metadata.auth.builtin.yaml.config.YamlUserConfiguration;
+import org.apache.shardingsphere.infra.metadata.auth.builtin.yaml.config.YamlUserRuleConfiguration;
+import org.apache.shardingsphere.infra.metadata.auth.model.user.Grantee;
+import org.apache.shardingsphere.infra.metadata.auth.model.user.ShardingSphereUser;
 import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.infra.yaml.config.YamlRuleConfiguration;
 import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper;
@@ -110,7 +111,9 @@ public final class StandardBootstrapInitializerTest extends AbstractBootstrapIni
     private void assertProxyConfiguration(final ProxyConfiguration actual) {
         assertSchemaDataSources(actual.getSchemaDataSources());
         assertSchemaRules(actual.getSchemaRules());
-        assertAuthentication(new DefaultAuthentication(actual.getUsers()));
+        Authentication authentication = new DefaultAuthentication();
+        authentication.init(actual.getUsers(), Collections.emptyMap());
+        assertAuthentication(authentication);
         assertProps(actual.getProps());
     }
     
@@ -148,7 +151,7 @@ public final class StandardBootstrapInitializerTest extends AbstractBootstrapIni
         assertThat(((FixtureRuleConfiguration) actual).getName(), is("testRule"));
     }
     
-    private void assertAuthentication(final DefaultAuthentication actual) {
+    private void assertAuthentication(final Authentication actual) {
         Optional<ShardingSphereUser> rootUser = actual.findUser(new Grantee("root", ""));
         assertTrue(rootUser.isPresent());
         assertThat(rootUser.get().getPassword(), is("root"));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java b/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java
index 40719c1..6c051a5 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-common/src/test/java/org/apache/shardingsphere/proxy/config/yaml/swapper/YamlProxyConfigurationSwapperTest.java
@@ -37,6 +37,7 @@ import org.apache.shardingsphere.proxy.config.yaml.YamlProxyServerConfiguration;
 import org.junit.Test;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.Map;
@@ -103,7 +104,8 @@ public final class YamlProxyConfigurationSwapperTest {
     }
     
     private void assertAuthentication(final ProxyConfiguration proxyConfig) {
-        DefaultAuthentication authentication = new DefaultAuthentication(proxyConfig.getUsers());
+        DefaultAuthentication authentication = new DefaultAuthentication();
+        authentication.init(proxyConfig.getUsers(), Collections.emptyMap());
         assertNotNull(authentication);
         Optional<ShardingSphereUser> user = authentication.findUser(new Grantee("user1", ""));
         assertTrue(user.isPresent());