You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kyuubi.apache.org by ch...@apache.org on 2022/07/18 11:54:34 UTC

[incubator-kyuubi] branch master updated: [KYUUBI #3094] Code refactor on Kyuubi Hive JDBC driver

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

chengpan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-kyuubi.git


The following commit(s) were added to refs/heads/master by this push:
     new eb705bd1f [KYUUBI #3094] Code refactor on Kyuubi Hive JDBC driver
eb705bd1f is described below

commit eb705bd1fb909ece69d0b32c3f20d941dc9946b2
Author: Cheng Pan <ch...@apache.org>
AuthorDate: Mon Jul 18 19:54:26 2022 +0800

    [KYUUBI #3094] Code refactor on Kyuubi Hive JDBC driver
    
    ### _Why are the changes needed?_
    
    This PR is separated from #3023, to make #3023 change more clear.
    
    It is a pure code refactor, and should not break any functionality and public API.
    
    ### _How was this patch tested?_
    - [ ] Add some test cases that check the changes thoroughly including negative and positive cases if possible
    
    - [ ] Add screenshots for manual tests if appropriate
    
    - [x] [Run test](https://kyuubi.apache.org/docs/latest/develop_tools/testing.html#running-tests) locally before make a pull request
    
    Closes #3094 from pan3793/noisy.
    
    Closes #3094
    
    0cf2ac2a [Cheng Pan] Minor refactor
    
    Authored-by: Cheng Pan <ch...@apache.org>
    Signed-off-by: Cheng Pan <ch...@apache.org>
---
 .../apache/kyuubi/jdbc/hive/KyuubiConnection.java  | 83 +++++++++-------------
 .../java/org/apache/kyuubi/jdbc/hive/Utils.java    | 62 ++++------------
 .../hive/{ => auth}/HttpBasicAuthInterceptor.java  |  2 +-
 .../{ => auth}/HttpKerberosRequestInterceptor.java | 11 ++-
 .../{ => auth}/HttpRequestInterceptorBase.java     | 40 ++++++++++-
 .../hive/{ => auth}/HttpTokenAuthInterceptor.java  |  2 +-
 .../HttpXsrfRequestInterceptor.java}               |  4 +-
 .../HiveAuthUtils.java => auth/ThriftUtils.java}   |  4 +-
 8 files changed, 96 insertions(+), 112 deletions(-)

diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiConnection.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiConnection.java
index 765fa9309..0578055e0 100644
--- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiConnection.java
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/KyuubiConnection.java
@@ -47,8 +47,8 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.hive.service.rpc.thrift.*;
 import org.apache.http.HttpRequestInterceptor;
 import org.apache.http.HttpResponse;
+import org.apache.http.NoHttpResponseException;
 import org.apache.http.client.CookieStore;
-import org.apache.http.client.HttpRequestRetryHandler;
 import org.apache.http.client.ServiceUnavailableRetryStrategy;
 import org.apache.http.config.Registry;
 import org.apache.http.config.RegistryBuilder;
@@ -64,14 +64,11 @@ import org.apache.http.protocol.HttpContext;
 import org.apache.http.ssl.SSLContexts;
 import org.apache.kyuubi.jdbc.hive.Utils.JdbcConnectionParams;
 import org.apache.kyuubi.jdbc.hive.adapter.SQLConnection;
-import org.apache.kyuubi.jdbc.hive.auth.KerberosSaslHelper;
-import org.apache.kyuubi.jdbc.hive.auth.PlainSaslHelper;
-import org.apache.kyuubi.jdbc.hive.auth.SaslQOP;
+import org.apache.kyuubi.jdbc.hive.auth.*;
 import org.apache.kyuubi.jdbc.hive.cli.FetchType;
 import org.apache.kyuubi.jdbc.hive.cli.RowSet;
 import org.apache.kyuubi.jdbc.hive.cli.RowSetFactory;
 import org.apache.kyuubi.jdbc.hive.cli.SessionUtils;
-import org.apache.kyuubi.jdbc.hive.common.HiveAuthUtils;
 import org.apache.kyuubi.jdbc.hive.logs.KyuubiLoggable;
 import org.apache.thrift.TException;
 import org.apache.thrift.protocol.TBinaryProtocol;
@@ -225,7 +222,7 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
         }
 
         if (numRetries >= maxRetries) {
-          throw new KyuubiSQLException(errMsg + e.getMessage(), " 08S01", e);
+          throw new KyuubiSQLException(errMsg + e.getMessage(), "08S01", e);
         } else {
           LOG.warn(warnMsg + e.getMessage() + " Retrying " + numRetries + " of " + maxRetries);
         }
@@ -425,13 +422,8 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
   }
 
   private CloseableHttpClient getHttpClient(Boolean useSsl) throws SQLException {
-    boolean isCookieEnabled =
-        sessConfMap.get(COOKIE_AUTH) == null
-            || (!COOKIE_AUTH_FALSE.equalsIgnoreCase(sessConfMap.get(COOKIE_AUTH)));
-    String cookieName =
-        sessConfMap.get(COOKIE_NAME) == null
-            ? DEFAULT_COOKIE_NAMES_HS2
-            : sessConfMap.get(COOKIE_NAME);
+    boolean isCookieEnabled = isCookieEnabled();
+    String cookieName = sessConfMap.getOrDefault(COOKIE_NAME, DEFAULT_COOKIE_NAMES_HS2);
     CookieStore cookieStore = isCookieEnabled ? new BasicCookieStore() : null;
     HttpClientBuilder httpClientBuilder;
     // Request interceptor for any request pre-processing logic
@@ -535,27 +527,23 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
     // In case the server's idletimeout is set to a lower value, it might close it's side of
     // connection. However we retry one more time on NoHttpResponseException
     httpClientBuilder.setRetryHandler(
-        new HttpRequestRetryHandler() {
-          @Override
-          public boolean retryRequest(
-              IOException exception, int executionCount, HttpContext context) {
-            if (executionCount > 1) {
-              LOG.info("Retry attempts to connect to server exceeded.");
-              return false;
-            }
-            if (exception instanceof org.apache.http.NoHttpResponseException) {
-              LOG.info("Could not connect to the server. Retrying one more time.");
-              return true;
-            }
+        (exception, executionCount, context) -> {
+          if (executionCount > 1) {
+            LOG.info("Retry attempts to connect to server exceeded.");
             return false;
           }
+          if (exception instanceof NoHttpResponseException) {
+            LOG.info("Could not connect to the server. Retrying one more time.");
+            return true;
+          }
+          return false;
         });
 
     // Add the request interceptor to the client builder
     httpClientBuilder.addInterceptorFirst(requestInterceptor);
 
     // Add an interceptor to add in an XSRF header
-    httpClientBuilder.addInterceptorLast(new XsrfHttpRequestInterceptor());
+    httpClientBuilder.addInterceptorLast(new HttpXsrfRequestInterceptor());
 
     // Configure http client for SSL
     if (useSsl) {
@@ -595,7 +583,7 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
       } catch (Exception e) {
         String msg =
             "Could not create an https connection to " + jdbcUriString + ". " + e.getMessage();
-        throw new KyuubiSQLException(msg, " 08S01", e);
+        throw new KyuubiSQLException(msg, "08S01", e);
       }
     }
     return httpClientBuilder.build();
@@ -615,15 +603,15 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
       String sslTrustStorePassword = sessConfMap.get(SSL_TRUST_STORE_PASSWORD);
 
       if (sslTrustStore == null || sslTrustStore.isEmpty()) {
-        transport = HiveAuthUtils.getSSLSocket(host, port, loginTimeout);
+        transport = ThriftUtils.getSSLSocket(host, port, loginTimeout);
       } else {
         transport =
-            HiveAuthUtils.getSSLSocket(
+            ThriftUtils.getSSLSocket(
                 host, port, loginTimeout, sslTrustStore, sslTrustStorePassword);
       }
     } else {
       // get non-SSL socket transport
-      transport = HiveAuthUtils.getSocketTransport(host, port, loginTimeout);
+      transport = ThriftUtils.getSocketTransport(host, port, loginTimeout);
     }
     return transport;
   }
@@ -682,7 +670,7 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
     } catch (SaslException e) {
       throw new KyuubiSQLException(
           "Could not create secure connection to " + jdbcUriString + ": " + e.getMessage(),
-          " 08S01",
+          "08S01",
           e);
     }
     return transport;
