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/02/11 12:21:15 UTC

[shardingsphere] branch master updated: Move authentication from AdapterContainer to ShardingSphereProxyContainer (#15365)

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 40ba3e1  Move authentication from AdapterContainer to ShardingSphereProxyContainer (#15365)
40ba3e1 is described below

commit 40ba3e12bd162f6ee03e8dd5cbc15b33faa4f8ae
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri Feb 11 20:20:22 2022 +0800

    Move authentication from AdapterContainer to ShardingSphereProxyContainer (#15365)
    
    * Refactor AdapterContainer
    
    * Move authentication from AdapterContainer to ShardingSphereProxyContainer
    
    * Move authentication from AdapterContainer to ShardingSphereProxyContainer
    
    * Move authentication from AdapterContainer to ShardingSphereProxyContainer
---
 .../user/yaml/config/YamlUserConfiguration.java    |  4 +--
 .../config/YamlUsersConfigurationConverter.java    | 34 ++++++++++----------
 .../container/atomic/adapter/AdapterContainer.java | 36 ----------------------
 .../adapter/impl/ShardingSphereProxyContainer.java | 27 ++++++++++++++--
 4 files changed, 44 insertions(+), 57 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/user/yaml/config/YamlUserConfiguration.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/user/yaml/config/YamlUserConfiguration.java
index ed94381..2ced5eb 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/user/yaml/config/YamlUserConfiguration.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/user/yaml/config/YamlUserConfiguration.java
@@ -27,13 +27,13 @@ import org.apache.shardingsphere.infra.yaml.config.pojo.YamlConfiguration;
 @Getter
 @Setter
 public final class YamlUserConfiguration implements YamlConfiguration {
-
+    
     private String username;
     
     private String hostname;
     
     private String password;
-
+    
     @Override
     public String toString() {
         return username + "@" + (null == hostname ? "%" : hostname) + ":" + password;
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/user/yaml/config/YamlUsersConfigurationConverter.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/user/yaml/config/YamlUsersConfigurationConverter.java
index 751da9c..a77d646 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/user/yaml/config/YamlUsersConfigurationConverter.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/user/yaml/config/YamlUsersConfigurationConverter.java
@@ -32,47 +32,47 @@ import java.util.stream.Collectors;
  */
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class YamlUsersConfigurationConverter {
-
+    
     private static final UserYamlSwapper USER_YAML_SWAPPER = new UserYamlSwapper();
-
+    
     /**
-     * Convert to users yaml content.
+     * Convert to users YAML content.
      *
-     * @param users sharding sphere users
-     * @return users yaml content
+     * @param users ShardingSphere users
+     * @return users YAML content
      */
     public static Collection<String> convertYamlUserConfigurations(final Collection<ShardingSphereUser> users) {
         Collection<String> result = new LinkedList<>();
         users.stream().map(USER_YAML_SWAPPER::swapToYamlConfiguration).forEach(user -> result.add(user.toString()));
         return result;
     }
-
+    
     /**
-     * Convert to sharding sphere users.
+     * Convert to ShardingSphere users.
      *
-     * @param users users yaml content
-     * @return sharding sphere users
+     * @param users users YAML content
+     * @return ShardingSphere users
      */
     public static Collection<ShardingSphereUser> convertShardingSphereUser(final Collection<String> users) {
         Collection<YamlUserConfiguration> yamlUsers = convertYamlUserConfiguration(users);
         return yamlUsers.stream().map(USER_YAML_SWAPPER::swapToObject).collect(Collectors.toList());
     }
-
+    
     /**
-     * Convert to yaml user configurations.
+     * Convert to YAML user configurations.
      *
-     * @param users users yaml content
-     * @return yaml user configurations
+     * @param users users YAML content
+     * @return YAML user configurations
      */
     public static Collection<YamlUserConfiguration> convertYamlUserConfiguration(final Collection<String> users) {
         return users.stream().map(YamlUsersConfigurationConverter::convertYamlUserConfiguration).collect(Collectors.toList());
     }
-
+    
     /**
-     * Convert to yaml user configuration.
+     * Convert to YAML user configuration.
      *
-     * @param yamlUser user yaml content
-     * @return yaml user configuration
+     * @param yamlUser user YAML content
+     * @return YAML user configuration
      */
     private static YamlUserConfiguration convertYamlUserConfiguration(final String yamlUser) {
         Preconditions.checkArgument(0 < yamlUser.indexOf("@") && 0 < yamlUser.indexOf(":") && yamlUser.indexOf(":") <= yamlUser.length() - 1,
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/adapter/AdapterContainer.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/adapter/AdapterContainer.java
index 683d196..9ea8ffd 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/adapter/AdapterContainer.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/adapter/AdapterContainer.java
@@ -17,31 +17,17 @@
 
 package org.apache.shardingsphere.test.integration.framework.container.atomic.adapter;
 
-import com.google.common.io.ByteStreams;
-import lombok.Getter;
 import lombok.SneakyThrows;
-import org.apache.shardingsphere.authority.yaml.config.YamlAuthorityRuleConfiguration;
-import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUserConfiguration;
-import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUsersConfigurationConverter;
-import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRuleConfiguration;
-import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
-import org.apache.shardingsphere.proxy.config.yaml.YamlProxyServerConfiguration;
 import org.apache.shardingsphere.test.integration.framework.container.atomic.ShardingSphereContainer;
 import org.apache.shardingsphere.test.integration.framework.param.model.ParameterizedArray;
 
 import javax.sql.DataSource;
-import java.io.IOException;
-import java.util.Collection;
-import java.util.Collections;
 
 /**
  * Adapter container.
  */
-@Getter
 public abstract class AdapterContainer extends ShardingSphereContainer {
     
-    private final YamlUserConfiguration authentication;
-    
     public AdapterContainer(final String dockerName, final String dockerImageName, final ParameterizedArray parameterizedArray) {
         this(dockerName, dockerImageName, false, parameterizedArray);
     }
@@ -49,19 +35,6 @@ public abstract class AdapterContainer extends ShardingSphereContainer {
     @SneakyThrows
     public AdapterContainer(final String name, final String dockerImageName, final boolean isFakedContainer, final ParameterizedArray parameterizedArray) {
         super(name, dockerImageName, isFakedContainer, parameterizedArray);
-        authentication = loadAuthentication(parameterizedArray);
-    }
-    
-    private YamlUserConfiguration loadAuthentication(final ParameterizedArray parameterizedArray) throws IOException {
-        YamlProxyServerConfiguration configuration = YamlEngine.unmarshal(
-                ByteStreams.toByteArray(this.getClass().getResourceAsStream(
-                        "/docker/proxy/conf/" + parameterizedArray.getScenario() + "/" + parameterizedArray.getDatabaseType().getName().toLowerCase() + "/server.yaml")),
-                YamlProxyServerConfiguration.class);
-        return YamlUsersConfigurationConverter.convertYamlUserConfiguration(getUsersFromConfiguration(configuration))
-                .stream()
-                .filter(each -> "root".equals(each.getUsername()))
-                .findFirst()
-                .orElse(new YamlUserConfiguration());
     }
     
     /**
@@ -79,13 +52,4 @@ public abstract class AdapterContainer extends ShardingSphereContainer {
      * @return data source
      */
     public abstract DataSource getDataSourceForReader(String serverLists);
-    
-    private Collection<String> getUsersFromConfiguration(final YamlProxyServerConfiguration serverConfig) {
-        for (YamlRuleConfiguration each : serverConfig.getRules()) {
-            if (each instanceof YamlAuthorityRuleConfiguration) {
-                return ((YamlAuthorityRuleConfiguration) each).getUsers();
-            }
-        }
-        return Collections.emptyList();
-    }
 }
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/adapter/impl/ShardingSphereProxyContainer.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/adapter/impl/ShardingSphereProxyContainer.java
index 4b8565e..ad39ded 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/adapter/impl/ShardingSphereProxyContainer.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/adapter/impl/ShardingSphereProxyContainer.java
@@ -17,8 +17,15 @@
 
 package org.apache.shardingsphere.test.integration.framework.container.atomic.adapter.impl;
 
+import com.google.common.io.ByteStreams;
 import com.zaxxer.hikari.HikariDataSource;
+import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.authority.yaml.config.YamlAuthorityRuleConfiguration;
+import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUserConfiguration;
+import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUsersConfigurationConverter;
+import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
+import org.apache.shardingsphere.proxy.config.yaml.YamlProxyServerConfiguration;
 import org.apache.shardingsphere.test.integration.env.DataSourceEnvironment;
 import org.apache.shardingsphere.test.integration.framework.container.atomic.adapter.AdapterContainer;
 import org.apache.shardingsphere.test.integration.framework.param.model.ParameterizedArray;
@@ -27,6 +34,9 @@ import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
 import org.testcontainers.utility.MountableFile;
 
 import javax.sql.DataSource;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.Objects;
 import java.util.concurrent.atomic.AtomicReference;
 
@@ -130,8 +140,9 @@ public final class ShardingSphereProxyContainer extends AdapterContainer {
         HikariDataSource result = new HikariDataSource();
         result.setDriverClassName(DataSourceEnvironment.getDriverClassName(databaseType));
         result.setJdbcUrl(DataSourceEnvironment.getURL(databaseType, getHost(), getMappedPort(3307), getParameterizedArray().getScenario()));
-        result.setUsername(getAuthentication().getUsername());
-        result.setPassword(getAuthentication().getPassword());
+        YamlUserConfiguration userConfig = loadUserConfiguration(getParameterizedArray());
+        result.setUsername(userConfig.getUsername());
+        result.setPassword(userConfig.getPassword());
         result.setMaximumPoolSize(2);
         result.setTransactionIsolation("TRANSACTION_READ_COMMITTED");
         if ("MySQL".equals(databaseType)) {
@@ -139,4 +150,16 @@ public final class ShardingSphereProxyContainer extends AdapterContainer {
         }
         return result;
     }
+    
+    @SneakyThrows(IOException.class)
+    private YamlUserConfiguration loadUserConfiguration(final ParameterizedArray parameterizedArray) {
+        YamlProxyServerConfiguration serverConfig = YamlEngine.unmarshal(ByteStreams.toByteArray(Objects.requireNonNull(this.getClass().getResourceAsStream(
+                "/docker/proxy/conf/" + parameterizedArray.getScenario() + "/" + parameterizedArray.getDatabaseType().getName().toLowerCase() + "/server.yaml"))), YamlProxyServerConfiguration.class);
+        return YamlUsersConfigurationConverter.convertYamlUserConfiguration(getProxyUsers(serverConfig)).stream().findFirst().orElse(new YamlUserConfiguration());
+    }
+    
+    private Collection<String> getProxyUsers(final YamlProxyServerConfiguration serverConfig) {
+        return serverConfig.getRules()
+                .stream().filter(each -> each instanceof YamlAuthorityRuleConfiguration).findFirst().map(each -> ((YamlAuthorityRuleConfiguration) each).getUsers()).orElse(Collections.emptyList());
+    }
 }