未验证 提交 2a8061ad 编写于 作者: J Juan Pan(Trista) 提交者: GitHub

Replace authentication of ShardingSphereProxyContext (#5752)

上级 8ffb18e9
......@@ -21,10 +21,10 @@ import com.google.common.base.Strings;
import lombok.Getter;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shardingsphere.infra.auth.ProxyUser;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerErrorCode;
import org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLAuthPluginData;
import org.apache.shardingsphere.proxy.context.ShardingSphereProxyContext;
import org.apache.shardingsphere.infra.auth.ProxyUser;
import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts;
import java.util.Arrays;
import java.util.Collection;
......@@ -37,7 +37,7 @@ import java.util.Optional;
@Getter
public final class MySQLAuthenticationHandler {
private static final ShardingSphereProxyContext SHARDING_PROXY_CONTEXT = ShardingSphereProxyContext.getInstance();
private static final ProxySchemaContexts PROXY_SCHEMA_CONTEXTS = ProxySchemaContexts.getInstance();
private final MySQLAuthPluginData authPluginData = new MySQLAuthPluginData();
......@@ -61,7 +61,7 @@ public final class MySQLAuthenticationHandler {
}
private Optional<ProxyUser> getUser(final String username) {
for (Entry<String, ProxyUser> entry : SHARDING_PROXY_CONTEXT.getAuthentication().getUsers().entrySet()) {
for (Entry<String, ProxyUser> entry : PROXY_SCHEMA_CONTEXTS.getSchemaContexts().getAuthentication().getUsers().entrySet()) {
if (entry.getKey().equals(username)) {
return Optional.of(entry.getValue());
}
......
......@@ -25,7 +25,6 @@ import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLOKPacket;
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts;
import org.apache.shardingsphere.proxy.context.ShardingSphereProxyContext;
import org.apache.shardingsphere.proxy.frontend.api.CommandExecutor;
import org.apache.shardingsphere.sql.parser.sql.util.SQLUtil;
......@@ -53,7 +52,7 @@ public final class MySQLComInitDbExecutor implements CommandExecutor {
}
private boolean isAuthorizedSchema(final String schema) {
Collection<String> authorizedSchemas = ShardingSphereProxyContext.getInstance().getAuthentication().getUsers().get(backendConnection.getUserName()).getAuthorizedSchemas();
Collection<String> authorizedSchemas = ProxySchemaContexts.getInstance().getSchemaContexts().getAuthentication().getUsers().get(backendConnection.getUserName()).getAuthorizedSchemas();
return authorizedSchemas.isEmpty() || authorizedSchemas.contains(schema);
}
}
......@@ -38,7 +38,7 @@ import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.query.QueryHeader;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.context.ShardingSphereProxyContext;
import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts;
import org.apache.shardingsphere.proxy.frontend.api.QueryCommandExecutor;
import org.apache.shardingsphere.proxy.frontend.mysql.MySQLErrPacketFactory;
......@@ -73,7 +73,7 @@ public final class MySQLComStmtExecuteExecutor implements QueryCommandExecutor {
@Override
public Collection<DatabasePacket> execute() {
if (ShardingSphereProxyContext.getInstance().isCircuitBreak()) {
if (ProxySchemaContexts.getInstance().isCircuitBreak()) {
return Collections.singletonList(new MySQLErrPacket(1, CommonErrorCode.CIRCUIT_BREAK_MODE));
}
BackendResponse backendResponse = databaseCommunicationEngine.execute();
......
......@@ -30,18 +30,18 @@ import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLEofPacket
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLOKPacket;
import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
import org.apache.shardingsphere.infra.database.type.DatabaseTypes;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryHeader;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandlerFactory;
import org.apache.shardingsphere.proxy.context.ShardingSphereProxyContext;
import org.apache.shardingsphere.proxy.frontend.api.QueryCommandExecutor;
import org.apache.shardingsphere.proxy.frontend.mysql.MySQLErrPacketFactory;
import org.apache.shardingsphere.infra.database.type.DatabaseTypes;
import java.sql.SQLException;
import java.util.Collection;
......@@ -72,7 +72,7 @@ public final class MySQLComQueryPacketExecutor implements QueryCommandExecutor {
@Override
public Collection<DatabasePacket> execute() throws SQLException {
if (ShardingSphereProxyContext.getInstance().isCircuitBreak()) {
if (ProxySchemaContexts.getInstance().isCircuitBreak()) {
return Collections.singletonList(new MySQLErrPacket(1, CommonErrorCode.CIRCUIT_BREAK_MODE));
}
BackendResponse backendResponse = textProtocolBackendHandler.execute();
......
......@@ -20,15 +20,16 @@ package org.apache.shardingsphere.proxy.frontend.mysql;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.auth.Authentication;
import org.apache.shardingsphere.infra.auth.ProxyUser;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLConnectionPhase;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLOKPacket;
import org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLHandshakePacket;
import org.apache.shardingsphere.db.protocol.mysql.payload.MySQLPacketPayload;
import org.apache.shardingsphere.infra.auth.Authentication;
import org.apache.shardingsphere.infra.auth.ProxyUser;
import org.apache.shardingsphere.orchestration.core.common.event.AuthenticationChangedEvent;
import org.apache.shardingsphere.orchestration.core.common.eventbus.ShardingOrchestrationEventBus;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.context.ShardingSphereProxyContext;
import org.apache.shardingsphere.proxy.frontend.ConnectionIdGenerator;
import org.apache.shardingsphere.proxy.frontend.mysql.auth.MySQLAuthenticationEngine;
import org.junit.Before;
......@@ -121,9 +122,7 @@ public final class MySQLProtocolFrontendEngineTest {
private void setAuthentication(final ProxyUser proxyUser) {
Authentication authentication = new Authentication();
authentication.getUsers().put("root", proxyUser);
Field field = ShardingSphereProxyContext.class.getDeclaredField("authentication");
field.setAccessible(true);
field.set(ShardingSphereProxyContext.getInstance(), authentication);
ShardingOrchestrationEventBus.getInstance().post(new AuthenticationChangedEvent(authentication));
}
@SneakyThrows
......
......@@ -19,11 +19,12 @@ package org.apache.shardingsphere.proxy.frontend.mysql.auth;
import com.google.common.primitives.Bytes;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.auth.Authentication;
import org.apache.shardingsphere.infra.auth.ProxyUser;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerErrorCode;
import org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLAuthPluginData;
import org.apache.shardingsphere.proxy.context.ShardingSphereProxyContext;
import org.apache.shardingsphere.infra.auth.Authentication;
import org.apache.shardingsphere.infra.auth.ProxyUser;
import org.apache.shardingsphere.orchestration.core.common.event.AuthenticationChangedEvent;
import org.apache.shardingsphere.orchestration.core.common.eventbus.ShardingOrchestrationEventBus;
import org.junit.Before;
import org.junit.Test;
......@@ -94,9 +95,7 @@ public final class MySQLAuthenticationHandlerTest {
private void setAuthentication(final ProxyUser proxyUser) {
Authentication authentication = new Authentication();
authentication.getUsers().put("root", proxyUser);
Field field = ShardingSphereProxyContext.class.getDeclaredField("authentication");
field.setAccessible(true);
field.set(ShardingSphereProxyContext.getInstance(), authentication);
ShardingOrchestrationEventBus.getInstance().post(new AuthenticationChangedEvent(authentication));
}
@Test
......
......@@ -38,7 +38,7 @@ import org.apache.shardingsphere.proxy.backend.response.query.QueryData;
import org.apache.shardingsphere.proxy.backend.response.query.QueryHeader;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.context.ShardingSphereProxyContext;
import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts;
import org.apache.shardingsphere.proxy.frontend.api.QueryCommandExecutor;
import org.apache.shardingsphere.proxy.frontend.postgresql.PostgreSQLErrPacketFactory;
......@@ -75,7 +75,7 @@ public final class PostgreSQLComBindExecutor implements QueryCommandExecutor {
@Override
public Collection<DatabasePacket> execute() {
if (ShardingSphereProxyContext.getInstance().isCircuitBreak()) {
if (ProxySchemaContexts.getInstance().isCircuitBreak()) {
return Collections.singletonList(new PostgreSQLErrorResponsePacket());
}
List<DatabasePacket> result = new LinkedList<>();
......
......@@ -26,18 +26,18 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.tex
import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.text.PostgreSQLDataRowPacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQLCommandCompletePacket;
import org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQLErrorResponsePacket;
import org.apache.shardingsphere.infra.database.type.DatabaseTypes;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
import org.apache.shardingsphere.proxy.backend.response.error.ErrorResponse;
import org.apache.shardingsphere.proxy.backend.response.query.QueryHeader;
import org.apache.shardingsphere.proxy.backend.response.query.QueryResponse;
import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
import org.apache.shardingsphere.proxy.backend.schema.ProxySchemaContexts;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandlerFactory;
import org.apache.shardingsphere.proxy.context.ShardingSphereProxyContext;
import org.apache.shardingsphere.proxy.frontend.api.QueryCommandExecutor;
import org.apache.shardingsphere.proxy.frontend.postgresql.PostgreSQLErrPacketFactory;
import org.apache.shardingsphere.infra.database.type.DatabaseTypes;
import java.sql.SQLException;
import java.util.Collection;
......@@ -67,7 +67,7 @@ public final class PostgreSQLComQueryExecutor implements QueryCommandExecutor {
@Override
public Collection<DatabasePacket> execute() throws SQLException {
if (ShardingSphereProxyContext.getInstance().isCircuitBreak()) {
if (ProxySchemaContexts.getInstance().isCircuitBreak()) {
return Collections.singletonList(new PostgreSQLErrorResponsePacket());
}
BackendResponse backendResponse = textProtocolBackendHandler.execute();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册