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 20b5174f288 Split AuthorityChecker and SQLChecker (#23806)
20b5174f288 is described below
commit 20b5174f288814ba8fd52f9fd4062d85d253f626
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Jan 29 19:05:36 2023 +0800
Split AuthorityChecker and SQLChecker (#23806)
---
.../infra/executor/check/SQLCheckEngine.java | 24 +++------
.../statement/ShardingSpherePreparedStatement.java | 2 +-
.../core/statement/ShardingSphereStatement.java | 2 +-
.../authority/checker/AuthorityChecker.java | 58 ++++++++++++++--------
.../authority/checker/UserAuthorityChecker.java | 56 ---------------------
...gsphere.infra.executor.check.checker.SQLChecker | 18 -------
.../authority/checker/AuthorityCheckerTest.java | 31 ++++++------
.../checker/UserAuthorityCheckerTest.java | 41 ---------------
.../handler/ProxyBackendHandlerFactory.java | 10 +++-
.../executor/AbstractDatabaseMetaDataExecutor.java | 4 +-
.../mysql/executor/ShowDatabasesExecutor.java | 6 +--
.../admin/mysql/executor/UseDatabaseExecutor.java | 6 +--
.../database/DropDatabaseBackendHandler.java | 6 +--
.../handler/ProxyBackendHandlerFactoryTest.java | 17 ++++---
.../authentication/MySQLAuthenticationHandler.java | 8 +--
.../admin/initdb/MySQLComInitDbExecutor.java | 6 +--
.../text/query/MySQLMultiStatementsHandler.java | 2 +-
.../query/MySQLComQueryPacketExecutorTest.java | 4 ++
.../query/MySQLMultiStatementsHandlerTest.java | 2 +
.../OpenGaussAuthenticationHandler.java | 8 +--
.../bind/OpenGaussComBatchBindExecutorTest.java | 1 +
.../PostgreSQLAuthenticationHandler.java | 8 +--
.../PostgreSQLBatchedStatementsExecutor.java | 2 +-
...egatedBatchedStatementsCommandExecutorTest.java | 1 +
.../PostgreSQLBatchedStatementsExecutorTest.java | 1 +
25 files changed, 114 insertions(+), 210 deletions(-)
diff --git
a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/check/SQLCheckEngine.java
b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/check/SQLCheckEngine.java
index 1f48d1e5676..613b3af4b19 100644
---
a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/check/SQLCheckEngine.java
+++
b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/check/SQLCheckEngine.java
@@ -28,6 +28,7 @@ import
org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.util.spi.type.ordered.OrderedSPILoader;
import java.util.Collection;
+import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
@@ -38,7 +39,7 @@ import java.util.Map.Entry;
public final class SQLCheckEngine {
/**
- * Check SQL with database rules.
+ * Check SQL.
*
* @param sqlStatementContext SQL statement context
* @param params SQL parameters
@@ -46,24 +47,11 @@ public final class SQLCheckEngine {
* @param database database
* @param grantee grantee
*/
- public static void checkWithDatabaseRules(final SQLStatementContext<?>
sqlStatementContext, final List<Object> params,
- final ShardingSphereRuleMetaData
globalRuleMetaData, final ShardingSphereDatabase database, final Grantee
grantee) {
- checkWithRules(sqlStatementContext, params, globalRuleMetaData,
database.getRuleMetaData().getRules(), database, grantee);
- }
-
- /**
- * Check SQL with rules.
- *
- * @param sqlStatementContext SQL statement context
- * @param params SQL parameters
- * @param globalRuleMetaData global rule meta data
- * @param rules rules
- * @param database database
- * @param grantee grantee
- */
@SuppressWarnings({"rawtypes", "unchecked"})
- public static void checkWithRules(final SQLStatementContext<?>
sqlStatementContext, final List<Object> params, final
ShardingSphereRuleMetaData globalRuleMetaData,
- final Collection<ShardingSphereRule>
rules, final ShardingSphereDatabase database, final Grantee grantee) {
+ public static void check(final SQLStatementContext<?> sqlStatementContext,
final List<Object> params,
+ final ShardingSphereRuleMetaData
globalRuleMetaData, final ShardingSphereDatabase database, final Grantee
grantee) {
+ Collection<ShardingSphereRule> rules = new
LinkedList<>(globalRuleMetaData.getRules());
+ rules.addAll(database.getRuleMetaData().getRules());
for (Entry<ShardingSphereRule, SQLChecker> entry :
OrderedSPILoader.getServices(SQLChecker.class, rules).entrySet()) {
entry.getValue().check(sqlStatementContext, params, grantee,
globalRuleMetaData, database, entry.getKey());
}
diff --git
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
index 918bb9e1c0b..727a11fb5d3 100644
---
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
+++
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
@@ -549,7 +549,7 @@ public final class ShardingSpherePreparedStatement extends
AbstractPreparedState
private ExecutionContext createExecutionContext(final QueryContext
queryContext) {
ShardingSphereRuleMetaData globalRuleMetaData =
metaDataContexts.getMetaData().getGlobalRuleMetaData();
ShardingSphereDatabase currentDatabase =
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName());
-
SQLCheckEngine.checkWithDatabaseRules(queryContext.getSqlStatementContext(),
queryContext.getParameters(), globalRuleMetaData, currentDatabase, null);
+ SQLCheckEngine.check(queryContext.getSqlStatementContext(),
queryContext.getParameters(), globalRuleMetaData, currentDatabase, null);
ExecutionContext result = kernelProcessor.generateExecutionContext(
queryContext, currentDatabase, globalRuleMetaData,
metaDataContexts.getMetaData().getProps(), connection.getConnectionContext());
findGeneratedKey(result).ifPresent(optional ->
generatedValues.addAll(optional.getGeneratedValues()));
diff --git
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
index bbe9b559822..7010d4119c9 100644
---
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
+++
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
@@ -524,7 +524,7 @@ public final class ShardingSphereStatement extends
AbstractStatementAdapter {
clearStatements();
ShardingSphereRuleMetaData globalRuleMetaData =
metaDataContexts.getMetaData().getGlobalRuleMetaData();
ShardingSphereDatabase currentDatabase =
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName());
-
SQLCheckEngine.checkWithDatabaseRules(queryContext.getSqlStatementContext(),
queryContext.getParameters(), globalRuleMetaData, currentDatabase, null);
+ SQLCheckEngine.check(queryContext.getSqlStatementContext(),
queryContext.getParameters(), globalRuleMetaData, currentDatabase, null);
return kernelProcessor.generateExecutionContext(queryContext,
currentDatabase, globalRuleMetaData, metaDataContexts.getMetaData().getProps(),
connection.getConnectionContext());
}
diff --git
a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
index 3a580ac9570..e5e0ab2733c 100644
---
a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
+++
b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
@@ -17,15 +17,13 @@
package org.apache.shardingsphere.authority.checker;
-import org.apache.shardingsphere.authority.constant.AuthorityOrder;
+import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.authority.model.PrivilegeType;
import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.executor.check.checker.SQLChecker;
import
org.apache.shardingsphere.infra.executor.check.exception.SQLCheckException;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
import
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
@@ -46,17 +44,47 @@ import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.UpdateState
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowDatabasesStatement;
import java.util.Collections;
-import java.util.List;
import java.util.Optional;
+import java.util.function.BiPredicate;
/**
* Authority checker.
*/
-public final class AuthorityChecker implements SQLChecker<AuthorityRule> {
+@RequiredArgsConstructor
+public final class AuthorityChecker {
- @Override
- public void check(final SQLStatementContext<?> sqlStatementContext, final
List<Object> params, final Grantee grantee, final ShardingSphereRuleMetaData
globalRuleMetaData,
- final ShardingSphereDatabase database, final
AuthorityRule rule) {
+ private final AuthorityRule rule;
+
+ private final Grantee grantee;
+
+ /**
+ * Check database authority.
+ *
+ * @param databaseName database name
+ * @return authorized or not
+ */
+ public boolean isAuthorized(final String databaseName) {
+ return null == grantee || rule.findPrivileges(grantee).map(optional ->
optional.hasPrivileges(databaseName)).orElse(false);
+ }
+
+ /**
+ * Check authority with cipher.
+ *
+ * @param validator validator
+ * @param cipher cipher
+ * @return authorized or not
+ */
+ public boolean isAuthorized(final BiPredicate<Object, Object> validator,
final Object cipher) {
+ return rule.findUser(grantee).filter(optional ->
validator.test(optional, cipher)).isPresent();
+ }
+
+ /**
+ * Check SQL authority.
+ *
+ * @param sqlStatementContext SQL statement context
+ * @param database current database
+ */
+ public void isAuthorized(final SQLStatementContext<?> sqlStatementContext,
final ShardingSphereDatabase database) {
if (null == grantee) {
return;
}
@@ -65,8 +93,8 @@ public final class AuthorityChecker implements
SQLChecker<AuthorityRule> {
ShardingSpherePreconditions.checkState(null == database ||
privileges.filter(optional ->
optional.hasPrivileges(database.getName())).isPresent(),
() -> new SQLCheckException(String.format("Unknown database
'%s'", null == database ? null : database.getName())));
PrivilegeType privilegeType =
getPrivilege(sqlStatementContext.getSqlStatement());
- boolean hasPrivileges =
privileges.get().hasPrivileges(Collections.singletonList(privilegeType));
- ShardingSpherePreconditions.checkState(hasPrivileges, () -> new
SQLCheckException(String.format("Access denied for operation %s.", null ==
privilegeType ? "" : privilegeType.name())));
+ boolean hasPrivileges =
privileges.get().hasPrivileges(Collections.singleton(privilegeType));
+ ShardingSpherePreconditions.checkState(hasPrivileges, () -> new
SQLCheckException(String.format("Access denied for operation %s", null ==
privilegeType ? "" : privilegeType.name())));
}
private PrivilegeType getPrivilege(final SQLStatement sqlStatement) {
@@ -123,14 +151,4 @@ public final class AuthorityChecker implements
SQLChecker<AuthorityRule> {
}
return null;
}
-
- @Override
- public int getOrder() {
- return AuthorityOrder.ORDER;
- }
-
- @Override
- public Class<AuthorityRule> getTypeClass() {
- return AuthorityRule.class;
- }
}
diff --git
a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/checker/UserAuthorityChecker.java
b/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/checker/UserAuthorityChecker.java
deleted file mode 100644
index 9db223e9a98..00000000000
---
a/kernel/authority/core/src/main/java/org/apache/shardingsphere/authority/checker/UserAuthorityChecker.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * 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.checker;
-
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.authority.rule.AuthorityRule;
-import org.apache.shardingsphere.infra.metadata.user.Grantee;
-
-import java.util.function.BiPredicate;
-
-/**
- * User authority checker.
- */
-@RequiredArgsConstructor
-public final class UserAuthorityChecker {
-
- private final AuthorityRule rule;
-
- private final Grantee grantee;
-
- /**
- * Check database authority.
- *
- * @param databaseName database name
- * @return authorized or not
- */
- public boolean isAuthorized(final String databaseName) {
- return null == grantee || rule.findPrivileges(grantee).map(optional ->
optional.hasPrivileges(databaseName)).orElse(false);
- }
-
- /**
- * Check authority with cipher.
- *
- * @param validator validator
- * @param cipher cipher
- * @return authorized or not
- */
- public boolean isAuthorized(final BiPredicate<Object, Object> validator,
final Object cipher) {
- return rule.findUser(grantee).filter(optional ->
validator.test(optional, cipher)).isPresent();
- }
-}
diff --git
a/kernel/authority/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.executor.check.checker.SQLChecker
b/kernel/authority/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.executor.check.checker.SQLChecker
deleted file mode 100644
index bb820253981..00000000000
---
a/kernel/authority/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.executor.check.checker.SQLChecker
+++ /dev/null
@@ -1,18 +0,0 @@
-#
-# 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.
-#
-
-org.apache.shardingsphere.authority.checker.AuthorityChecker
diff --git
a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/checker/AuthorityCheckerTest.java
b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/checker/AuthorityCheckerTest.java
index b013ef2b50a..f469f758ca0 100644
---
a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/checker/AuthorityCheckerTest.java
+++
b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/checker/AuthorityCheckerTest.java
@@ -23,36 +23,33 @@ import
org.apache.shardingsphere.infra.binder.statement.ddl.CreateTableStatement
import
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
import
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import org.apache.shardingsphere.infra.executor.check.checker.SQLChecker;
-import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
-import org.apache.shardingsphere.infra.util.spi.type.ordered.OrderedSPILoader;
import org.junit.Test;
import java.util.Collection;
import java.util.Collections;
-import java.util.LinkedList;
import java.util.Properties;
+import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
public final class AuthorityCheckerTest {
- @SuppressWarnings("unchecked")
@Test
- public void assertCheck() {
- Collection<ShardingSphereUser> users = new LinkedList<>();
- ShardingSphereUser root = new ShardingSphereUser("root", "",
"localhost");
- users.add(root);
+ public void assertCheckIsAuthorizedDatabase() {
+ Collection<ShardingSphereUser> users = Collections.singleton(new
ShardingSphereUser("root", "", "localhost"));
AuthorityRuleConfiguration ruleConfig = new
AuthorityRuleConfiguration(users, new AlgorithmConfiguration("ALL_PERMITTED",
new Properties()));
- AuthorityRule rule = new AuthorityRule(ruleConfig,
Collections.emptyMap());
- SQLChecker<AuthorityRule> sqlChecker =
OrderedSPILoader.getServices(SQLChecker.class,
Collections.singleton(rule)).get(rule);
- SelectStatementContext selectStatementContext =
mock(SelectStatementContext.class);
- CreateTableStatementContext createTableStatementContext =
mock(CreateTableStatementContext.class);
- InsertStatementContext insertStatementContext =
mock(InsertStatementContext.class);
- sqlChecker.check(selectStatementContext, Collections.emptyList(), new
Grantee("root", "localhost"), mock(ShardingSphereRuleMetaData.class), null,
rule);
- sqlChecker.check(insertStatementContext, Collections.emptyList(), new
Grantee("root", "localhost"), mock(ShardingSphereRuleMetaData.class), null,
rule);
- sqlChecker.check(createTableStatementContext, Collections.emptyList(),
new Grantee("root", "localhost"), mock(ShardingSphereRuleMetaData.class), null,
rule);
+ assertTrue(new AuthorityChecker(new AuthorityRule(ruleConfig,
Collections.emptyMap()), new Grantee("root", "localhost")).isAuthorized("db0"));
+ }
+
+ @Test
+ public void assertCheck() {
+ Collection<ShardingSphereUser> users = Collections.singleton(new
ShardingSphereUser("root", "", "localhost"));
+ AuthorityRule rule = new AuthorityRule(new
AuthorityRuleConfiguration(users, new AlgorithmConfiguration("ALL_PERMITTED",
new Properties())), Collections.emptyMap());
+ AuthorityChecker authorityChecker = new AuthorityChecker(rule, new
Grantee("root", "localhost"));
+ authorityChecker.isAuthorized(mock(SelectStatementContext.class),
null);
+ authorityChecker.isAuthorized(mock(InsertStatementContext.class),
null);
+ authorityChecker.isAuthorized(mock(CreateTableStatementContext.class),
null);
}
}
diff --git
a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/checker/UserAuthorityCheckerTest.java
b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/checker/UserAuthorityCheckerTest.java
deleted file mode 100644
index 3aefe752f44..00000000000
---
a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/checker/UserAuthorityCheckerTest.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * 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.checker;
-
-import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
-import org.apache.shardingsphere.authority.rule.AuthorityRule;
-import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import org.apache.shardingsphere.infra.metadata.user.Grantee;
-import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
-import org.junit.Test;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Properties;
-
-import static org.junit.Assert.assertTrue;
-
-public final class UserAuthorityCheckerTest {
-
- @Test
- public void assertCheckIsAuthorizedDatabase() {
- Collection<ShardingSphereUser> users = Collections.singleton(new
ShardingSphereUser("root", "", "localhost"));
- AuthorityRuleConfiguration ruleConfig = new
AuthorityRuleConfiguration(users, new AlgorithmConfiguration("ALL_PERMITTED",
new Properties()));
- assertTrue(new UserAuthorityChecker(new AuthorityRule(ruleConfig,
Collections.emptyMap()), new Grantee("root", "localhost")).isAuthorized("db0"));
- }
-}
diff --git
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactory.java
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactory.java
index 7d60f2d823e..b8de04a1475 100644
---
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactory.java
+++
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactory.java
@@ -20,6 +20,8 @@ package org.apache.shardingsphere.proxy.backend.handler;
import com.google.common.base.Strings;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.authority.checker.AuthorityChecker;
+import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.distsql.parser.statement.DistSQLStatement;
import
org.apache.shardingsphere.distsql.parser.statement.ral.QueryableRALStatement;
import org.apache.shardingsphere.distsql.parser.statement.rql.RQLStatement;
@@ -30,6 +32,7 @@ import
org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.executor.check.SQLCheckEngine;
import org.apache.shardingsphere.infra.hint.HintValueContext;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import
org.apache.shardingsphere.infra.util.exception.external.sql.type.generic.UnsupportedSQLOperationException;
@@ -157,8 +160,11 @@ public final class ProxyBackendHandlerFactory {
String databaseName =
sqlStatementContext.getTablesContext().getDatabaseName().isPresent()
?
sqlStatementContext.getTablesContext().getDatabaseName().get()
: connectionSession.getDatabaseName();
- SQLCheckEngine.checkWithRules(sqlStatementContext,
queryContext.getParameters(),
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData(),
- getRules(databaseName),
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase(databaseName),
connectionSession.getGrantee());
+ AuthorityRule authorityRule =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
+ ShardingSphereDatabase database =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase(databaseName);
+ new AuthorityChecker(authorityRule,
connectionSession.getGrantee()).isAuthorized(sqlStatementContext, database);
+ SQLCheckEngine.check(sqlStatementContext, queryContext.getParameters(),
+
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData(),
database, connectionSession.getGrantee());
backendHandler =
DatabaseAdminBackendHandlerFactory.newInstance(databaseType,
sqlStatementContext, connectionSession);
return backendHandler.orElseGet(() ->
DatabaseBackendHandlerFactory.newInstance(queryContext, connectionSession,
preferPreparedStatement));
}
diff --git
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/AbstractDatabaseMetaDataExecutor.java
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/AbstractDatabaseMetaDataExecutor.java
index bd119d164bd..348d73062f7 100644
---
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/AbstractDatabaseMetaDataExecutor.java
+++
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/AbstractDatabaseMetaDataExecutor.java
@@ -20,7 +20,7 @@ package
org.apache.shardingsphere.proxy.backend.handler.admin.executor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
-import org.apache.shardingsphere.authority.checker.UserAuthorityChecker;
+import org.apache.shardingsphere.authority.checker.AuthorityChecker;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResultMetaData;
import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.metadata.RawQueryResultColumnMetaData;
@@ -132,7 +132,7 @@ public abstract class AbstractDatabaseMetaDataExecutor
implements DatabaseAdminQ
protected static boolean isAuthorized(final String databaseName, final
Grantee grantee) {
AuthorityRule authorityRule =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
- return new UserAuthorityChecker(authorityRule,
grantee).isAuthorized(databaseName);
+ return new AuthorityChecker(authorityRule,
grantee).isAuthorized(databaseName);
}
/**
diff --git
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/ShowDatabasesExecutor.java
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/ShowDatabasesExecutor.java
index b7f723d96f5..fe238157fb2 100644
---
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/ShowDatabasesExecutor.java
+++
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/ShowDatabasesExecutor.java
@@ -19,7 +19,7 @@ package
org.apache.shardingsphere.proxy.backend.handler.admin.mysql.executor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.authority.checker.UserAuthorityChecker;
+import org.apache.shardingsphere.authority.checker.AuthorityChecker;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResultMetaData;
import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.metadata.RawQueryResultColumnMetaData;
@@ -57,9 +57,9 @@ public final class ShowDatabasesExecutor implements
DatabaseAdminQueryExecutor {
private Collection<LocalDataQueryResultRow> getDatabaseNames(final
ConnectionSession connectionSession) {
AuthorityRule authorityRule =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
- UserAuthorityChecker userAuthorityChecker = new
UserAuthorityChecker(authorityRule, connectionSession.getGrantee());
+ AuthorityChecker authorityChecker = new
AuthorityChecker(authorityRule, connectionSession.getGrantee());
return
ProxyContext.getInstance().getAllDatabaseNames().stream().sorted()
- .filter(each -> checkLikePattern(each) &&
userAuthorityChecker.isAuthorized(each)).map(LocalDataQueryResultRow::new).collect(Collectors.toList());
+ .filter(each -> checkLikePattern(each) &&
authorityChecker.isAuthorized(each)).map(LocalDataQueryResultRow::new).collect(Collectors.toList());
}
private boolean checkLikePattern(final String databaseName) {
diff --git
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/UseDatabaseExecutor.java
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/UseDatabaseExecutor.java
index ab786d874a6..224d9a38615 100644
---
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/UseDatabaseExecutor.java
+++
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/UseDatabaseExecutor.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.proxy.backend.handler.admin.mysql.executor;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.authority.checker.UserAuthorityChecker;
+import org.apache.shardingsphere.authority.checker.AuthorityChecker;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import
org.apache.shardingsphere.dialect.exception.syntax.database.UnknownDatabaseException;
import
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
@@ -40,8 +40,8 @@ public final class UseDatabaseExecutor implements
DatabaseAdminExecutor {
public void execute(final ConnectionSession connectionSession) {
String databaseName =
SQLUtil.getExactlyValue(useStatement.getSchema());
AuthorityRule authorityRule =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
- UserAuthorityChecker userAuthorityChecker = new
UserAuthorityChecker(authorityRule, connectionSession.getGrantee());
-
ShardingSpherePreconditions.checkState(ProxyContext.getInstance().databaseExists(databaseName)
&& userAuthorityChecker.isAuthorized(databaseName),
+ AuthorityChecker authorityChecker = new
AuthorityChecker(authorityRule, connectionSession.getGrantee());
+
ShardingSpherePreconditions.checkState(ProxyContext.getInstance().databaseExists(databaseName)
&& authorityChecker.isAuthorized(databaseName),
() -> new UnknownDatabaseException(databaseName));
connectionSession.setCurrentDatabase(databaseName);
}
diff --git
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/database/DropDatabaseBackendHandler.java
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/database/DropDatabaseBackendHandler.java
index 7dc9c16d5dc..f0de60d0da3 100644
---
a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/database/DropDatabaseBackendHandler.java
+++
b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/database/DropDatabaseBackendHandler.java
@@ -19,7 +19,7 @@ package
org.apache.shardingsphere.proxy.backend.handler.database;
import com.google.common.base.Strings;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.authority.checker.UserAuthorityChecker;
+import org.apache.shardingsphere.authority.checker.AuthorityChecker;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import
org.apache.shardingsphere.dialect.exception.syntax.database.DatabaseDropNotExistsException;
import
org.apache.shardingsphere.dialect.exception.syntax.database.UnknownDatabaseException;
@@ -55,8 +55,8 @@ public final class DropDatabaseBackendHandler implements
ProxyBackendHandler {
private void check(final DropDatabaseStatement sqlStatement, final Grantee
grantee) {
String databaseName = sqlStatement.getDatabaseName().toLowerCase();
AuthorityRule authorityRule =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
- UserAuthorityChecker userAuthorityChecker = new
UserAuthorityChecker(authorityRule, grantee);
-
ShardingSpherePreconditions.checkState(userAuthorityChecker.isAuthorized(databaseName),
() -> new UnknownDatabaseException(databaseName));
+ AuthorityChecker authorityChecker = new
AuthorityChecker(authorityRule, grantee);
+
ShardingSpherePreconditions.checkState(authorityChecker.isAuthorized(databaseName),
() -> new UnknownDatabaseException(databaseName));
ShardingSpherePreconditions.checkState(sqlStatement.isIfExists() ||
ProxyContext.getInstance().databaseExists(databaseName), () -> new
DatabaseDropNotExistsException(databaseName));
}
diff --git
a/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactoryTest.java
b/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactoryTest.java
index c1ade89344a..b0eb9332308 100644
---
a/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactoryTest.java
+++
b/proxy/backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactoryTest.java
@@ -17,6 +17,8 @@
package org.apache.shardingsphere.proxy.backend.handler;
+import org.apache.shardingsphere.authority.rule.AuthorityRule;
+import
org.apache.shardingsphere.authority.rule.builder.DefaultAuthorityRuleConfigurationBuilder;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.DefaultDatabase;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
@@ -26,8 +28,8 @@ import
org.apache.shardingsphere.infra.util.exception.external.sql.type.generic.
import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
-import org.apache.shardingsphere.parser.config.SQLParserRuleConfiguration;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
+import
org.apache.shardingsphere.parser.rule.builder.DefaultSQLParserRuleConfigurationBuilder;
import org.apache.shardingsphere.proxy.backend.communication.BackendConnection;
import
org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
@@ -44,11 +46,10 @@ import
org.apache.shardingsphere.proxy.backend.handler.skip.SkipBackendHandler;
import
org.apache.shardingsphere.proxy.backend.handler.transaction.TransactionBackendHandler;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.proxy.backend.util.ProxyContextRestorer;
-import org.apache.shardingsphere.sql.parser.api.CacheOption;
import org.apache.shardingsphere.sql.parser.exception.SQLParsingException;
-import
org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine;
import org.apache.shardingsphere.transaction.api.TransactionType;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
+import
org.apache.shardingsphere.transaction.rule.builder.DefaultTransactionRuleConfigurationBuilder;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
@@ -84,6 +85,7 @@ public final class ProxyBackendHandlerFactoryTest extends
ProxyContextRestorer {
public void setUp() {
when(connectionSession.getTransactionStatus().getTransactionType()).thenReturn(TransactionType.LOCAL);
when(connectionSession.getDefaultDatabaseName()).thenReturn("db");
+ when(connectionSession.getDatabaseName()).thenReturn("db");
when(connectionSession.getBackendConnection()).thenReturn(backendConnection);
when(backendConnection.getConnectionSession()).thenReturn(connectionSession);
MetaDataContexts metaDataContexts = mock(MetaDataContexts.class,
RETURNS_DEEP_STUBS);
@@ -92,11 +94,10 @@ public final class ProxyBackendHandlerFactoryTest extends
ProxyContextRestorer {
ContextManager contextManager = mock(ContextManager.class,
RETURNS_DEEP_STUBS);
when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
when(metaDataContexts.getMetaData().getProps()).thenReturn(new
ConfigurationProperties(new Properties()));
- CacheOption cacheOption = new CacheOption(1024, 1024);
- TransactionRule transactionRule = mock(TransactionRule.class);
- when(transactionRule.getResource()).thenReturn(new
ShardingSphereTransactionManagerEngine());
- ShardingSphereRuleMetaData globalRuleMetaData = new
ShardingSphereRuleMetaData(
- Arrays.asList(transactionRule, new SQLParserRule(new
SQLParserRuleConfiguration(true, cacheOption, cacheOption))));
+ ShardingSphereRuleMetaData globalRuleMetaData = new
ShardingSphereRuleMetaData(Arrays.asList(
+ new AuthorityRule(new
DefaultAuthorityRuleConfigurationBuilder().build(), Collections.emptyMap()),
+ new SQLParserRule(new
DefaultSQLParserRuleConfigurationBuilder().build()),
+ new TransactionRule(new
DefaultTransactionRuleConfigurationBuilder().build(), Collections.emptyMap())));
when(metaDataContexts.getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
ProxyContext.init(contextManager);
}
diff --git
a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationHandler.java
b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationHandler.java
index 770ef0ad32f..1de8b70caf0 100644
---
a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationHandler.java
+++
b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationHandler.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.proxy.frontend.mysql.authentication;
import lombok.Getter;
-import org.apache.shardingsphere.authority.checker.UserAuthorityChecker;
+import org.apache.shardingsphere.authority.checker.AuthorityChecker;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import
org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLAuthPluginData;
import org.apache.shardingsphere.dialect.mysql.vendor.MySQLVendorError;
@@ -50,12 +50,12 @@ public final class MySQLAuthenticationHandler {
public Optional<MySQLVendorError> login(final String username, final
String hostname, final byte[] authenticationResponse, final String
databaseName) {
AuthorityRule authorityRule =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
Grantee grantee = new Grantee(username, hostname);
- UserAuthorityChecker userAuthorityChecker = new
UserAuthorityChecker(authorityRule, grantee);
+ AuthorityChecker authorityChecker = new
AuthorityChecker(authorityRule, grantee);
MySQLAuthenticator authenticator = getAuthenticator(username,
hostname);
- if (!userAuthorityChecker.isAuthorized((a, b) ->
authenticator.authenticate((ShardingSphereUser) a, (byte[]) b),
authenticationResponse)) {
+ if (!authorityChecker.isAuthorized((a, b) ->
authenticator.authenticate((ShardingSphereUser) a, (byte[]) b),
authenticationResponse)) {
return Optional.of(MySQLVendorError.ER_ACCESS_DENIED_ERROR);
}
- return null == databaseName ||
userAuthorityChecker.isAuthorized(databaseName) ? Optional.empty() :
Optional.of(MySQLVendorError.ER_DBACCESS_DENIED_ERROR);
+ return null == databaseName ||
authorityChecker.isAuthorized(databaseName) ? Optional.empty() :
Optional.of(MySQLVendorError.ER_DBACCESS_DENIED_ERROR);
}
/**
diff --git
a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/admin/initdb/MySQLComInitDbExecutor.java
b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/admin/initdb/MySQLComInitDbExecutor.java
index c0f8333c66a..c386643e569 100644
---
a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/admin/initdb/MySQLComInitDbExecutor.java
+++
b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/admin/initdb/MySQLComInitDbExecutor.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.proxy.frontend.mysql.command.admin.initdb;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.authority.checker.UserAuthorityChecker;
+import org.apache.shardingsphere.authority.checker.AuthorityChecker;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import
org.apache.shardingsphere.db.protocol.mysql.packet.command.admin.initdb.MySQLComInitDbPacket;
import
org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLOKPacket;
@@ -48,8 +48,8 @@ public final class MySQLComInitDbExecutor implements
CommandExecutor {
public Collection<DatabasePacket<?>> execute() {
String databaseName = SQLUtil.getExactlyValue(packet.getSchema());
AuthorityRule authorityRule =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
- UserAuthorityChecker userAuthorityChecker = new
UserAuthorityChecker(authorityRule, connectionSession.getGrantee());
-
ShardingSpherePreconditions.checkState(ProxyContext.getInstance().databaseExists(databaseName)
&& userAuthorityChecker.isAuthorized(databaseName),
+ AuthorityChecker authorityChecker = new
AuthorityChecker(authorityRule, connectionSession.getGrantee());
+
ShardingSpherePreconditions.checkState(ProxyContext.getInstance().databaseExists(databaseName)
&& authorityChecker.isAuthorized(databaseName),
() -> new UnknownDatabaseException(packet.getSchema()));
connectionSession.setCurrentDatabase(packet.getSchema());
return Collections.singletonList(new
MySQLOKPacket(ServerStatusFlagCalculator.calculateFor(connectionSession)));
diff --git
a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLMultiStatementsHandler.java
b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLMultiStatementsHandler.java
index d35b2904da9..6f8e4154af0 100644
---
a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLMultiStatementsHandler.java
+++
b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLMultiStatementsHandler.java
@@ -129,7 +129,7 @@ public final class MySQLMultiStatementsHandler implements
ProxyBackendHandler {
private ExecutionContext createExecutionContext(final QueryContext
queryContext) {
ShardingSphereRuleMetaData globalRuleMetaData =
metaDataContexts.getMetaData().getGlobalRuleMetaData();
ShardingSphereDatabase currentDatabase =
metaDataContexts.getMetaData().getDatabase(connectionSession.getDatabaseName());
-
SQLCheckEngine.checkWithDatabaseRules(queryContext.getSqlStatementContext(),
queryContext.getParameters(), globalRuleMetaData, currentDatabase, null);
+ SQLCheckEngine.check(queryContext.getSqlStatementContext(),
queryContext.getParameters(), globalRuleMetaData, currentDatabase, null);
return kernelProcessor.generateExecutionContext(queryContext,
currentDatabase, globalRuleMetaData, metaDataContexts.getMetaData().getProps(),
connectionSession.getConnectionContext());
}
diff --git
a/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutorTest.java
b/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutorTest.java
index c6fcba9ebea..bba55450164 100644
---
a/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutorTest.java
+++
b/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLComQueryPacketExecutorTest.java
@@ -25,6 +25,7 @@ import
org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLOKPacket;
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
import
org.apache.shardingsphere.parser.rule.builder.DefaultSQLParserRuleConfigurationBuilder;
@@ -121,6 +122,9 @@ public final class MySQLComQueryPacketExecutorTest {
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps().<Integer>getValue(ConfigurationPropertyKey.KERNEL_EXECUTOR_SIZE)).thenReturn(1);
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps().<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW)).thenReturn(false);
+ ShardingSphereDatabase database =
mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
+ when(database.getRuleMetaData()).thenReturn(new
ShardingSphereRuleMetaData(Collections.emptyList()));
+
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase("db_name")).thenReturn(database);
MySQLComQueryPacketExecutor actual = new
MySQLComQueryPacketExecutor(packet, connectionSession);
MemberAccessor accessor = Plugins.getMemberAccessor();
accessor.set(MySQLComQueryPacketExecutor.class.getDeclaredField("proxyBackendHandler"),
actual, proxyBackendHandler);
diff --git
a/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLMultiStatementsHandlerTest.java
b/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLMultiStatementsHandlerTest.java
index 27efb1f740f..cf0d2d84aea 100644
---
a/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLMultiStatementsHandlerTest.java
+++
b/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/text/query/MySQLMultiStatementsHandlerTest.java
@@ -95,6 +95,8 @@ public final class MySQLMultiStatementsHandlerTest {
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase("db").getResourceMetaData().getStorageTypes())
.thenReturn(Collections.singletonMap("ds_0", new
MySQLDatabaseType()));
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase("db").getProtocolType()).thenReturn(new
MySQLDatabaseType());
+
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase("db").getRuleMetaData())
+ .thenReturn(new
ShardingSphereRuleMetaData(Collections.emptyList()));
ShardingSphereRuleMetaData globalRuleMetaData = new
ShardingSphereRuleMetaData(
Arrays.asList(new SQLParserRule(new
DefaultSQLParserRuleConfigurationBuilder().build()), new SQLTranslatorRule(new
DefaultSQLTranslatorRuleConfigurationBuilder().build())));
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
diff --git
a/proxy/frontend/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationHandler.java
b/proxy/frontend/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationHandler.java
index 85585354c43..aab6c3e7c1d 100644
---
a/proxy/frontend/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationHandler.java
+++
b/proxy/frontend/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationHandler.java
@@ -22,7 +22,7 @@ import com.google.common.base.Strings;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
-import org.apache.shardingsphere.authority.checker.UserAuthorityChecker;
+import org.apache.shardingsphere.authority.checker.AuthorityChecker;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import
org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.PostgreSQLPasswordMessagePacket;
import
org.apache.shardingsphere.dialect.exception.syntax.database.UnknownDatabaseException;
@@ -108,11 +108,11 @@ public final class OpenGaussAuthenticationHandler {
AuthorityRule authorityRule =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
Grantee grantee = new Grantee(username, "%");
ShardingSpherePreconditions.checkState(authorityRule.findUser(grantee).isPresent(),
() -> new UnknownUsernameException(username));
- UserAuthorityChecker userAuthorityChecker = new
UserAuthorityChecker(authorityRule, grantee);
- if (!userAuthorityChecker.isAuthorized((a, b) ->
isPasswordRight((ShardingSphereUser) a, (Object[]) b), new
Object[]{passwordMessagePacket.getDigest(), salt, nonce, serverIteration})) {
+ AuthorityChecker authorityChecker = new
AuthorityChecker(authorityRule, grantee);
+ if (!authorityChecker.isAuthorized((a, b) ->
isPasswordRight((ShardingSphereUser) a, (Object[]) b), new
Object[]{passwordMessagePacket.getDigest(), salt, nonce, serverIteration})) {
throw new InvalidPasswordException(username);
}
- ShardingSpherePreconditions.checkState(null == databaseName ||
userAuthorityChecker.isAuthorized(databaseName), () -> new
PrivilegeNotGrantedException(username, databaseName));
+ ShardingSpherePreconditions.checkState(null == databaseName ||
authorityChecker.isAuthorized(databaseName), () -> new
PrivilegeNotGrantedException(username, databaseName));
}
private static boolean isPasswordRight(final ShardingSphereUser user,
final Object[] args) {
diff --git
a/proxy/frontend/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java
b/proxy/frontend/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java
index 9a3e3462d06..8d195689f5b 100644
---
a/proxy/frontend/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java
+++
b/proxy/frontend/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/command/query/extended/bind/OpenGaussComBatchBindExecutorTest.java
@@ -86,6 +86,7 @@ public final class OpenGaussComBatchBindExecutorTest extends
ProxyContextRestore
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
when(database.getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(Collections.singletonList("ds_0"));
when(database.getResourceMetaData().getStorageTypes()).thenReturn(Collections.singletonMap("ds_0",
new OpenGaussDatabaseType()));
+ when(database.getRuleMetaData()).thenReturn(new
ShardingSphereRuleMetaData(Collections.emptyList()));
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase("db")).thenReturn(database);
String statement = "S_1";
OpenGaussComBatchBindPacket packet =
mock(OpenGaussComBatchBindPacket.class);
diff --git
a/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationHandler.java
b/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationHandler.java
index 19f9ac7d501..0ee28f33326 100644
---
a/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationHandler.java
+++
b/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationHandler.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.proxy.frontend.postgresql.authentication;
import com.google.common.base.Strings;
-import org.apache.shardingsphere.authority.checker.UserAuthorityChecker;
+import org.apache.shardingsphere.authority.checker.AuthorityChecker;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import
org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.PostgreSQLPasswordMessagePacket;
import
org.apache.shardingsphere.dialect.exception.syntax.database.UnknownDatabaseException;
@@ -50,12 +50,12 @@ public final class PostgreSQLAuthenticationHandler {
AuthorityRule authorityRule =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
Grantee grantee = new Grantee(username, "%");
ShardingSpherePreconditions.checkState(authorityRule.findUser(grantee).isPresent(),
() -> new UnknownUsernameException(username));
- UserAuthorityChecker userAuthorityChecker = new
UserAuthorityChecker(authorityRule, grantee);
+ AuthorityChecker authorityChecker = new
AuthorityChecker(authorityRule, grantee);
PostgreSQLAuthenticator authenticator = getAuthenticator(username,
grantee.getHostname());
- if (!userAuthorityChecker.isAuthorized((a, b) ->
authenticator.authenticate((ShardingSphereUser) a, (Object[]) b), new
Object[]{passwordMessagePacket.getDigest(), md5Salt})) {
+ if (!authorityChecker.isAuthorized((a, b) ->
authenticator.authenticate((ShardingSphereUser) a, (Object[]) b), new
Object[]{passwordMessagePacket.getDigest(), md5Salt})) {
throw new InvalidPasswordException(username);
}
- ShardingSpherePreconditions.checkState(null == databaseName ||
userAuthorityChecker.isAuthorized(databaseName), () -> new
PrivilegeNotGrantedException(username, databaseName));
+ ShardingSpherePreconditions.checkState(null == databaseName ||
authorityChecker.isAuthorized(databaseName), () -> new
PrivilegeNotGrantedException(username, databaseName));
}
/**
diff --git
a/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutor.java
b/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutor.java
index a6f6841ce06..8e6dc960ab4 100644
---
a/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutor.java
+++
b/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutor.java
@@ -128,7 +128,7 @@ public final class PostgreSQLBatchedStatementsExecutor {
private ExecutionContext createExecutionContext(final QueryContext
queryContext) {
ShardingSphereRuleMetaData globalRuleMetaData =
metaDataContexts.getMetaData().getGlobalRuleMetaData();
ShardingSphereDatabase currentDatabase =
metaDataContexts.getMetaData().getDatabase(connectionSession.getDatabaseName());
-
SQLCheckEngine.checkWithDatabaseRules(queryContext.getSqlStatementContext(),
queryContext.getParameters(), globalRuleMetaData, currentDatabase, null);
+ SQLCheckEngine.check(queryContext.getSqlStatementContext(),
queryContext.getParameters(), globalRuleMetaData, currentDatabase, null);
return kernelProcessor.generateExecutionContext(queryContext,
currentDatabase, globalRuleMetaData, metaDataContexts.getMetaData().getProps(),
connectionSession.getConnectionContext());
}
diff --git
a/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedStatementsCommandExecutorTest.java
b/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedStatementsCommandExecutorTest.java
index ca8f929f9e6..f4899a79e40 100644
---
a/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedStatementsCommandExecutorTest.java
+++
b/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLAggregatedBatchedStatementsCommandExecutorTest.java
@@ -101,6 +101,7 @@ public final class
PostgreSQLAggregatedBatchedStatementsCommandExecutorTest exte
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
when(database.getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(Collections.singletonList("ds_0"));
when(database.getResourceMetaData().getStorageTypes()).thenReturn(Collections.singletonMap("ds_0",
new PostgreSQLDatabaseType()));
+ when(database.getRuleMetaData()).thenReturn(new
ShardingSphereRuleMetaData(Collections.emptyList()));
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase("db")).thenReturn(database);
SQLStatement sqlStatement = SQL_PARSER_ENGINE.parse(SQL, false);
SQLStatementContext sqlStatementContext =
mock(InsertStatementContext.class);
diff --git
a/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutorTest.java
b/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutorTest.java
index 16317e1bdd6..cfdf05c7a74 100644
---
a/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutorTest.java
+++
b/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PostgreSQLBatchedStatementsExecutorTest.java
@@ -95,6 +95,7 @@ public final class PostgreSQLBatchedStatementsExecutorTest
extends ProxyContextR
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
when(database.getResourceMetaData().getStorageTypes()).thenReturn(Collections.singletonMap("ds_0",
new PostgreSQLDatabaseType()));
when(database.getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(Collections.singletonList("ds_0"));
+ when(database.getRuleMetaData()).thenReturn(new
ShardingSphereRuleMetaData(Collections.emptyList()));
when(contextManager.getMetaDataContexts().getMetaData().getDatabase("db")).thenReturn(database);
ShardingSphereRuleMetaData globalRuleMetaData = new
ShardingSphereRuleMetaData(Collections.singleton(new SQLTranslatorRule(new
DefaultSQLTranslatorRuleConfigurationBuilder().build())));
when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);