Skip to content

Commit

Permalink
Refactor ProxyBackendHandlerChecker (#32348)
Browse files Browse the repository at this point in the history
* Refactor ProxyBackendHandlerChecker

* Refactor ProxyBackendHandlerChecker
  • Loading branch information
terrymanu authored Jul 31, 2024
1 parent 1fbc3bb commit a002c58
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
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.user.Grantee;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
Expand Down Expand Up @@ -141,8 +144,11 @@ public static ProxyBackendHandler newInstance(final DatabaseType databaseType, f
if (null == databaseName) {
return DatabaseBackendHandlerFactory.newInstance(queryContext, connectionSession, preferPreparedStatement);
}
Grantee grantee = connectionSession.getConnectionContext().getGrantee();
ShardingSphereMetaData metaData = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData();
ShardingSphereDatabase database = metaData.getDatabase(databaseName);
for (ProxyBackendHandlerChecker each : ShardingSphereServiceLoader.getServiceInstances(ProxyBackendHandlerChecker.class)) {
each.check(connectionSession, queryContext, ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase(databaseName));
each.check(metaData, grantee, queryContext, database);
}
return DatabaseAdminBackendHandlerFactory.newInstance(databaseType, sqlStatementContext, connectionSession)
.orElseGet(() -> DatabaseBackendHandlerFactory.newInstance(queryContext, connectionSession, preferPreparedStatement));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
package org.apache.shardingsphere.proxy.backend.handler.checker;

import org.apache.shardingsphere.infra.executor.audit.SQLAuditEngine;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
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);
public void check(final ShardingSphereMetaData metaData, final Grantee grantee, final QueryContext queryContext, final ShardingSphereDatabase database) {
SQLAuditEngine.audit(queryContext, metaData.getGlobalRuleMetaData(), database);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@
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.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
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()),
public void check(final ShardingSphereMetaData metaData, final Grantee grantee, final QueryContext queryContext, final ShardingSphereDatabase database) {
AuthorityRule authorityRule = metaData.getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
ShardingSpherePreconditions.checkState(new AuthorityChecker(authorityRule, grantee).isAuthorized(database.getName()),
() -> new UnknownDatabaseException(database.getName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@

package org.apache.shardingsphere.proxy.backend.handler.checker;

import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
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.
Expand All @@ -31,9 +32,10 @@ public interface ProxyBackendHandlerChecker {
/**
* Check.
*
* @param connectionSession connection session
* @param metaData ShardingSphere meta data
* @param grantee grantee
* @param queryContext query context
* @param database database
*/
void check(ConnectionSession connectionSession, QueryContext queryContext, ShardingSphereDatabase database);
void check(ShardingSphereMetaData metaData, Grantee grantee, QueryContext queryContext, ShardingSphereDatabase database);
}

0 comments on commit a002c58

Please sign in to comment.