You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/11/14 09:29:11 UTC

[shardingsphere] branch master updated: Refactor MySQLAuthenticationEngine (#22164)

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 f3cc852dcbb Refactor MySQLAuthenticationEngine (#22164)
f3cc852dcbb is described below

commit f3cc852dcbba89244ffcaef15a03ebf29693a14d
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Mon Nov 14 17:29:02 2022 +0800

    Refactor MySQLAuthenticationEngine (#22164)
    
    * Refactor MySQLAuthenticationEngine
    
    * Complete tests
---
 .../mysql/authentication/MySQLAuthenticationEngine.java     |  8 +++++++-
 .../proxy/frontend/mysql/MySQLFrontendEngineTest.java       |  9 +++++----
 .../mysql/authentication/MySQLAuthenticationEngineTest.java | 13 +++++++++++++
 3 files changed, 25 insertions(+), 5 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 524786322d6..c28d5ce332c 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
@@ -83,7 +83,12 @@ public final class MySQLAuthenticationEngine implements AuthenticationEngine {
             authenticationMethodMismatch((MySQLPacketPayload) payload);
         }
         Optional<MySQLVendorError> vendorError = authenticationHandler.login(currentAuthResult.getUsername(), getHostAddress(context), authResponse, currentAuthResult.getDatabase());
-        context.writeAndFlush(vendorError.isPresent() ? createErrorPacket(vendorError.get(), context) : new MySQLOKPacket(++sequenceId, DEFAULT_STATUS_FLAG));
+        if (vendorError.isPresent()) {
+            context.writeAndFlush(createErrorPacket(vendorError.get(), context));
+            context.close();
+            return AuthenticationResultBuilder.continued();
+        }
+        context.writeAndFlush(new MySQLOKPacket(++sequenceId, DEFAULT_STATUS_FLAG));
         return AuthenticationResultBuilder.finished(currentAuthResult.getUsername(), getHostAddress(context), currentAuthResult.getDatabase());
     }
     
@@ -96,6 +101,7 @@ public final class MySQLAuthenticationEngine implements AuthenticationEngine {
         context.channel().attr(MySQLConstants.MYSQL_CHARACTER_SET_ATTRIBUTE_KEY).set(characterSet);
         if (!Strings.isNullOrEmpty(packet.getDatabase()) && !ProxyContext.getInstance().databaseExists(packet.getDatabase())) {
             context.writeAndFlush(new MySQLErrPacket(++sequenceId, MySQLVendorError.ER_BAD_DB_ERROR, packet.getDatabase()));
+            context.close();
             return AuthenticationResultBuilder.continued();
         }
         MySQLAuthenticator authenticator = authenticationHandler.getAuthenticator(packet.getUsername(), getHostAddress(context));
diff --git a/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngineTest.java b/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngineTest.java
index 391bea9c8e5..13ee0cdd1e0 100644
--- a/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngineTest.java
+++ b/proxy/frontend/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLFrontendEngineTest.java
@@ -64,6 +64,7 @@ import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.argThat;
@@ -137,9 +138,9 @@ public final class MySQLFrontendEngineTest extends ProxyContextRestorer {
         when(payload.readStringNulByBytes()).thenReturn("root".getBytes());
         when(channel.remoteAddress()).thenReturn(new InetSocketAddress("localhost", 3307));
         AuthenticationResult actual = mysqlFrontendEngine.getAuthenticationEngine().authenticate(context, payload);
-        assertThat(actual.getUsername(), is("root"));
+        assertNull(actual.getUsername());
         assertNull(actual.getDatabase());
-        assertTrue(actual.isFinished());
+        assertFalse(actual.isFinished());
         verify(context).writeAndFlush(isA(MySQLErrPacket.class));
     }
     
@@ -152,9 +153,9 @@ public final class MySQLFrontendEngineTest extends ProxyContextRestorer {
         when(payload.readStringNulByBytes()).thenReturn("root".getBytes());
         when(channel.remoteAddress()).thenReturn(new InetSocketAddress(InetAddress.getByAddress(new byte[]{(byte) 192, (byte) 168, (byte) 0, (byte) 102}), 3307));
         AuthenticationResult actual = mysqlFrontendEngine.getAuthenticationEngine().authenticate(context, payload);
-        assertThat(actual.getUsername(), is("root"));
+        assertNull(actual.getUsername());
         assertNull(actual.getDatabase());
-        assertTrue(actual.isFinished());
+        assertFalse(actual.isFinished());
         verify(context).writeAndFlush(argThat((ArgumentMatcher<MySQLErrPacket>) argument -> "Access denied for user 'root'@'192.168.0.102' (using password: YES)".equals(argument.getErrorMessage())));
     }
     
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 00380ef4907..9166158d52b 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
@@ -138,6 +138,18 @@ public final class MySQLAuthenticationEngineTest extends ProxyContextRestorer {
         when(authenticationHandler.login(anyString(), any(), any(), anyString())).thenReturn(Optional.of(MySQLVendorError.ER_ACCESS_DENIED_ERROR));
         authenticationEngine.authenticate(context, getPayload("root", "sharding_db", authResponse));
         verify(context).writeAndFlush(any(MySQLErrPacket.class));
+        verify(context).close();
+    }
+    
+    @Test
+    public void assertAuthWithDatabaseAccessDenied() {
+        setConnectionPhase(MySQLConnectionPhase.AUTH_PHASE_FAST_PATH);
+        ChannelHandlerContext context = getContext();
+        setMetaDataContexts();
+        when(authenticationHandler.login(anyString(), any(), any(), anyString())).thenReturn(Optional.of(MySQLVendorError.ER_DBACCESS_DENIED_ERROR));
+        authenticationEngine.authenticate(context, getPayload("root", "sharding_db", authResponse));
+        verify(context).writeAndFlush(any(MySQLErrPacket.class));
+        verify(context).close();
     }
     
     @Test
@@ -147,6 +159,7 @@ public final class MySQLAuthenticationEngineTest extends ProxyContextRestorer {
         setConnectionPhase(MySQLConnectionPhase.AUTH_PHASE_FAST_PATH);
         authenticationEngine.authenticate(context, getPayload("root", "ABSENT DATABASE", authResponse));
         verify(context).writeAndFlush(any(MySQLErrPacket.class));
+        verify(context).close();
     }
     
     @Test