Skip to content

Commit

Permalink
Revert AuthorityChecker (#29646)
Browse files Browse the repository at this point in the history
* Revert AuthorityChecker

* Revert AuthorityChecker
  • Loading branch information
terrymanu committed Jan 3, 2024
1 parent 635bad4 commit 2241e5b
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 102 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
package org.apache.shardingsphere.authority.checker;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.authority.model.ACLObject;
import org.apache.shardingsphere.authority.obj.DatabaseACLObject;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.infra.metadata.user.Grantee;

Expand All @@ -34,19 +32,12 @@ public final class AuthorityChecker {
private final Grantee grantee;

/**
* Check authority.
*
* @param aclObject acl object
* Check database authority.
*
* @param database database name
* @return authorized or not
*/
public boolean isAuthorized(final ACLObject aclObject) {
if (aclObject instanceof DatabaseACLObject) {
return isAuthorized((DatabaseACLObject) aclObject);
}
return true;
}

private boolean isAuthorized(final DatabaseACLObject aclObject) {
return null == grantee || rule.findPrivileges(grantee).map(optional -> optional.hasPrivileges(aclObject.getDatabase())).orElse(false);
public boolean isAuthorized(final String database) {
return null == grantee || rule.findPrivileges(grantee).map(optional -> optional.hasPrivileges(database)).orElse(false);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.shardingsphere.authority.checker;

import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
import org.apache.shardingsphere.authority.obj.DatabaseACLObject;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
Expand All @@ -37,6 +36,6 @@ class AuthorityCheckerTest {
void assertCheckIsAuthorizedDatabase() {
Collection<ShardingSphereUser> users = Collections.singleton(new ShardingSphereUser("root", "", "localhost"));
AuthorityRuleConfiguration ruleConfig = new AuthorityRuleConfiguration(users, new AlgorithmConfiguration("FIXTURE", new Properties()), Collections.emptyMap(), null);
assertTrue(new AuthorityChecker(new AuthorityRule(ruleConfig), new Grantee("root", "localhost")).isAuthorized(new DatabaseACLObject("db0")));
assertTrue(new AuthorityChecker(new AuthorityRule(ruleConfig), new Grantee("root", "localhost")).isAuthorized("db0"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.authority.checker.AuthorityChecker;
import org.apache.shardingsphere.authority.obj.DatabaseACLObject;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.distsql.statement.DistSQLStatement;
import org.apache.shardingsphere.distsql.statement.ral.QueryableRALStatement;
Expand Down Expand Up @@ -169,7 +168,7 @@ public static ProxyBackendHandler newInstance(final DatabaseType databaseType, f
}
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.getGrantee()).isAuthorized(new DatabaseACLObject(databaseName)),
ShardingSpherePreconditions.checkState(new AuthorityChecker(authorityRule, connectionSession.getGrantee()).isAuthorized(databaseName),
() -> new UnknownDatabaseException(databaseName));
SQLAuditEngine.audit(sqlStatementContext, queryContext.getParameters(), ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData(),
database, connectionSession.getGrantee(), queryContext.getHintValueContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.apache.shardingsphere.authority.checker.AuthorityChecker;
import org.apache.shardingsphere.authority.obj.DatabaseACLObject;
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;
Expand Down Expand Up @@ -132,7 +131,7 @@ protected static Boolean hasDataSource(final String databaseName) {

protected static boolean isAuthorized(final String databaseName, final Grantee grantee) {
AuthorityRule authorityRule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
return new AuthorityChecker(authorityRule, grantee).isAuthorized(new DatabaseACLObject(databaseName));
return new AuthorityChecker(authorityRule, grantee).isAuthorized(databaseName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.common.base.Strings;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.authority.checker.AuthorityChecker;
import org.apache.shardingsphere.authority.obj.DatabaseACLObject;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
Expand Down Expand Up @@ -62,7 +61,7 @@ private void check(final DropDatabaseStatement sqlStatement, final Grantee grant
String databaseName = sqlStatement.getDatabaseName().toLowerCase();
AuthorityRule authorityRule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
AuthorityChecker authorityChecker = new AuthorityChecker(authorityRule, grantee);
ShardingSpherePreconditions.checkState(authorityChecker.isAuthorized(new DatabaseACLObject(databaseName)), () -> new UnknownDatabaseException(databaseName));
ShardingSpherePreconditions.checkState(authorityChecker.isAuthorized(databaseName), () -> new UnknownDatabaseException(databaseName));
ShardingSpherePreconditions.checkState(sqlStatement.isIfExists() || ProxyContext.getInstance().databaseExists(databaseName), () -> new DatabaseDropNotExistsException(databaseName));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.authority.checker.AuthorityChecker;
import org.apache.shardingsphere.authority.obj.DatabaseACLObject;
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;
Expand Down Expand Up @@ -60,7 +59,7 @@ private Collection<LocalDataQueryResultRow> getDatabaseNames(final ConnectionSes
AuthorityRule authorityRule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
AuthorityChecker authorityChecker = new AuthorityChecker(authorityRule, connectionSession.getGrantee());
return ProxyContext.getInstance().getAllDatabaseNames().stream().sorted()
.filter(each -> checkLikePattern(each) && authorityChecker.isAuthorized(new DatabaseACLObject(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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.authority.checker.AuthorityChecker;
import org.apache.shardingsphere.authority.obj.DatabaseACLObject;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.UnknownDatabaseException;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.UnknownDatabaseException;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.handler.admin.executor.DatabaseAdminExecutor;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
Expand All @@ -42,7 +41,7 @@ public void execute(final ConnectionSession connectionSession) {
String databaseName = SQLUtils.getExactlyValue(useStatement.getSchema());
AuthorityRule authorityRule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
AuthorityChecker authorityChecker = new AuthorityChecker(authorityRule, connectionSession.getGrantee());
ShardingSpherePreconditions.checkState(ProxyContext.getInstance().databaseExists(databaseName) && authorityChecker.isAuthorized(new DatabaseACLObject(databaseName)),
ShardingSpherePreconditions.checkState(ProxyContext.getInstance().databaseExists(databaseName) && authorityChecker.isAuthorized(databaseName),
() -> new UnknownDatabaseException(databaseName));
connectionSession.setCurrentDatabase(databaseName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import io.netty.channel.epoll.EpollDomainSocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.authority.checker.AuthorityChecker;
import org.apache.shardingsphere.authority.obj.DatabaseACLObject;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.db.protocol.constant.CommonConstants;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLCapabilityFlag;
Expand Down Expand Up @@ -159,7 +158,7 @@ private boolean login(final AuthorityRule rule, final Grantee grantee, final byt
}

private boolean authorizeDatabase(final AuthorityRule rule, final Grantee grantee, final String databaseName) {
return null == databaseName || new AuthorityChecker(rule, grantee).isAuthorized(new DatabaseACLObject(databaseName));
return null == databaseName || new AuthorityChecker(rule, grantee).isAuthorized(databaseName);
}

private String getHostAddress(final ChannelHandlerContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.authority.checker.AuthorityChecker;
import org.apache.shardingsphere.authority.obj.DatabaseACLObject;
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;
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.UnknownDatabaseException;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.UnknownDatabaseException;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.proxy.frontend.command.executor.CommandExecutor;
Expand All @@ -50,7 +49,7 @@ public Collection<DatabasePacket> execute() {
String databaseName = SQLUtils.getExactlyValue(packet.getSchema());
AuthorityRule authorityRule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
AuthorityChecker authorityChecker = new AuthorityChecker(authorityRule, connectionSession.getGrantee());
ShardingSpherePreconditions.checkState(ProxyContext.getInstance().databaseExists(databaseName) && authorityChecker.isAuthorized(new DatabaseACLObject(databaseName)),
ShardingSpherePreconditions.checkState(ProxyContext.getInstance().databaseExists(databaseName) && authorityChecker.isAuthorized(databaseName),
() -> new UnknownDatabaseException(packet.getSchema()));
connectionSession.setCurrentDatabase(packet.getSchema());
return Collections.singleton(new MySQLOKPacket(ServerStatusFlagCalculator.calculateFor(connectionSession)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.ssl.SslHandler;
import org.apache.shardingsphere.authority.checker.AuthorityChecker;
import org.apache.shardingsphere.authority.obj.DatabaseACLObject;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.db.protocol.constant.CommonConstants;
import org.apache.shardingsphere.db.protocol.constant.DatabaseProtocolServerInfo;
Expand All @@ -42,16 +41,16 @@
import org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.PostgreSQLIdentifierPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.PostgreSQLMessagePacketType;
import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
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.postgresql.exception.authority.EmptyUsernameException;
import org.apache.shardingsphere.infra.exception.postgresql.exception.authority.InvalidPasswordException;
import org.apache.shardingsphere.infra.exception.postgresql.exception.authority.PrivilegeNotGrantedException;
import org.apache.shardingsphere.infra.exception.postgresql.exception.authority.UnknownUsernameException;
import org.apache.shardingsphere.infra.exception.postgresql.exception.protocol.ProtocolViolationException;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.postgresql.handler.admin.executor.variable.charset.PostgreSQLCharacterSets;
Expand Down Expand Up @@ -140,8 +139,7 @@ private void login(final AuthorityRule rule, final String digest) {
ShardingSpherePreconditions.checkState(user.isPresent(), () -> new UnknownUsernameException(username));
Authenticator authenticator = new AuthenticatorFactory<>(OpenGaussAuthenticatorType.class, rule).newInstance(user.get());
ShardingSpherePreconditions.checkState(login(authenticator, user.get(), digest), () -> new InvalidPasswordException(username));
ShardingSpherePreconditions.checkState(null == databaseName || new AuthorityChecker(rule, grantee).isAuthorized(new DatabaseACLObject(databaseName)),
() -> new PrivilegeNotGrantedException(username, databaseName));
ShardingSpherePreconditions.checkState(null == databaseName || new AuthorityChecker(rule, grantee).isAuthorized(databaseName), () -> new PrivilegeNotGrantedException(username, databaseName));
}

private boolean login(final Authenticator authenticator, final ShardingSphereUser user, final String digest) {
Expand Down
Loading

0 comments on commit 2241e5b

Please sign in to comment.