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 ed0f104c9c4 Refactor MySQLAuthenticationEngine (#24166)
ed0f104c9c4 is described below
commit ed0f104c9c4cad002f8641dedd795ee50f859b32
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Feb 15 07:42:23 2023 +0800
Refactor MySQLAuthenticationEngine (#24166)
---
.../handshake/MySQLAuthSwitchRequestPacket.java | 4 +-
...ata.java => MySQLAuthenticationPluginData.java} | 10 ++---
.../packet/handshake/MySQLHandshakePacket.java | 10 ++---
.../MySQLAuthSwitchRequestPacketTest.java | 2 +-
...java => MySQLAuthenticationPluginDataTest.java} | 14 +++---
.../packet/handshake/MySQLHandshakePacketTest.java | 20 ++++-----
.../client/netty/MySQLNegotiateHandlerTest.java | 4 +-
.../authentication/MySQLAuthenticationEngine.java | 51 ++++++++++++----------
.../MySQLNativePasswordAuthenticator.java | 4 +-
9 files changed, 62 insertions(+), 57 deletions(-)
diff --git
a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthSwitchRequestPacket.java
b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthSwitchRequestPacket.java
index a4b04f0a9dd..3042d1a1445 100644
---
a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthSwitchRequestPacket.java
+++
b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthSwitchRequestPacket.java
@@ -42,13 +42,13 @@ public final class MySQLAuthSwitchRequestPacket implements
MySQLPacket {
private final String authPluginName;
@Getter
- private final MySQLAuthPluginData authPluginData;
+ private final MySQLAuthenticationPluginData authPluginData;
public MySQLAuthSwitchRequestPacket(final MySQLPacketPayload payload) {
Preconditions.checkArgument(HEADER == payload.readInt1(), "Header of
MySQL auth switch request packet must be `0xfe`.");
authPluginName = payload.readStringNul();
String strAuthPluginData = payload.readStringNul();
- authPluginData = new
MySQLAuthPluginData(Arrays.copyOfRange(strAuthPluginData.getBytes(), 0, 8),
+ authPluginData = new
MySQLAuthenticationPluginData(Arrays.copyOfRange(strAuthPluginData.getBytes(),
0, 8),
Arrays.copyOfRange(strAuthPluginData.getBytes(), 8, 20));
}
diff --git
a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthPluginData.java
b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthenticationPluginData.java
similarity index 84%
rename from
db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthPluginData.java
rename to
db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthenticationPluginData.java
index 0b6de314984..878f53bbd48 100644
---
a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthPluginData.java
+++
b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthenticationPluginData.java
@@ -31,13 +31,13 @@ import lombok.RequiredArgsConstructor;
*/
@RequiredArgsConstructor
@Getter
-public final class MySQLAuthPluginData {
+public final class MySQLAuthenticationPluginData {
- private final byte[] authPluginDataPart1;
+ private final byte[] authenticationPluginDataPart1;
- private final byte[] authPluginDataPart2;
+ private final byte[] authenticationPluginDataPart2;
- public MySQLAuthPluginData() {
+ public MySQLAuthenticationPluginData() {
this(MySQLRandomGenerator.getINSTANCE().generateRandomBytes(8),
MySQLRandomGenerator.getINSTANCE().generateRandomBytes(12));
}
@@ -47,6 +47,6 @@ public final class MySQLAuthPluginData {
* @return authentication plugin data
*/
public byte[] getAuthenticationPluginData() {
- return Bytes.concat(authPluginDataPart1, authPluginDataPart2);
+ return Bytes.concat(authenticationPluginDataPart1,
authenticationPluginDataPart2);
}
}
diff --git
a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacket.java
b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacket.java
index a4e59ba2857..88f87394e5a 100644
---
a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacket.java
+++
b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacket.java
@@ -46,13 +46,13 @@ public final class MySQLHandshakePacket implements
MySQLPacket {
private final MySQLStatusFlag statusFlag;
- private final MySQLAuthPluginData authPluginData;
+ private final MySQLAuthenticationPluginData authPluginData;
private int capabilityFlagsUpper;
private String authPluginName;
- public MySQLHandshakePacket(final int connectionId, final
MySQLAuthPluginData authPluginData) {
+ public MySQLHandshakePacket(final int connectionId, final
MySQLAuthenticationPluginData authPluginData) {
serverVersion = MySQLServerInfo.getDefaultServerVersion();
this.connectionId = connectionId;
capabilityFlagsLower =
MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsLower();
@@ -74,7 +74,7 @@ public final class MySQLHandshakePacket implements
MySQLPacket {
capabilityFlagsUpper = payload.readInt2();
payload.readInt1();
payload.skipReserved(10);
- authPluginData = new MySQLAuthPluginData(authPluginDataPart1,
readAuthPluginDataPart2(payload));
+ authPluginData = new
MySQLAuthenticationPluginData(authPluginDataPart1,
readAuthPluginDataPart2(payload));
authPluginName = readAuthPluginName(payload);
}
@@ -110,7 +110,7 @@ public final class MySQLHandshakePacket implements
MySQLPacket {
payload.writeInt1(protocolVersion);
payload.writeStringNul(serverVersion);
payload.writeInt4(connectionId);
- payload.writeStringNul(new
String(authPluginData.getAuthPluginDataPart1()));
+ payload.writeStringNul(new
String(authPluginData.getAuthenticationPluginDataPart1()));
payload.writeInt2(capabilityFlagsLower);
payload.writeInt1(characterSet);
payload.writeInt2(statusFlag.getValue());
@@ -123,7 +123,7 @@ public final class MySQLHandshakePacket implements
MySQLPacket {
private void writeAuthPluginDataPart2(final MySQLPacketPayload payload) {
if (isClientSecureConnection()) {
- payload.writeStringNul(new
String(authPluginData.getAuthPluginDataPart2()));
+ payload.writeStringNul(new
String(authPluginData.getAuthenticationPluginDataPart2()));
}
}
diff --git
a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthSwitchRequestPacketTest.java
b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthSwitchRequestPacketTest.java
index 8724f3863eb..bfb08c49e5d 100644
---
a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthSwitchRequestPacketTest.java
+++
b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthSwitchRequestPacketTest.java
@@ -32,7 +32,7 @@ import static org.mockito.Mockito.when;
public final class MySQLAuthSwitchRequestPacketTest {
@Mock
- private MySQLAuthPluginData authPluginData;
+ private MySQLAuthenticationPluginData authPluginData;
@Mock
private MySQLPacketPayload payload;
diff --git
a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthPluginDataTest.java
b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthenticationPluginDataTest.java
similarity index 72%
rename from
db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthPluginDataTest.java
rename to
db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthenticationPluginDataTest.java
index fb5c0aaae95..9a7c59ff32b 100644
---
a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthPluginDataTest.java
+++
b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLAuthenticationPluginDataTest.java
@@ -23,23 +23,23 @@ import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-public final class MySQLAuthPluginDataTest {
+public final class MySQLAuthenticationPluginDataTest {
@Test
public void assertGetAuthPluginData() {
byte[] actualPart1 = {106, 105, 55, 122, 117, 98, 115, 109};
byte[] actualPart2 = {68, 102, 53, 122, 65, 49, 84, 79, 85, 115, 116,
113};
- MySQLAuthPluginData actual = new MySQLAuthPluginData(actualPart1,
actualPart2);
- assertThat(actual.getAuthPluginDataPart1(), is(actualPart1));
- assertThat(actual.getAuthPluginDataPart2(), is(actualPart2));
+ MySQLAuthenticationPluginData actual = new
MySQLAuthenticationPluginData(actualPart1, actualPart2);
+ assertThat(actual.getAuthenticationPluginDataPart1(), is(actualPart1));
+ assertThat(actual.getAuthenticationPluginDataPart2(), is(actualPart2));
assertThat(actual.getAuthenticationPluginData(),
is(Bytes.concat(actualPart1, actualPart2)));
}
@Test
public void assertGetAuthPluginDataWithoutArguments() {
- MySQLAuthPluginData actual = new MySQLAuthPluginData();
- assertThat(actual.getAuthPluginDataPart1().length, is(8));
- assertThat(actual.getAuthPluginDataPart2().length, is(12));
+ MySQLAuthenticationPluginData actual = new
MySQLAuthenticationPluginData();
+ assertThat(actual.getAuthenticationPluginDataPart1().length, is(8));
+ assertThat(actual.getAuthenticationPluginDataPart2().length, is(12));
assertThat(actual.getAuthenticationPluginData().length, is(20));
}
}
diff --git
a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacketTest.java
b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacketTest.java
index 3c8a85358f1..94410453a19 100644
---
a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacketTest.java
+++
b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakePacketTest.java
@@ -57,8 +57,8 @@ public final class MySQLHandshakePacketTest {
assertThat(actual.getCharacterSet(),
is(MySQLServerInfo.DEFAULT_CHARSET.getId()));
assertThat(actual.getStatusFlag(),
is(MySQLStatusFlag.SERVER_STATUS_AUTOCOMMIT));
assertThat(actual.getCapabilityFlagsUpper(),
is(MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsUpper()));
- assertThat(actual.getAuthPluginData().getAuthPluginDataPart1(),
is(part1));
- assertThat(actual.getAuthPluginData().getAuthPluginDataPart2(),
is(part2));
+
assertThat(actual.getAuthPluginData().getAuthenticationPluginDataPart1(),
is(part1));
+
assertThat(actual.getAuthPluginData().getAuthenticationPluginDataPart2(),
is(part2));
verify(payload).skipReserved(10);
}
@@ -77,46 +77,46 @@ public final class MySQLHandshakePacketTest {
assertThat(actual.getCharacterSet(),
is(MySQLServerInfo.DEFAULT_CHARSET.getId()));
assertThat(actual.getStatusFlag(),
is(MySQLStatusFlag.SERVER_STATUS_AUTOCOMMIT));
assertThat(actual.getCapabilityFlagsUpper(),
is(MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH.getValue() >> 16));
- assertThat(actual.getAuthPluginData().getAuthPluginDataPart1(),
is(part1));
- assertThat(actual.getAuthPluginData().getAuthPluginDataPart2(),
is(part2));
+
assertThat(actual.getAuthPluginData().getAuthenticationPluginDataPart1(),
is(part1));
+
assertThat(actual.getAuthPluginData().getAuthenticationPluginDataPart2(),
is(part2));
verify(payload).skipReserved(10);
assertThat(actual.getAuthPluginName(),
is(MySQLAuthenticationMethod.NATIVE.getMethodName()));
}
@Test
public void assertWrite() {
- MySQLAuthPluginData authPluginData = new MySQLAuthPluginData(part1,
part2);
+ MySQLAuthenticationPluginData authPluginData = new
MySQLAuthenticationPluginData(part1, part2);
new MySQLHandshakePacket(1000, authPluginData).write(payload);
verify(payload).writeInt1(MySQLServerInfo.PROTOCOL_VERSION);
verify(payload).writeStringNul(MySQLServerInfo.getDefaultServerVersion());
verify(payload).writeInt4(1000);
- verify(payload).writeStringNul(new
String(authPluginData.getAuthPluginDataPart1()));
+ verify(payload).writeStringNul(new
String(authPluginData.getAuthenticationPluginDataPart1()));
verify(payload).writeInt2(MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsLower());
verify(payload).writeInt1(MySQLServerInfo.DEFAULT_CHARSET.getId());
verify(payload).writeInt2(MySQLStatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue());
verify(payload).writeInt2(MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsUpper());
verify(payload).writeInt1(authPluginData.getAuthenticationPluginData().length +
1);
verify(payload).writeReserved(10);
- verify(payload).writeStringNul(new
String(authPluginData.getAuthPluginDataPart2()));
+ verify(payload).writeStringNul(new
String(authPluginData.getAuthenticationPluginDataPart2()));
}
@Test
public void assertWriteWithClientPluginAuth() {
- MySQLAuthPluginData authPluginData = new MySQLAuthPluginData(part1,
part2);
+ MySQLAuthenticationPluginData authPluginData = new
MySQLAuthenticationPluginData(part1, part2);
MySQLHandshakePacket actual = new MySQLHandshakePacket(1000,
authPluginData);
actual.setAuthPluginName(MySQLAuthenticationMethod.NATIVE);
actual.write(payload);
verify(payload).writeInt1(MySQLServerInfo.PROTOCOL_VERSION);
verify(payload).writeStringNul(MySQLServerInfo.getDefaultServerVersion());
verify(payload).writeInt4(1000);
- verify(payload).writeStringNul(new
String(authPluginData.getAuthPluginDataPart1()));
+ verify(payload).writeStringNul(new
String(authPluginData.getAuthenticationPluginDataPart1()));
verify(payload).writeInt2(MySQLCapabilityFlag.calculateHandshakeCapabilityFlagsLower());
verify(payload).writeInt1(MySQLServerInfo.DEFAULT_CHARSET.getId());
verify(payload).writeInt2(MySQLStatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue());
verify(payload).writeInt2(MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH.getValue() >>
16);
verify(payload).writeInt1(authPluginData.getAuthenticationPluginData().length +
1);
verify(payload).writeReserved(10);
- verify(payload).writeStringNul(new
String(authPluginData.getAuthPluginDataPart2()));
+ verify(payload).writeStringNul(new
String(authPluginData.getAuthenticationPluginDataPart2()));
verify(payload).writeStringNul(MySQLAuthenticationMethod.NATIVE.getMethodName());
}
}
diff --git
a/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/client/netty/MySQLNegotiateHandlerTest.java
b/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/client/netty/MySQLNegotiateHandlerTest.java
index d8b2b7852d7..19c420f909a 100644
---
a/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/client/netty/MySQLNegotiateHandlerTest.java
+++
b/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/client/netty/MySQLNegotiateHandlerTest.java
@@ -25,7 +25,7 @@ import
org.apache.shardingsphere.data.pipeline.mysql.ingest.client.ServerInfo;
import
org.apache.shardingsphere.db.protocol.mysql.constant.MySQLAuthenticationMethod;
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.MySQLAuthPluginData;
+import
org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLAuthenticationPluginData;
import
org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLHandshakePacket;
import
org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLHandshakeResponse41Packet;
import org.apache.shardingsphere.dialect.mysql.vendor.MySQLVendorError;
@@ -73,7 +73,7 @@ public final class MySQLNegotiateHandlerTest {
@Test
public void assertChannelReadHandshakeInitPacket() throws
ReflectiveOperationException {
- MySQLHandshakePacket handshakePacket = new MySQLHandshakePacket(0, new
MySQLAuthPluginData(new byte[8], new byte[12]));
+ MySQLHandshakePacket handshakePacket = new MySQLHandshakePacket(0, new
MySQLAuthenticationPluginData(new byte[8], new byte[12]));
handshakePacket.setAuthPluginName(MySQLAuthenticationMethod.NATIVE);
mysqlNegotiateHandler.channelRead(channelHandlerContext,
handshakePacket);
verify(channel).writeAndFlush(ArgumentMatchers.any(MySQLHandshakeResponse41Packet.class));
diff --git
a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngine.java
b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngine.java
index 24d34bc0cfa..6a0bfc8ce57 100644
---
a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngine.java
+++
b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngine.java
@@ -29,7 +29,7 @@ import
org.apache.shardingsphere.db.protocol.mysql.constant.MySQLConstants;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLStatusFlag;
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.MySQLAuthPluginData;
+import
org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLAuthenticationPluginData;
import
org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLAuthSwitchRequestPacket;
import
org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLAuthSwitchResponsePacket;
import
org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLHandshakePacket;
@@ -58,9 +58,7 @@ import java.util.Optional;
*/
public final class MySQLAuthenticationEngine implements AuthenticationEngine {
- private static final int DEFAULT_STATUS_FLAG =
MySQLStatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue();
-
- private final MySQLAuthPluginData authPluginData = new
MySQLAuthPluginData();
+ private final MySQLAuthenticationPluginData authPluginData = new
MySQLAuthenticationPluginData();
private MySQLConnectionPhase connectionPhase =
MySQLConnectionPhase.INITIAL_HANDSHAKE;
@@ -81,53 +79,60 @@ public final class MySQLAuthenticationEngine implements
AuthenticationEngine {
public AuthenticationResult authenticate(final ChannelHandlerContext
context, final PacketPayload payload) {
AuthorityRule rule =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
if (MySQLConnectionPhase.AUTH_PHASE_FAST_PATH == connectionPhase) {
- currentAuthResult = authPhaseFastPath(context, payload, rule);
+ currentAuthResult = authenticatePhaseFastPath(context, payload,
rule);
if (!currentAuthResult.isFinished()) {
return currentAuthResult;
}
} else if (MySQLConnectionPhase.AUTHENTICATION_METHOD_MISMATCH ==
connectionPhase) {
- authenticationMethodMismatch((MySQLPacketPayload) payload);
+ authenticateMismatchedMethod((MySQLPacketPayload) payload);
}
Grantee grantee = new Grantee(currentAuthResult.getUsername(),
getHostAddress(context));
if (!login(rule, grantee, authResponse)) {
writeErrorPacket(context,
- new
MySQLErrPacket(MySQLVendorError.ER_ACCESS_DENIED_ERROR,
currentAuthResult.getUsername(), getHostAddress(context), 0 ==
authResponse.length ? "NO" : "YES"));
+ new
MySQLErrPacket(MySQLVendorError.ER_ACCESS_DENIED_ERROR,
currentAuthResult.getUsername(), grantee.getHostname(), 0 ==
authResponse.length ? "NO" : "YES"));
return AuthenticationResultBuilder.continued();
}
if (!authorizeDatabase(rule, grantee,
currentAuthResult.getDatabase())) {
writeErrorPacket(context,
- new
MySQLErrPacket(MySQLVendorError.ER_DBACCESS_DENIED_ERROR,
currentAuthResult.getUsername(), getHostAddress(context),
currentAuthResult.getDatabase()));
+ new
MySQLErrPacket(MySQLVendorError.ER_DBACCESS_DENIED_ERROR,
currentAuthResult.getUsername(), grantee.getHostname(),
currentAuthResult.getDatabase()));
return AuthenticationResultBuilder.continued();
}
writeOKPacket(context);
return AuthenticationResultBuilder.finished(grantee.getUsername(),
grantee.getHostname(), currentAuthResult.getDatabase());
}
- private AuthenticationResult authPhaseFastPath(final ChannelHandlerContext
context, final PacketPayload payload, final AuthorityRule rule) {
- MySQLHandshakeResponse41Packet packet = new
MySQLHandshakeResponse41Packet((MySQLPacketPayload) payload);
- authResponse = packet.getAuthResponse();
- MySQLCharacterSet characterSet =
MySQLCharacterSet.findById(packet.getCharacterSet());
-
context.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).set(characterSet.getCharset());
-
context.channel().attr(MySQLConstants.MYSQL_CHARACTER_SET_ATTRIBUTE_KEY).set(characterSet);
- if (!Strings.isNullOrEmpty(packet.getDatabase()) &&
!ProxyContext.getInstance().databaseExists(packet.getDatabase())) {
- writeErrorPacket(context, new
MySQLErrPacket(MySQLVendorError.ER_BAD_DB_ERROR, packet.getDatabase()));
+ private AuthenticationResult authenticatePhaseFastPath(final
ChannelHandlerContext context, final PacketPayload payload, final AuthorityRule
rule) {
+ MySQLHandshakeResponse41Packet handshakeResponsePacket = new
MySQLHandshakeResponse41Packet((MySQLPacketPayload) payload);
+ String database = handshakeResponsePacket.getDatabase();
+ authResponse = handshakeResponsePacket.getAuthResponse();
+ setCharacterSet(context, handshakeResponsePacket);
+ if (!Strings.isNullOrEmpty(database) &&
!ProxyContext.getInstance().databaseExists(database)) {
+ writeErrorPacket(context, new
MySQLErrPacket(MySQLVendorError.ER_BAD_DB_ERROR, database));
return AuthenticationResultBuilder.continued();
}
- ShardingSphereUser user = rule.findUser(new
Grantee(packet.getUsername(), getHostAddress(context))).orElseGet(() -> new
ShardingSphereUser(packet.getUsername(), "", getHostAddress(context)));
+ String username = handshakeResponsePacket.getUsername();
+ String hostname = getHostAddress(context);
+ ShardingSphereUser user = rule.findUser(new Grantee(username,
hostname)).orElseGet(() -> new ShardingSphereUser(username, "", hostname));
Authenticator authenticator = new
AuthenticatorFactory<>(MySQLAuthenticatorType.class, rule).newInstance(user);
- if (isClientPluginAuth(packet) &&
!authenticator.getAuthenticationMethodName().equals(packet.getAuthPluginName()))
{
+ if (isClientPluginAuthenticate(handshakeResponsePacket) &&
!authenticator.getAuthenticationMethodName().equals(handshakeResponsePacket.getAuthPluginName()))
{
connectionPhase =
MySQLConnectionPhase.AUTHENTICATION_METHOD_MISMATCH;
context.writeAndFlush(new
MySQLAuthSwitchRequestPacket(authenticator.getAuthenticationMethodName(),
authPluginData));
- return AuthenticationResultBuilder.continued(packet.getUsername(),
getHostAddress(context), packet.getDatabase());
+ return AuthenticationResultBuilder.continued(username, hostname,
database);
}
- return AuthenticationResultBuilder.finished(packet.getUsername(),
getHostAddress(context), packet.getDatabase());
+ return AuthenticationResultBuilder.finished(username, hostname,
database);
+ }
+
+ private void setCharacterSet(final ChannelHandlerContext context, final
MySQLHandshakeResponse41Packet handshakeResponsePacket) {
+ MySQLCharacterSet characterSet =
MySQLCharacterSet.findById(handshakeResponsePacket.getCharacterSet());
+
context.channel().attr(CommonConstants.CHARSET_ATTRIBUTE_KEY).set(characterSet.getCharset());
+
context.channel().attr(MySQLConstants.MYSQL_CHARACTER_SET_ATTRIBUTE_KEY).set(characterSet);
}
- private boolean isClientPluginAuth(final MySQLHandshakeResponse41Packet
packet) {
+ private boolean isClientPluginAuthenticate(final
MySQLHandshakeResponse41Packet packet) {
return 0 != (packet.getCapabilityFlags() &
MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH.getValue());
}
- private void authenticationMethodMismatch(final MySQLPacketPayload
payload) {
+ private void authenticateMismatchedMethod(final MySQLPacketPayload
payload) {
authResponse = new
MySQLAuthSwitchResponsePacket(payload).getAuthPluginResponse();
}
@@ -152,6 +157,6 @@ public final class MySQLAuthenticationEngine implements
AuthenticationEngine {
}
private void writeOKPacket(final ChannelHandlerContext context) {
- context.writeAndFlush(new MySQLOKPacket(DEFAULT_STATUS_FLAG));
+ context.writeAndFlush(new
MySQLOKPacket(MySQLStatusFlag.SERVER_STATUS_AUTOCOMMIT.getValue()));
}
}
diff --git
a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/MySQLNativePasswordAuthenticator.java
b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/MySQLNativePasswordAuthenticator.java
index 908ca33ee52..45d00ebb856 100644
---
a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/MySQLNativePasswordAuthenticator.java
+++
b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/MySQLNativePasswordAuthenticator.java
@@ -20,7 +20,7 @@ package
org.apache.shardingsphere.proxy.frontend.mysql.authentication.authentica
import com.google.common.base.Strings;
import org.apache.commons.codec.digest.DigestUtils;
import
org.apache.shardingsphere.db.protocol.mysql.constant.MySQLAuthenticationMethod;
-import
org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLAuthPluginData;
+import
org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLAuthenticationPluginData;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import java.util.Arrays;
@@ -35,7 +35,7 @@ public final class MySQLNativePasswordAuthenticator
implements MySQLAuthenticato
@Override
public boolean authenticate(final ShardingSphereUser user, final Object[]
authInfo) {
byte[] authResponse = (byte[]) authInfo[0];
- MySQLAuthPluginData authPluginData = (MySQLAuthPluginData) authInfo[1];
+ MySQLAuthenticationPluginData authPluginData =
(MySQLAuthenticationPluginData) authInfo[1];
return Strings.isNullOrEmpty(user.getPassword()) ||
Arrays.equals(getAuthCipherBytes(user.getPassword(),
authPluginData.getAuthenticationPluginData()), authResponse);
}