@@ -830,9 +818,7 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
     } catch (TException e) {
       LOG.error("Error opening session", e);
       throw new KyuubiSQLException(
-          "Could not establish connection to " + jdbcUriString + ": " + e.getMessage(),
-          " 08S01",
-          e);
+          "Could not establish connection to " + jdbcUriString + ": " + e.getMessage(), "08S01", e);
     }
     isClosed = false;
   }
@@ -847,6 +833,10 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
     return getSessionValue(AUTH_PASSWD, ANONYMOUS_PASSWD);
   }
 
+  private boolean isCookieEnabled() {
+    return !"false".equalsIgnoreCase(sessConfMap.get(COOKIE_AUTH));
+  }
+
   private boolean isSslConnection() {
     return "true".equalsIgnoreCase(sessConfMap.get(USE_SSL));
   }
@@ -857,8 +847,7 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
   }
 
   private boolean isHttpTransportMode() {
-    String transportMode = sessConfMap.get(TRANSPORT_MODE);
-    return transportMode != null && (transportMode.equalsIgnoreCase("http"));
+    return "http".equalsIgnoreCase(sessConfMap.get(TRANSPORT_MODE));
   }
 
   private void logZkDiscoveryMessage(String message) {
@@ -870,7 +859,7 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
   /** Lookup varName in sessConfMap, if its null or empty return the default value varDefault */
   private String getSessionValue(String varName, String varDefault) {
     String varValue = sessConfMap.get(varName);
-    if ((varValue == null) || varValue.isEmpty()) {
+    if (varValue == null || varValue.isEmpty()) {
       varValue = varDefault;
     }
     return varValue;
@@ -896,7 +885,7 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
       Utils.verifySuccess(tokenResp.getStatus());
       return tokenResp.getDelegationToken();
     } catch (TException e) {
-      throw new KyuubiSQLException("Could not retrieve token: " + e.getMessage(), " 08S01", e);
+      throw new KyuubiSQLException("Could not retrieve token: " + e.getMessage(), "08S01", e);
     }
   }
 
@@ -909,7 +898,7 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
       TCancelDelegationTokenResp cancelResp = client.CancelDelegationToken(cancelReq);
       Utils.verifySuccess(cancelResp.getStatus());
     } catch (TException e) {
-      throw new KyuubiSQLException("Could not cancel token: " + e.getMessage(), " 08S01", e);
+      throw new KyuubiSQLException("Could not cancel token: " + e.getMessage(), "08S01", e);
     }
   }
 
