You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2023/02/12 14:32:52 UTC
[shardingsphere] branch master updated: Refactor MySQLNativePasswordAuthenticator (#24129)
This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 31f6569a177 Refactor MySQLNativePasswordAuthenticator (#24129)
31f6569a177 is described below
commit 31f6569a177f4a9c43f106a50f6e5da150a1d87d
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Feb 12 22:32:45 2023 +0800
Refactor MySQLNativePasswordAuthenticator (#24129)
* Refactor MySQLNativePasswordAuthenticator
* Refactor MySQLNativePasswordAuthenticator
---
.../mysql/packet/handshake/MySQLHandshakePacket.java | 6 +++---
.../handshake/MySQLHandshakeResponse41Packet.java | 6 +++---
.../authentication/MySQLAuthenticationHandler.java | 8 ++++----
.../authenticator/MySQLAuthenticator.java | 2 +-
.../authenticator/MySQLClearPasswordAuthenticator.java | 3 ++-
.../MySQLNativePasswordAuthenticator.java | 18 ++++++++----------
.../authentication/MySQLAuthenticationEngineTest.java | 3 +--
7 files changed, 22 insertions(+), 24 deletions(-)
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 40d21c5f7b8..32c8900d6e4 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
@@ -98,10 +98,10 @@ public final class MySQLHandshakePacket implements MySQLPacket {
/**
* Set authentication plugin name.
*
- * @param mysqlAuthenticationMethod MySQL authentication method
+ * @param authenticationMethod MySQL authentication method
*/
- public void setAuthPluginName(final MySQLAuthenticationMethod mysqlAuthenticationMethod) {
- authPluginName = mysqlAuthenticationMethod.getMethodName();
+ public void setAuthPluginName(final MySQLAuthenticationMethod authenticationMethod) {
+ authPluginName = authenticationMethod.getMethodName();
capabilityFlagsUpper |= MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH.getValue() >> 16;
}
diff --git a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakeResponse41Packet.java b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakeResponse41Packet.java
index 74d1ebae1fb..4b84b330293 100644
--- a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakeResponse41Packet.java
+++ b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/handshake/MySQLHandshakeResponse41Packet.java
@@ -92,10 +92,10 @@ public final class MySQLHandshakeResponse41Packet implements MySQLPacket {
/**
* Set authentication plugin name.
*
- * @param mysqlAuthenticationMethod MySQL authentication method
+ * @param authenticationMethod MySQL authentication method
*/
- public void setAuthPluginName(final MySQLAuthenticationMethod mysqlAuthenticationMethod) {
- authPluginName = mysqlAuthenticationMethod.getMethodName();
+ public void setAuthPluginName(final MySQLAuthenticationMethod authenticationMethod) {
+ authPluginName = authenticationMethod.getMethodName();
capabilityFlags |= MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH.getValue();
}
diff --git a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationHandler.java b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationHandler.java
index 6557629da69..2b4d6e1d87f 100644
--- a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationHandler.java
+++ b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationHandler.java
@@ -53,7 +53,7 @@ public final class MySQLAuthenticationHandler {
AuthorityRule rule = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(AuthorityRule.class);
Grantee grantee = new Grantee(username, hostname);
Optional<ShardingSphereUser> user = rule.findUser(grantee);
- if (!user.isPresent() || !getAuthenticator(rule, user.get()).authenticate(user.get(), authenticationResponse)) {
+ if (!user.isPresent() || !getAuthenticator(rule, user.get()).authenticate(user.get(), new Object[]{authenticationResponse, authPluginData})) {
return Optional.of(MySQLVendorError.ER_ACCESS_DENIED_ERROR);
}
return null == databaseName || new AuthorityChecker(rule, grantee).isAuthorized(databaseName) ? Optional.empty() : Optional.of(MySQLVendorError.ER_DBACCESS_DENIED_ERROR);
@@ -71,11 +71,11 @@ public final class MySQLAuthenticationHandler {
try {
authenticationMethod = MySQLAuthenticationMethod.valueOf(rule.getAuthenticatorType(user).toUpperCase());
} catch (final IllegalArgumentException ignored) {
- return new MySQLNativePasswordAuthenticator(authPluginData);
+ return new MySQLNativePasswordAuthenticator();
}
switch (authenticationMethod) {
case SECURE_PASSWORD_AUTHENTICATION:
- return new MySQLNativePasswordAuthenticator(authPluginData);
+ return new MySQLNativePasswordAuthenticator();
case CLEAR_TEXT_AUTHENTICATION:
return new MySQLClearPasswordAuthenticator();
// TODO add other Authenticator
@@ -83,7 +83,7 @@ public final class MySQLAuthenticationHandler {
case WINDOWS_NATIVE_AUTHENTICATION:
case SHA256:
default:
- return new MySQLNativePasswordAuthenticator(authPluginData);
+ return new MySQLNativePasswordAuthenticator();
}
}
}
diff --git a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/MySQLAuthenticator.java b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/MySQLAuthenticator.java
index c7a8d14034d..c31001f17f9 100644
--- a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/MySQLAuthenticator.java
+++ b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/MySQLAuthenticator.java
@@ -22,5 +22,5 @@ import org.apache.shardingsphere.proxy.frontend.authentication.Authenticator;
/**
* MySQL authenticator.
*/
-public interface MySQLAuthenticator extends Authenticator<byte[]> {
+public interface MySQLAuthenticator extends Authenticator<Object[]> {
}
diff --git a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/MySQLClearPasswordAuthenticator.java b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/MySQLClearPasswordAuthenticator.java
index 782ad4654d4..7f08cc03dc9 100644
--- a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/MySQLClearPasswordAuthenticator.java
+++ b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/MySQLClearPasswordAuthenticator.java
@@ -29,7 +29,8 @@ import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
public final class MySQLClearPasswordAuthenticator implements MySQLAuthenticator {
@Override
- public boolean authenticate(final ShardingSphereUser user, final byte[] authResponse) {
+ public boolean authenticate(final ShardingSphereUser user, final Object[] args) {
+ byte[] authResponse = (byte[]) args[0];
byte[] password = new byte[authResponse.length - 1];
System.arraycopy(authResponse, 0, password, 0, authResponse.length - 1);
return Strings.isNullOrEmpty(user.getPassword()) || user.getPassword().equals(new String(password));
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 efff048eeb8..01416ed8764 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
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.proxy.frontend.mysql.authentication.authenticator;
import com.google.common.base.Strings;
-import lombok.RequiredArgsConstructor;
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;
@@ -31,22 +30,21 @@ import java.util.Arrays;
*
* @see <a href="https://dev.mysql.com/doc/internals/en/secure-password-authentication.html">Secure Password Authentication</a>
*/
-@RequiredArgsConstructor
public final class MySQLNativePasswordAuthenticator implements MySQLAuthenticator {
- private final MySQLAuthPluginData authPluginData;
-
@Override
- public boolean authenticate(final ShardingSphereUser user, final byte[] authResponse) {
- return Strings.isNullOrEmpty(user.getPassword()) || Arrays.equals(getAuthCipherBytes(user.getPassword()), authResponse);
+ public boolean authenticate(final ShardingSphereUser user, final Object[] args) {
+ byte[] authResponse = (byte[]) args[0];
+ MySQLAuthPluginData authPluginData = (MySQLAuthPluginData) args[1];
+ return Strings.isNullOrEmpty(user.getPassword()) || Arrays.equals(getAuthCipherBytes(user.getPassword(), authPluginData.getAuthenticationPluginData()), authResponse);
}
- private byte[] getAuthCipherBytes(final String password) {
+ private byte[] getAuthCipherBytes(final String password, final byte[] authenticationPluginData) {
byte[] sha1Password = DigestUtils.sha1(password);
byte[] doubleSha1Password = DigestUtils.sha1(sha1Password);
- byte[] concatBytes = new byte[authPluginData.getAuthenticationPluginData().length + doubleSha1Password.length];
- System.arraycopy(authPluginData.getAuthenticationPluginData(), 0, concatBytes, 0, authPluginData.getAuthenticationPluginData().length);
- System.arraycopy(doubleSha1Password, 0, concatBytes, authPluginData.getAuthenticationPluginData().length, doubleSha1Password.length);
+ byte[] concatBytes = new byte[authenticationPluginData.length + doubleSha1Password.length];
+ System.arraycopy(authenticationPluginData, 0, concatBytes, 0, authenticationPluginData.length);
+ System.arraycopy(doubleSha1Password, 0, concatBytes, authenticationPluginData.length, doubleSha1Password.length);
byte[] sha1ConcatBytes = DigestUtils.sha1(concatBytes);
return xor(sha1Password, sha1ConcatBytes);
}
diff --git a/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngineTest.java b/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngineTest.java
index 6099b4834c6..3a962872a5a 100644
--- a/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngineTest.java
+++ b/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngineTest.java
@@ -27,7 +27,6 @@ import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLConnectionPhase
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLConstants;
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.MySQLHandshakePacket;
import org.apache.shardingsphere.db.protocol.mysql.payload.MySQLPacketPayload;
import org.apache.shardingsphere.dialect.mysql.vendor.MySQLVendorError;
@@ -105,7 +104,7 @@ public final class MySQLAuthenticationEngineTest extends ProxyContextRestorer {
when(payload.readInt1()).thenReturn(1);
when(payload.readInt4()).thenReturn(MySQLCapabilityFlag.CLIENT_PLUGIN_AUTH.getValue());
when(payload.readStringNul()).thenReturn("root");
- when(authenticationHandler.getAuthenticator(any(), any())).thenReturn(new MySQLNativePasswordAuthenticator(mock(MySQLAuthPluginData.class)));
+ when(authenticationHandler.getAuthenticator(any(), any())).thenReturn(new MySQLNativePasswordAuthenticator());
authenticationEngine.authenticate(channelHandlerContext, payload);
assertThat(getConnectionPhase(), is(MySQLConnectionPhase.AUTHENTICATION_METHOD_MISMATCH));
}