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

[shardingsphere] branch master updated: Remove initializedUsers from Authentication.init() (#9843)

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

zhangyonglun 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 2c89ec0  Remove initializedUsers from Authentication.init() (#9843)
2c89ec0 is described below

commit 2c89ec08e9e3b2ae6ea67334dc02c68f9b0ec319
Author: Liang Zhang <te...@163.com>
AuthorDate: Fri Mar 26 20:51:36 2021 +0800

    Remove initializedUsers from Authentication.init() (#9843)
---
 .../metadata/GovernanceMetaDataContexts.java       |  8 ++-
 .../infra/metadata/auth/Authentication.java        |  3 +-
 .../auth/builtin/DefaultAuthentication.java        | 11 +--
 .../context/metadata/MetaDataContextsBuilder.java  |  2 +-
 .../impl/GovernanceBootstrapInitializerTest.java   | 13 +++-
 .../impl/StandardBootstrapInitializerTest.java     | 11 ++-
 .../swapper/YamlProxyConfigurationSwapperTest.java | 82 ++++++++++++----------
 7 files changed, 75 insertions(+), 55 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 1cab314..916a4c0 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
@@ -335,11 +335,15 @@ public final class GovernanceMetaDataContexts implements MetaDataContexts {
     
     private Authentication createAuthentication(final Collection<ShardingSphereUser> users) {
         Authentication result = new DefaultAuthentication();
-        result.init(getNewUsers(users), getModifiedUsers(users));
+        Collection<ShardingSphereUser> newUsers = getNewUsers(users);
+        Map<ShardingSphereUser, ShardingSpherePrivilege> modifiedUsers = getModifiedUsers(users);
+        for (ShardingSphereUser each : newUsers) {
+            modifiedUsers.put(each, new ShardingSpherePrivilege());
+        }
+        result.init(modifiedUsers);
         return result;
     }
     
-    // TODO is it correct for new users with super privilege by default?
     private Collection<ShardingSphereUser> getNewUsers(final Collection<ShardingSphereUser> users) {
         return users.stream().filter(each -> !metaDataContexts.getAuthentication().findUser(each.getGrantee()).isPresent()).collect(Collectors.toList());
     }
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 02f61c0..7e6321d 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
@@ -33,10 +33,9 @@ public interface Authentication {
     /**
      * Initialize authentication.
      * 
-     * @param initializedUsers initialized users
      * @param loadedPrivileges loaded privileges
      */
-    void init(Collection<ShardingSphereUser> initializedUsers, Map<ShardingSphereUser, ShardingSpherePrivilege> loadedPrivileges);
+    void init(Map<ShardingSphereUser, ShardingSpherePrivilege> loadedPrivileges);
     
     /**
      * Get 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 159cc4e..1e0eee6 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
@@ -37,19 +37,10 @@ public final class DefaultAuthentication implements Authentication {
     private final Map<ShardingSphereUser, ShardingSpherePrivilege> authentication = new ConcurrentHashMap<>();
     
     @Override
-    public void init(final Collection<ShardingSphereUser> initializedUsers, final Map<ShardingSphereUser, ShardingSpherePrivilege> loadedPrivileges) {
-        for (ShardingSphereUser each : initializedUsers) {
-            authentication.put(each, createShardingSpherePrivilege());
-        }
+    public void init(final Map<ShardingSphereUser, ShardingSpherePrivilege> loadedPrivileges) {
         authentication.putAll(loadedPrivileges);
     }
     
-    private ShardingSpherePrivilege createShardingSpherePrivilege() {
-        ShardingSpherePrivilege result = new ShardingSpherePrivilege();
-        result.setSuperPrivilege();
-        return result;
-    }
-    
     @Override
     public Collection<ShardingSphereUser> getAllUsers() {
         return authentication.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 f8fc4a6..36dd42f 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.init(Collections.emptyList(), PrivilegeBuilder.build(metaDataMap.values(), users, props));
+        result.init(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 9ff3c02..c5c4577 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
@@ -29,6 +29,7 @@ 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.privilege.ShardingSpherePrivilege;
 import org.apache.shardingsphere.infra.metadata.auth.model.user.Grantee;
 import org.apache.shardingsphere.infra.metadata.auth.model.user.ShardingSphereUser;
 import org.apache.shardingsphere.proxy.config.ProxyConfiguration;
@@ -48,7 +49,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.HashMap;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Properties;
@@ -115,7 +116,7 @@ public final class GovernanceBootstrapInitializerTest extends AbstractBootstrapI
         assertSchemaDataSources(actual.getSchemaDataSources());
         assertSchemaRules(actual.getSchemaRules());
         Authentication authentication = new DefaultAuthentication();
-        authentication.init(actual.getUsers(), Collections.emptyMap());
+        authentication.init(getPrivileges(actual.getUsers()));
         assertAuthentication(authentication);
         assertProps(actual.getProps());
     }
@@ -196,6 +197,14 @@ public final class GovernanceBootstrapInitializerTest extends AbstractBootstrapI
         assertThat(props.getProperty("algorithm-expression"), is(expectedAlgorithmExpr));
     }
     
+    private Map<ShardingSphereUser, ShardingSpherePrivilege> getPrivileges(final Collection<ShardingSphereUser> users) {
+        Map<ShardingSphereUser, ShardingSpherePrivilege> privileges = new HashMap<>(users.size(), 1);
+        for (ShardingSphereUser each : users) {
+            privileges.put(each, new ShardingSpherePrivilege());
+        }
+        return privileges;
+    }
+    
     private void assertAuthentication(final Authentication actual) {
         Optional<ShardingSphereUser> rootUser = actual.findUser(new Grantee("root", ""));
         assertTrue(rootUser.isPresent());
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 924258e..e2a3542 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
@@ -24,6 +24,7 @@ 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.privilege.ShardingSpherePrivilege;
 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;
@@ -112,7 +113,7 @@ public final class StandardBootstrapInitializerTest extends AbstractBootstrapIni
         assertSchemaDataSources(actual.getSchemaDataSources());
         assertSchemaRules(actual.getSchemaRules());
         Authentication authentication = new DefaultAuthentication();
-        authentication.init(actual.getUsers(), Collections.emptyMap());
+        authentication.init(getPrivileges(actual.getUsers()));
         assertAuthentication(authentication);
         assertProps(actual.getProps());
     }
@@ -151,6 +152,14 @@ public final class StandardBootstrapInitializerTest extends AbstractBootstrapIni
         assertThat(((FixtureRuleConfiguration) actual).getName(), is("testRule"));
     }
     
+    private Map<ShardingSphereUser, ShardingSpherePrivilege> getPrivileges(final Collection<ShardingSphereUser> users) {
+        Map<ShardingSphereUser, ShardingSpherePrivilege> privileges = new HashMap<>(users.size(), 1);
+        for (ShardingSphereUser each : users) {
+            privileges.put(each, new ShardingSpherePrivilege());
+        }
+        return privileges;
+    }
+    
     private void assertAuthentication(final Authentication actual) {
         Optional<ShardingSphereUser> rootUser = actual.findUser(new Grantee("root", ""));
         assertTrue(rootUser.isPresent());
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 6c051a5..cda3210 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
@@ -19,25 +19,25 @@ package org.apache.shardingsphere.proxy.config.yaml.swapper;
 
 import org.apache.shardingsphere.governance.core.yaml.config.YamlGovernanceCenterConfiguration;
 import org.apache.shardingsphere.governance.core.yaml.config.YamlGovernanceConfiguration;
-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.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.privilege.ShardingSpherePrivilege;
+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.yaml.config.YamlRuleConfiguration;
-import org.apache.shardingsphere.readwrite.splitting.api.ReadWriteSplittingRuleConfiguration;
-import org.apache.shardingsphere.readwrite.splitting.common.yaml.config.YamlReadWriteSplittingRuleConfiguration;
 import org.apache.shardingsphere.proxy.config.ProxyConfiguration;
 import org.apache.shardingsphere.proxy.config.YamlProxyConfiguration;
 import org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameter;
 import org.apache.shardingsphere.proxy.config.yaml.YamlProxyRuleConfiguration;
 import org.apache.shardingsphere.proxy.config.yaml.YamlProxyServerConfiguration;
+import org.apache.shardingsphere.readwrite.splitting.api.ReadWriteSplittingRuleConfiguration;
+import org.apache.shardingsphere.readwrite.splitting.common.yaml.config.YamlReadWriteSplittingRuleConfiguration;
 import org.junit.Test;
 
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.Map;
@@ -105,7 +105,7 @@ public final class YamlProxyConfigurationSwapperTest {
     
     private void assertAuthentication(final ProxyConfiguration proxyConfig) {
         DefaultAuthentication authentication = new DefaultAuthentication();
-        authentication.init(proxyConfig.getUsers(), Collections.emptyMap());
+        authentication.init(getPrivileges(proxyConfig.getUsers()));
         assertNotNull(authentication);
         Optional<ShardingSphereUser> user = authentication.findUser(new Grantee("user1", ""));
         assertTrue(user.isPresent());
@@ -113,6 +113,14 @@ public final class YamlProxyConfigurationSwapperTest {
         assertNotNull(authentication);
     }
     
+    private Map<ShardingSphereUser, ShardingSpherePrivilege> getPrivileges(final Collection<ShardingSphereUser> users) {
+        Map<ShardingSphereUser, ShardingSpherePrivilege> privileges = new HashMap<>(users.size(), 1);
+        for (ShardingSphereUser each : users) {
+            privileges.put(each, new ShardingSpherePrivilege());
+        }
+        return privileges;
+    }
+    
     private YamlProxyConfiguration getYamlProxyConfiguration() {
         YamlProxyConfiguration result = mock(YamlProxyConfiguration.class);
         YamlProxyServerConfiguration yamlProxyServerConfig = getYamlProxyServerConfiguration(result);
@@ -128,11 +136,29 @@ public final class YamlProxyConfigurationSwapperTest {
         return result;
     }
     
-    private void prepareRules(final YamlProxyRuleConfiguration yamlProxyRuleConfig) {
-        Collection<YamlRuleConfiguration> rules = new LinkedList<>();
-        YamlRuleConfiguration testRuleConfig = new YamlReadWriteSplittingRuleConfiguration();
-        rules.add(testRuleConfig);
-        when(yamlProxyRuleConfig.getRules()).thenReturn(rules);
+    private void prepareRegistryCenter(final YamlGovernanceConfiguration yamlGovernanceConfig) {
+        YamlGovernanceCenterConfiguration registryCenterConfig = mock(YamlGovernanceCenterConfiguration.class);
+        when(yamlGovernanceConfig.getRegistryCenter()).thenReturn(registryCenterConfig);
+        when(registryCenterConfig.getType()).thenReturn("typeOne");
+        when(registryCenterConfig.getServerLists()).thenReturn("serverLists1");
+        Properties props = new Properties();
+        props.setProperty("key1", "value1");
+        when(registryCenterConfig.getProps()).thenReturn(props);
+        when(yamlGovernanceConfig.getRegistryCenter()).thenReturn(registryCenterConfig);
+    }
+
+    private void prepareProps(final YamlProxyServerConfiguration yamlProxyServerConfig) {
+        Properties props = new Properties();
+        props.setProperty("key4", "value4");
+        when(yamlProxyServerConfig.getProps()).thenReturn(props);
+    }
+    
+    private YamlProxyRuleConfiguration prepareRuleConfigurations(final YamlProxyConfiguration yamlProxyConfig) {
+        Map<String, YamlProxyRuleConfiguration> yamlProxyRuleConfigMap = new HashMap<>(1, 1);
+        when(yamlProxyConfig.getRuleConfigurations()).thenReturn(yamlProxyRuleConfigMap);
+        YamlProxyRuleConfiguration result = mock(YamlProxyRuleConfiguration.class);
+        yamlProxyRuleConfigMap.put("yamlProxyRule1", result);
+        return result;
     }
     
     private void prepareDataSources(final YamlProxyRuleConfiguration yamlProxyRuleConfig) {
@@ -168,29 +194,11 @@ public final class YamlProxyConfigurationSwapperTest {
         when(yamlDataSourceParameter.isReadOnly()).thenReturn(true);
     }
     
-    private YamlProxyRuleConfiguration prepareRuleConfigurations(final YamlProxyConfiguration yamlProxyConfig) {
-        Map<String, YamlProxyRuleConfiguration> yamlProxyRuleConfigMap = new HashMap<>(1, 1);
-        when(yamlProxyConfig.getRuleConfigurations()).thenReturn(yamlProxyRuleConfigMap);
-        YamlProxyRuleConfiguration result = mock(YamlProxyRuleConfiguration.class);
-        yamlProxyRuleConfigMap.put("yamlProxyRule1", result);
-        return result;
-    }
-    
-    private void prepareProps(final YamlProxyServerConfiguration yamlProxyServerConfig) {
-        Properties props = new Properties();
-        props.setProperty("key4", "value4");
-        when(yamlProxyServerConfig.getProps()).thenReturn(props);
-    }
-    
-    private void prepareRegistryCenter(final YamlGovernanceConfiguration yamlGovernanceConfig) {
-        YamlGovernanceCenterConfiguration registryCenterConfig = mock(YamlGovernanceCenterConfiguration.class);
-        when(yamlGovernanceConfig.getRegistryCenter()).thenReturn(registryCenterConfig);
-        when(registryCenterConfig.getType()).thenReturn("typeOne");
-        when(registryCenterConfig.getServerLists()).thenReturn("serverLists1");
-        Properties props = new Properties();
-        props.setProperty("key1", "value1");
-        when(registryCenterConfig.getProps()).thenReturn(props);
-        when(yamlGovernanceConfig.getRegistryCenter()).thenReturn(registryCenterConfig);
+    private void prepareRules(final YamlProxyRuleConfiguration yamlProxyRuleConfig) {
+        Collection<YamlRuleConfiguration> rules = new LinkedList<>();
+        YamlRuleConfiguration testRuleConfig = new YamlReadWriteSplittingRuleConfiguration();
+        rules.add(testRuleConfig);
+        when(yamlProxyRuleConfig.getRules()).thenReturn(rules);
     }
     
     private YamlGovernanceConfiguration prepareGovernance(final YamlProxyServerConfiguration yamlProxyServerConfig) {