You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2023/02/18 14:37:58 UTC

[shardingsphere] branch master updated: Refactor Authenticator.getAuthenticationMethodName to getAuthenticationMethod (#24232)

This is an automated email from the ASF dual-hosted git repository.

sunnianjun 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 23e9900321f Refactor Authenticator.getAuthenticationMethodName to getAuthenticationMethod (#24232)
23e9900321f is described below

commit 23e9900321f13e7514b85272187073d49982b7e6
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Feb 18 22:37:51 2023 +0800

    Refactor Authenticator.getAuthenticationMethodName to getAuthenticationMethod (#24232)
---
 .../authentication/MySQLAuthenticationEngine.java     |  4 ++--
 .../impl/MySQLClearPasswordAuthenticator.java         |  5 +++--
 .../impl/MySQLNativePasswordAuthenticator.java        |  5 +++--
 .../impl/MySQLClearPasswordAuthenticatorTest.java     |  7 -------
 .../OpenGaussSCRAMSha256PasswordAuthenticator.java    |  5 +++--
 .../PostgreSQLAuthenticationEngine.java               |  2 +-
 .../impl/PostgreSQLMD5PasswordAuthenticator.java      |  5 +++--
 .../impl/PostgreSQLPasswordAuthenticator.java         |  5 +++--
 .../impl/PostgreSQLMD5PasswordAuthenticatorTest.java  |  8 --------
 .../impl/PostgreSQLPasswordAuthenticatorTest.java     | 19 ++++---------------
 .../proxy/frontend/authentication/Authenticator.java  |  7 ++++---
 11 files changed, 26 insertions(+), 46 deletions(-)

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 6a0bfc8ce57..ee0ed45c408 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
@@ -114,9 +114,9 @@ public final class MySQLAuthenticationEngine implements AuthenticationEngine {
         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 (isClientPluginAuthenticate(handshakeResponsePacket) && !authenticator.getAuthenticationMethodName().equals(handshakeResponsePacket.getAuthPluginName())) {
+        if (isClientPluginAuthenticate(handshakeResponsePacket) && !authenticator.getAuthenticationMethod().getMethodName().equals(handshakeResponsePacket.getAuthPluginName())) {
             connectionPhase = MySQLConnectionPhase.AUTHENTICATION_METHOD_MISMATCH;
-            context.writeAndFlush(new MySQLAuthSwitchRequestPacket(authenticator.getAuthenticationMethodName(), authPluginData));
+            context.writeAndFlush(new MySQLAuthSwitchRequestPacket(authenticator.getAuthenticationMethod().getMethodName(), authPluginData));
             return AuthenticationResultBuilder.continued(username, hostname, database);
         }
         return AuthenticationResultBuilder.finished(username, hostname, database);
diff --git a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/impl/MySQLClearPasswordAuthenticator.java b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/impl/MySQLClearPasswordAuthenticator.java
index dac127701a5..d640e6bbe68 100644
--- a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/impl/MySQLClearPasswordAuthenticator.java
+++ b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/impl/MySQLClearPasswordAuthenticator.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.proxy.frontend.mysql.authentication.authenticator.impl;
 
 import com.google.common.base.Strings;
+import org.apache.shardingsphere.db.protocol.constant.AuthenticationMethod;
 import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLAuthenticationMethod;
 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
 import org.apache.shardingsphere.proxy.frontend.mysql.authentication.authenticator.MySQLAuthenticator;
@@ -38,7 +39,7 @@ public final class MySQLClearPasswordAuthenticator implements MySQLAuthenticator
     }
     
     @Override
-    public String getAuthenticationMethodName() {
-        return MySQLAuthenticationMethod.CLEAR_TEXT.getMethodName();
+    public AuthenticationMethod getAuthenticationMethod() {
+        return MySQLAuthenticationMethod.CLEAR_TEXT;
     }
 }
diff --git a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/impl/MySQLNativePasswordAuthenticator.java b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/impl/MySQLNativePasswordAuthenticator.java
index 080e130b8b6..5267a45e0c6 100644
--- a/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/impl/MySQLNativePasswordAuthenticator.java
+++ b/proxy/frontend/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/impl/MySQLNativePasswordAuthenticator.java
@@ -19,6 +19,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.constant.AuthenticationMethod;
 import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLAuthenticationMethod;
 import org.apache.shardingsphere.db.protocol.mysql.packet.handshake.MySQLAuthenticationPluginData;
 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
@@ -59,7 +60,7 @@ public final class MySQLNativePasswordAuthenticator implements MySQLAuthenticato
     }
     
     @Override
-    public String getAuthenticationMethodName() {
-        return MySQLAuthenticationMethod.NATIVE.getMethodName();
+    public AuthenticationMethod getAuthenticationMethod() {
+        return MySQLAuthenticationMethod.NATIVE;
     }
 }
diff --git a/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/impl/MySQLClearPasswordAuthenticatorTest.java b/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/impl/MySQLClearPasswordAuthenticatorTest.java
index bbf96daf012..a4a4929721a 100644
--- a/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/impl/MySQLClearPasswordAuthenticatorTest.java
+++ b/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/authenticator/impl/MySQLClearPasswordAuthenticatorTest.java
@@ -21,18 +21,11 @@ import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
 import org.apache.shardingsphere.proxy.frontend.mysql.ProxyContextRestorer;
 import org.junit.Test;
 
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 public final class MySQLClearPasswordAuthenticatorTest extends ProxyContextRestorer {
     
-    @Test
-    public void assertAuthenticationMethodName() {
-        assertThat(new MySQLClearPasswordAuthenticator().getAuthenticationMethodName(), is("mysql_clear_password"));
-    }
-    
     @Test
     public void assertAuthenticate() {
         ShardingSphereUser user = new ShardingSphereUser("foo", "password", "%");
diff --git a/proxy/frontend/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/authenticator/impl/OpenGaussSCRAMSha256PasswordAuthenticator.java b/proxy/frontend/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/authenticator/impl/OpenGaussSCRAMSha256PasswordAuthenticator.java
index 024d9208f2c..4f7501a89a2 100644
--- a/proxy/frontend/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/authenticator/impl/OpenGaussSCRAMSha256PasswordAuthenticator.java
+++ b/proxy/frontend/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/authenticator/impl/OpenGaussSCRAMSha256PasswordAuthenticator.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.proxy.frontend.opengauss.authentication.authenticator.impl;
 
+import org.apache.shardingsphere.db.protocol.constant.AuthenticationMethod;
 import org.apache.shardingsphere.db.protocol.opengauss.packet.authentication.OpenGaussMacCalculator;
 import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLAuthenticationMethod;
 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
@@ -41,7 +42,7 @@ public final class OpenGaussSCRAMSha256PasswordAuthenticator implements OpenGaus
     }
     
     @Override
-    public String getAuthenticationMethodName() {
-        return PostgreSQLAuthenticationMethod.SCRAM_SHA256.getMethodName();
+    public AuthenticationMethod getAuthenticationMethod() {
+        return PostgreSQLAuthenticationMethod.SCRAM_SHA256;
     }
 }
diff --git a/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngine.java b/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngine.java
index 35bfcc2a71d..4184cb4e1d9 100644
--- a/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngine.java
+++ b/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngine.java
@@ -132,7 +132,7 @@ public final class PostgreSQLAuthenticationEngine implements AuthenticationEngin
     
     private PostgreSQLIdentifierPacket getIdentifierPacket(final String username, final AuthorityRule rule) {
         Optional<Authenticator> authenticator = rule.findUser(new Grantee(username, "")).map(optional -> new AuthenticatorFactory<>(PostgreSQLAuthenticatorType.class, rule).newInstance(optional));
-        if (authenticator.isPresent() && PostgreSQLAuthenticationMethod.PASSWORD.getMethodName().equals(authenticator.get().getAuthenticationMethodName())) {
+        if (authenticator.isPresent() && PostgreSQLAuthenticationMethod.PASSWORD == authenticator.get().getAuthenticationMethod()) {
             return new PostgreSQLPasswordAuthenticationPacket();
         }
         md5Salt = PostgreSQLRandomGenerator.getInstance().generateRandomBytes(4);
diff --git a/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/impl/PostgreSQLMD5PasswordAuthenticator.java b/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/impl/PostgreSQLMD5PasswordAuthenticator.java
index 353441aaa02..340249e3f1e 100644
--- a/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/impl/PostgreSQLMD5PasswordAuthenticator.java
+++ b/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/impl/PostgreSQLMD5PasswordAuthenticator.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.proxy.frontend.postgresql.authentication.authe
 
 import org.apache.commons.codec.binary.Hex;
 import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.shardingsphere.db.protocol.constant.AuthenticationMethod;
 import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLAuthenticationMethod;
 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
 import org.apache.shardingsphere.proxy.frontend.postgresql.authentication.authenticator.PostgreSQLAuthenticator;
@@ -47,7 +48,7 @@ public final class PostgreSQLMD5PasswordAuthenticator implements PostgreSQLAuthe
     }
     
     @Override
-    public String getAuthenticationMethodName() {
-        return PostgreSQLAuthenticationMethod.MD5.getMethodName();
+    public AuthenticationMethod getAuthenticationMethod() {
+        return PostgreSQLAuthenticationMethod.MD5;
     }
 }
diff --git a/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/impl/PostgreSQLPasswordAuthenticator.java b/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/impl/PostgreSQLPasswordAuthenticator.java
index 43834320222..95ec85ef32e 100644
--- a/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/impl/PostgreSQLPasswordAuthenticator.java
+++ b/proxy/frontend/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/impl/PostgreSQLPasswordAuthenticator.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.proxy.frontend.postgresql.authentication.authenticator.impl;
 
 import com.google.common.base.Strings;
+import org.apache.shardingsphere.db.protocol.constant.AuthenticationMethod;
 import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLAuthenticationMethod;
 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
 import org.apache.shardingsphere.proxy.frontend.postgresql.authentication.authenticator.PostgreSQLAuthenticator;
@@ -34,7 +35,7 @@ public final class PostgreSQLPasswordAuthenticator implements PostgreSQLAuthenti
     }
     
     @Override
-    public String getAuthenticationMethodName() {
-        return PostgreSQLAuthenticationMethod.PASSWORD.getMethodName();
+    public AuthenticationMethod getAuthenticationMethod() {
+        return PostgreSQLAuthenticationMethod.PASSWORD;
     }
 }
diff --git a/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/impl/PostgreSQLMD5PasswordAuthenticatorTest.java b/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/impl/PostgreSQLMD5PasswordAuthenticatorTest.java
index c7ac2ba4471..2e8f62da70c 100644
--- a/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/impl/PostgreSQLMD5PasswordAuthenticatorTest.java
+++ b/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/impl/PostgreSQLMD5PasswordAuthenticatorTest.java
@@ -18,14 +18,11 @@
 package org.apache.shardingsphere.proxy.frontend.postgresql.authentication.authenticator.impl;
 
 import lombok.SneakyThrows;
-import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLAuthenticationMethod;
 import org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.PostgreSQLRandomGenerator;
 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
 import org.junit.Test;
 import org.mockito.internal.configuration.plugins.Plugins;
 
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
@@ -37,11 +34,6 @@ public final class PostgreSQLMD5PasswordAuthenticatorTest {
     
     private final String password = "password";
     
-    @Test
-    public void assertGetAuthenticationMethodName() {
-        assertThat(authenticator.getAuthenticationMethodName(), is(PostgreSQLAuthenticationMethod.MD5.getMethodName()));
-    }
-    
     @Test
     public void assertAuthenticate() {
         ShardingSphereUser user = new ShardingSphereUser(username, password, "");
diff --git a/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/impl/PostgreSQLPasswordAuthenticatorTest.java b/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/impl/PostgreSQLPasswordAuthenticatorTest.java
index 140e64118a8..cba578906e1 100644
--- a/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/impl/PostgreSQLPasswordAuthenticatorTest.java
+++ b/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/impl/PostgreSQLPasswordAuthenticatorTest.java
@@ -17,32 +17,21 @@
 
 package org.apache.shardingsphere.proxy.frontend.postgresql.authentication.authenticator.impl;
 
-import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLAuthenticationMethod;
 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
 import org.junit.Test;
 
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 public final class PostgreSQLPasswordAuthenticatorTest {
     
-    private final PostgreSQLPasswordAuthenticator authenticator = new PostgreSQLPasswordAuthenticator();
-    
-    private final String username = "root";
-    
-    private final String password = "password";
-    
     @Test
-    public void assertGetAuthenticationMethodName() {
-        assertThat(authenticator.getAuthenticationMethodName(), is(PostgreSQLAuthenticationMethod.PASSWORD.getMethodName()));
+    public void assertAuthenticateSuccess() {
+        assertTrue(new PostgreSQLPasswordAuthenticator().authenticate(new ShardingSphereUser("root", "password", ""), new Object[]{"password", null}));
     }
     
     @Test
-    public void assertAuthenticate() {
-        ShardingSphereUser user = new ShardingSphereUser(username, password, "");
-        assertTrue(authenticator.authenticate(user, new Object[]{password, null}));
-        assertFalse(authenticator.authenticate(user, new Object[]{"wrong", null}));
+    public void assertAuthenticateFailed() {
+        assertFalse(new PostgreSQLPasswordAuthenticator().authenticate(new ShardingSphereUser("root", "password", ""), new Object[]{"wrong", null}));
     }
 }
diff --git a/proxy/frontend/spi/src/main/java/org/apache/shardingsphere/proxy/frontend/authentication/Authenticator.java b/proxy/frontend/spi/src/main/java/org/apache/shardingsphere/proxy/frontend/authentication/Authenticator.java
index 1fcb43923ed..c4923e6f445 100644
--- a/proxy/frontend/spi/src/main/java/org/apache/shardingsphere/proxy/frontend/authentication/Authenticator.java
+++ b/proxy/frontend/spi/src/main/java/org/apache/shardingsphere/proxy/frontend/authentication/Authenticator.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.proxy.frontend.authentication;
 
+import org.apache.shardingsphere.db.protocol.constant.AuthenticationMethod;
 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
 
 /**
@@ -34,9 +35,9 @@ public interface Authenticator {
     boolean authenticate(ShardingSphereUser user, Object[] authInfo);
     
     /**
-     * Get the name of authentication method.
+     * Get authentication method.
      *
-     * @return name of authentication method
+     * @return authentication method
      */
-    String getAuthenticationMethodName();
+    AuthenticationMethod getAuthenticationMethod();
 }