This is an automated email from the ASF dual-hosted git repository.
zhaojinchao 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 1fbc3bb60fb Add ProxyBackendHandlerChecker (#32346)
1fbc3bb60fb is described below
commit 1fbc3bb60fb8794d6c806c97c8f15cec842f3f69
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Aug 1 00:26:37 2024 +0800
Add ProxyBackendHandlerChecker (#32346)
* Add ProxyBackendHandlerChecker
* Add ProxyBackendHandlerChecker
* Add ProxyBackendHandlerChecker
* Add ProxyBackendHandlerChecker
---
.../handler/ProxyBackendHandlerFactory.java | 18 ++++------
.../checker/AuditProxyBackendHandlerChecker.java | 35 +++++++++++++++++++
.../AuthorityProxyBackendHandlerChecker.java | 40 ++++++++++++++++++++++
.../checker/ProxyBackendHandlerChecker.java | 39 +++++++++++++++++++++
...kend.handler.checker.ProxyBackendHandlerChecker | 19 ++++++++++
.../handler/ProxyBackendHandlerFactoryTest.java | 5 +--
6 files changed, 140 insertions(+), 16 deletions(-)
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactory.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactory.java
index fd2f2371145..34bb5e5dd53 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactory.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactory.java
@@ -19,8 +19,6 @@ package org.apache.shardingsphere.proxy.backend.handler;
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.statement.DistSQLStatement;
import
org.apache.shardingsphere.distsql.statement.ral.queryable.QueryableRALStatement;
import org.apache.shardingsphere.distsql.statement.rql.RQLStatement;
@@ -31,11 +29,8 @@ import
org.apache.shardingsphere.infra.binder.engine.SQLBindEngine;
import
org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
-import
org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.UnknownDatabaseException;
import
org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
-import org.apache.shardingsphere.infra.executor.audit.SQLAuditEngine;
import org.apache.shardingsphere.infra.hint.HintValueContext;
-import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
@@ -43,6 +38,7 @@ import
org.apache.shardingsphere.infra.state.cluster.ClusterState;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.distsql.DistSQLStatementContext;
import
org.apache.shardingsphere.proxy.backend.handler.admin.DatabaseAdminBackendHandlerFactory;
+import
org.apache.shardingsphere.proxy.backend.handler.checker.ProxyBackendHandlerChecker;
import
org.apache.shardingsphere.proxy.backend.handler.data.DatabaseBackendHandlerFactory;
import
org.apache.shardingsphere.proxy.backend.handler.database.DatabaseOperateBackendHandlerFactory;
import
org.apache.shardingsphere.proxy.backend.handler.distsql.DistSQLBackendHandlerFactory;
@@ -145,13 +141,11 @@ public final class ProxyBackendHandlerFactory {
if (null == databaseName) {
return DatabaseBackendHandlerFactory.newInstance(queryContext,
connectionSession, preferPreparedStatement);
}
- AuthorityRule authorityRule =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
- ShardingSphereDatabase database =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase(databaseName);
- ShardingSpherePreconditions.checkState(new
AuthorityChecker(authorityRule,
connectionSession.getConnectionContext().getGrantee()).isAuthorized(databaseName),
- () -> new UnknownDatabaseException(databaseName));
- SQLAuditEngine.audit(queryContext,
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData(),
database);
- backendHandler =
DatabaseAdminBackendHandlerFactory.newInstance(databaseType,
sqlStatementContext, connectionSession);
- return backendHandler.orElseGet(() ->
DatabaseBackendHandlerFactory.newInstance(queryContext, connectionSession,
preferPreparedStatement));
+ for (ProxyBackendHandlerChecker each :
ShardingSphereServiceLoader.getServiceInstances(ProxyBackendHandlerChecker.class))
{
+ each.check(connectionSession, queryContext,
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase(databaseName));
+ }
+ return DatabaseAdminBackendHandlerFactory.newInstance(databaseType,
sqlStatementContext, connectionSession)
+ .orElseGet(() ->
DatabaseBackendHandlerFactory.newInstance(queryContext, connectionSession,
preferPreparedStatement));
}
private static void allowExecutingWhenTransactionalError(final
DatabaseType databaseType, final ConnectionSession connectionSession, final
SQLStatement sqlStatement) throws SQLException {
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/checker/AuditProxyBackendHandlerChecker.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/checker/AuditProxyBackendHandlerChecker.java
new file mode 100644
index 00000000000..445a8b77b35
--- /dev/null
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/checker/AuditProxyBackendHandlerChecker.java
@@ -0,0 +1,35 @@
+/*
+ * 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.proxy.backend.handler.checker;
+
+import org.apache.shardingsphere.infra.executor.audit.SQLAuditEngine;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.session.query.QueryContext;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+
+/**
+ * Audit proxy backend handler checker.
+ */
+public final class AuditProxyBackendHandlerChecker implements
ProxyBackendHandlerChecker {
+
+ @Override
+ public void check(final ConnectionSession connectionSession, final
QueryContext queryContext, final ShardingSphereDatabase database) {
+ SQLAuditEngine.audit(queryContext,
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData(),
database);
+ }
+}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/checker/AuthorityProxyBackendHandlerChecker.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/checker/AuthorityProxyBackendHandlerChecker.java
new file mode 100644
index 00000000000..2d0aaef7689
--- /dev/null
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/checker/AuthorityProxyBackendHandlerChecker.java
@@ -0,0 +1,40 @@
+/*
+ * 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.proxy.backend.handler.checker;
+
+import org.apache.shardingsphere.authority.checker.AuthorityChecker;
+import org.apache.shardingsphere.authority.rule.AuthorityRule;
+import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
+import
org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.UnknownDatabaseException;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.session.query.QueryContext;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+
+/**
+ * Authority proxy backend handler checker.
+ */
+public final class AuthorityProxyBackendHandlerChecker implements
ProxyBackendHandlerChecker {
+
+ @Override
+ public void check(final ConnectionSession connectionSession, final
QueryContext queryContext, final ShardingSphereDatabase database) {
+ AuthorityRule authorityRule =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
+ ShardingSpherePreconditions.checkState(new
AuthorityChecker(authorityRule,
connectionSession.getConnectionContext().getGrantee()).isAuthorized(database.getName()),
+ () -> new UnknownDatabaseException(database.getName()));
+ }
+}
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/checker/ProxyBackendHandlerChecker.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/checker/ProxyBackendHandlerChecker.java
new file mode 100644
index 00000000000..b1220b4ff4b
--- /dev/null
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/checker/ProxyBackendHandlerChecker.java
@@ -0,0 +1,39 @@
+/*
+ * 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.proxy.backend.handler.checker;
+
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.session.query.QueryContext;
+import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+
+/**
+ * Proxy backend handler checker.
+ */
+@SingletonSPI
+public interface ProxyBackendHandlerChecker {
+
+ /**
+ * Check.
+ *
+ * @param connectionSession connection session
+ * @param queryContext query context
+ * @param database database
+ */
+ void check(ConnectionSession connectionSession, QueryContext queryContext,
ShardingSphereDatabase database);
+}
diff --git
a/proxy/backend/core/src/main/resources/META-INF/services/org.apache.shardingsphere.proxy.backend.handler.checker.ProxyBackendHandlerChecker
b/proxy/backend/core/src/main/resources/META-INF/services/org.apache.shardingsphere.proxy.backend.handler.checker.ProxyBackendHandlerChecker
new file mode 100644
index 00000000000..9d048920821
--- /dev/null
+++
b/proxy/backend/core/src/main/resources/META-INF/services/org.apache.shardingsphere.proxy.backend.handler.checker.ProxyBackendHandlerChecker
@@ -0,0 +1,19 @@
+#
+# 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.proxy.backend.handler.checker.AuthorityProxyBackendHandlerChecker
+org.apache.shardingsphere.proxy.backend.handler.checker.AuditProxyBackendHandlerChecker
diff --git
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactoryTest.java
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactoryTest.java
index ce3a26eaf2a..ed179228666 100644
---
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactoryTest.java
+++
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/ProxyBackendHandlerFactoryTest.java
@@ -25,7 +25,6 @@ import
org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import
org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
import org.apache.shardingsphere.infra.hint.HintValueContext;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
@@ -125,9 +124,7 @@ class ProxyBackendHandlerFactoryTest {
new AuthorityRule(new
DefaultAuthorityRuleConfigurationBuilder().build()),
new SQLParserRule(new
DefaultSQLParserRuleConfigurationBuilder().build()),
new TransactionRule(new
DefaultTransactionRuleConfigurationBuilder().build(), Collections.emptyMap())));
- ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
- when(metaDataContexts.getMetaData()).thenReturn(metaData);
- when(metaData.getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
+
when(metaDataContexts.getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
return result;
}