@@ -922,7 +911,7 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
       TRenewDelegationTokenResp renewResp = client.RenewDelegationToken(cancelReq);
       Utils.verifySuccess(renewResp.getStatus());
     } catch (TException e) {
-      throw new KyuubiSQLException("Could not renew token: " + e.getMessage(), " 08S01", e);
+      throw new KyuubiSQLException("Could not renew token: " + e.getMessage(), "08S01", e);
     }
   }
 
@@ -955,7 +944,7 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
       engineLogThread.interrupt();
       try {
         engineLogThread.join(DEFAULT_ENGINE_LOG_THREAD_TIMEOUT);
-      } catch (Exception e) {
+      } catch (Exception ignore) {
       }
     }
     engineLogThread = null;
@@ -1016,7 +1005,7 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
         throw new KyuubiSQLException("Failed to get catalog information");
       }
       return res.getString(1);
-    } catch (Exception e) {
+    } catch (Exception ignore) {
       return "";
     }
   }
@@ -1084,11 +1073,9 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
     }
     boolean rc = false;
     try {
-      String productName =
-          new KyuubiDatabaseMetaData(this, client, sessHandle).getDatabaseProductName();
+      new KyuubiDatabaseMetaData(this, client, sessHandle).getDatabaseProductName();
       rc = true;
-    } catch (SQLException e) {
-      // IGNORE
+    } catch (SQLException ignore) {
     }
     return rc;
   }
