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/02/02 11:09:39 UTC
[shardingsphere] branch master updated: New feature: Privileges for
Proxy (#9274)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 c4ffe96 New feature: Privileges for Proxy (#9274)
c4ffe96 is described below
commit c4ffe969bdb008776c4d7ca48061ec4776e3c049
Author: Juan Pan(Trista) <pa...@apache.org>
AuthorDate: Tue Feb 2 19:09:08 2021 +0800
New feature: Privileges for Proxy (#9274)
* New feature: Privileges for Proxy
* javadoc
---
.../metadata/GovernanceMetaDataContexts.java | 34 ++++--
.../metadata/GovernanceMetaDataContextsTest.java | 10 +-
.../governance/core/config/ConfigCenter.java | 26 ++---
.../listener/AuthenticationChangedListener.java | 8 +-
...ChangedEvent.java => UserRuleChangedEvent.java} | 10 +-
.../governance/core/facade/GovernanceFacade.java | 8 +-
.../yaml/config/YamlConfigurationConverter.java | 12 +--
.../governance/core/config/ConfigCenterTest.java | 19 ++--
.../AuthenticationChangedListenerTest.java | 5 +-
.../core/facade/GovernanceFacadeTest.java | 8 +-
.../shardingsphere/infra/auth/Authentication.java | 18 ++++
.../infra/auth/builtin/DefaultAuthentication.java | 34 +++++-
...uration.java => YamlUserRuleConfiguration.java} | 4 +-
...onYamlSwapper.java => UserRuleYamlSwapper.java} | 21 ++--
.../InstancePrivilege.java} | 27 +++--
.../ShardingSpherePrivilege.java} | 25 +++--
.../privilege/builder/PrivilegeBuilder.java | 117 +++++++++++++++++++++
.../privilege/builder/PrivilegeMerger.java | 53 ++++++++++
.../privilege/loader/PrivilegeLoader.java} | 23 ++--
.../privilege/loader/PrivilegeLoaderEngine.java} | 38 ++++---
...apperTest.java => UserRuleYamlSwapperTest.java} | 27 ++---
.../context/metadata/MetaDataContextsBuilder.java | 20 +++-
.../metadata/MetaDataContextsBuilderTest.java | 6 +-
.../GovernanceShardingSphereDataSource.java | 8 +-
.../mysql/executor/ShowDatabasesExecutorTest.java | 4 +-
.../mysql/executor/ShowTablesExecutorTest.java | 4 +-
.../mysql/executor/UseDatabaseExecutorTest.java | 4 +-
.../SchemaAssignedDatabaseBackendHandlerTest.java | 4 +-
.../impl/UnicastDatabaseBackendHandlerTest.java | 4 +-
.../impl/AbstractBootstrapInitializer.java | 2 +-
.../impl/GovernanceBootstrapInitializer.java | 13 +--
.../impl/GovernanceBootstrapInitializerTest.java | 2 +-
.../impl/StandardBootstrapInitializerTest.java | 8 +-
.../proxy/config/ProxyConfiguration.java | 4 +-
.../config/yaml/YamlProxyServerConfiguration.java | 4 +-
.../swapper/YamlProxyConfigurationSwapper.java | 10 +-
.../swapper/YamlProxyConfigurationSwapperTest.java | 6 +-
.../frontend/mysql/MySQLFrontendEngineTest.java | 3 +-
.../mysql/auth/MySQLAuthenticationHandlerTest.java | 3 +-
.../auth/PostgreSQLAuthenticationEngineTest.java | 4 +-
40 files changed, 468 insertions(+), 172 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 3061c92..3a1b4ce 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
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.governance.context.metadata;
import com.google.common.collect.Maps;
import com.google.common.eventbus.Subscribe;
-import org.apache.shardingsphere.governance.core.event.model.auth.AuthenticationChangedEvent;
+import org.apache.shardingsphere.governance.core.event.model.auth.UserRuleChangedEvent;
import org.apache.shardingsphere.governance.core.event.model.datasource.DataSourceChangeCompletedEvent;
import org.apache.shardingsphere.governance.core.event.model.datasource.DataSourceChangedEvent;
import org.apache.shardingsphere.governance.core.event.model.lock.UnlockEvent;
@@ -34,6 +34,9 @@ import org.apache.shardingsphere.governance.core.registry.event.DisabledStateCha
import org.apache.shardingsphere.governance.core.registry.event.PrimaryStateChangedEvent;
import org.apache.shardingsphere.governance.core.registry.schema.GovernanceSchema;
import org.apache.shardingsphere.infra.auth.Authentication;
+import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
+import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
+import org.apache.shardingsphere.infra.auth.privilege.ShardingSpherePrivilege;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
@@ -62,6 +65,7 @@ import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Optional;
import java.util.stream.Collectors;
/**
@@ -199,8 +203,11 @@ public final class GovernanceMetaDataContexts implements MetaDataContexts {
* @param event authentication changed event
*/
@Subscribe
- public synchronized void renew(final AuthenticationChangedEvent event) {
- metaDataContexts = new StandardMetaDataContexts(metaDataContexts.getMetaDataMap(), metaDataContexts.getExecutorEngine(), event.getAuthentication(), metaDataContexts.getProps());
+ public synchronized void renew(final UserRuleChangedEvent event) {
+ Collection<ShardingSphereUser> users = event.getUsers();
+ DefaultAuthentication authentication = new DefaultAuthentication(getNewUsers(users));
+ authentication.getAuthentication().putAll(getModifiedUsers(users));
+ metaDataContexts = new StandardMetaDataContexts(metaDataContexts.getMetaDataMap(), metaDataContexts.getExecutorEngine(), authentication, metaDataContexts.getProps());
}
/**
@@ -295,7 +302,7 @@ public final class GovernanceMetaDataContexts implements MetaDataContexts {
governanceFacade.getConfigCenter().loadDataSourceConfigurations(schemaName)));
MetaDataContextsBuilder metaDataContextsBuilder = new MetaDataContextsBuilder(dataSourcesMap,
Collections.singletonMap(schemaName, governanceFacade.getConfigCenter().loadRuleConfigurations(schemaName)),
- metaDataContexts.getAuthentication(), metaDataContexts.getProps().getProps());
+ metaDataContexts.getAuthentication().getAuthentication().keySet(), metaDataContexts.getProps().getProps());
return metaDataContextsBuilder.build().getMetaDataMap().get(schemaName);
}
@@ -314,7 +321,7 @@ public final class GovernanceMetaDataContexts implements MetaDataContexts {
private ShardingSphereMetaData getChangedMetaData(final ShardingSphereMetaData oldMetaData, final Collection<RuleConfiguration> ruleConfigs) throws SQLException {
MetaDataContextsBuilder builder = new MetaDataContextsBuilder(Collections.singletonMap(oldMetaData.getName(), oldMetaData.getResource().getDataSources()),
- Collections.singletonMap(oldMetaData.getName(), ruleConfigs), metaDataContexts.getAuthentication(), metaDataContexts.getProps().getProps());
+ Collections.singletonMap(oldMetaData.getName(), ruleConfigs), metaDataContexts.getAuthentication().getAuthentication().keySet(), metaDataContexts.getProps().getProps());
return builder.build().getMetaDataMap().values().iterator().next();
}
@@ -325,8 +332,8 @@ public final class GovernanceMetaDataContexts implements MetaDataContexts {
oldMetaData.getResource().close(modifiedDataSources.keySet());
Map<String, Map<String, DataSource>> dataSourcesMap = Collections.singletonMap(oldMetaData.getName(),
getNewDataSources(oldMetaData.getResource().getDataSources(), getAddedDataSources(oldMetaData, newDataSourceConfigs), modifiedDataSources, deletedDataSources));
- return new MetaDataContextsBuilder(dataSourcesMap, Collections.singletonMap(oldMetaData.getName(), oldMetaData.getRuleMetaData().getConfigurations()), metaDataContexts.getAuthentication(),
- metaDataContexts.getProps().getProps()).build().getMetaDataMap().get(oldMetaData.getName());
+ return new MetaDataContextsBuilder(dataSourcesMap, Collections.singletonMap(oldMetaData.getName(), oldMetaData.getRuleMetaData().getConfigurations()),
+ metaDataContexts.getAuthentication().getAuthentication().keySet(), metaDataContexts.getProps().getProps()).build().getMetaDataMap().get(oldMetaData.getName());
}
private Map<String, DataSource> getNewDataSources(final Map<String, DataSource> oldDataSources,
@@ -368,4 +375,17 @@ public final class GovernanceMetaDataContexts implements MetaDataContexts {
}
return result;
}
+
+ private Collection<ShardingSphereUser> getNewUsers(final Collection<ShardingSphereUser> users) {
+ return users.stream().filter(each -> !metaDataContexts.getAuthentication().findUser(each.getGrantee()).isPresent()).collect(Collectors.toList());
+ }
+
+ private Map<ShardingSphereUser, ShardingSpherePrivilege> getModifiedUsers(final Collection<ShardingSphereUser> users) {
+ Map<ShardingSphereUser, ShardingSpherePrivilege> result = new LinkedHashMap<>();
+ for (Entry<ShardingSphereUser, ShardingSpherePrivilege> entry : metaDataContexts.getAuthentication().getAuthentication().entrySet()) {
+ Optional<ShardingSphereUser> modified = users.stream().filter(each -> each.getGrantee().equals(entry.getKey().getGrantee())).findFirst();
+ modified.ifPresent(shardingSphereUser -> result.put(shardingSphereUser, entry.getValue()));
+ }
+ return result;
+ }
}
diff --git a/shardingsphere-governance/shardingsphere-governance-context/src/test/java/org/apache/shardingsphere/governance/context/metadata/GovernanceMetaDataContextsTest.java b/shardingsphere-governance/shardingsphere-governance-context/src/test/java/org/apache/shardingsphere/governance/context/metadata/GovernanceMetaDataContextsTest.java
index 6db48d1..3c56c85 100644
--- a/shardingsphere-governance/shardingsphere-governance-context/src/test/java/org/apache/shardingsphere/governance/context/metadata/GovernanceMetaDataContextsTest.java
+++ b/shardingsphere-governance/shardingsphere-governance-context/src/test/java/org/apache/shardingsphere/governance/context/metadata/GovernanceMetaDataContextsTest.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.governance.context.metadata;
import org.apache.shardingsphere.governance.core.config.ConfigCenter;
-import org.apache.shardingsphere.governance.core.event.model.auth.AuthenticationChangedEvent;
+import org.apache.shardingsphere.governance.core.event.model.auth.UserRuleChangedEvent;
import org.apache.shardingsphere.governance.core.event.model.datasource.DataSourceChangedEvent;
import org.apache.shardingsphere.governance.core.event.model.metadata.MetaDataDeletedEvent;
import org.apache.shardingsphere.governance.core.event.model.metadata.MetaDataPersistedEvent;
@@ -34,7 +34,6 @@ import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
@@ -77,9 +76,6 @@ public final class GovernanceMetaDataContextsTest {
private final ConfigurationProperties props = new ConfigurationProperties(new Properties());
@Mock
- private DatabaseType databaseType;
-
- @Mock
private GovernanceFacade governanceFacade;
@Mock
@@ -169,9 +165,9 @@ public final class GovernanceMetaDataContextsTest {
@Test
public void assertAuthenticationChanged() {
DefaultAuthentication authentication = new DefaultAuthentication();
- AuthenticationChangedEvent event = new AuthenticationChangedEvent(authentication);
+ UserRuleChangedEvent event = new UserRuleChangedEvent(authentication.getAuthentication().keySet());
governanceMetaDataContexts.renew(event);
- assertThat(governanceMetaDataContexts.getAuthentication(), is(authentication));
+ assertThat(governanceMetaDataContexts.getAuthentication().getAuthentication().size(), is(authentication.getAuthentication().size()));
}
@Test
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java
index b32d005..2c591fb 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/ConfigCenter.java
@@ -39,8 +39,8 @@ import org.apache.shardingsphere.governance.core.yaml.config.schema.YamlSchema;
import org.apache.shardingsphere.governance.core.yaml.swapper.DataSourceConfigurationYamlSwapper;
import org.apache.shardingsphere.governance.core.yaml.swapper.SchemaYamlSwapper;
import org.apache.shardingsphere.governance.repository.api.ConfigurationRepository;
-import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
-import org.apache.shardingsphere.infra.auth.builtin.yaml.swapper.AuthenticationYamlSwapper;
+import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
+import org.apache.shardingsphere.infra.auth.builtin.yaml.swapper.UserRuleYamlSwapper;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
@@ -51,6 +51,7 @@ import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
@@ -98,12 +99,12 @@ public final class ConfigCenter {
/**
* Persist global configuration.
*
- * @param authentication authentication
+ * @param users user
* @param props properties
* @param isOverwrite is overwrite config center's configuration
*/
- public void persistGlobalConfiguration(final DefaultAuthentication authentication, final Properties props, final boolean isOverwrite) {
- persistAuthentication(authentication, isOverwrite);
+ public void persistGlobalConfiguration(final Collection<ShardingSphereUser> users, final Properties props, final boolean isOverwrite) {
+ persistAuthentication(users, isOverwrite);
persistProperties(props, isOverwrite);
}
@@ -265,9 +266,10 @@ public final class ConfigCenter {
return result;
}
- private void persistAuthentication(final DefaultAuthentication authentication, final boolean isOverwrite) {
- if (null != authentication && (isOverwrite || !hasAuthentication())) {
- repository.persist(node.getAuthenticationPath(), YamlEngine.marshal(new AuthenticationYamlSwapper().swapToYamlConfiguration(authentication)));
+ private void persistAuthentication(final Collection<ShardingSphereUser> users, final boolean isOverwrite) {
+ if (!users.isEmpty() && (isOverwrite || !hasAuthentication())) {
+ repository.persist(node.getAuthenticationPath(),
+ YamlEngine.marshal(new UserRuleYamlSwapper().swapToYamlConfiguration(users)));
}
}
@@ -319,14 +321,14 @@ public final class ConfigCenter {
}
/**
- * Load authentication.
+ * Load user rule.
*
* @return authentication
*/
- public DefaultAuthentication loadAuthentication() {
+ public Collection<ShardingSphereUser> loadUserRule() {
return hasAuthentication()
- ? YamlConfigurationConverter.convertAuthentication(repository.get(node.getAuthenticationPath()))
- : new DefaultAuthentication();
+ ? YamlConfigurationConverter.convertUserRule(repository.get(node.getAuthenticationPath()))
+ : Collections.emptyList();
}
/**
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/AuthenticationChangedListener.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/AuthenticationChangedListener.java
index 464552e..66d746f 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/AuthenticationChangedListener.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/AuthenticationChangedListener.java
@@ -20,11 +20,11 @@ package org.apache.shardingsphere.governance.core.config.listener;
import org.apache.shardingsphere.governance.core.config.ConfigCenterNode;
import org.apache.shardingsphere.governance.core.event.listener.PostGovernanceRepositoryEventListener;
import org.apache.shardingsphere.governance.core.event.model.GovernanceEvent;
-import org.apache.shardingsphere.governance.core.event.model.auth.AuthenticationChangedEvent;
+import org.apache.shardingsphere.governance.core.event.model.auth.UserRuleChangedEvent;
import org.apache.shardingsphere.governance.repository.api.ConfigurationRepository;
import org.apache.shardingsphere.governance.repository.api.listener.DataChangedEvent;
-import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlAuthenticationConfiguration;
-import org.apache.shardingsphere.infra.auth.builtin.yaml.swapper.AuthenticationYamlSwapper;
+import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlUserRuleConfiguration;
+import org.apache.shardingsphere.infra.auth.builtin.yaml.swapper.UserRuleYamlSwapper;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
import java.util.Collections;
@@ -41,6 +41,6 @@ public final class AuthenticationChangedListener extends PostGovernanceRepositor
@Override
protected Optional<GovernanceEvent> createEvent(final DataChangedEvent event) {
- return Optional.of(new AuthenticationChangedEvent(new AuthenticationYamlSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(), YamlAuthenticationConfiguration.class))));
+ return Optional.of(new UserRuleChangedEvent(new UserRuleYamlSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(), YamlUserRuleConfiguration.class))));
}
}
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/event/model/auth/AuthenticationChangedEvent.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/event/model/auth/UserRuleChangedEvent.java
similarity index 81%
rename from shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/event/model/auth/AuthenticationChangedEvent.java
rename to shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/event/model/auth/UserRuleChangedEvent.java
index 3ed0033..09f4628 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/event/model/auth/AuthenticationChangedEvent.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/event/model/auth/UserRuleChangedEvent.java
@@ -20,14 +20,16 @@ package org.apache.shardingsphere.governance.core.event.model.auth;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.governance.core.event.model.GovernanceEvent;
-import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
+import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
+
+import java.util.Collection;
/**
- * Authentication changed event.
+ * User rule changed event.
*/
@RequiredArgsConstructor
@Getter
-public final class AuthenticationChangedEvent implements GovernanceEvent {
+public final class UserRuleChangedEvent implements GovernanceEvent {
- private final DefaultAuthentication authentication;
+ private final Collection<ShardingSphereUser> users;
}
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/facade/GovernanceFacade.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/facade/GovernanceFacade.java
index a47b0bd..544847a 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/facade/GovernanceFacade.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/facade/GovernanceFacade.java
@@ -23,7 +23,7 @@ import org.apache.shardingsphere.governance.core.facade.listener.GovernanceListe
import org.apache.shardingsphere.governance.core.facade.repository.GovernanceRepositoryFacade;
import org.apache.shardingsphere.governance.core.registry.RegistryCenter;
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.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
@@ -72,12 +72,12 @@ public final class GovernanceFacade implements AutoCloseable {
*
* @param dataSourceConfigMap schema data source configuration map
* @param schemaRuleMap schema rule map
- * @param authentication authentication
+ * @param users users
* @param props properties
*/
public void onlineInstance(final Map<String, Map<String, DataSourceConfiguration>> dataSourceConfigMap,
- final Map<String, Collection<RuleConfiguration>> schemaRuleMap, final DefaultAuthentication authentication, final Properties props) {
- configCenter.persistGlobalConfiguration(authentication, props, isOverwrite);
+ final Map<String, Collection<RuleConfiguration>> schemaRuleMap, final Collection<ShardingSphereUser> users, final Properties props) {
+ configCenter.persistGlobalConfiguration(users, props, isOverwrite);
for (Entry<String, Map<String, DataSourceConfiguration>> entry : dataSourceConfigMap.entrySet()) {
configCenter.persistConfigurations(entry.getKey(), dataSourceConfigMap.get(entry.getKey()), schemaRuleMap.get(entry.getKey()), isOverwrite);
}
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/yaml/config/YamlConfigurationConverter.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/yaml/config/YamlConfigurationConverter.java
index d823062..624647f 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/yaml/config/YamlConfigurationConverter.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/yaml/config/YamlConfigurationConverter.java
@@ -18,9 +18,9 @@
package org.apache.shardingsphere.governance.core.yaml.config;
import org.apache.shardingsphere.governance.core.yaml.swapper.DataSourceConfigurationYamlSwapper;
-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.swapper.AuthenticationYamlSwapper;
+import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
+import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlUserRuleConfiguration;
+import org.apache.shardingsphere.infra.auth.builtin.yaml.swapper.UserRuleYamlSwapper;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
@@ -62,13 +62,13 @@ public final class YamlConfigurationConverter {
}
/**
- * Convert authentication from YAML content.
+ * Convert user rule from YAML content.
*
* @param yamlContent YAML content
* @return authentication
*/
- public static DefaultAuthentication convertAuthentication(final String yamlContent) {
- return new AuthenticationYamlSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAuthenticationConfiguration.class));
+ public static Collection<ShardingSphereUser> convertUserRule(final String yamlContent) {
+ return new UserRuleYamlSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlUserRuleConfiguration.class));
}
/**
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 c51cb4b..a3485b3 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
@@ -34,8 +34,9 @@ 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;
-import org.apache.shardingsphere.infra.auth.builtin.yaml.swapper.AuthenticationYamlSwapper;
+import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlUserRuleConfiguration;
+import org.apache.shardingsphere.infra.auth.builtin.yaml.swapper.UserRuleYamlSwapper;
+import org.apache.shardingsphere.infra.auth.privilege.ShardingSpherePrivilege;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
@@ -262,7 +263,7 @@ public final class ConfigCenterTest {
@Test
public void assertPersistGlobalConfiguration() {
ConfigCenter configCenter = new ConfigCenter(configurationRepository);
- configCenter.persistGlobalConfiguration(createAuthentication(), createProperties(), true);
+ configCenter.persistGlobalConfiguration(createAuthentication().getAuthentication().keySet(), createProperties(), true);
verify(configurationRepository, times(0)).persist("/authentication", readYAML(AUTHENTICATION_YAML));
verify(configurationRepository).persist("/props", PROPS_YAML);
}
@@ -313,7 +314,13 @@ public final class ConfigCenterTest {
}
private DefaultAuthentication createAuthentication() {
- return new AuthenticationYamlSwapper().swapToObject(YamlEngine.unmarshal(readYAML(AUTHENTICATION_YAML), YamlAuthenticationConfiguration.class));
+ Collection<ShardingSphereUser> users =
+ new UserRuleYamlSwapper().swapToObject(YamlEngine.unmarshal(readYAML(AUTHENTICATION_YAML), YamlUserRuleConfiguration.class));
+ DefaultAuthentication result = new DefaultAuthentication();
+ for (ShardingSphereUser each : users) {
+ result.getAuthentication().put(each, new ShardingSpherePrivilege());
+ }
+ return result;
}
private Properties createProperties() {
@@ -425,8 +432,8 @@ public final class ConfigCenterTest {
public void assertLoadAuthentication() {
when(configurationRepository.get("/authentication")).thenReturn(readYAML(AUTHENTICATION_YAML));
ConfigCenter configCenter = new ConfigCenter(configurationRepository);
- DefaultAuthentication actual = configCenter.loadAuthentication();
- Optional<ShardingSphereUser> user = actual.findUser(new Grantee("root1", ""));
+ Collection<ShardingSphereUser> actual = configCenter.loadUserRule();
+ Optional<ShardingSphereUser> user = actual.stream().filter(each -> each.getGrantee().equals(new Grantee("root1", ""))).findFirst();
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 f536dfb..7f7cc14 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
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.governance.core.config.listener;
import org.apache.shardingsphere.governance.core.event.model.GovernanceEvent;
-import org.apache.shardingsphere.governance.core.event.model.auth.AuthenticationChangedEvent;
+import org.apache.shardingsphere.governance.core.event.model.auth.UserRuleChangedEvent;
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;
@@ -56,7 +56,8 @@ 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(new Grantee("root1", ""));
+ Optional<ShardingSphereUser> user =
+ ((UserRuleChangedEvent) actual.get()).getUsers().stream().filter(each -> each.getGrantee().equals(new Grantee("root1", ""))).findFirst();
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 f96dafd..aeb061f 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
@@ -26,6 +26,7 @@ import org.apache.shardingsphere.governance.repository.api.config.GovernanceCent
import org.apache.shardingsphere.governance.repository.api.config.GovernanceConfiguration;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
+import org.apache.shardingsphere.infra.auth.privilege.ShardingSpherePrivilege;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.junit.Before;
@@ -76,11 +77,12 @@ public final class GovernanceFacadeTest {
Map<String, Collection<RuleConfiguration>> ruleConfigurationMap = Collections.singletonMap("sharding_db", Collections.singletonList(mock(RuleConfiguration.class)));
ShardingSphereUser user = new ShardingSphereUser("root", "root", "", Collections.singleton("db1"));
DefaultAuthentication authentication = new DefaultAuthentication();
- authentication.getUsers().add(user);
+ authentication.getAuthentication().put(user, new ShardingSpherePrivilege());
Properties props = new Properties();
- governanceFacade.onlineInstance(Collections.singletonMap("sharding_db", dataSourceConfigMap), ruleConfigurationMap, authentication, props);
+ governanceFacade.onlineInstance(
+ Collections.singletonMap("sharding_db", dataSourceConfigMap), ruleConfigurationMap, authentication.getAuthentication().keySet(), props);
verify(configCenter).persistConfigurations("sharding_db", dataSourceConfigMap, ruleConfigurationMap.get("sharding_db"), false);
- verify(configCenter).persistGlobalConfiguration(authentication, props, false);
+ verify(configCenter).persistGlobalConfiguration(authentication.getAuthentication().keySet(), props, false);
verify(registryCenter).persistInstanceOnline();
verify(registryCenter).persistDataNodes();
verify(listenerManager).init();
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 6219dd9..32d3b6b 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
@@ -17,6 +17,9 @@
package org.apache.shardingsphere.infra.auth;
+import org.apache.shardingsphere.infra.auth.privilege.ShardingSpherePrivilege;
+
+import java.util.Map;
import java.util.Optional;
/**
@@ -25,10 +28,25 @@ import java.util.Optional;
public interface Authentication {
/**
+ * Get authentication.
+ *
+ * @return Authentication
+ */
+ Map<ShardingSphereUser, ShardingSpherePrivilege> getAuthentication();
+
+ /**
* Find user.
*
* @param grantee grantee
* @return found user
*/
Optional<ShardingSphereUser> findUser(Grantee grantee);
+
+ /**
+ * Find Privilege.
+ *
+ * @param grantee grantee
+ * @return found user
+ */
+ Optional<ShardingSpherePrivilege> findPrivilege(Grantee grantee);
}
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 997ad4e..107d644 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
@@ -19,25 +19,51 @@ package org.apache.shardingsphere.infra.auth.builtin;
import com.google.common.base.Strings;
import lombok.Getter;
+import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.auth.Authentication;
import org.apache.shardingsphere.infra.auth.Grantee;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
+import org.apache.shardingsphere.infra.auth.privilege.PrivilegeType;
+import org.apache.shardingsphere.infra.auth.privilege.ShardingSpherePrivilege;
+import org.apache.shardingsphere.infra.auth.privilege.TablePrivilege;
import java.util.Collection;
-import java.util.LinkedHashSet;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
import java.util.Optional;
/**
* Default authentication.
*/
+@NoArgsConstructor
@Getter
public final class DefaultAuthentication implements Authentication {
- private final Collection<ShardingSphereUser> users = new LinkedHashSet<>();
+ private final Map<ShardingSphereUser, ShardingSpherePrivilege> auth = new LinkedHashMap<>();
+
+ public DefaultAuthentication(final Collection<ShardingSphereUser> users) {
+ for (ShardingSphereUser each : users) {
+ auth.put(each, createShardingSpherePrivilege());
+ }
+ }
+
+ private ShardingSpherePrivilege createShardingSpherePrivilege() {
+ ShardingSpherePrivilege result = new ShardingSpherePrivilege();
+ result.getInstancePrivilege().getPrivileges().add(PrivilegeType.ALL);
+ result.getSchemaPrivilege().getTablePrivileges().put(PrivilegeType.ALL.getName(),
+ new TablePrivilege(PrivilegeType.ALL.getName(), Collections.singleton(PrivilegeType.ALL)));
+ return result;
+ }
@Override
public Optional<ShardingSphereUser> findUser(final Grantee grantee) {
- return users.stream().filter(entry -> entry.getUsername().equals(grantee.getUsername())
- && (entry.getHostname().equals(grantee.getHostname()) || Strings.isNullOrEmpty(entry.getHostname()))).findFirst();
+ return auth.keySet().stream().filter(each -> each.getUsername().equals(grantee.getUsername())
+ && (each.getHostname().equals(grantee.getHostname()) || Strings.isNullOrEmpty(each.getHostname()))).findFirst();
+ }
+
+ @Override
+ public Optional<ShardingSpherePrivilege> findPrivilege(final Grantee grantee) {
+ return findUser(grantee).map(auth::get);
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlAuthenticationConfiguration.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlUserRuleConfiguration.java
similarity index 89%
copy from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlAuthenticationConfiguration.java
copy to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlUserRuleConfiguration.java
index ddd1c78..804a2f3 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlAuthenticationConfiguration.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlUserRuleConfiguration.java
@@ -24,11 +24,11 @@ import org.apache.shardingsphere.infra.yaml.config.YamlConfiguration;
import java.util.Map;
/**
- * Authentication configuration for YAML.
+ * User rule configuration for YAML.
*/
@Getter
@Setter
-public final class YamlAuthenticationConfiguration implements YamlConfiguration {
+public final class YamlUserRuleConfiguration implements YamlConfiguration {
private Map<String, YamlUserConfiguration> users;
}
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/UserRuleYamlSwapper.java
similarity index 79%
rename from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/AuthenticationYamlSwapper.java
rename to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserRuleYamlSwapper.java
index 44d1626..807bfe2 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/UserRuleYamlSwapper.java
@@ -21,26 +21,27 @@ import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
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.auth.builtin.yaml.config.YamlUserRuleConfiguration;
import org.apache.shardingsphere.infra.yaml.swapper.YamlSwapper;
+import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
/**
- * Authentication YAML swapper.
+ * User rule YAML swapper.
*/
-public final class AuthenticationYamlSwapper implements YamlSwapper<YamlAuthenticationConfiguration, DefaultAuthentication> {
+public final class UserRuleYamlSwapper implements YamlSwapper<YamlUserRuleConfiguration, Collection<ShardingSphereUser>> {
@Override
- public YamlAuthenticationConfiguration swapToYamlConfiguration(final DefaultAuthentication data) {
- YamlAuthenticationConfiguration result = new YamlAuthenticationConfiguration();
+ public YamlUserRuleConfiguration swapToYamlConfiguration(final Collection<ShardingSphereUser> data) {
+ YamlUserRuleConfiguration result = new YamlUserRuleConfiguration();
Map<String, YamlUserConfiguration> users = new LinkedHashMap<>();
- for (ShardingSphereUser each : data.getUsers()) {
+ for (ShardingSphereUser each : data) {
users.put(each.getUsername(), swapToYamlConfiguration(each));
}
result.setUsers(users);
@@ -57,13 +58,13 @@ public final class AuthenticationYamlSwapper implements YamlSwapper<YamlAuthenti
}
@Override
- public DefaultAuthentication swapToObject(final YamlAuthenticationConfiguration yamlConfig) {
- DefaultAuthentication result = new DefaultAuthentication();
+ public Collection<ShardingSphereUser> swapToObject(final YamlUserRuleConfiguration yamlConfig) {
+ Collection<ShardingSphereUser> result = new LinkedHashSet<>();
if (null == yamlConfig) {
return result;
}
for (Entry<String, YamlUserConfiguration> entry : yamlConfig.getUsers().entrySet()) {
- result.getUsers().add(swapToObject(entry.getKey(), entry.getValue()));
+ result.add(swapToObject(entry.getKey(), entry.getValue()));
}
return result;
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlAuthenticationConfiguration.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/privilege/InstancePrivilege.java
similarity index 57%
copy from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlAuthenticationConfiguration.java
copy to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/privilege/InstancePrivilege.java
index ddd1c78..90d3b7b 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlAuthenticationConfiguration.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/privilege/InstancePrivilege.java
@@ -15,20 +15,31 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.auth.builtin.yaml.config;
+package org.apache.shardingsphere.infra.auth.privilege;
import lombok.Getter;
-import lombok.Setter;
-import org.apache.shardingsphere.infra.yaml.config.YamlConfiguration;
-import java.util.Map;
+import java.util.Collection;
+import java.util.LinkedHashSet;
/**
- * Authentication configuration for YAML.
+ * Instance privilege.
*/
@Getter
-@Setter
-public final class YamlAuthenticationConfiguration implements YamlConfiguration {
+public final class InstancePrivilege {
- private Map<String, YamlUserConfiguration> users;
+ private final Collection<PrivilegeType> privileges = new LinkedHashSet<>();
+
+ /**
+ * 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/yaml/config/YamlAuthenticationConfiguration.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/privilege/ShardingSpherePrivilege.java
similarity index 56%
rename from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlAuthenticationConfiguration.java
rename to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/privilege/ShardingSpherePrivilege.java
index ddd1c78..dc25424 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlAuthenticationConfiguration.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/privilege/ShardingSpherePrivilege.java
@@ -15,20 +15,29 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.auth.builtin.yaml.config;
+package org.apache.shardingsphere.infra.auth.privilege;
import lombok.Getter;
-import lombok.Setter;
-import org.apache.shardingsphere.infra.yaml.config.YamlConfiguration;
-import java.util.Map;
+import java.util.Collections;
/**
- * Authentication configuration for YAML.
+ * ShardingSphere privilege.
*/
@Getter
-@Setter
-public final class YamlAuthenticationConfiguration implements YamlConfiguration {
+public final class ShardingSpherePrivilege {
- private Map<String, YamlUserConfiguration> users;
+ private final InstancePrivilege instancePrivilege = new InstancePrivilege();
+
+ private final SchemaPrivilege schemaPrivilege = new SchemaPrivilege();
+
+ /**
+ * Set super privilege.
+ *
+ */
+ public void setSuper() {
+ instancePrivilege.getPrivileges().add(PrivilegeType.ALL);
+ schemaPrivilege.getTablePrivileges().put(PrivilegeType.ALL.getName(),
+ new TablePrivilege(PrivilegeType.ALL.getName(), Collections.singletonList(PrivilegeType.ALL)));
+ }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/privilege/builder/PrivilegeBuilder.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/privilege/builder/PrivilegeBuilder.java
new file mode 100644
index 0000000..d1f7661
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/privilege/builder/PrivilegeBuilder.java
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.metadata.privilege.builder;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
+import org.apache.shardingsphere.infra.auth.privilege.ShardingSpherePrivilege;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.metadata.privilege.loader.PrivilegeLoader;
+import org.apache.shardingsphere.infra.metadata.privilege.loader.PrivilegeLoaderEngine;
+
+import javax.sql.DataSource;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * Privilege builder.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class PrivilegeBuilder {
+
+ private static final int CPU_CORES = Runtime.getRuntime().availableProcessors();
+
+ private static final int FUTURE_GET_TIME_OUT_SECOND = 5;
+
+ /**
+ * Build privileges.
+ *
+ * @param metaDatas metadatas
+ * @param users users
+ * @param props props
+ * @return privileges
+ */
+ public static Map<ShardingSphereUser, ShardingSpherePrivilege> build(final Collection<ShardingSphereMetaData> metaDatas,
+ final Collection<ShardingSphereUser> users, final ConfigurationProperties props) {
+ Optional<PrivilegeLoader> loader = PrivilegeLoaderEngine.getPrivilegeLoader();
+ if (!loader.isPresent()) {
+ return getDefaultShardingSpherePrivileges(users);
+ }
+ Map<ShardingSphereUser, ShardingSpherePrivilege> result = new LinkedHashMap<>();
+ for (ShardingSphereMetaData each : metaDatas) {
+ result.putAll(build0(each, users, loader.get(), props));
+ }
+ return result;
+ }
+
+ private static Map<ShardingSphereUser, ShardingSpherePrivilege> getDefaultShardingSpherePrivileges(final Collection<ShardingSphereUser> users) {
+ Map<ShardingSphereUser, ShardingSpherePrivilege> result = new LinkedHashMap<>();
+ ShardingSpherePrivilege privilege = new ShardingSpherePrivilege();
+ privilege.setSuper();
+ users.forEach(each -> result.put(each, privilege));
+ return result;
+ }
+
+ private static Map<ShardingSphereUser, ShardingSpherePrivilege> build0(final ShardingSphereMetaData metaData, final Collection<ShardingSphereUser> users,
+ final PrivilegeLoader loader, final ConfigurationProperties props) {
+ Map<ShardingSphereUser, Collection<ShardingSpherePrivilege>> result =
+ build0(metaData.getResource().getDataSources(), users, loader, props.getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY));
+ return PrivilegeMerger.merge(result, metaData.getName(), metaData.getRuleMetaData().getRules());
+ }
+
+ private static Map<ShardingSphereUser, Collection<ShardingSpherePrivilege>> build0(final Map<String, DataSource> dataSources, final Collection<ShardingSphereUser> users,
+ final PrivilegeLoader loader, final int maxConnectionsSizePerQuery) {
+ Map<ShardingSphereUser, Collection<ShardingSpherePrivilege>> result = new LinkedHashMap<>();
+ for (ShardingSphereUser each : users) {
+ Collection<ShardingSpherePrivilege> privileges = parallelLoadPrivileges(dataSources, each, loader, maxConnectionsSizePerQuery);
+ result.put(each, privileges);
+ }
+ return result;
+ }
+
+ private static Collection<ShardingSpherePrivilege> parallelLoadPrivileges(final Map<String, DataSource> dataSources,
+ final ShardingSphereUser user, final PrivilegeLoader loader, final int maxConnectionsSizePerQuery) {
+ Collection<ShardingSpherePrivilege> result = new LinkedHashSet<>(dataSources.size(), 1);
+ Collection<Future<Optional<ShardingSpherePrivilege>>> futures = new LinkedHashSet<>(dataSources.size(), 1);
+ ExecutorService executorService = Executors.newFixedThreadPool(Math.min(CPU_CORES * 2, dataSources.size() * maxConnectionsSizePerQuery));
+ for (DataSource each : dataSources.values()) {
+ futures.add(executorService.submit(() -> loader.load(user, each)));
+ }
+ futures.forEach(each -> {
+ try {
+ each.get(FUTURE_GET_TIME_OUT_SECOND, TimeUnit.SECONDS).ifPresent(result::add);
+ } catch (final InterruptedException | ExecutionException | TimeoutException ex) {
+ throw new IllegalStateException(String.format("Error while fetching privilege with %s", each), ex);
+ }
+ });
+ executorService.shutdownNow();
+ return result;
+ }
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/privilege/builder/PrivilegeMerger.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/privilege/builder/PrivilegeMerger.java
new file mode 100644
index 0000000..94c97d9
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/privilege/builder/PrivilegeMerger.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.metadata.privilege.builder;
+
+import com.google.common.collect.Maps;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
+import org.apache.shardingsphere.infra.auth.privilege.ShardingSpherePrivilege;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * Privilege merger.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class PrivilegeMerger {
+
+ /**
+ * Merge.
+ *
+ * @param authentication authentication
+ * @param schemaName schema name
+ * @param rules ShardingSphere rules
+ * @return privileges
+ */
+ public static Map<ShardingSphereUser, ShardingSpherePrivilege> merge(final Map<ShardingSphereUser, Collection<ShardingSpherePrivilege>> authentication,
+ final String schemaName, final Collection<ShardingSphereRule> rules) {
+ // TODO :merge by rules
+ return Maps.transformEntries(authentication, (key, value) -> merge(value));
+ }
+
+ private static ShardingSpherePrivilege merge(final Collection<ShardingSpherePrivilege> privileges) {
+ return privileges.isEmpty() ? new ShardingSpherePrivilege() : privileges.iterator().next();
+ }
+}
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/metadata/privilege/loader/PrivilegeLoader.java
similarity index 62%
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/metadata/privilege/loader/PrivilegeLoader.java
index 6219dd9..9d85355 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/metadata/privilege/loader/PrivilegeLoader.java
@@ -15,20 +15,25 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.auth;
+package org.apache.shardingsphere.infra.metadata.privilege.loader;
+import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
+import org.apache.shardingsphere.infra.auth.privilege.ShardingSpherePrivilege;
+
+import javax.sql.DataSource;
import java.util.Optional;
/**
- * Authentication.
-*/
-public interface Authentication {
+ * Privilege loader.
+ */
+public interface PrivilegeLoader {
/**
- * Find user.
- *
- * @param grantee grantee
- * @return found user
+ * Load.
+ *
+ * @param user user
+ * @param dataSource data source
+ * @return sharding sphere privilege
*/
- Optional<ShardingSphereUser> findUser(Grantee grantee);
+ Optional<ShardingSpherePrivilege> load(ShardingSphereUser user, DataSource dataSource);
}
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/metadata/privilege/loader/PrivilegeLoaderEngine.java
similarity index 51%
copy from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/auth/builtin/DefaultAuthentication.java
copy to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/privilege/loader/PrivilegeLoaderEngine.java
index 997ad4e..95e0d3e 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/metadata/privilege/loader/PrivilegeLoaderEngine.java
@@ -15,29 +15,35 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.auth.builtin;
+package org.apache.shardingsphere.infra.metadata.privilege.loader;
-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 lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import java.util.Collection;
-import java.util.LinkedHashSet;
import java.util.Optional;
/**
- * Default authentication.
-*/
-@Getter
-public final class DefaultAuthentication implements Authentication {
+ * Privilege loader engine.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class PrivilegeLoaderEngine {
- private final Collection<ShardingSphereUser> users = new LinkedHashSet<>();
+ static {
+ ShardingSphereServiceLoader.register(PrivilegeLoader.class);
+ }
- @Override
- public Optional<ShardingSphereUser> findUser(final Grantee grantee) {
- return users.stream().filter(entry -> entry.getUsername().equals(grantee.getUsername())
- && (entry.getHostname().equals(grantee.getHostname()) || Strings.isNullOrEmpty(entry.getHostname()))).findFirst();
+ /**
+ * Get privilege loader.
+ *
+ * @return privilege loader
+ */
+ public static Optional<PrivilegeLoader> getPrivilegeLoader() {
+ Collection<PrivilegeLoader> loaders = ShardingSphereServiceLoader.newServiceInstances(PrivilegeLoader.class);
+ if (loaders.isEmpty()) {
+ return Optional.empty();
+ }
+ return Optional.of(loaders.iterator().next());
}
}
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/UserRuleYamlSwapperTest.java
similarity index 69%
rename from shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/AuthenticationYamlSwapperTest.java
rename to shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/swapper/UserRuleYamlSwapperTest.java
index 0d780e1..4968c70 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/UserRuleYamlSwapperTest.java
@@ -20,12 +20,15 @@ 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;
+import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlUserRuleConfiguration;
import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlUserConfiguration;
+import org.apache.shardingsphere.infra.auth.privilege.ShardingSpherePrivilege;
import org.junit.Test;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
+import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Optional;
@@ -33,14 +36,14 @@ import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
-public final class AuthenticationYamlSwapperTest {
+public final class UserRuleYamlSwapperTest {
@Test
public void assertSwapToYaml() {
- DefaultAuthentication authentication = new DefaultAuthentication();
- authentication.getUsers().add(new ShardingSphereUser("user1", "pwd1", "127.0.0.1", Collections.singleton("db1")));
- authentication.getUsers().add(new ShardingSphereUser("user2", "pwd2", "127.0.0.2", Collections.singleton("db2")));
- YamlAuthenticationConfiguration actual = new AuthenticationYamlSwapper().swapToYamlConfiguration(authentication);
+ DefaultAuthentication authentication = new DefaultAuthentication(new LinkedHashSet<>());
+ authentication.getAuthentication().put(new ShardingSphereUser("user1", "pwd1", "127.0.0.1", Collections.singleton("db1")), new ShardingSpherePrivilege());
+ authentication.getAuthentication().put(new ShardingSphereUser("user2", "pwd2", "127.0.0.2", Collections.singleton("db2")), new ShardingSpherePrivilege());
+ YamlUserRuleConfiguration actual = new UserRuleYamlSwapper().swapToYamlConfiguration(authentication.getAuthentication().keySet());
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"));
@@ -61,20 +64,20 @@ public final class AuthenticationYamlSwapperTest {
Map<String, YamlUserConfiguration> users = new HashMap<>(2, 1);
users.put("user1", user1);
users.put("user2", user2);
- YamlAuthenticationConfiguration yamlConfig = new YamlAuthenticationConfiguration();
+ YamlUserRuleConfiguration yamlConfig = new YamlUserRuleConfiguration();
yamlConfig.setUsers(users);
- DefaultAuthentication actual = new AuthenticationYamlSwapper().swapToObject(yamlConfig);
- Optional<ShardingSphereUser> actualUser1 = actual.findUser(new Grantee("user1", ""));
+ Collection<ShardingSphereUser> actual = new UserRuleYamlSwapper().swapToObject(yamlConfig);
+ Optional<ShardingSphereUser> actualUser1 = actual.stream().filter(each -> each.getGrantee().equals(new Grantee("user1", ""))).findFirst();
assertTrue(actualUser1.isPresent());
assertThat(actualUser1.get().getAuthorizedSchemas().size(), is(1));
- Optional<ShardingSphereUser> actualUser2 = actual.findUser(new Grantee("user2", ""));
+ Optional<ShardingSphereUser> actualUser2 = actual.stream().filter(each -> each.getGrantee().equals(new Grantee("user2", ""))).findFirst();
assertTrue(actualUser2.isPresent());
assertThat(actualUser2.get().getAuthorizedSchemas().size(), is(2));
}
@Test
public void assertSwapToObjectForNull() {
- DefaultAuthentication actual = new AuthenticationYamlSwapper().swapToObject(null);
- assertTrue(actual.getUsers().isEmpty());
+ Collection<ShardingSphereUser> actual = new UserRuleYamlSwapper().swapToObject(null);
+ assertTrue(actual.isEmpty());
}
}
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 57d9c66..be6a191 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
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.infra.context.metadata;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.infra.auth.Authentication;
+import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
import org.apache.shardingsphere.infra.config.DatabaseAccessConfiguration;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
@@ -30,6 +31,7 @@ import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.metadata.privilege.builder.PrivilegeBuilder;
import org.apache.shardingsphere.infra.metadata.resource.CachedDatabaseMetaData;
import org.apache.shardingsphere.infra.metadata.resource.DataSourcesMetaData;
import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
@@ -45,6 +47,7 @@ import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -62,21 +65,21 @@ public final class MetaDataContextsBuilder {
private final Map<String, Collection<RuleConfiguration>> ruleConfigs;
- private final Authentication authentication;
+ private final Collection<ShardingSphereUser> users;
private final ConfigurationProperties props;
private final ExecutorEngine executorEngine;
public MetaDataContextsBuilder(final Map<String, Map<String, DataSource>> dataSources, final Map<String, Collection<RuleConfiguration>> ruleConfigs, final Properties props) {
- this(dataSources, ruleConfigs, new DefaultAuthentication(), props);
+ this(dataSources, ruleConfigs, Collections.emptyList(), props);
}
public MetaDataContextsBuilder(final Map<String, Map<String, DataSource>> dataSources,
- final Map<String, Collection<RuleConfiguration>> ruleConfigs, final Authentication authentication, final Properties props) {
+ final Map<String, Collection<RuleConfiguration>> ruleConfigs, final Collection<ShardingSphereUser> users, final Properties props) {
this.dataSources = dataSources;
this.ruleConfigs = ruleConfigs;
- this.authentication = authentication;
+ this.users = users;
this.props = new ConfigurationProperties(null == props ? new Properties() : props);
executorEngine = new ExecutorEngine(this.props.<Integer>getValue(ConfigurationPropertyKey.EXECUTOR_SIZE));
}
@@ -89,6 +92,7 @@ public final class MetaDataContextsBuilder {
*/
public StandardMetaDataContexts build() throws SQLException {
Map<String, ShardingSphereMetaData> mataDataMap = new HashMap<>(ruleConfigs.size(), 1);
+ Authentication authentication = buildAuthentication(users, mataDataMap);
for (String each : ruleConfigs.keySet()) {
mataDataMap.put(each, buildMetaData(each));
}
@@ -104,7 +108,7 @@ public final class MetaDataContextsBuilder {
return new ShardingSphereMetaData(schemaName, buildResource(databaseType, dataSourceMap), ruleMetaData, buildSchema(schemaName, databaseType, dataSourceMap, rules));
}
- private DatabaseType getDatabaseType(final Map<String, DataSource> dataSourceMap) throws SQLException {
+ private DatabaseType getDatabaseType(final Map<String, DataSource> dataSourceMap) {
DatabaseType result = null;
for (DataSource each : dataSourceMap.values()) {
DatabaseType databaseType = getDatabaseType(each);
@@ -156,4 +160,10 @@ public final class MetaDataContextsBuilder {
log.info("Load meta data for schema {} finished, cost {} milliseconds.", schemaName, System.currentTimeMillis() - start);
return result;
}
+
+ private Authentication buildAuthentication(final Collection<ShardingSphereUser> users, final Map<String, ShardingSphereMetaData> metaDataMap) {
+ DefaultAuthentication result = new DefaultAuthentication();
+ result.getAuthentication().putAll(PrivilegeBuilder.build(metaDataMap.values(), users, props));
+ return result;
+ }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilderTest.java b/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilderTest.java
index 09c1d4e..6914d26 100644
--- a/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilderTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilderTest.java
@@ -42,7 +42,7 @@ public final class MetaDataContextsBuilderTest {
public void assertBuildWithoutConfiguration() throws SQLException {
MetaDataContexts actual = new MetaDataContextsBuilder(Collections.emptyMap(), Collections.emptyMap(), null).build();
assertTrue(actual.getAllSchemaNames().isEmpty());
- assertTrue(((DefaultAuthentication) actual.getAuthentication()).getUsers().isEmpty());
+ assertTrue(((DefaultAuthentication) actual.getAuthentication()).getAuthentication().isEmpty());
assertTrue(actual.getProps().getProps().isEmpty());
}
@@ -54,7 +54,7 @@ public final class MetaDataContextsBuilderTest {
Collections.singletonMap("logic_db", Collections.emptyMap()), Collections.singletonMap("logic_db", Collections.singleton(new FixtureRuleConfiguration())), props).build();
assertRules(actual);
assertTrue(actual.getMetaData("logic_db").getResource().getDataSources().isEmpty());
- assertTrue(((DefaultAuthentication) actual.getAuthentication()).getUsers().isEmpty());
+ assertTrue(((DefaultAuthentication) actual.getAuthentication()).getAuthentication().isEmpty());
assertThat(actual.getProps().getProps().size(), is(1));
assertThat(actual.getProps().getValue(ConfigurationPropertyKey.EXECUTOR_SIZE), is(1));
}
@@ -67,7 +67,7 @@ public final class MetaDataContextsBuilderTest {
Collections.singletonMap("logic_db", Collections.singleton(new FixtureRuleConfiguration())), props).build();
assertRules(actual);
assertDataSources(actual);
- assertTrue(((DefaultAuthentication) actual.getAuthentication()).getUsers().isEmpty());
+ assertTrue(((DefaultAuthentication) actual.getAuthentication()).getAuthentication().isEmpty());
assertThat(actual.getProps().getProps().size(), is(1));
assertThat(actual.getProps().getValue(ConfigurationPropertyKey.EXECUTOR_SIZE), is(1));
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-governance/src/main/java/org/apache/shardingsphere/driver/governance/internal/datasource/GovernanceShardingSphereDataSource.java b/shardingsphere-jdbc/shardingsphere-jdbc-governance/src/main/java/org/apache/shardingsphere/driver/governance/internal/datasource/GovernanceShardingSphereDataSource.java
index 16e4ff3..8886196 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-governance/src/main/java/org/apache/shardingsphere/driver/governance/internal/datasource/GovernanceShardingSphereDataSource.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-governance/src/main/java/org/apache/shardingsphere/driver/governance/internal/datasource/GovernanceShardingSphereDataSource.java
@@ -24,7 +24,6 @@ import org.apache.shardingsphere.governance.context.metadata.GovernanceMetaDataC
import org.apache.shardingsphere.governance.core.config.ConfigCenter;
import org.apache.shardingsphere.governance.core.facade.GovernanceFacade;
import org.apache.shardingsphere.governance.repository.api.config.GovernanceConfiguration;
-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.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
@@ -45,6 +44,7 @@ import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
+import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
@@ -90,13 +90,13 @@ public final class GovernanceShardingSphereDataSource extends AbstractUnsupporte
Map<String, DataSource> dataSourceMap = DataSourceConverter.getDataSourceMap(dataSourceConfigs);
MetaDataContextsBuilder metaDataContextsBuilder = new MetaDataContextsBuilder(
Collections.singletonMap(DefaultSchema.LOGIC_NAME, dataSourceMap),
- Collections.singletonMap(DefaultSchema.LOGIC_NAME, ruleConfigurations), new DefaultAuthentication(), configCenter.loadProperties());
+ Collections.singletonMap(DefaultSchema.LOGIC_NAME, ruleConfigurations), new LinkedList<>(), configCenter.loadProperties());
return metaDataContextsBuilder.build();
}
private StandardMetaDataContexts createMetaDataContexts(final Map<String, DataSource> dataSourceMap, final Collection<RuleConfiguration> ruleConfigs, final Properties props) throws SQLException {
MetaDataContextsBuilder metaDataContextsBuilder = new MetaDataContextsBuilder(
- Collections.singletonMap(DefaultSchema.LOGIC_NAME, dataSourceMap), Collections.singletonMap(DefaultSchema.LOGIC_NAME, ruleConfigs), new DefaultAuthentication(), props);
+ Collections.singletonMap(DefaultSchema.LOGIC_NAME, dataSourceMap), Collections.singletonMap(DefaultSchema.LOGIC_NAME, ruleConfigs), new LinkedList<>(), props);
return metaDataContextsBuilder.build();
}
@@ -110,7 +110,7 @@ public final class GovernanceShardingSphereDataSource extends AbstractUnsupporte
Map<String, DataSourceConfiguration> dataSourceConfigs = DataSourceConverter.getDataSourceConfigurationMap(metaDataContexts.getDefaultMetaData().getResource().getDataSources());
Collection<RuleConfiguration> ruleConfigurations = metaDataContexts.getDefaultMetaData().getRuleMetaData().getConfigurations();
governanceFacade.onlineInstance(Collections.singletonMap(DefaultSchema.LOGIC_NAME, dataSourceConfigs),
- Collections.singletonMap(DefaultSchema.LOGIC_NAME, ruleConfigurations), null, metaDataContexts.getProps().getProps());
+ Collections.singletonMap(DefaultSchema.LOGIC_NAME, ruleConfigurations), Collections.emptyList(), metaDataContexts.getProps().getProps());
}
@Override
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 561d515..15e1e4a 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
@@ -20,6 +20,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.auth.privilege.ShardingSpherePrivilege;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
@@ -76,7 +77,8 @@ public final class ShowDatabasesExecutorTest {
private DefaultAuthentication getAuthentication() {
DefaultAuthentication result = new DefaultAuthentication();
- result.getUsers().add(new ShardingSphereUser("root", "root", "", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1))));
+ result.getAuthentication().put(new ShardingSphereUser("root", "root", "", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1))),
+ new ShardingSpherePrivilege());
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 27c271a..585db77 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
@@ -20,6 +20,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.auth.privilege.ShardingSpherePrivilege;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
@@ -71,7 +72,8 @@ public final class ShowTablesExecutorTest {
private DefaultAuthentication getAuthentication() {
DefaultAuthentication result = new DefaultAuthentication();
- result.getUsers().add(new ShardingSphereUser("root", "root", "", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1))));
+ result.getAuthentication().put(new ShardingSphereUser("root", "root", "", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1))),
+ new ShardingSpherePrivilege());
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 1b30e0b..ab0c548 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
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.proxy.backend.text.admin.mysql.executor;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
+import org.apache.shardingsphere.infra.auth.privilege.ShardingSpherePrivilege;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
import org.apache.shardingsphere.infra.database.type.dialect.H2DatabaseType;
@@ -75,7 +76,8 @@ public final class UseDatabaseExecutorTest {
private DefaultAuthentication getAuthentication() {
DefaultAuthentication result = new DefaultAuthentication();
- result.getUsers().add(new ShardingSphereUser("root", "root", "", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1))));
+ result.getAuthentication().put(new ShardingSphereUser("root", "root", "", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1))),
+ new ShardingSpherePrivilege());
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 eef9ad1..59b0062 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
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.proxy.backend.text.data.impl;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
+import org.apache.shardingsphere.infra.auth.privilege.ShardingSpherePrivilege;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
@@ -108,7 +109,8 @@ public final class SchemaAssignedDatabaseBackendHandlerTest {
private DefaultAuthentication getAuthentication() {
DefaultAuthentication result = new DefaultAuthentication();
- result.getUsers().add(new ShardingSphereUser("root", "root", "", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1))));
+ result.getAuthentication().put(new ShardingSphereUser("root", "root", "", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1))),
+ new ShardingSpherePrivilege());
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 ba02019..254f4f0 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
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.proxy.backend.text.data.impl;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
+import org.apache.shardingsphere.infra.auth.privilege.ShardingSpherePrivilege;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
import org.apache.shardingsphere.infra.database.type.dialect.H2DatabaseType;
@@ -98,7 +99,8 @@ public final class UnicastDatabaseBackendHandlerTest {
private DefaultAuthentication getAuthentication() {
DefaultAuthentication result = new DefaultAuthentication();
- result.getUsers().add(new ShardingSphereUser("root", "root", "", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1))));
+ result.getAuthentication().put(new ShardingSphereUser("root", "root", "", Arrays.asList(String.format(SCHEMA_PATTERN, 0), String.format(SCHEMA_PATTERN, 1))),
+ new ShardingSpherePrivilege());
return result;
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java
index 8415b43..9c2e5b4 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/AbstractBootstrapInitializer.java
@@ -67,7 +67,7 @@ public abstract class AbstractBootstrapInitializer implements BootstrapInitializ
private MetaDataContexts createMetaDataContexts(final ProxyConfiguration proxyConfig) throws SQLException {
Map<String, Map<String, DataSource>> dataSourcesMap = createDataSourcesMap(proxyConfig.getSchemaDataSources());
- MetaDataContextsBuilder metaDataContextsBuilder = new MetaDataContextsBuilder(dataSourcesMap, proxyConfig.getSchemaRules(), proxyConfig.getAuthentication(), proxyConfig.getProps());
+ MetaDataContextsBuilder metaDataContextsBuilder = new MetaDataContextsBuilder(dataSourcesMap, proxyConfig.getSchemaRules(), proxyConfig.getUsers(), proxyConfig.getProps());
return metaDataContextsBuilder.build();
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializer.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializer.java
index feb93c4..4d1751f 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializer.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/impl/GovernanceBootstrapInitializer.java
@@ -21,9 +21,7 @@ import org.apache.shardingsphere.governance.context.metadata.GovernanceMetaDataC
import org.apache.shardingsphere.governance.context.transaction.GovernanceTransactionContexts;
import org.apache.shardingsphere.governance.core.facade.GovernanceFacade;
import org.apache.shardingsphere.governance.core.yaml.swapper.GovernanceConfigurationYamlSwapper;
-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.swapper.AuthenticationYamlSwapper;
+import org.apache.shardingsphere.infra.auth.builtin.yaml.swapper.UserRuleYamlSwapper;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
@@ -69,7 +67,7 @@ public final class GovernanceBootstrapInitializer extends AbstractBootstrapIniti
governanceFacade.onlineInstance();
} else {
governanceFacade.onlineInstance(
- getDataSourceConfigurationMap(ruleConfigs), getRuleConfigurations(ruleConfigs), getAuthentication(serverConfig.getAuthentication()), serverConfig.getProps());
+ getDataSourceConfigurationMap(ruleConfigs), getRuleConfigurations(ruleConfigs), new UserRuleYamlSwapper().swapToObject(serverConfig.getAuthentication()), serverConfig.getProps());
}
}
@@ -92,17 +90,12 @@ public final class GovernanceBootstrapInitializer extends AbstractBootstrapIniti
entry -> swapperEngine.swapToRuleConfigurations(entry.getValue().getRules()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
}
- private DefaultAuthentication getAuthentication(final YamlAuthenticationConfiguration authConfig) {
- return new AuthenticationYamlSwapper().swapToObject(authConfig);
- }
-
private ProxyConfiguration loadProxyConfiguration() {
Collection<String> schemaNames = governanceFacade.getConfigCenter().getAllSchemaNames();
Map<String, Map<String, DataSourceParameter>> schemaDataSources = loadDataSourceParametersMap(schemaNames);
Map<String, Collection<RuleConfiguration>> schemaRules = loadSchemaRules(schemaNames);
- DefaultAuthentication authentication = governanceFacade.getConfigCenter().loadAuthentication();
Properties props = governanceFacade.getConfigCenter().loadProperties();
- return new ProxyConfiguration(schemaDataSources, schemaRules, authentication, props);
+ return new ProxyConfiguration(schemaDataSources, schemaRules, governanceFacade.getConfigCenter().loadUserRule(), props);
}
private Map<String, Map<String, DataSourceParameter>> loadDataSourceParametersMap(final Collection<String> schemaNames) {
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 bd3f2b0..071a563 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
@@ -116,7 +116,7 @@ public final class GovernanceBootstrapInitializerTest extends AbstractBootstrapI
assertNotNull(actual);
assertSchemaDataSources(actual.getSchemaDataSources());
assertSchemaRules(actual.getSchemaRules());
- assertAuthentication(actual.getAuthentication());
+ assertAuthentication(new DefaultAuthentication(actual.getUsers()));
assertProps(actual.getProps());
}
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 f6234ff..2fbd45c 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
@@ -20,7 +20,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;
+import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlUserRuleConfiguration;
import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlUserConfiguration;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
@@ -110,7 +110,7 @@ public final class StandardBootstrapInitializerTest extends AbstractBootstrapIni
private void assertProxyConfiguration(final ProxyConfiguration actual) {
assertSchemaDataSources(actual.getSchemaDataSources());
assertSchemaRules(actual.getSchemaRules());
- assertAuthentication(actual.getAuthentication());
+ assertAuthentication(new DefaultAuthentication(actual.getUsers()));
assertProps(actual.getProps());
}
@@ -171,10 +171,10 @@ public final class StandardBootstrapInitializerTest extends AbstractBootstrapIni
return result;
}
- private YamlAuthenticationConfiguration createYamlAuthenticationConfiguration() {
+ private YamlUserRuleConfiguration createYamlAuthenticationConfiguration() {
Map<String, YamlUserConfiguration> users = new HashMap<>(1, 1);
users.put("root", createYamlUserConfiguration());
- YamlAuthenticationConfiguration result = new YamlAuthenticationConfiguration();
+ YamlUserRuleConfiguration result = new YamlUserRuleConfiguration();
result.setUsers(users);
return result;
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/ProxyConfiguration.java b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/ProxyConfiguration.java
index 939c062..bfb2199 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/ProxyConfiguration.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/ProxyConfiguration.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.proxy.config;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
+import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
@@ -38,7 +38,7 @@ public final class ProxyConfiguration {
private final Map<String, Collection<RuleConfiguration>> schemaRules;
- private final DefaultAuthentication authentication;
+ private final Collection<ShardingSphereUser> users;
private final Properties props;
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/yaml/YamlProxyServerConfiguration.java b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/yaml/YamlProxyServerConfiguration.java
index d7c3f80..618ff6a 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/yaml/YamlProxyServerConfiguration.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/yaml/YamlProxyServerConfiguration.java
@@ -21,7 +21,7 @@ import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.governance.core.yaml.config.YamlGovernanceConfiguration;
import org.apache.shardingsphere.governance.core.yaml.config.YamlScalingConfiguration;
-import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlAuthenticationConfiguration;
+import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlUserRuleConfiguration;
import org.apache.shardingsphere.infra.yaml.config.YamlConfiguration;
import java.util.Properties;
@@ -33,7 +33,7 @@ import java.util.Properties;
@Setter
public final class YamlProxyServerConfiguration implements YamlConfiguration {
- private YamlAuthenticationConfiguration authentication;
+ private YamlUserRuleConfiguration authentication;
private YamlGovernanceConfiguration governance;
diff --git a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/yaml/swapper/YamlProxyConfigurationSwapper.java b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/yaml/swapper/YamlProxyConfigurationSwapper.java
index 747c291..68d5c76 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/yaml/swapper/YamlProxyConfigurationSwapper.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-common/src/main/java/org/apache/shardingsphere/proxy/config/yaml/swapper/YamlProxyConfigurationSwapper.java
@@ -17,11 +17,11 @@
package org.apache.shardingsphere.proxy.config.yaml.swapper;
-import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
-import org.apache.shardingsphere.infra.auth.builtin.yaml.swapper.AuthenticationYamlSwapper;
+import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
+import org.apache.shardingsphere.infra.auth.builtin.yaml.swapper.UserRuleYamlSwapper;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
-import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
+import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
import org.apache.shardingsphere.proxy.config.ProxyConfiguration;
import org.apache.shardingsphere.proxy.config.YamlProxyConfiguration;
import org.apache.shardingsphere.proxy.config.util.DataSourceParameterConverter;
@@ -48,9 +48,9 @@ public final class YamlProxyConfigurationSwapper {
public ProxyConfiguration swap(final YamlProxyConfiguration yamlConfig) {
Map<String, Map<String, DataSourceParameter>> schemaDataSources = getDataSourceParametersMap(yamlConfig.getRuleConfigurations());
Map<String, Collection<RuleConfiguration>> schemaRules = getRuleConfigurations(yamlConfig.getRuleConfigurations());
- DefaultAuthentication authentication = new AuthenticationYamlSwapper().swapToObject(yamlConfig.getServerConfiguration().getAuthentication());
+ Collection<ShardingSphereUser> users = new UserRuleYamlSwapper().swapToObject(yamlConfig.getServerConfiguration().getAuthentication());
Properties props = yamlConfig.getServerConfiguration().getProps();
- return new ProxyConfiguration(schemaDataSources, schemaRules, authentication, props);
+ return new ProxyConfiguration(schemaDataSources, schemaRules, users, props);
}
private Map<String, Collection<RuleConfiguration>> getRuleConfigurations(final Map<String, YamlProxyRuleConfiguration> yamlRuleConfigurations) {
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 0244ad0..1dbc500 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
@@ -22,7 +22,7 @@ import org.apache.shardingsphere.governance.core.yaml.config.YamlGovernanceConfi
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;
+import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlUserRuleConfiguration;
import org.apache.shardingsphere.infra.auth.builtin.yaml.config.YamlUserConfiguration;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
@@ -103,7 +103,7 @@ public final class YamlProxyConfigurationSwapperTest {
}
private void assertAuthentication(final ProxyConfiguration proxyConfig) {
- DefaultAuthentication authentication = proxyConfig.getAuthentication();
+ DefaultAuthentication authentication = new DefaultAuthentication(proxyConfig.getUsers());
assertNotNull(authentication);
Optional<ShardingSphereUser> user = authentication.findUser(new Grantee("user1", ""));
assertTrue(user.isPresent());
@@ -219,7 +219,7 @@ public final class YamlProxyConfigurationSwapperTest {
when(yamlUserConfig.getPassword()).thenReturn("pass");
when(yamlUserConfig.getAuthorizedSchemas()).thenReturn("db1");
yamlUserConfigurationMap.put("user1", yamlUserConfig);
- YamlAuthenticationConfiguration yamlAuthenticationConfig = mock(YamlAuthenticationConfiguration.class);
+ YamlUserRuleConfiguration yamlAuthenticationConfig = mock(YamlUserRuleConfiguration.class);
when(yamlAuthenticationConfig.getUsers()).thenReturn(yamlUserConfigurationMap);
when(yamlProxyServerConfig.getAuthentication()).thenReturn(yamlAuthenticationConfig);
}
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 82dac23..b5dc12f 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
@@ -27,6 +27,7 @@ import org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLHandsha
import org.apache.shardingsphere.db.protocol.mysql.payload.MySQLPacketPayload;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
+import org.apache.shardingsphere.infra.auth.privilege.ShardingSpherePrivilege;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
@@ -150,7 +151,7 @@ public final class MySQLFrontendEngineTest {
private void setAuthentication(final ShardingSphereUser user) {
DefaultAuthentication authentication = new DefaultAuthentication();
- authentication.getUsers().add(user);
+ authentication.getAuthentication().put(user, new ShardingSpherePrivilege());
initProxyContext(authentication);
}
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 c4dae36..2be8a3c 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
@@ -23,6 +23,7 @@ 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.ShardingSphereUser;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
+import org.apache.shardingsphere.infra.auth.privilege.ShardingSpherePrivilege;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
@@ -112,7 +113,7 @@ public final class MySQLAuthenticationHandlerTest {
private void setAuthentication(final ShardingSphereUser user) {
DefaultAuthentication authentication = new DefaultAuthentication();
- authentication.getUsers().add(user);
+ authentication.getAuthentication().put(user, new ShardingSpherePrivilege());
initProxyContext(authentication);
}
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 592d783..52f6765 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
@@ -28,6 +28,7 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.Postgre
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.apache.shardingsphere.infra.auth.ShardingSphereUser;
import org.apache.shardingsphere.infra.auth.builtin.DefaultAuthentication;
+import org.apache.shardingsphere.infra.auth.privilege.ShardingSpherePrivilege;
import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.frontend.auth.AuthenticationResult;
@@ -126,7 +127,8 @@ public final class PostgreSQLAuthenticationEngineTest {
ProxyContext proxyContext = ProxyContext.getInstance();
StandardMetaDataContexts standardMetaDataContexts = new StandardMetaDataContexts();
- ((DefaultAuthentication) standardMetaDataContexts.getAuthentication()).getUsers().add(new ShardingSphereUser(username, password, "", null));
+ ((DefaultAuthentication) standardMetaDataContexts.getAuthentication()).getAuthentication().put(
+ new ShardingSphereUser(username, password, "", null), new ShardingSpherePrivilege());
proxyContext.init(standardMetaDataContexts, mock(TransactionContexts.class));
actual = engine.auth(channelHandlerContext, payload);
assertThat(actual.isFinished(), is(password.equals(inputPassword)));