You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/04/05 04:40:06 UTC
[shardingsphere] branch master updated: Add
StoragePrivilegeLoadAlgorithm (#9935)
This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 2807917 Add StoragePrivilegeLoadAlgorithm (#9935)
2807917 is described below
commit 280791792c3454f4019bb3bbd09fdf3584a59d84
Author: Liang Zhang <te...@163.com>
AuthorDate: Mon Apr 5 12:39:28 2021 +0800
Add StoragePrivilegeLoadAlgorithm (#9935)
---
.../authority/spi/PrivilegeLoadAlgorithm.java | 5 +-
.../loader/StoragePrivilegeLoadAlgorithm.java | 56 ++++++++++++++++++++++
.../authority/rule/AuthorityRule.java | 7 +--
.../rule/builder/AuthorityRuleBuilder.java | 2 +-
.../metadata/auth/builder/PrivilegeBuilder.java | 12 +++--
5 files changed, 74 insertions(+), 8 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/PrivilegeLoadAlgorithm.java b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/PrivilegeLoadAlgorithm.java
index 5e2e296..003efbe 100644
--- a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/PrivilegeLoadAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/PrivilegeLoadAlgorithm.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.authority.spi;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithm;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.auth.model.privilege.ShardingSpherePrivilege;
import org.apache.shardingsphere.infra.metadata.auth.model.user.ShardingSphereUser;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
@@ -35,10 +36,12 @@ public interface PrivilegeLoadAlgorithm extends ShardingSphereAlgorithm {
* Load privileges.
*
* @param schemaName schema name
+ * @param databaseType database type
* @param dataSources data sources
* @param rules rules
* @param users users
* @return user and privileges map
*/
- Map<ShardingSphereUser, ShardingSpherePrivilege> load(String schemaName, Collection<DataSource> dataSources, Collection<ShardingSphereRule> rules, Collection<ShardingSphereUser> users);
+ Map<ShardingSphereUser, ShardingSpherePrivilege> load(String schemaName, DatabaseType databaseType,
+ Collection<DataSource> dataSources, Collection<ShardingSphereRule> rules, Collection<ShardingSphereUser> users);
}
diff --git a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/loader/StoragePrivilegeLoadAlgorithm.java b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/loader/StoragePrivilegeLoadAlgorithm.java
new file mode 100644
index 0000000..9559a81
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/loader/StoragePrivilegeLoadAlgorithm.java
@@ -0,0 +1,56 @@
+/*
+ * 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.authority.loader;
+
+import org.apache.shardingsphere.authority.spi.PrivilegeLoadAlgorithm;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.metadata.auth.builder.PrivilegeBuilder;
+import org.apache.shardingsphere.infra.metadata.auth.builder.loader.PrivilegeLoader;
+import org.apache.shardingsphere.infra.metadata.auth.builder.loader.PrivilegeLoaderEngine;
+import org.apache.shardingsphere.infra.metadata.auth.model.privilege.ShardingSpherePrivilege;
+import org.apache.shardingsphere.infra.metadata.auth.model.user.ShardingSphereUser;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+
+import javax.sql.DataSource;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * Storage privilege load algorithm.
+ */
+public final class StoragePrivilegeLoadAlgorithm implements PrivilegeLoadAlgorithm {
+
+ @Override
+ public Map<ShardingSphereUser, ShardingSpherePrivilege> load(final String schemaName, final DatabaseType databaseType, final Collection<DataSource> dataSources,
+ final Collection<ShardingSphereRule> rules, final Collection<ShardingSphereUser> users) {
+ Optional<PrivilegeLoader> loader = PrivilegeLoaderEngine.findPrivilegeLoader(databaseType);
+ return loader.map(
+ optional -> PrivilegeBuilder.build(schemaName, groupDataSourcesByInstance(dataSources), rules, users, optional)).orElseGet(() -> PrivilegeBuilder.buildDefaultPrivileges(users));
+ }
+
+ private Collection<DataSource> groupDataSourcesByInstance(final Collection<DataSource> dataSources) {
+ // TODO recognize and merge data sources from same instance
+ return dataSources;
+ }
+
+ @Override
+ public String getType() {
+ return "STORAGE";
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
index ab0a2bb..b047b74 100644
--- a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
+++ b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
@@ -21,6 +21,7 @@ import com.google.common.base.Preconditions;
import org.apache.shardingsphere.authority.api.config.AuthorityRuleConfiguration;
import org.apache.shardingsphere.authority.spi.PrivilegeLoadAlgorithm;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmFactory;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.auth.Authentication;
import org.apache.shardingsphere.infra.metadata.auth.AuthenticationContext;
import org.apache.shardingsphere.infra.metadata.auth.builtin.DefaultAuthentication;
@@ -40,12 +41,12 @@ public final class AuthorityRule implements ShardingSphereRule {
ShardingSphereServiceLoader.register(PrivilegeLoadAlgorithm.class);
}
- public AuthorityRule(final AuthorityRuleConfiguration config,
- final String schemaName, final Collection<DataSource> dataSources, final Collection<ShardingSphereUser> users, final Collection<ShardingSphereRule> builtRules) {
+ public AuthorityRule(final AuthorityRuleConfiguration config, final String schemaName, final DatabaseType databaseType,
+ final Collection<DataSource> dataSources, final Collection<ShardingSphereUser> users, final Collection<ShardingSphereRule> builtRules) {
Preconditions.checkState(1 == config.getPrivilegeLoaders().size(), "Only support one privilege loader.");
PrivilegeLoadAlgorithm privilegeLoader = ShardingSphereAlgorithmFactory.createAlgorithm(config.getPrivilegeLoaders().values().iterator().next(), PrivilegeLoadAlgorithm.class);
Authentication authentication = null == AuthenticationContext.getInstance().getAuthentication() ? new DefaultAuthentication() : AuthenticationContext.getInstance().getAuthentication();
- authentication.init(privilegeLoader.load(schemaName, dataSources, builtRules, users));
+ authentication.init(privilegeLoader.load(schemaName, databaseType, dataSources, builtRules, users));
AuthenticationContext.getInstance().init(authentication);
}
}
diff --git a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/rule/builder/AuthorityRuleBuilder.java b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/rule/builder/AuthorityRuleBuilder.java
index 573e6d1..bbfb9c3 100644
--- a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/rule/builder/AuthorityRuleBuilder.java
+++ b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/rule/builder/AuthorityRuleBuilder.java
@@ -37,7 +37,7 @@ public final class AuthorityRuleBuilder implements ShardingSphereRuleBuilder<Aut
@Override
public AuthorityRule build(final String schemaName, final Map<String, DataSource> dataSourceMap, final DatabaseType databaseType,
final AuthorityRuleConfiguration ruleConfig, final Collection<ShardingSphereUser> users, final Collection<ShardingSphereRule> builtRules) {
- return new AuthorityRule(ruleConfig, schemaName, dataSourceMap.values(), users, builtRules);
+ return new AuthorityRule(ruleConfig, schemaName, databaseType, dataSourceMap.values(), users, builtRules);
}
@Override
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builder/PrivilegeBuilder.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builder/PrivilegeBuilder.java
index efe9928..add2cc4 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builder/PrivilegeBuilder.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/auth/builder/PrivilegeBuilder.java
@@ -64,10 +64,10 @@ public final class PrivilegeBuilder {
public static Map<ShardingSphereUser, ShardingSpherePrivilege> build(final DatabaseType databaseType,
final Collection<ShardingSphereMetaData> metaDataList, final Collection<ShardingSphereUser> users) {
if (metaDataList.isEmpty()) {
- return createDefaultPrivileges(users);
+ return buildDefaultPrivileges(users);
}
Optional<PrivilegeLoader> loader = PrivilegeLoaderEngine.findPrivilegeLoader(databaseType);
- return loader.map(optional -> build(metaDataList, users, optional)).orElseGet(() -> createDefaultPrivileges(users));
+ return loader.map(optional -> build(metaDataList, users, optional)).orElseGet(() -> buildDefaultPrivileges(users));
}
private static Map<ShardingSphereUser, ShardingSpherePrivilege> build(final Collection<ShardingSphereMetaData> metaDataList,
@@ -119,7 +119,13 @@ public final class PrivilegeBuilder {
return result;
}
- private static Map<ShardingSphereUser, ShardingSpherePrivilege> createDefaultPrivileges(final Collection<ShardingSphereUser> users) {
+ /**
+ * Build default privileges.
+ *
+ * @param users users
+ * @return privileges
+ */
+ public static Map<ShardingSphereUser, ShardingSpherePrivilege> buildDefaultPrivileges(final Collection<ShardingSphereUser> users) {
Map<ShardingSphereUser, ShardingSpherePrivilege> result = new LinkedHashMap<>(users.size(), 1);
ShardingSpherePrivilege privilege = new ShardingSpherePrivilege();
privilege.setSuperPrivilege();