@@ -1141,7 +1128,7 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
     }
     try (KyuubiStatement stmt = createKyuubiStatement()) {
       stmt.executeSetCurrentCatalog("_SET_CATALOG", catalog);
-    } catch (SQLException e) {
+    } catch (SQLException ignore) {
 
     }
   }
@@ -1259,7 +1246,7 @@ public class KyuubiConnection implements SQLConnection, KyuubiLoggable {
       } catch (InvocationTargetException e) {
         // all IFace APIs throw TException
         if (e.getTargetException() instanceof TException) {
-          throw (TException) e.getTargetException();
+          throw e.getTargetException();
         } else {
           // should not happen
           throw new TException(
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/Utils.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/Utils.java
index 20495a4b1..5319452f0 100644
--- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/Utils.java
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/Utils.java
@@ -26,13 +26,11 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import org.apache.hive.service.rpc.thrift.TStatus;
 import org.apache.hive.service.rpc.thrift.TStatusCode;
-import org.apache.http.client.CookieStore;
-import org.apache.http.cookie.Cookie;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class Utils {
-  static final Logger LOG = LoggerFactory.getLogger(Utils.class.getName());
+  static final Logger LOG = LoggerFactory.getLogger(Utils.class);
   /** The required prefix list for the connection URL. */
   public static final List<String> URL_PREFIX_LIST =
       Arrays.asList("jdbc:hive2://", "jdbc:kyuubi://");
@@ -52,9 +50,9 @@ public class Utils {
 
   // This value is set to true by the setServiceUnavailableRetryStrategy() when the server returns
   // 401
-  static final String HIVE_SERVER2_RETRY_KEY = "hive.server2.retryserver";
-  static final String HIVE_SERVER2_RETRY_TRUE = "true";
-  static final String HIVE_SERVER2_RETRY_FALSE = "false";
+  public static final String HIVE_SERVER2_RETRY_KEY = "hive.server2.retryserver";
+  public static final String HIVE_SERVER2_RETRY_TRUE = "true";
+  public static final String HIVE_SERVER2_RETRY_FALSE = "false";
 
   static String getMatchedUrlPrefix(String uri) throws JdbcUriParseException {
     for (String urlPrefix : URL_PREFIX_LIST) {
@@ -115,7 +113,6 @@ public class Utils {
     // This value is used if the param "zooKeeperNamespace" is not specified in the JDBC Uri.
     static final String ZOOKEEPER_DEFAULT_NAMESPACE = "hiveserver2";
     static final String COOKIE_AUTH = "cookieAuth";
-    static final String COOKIE_AUTH_FALSE = "false";
     static final String COOKIE_NAME = "cookieName";
     // The default value of the cookie name when CookieAuth=true
     static final String DEFAULT_COOKIE_NAMES_HS2 = "hive.server2.auth";
@@ -157,13 +154,13 @@ public class Utils {
     private int port = 0;
     private String jdbcUriString;
     private String dbName = DEFAULT_DATABASE;
-    private Map<String, String> hiveConfs = new LinkedHashMap<String, String>();
-    private Map<String, String> hiveVars = new LinkedHashMap<String, String>();
-    private Map<String, String> sessionVars = new LinkedHashMap<String, String>();
+    private Map<String, String> hiveConfs = new LinkedHashMap<>();
+    private Map<String, String> hiveVars = new LinkedHashMap<>();
+    private Map<String, String> sessionVars = new LinkedHashMap<>();
     private String suppliedURLAuthority;
     private String zooKeeperEnsemble = null;
     private String currentHostZnodePath;
-    private final List<String> rejectedHostZnodePaths = new ArrayList<String>();
+    private final List<String> rejectedHostZnodePaths = new ArrayList<>();
 
     public JdbcConnectionParams() {}
 
@@ -561,7 +558,7 @@ public class Utils {
    */
   private static String getAuthorityFromJdbcURL(String uri) throws JdbcUriParseException {
     String authorities;
-    /**
+    /*
      * For a jdbc uri like:
      * jdbc:hive2://<host1>:<port1>,<host2>:<port2>/dbName;sess_var_list?conf_list#var_list Extract
      * the uri host:port list starting after "jdbc:hive2://", till the 1st "/" or "?" or "#"
@@ -574,7 +571,7 @@ public class Utils {
     String matchedUrlPrefix = getMatchedUrlPrefix(uri);
     int fromIndex = matchedUrlPrefix.length();
     int toIndex = -1;
-    ArrayList<String> toIndexChars = new ArrayList<String>(Arrays.asList("/", "?", "#"));
+    ArrayList<String> toIndexChars = new ArrayList<>(Arrays.asList("/", "?", "#"));
     for (String toIndexChar : toIndexChars) {
       toIndex = uri.indexOf(toIndexChar, fromIndex);
       if (toIndex > 0) {
@@ -630,49 +627,16 @@ public class Utils {
   static int getVersionPart(String fullVersion, int position) {
     int version = -1;
     try {
-      String[] tokens = fullVersion.split("[\\.-]"); // $NON-NLS-1$
+      String[] tokens = fullVersion.split("[.-]"); // $NON-NLS-1$
 
-      if (tokens != null && tokens.length > 1 && tokens[position] != null) {
+      if (tokens.length > 1 && tokens[position] != null) {
         version = Integer.parseInt(tokens[position]);
       }
-    } catch (Exception e) {
-      version = -1;
+    } catch (Exception ignore) {
     }
     return version;
   }
 
-  /**
-   * The function iterates through the list of cookies in the cookiestore and tries to match them
-   * with the cookieName. If there is a match, the cookieStore already has a valid cookie and the
-   * client need not send Credentials for validation purpose.
-   *
-   * @param cookieStore The cookie Store
-   * @param cookieName Name of the cookie which needs to be validated
-   * @param isSSL Whether this is a http/https connection
-   * @return true or false based on whether the client needs to send the credentials or not to the
-   *     server.
-   */
-  static boolean needToSendCredentials(CookieStore cookieStore, String cookieName, boolean isSSL) {
-    if (cookieName == null || cookieStore == null) {
-      return true;
-    }
-
-    List<Cookie> cookies = cookieStore.getCookies();
-
-    for (Cookie c : cookies) {
-      // If this is a secured cookie and the current connection is non-secured,
-      // then, skip this cookie. We need to skip this cookie because, the cookie
-      // replay will not be transmitted to the server.
-      if (c.isSecure() && !isSSL) {
-        continue;
-      }
-      if (c.getName().equals(cookieName)) {
-        return false;
-      }
-    }
-    return true;
-  }
-
   public static String parsePropertyFromUrl(final String url, final String key) {
     String[] tokens = url.split(";");
     for (String token : tokens) {
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/HttpBasicAuthInterceptor.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpBasicAuthInterceptor.java
similarity index 98%
rename from kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/HttpBasicAuthInterceptor.java
rename to kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpBasicAuthInterceptor.java
index 97782c06c..4a5e92aa5 100644
--- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/HttpBasicAuthInterceptor.java
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpBasicAuthInterceptor.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.kyuubi.jdbc.hive;
+package org.apache.kyuubi.jdbc.hive.auth;
 
 import java.util.Map;
 import org.apache.http.Header;
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/HttpKerberosRequestInterceptor.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpKerberosRequestInterceptor.java
similarity index 90%
rename from kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/HttpKerberosRequestInterceptor.java
rename to kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpKerberosRequestInterceptor.java
index ce36cf37d..278cef0b4 100644
--- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/HttpKerberosRequestInterceptor.java
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpKerberosRequestInterceptor.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.kyuubi.jdbc.hive;
+package org.apache.kyuubi.jdbc.hive.auth;
 
 import java.util.Map;
 import java.util.concurrent.locks.ReentrantLock;
@@ -24,7 +24,6 @@ import org.apache.http.HttpException;
 import org.apache.http.HttpRequest;
 import org.apache.http.client.CookieStore;
 import org.apache.http.protocol.HttpContext;
-import org.apache.kyuubi.jdbc.hive.auth.HttpAuthUtils;
 
 /**
  * Authentication interceptor which adds Base64 encoded payload, containing the username and
@@ -34,12 +33,12 @@ public class HttpKerberosRequestInterceptor extends HttpRequestInterceptorBase {
 
   private static final ReentrantLock kerberosLock = new ReentrantLock(true);
 
-  String principal;
+  String serverPrincipal;
   String host;
   Subject loggedInSubject;
 
   public HttpKerberosRequestInterceptor(
-      String principal,
+      String serverPrincipal,
       String host,
       Subject loggedInSubject,
       CookieStore cs,
@@ -48,7 +47,7 @@ public class HttpKerberosRequestInterceptor extends HttpRequestInterceptorBase {
       Map<String, String> additionalHeaders,
       Map<String, String> customCookies) {
     super(cs, cn, isSSL, additionalHeaders, customCookies);
-    this.principal = principal;
+    this.serverPrincipal = serverPrincipal;
     this.host = host;
     this.loggedInSubject = loggedInSubject;
   }
@@ -61,7 +60,7 @@ public class HttpKerberosRequestInterceptor extends HttpRequestInterceptorBase {
       // Locking ensures the tokens are unique in case of concurrent requests
       kerberosLock.lock();
       String kerberosAuthHeader =
-          HttpAuthUtils.getKerberosServiceTicket(principal, host, loggedInSubject);
+          HttpAuthUtils.getKerberosServiceTicket(serverPrincipal, host, loggedInSubject);
       // Set the session key token (Base64 encoded) in the headers
       httpRequest.addHeader(
           HttpAuthUtils.AUTHORIZATION, HttpAuthUtils.NEGOTIATE + " " + kerberosAuthHeader);
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/HttpRequestInterceptorBase.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpRequestInterceptorBase.java
similarity index 76%
rename from kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/HttpRequestInterceptorBase.java
rename to kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpRequestInterceptorBase.java
index b3a035025..9ce5a330b 100644
--- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/HttpRequestInterceptorBase.java
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpRequestInterceptorBase.java
@@ -15,9 +15,10 @@
  * limitations under the License.
  */
 
-package org.apache.kyuubi.jdbc.hive;
+package org.apache.kyuubi.jdbc.hive.auth;
 
 import java.io.IOException;
+import java.util.List;
 import java.util.Map;
 import org.apache.http.Header;
 import org.apache.http.HttpException;
@@ -25,7 +26,9 @@ import org.apache.http.HttpRequest;
 import org.apache.http.HttpRequestInterceptor;
 import org.apache.http.client.CookieStore;
 import org.apache.http.client.protocol.HttpClientContext;
+import org.apache.http.cookie.Cookie;
 import org.apache.http.protocol.HttpContext;
+import org.apache.kyuubi.jdbc.hive.Utils;
 
 public abstract class HttpRequestInterceptorBase implements HttpRequestInterceptor {
   CookieStore cookieStore;
@@ -71,8 +74,7 @@ public abstract class HttpRequestInterceptorBase implements HttpRequestIntercept
       // 4. The cookie is secured where as the client connect does not use SSL
       if (!isCookieEnabled
           || ((httpContext.getAttribute(Utils.HIVE_SERVER2_RETRY_KEY) == null
-                  && (cookieStore == null
-                      || Utils.needToSendCredentials(cookieStore, cookieName, isSSL)))
+                  && (cookieStore == null || needToSendCredentials(cookieStore, cookieName, isSSL)))
               || (httpContext.getAttribute(Utils.HIVE_SERVER2_RETRY_KEY) != null
                   && httpContext
                       .getAttribute(Utils.HIVE_SERVER2_RETRY_KEY)
@@ -111,4 +113,36 @@ public abstract class HttpRequestInterceptorBase implements HttpRequestIntercept
       throw new HttpException(e.getMessage(), e);
     }
   }
+
+  /**
+   * The function iterates through the list of cookies in the cookiestore and tries to match them
+   * with the cookieName. If there is a match, the cookieStore already has a valid cookie and the
+   * client need not send Credentials for validation purpose.
+   *
+   * @param cookieStore The cookie Store
+   * @param cookieName Name of the cookie which needs to be validated
+   * @param isSSL Whether this is a http/https connection
+   * @return true or false based on whether the client needs to send the credentials or not to the
+   *     server.
+   */
+  private boolean needToSendCredentials(CookieStore cookieStore, String cookieName, boolean isSSL) {
+    if (cookieName == null || cookieStore == null) {
+      return true;
+    }
+
+    List<Cookie> cookies = cookieStore.getCookies();
+
+    for (Cookie c : cookies) {
+      // If this is a secured cookie and the current connection is non-secured,
+      // then, skip this cookie. We need to skip this cookie because, the cookie
+      // replay will not be transmitted to the server.
+      if (c.isSecure() && !isSSL) {
+        continue;
+      }
+      if (c.getName().equals(cookieName)) {
+        return false;
+      }
+    }
+    return true;
+  }
 }
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/HttpTokenAuthInterceptor.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpTokenAuthInterceptor.java
similarity index 97%
rename from kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/HttpTokenAuthInterceptor.java
rename to kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpTokenAuthInterceptor.java
index 2e6a45007..cbe587e30 100644
--- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/HttpTokenAuthInterceptor.java
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpTokenAuthInterceptor.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.kyuubi.jdbc.hive;
+package org.apache.kyuubi.jdbc.hive.auth;
 
 import java.util.Map;
 import org.apache.http.HttpRequest;
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/XsrfHttpRequestInterceptor.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpXsrfRequestInterceptor.java
similarity index 94%
rename from kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/XsrfHttpRequestInterceptor.java
rename to kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpXsrfRequestInterceptor.java
index ee20069bd..0d413972a 100644
--- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/XsrfHttpRequestInterceptor.java
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/HttpXsrfRequestInterceptor.java
@@ -15,13 +15,13 @@
  * limitations under the License.
  */
 
-package org.apache.kyuubi.jdbc.hive;
+package org.apache.kyuubi.jdbc.hive.auth;
 
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpRequestInterceptor;
 import org.apache.http.protocol.HttpContext;
 
-public class XsrfHttpRequestInterceptor implements HttpRequestInterceptor {
+public class HttpXsrfRequestInterceptor implements HttpRequestInterceptor {
 
   // Note : This implements HttpRequestInterceptor rather than extending
   // HttpRequestInterceptorBase, because that class is an auth-specific
diff --git a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/common/HiveAuthUtils.java b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/ThriftUtils.java
similarity index 97%
rename from kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/common/HiveAuthUtils.java
rename to kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/ThriftUtils.java
index df166cb6e..5177d2825 100644
--- a/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/common/HiveAuthUtils.java
+++ b/kyuubi-hive-jdbc/src/main/java/org/apache/kyuubi/jdbc/hive/auth/ThriftUtils.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.kyuubi.jdbc.hive.common;
+package org.apache.kyuubi.jdbc.hive.auth;
 
 import javax.net.ssl.SSLParameters;
 import javax.net.ssl.SSLSocket;
@@ -28,7 +28,7 @@ import org.apache.thrift.transport.TTransportException;
  * This class helps in some aspects of authentication. It creates the proper Thrift classes for the
  * given configuration as well as helps with authenticating requests.
  */
-public class HiveAuthUtils {
+public class ThriftUtils {
   public static TTransport getSocketTransport(String host, int port, int loginTimeout) {
     return new TSocket(host, port, loginTimeout);
   }