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/01/31 05:59:01 UTC
[shardingsphere] branch master updated: New Privileges feature
(#9232)
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 a21d173 New Privileges feature (#9232)
a21d173 is described below
commit a21d173551d1f38832312f501cf2e27b7ac2edbe
Author: Juan Pan(Trista) <pa...@apache.org>
AuthorDate: Sun Jan 31 13:58:22 2021 +0800
New Privileges feature (#9232)
* Privileges function.
* revert
* Remove this class
* fix ut
* fix ut
* fix ut
* bug fix
* bug fix
* bug fix
---
.../service/PrometheusPluginBootServiceTest.java | 5 +++--
.../governance/core/config/ConfigCenterTest.java | 3 ++-
.../listener/AuthenticationChangedListenerTest.java | 3 ++-
.../core/facade/GovernanceFacadeTest.java | 4 ++--
.../shardingsphere/infra/auth/Authentication.java | 4 ++--
.../auth/{ShardingSphereUser.java => Grantee.java} | 12 ++++++------
.../{ShardingSphereUser.java => PrivilegeType.java} | 9 ++++-----
.../{Authentication.java => SchemaPrivilege.java} | 17 ++++-------------
.../infra/auth/ShardingSphereUser.java | 2 ++
...{ShardingSphereUser.java => TablePrivilege.java} | 21 +++++++++++++++++----
.../infra/auth/builtin/DefaultAuthentication.java | 9 +++++++--
.../builtin/yaml/config/YamlUserConfiguration.java | 2 ++
.../yaml/swapper/AuthenticationYamlSwapper.java | 17 ++++++++++++++---
.../auth/builtin/yaml/swapper/UserYamlSwapper.java | 6 ++++--
.../yaml/swapper/AuthenticationYamlSwapperTest.java | 11 +++++++----
.../builtin/yaml/swapper/UserYamlSwapperTest.java | 3 ++-
.../jdbc/connection/BackendConnection.java | 3 ++-
.../admin/mysql/executor/ShowDatabasesExecutor.java | 2 +-
.../admin/mysql/executor/UseDatabaseExecutor.java | 2 +-
.../mysql/executor/ShowDatabasesExecutorTest.java | 5 +++--
.../mysql/executor/ShowTablesExecutorTest.java | 5 +++--
.../mysql/executor/UseDatabaseExecutorTest.java | 2 +-
.../SchemaAssignedDatabaseBackendHandlerTest.java | 2 +-
.../impl/UnicastDatabaseBackendHandlerTest.java | 2 +-
.../src/main/resources/conf/server.yaml | 1 +
.../impl/GovernanceBootstrapInitializerTest.java | 5 +++--
.../impl/StandardBootstrapInitializerTest.java | 3 ++-
.../swapper/YamlProxyConfigurationSwapperTest.java | 3 ++-
.../frontend/auth/AuthenticationResultBuilder.java | 12 +++++++-----
.../netty/FrontendChannelInboundHandler.java | 3 ++-
.../auth/AuthenticationResultBuilderTest.java | 6 ++++--
.../mysql/auth/MySQLAuthenticationEngine.java | 8 ++++----
.../mysql/auth/MySQLAuthenticationHandler.java | 8 +++++---
.../admin/initdb/MySQLComInitDbExecutor.java | 2 +-
.../frontend/mysql/MySQLFrontendEngineTest.java | 9 ++++++---
.../mysql/auth/MySQLAuthenticationEngineTest.java | 16 +++++++++++++---
.../mysql/auth/MySQLAuthenticationHandlerTest.java | 20 ++++++++++----------
.../auth/PostgreSQLAuthenticationEngine.java | 4 ++--
.../auth/PostgreSQLAuthenticationHandler.java | 3 ++-
.../auth/PostgreSQLAuthenticationEngineTest.java | 2 +-
.../proxy/frontend/auth/AuthenticationResult.java | 2 ++
41 files changed, 160 insertions(+), 98 deletions(-)
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/service/PrometheusPluginBootServiceTest.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/service/PrometheusPluginBootServiceTest.java
index 8d3d3de..6b7cc90 100644
--- a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/service/PrometheusPluginBootServiceTest.java
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/shardingsphere-agent-metrics-prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/service/PrometheusPluginBootServiceTest.java
@@ -18,13 +18,14 @@
package org.apache.shardingsphere.agent.metrics.prometheus.service;
import io.prometheus.client.exporter.HTTPServer;
-import java.lang.reflect.Field;
-import java.util.Properties;
import lombok.SneakyThrows;
import org.apache.shardingsphere.agent.config.PluginConfiguration;
import org.junit.AfterClass;
import org.junit.Test;
+import java.lang.reflect.Field;
+import java.util.Properties;
+
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/config/ConfigCenterTest.java b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/config/ConfigCenterTest.java
index d164940..30082dd 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/config/ConfigCenterTest.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/config/ConfigCenterTest.java
@@ -30,6 +30,7 @@ import org.apache.shardingsphere.governance.core.yaml.config.schema.YamlSchema;
import org.apache.shardingsphere.governance.core.yaml.swapper.SchemaYamlSwapper;
import org.apache.shardingsphere.governance.repository.api.ConfigurationRepository;
import org.apache.shardingsphere.ha.api.config.HARuleConfiguration;
+import org.apache.shardingsphere.infra.auth.Grantee;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlAuthenticationConfiguration;
@@ -424,7 +425,7 @@ public final class ConfigCenterTest {
when(configurationRepository.get("/authentication")).thenReturn(readYAML(AUTHENTICATION_YAML));
ConfigCenter configCenter = new ConfigCenter(configurationRepository);
DefaultAuthentication actual = configCenter.loadAuthentication();
- Optional<ShardingSphereUser> user = actual.findUser("root1");
+ Optional<ShardingSphereUser> user = actual.findUser(new Grantee("root1", ""));
assertTrue(user.isPresent());
assertThat(user.get().getPassword(), is("root1"));
}
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/config/listener/AuthenticationChangedListenerTest.java b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/config/listener/AuthenticationChangedListenerTest.java
index c258fb4..f536dfb 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/config/listener/AuthenticationChangedListenerTest.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/config/listener/AuthenticationChangedListenerTest.java
@@ -22,6 +22,7 @@ import org.apache.shardingsphere.governance.core.event.model.auth.Authentication
import org.apache.shardingsphere.governance.repository.api.ConfigurationRepository;
import org.apache.shardingsphere.governance.repository.api.listener.DataChangedEvent;
import org.apache.shardingsphere.governance.repository.api.listener.DataChangedEvent.Type;
+import org.apache.shardingsphere.infra.auth.Grantee;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.junit.Before;
import org.junit.Test;
@@ -55,7 +56,7 @@ public final class AuthenticationChangedListenerTest {
public void assertCreateEvent() {
Optional<GovernanceEvent> actual = authenticationChangedListener.createEvent(new DataChangedEvent("test", AUTHENTICATION_YAML, Type.UPDATED));
assertTrue(actual.isPresent());
- Optional<ShardingSphereUser> user = ((AuthenticationChangedEvent) actual.get()).getAuthentication().findUser("root1");
+ Optional<ShardingSphereUser> user = ((AuthenticationChangedEvent) actual.get()).getAuthentication().findUser(new Grantee("root1", ""));
assertTrue(user.isPresent());
assertThat(user.get().getPassword(), is("root1"));
}
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/facade/GovernanceFacadeTest.java b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/facade/GovernanceFacadeTest.java
index ae83e3a..969c548 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/facade/GovernanceFacadeTest.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/test/java/org/apache/shardingsphere/governance/core/facade/GovernanceFacadeTest.java
@@ -24,8 +24,8 @@ import org.apache.shardingsphere.governance.core.facade.util.FieldUtil;
import org.apache.shardingsphere.governance.core.registry.RegistryCenter;
import org.apache.shardingsphere.governance.repository.api.config.GovernanceCenterConfiguration;
import org.apache.shardingsphere.governance.repository.api.config.GovernanceConfiguration;
-import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
+import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.junit.Before;
@@ -74,7 +74,7 @@ public final class GovernanceFacadeTest {
public void assertOnlineInstanceWithParameters() {
Map<String, DataSourceConfiguration> dataSourceConfigMap = Collections.singletonMap("test_ds", mock(DataSourceConfiguration.class));
Map<String, Collection<RuleConfiguration>> ruleConfigurationMap = Collections.singletonMap("sharding_db", Collections.singletonList(mock(RuleConfiguration.class)));
- ShardingSphereUser user = new ShardingSphereUser("root", Collections.singleton("db1"));
+ ShardingSphereUser user = new ShardingSphereUser("root", "", Collections.singleton("db1"));
DefaultAuthentication authentication = new DefaultAuthentication();
authentication.getUsers().put("root", user);
Properties props = new Properties();
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/Authentication.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/Authentication.java
index 30fc63a..6219dd9 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/Authentication.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/Authentication.java
@@ -27,8 +27,8 @@ public interface Authentication {
/**
* Find user.
*
- * @param username username
+ * @param grantee grantee
* @return found user
*/
- Optional<ShardingSphereUser> findUser(String username);
+ Optional<ShardingSphereUser> findUser(Grantee grantee);
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/ShardingSphereUser.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/Grantee.java
similarity index 83%
copy from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/ShardingSphereUser.java
copy to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/Grantee.java
index c3d08db..95e5979 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/ShardingSphereUser.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/Grantee.java
@@ -17,19 +17,19 @@
package org.apache.shardingsphere.infra.auth;
+import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import java.util.Collection;
-
/**
- * ShardingSphere user.
+ * Grantee.
*/
@RequiredArgsConstructor
@Getter
-public final class ShardingSphereUser {
+@EqualsAndHashCode
+public final class Grantee {
- private final String password;
+ private final String username;
- private final Collection<String> authorizedSchemas;
+ private final String hostname;
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/ShardingSphereUser.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/PrivilegeType.java
similarity index 84%
copy from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/ShardingSphereUser.java
copy to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/PrivilegeType.java
index c3d08db..348eb73 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/ShardingSphereUser.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/PrivilegeType.java
@@ -20,16 +20,15 @@ package org.apache.shardingsphere.infra.auth;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import java.util.Collection;
/**
- * ShardingSphere user.
+ * Privilege Type.
*/
@RequiredArgsConstructor
@Getter
-public final class ShardingSphereUser {
+public enum PrivilegeType {
- private final String password;
+ ALL("*"), SELECT("SELECT"), DELETE("DELETE"), UPDATE("UPDATE"), INSERT("INSERT");
- private final Collection<String> authorizedSchemas;
+ private final String name;
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/Authentication.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/SchemaPrivilege.java
similarity index 77%
copy from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/Authentication.java
copy to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/SchemaPrivilege.java
index 30fc63a..68b1b8c 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/Authentication.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/SchemaPrivilege.java
@@ -17,18 +17,9 @@
package org.apache.shardingsphere.infra.auth;
-import java.util.Optional;
-
/**
- * Authentication.
-*/
-public interface Authentication {
-
- /**
- * Find user.
- *
- * @param username username
- * @return found user
- */
- Optional<ShardingSphereUser> findUser(String username);
+ * Schema privilege.
+ */
+public final class SchemaPrivilege {
+
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/ShardingSphereUser.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/ShardingSphereUser.java
index c3d08db..c1cc034 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/ShardingSphereUser.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/ShardingSphereUser.java
@@ -31,5 +31,7 @@ public final class ShardingSphereUser {
private final String password;
+ private final String hostname;
+
private final Collection<String> authorizedSchemas;
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/ShardingSphereUser.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/TablePrivilege.java
similarity index 66%
copy from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/ShardingSphereUser.java
copy to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/TablePrivilege.java
index c3d08db..7d83752 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/ShardingSphereUser.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/TablePrivilege.java
@@ -23,13 +23,26 @@ import lombok.RequiredArgsConstructor;
import java.util.Collection;
/**
- * ShardingSphere user.
+ * Table privilege.
*/
@RequiredArgsConstructor
@Getter
-public final class ShardingSphereUser {
+public final class TablePrivilege {
- private final String password;
+ private final String tableName;
- private final Collection<String> authorizedSchemas;
+ private final Collection<PrivilegeType> privileges;
+
+ /**
+ * Has privileges.
+ *
+ * @param privileges privileges
+ * @return has privileges or not
+ */
+ public boolean hasPrivileges(final Collection<PrivilegeType> privileges) {
+ if (this.privileges.contains(PrivilegeType.ALL)) {
+ return true;
+ }
+ return this.privileges.containsAll(privileges);
+ }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/DefaultAuthentication.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/DefaultAuthentication.java
index 4998028..52557f0 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/DefaultAuthentication.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/DefaultAuthentication.java
@@ -17,12 +17,15 @@
package org.apache.shardingsphere.infra.auth.builtin;
+import com.google.common.base.Strings;
import lombok.Getter;
import org.apache.shardingsphere.infra.auth.Authentication;
+import org.apache.shardingsphere.infra.auth.Grantee;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import java.util.LinkedHashMap;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Optional;
/**
@@ -34,7 +37,9 @@ public final class DefaultAuthentication implements Authentication {
private final Map<String, ShardingSphereUser> users = new LinkedHashMap<>();
@Override
- public Optional<ShardingSphereUser> findUser(final String username) {
- return Optional.ofNullable(users.get(username));
+ public Optional<ShardingSphereUser> findUser(final Grantee grantee) {
+ Optional<Entry<String, ShardingSphereUser>> result = users.entrySet().stream().filter(entry -> entry.getKey().equals(grantee.getUsername())
+ && (entry.getValue().getHostname().equals(grantee.getHostname()) || Strings.isNullOrEmpty(entry.getValue().getHostname()))).findFirst();
+ return result.map(Entry::getValue);
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlUserConfiguration.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlUserConfiguration.java
index ed8c8aa..00eba8d 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlUserConfiguration.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlUserConfiguration.java
@@ -28,6 +28,8 @@ import org.apache.shardingsphere.infra.yaml.config.YamlConfiguration;
@Setter
public final class YamlUserConfiguration implements YamlConfiguration {
+ private String hostname;
+
private String password;
private String authorizedSchemas;
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/AuthenticationYamlSwapper.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/AuthenticationYamlSwapper.java
index f660287..2a10b0c 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/AuthenticationYamlSwapper.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/AuthenticationYamlSwapper.java
@@ -17,11 +17,16 @@
package org.apache.shardingsphere.infra.auth.builtin.yaml.swapper;
-import com.google.common.collect.Maps;
+import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlAuthenticationConfiguration;
+import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlUserConfiguration;
import org.apache.shardingsphere.infra.yaml.swapper.YamlSwapper;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
/**
* Authentication YAML swapper.
*/
@@ -32,7 +37,11 @@ public final class AuthenticationYamlSwapper implements YamlSwapper<YamlAuthenti
@Override
public YamlAuthenticationConfiguration swapToYamlConfiguration(final DefaultAuthentication data) {
YamlAuthenticationConfiguration result = new YamlAuthenticationConfiguration();
- result.setUsers(Maps.transformValues(data.getUsers(), userYamlSwapper::swapToYamlConfiguration));
+ Map<String, YamlUserConfiguration> users = new LinkedHashMap<>();
+ for (Entry<String, ShardingSphereUser> entry : data.getUsers().entrySet()) {
+ users.put(entry.getKey(), userYamlSwapper.swapToYamlConfiguration(entry.getValue()));
+ }
+ result.setUsers(users);
return result;
}
@@ -42,7 +51,9 @@ public final class AuthenticationYamlSwapper implements YamlSwapper<YamlAuthenti
if (null == yamlConfig) {
return result;
}
- result.getUsers().putAll(Maps.transformValues(yamlConfig.getUsers(), userYamlSwapper::swapToObject));
+ for (Entry<String, YamlUserConfiguration> entry : yamlConfig.getUsers().entrySet()) {
+ result.getUsers().put(entry.getKey(), userYamlSwapper.swapToObject(entry.getValue()));
+ }
return result;
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserYamlSwapper.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserYamlSwapper.java
index a87befe..5742b40 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserYamlSwapper.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserYamlSwapper.java
@@ -34,6 +34,7 @@ public final class UserYamlSwapper implements YamlSwapper<YamlUserConfiguration,
@Override
public YamlUserConfiguration swapToYamlConfiguration(final ShardingSphereUser data) {
YamlUserConfiguration result = new YamlUserConfiguration();
+ result.setHostname(data.getHostname());
result.setPassword(data.getPassword());
String authorizedSchemas = null == data.getAuthorizedSchemas() ? "" : Joiner.on(',').join(data.getAuthorizedSchemas());
result.setAuthorizedSchemas(authorizedSchemas);
@@ -43,8 +44,9 @@ public final class UserYamlSwapper implements YamlSwapper<YamlUserConfiguration,
@Override
public ShardingSphereUser swapToObject(final YamlUserConfiguration yamlConfig) {
if (Strings.isNullOrEmpty(yamlConfig.getAuthorizedSchemas())) {
- return new ShardingSphereUser(yamlConfig.getPassword(), Collections.emptyList());
+ return new ShardingSphereUser(yamlConfig.getPassword(), null == yamlConfig.getHostname() ? "" : yamlConfig.getHostname(), Collections.emptyList());
}
- return new ShardingSphereUser(yamlConfig.getPassword(), Splitter.on(',').trimResults().splitToList(yamlConfig.getAuthorizedSchemas()));
+ return new ShardingSphereUser(yamlConfig.getPassword(), null == yamlConfig.getHostname() ? "" : yamlConfig.getHostname(),
+ Splitter.on(',').trimResults().splitToList(yamlConfig.getAuthorizedSchemas()));
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/AuthenticationYamlSwapperTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/AuthenticationYamlSwapperTest.java
index 109dea3..9544872 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/AuthenticationYamlSwapperTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/AuthenticationYamlSwapperTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.infra.auth.builtin.yaml.swapper;
+import org.apache.shardingsphere.infra.auth.Grantee;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlAuthenticationConfiguration;
@@ -37,13 +38,15 @@ public final class AuthenticationYamlSwapperTest {
@Test
public void assertSwapToYaml() {
DefaultAuthentication authentication = new DefaultAuthentication();
- authentication.getUsers().put("user1", new ShardingSphereUser("pwd1", Collections.singleton("db1")));
- authentication.getUsers().put("user2", new ShardingSphereUser("pwd2", Collections.singleton("db2")));
+ authentication.getUsers().put("user1", new ShardingSphereUser("pwd1", "127.0.0.1", Collections.singleton("db1")));
+ authentication.getUsers().put("user2", new ShardingSphereUser("pwd2", "127.0.0.2", Collections.singleton("db2")));
YamlAuthenticationConfiguration actual = new AuthenticationYamlSwapper().swapToYamlConfiguration(authentication);
assertThat(actual.getUsers().size(), is(2));
assertThat(actual.getUsers().get("user1").getPassword(), is("pwd1"));
+ assertThat(actual.getUsers().get("user1").getHostname(), is("127.0.0.1"));
assertThat(actual.getUsers().get("user1").getAuthorizedSchemas(), is("db1"));
assertThat(actual.getUsers().get("user2").getPassword(), is("pwd2"));
+ assertThat(actual.getUsers().get("user2").getHostname(), is("127.0.0.2"));
assertThat(actual.getUsers().get("user2").getAuthorizedSchemas(), is("db2"));
}
@@ -61,10 +64,10 @@ public final class AuthenticationYamlSwapperTest {
YamlAuthenticationConfiguration yamlConfig = new YamlAuthenticationConfiguration();
yamlConfig.setUsers(users);
DefaultAuthentication actual = new AuthenticationYamlSwapper().swapToObject(yamlConfig);
- Optional<ShardingSphereUser> actualUser1 = actual.findUser("user1");
+ Optional<ShardingSphereUser> actualUser1 = actual.findUser(new Grantee("user1", ""));
assertTrue(actualUser1.isPresent());
assertThat(actualUser1.get().getAuthorizedSchemas().size(), is(1));
- Optional<ShardingSphereUser> actualUser2 = actual.findUser("user2");
+ Optional<ShardingSphereUser> actualUser2 = actual.findUser(new Grantee("user2", ""));
assertTrue(actualUser2.isPresent());
assertThat(actualUser2.get().getAuthorizedSchemas().size(), is(2));
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserYamlSwapperTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserYamlSwapperTest.java
index d4f414e..afb33a9 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserYamlSwapperTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserYamlSwapperTest.java
@@ -30,9 +30,10 @@ public final class UserYamlSwapperTest {
@Test
public void assertSwapToYaml() {
- YamlUserConfiguration actual = new UserYamlSwapper().swapToYamlConfiguration(new ShardingSphereUser("pwd", Collections.singleton("db1")));
+ YamlUserConfiguration actual = new UserYamlSwapper().swapToYamlConfiguration(new ShardingSphereUser("pwd", "127.0.0.1", Collections.singleton("db1")));
assertThat(actual.getAuthorizedSchemas(), is("db1"));
assertThat(actual.getPassword(), is("pwd"));
+ assertThat(actual.getHostname(), is("127.0.0.1"));
}
@Test
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnection.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnection.java
index ff7a493..b1c1487 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnection.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/connection/BackendConnection.java
@@ -23,6 +23,7 @@ import com.google.common.collect.Multimap;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.db.protocol.parameter.TypeUnspecifiedSQLParameter;
+import org.apache.shardingsphere.infra.auth.Grantee;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode;
@@ -66,7 +67,7 @@ public final class BackendConnection implements ExecutorJDBCManager {
private int connectionId;
@Setter
- private String username;
+ private Grantee grantee;
@Setter
private CalciteExecutor calciteExecutor;
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowDatabasesExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowDatabasesExecutor.java
index 2db5209..0e870ac 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowDatabasesExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowDatabasesExecutor.java
@@ -49,7 +49,7 @@ public final class ShowDatabasesExecutor implements DatabaseAdminQueryExecutor {
private Collection<Object> getSchemaNames(final BackendConnection backendConnection) {
Collection<Object> result = new LinkedList<>(ProxyContext.getInstance().getAllSchemaNames());
- Optional<ShardingSphereUser> user = ProxyContext.getInstance().getMetaDataContexts().getAuthentication().findUser(backendConnection.getUsername());
+ Optional<ShardingSphereUser> user = ProxyContext.getInstance().getMetaDataContexts().getAuthentication().findUser(backendConnection.getGrantee());
Collection<String> authorizedSchemas = user.isPresent() ? user.get().getAuthorizedSchemas() : Collections.emptyList();
if (!authorizedSchemas.isEmpty()) {
result.retainAll(authorizedSchemas);
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/UseDatabaseExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/UseDatabaseExecutor.java
index 3d0c27e..da7f611 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/UseDatabaseExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/UseDatabaseExecutor.java
@@ -48,7 +48,7 @@ public final class UseDatabaseExecutor implements DatabaseAdminExecutor {
}
private boolean isAuthorizedSchema(final BackendConnection backendConnection, final String schema) {
- Optional<ShardingSphereUser> user = ProxyContext.getInstance().getMetaDataContexts().getAuthentication().findUser(backendConnection.getUsername());
+ Optional<ShardingSphereUser> user = ProxyContext.getInstance().getMetaDataContexts().getAuthentication().findUser(backendConnection.getGrantee());
Collection<String> authorizedSchemas = user.isPresent() ? user.get().getAuthorizedSchemas() : Collections.emptyList();
return authorizedSchemas.isEmpty() || authorizedSchemas.contains(schema);
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowDatabasesExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowDatabasesExecutorTest.java
index c7a0e0a..61c968c 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowDatabasesExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowDatabasesExecutorTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.proxy.backend.text.admin.mysql.executor;
+import org.apache.shardingsphere.infra.auth.Grantee;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
@@ -74,7 +75,7 @@ public final class ShowDatabasesExecutorTest {
}
private DefaultAuthentication getAuthentication() {
- ShardingSphereUser user = new ShardingSphereUser("root", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1)));
+ ShardingSphereUser user = new ShardingSphereUser("root", "", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1)));
DefaultAuthentication result = new DefaultAuthentication();
result.getUsers().put("root", user);
return result;
@@ -93,7 +94,7 @@ public final class ShowDatabasesExecutorTest {
private BackendConnection mockBackendConnection() {
BackendConnection result = mock(BackendConnection.class);
- when(result.getUsername()).thenReturn("root");
+ when(result.getGrantee()).thenReturn(new Grantee("root", ""));
return result;
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowTablesExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowTablesExecutorTest.java
index 0df200f..8d51d32 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowTablesExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/ShowTablesExecutorTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.proxy.backend.text.admin.mysql.executor;
+import org.apache.shardingsphere.infra.auth.Grantee;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
@@ -69,7 +70,7 @@ public final class ShowTablesExecutorTest {
}
private DefaultAuthentication getAuthentication() {
- ShardingSphereUser user = new ShardingSphereUser("root", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1)));
+ ShardingSphereUser user = new ShardingSphereUser("root", "", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1)));
DefaultAuthentication result = new DefaultAuthentication();
result.getUsers().put("root", user);
return result;
@@ -86,7 +87,7 @@ public final class ShowTablesExecutorTest {
private BackendConnection mockBackendConnection() {
BackendConnection result = mock(BackendConnection.class);
- when(result.getUsername()).thenReturn("root");
+ when(result.getGrantee()).thenReturn(new Grantee("root", ""));
when(result.getSchemaName()).thenReturn(String.format(SCHEMA_PATTERN, 0));
return result;
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/UseDatabaseExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/UseDatabaseExecutorTest.java
index 268aed5..3e6c1f7 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/UseDatabaseExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/mysql/executor/UseDatabaseExecutorTest.java
@@ -74,7 +74,7 @@ public final class UseDatabaseExecutorTest {
}
private DefaultAuthentication getAuthentication() {
- ShardingSphereUser user = new ShardingSphereUser("root", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1)));
+ ShardingSphereUser user = new ShardingSphereUser("root", "", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1)));
DefaultAuthentication result = new DefaultAuthentication();
result.getUsers().put("root", user);
return result;
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/data/impl/SchemaAssignedDatabaseBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/data/impl/SchemaAssignedDatabaseBackendHandlerTest.java
index 9f68b9c..33da425 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/data/impl/SchemaAssignedDatabaseBackendHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/data/impl/SchemaAssignedDatabaseBackendHandlerTest.java
@@ -107,7 +107,7 @@ public final class SchemaAssignedDatabaseBackendHandlerTest {
}
private DefaultAuthentication getAuthentication() {
- ShardingSphereUser user = new ShardingSphereUser("root", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1)));
+ ShardingSphereUser user = new ShardingSphereUser("root", "", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1)));
DefaultAuthentication result = new DefaultAuthentication();
result.getUsers().put("root", user);
return result;
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/data/impl/UnicastDatabaseBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/data/impl/UnicastDatabaseBackendHandlerTest.java
index fc066ba..580c1b2 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/data/impl/UnicastDatabaseBackendHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/data/impl/UnicastDatabaseBackendHandlerTest.java
@@ -97,7 +97,7 @@ public final class UnicastDatabaseBackendHandlerTest {
}
private DefaultAuthentication getAuthentication() {
- ShardingSphereUser user = new ShardingSphereUser("root", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1)));
+ ShardingSphereUser user = new ShardingSphereUser("root", "", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1)));
DefaultAuthentication result = new DefaultAuthentication();
result.getUsers().put("root", user);
return result;
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/server.yaml b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/server.yaml
index 3731ec2..a6830a6 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/server.yaml
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/server.yaml
@@ -36,6 +36,7 @@
#authentication:
# users:
# root:
+# hostname: 127.0.01
# password: root
# sharding:
# password: sharding
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 5f50481..bd3f2b0 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
@@ -21,6 +21,7 @@ import lombok.SneakyThrows;
import org.apache.shardingsphere.governance.context.metadata.GovernanceMetaDataContexts;
import org.apache.shardingsphere.governance.context.transaction.GovernanceTransactionContexts;
import org.apache.shardingsphere.governance.core.config.ConfigCenterNode;
+import org.apache.shardingsphere.infra.auth.Grantee;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
@@ -196,11 +197,11 @@ public final class GovernanceBootstrapInitializerTest extends AbstractBootstrapI
}
private void assertAuthentication(final DefaultAuthentication actual) {
- Optional<ShardingSphereUser> rootUser = actual.findUser("root");
+ Optional<ShardingSphereUser> rootUser = actual.findUser(new Grantee("root", ""));
assertTrue(rootUser.isPresent());
assertThat(rootUser.get().getPassword(), is("root"));
assertThat(rootUser.get().getAuthorizedSchemas().size(), is(0));
- Optional<ShardingSphereUser> shardingUser = actual.findUser("sharding");
+ Optional<ShardingSphereUser> shardingUser = actual.findUser(new Grantee("sharding", ""));
assertTrue(shardingUser.isPresent());
assertThat(shardingUser.get().getPassword(), is("sharding"));
assertThat(shardingUser.get().getAuthorizedSchemas().size(), is(1));
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 472d1b3..f6234ff 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,6 +17,7 @@
package org.apache.shardingsphere.proxy.initializer.impl;
+import org.apache.shardingsphere.infra.auth.Grantee;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlAuthenticationConfiguration;
@@ -148,7 +149,7 @@ public final class StandardBootstrapInitializerTest extends AbstractBootstrapIni
}
private void assertAuthentication(final DefaultAuthentication actual) {
- Optional<ShardingSphereUser> rootUser = actual.findUser("root");
+ Optional<ShardingSphereUser> rootUser = actual.findUser(new Grantee("root", ""));
assertTrue(rootUser.isPresent());
assertThat(rootUser.get().getPassword(), is("root"));
assertThat(rootUser.get().getAuthorizedSchemas().size(), is(2));
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 9857516..0244ad0 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,6 +19,7 @@ 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.auth.Grantee;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlAuthenticationConfiguration;
@@ -104,7 +105,7 @@ public final class YamlProxyConfigurationSwapperTest {
private void assertAuthentication(final ProxyConfiguration proxyConfig) {
DefaultAuthentication authentication = proxyConfig.getAuthentication();
assertNotNull(authentication);
- Optional<ShardingSphereUser> user = authentication.findUser("user1");
+ Optional<ShardingSphereUser> user = authentication.findUser(new Grantee("user1", ""));
assertTrue(user.isPresent());
assertThat(user.get().getPassword(), is("pass"));
Collection<String> authorizedSchemas = user.get().getAuthorizedSchemas();
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/auth/AuthenticationResultBuilder.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/auth/AuthenticationResultBuilder.java
index 43d21b1..95a027a 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/auth/AuthenticationResultBuilder.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/auth/AuthenticationResultBuilder.java
@@ -30,11 +30,12 @@ public final class AuthenticationResultBuilder {
* Create finished authentication result.
*
* @param username username
+ * @param hostname hostname
* @param database database
* @return finished authentication result
*/
- public static AuthenticationResult finished(final String username, final String database) {
- return new AuthenticationResult(username, database, true);
+ public static AuthenticationResult finished(final String username, final String hostname, final String database) {
+ return new AuthenticationResult(username, hostname, database, true);
}
/**
@@ -43,17 +44,18 @@ public final class AuthenticationResultBuilder {
* @return continued authentication result
*/
public static AuthenticationResult continued() {
- return new AuthenticationResult(null, null, false);
+ return new AuthenticationResult(null, null, null, false);
}
/**
* Create continued authentication result.
*
* @param username username
+ * @param hostname hostname
* @param database database
* @return continued authentication result
*/
- public static AuthenticationResult continued(final String username, final String database) {
- return new AuthenticationResult(username, database, false);
+ public static AuthenticationResult continued(final String username, final String hostname, final String database) {
+ return new AuthenticationResult(username, hostname, database, false);
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/FrontendChannelInboundHandler.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/FrontendChannelInboundHandler.java
index fdd3c0d..8cec3c0 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/FrontendChannelInboundHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/netty/FrontendChannelInboundHandler.java
@@ -22,6 +22,7 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.db.protocol.payload.PacketPayload;
+import org.apache.shardingsphere.infra.auth.Grantee;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
@@ -69,7 +70,7 @@ public final class FrontendChannelInboundHandler extends ChannelInboundHandlerAd
try (PacketPayload payload = databaseProtocolFrontendEngine.getCodecEngine().createPacketPayload(message)) {
AuthenticationResult authResult = databaseProtocolFrontendEngine.getAuthEngine().auth(context, payload);
if (authResult.isFinished()) {
- backendConnection.setUsername(authResult.getUsername());
+ backendConnection.setGrantee(new Grantee(authResult.getUsername(), authResult.getHostname()));
backendConnection.setCurrentSchema(authResult.getDatabase());
}
return authResult.isFinished();
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/auth/AuthenticationResultBuilderTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/auth/AuthenticationResultBuilderTest.java
index 5e9e4e5..b95193f 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/auth/AuthenticationResultBuilderTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/auth/AuthenticationResultBuilderTest.java
@@ -37,16 +37,18 @@ public final class AuthenticationResultBuilderTest {
@Test
public void assertContinued() {
- AuthenticationResult actual = AuthenticationResultBuilder.continued("username", "database");
+ AuthenticationResult actual = AuthenticationResultBuilder.continued("username", "127.0.0.1", "database");
assertThat(actual.getUsername(), is("username"));
+ assertThat(actual.getHostname(), is("127.0.0.1"));
assertThat(actual.getDatabase(), is("database"));
assertFalse(actual.isFinished());
}
@Test
public void assertFinished() {
- AuthenticationResult actual = AuthenticationResultBuilder.finished("username", "database");
+ AuthenticationResult actual = AuthenticationResultBuilder.finished("username", "127.0.0.1", "database");
assertThat(actual.getUsername(), is("username"));
+ assertThat(actual.getHostname(), is("127.0.0.1"));
assertThat(actual.getDatabase(), is("database"));
assertTrue(actual.isFinished());
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationEngine.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationEngine.java
index 78e8bd3..4e27a13 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationEngine.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationEngine.java
@@ -74,9 +74,9 @@ public final class MySQLAuthenticationEngine implements AuthenticationEngine {
} else if (MySQLConnectionPhase.AUTHENTICATION_METHOD_MISMATCH == connectionPhase) {
authenticationMethodMismatch((MySQLPacketPayload) payload);
}
- Optional<MySQLServerErrorCode> errorCode = authenticationHandler.login(currentAuthResult.getUsername(), authResponse, currentAuthResult.getDatabase());
+ Optional<MySQLServerErrorCode> errorCode = authenticationHandler.login(currentAuthResult.getUsername(), getHostAddress(context), authResponse, currentAuthResult.getDatabase());
context.writeAndFlush(errorCode.isPresent() ? createErrorPacket(errorCode.get(), context) : new MySQLOKPacket(++sequenceId));
- return AuthenticationResultBuilder.finished(currentAuthResult.getUsername(), currentAuthResult.getDatabase());
+ return AuthenticationResultBuilder.finished(currentAuthResult.getUsername(), getHostAddress(context), currentAuthResult.getDatabase());
}
private AuthenticationResult authPhaseFastPath(final ChannelHandlerContext context, final PacketPayload payload) {
@@ -90,9 +90,9 @@ public final class MySQLAuthenticationEngine implements AuthenticationEngine {
if (isClientPluginAuth(packet) && !MySQLAuthenticationMethod.SECURE_PASSWORD_AUTHENTICATION.getMethodName().equals(packet.getAuthPluginName())) {
connectionPhase = MySQLConnectionPhase.AUTHENTICATION_METHOD_MISMATCH;
context.writeAndFlush(new MySQLAuthSwitchRequestPacket(++sequenceId, MySQLAuthenticationMethod.SECURE_PASSWORD_AUTHENTICATION.getMethodName(), authenticationHandler.getAuthPluginData()));
- return AuthenticationResultBuilder.continued(packet.getUsername(), packet.getDatabase());
+ return AuthenticationResultBuilder.continued(packet.getUsername(), getHostAddress(context), packet.getDatabase());
}
- return AuthenticationResultBuilder.finished(packet.getUsername(), packet.getDatabase());
+ return AuthenticationResultBuilder.finished(packet.getUsername(), getHostAddress(context), packet.getDatabase());
}
private boolean isClientPluginAuth(final MySQLHandshakeResponse41Packet packet) {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationHandler.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationHandler.java
index 4e008ce..6d87a1e 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationHandler.java
@@ -23,6 +23,7 @@ import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerErrorCode;
import org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLAuthPluginData;
+import org.apache.shardingsphere.infra.auth.Grantee;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
@@ -43,13 +44,14 @@ public final class MySQLAuthenticationHandler {
/**
* Login.
*
- * @param username username.
+ * @param username username
+ * @param hostname hostname
* @param authResponse auth response
* @param database database
* @return login success or failure
*/
- public Optional<MySQLServerErrorCode> login(final String username, final byte[] authResponse, final String database) {
- Optional<ShardingSphereUser> user = ProxyContext.getInstance().getMetaDataContexts().getAuthentication().findUser(username);
+ public Optional<MySQLServerErrorCode> login(final String username, final String hostname, final byte[] authResponse, final String database) {
+ Optional<ShardingSphereUser> user = ProxyContext.getInstance().getMetaDataContexts().getAuthentication().findUser(new Grantee(username, hostname));
if (!user.isPresent() || !isPasswordRight(user.get().getPassword(), authResponse)) {
return Optional.of(MySQLServerErrorCode.ER_ACCESS_DENIED_ERROR);
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/admin/initdb/MySQLComInitDbExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/admin/initdb/MySQLComInitDbExecutor.java
index 64ae3eb..a5201c8 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/admin/initdb/MySQLComInitDbExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/admin/initdb/MySQLComInitDbExecutor.java
@@ -53,7 +53,7 @@ public final class MySQLComInitDbExecutor implements CommandExecutor {
}
private boolean isAuthorizedSchema(final String schema) {
- Optional<ShardingSphereUser> user = ProxyContext.getInstance().getMetaDataContexts().getAuthentication().findUser(backendConnection.getUsername());
+ Optional<ShardingSphereUser> user = ProxyContext.getInstance().getMetaDataContexts().getAuthentication().findUser(backendConnection.getGrantee());
Collection<String> authorizedSchemas = user.isPresent() ? user.get().getAuthorizedSchemas() : Collections.emptyList();
return authorizedSchemas.isEmpty() || authorizedSchemas.contains(schema);
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngineTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngineTest.java
index 21c143b..c33c495 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngineTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngineTest.java
@@ -103,9 +103,12 @@ public final class MySQLFrontendEngineTest {
@Test
public void assertAuthWhenLoginSuccess() {
setConnectionPhase(MySQLConnectionPhase.AUTH_PHASE_FAST_PATH);
- ShardingSphereUser user = new ShardingSphereUser("", Collections.singleton("db1"));
+ ShardingSphereUser user = new ShardingSphereUser("", "", Collections.singleton("db1"));
setAuthentication(user);
when(payload.readStringNul()).thenReturn("root");
+ when(payload.readStringNulByBytes()).thenReturn("root".getBytes());
+ when(channel.remoteAddress()).thenReturn(new InetSocketAddress("localhost", 3307));
+ when(context.channel()).thenReturn(channel);
AuthenticationResult actual = mysqlFrontendEngine.getAuthEngine().auth(context, payload);
assertThat(actual.getUsername(), is("root"));
assertNull(actual.getDatabase());
@@ -116,7 +119,7 @@ public final class MySQLFrontendEngineTest {
@Test
public void assertAuthWhenLoginFailure() {
setConnectionPhase(MySQLConnectionPhase.AUTH_PHASE_FAST_PATH);
- ShardingSphereUser user = new ShardingSphereUser("error", Collections.singleton("db1"));
+ ShardingSphereUser user = new ShardingSphereUser("error", "", Collections.singleton("db1"));
setAuthentication(user);
when(payload.readStringNul()).thenReturn("root");
when(payload.readStringNulByBytes()).thenReturn("root".getBytes());
@@ -132,7 +135,7 @@ public final class MySQLFrontendEngineTest {
@Test
public void assertErrorMsgWhenLoginFailure() throws UnknownHostException {
setConnectionPhase(MySQLConnectionPhase.AUTH_PHASE_FAST_PATH);
- ShardingSphereUser user = new ShardingSphereUser("error", Collections.singleton("db1"));
+ ShardingSphereUser user = new ShardingSphereUser("error", "", Collections.singleton("db1"));
setAuthentication(user);
when(payload.readStringNul()).thenReturn("root");
when(payload.readStringNulByBytes()).thenReturn("root".getBytes());
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationEngineTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationEngineTest.java
index 029052a..35b3232 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationEngineTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationEngineTest.java
@@ -41,6 +41,7 @@ import org.junit.Before;
import org.junit.Test;
import java.lang.reflect.Field;
+import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Collections;
import java.util.Optional;
@@ -87,6 +88,11 @@ public final class MySQLAuthenticationEngineTest {
setConnectionPhase(MySQLConnectionPhase.AUTH_PHASE_FAST_PATH);
MySQLPacketPayload payload = mock(MySQLPacketPayload.class);
ChannelHandlerContext channelHandlerContext = mock(ChannelHandlerContext.class);
+ Channel channel = mock(Channel.class);
+ when(payload.readStringEOFByBytes()).thenReturn(authResponse);
+ when(payload.readStringNulByBytes()).thenReturn("root".getBytes());
+ when(channel.remoteAddress()).thenReturn(new InetSocketAddress("localhost", 3307));
+ when(channelHandlerContext.channel()).thenReturn(channel);
when(payload.readInt4()).thenReturn(MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH.getValue());
authenticationEngine.auth(channelHandlerContext, payload);
assertThat(getConnectionPhase(), is(MySQLConnectionPhase.AUTHENTICATION_METHOD_MISMATCH));
@@ -96,8 +102,12 @@ public final class MySQLAuthenticationEngineTest {
public void assertAuthSwitchResponse() {
setConnectionPhase(MySQLConnectionPhase.AUTHENTICATION_METHOD_MISMATCH);
MySQLPacketPayload payload = mock(MySQLPacketPayload.class);
+ Channel channel = mock(Channel.class);
ChannelHandlerContext channelHandlerContext = mock(ChannelHandlerContext.class);
when(payload.readStringEOFByBytes()).thenReturn(authResponse);
+ when(payload.readStringNulByBytes()).thenReturn("root".getBytes());
+ when(channel.remoteAddress()).thenReturn(new InetSocketAddress("localhost", 3307));
+ when(channelHandlerContext.channel()).thenReturn(channel);
setAuthenticationResult();
authenticationEngine.auth(channelHandlerContext, payload);
assertThat(getAuthResponse(), is(authResponse));
@@ -107,7 +117,7 @@ public final class MySQLAuthenticationEngineTest {
private void setAuthenticationResult() {
Field field = MySQLAuthenticationEngine.class.getDeclaredField("currentAuthResult");
field.setAccessible(true);
- field.set(authenticationEngine, AuthenticationResultBuilder.continued("root", "sharding_db"));
+ field.set(authenticationEngine, AuthenticationResultBuilder.continued("root", "", "sharding_db"));
}
@Test
@@ -115,7 +125,7 @@ public final class MySQLAuthenticationEngineTest {
setConnectionPhase(MySQLConnectionPhase.AUTH_PHASE_FAST_PATH);
ChannelHandlerContext context = getContext();
setMetaDataContexts();
- when(authenticationHandler.login(anyString(), any(), anyString())).thenReturn(Optional.of(MySQLServerErrorCode.ER_ACCESS_DENIED_ERROR));
+ when(authenticationHandler.login(anyString(), any(), any(), anyString())).thenReturn(Optional.of(MySQLServerErrorCode.ER_ACCESS_DENIED_ERROR));
authenticationEngine.auth(context, getPayload("root", "sharding_db", authResponse));
verify(context).writeAndFlush(any(MySQLErrPacket.class));
}
@@ -133,7 +143,7 @@ public final class MySQLAuthenticationEngineTest {
public void assertAuth() throws NoSuchFieldException, IllegalAccessException {
setConnectionPhase(MySQLConnectionPhase.AUTH_PHASE_FAST_PATH);
ChannelHandlerContext context = getContext();
- when(authenticationHandler.login(anyString(), any(), anyString())).thenReturn(Optional.empty());
+ when(authenticationHandler.login(anyString(), any(), any(), anyString())).thenReturn(Optional.empty());
setMetaDataContexts();
authenticationEngine.auth(context, getPayload("root", "sharding_db", authResponse));
verify(context).writeAndFlush(any(MySQLOKPacket.class));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationHandlerTest.java
index fbd4b1c..3f29874 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/auth/MySQLAuthenticationHandlerTest.java
@@ -72,37 +72,37 @@ public final class MySQLAuthenticationHandlerTest {
@Test
public void assertLoginWithPassword() {
- setAuthentication(new ShardingSphereUser("root", Collections.singleton("db1")));
+ setAuthentication(new ShardingSphereUser("root", "", Collections.singleton("db1")));
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());
+ assertFalse(authenticationHandler.login("root", "", authResponse, "db1").isPresent());
}
@Test
public void assertLoginWithAbsentUser() {
- setAuthentication(new ShardingSphereUser("root", Collections.singleton("db1")));
+ setAuthentication(new ShardingSphereUser("root", "", Collections.singleton("db1")));
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));
+ assertThat(authenticationHandler.login("root1", "", authResponse, "db1").orElse(null), is(MySQLServerErrorCode.ER_ACCESS_DENIED_ERROR));
}
@Test
public void assertLoginWithIncorrectPassword() {
- setAuthentication(new ShardingSphereUser("root", Collections.singleton("db1")));
+ setAuthentication(new ShardingSphereUser("root", "", Collections.singleton("db1")));
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));
+ assertThat(authenticationHandler.login("root", "", authResponse, "db1").orElse(null), is(MySQLServerErrorCode.ER_ACCESS_DENIED_ERROR));
}
@Test
public void assertLoginWithoutPassword() {
- setAuthentication(new ShardingSphereUser(null, null));
+ setAuthentication(new ShardingSphereUser(null, "", null));
byte[] authResponse = {};
- assertFalse(authenticationHandler.login("root", authResponse, "db1").isPresent());
+ assertFalse(authenticationHandler.login("root", "", authResponse, "db1").isPresent());
}
@Test
public void assertLoginWithUnauthorizedSchema() {
- setAuthentication(new ShardingSphereUser("root", Collections.singleton("db1")));
+ setAuthentication(new ShardingSphereUser("root", "", Collections.singleton("db1")));
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("root", authResponse, "db2").orElse(null), is(MySQLServerErrorCode.ER_DBACCESS_DENIED_ERROR));
+ assertThat(authenticationHandler.login("root", "", authResponse, "db2").orElse(null), is(MySQLServerErrorCode.ER_DBACCESS_DENIED_ERROR));
}
@Test
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/auth/PostgreSQLAuthenticationEngine.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/auth/PostgreSQLAuthenticationEngine.java
index 7cea77c..570c2c7 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/auth/PostgreSQLAuthenticationEngine.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/auth/PostgreSQLAuthenticationEngine.java
@@ -99,7 +99,7 @@ public final class PostgreSQLAuthenticationEngine implements AuthenticationEngin
}
md5Salt = PostgreSQLRandomGenerator.getInstance().generateRandomBytes(4);
context.writeAndFlush(new PostgreSQLAuthenticationMD5PasswordPacket(md5Salt));
- currentAuthResult = AuthenticationResultBuilder.continued(username, databaseName);
+ currentAuthResult = AuthenticationResultBuilder.continued(username, "", databaseName);
return currentAuthResult;
}
@@ -127,7 +127,7 @@ public final class PostgreSQLAuthenticationEngine implements AuthenticationEngin
context.write(new PostgreSQLParameterStatusPacket("client_encoding", "UTF8"));
context.write(new PostgreSQLParameterStatusPacket("server_encoding", "UTF8"));
context.writeAndFlush(new PostgreSQLReadyForQueryPacket());
- return AuthenticationResultBuilder.finished(currentAuthResult.getUsername(), currentAuthResult.getDatabase());
+ return AuthenticationResultBuilder.finished(currentAuthResult.getUsername(), "", currentAuthResult.getDatabase());
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/auth/PostgreSQLAuthenticationHandler.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/auth/PostgreSQLAuthenticationHandler.java
index 702b04e..78cb35e 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/auth/PostgreSQLAuthenticationHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/auth/PostgreSQLAuthenticationHandler.java
@@ -25,6 +25,7 @@ import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLErrorCode;
import org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.PostgreSQLPasswordMessagePacket;
+import org.apache.shardingsphere.infra.auth.Grantee;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
@@ -48,7 +49,7 @@ public final class PostgreSQLAuthenticationHandler {
* @return PostgreSQL login result
*/
public static PostgreSQLLoginResult loginWithMd5Password(final String username, final String databaseName, final byte[] md5Salt, final PostgreSQLPasswordMessagePacket passwordMessagePacket) {
- Optional<ShardingSphereUser> user = ProxyContext.getInstance().getMetaDataContexts().getAuthentication().findUser(username);
+ Optional<ShardingSphereUser> user = ProxyContext.getInstance().getMetaDataContexts().getAuthentication().findUser(new Grantee(username, ""));
if (!user.isPresent()) {
return new PostgreSQLLoginResult(PostgreSQLErrorCode.INVALID_AUTHORIZATION_SPECIFICATION, String.format("unknown username: %s", username));
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/auth/PostgreSQLAuthenticationEngineTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/auth/PostgreSQLAuthenticationEngineTest.java
index 055f5ab..e01d5a9 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/auth/PostgreSQLAuthenticationEngineTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/auth/PostgreSQLAuthenticationEngineTest.java
@@ -126,7 +126,7 @@ public final class PostgreSQLAuthenticationEngineTest {
ProxyContext proxyContext = ProxyContext.getInstance();
StandardMetaDataContexts standardMetaDataContexts = new StandardMetaDataContexts();
- ((DefaultAuthentication) standardMetaDataContexts.getAuthentication()).getUsers().put(username, new ShardingSphereUser(password, null));
+ ((DefaultAuthentication) standardMetaDataContexts.getAuthentication()).getUsers().put(username, new ShardingSphereUser(password, "", null));
proxyContext.init(standardMetaDataContexts, mock(TransactionContexts.class));
actual = engine.auth(channelHandlerContext, payload);
assertThat(actual.isFinished(), is(password.equals(inputPassword)));
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/auth/AuthenticationResult.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/auth/AuthenticationResult.java
index 878cbd4..3aa6f26 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/auth/AuthenticationResult.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-spi/src/main/java/org/apache/shardingsphere/proxy/frontend/auth/AuthenticationResult.java
@@ -29,6 +29,8 @@ public final class AuthenticationResult {
private final String username;
+ private final String hostname;
+
private final String database;
private final boolean finished;