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

dengzhhu653 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/master by this push:
     new e5d559dce04 HIVE-23157: Mutual TLS authentication for Metastore (#6914)
e5d559dce04 is described below

commit e5d559dce04a0f4d99fa8e7398abdc5ce3607bae
Author: Venu Reddy <[email protected]>
AuthorDate: Fri Nov 13 07:58:18 2026 +0530

    HIVE-23157: Mutual TLS authentication for Metastore (#6914)
---
 .../java/org/apache/hadoop/hive/conf/HiveConf.java |   4 +-
 .../client/ThriftHiveMetaStoreClient.java          |  25 +--
 .../hadoop/hive/metastore/conf/MetastoreConf.java  | 126 +++++++++++++
 .../hadoop/hive/metastore/utils/SecurityUtils.java | 113 ++++++++----
 .../hadoop/hive/metastore/HiveMetaStore.java       |  23 +--
 .../hadoop/hive/metastore/ServletSecurity.java     |  42 +++--
 .../hive/metastore/ServletServerBuilder.java       |   6 +-
 .../hive/metastore/TestMetastoreMutualTls.java     | 202 +++++++++++++++++++++
 .../src/test/resources/mtls_client_keystore.p12    | Bin 0 -> 2632 bytes
 .../src/test/resources/mtls_client_truststore.p12  | Bin 0 -> 1190 bytes
 .../src/test/resources/mtls_server_keystore.p12    | Bin 0 -> 2648 bytes
 .../src/test/resources/mtls_server_truststore.p12  | Bin 0 -> 1158 bytes
 .../resources/mtls_untrusted_client_keystore.p12   | Bin 0 -> 2654 bytes
 .../hadoop/hive/metastore/tools/HMSClient.java     |  13 +-
 14 files changed, 447 insertions(+), 107 deletions(-)

diff --git a/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java 
b/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java
index c9132737542..a9821e645e6 100644
--- a/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java
+++ b/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java
@@ -5631,7 +5631,9 @@ public static enum ConfVars {
         + ",hive.zookeeper.ssl.keystore.location"
         + ",hive.zookeeper.ssl.keystore.password"
         + ",hive.zookeeper.ssl.truststore.location"
-        + ",hive.zookeeper.ssl.truststore.password",
+        + ",hive.zookeeper.ssl.truststore.password"
+        + ",hive.metastore.server.truststore.password"
+        + ",hive.metastore.client.keystore.password",
         "Comma separated list of configuration options which should not be 
read by normal user like passwords"),
     HIVE_CONF_INTERNAL_VARIABLE_LIST("hive.conf.internal.variable.list",
         "hive.added.files.path,hive.added.jars.path,hive.added.archives.path",
diff --git 
a/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/client/ThriftHiveMetaStoreClient.java
 
b/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/client/ThriftHiveMetaStoreClient.java
index ea7a1a6f981..9a229799df2 100644
--- 
a/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/client/ThriftHiveMetaStoreClient.java
+++ 
b/standalone-metastore/metastore-client/src/main/java/org/apache/hadoop/hive/metastore/client/ThriftHiveMetaStoreClient.java
@@ -585,18 +585,10 @@ private TTransport createHttpClient(URI store, boolean 
useSSL) throws MetaExcept
     THttpClient tHttpClient;
     try {
       if (useSSL) {
-        String trustStorePath = MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_TRUSTSTORE_PATH).trim();
-        if (trustStorePath.isEmpty()) {
-          throw new 
IllegalArgumentException(MetastoreConf.ConfVars.SSL_TRUSTSTORE_PATH + " Not 
configured for SSL connection");
-        }
-        String trustStorePassword = MetastoreConf.getPassword(conf, 
MetastoreConf.ConfVars.SSL_TRUSTSTORE_PASSWORD);
-        String trustStoreType = MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_TRUSTSTORE_TYPE).trim();
-        String trustStoreAlgorithm = MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_TRUSTMANAGERFACTORY_ALGORITHM).trim();
         String includeProtocols = MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_INCLUDE_PROTOCOLS);
         String includeCipherSuites = MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_INCLUDE_CIPHERSUITES);
-        tHttpClient =
-            SecurityUtils.getThriftHttpsClient(httpUrl, trustStorePath, 
trustStorePassword, trustStoreAlgorithm,
-                trustStoreType, includeProtocols, includeCipherSuites, 
httpClientBuilder);
+        tHttpClient = SecurityUtils.getThriftHttpsClient(httpUrl, 
MetastoreConf.getSslClientKeyStore(conf),
+            MetastoreConf.getSslClientTrustStore(conf), includeProtocols, 
includeCipherSuites, httpClientBuilder);
       } else {
         tHttpClient = new THttpClient(httpUrl, httpClientBuilder.build());
       }
@@ -667,21 +659,10 @@ private TTransport createBinaryClient(URI store, boolean 
useSSL) throws TTranspo
       int connectionTimeout = (int) MetastoreConf.getTimeVar(conf,
           MetastoreConf.ConfVars.CLIENT_CONNECTION_TIMEOUT, 
TimeUnit.MILLISECONDS);
       if (useSSL) {
-        String trustStorePath = MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_TRUSTSTORE_PATH).trim();
-        if (trustStorePath.isEmpty()) {
-          throw new 
IllegalArgumentException(MetastoreConf.ConfVars.SSL_TRUSTSTORE_PATH
-              + " Not configured for SSL connection");
-        }
-        String trustStorePassword =
-            MetastoreConf.getPassword(conf, 
MetastoreConf.ConfVars.SSL_TRUSTSTORE_PASSWORD);
-        String trustStoreType =
-            MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_TRUSTSTORE_TYPE).trim();
-        String trustStoreAlgorithm =
-            MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_TRUSTMANAGERFACTORY_ALGORITHM).trim();
         String includeProtocols = MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_INCLUDE_PROTOCOLS);
         String includeCipherSuites = MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_INCLUDE_CIPHERSUITES);
         binaryTransport = SecurityUtils.getSSLSocket(store.getHost(), 
store.getPort(), clientSocketTimeout,
-            connectionTimeout, trustStorePath, trustStorePassword, 
trustStoreType, trustStoreAlgorithm,
+            connectionTimeout, MetastoreConf.getSslClientKeyStore(conf), 
MetastoreConf.getSslClientTrustStore(conf),
             includeProtocols, includeCipherSuites);
       } else {
         binaryTransport = new TSocket(new TConfiguration(), store.getHost(), 
store.getPort(),
diff --git 
a/standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/conf/MetastoreConf.java
 
b/standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/conf/MetastoreConf.java
index 5d94435656a..eb270e84e23 100644
--- 
a/standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/conf/MetastoreConf.java
+++ 
b/standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/conf/MetastoreConf.java
@@ -37,6 +37,7 @@
 import com.google.common.base.Preconditions;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hive.common.ZooKeeperHiveHelper;
+import org.apache.hadoop.hive.metastore.utils.SecurityUtils;
 import org.apache.hadoop.hive.metastore.utils.StringUtils;
 import org.apache.hadoop.security.alias.CredentialProviderFactory;
 import org.apache.hive.common.util.SuppressFBWarnings;
@@ -296,6 +297,10 @@ public String toString() {
       ConfVars.SSL_KEYSTORE_PASSWORD.hiveName,
       ConfVars.SSL_TRUSTSTORE_PASSWORD.varname,
       ConfVars.SSL_TRUSTSTORE_PASSWORD.hiveName,
+      ConfVars.SSL_SERVER_TRUSTSTORE_PASSWORD.varname,
+      ConfVars.SSL_SERVER_TRUSTSTORE_PASSWORD.hiveName,
+      ConfVars.SSL_CLIENT_KEYSTORE_PASSWORD.varname,
+      ConfVars.SSL_CLIENT_KEYSTORE_PASSWORD.hiveName,
       ConfVars.DBACCESS_SSL_TRUSTSTORE_PASSWORD.varname,
       ConfVars.DBACCESS_SSL_TRUSTSTORE_PASSWORD.hiveName,
       ConfVars.THRIFT_ZOOKEEPER_SSL_KEYSTORE_PASSWORD.varname,
@@ -1519,6 +1524,39 @@ public enum ConfVars {
         "Comma-separated list of SSL protocols to include for the Metastore."),
     SSL_INCLUDE_CIPHERSUITES("metastore.include.ciphersuites", 
"hive.metastore.include.ciphersuites", "",
         "Colon-separated list of cipher suite names to include for the 
Metastore."),
+    SSL_CLIENT_AUTH("metastore.ssl.client.auth", 
"hive.metastore.ssl.client.auth",
+        SecurityUtils.ClientAuth.NONE.name(), new 
EnumValidator(SecurityUtils.ClientAuth.values()),
+        "TLS client authentication mode for the Metastore Thrift endpoints 
(binary and HTTP transport). \n" +
+            "NONE: client certificates are not requested. WANT: client 
certificates are requested and \n" +
+            "verified if presented, but clients without one are still 
accepted, so WANT alone provides no \n" +
+            "access control and is intended as a migration step toward NEED. 
NEED: client certificates are \n" +
+            "required and verified, and clients without one are rejected. 
Client certificates are verified \n" +
+            "against hive.metastore.server.truststore.path, which is required 
unless the mode is NONE. \n" +
+            "The client certificate is only used to accept or reject the TLS 
connection: its identity is not \n" +
+            "mapped to a user or used for authorization. Without Kerberos, the 
server still trusts the user \n" +
+            "name reported by the client. Use Kerberos to authenticate 
users."),
+    SSL_SERVER_TRUSTSTORE_PATH("metastore.server.truststore.path", 
"hive.metastore.server.truststore.path", "",
+        "Metastore SSL server certificate truststore location, used to verify 
client certificates. Any client \n" +
+            "certificate issued by a CA in this truststore is accepted, so it 
should contain only the CA that \n" +
+            "issues the Metastore client certificates (e.g. an internal CA), 
not a general-purpose bundle such \n" +
+            "as the JDK cacerts."),
+    SSL_SERVER_TRUSTSTORE_PASSWORD("metastore.server.truststore.password",
+        "hive.metastore.server.truststore.password", "",
+        "Metastore SSL server certificate truststore password."),
+    SSL_SERVER_TRUSTSTORE_TYPE("metastore.server.truststore.type", 
"hive.metastore.server.truststore.type", "",
+        "Metastore SSL server certificate truststore type."),
+    
SSL_SERVER_TRUSTMANAGERFACTORY_ALGORITHM("metastore.server.trustmanagerfactory.algorithm",
+        "hive.metastore.server.trustmanagerfactory.algorithm", "",
+        "Metastore SSL server certificate truststore algorithm."),
+    SSL_CLIENT_KEYSTORE_PATH("metastore.client.keystore.path", 
"hive.metastore.client.keystore.path", "",
+        "Metastore SSL client certificate keystore location, used when the 
server requests a client certificate."),
+    SSL_CLIENT_KEYSTORE_PASSWORD("metastore.client.keystore.password", 
"hive.metastore.client.keystore.password", "",
+        "Metastore SSL client certificate keystore password."),
+    SSL_CLIENT_KEYSTORE_TYPE("metastore.client.keystore.type", 
"hive.metastore.client.keystore.type", "",
+        "Metastore SSL client certificate keystore type."),
+    
SSL_CLIENT_KEYMANAGERFACTORY_ALGORITHM("metastore.client.keymanagerfactory.algorithm",
+        "hive.metastore.client.keymanagerfactory.algorithm", "",
+        "Metastore SSL client certificate keystore algorithm."),
     STATS_AUTO_GATHER("metastore.stats.autogather", "hive.stats.autogather", 
true,
         "A flag to gather statistics (only basic) automatically during the 
INSERT OVERWRITE command."),
     STATS_FETCH_BITVECTOR("metastore.stats.fetch.bitvector", 
"hive.stats.fetch.bitvector", false,
@@ -2848,6 +2886,94 @@ public static ZooKeeperHiveHelper 
getZKConfig(Configuration conf) {
         .trustStoreType(MetastoreConf.getVar(conf, 
ConfVars.THRIFT_ZOOKEEPER_SSL_TRUSTSTORE_TYPE)).build();
   }
 
+  /**
+   * Get the TLS client authentication mode of the metastore server endpoints, 
ignoring case and surrounding
+   * whitespace.
+   * @param conf configuration to read
+   * @return the client authentication mode
+   * @throws IllegalArgumentException if the configured mode is not one of 
NONE, WANT or NEED
+   */
+  public static SecurityUtils.ClientAuth getSslClientAuth(Configuration conf) {
+    String clientAuth = getVar(conf, ConfVars.SSL_CLIENT_AUTH);
+    try {
+      return SecurityUtils.ClientAuth.valueOf(clientAuth.trim().toUpperCase());
+    } catch (IllegalArgumentException e) {
+      throw new IllegalArgumentException("Invalid value '" + clientAuth + "' 
for " + ConfVars.SSL_CLIENT_AUTH
+          + ", expected one of " + 
Arrays.toString(SecurityUtils.ClientAuth.values()), e);
+    }
+  }
+
+  /**
+   * Get the keystore holding the metastore server certificate.
+   * @param conf configuration to read
+   * @return the server keystore
+   * @throws IllegalArgumentException if the keystore path is not configured
+   * @throws IOException if thrown by Configuration.getPassword
+   */
+  public static SecurityUtils.StoreConfig getSslServerKeyStore(Configuration 
conf) throws IOException {
+    return getSslStoreConfig(conf, ConfVars.SSL_KEYSTORE_PATH, 
ConfVars.SSL_KEYSTORE_PASSWORD,
+        ConfVars.SSL_KEYSTORE_TYPE, ConfVars.SSL_KEYMANAGERFACTORY_ALGORITHM, 
"SSL connection");
+  }
+
+  /**
+   * Get the truststore used by the metastore server to verify client 
certificates.
+   * @param conf configuration to read
+   * @return the server truststore
+   * @throws IllegalArgumentException if the truststore path is not configured
+   * @throws IOException if thrown by Configuration.getPassword
+   */
+  public static SecurityUtils.StoreConfig getSslServerTrustStore(Configuration 
conf) throws IOException {
+    return getSslStoreConfig(conf, ConfVars.SSL_SERVER_TRUSTSTORE_PATH, 
ConfVars.SSL_SERVER_TRUSTSTORE_PASSWORD,
+        ConfVars.SSL_SERVER_TRUSTSTORE_TYPE, 
ConfVars.SSL_SERVER_TRUSTMANAGERFACTORY_ALGORITHM,
+        "SSL client authentication");
+  }
+
+  /**
+   * Get the keystore holding the metastore client certificate. The client 
keystore is optional, so the returned
+   * store is not configured if its path is not set.
+   * @param conf configuration to read
+   * @return the client keystore
+   * @throws IOException if thrown by Configuration.getPassword
+   */
+  public static SecurityUtils.StoreConfig getSslClientKeyStore(Configuration 
conf) throws IOException {
+    return getSslStoreConfig(conf, ConfVars.SSL_CLIENT_KEYSTORE_PATH, 
ConfVars.SSL_CLIENT_KEYSTORE_PASSWORD,
+        ConfVars.SSL_CLIENT_KEYSTORE_TYPE, 
ConfVars.SSL_CLIENT_KEYMANAGERFACTORY_ALGORITHM, null);
+  }
+
+  /**
+   * Get the truststore used by the metastore client to verify the server 
certificate.
+   * @param conf configuration to read
+   * @return the client truststore
+   * @throws IllegalArgumentException if the truststore path is not configured
+   * @throws IOException if thrown by Configuration.getPassword
+   */
+  public static SecurityUtils.StoreConfig getSslClientTrustStore(Configuration 
conf) throws IOException {
+    return getSslStoreConfig(conf, ConfVars.SSL_TRUSTSTORE_PATH, 
ConfVars.SSL_TRUSTSTORE_PASSWORD,
+        ConfVars.SSL_TRUSTSTORE_TYPE, 
ConfVars.SSL_TRUSTMANAGERFACTORY_ALGORITHM, "SSL connection");
+  }
+
+  /**
+   * Get a keystore or truststore from the given configuration variables.
+   * @param conf configuration to read
+   * @param pathVar store path variable
+   * @param passwordVar store password variable
+   * @param typeVar store type variable
+   * @param algorithmVar key or trust manager factory algorithm variable
+   * @param requiredFor if not null, the store path must be configured, and 
the error mentions this purpose
+   * @return the store configuration
+   * @throws IllegalArgumentException if requiredFor is not null and the store 
path is not configured
+   * @throws IOException if thrown by Configuration.getPassword
+   */
+  private static SecurityUtils.StoreConfig getSslStoreConfig(Configuration 
conf, ConfVars pathVar,
+      ConfVars passwordVar, ConfVars typeVar, ConfVars algorithmVar, String 
requiredFor) throws IOException {
+    String path = getVar(conf, pathVar).trim();
+    if (requiredFor != null && path.isEmpty()) {
+      throw new IllegalArgumentException(pathVar + " Not configured for " + 
requiredFor);
+    }
+    return new SecurityUtils.StoreConfig(path, getPassword(conf, passwordVar), 
getVar(conf, typeVar).trim(),
+        getVar(conf, algorithmVar).trim());
+  }
+
   /**
    * Dump the configuration file to the log.  It will be dumped at an INFO 
level.  This can
    * potentially produce a lot of logs, so you might want to be careful when 
and where you do it.
diff --git 
a/standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/utils/SecurityUtils.java
 
b/standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/utils/SecurityUtils.java
index 7e1eacf3ecc..a245c8b0664 100644
--- 
a/standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/utils/SecurityUtils.java
+++ 
b/standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/utils/SecurityUtils.java
@@ -24,6 +24,7 @@
 import java.security.KeyManagementException;
 import java.security.KeyStoreException;
 import java.security.NoSuchAlgorithmException;
+import java.security.UnrecoverableKeyException;
 import java.security.cert.CertificateException;
 import javax.net.ssl.SSLContext;
 
@@ -48,6 +49,7 @@
 import org.apache.hc.client5.http.ssl.TlsSocketStrategy;
 import org.apache.hc.core5.http.config.Registry;
 import org.apache.hc.core5.http.config.RegistryBuilder;
+import org.apache.hc.core5.ssl.SSLContextBuilder;
 import org.apache.hc.core5.ssl.SSLContexts;
 import org.apache.thrift.transport.THttpClient;
 import org.apache.thrift.transport.TSSLTransportFactory;
@@ -312,8 +314,39 @@ private static String[] filterEnabledProtocols(String[] 
enabledProtocols,
     return filteredProtocols;
   }
 
-  private static TSSLTransportFactory.TSSLTransportParameters 
getSSLTransportParameters(boolean isKeyStore,
-      String storePath, String storePassword, String storeAlgorithm, String 
storeType, String includeCipherSuites) {
+  /**
+   * TLS client authentication mode of a server endpoint.
+   */
+  public enum ClientAuth {
+    NONE, WANT, NEED
+  }
+
+  /**
+   * Location and access settings of a keystore or truststore. An empty path 
means none is configured, and an
+   * empty type or algorithm means the JVM default.
+   */
+  public record StoreConfig(String path, String password, String type, String 
algorithm) {
+    public static final StoreConfig NONE = new StoreConfig("", "", "", "");
+
+    public boolean isConfigured() {
+      return !path.isEmpty();
+    }
+
+    public String typeOrDefault() {
+      return type.isEmpty() ? KeyStore.getDefaultType() : type;
+    }
+
+    public String keyManagerAlgorithmOrDefault() {
+      return algorithm.isEmpty() ? KeyManagerFactory.getDefaultAlgorithm() : 
algorithm;
+    }
+
+    public String trustManagerAlgorithmOrDefault() {
+      return algorithm.isEmpty() ? TrustManagerFactory.getDefaultAlgorithm() : 
algorithm;
+    }
+  }
+
+  private static TSSLTransportFactory.TSSLTransportParameters 
getSSLTransportParameters(
+      StoreConfig keyStore, StoreConfig trustStore, String 
includeCipherSuites) {
     TSSLTransportFactory.TSSLTransportParameters params;
     String[] parsedCipherSuites = 
parseIncludeCipherSuites(includeCipherSuites);
     if (parsedCipherSuites.length > 0) {
@@ -321,22 +354,22 @@ private static 
TSSLTransportFactory.TSSLTransportParameters getSSLTransportParam
     } else {
       params = new TSSLTransportFactory.TSSLTransportParameters();
     }
-    if (isKeyStore) {
-      params.setKeyStore(storePath, storePassword, storeAlgorithm, storeType);
-    } else {
-      params.setTrustStore(storePath, storePassword, storeAlgorithm, 
storeType);
-      params.requireClientAuth(true);
+    if (keyStore.isConfigured()) {
+      params.setKeyStore(keyStore.path(), keyStore.password(), 
keyStore.keyManagerAlgorithmOrDefault(),
+          keyStore.typeOrDefault());
+    }
+    if (trustStore.isConfigured()) {
+      params.setTrustStore(trustStore.path(), trustStore.password(), 
trustStore.trustManagerAlgorithmOrDefault(),
+          trustStore.typeOrDefault());
     }
     return params;
   }
 
-  public static TServerSocket getServerSSLSocket(String hiveHost, int portNum, 
String keyStorePath,
-      String keyStorePassWord, String keyStoreType, String keyStoreAlgorithm, 
List<String> sslVersionBlacklist,
-      String includeProtocols, String includeCipherSuites) throws 
TTransportException {
-    TSSLTransportFactory.TSSLTransportParameters params = 
getSSLTransportParameters(true,
-        keyStorePath, keyStorePassWord,
-        keyStoreAlgorithm.isEmpty() ? KeyManagerFactory.getDefaultAlgorithm() 
: keyStoreAlgorithm,
-        keyStoreType.isEmpty() ? KeyStore.getDefaultType() : keyStoreType, 
includeCipherSuites);
+  public static TServerSocket getServerSSLSocket(String hiveHost, int portNum, 
StoreConfig keyStore,
+      ClientAuth clientAuth, StoreConfig trustStore, List<String> 
sslVersionBlacklist, String includeProtocols,
+      String includeCipherSuites) throws TTransportException {
+    TSSLTransportFactory.TSSLTransportParameters params =
+        getSSLTransportParameters(keyStore, trustStore, includeCipherSuites);
     InetSocketAddress serverAddress;
     if (StringUtils.isEmpty(hiveHost)) {
       // Wildcard bind
@@ -368,17 +401,25 @@ public static TServerSocket getServerSSLSocket(String 
hiveHost, int portNum, Str
           includeProtocols));
       LOG.info("SSL Server Socket Enabled Protocols: "
           + Arrays.toString(sslServerSocket.getEnabledProtocols()));
+      setClientAuth(sslServerSocket, clientAuth);
     }
     return thriftServerSocket;
   }
 
+  private static void setClientAuth(SSLServerSocket sslServerSocket, 
ClientAuth clientAuth) {
+    if (clientAuth == ClientAuth.NEED) {
+      sslServerSocket.setNeedClientAuth(true);
+    } else if (clientAuth == ClientAuth.WANT) {
+      sslServerSocket.setWantClientAuth(true);
+    }
+    LOG.info("SSL Server Socket client authentication: {}", clientAuth);
+  }
+
   public static TTransport getSSLSocket(String host, int port, int 
socketTimeout, int connectionTimeout,
-      String trustStorePath, String trustStorePassWord, String trustStoreType,
-      String trustStoreAlgorithm, String includeProtocols, String 
includeCipherSuites) throws TTransportException {
-    TSSLTransportFactory.TSSLTransportParameters params = 
getSSLTransportParameters(false,
-        trustStorePath, trustStorePassWord,
-        trustStoreAlgorithm.isEmpty() ? 
TrustManagerFactory.getDefaultAlgorithm() : trustStoreAlgorithm,
-        trustStoreType.isEmpty() ? KeyStore.getDefaultType() : trustStoreType, 
includeCipherSuites);
+      StoreConfig keyStore, StoreConfig trustStore, String includeProtocols, 
String includeCipherSuites)
+      throws TTransportException {
+    TSSLTransportFactory.TSSLTransportParameters params =
+        getSSLTransportParameters(keyStore, trustStore, includeCipherSuites);
     // The underlying SSLSocket object is bound to host:port with the given 
SO_TIMEOUT and
     // connection timeout and SSLContext created with the given params
     TSocket tSSLSocket = TSSLTransportFactory.getClientSocket(host, port, 
socketTimeout, params);
@@ -392,27 +433,29 @@ public static TTransport getSSLSocket(String host, int 
port, int socketTimeout,
   Sets the ssl related configs in the underlying http client builder and wrap 
it up
   in a THttpClient
    */
-  public static THttpClient getThriftHttpsClient(String httpsUrl, String 
trustStorePath,
-      String trustStorePasswd, String trustStoreAlgorithm, String 
trustStoreType,
+  public static THttpClient getThriftHttpsClient(String httpsUrl, StoreConfig 
keyStore, StoreConfig trustStore,
       String includeProtocols, String includeCipherSuites,
        HttpClientBuilder underlyingHttpClientBuilder) throws 
TTransportException, IOException,
       KeyStoreException, NoSuchAlgorithmException, CertificateException,
-      KeyManagementException {
+      KeyManagementException, UnrecoverableKeyException {
     Preconditions.checkNotNull(underlyingHttpClientBuilder, "httpClientBuilder 
should not be null");
-    if (trustStoreType == null || trustStoreType.isEmpty()) {
-      trustStoreType = KeyStore.getDefaultType();
-    }
-    if (trustStoreAlgorithm.isEmpty()) {
-      trustStoreAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
-    }
-    KeyStore sslTrustStore = KeyStore.getInstance(trustStoreType);
-    try (FileInputStream fis = new FileInputStream(trustStorePath)) {
-      sslTrustStore.load(fis, trustStorePasswd.toCharArray());
+    KeyStore sslTrustStore = KeyStore.getInstance(trustStore.typeOrDefault());
+    try (FileInputStream fis = new FileInputStream(trustStore.path())) {
+      sslTrustStore.load(fis, trustStore.password().toCharArray());
     }
 
-    SSLContext sslContext =
-        
SSLContexts.custom().setTrustManagerFactoryAlgorithm(trustStoreAlgorithm).
-            loadTrustMaterial(sslTrustStore, null).build();
+    SSLContextBuilder sslContextBuilder =
+        
SSLContexts.custom().setTrustManagerFactoryAlgorithm(trustStore.trustManagerAlgorithmOrDefault())
+            .loadTrustMaterial(sslTrustStore, null);
+    if (keyStore.isConfigured()) {
+      KeyStore sslKeyStore = KeyStore.getInstance(keyStore.typeOrDefault());
+      try (FileInputStream fis = new FileInputStream(keyStore.path())) {
+        sslKeyStore.load(fis, keyStore.password().toCharArray());
+      }
+      
sslContextBuilder.setKeyManagerFactoryAlgorithm(keyStore.keyManagerAlgorithmOrDefault())
+          .loadKeyMaterial(sslKeyStore, keyStore.password().toCharArray());
+    }
+    SSLContext sslContext = sslContextBuilder.build();
     String[] protocols = null;
     String[] parsedProtocols = parseIncludeProtocols(includeProtocols);
     if (parsedProtocols.length > 0) {
diff --git 
a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java
 
b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java
index d6fb7ef1903..28fca55fa13 100644
--- 
a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java
+++ 
b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java
@@ -383,7 +383,8 @@ public void setThreadFactory(ThreadFactory threadFactory) {
 
     final HttpConnectionFactory http = new 
HttpConnectionFactory(httpServerConf);
 
-    final SslContextFactory sslContextFactory = 
ServletSecurity.createSslContextFactory(conf);
+    final SslContextFactory sslContextFactory =
+        ServletSecurity.createSslContextFactory(conf, 
MetastoreConf.getSslClientAuth(conf));
     if (sslContextFactory != null) {
       connector = new ServerConnector(server, sslContextFactory, http);
     } else {
@@ -541,17 +542,7 @@ private static ThriftServer startBinaryMetastore(int port, 
HadoopThriftAuthBridg
     if (!useSSL) {
       serverSocket = SecurityUtils.getServerSocket(msHost, port);
     } else {
-      String keyStorePath = MetastoreConf.getVar(conf, 
ConfVars.SSL_KEYSTORE_PATH).trim();
-      if (keyStorePath.isEmpty()) {
-        throw new 
IllegalArgumentException(ConfVars.SSL_KEYSTORE_PATH.toString()
-            + " Not configured for SSL connection");
-      }
-      String keyStorePassword =
-          MetastoreConf.getPassword(conf, 
MetastoreConf.ConfVars.SSL_KEYSTORE_PASSWORD);
-      String keyStoreType =
-          MetastoreConf.getVar(conf, ConfVars.SSL_KEYSTORE_TYPE).trim();
-      String keyStoreAlgorithm =
-          MetastoreConf.getVar(conf, 
ConfVars.SSL_KEYMANAGERFACTORY_ALGORITHM).trim();
+      SecurityUtils.StoreConfig keyStore = 
MetastoreConf.getSslServerKeyStore(conf);
       // enable SSL support for HMS
       List<String> sslVersionBlacklist = new ArrayList<>();
       for (String sslVersion : MetastoreConf.getVar(conf, 
ConfVars.SSL_PROTOCOL_BLACKLIST).split(",")) {
@@ -559,10 +550,12 @@ private static ThriftServer startBinaryMetastore(int 
port, HadoopThriftAuthBridg
       }
       String includeProtocols = MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_INCLUDE_PROTOCOLS);
       String includeCipherSuites = MetastoreConf.getVar(conf, 
ConfVars.SSL_INCLUDE_CIPHERSUITES);
+      SecurityUtils.ClientAuth clientAuth = 
MetastoreConf.getSslClientAuth(conf);
+      SecurityUtils.StoreConfig trustStore = clientAuth == 
SecurityUtils.ClientAuth.NONE
+          ? SecurityUtils.StoreConfig.NONE : 
MetastoreConf.getSslServerTrustStore(conf);
 
-      serverSocket = SecurityUtils.getServerSSLSocket(msHost, port, 
keyStorePath,
-          keyStorePassword, keyStoreType, keyStoreAlgorithm, 
sslVersionBlacklist,
-          includeProtocols, includeCipherSuites);
+      serverSocket = SecurityUtils.getServerSSLSocket(msHost, port, keyStore, 
clientAuth, trustStore,
+          sslVersionBlacklist, includeProtocols, includeCipherSuites);
     }
 
     ExecutorService executorService = new ThreadPoolExecutor(minWorkerThreads, 
maxWorkerThreads,
diff --git 
a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ServletSecurity.java
 
b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ServletSecurity.java
index f7d6a5e77b5..92b38f40dde 100644
--- 
a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ServletSecurity.java
+++ 
b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ServletSecurity.java
@@ -28,7 +28,6 @@
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 
-import java.security.KeyStore;
 import java.time.Duration;
 import java.util.List;
 import java.util.concurrent.Executor;
@@ -54,7 +53,6 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.net.ssl.KeyManagerFactory;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
@@ -485,25 +483,22 @@ static void loginServerPrincipal(Configuration conf) 
throws IOException {
   /**
    * Creates an SSL context factory if the configuration states so.
    * @param conf the configuration
+   * @param clientAuth the TLS client authentication mode
    * @return null if no ssl in config, an instance otherwise
    * @throws IOException if getting password fails
    */
-  static SslContextFactory createSslContextFactory(Configuration conf) throws 
IOException {
+  static SslContextFactory createSslContextFactory(Configuration conf, 
SecurityUtils.ClientAuth clientAuth)
+      throws IOException {
     final boolean useSsl = MetastoreConf.getBoolVar(conf, 
MetastoreConf.ConfVars.USE_SSL);
     if (!useSsl) {
       return null;
     }
-    final String keyStorePath = MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_KEYSTORE_PATH).trim();
-    if (keyStorePath.isEmpty()) {
-      throw new 
IllegalArgumentException(MetastoreConf.ConfVars.SSL_KEYSTORE_PATH
-          + " Not configured for SSL connection");
-    }
-    final String keyStorePassword =
-        MetastoreConf.getPassword(conf, 
MetastoreConf.ConfVars.SSL_KEYSTORE_PASSWORD);
-    final String keyStoreType =
-        MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_KEYSTORE_TYPE).trim();
-    final String keyStoreAlgorithm =
-        MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_KEYMANAGERFACTORY_ALGORITHM).trim();
+    final SecurityUtils.StoreConfig keyStore = 
MetastoreConf.getSslServerKeyStore(conf);
+    SslContextFactory.Server factory = new SslContextFactory.Server();
+    factory.setKeyStorePath(keyStore.path());
+    factory.setKeyStorePassword(keyStore.password());
+    factory.setKeyStoreType(keyStore.typeOrDefault());
+    
factory.setKeyManagerFactoryAlgorithm(keyStore.keyManagerAlgorithmOrDefault());
     final String[] excludedProtocols =
         MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_PROTOCOL_BLACKLIST).split(",");
     if (LOG.isInfoEnabled()) {
@@ -513,7 +508,6 @@ static SslContextFactory 
createSslContextFactory(Configuration conf) throws IOEx
         MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_INCLUDE_PROTOCOLS));
     String[] includeCipherSuites = SecurityUtils.parseIncludeCipherSuites(
         MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_INCLUDE_CIPHERSUITES));
-    SslContextFactory factory = new SslContextFactory.Server();
     if (includeProtocols.length > 0) {
       factory.setIncludeProtocols(includeProtocols);
     }
@@ -522,14 +516,22 @@ static SslContextFactory 
createSslContextFactory(Configuration conf) throws IOEx
       LOG.info("HTTP Server SSL: SslContextFactory.getExcludeProtocols = {}",
         Arrays.toString(factory.getExcludeProtocols()));
     }
-    factory.setKeyStorePath(keyStorePath);
-    factory.setKeyStorePassword(keyStorePassword);
-    factory.setKeyStoreType(keyStoreType.isEmpty() ? KeyStore.getDefaultType() 
: keyStoreType);
-    factory.setKeyManagerFactoryAlgorithm(keyStoreAlgorithm.isEmpty() ?
-        KeyManagerFactory.getDefaultAlgorithm() : keyStoreAlgorithm);
     if (includeCipherSuites.length > 0) {
       factory.setIncludeCipherSuites(includeCipherSuites);
     }
+    if (clientAuth != SecurityUtils.ClientAuth.NONE) {
+      final SecurityUtils.StoreConfig trustStore = 
MetastoreConf.getSslServerTrustStore(conf);
+      factory.setTrustStorePath(trustStore.path());
+      factory.setTrustStorePassword(trustStore.password());
+      factory.setTrustStoreType(trustStore.typeOrDefault());
+      
factory.setTrustManagerFactoryAlgorithm(trustStore.trustManagerAlgorithmOrDefault());
+      if (clientAuth == SecurityUtils.ClientAuth.NEED) {
+        factory.setNeedClientAuth(true);
+      } else {
+        factory.setWantClientAuth(true);
+      }
+    }
+    LOG.info("HTTP Server SSL: client authentication: {}", clientAuth);
     return factory;
   }
 }
diff --git 
a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ServletServerBuilder.java
 
b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ServletServerBuilder.java
index 6367cf9fca5..54a7630c8c6 100644
--- 
a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ServletServerBuilder.java
+++ 
b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ServletServerBuilder.java
@@ -23,6 +23,7 @@
 
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hive.metastore.conf.MetastoreConf;
+import org.apache.hadoop.hive.metastore.utils.SecurityUtils;
 import org.eclipse.jetty.server.HttpConfiguration;
 import org.eclipse.jetty.server.HttpConnectionFactory;
 import org.eclipse.jetty.server.SecureRequestCustomizer;
@@ -235,8 +236,9 @@ public Server startServer() throws Exception {
       return null;
     }
     final Server server = createServer();
-    // create the connectors
-    final SslContextFactory sslContextFactory = 
ServletSecurity.createSslContextFactory(configuration);
+    // create the connectors, TLS client authentication applies to the Thrift 
endpoints only
+    final SslContextFactory sslContextFactory =
+        ServletSecurity.createSslContextFactory(configuration, 
SecurityUtils.ClientAuth.NONE);
     final ServerConnector[] connectors = new ServerConnector[size];
     final ServletContextHandler[] handlers = new ServletContextHandler[size];
     Iterator<Map.Entry<Integer, ServletContextHandler>> it = 
handlersMap.entrySet().iterator();
diff --git 
a/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestMetastoreMutualTls.java
 
b/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestMetastoreMutualTls.java
new file mode 100644
index 00000000000..75e474541d1
--- /dev/null
+++ 
b/standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestMetastoreMutualTls.java
@@ -0,0 +1,202 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.hadoop.hive.metastore;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.Collection;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest;
+import org.apache.hadoop.hive.metastore.conf.MetastoreConf;
+import org.apache.hadoop.hive.metastore.conf.MetastoreConf.ConfVars;
+import org.apache.hadoop.hive.metastore.security.HadoopThriftAuthBridge;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+/**
+ * Tests TLS client authentication between the Metastore server and 
HiveMetaStoreClient, in binary and HTTP
+ * transport modes.
+ *
+ * The test stores were generated with:
+ * <pre>
+ * keytool -genkeypair -alias server -keyalg RSA -validity 3650 -dname 
"CN=localhost, OU=Hive, O=Apache" \
+ *   -ext "SAN=dns:localhost,ip:127.0.0.1" -storetype PKCS12 -keystore 
mtls_server_keystore.p12 \
+ *   -storepass apache -keypass apache
+ * keytool -genkeypair -alias client -keyalg RSA -validity 3650 -dname 
"CN=hive-client, OU=Hive, O=Apache" \
+ *   -storetype PKCS12 -keystore mtls_client_keystore.p12 -storepass apache 
-keypass apache
+ * keytool -genkeypair -alias untrusted -keyalg RSA -validity 3650 -dname 
"CN=untrusted-client, OU=Hive, O=Apache" \
+ *   -storetype PKCS12 -keystore mtls_untrusted_client_keystore.p12 -storepass 
apache -keypass apache
+ * keytool -exportcert -alias server -keystore mtls_server_keystore.p12 
-storepass apache -file server.crt
+ * keytool -exportcert -alias client -keystore mtls_client_keystore.p12 
-storepass apache -file client.crt
+ * keytool -importcert -noprompt -alias server -file server.crt -storetype 
PKCS12 \
+ *   -keystore mtls_client_truststore.p12 -storepass apache
+ * keytool -importcert -noprompt -alias client -file client.crt -storetype 
PKCS12 \
+ *   -keystore mtls_server_truststore.p12 -storepass apache
+ * </pre>
+ */
+@Category(MetastoreCheckinTest.class)
+@RunWith(Parameterized.class)
+public class TestMetastoreMutualTls {
+  private static final String STORES_DIR = "src/test/resources";
+  private static final String PASSWORD = "apache";
+  private static final String SERVER_KEYSTORE = 
store("mtls_server_keystore.p12");
+  private static final String SERVER_TRUSTSTORE = 
store("mtls_server_truststore.p12");
+  private static final String CLIENT_KEYSTORE = 
store("mtls_client_keystore.p12");
+  private static final String CLIENT_TRUSTSTORE = 
store("mtls_client_truststore.p12");
+  private static final String UNTRUSTED_CLIENT_KEYSTORE = 
store("mtls_untrusted_client_keystore.p12");
+
+  @Parameterized.Parameters(name = "{0}")
+  public static Collection<Object[]> transportModes() {
+    return Arrays.asList(new Object[][] {{"binary"}, {"http"}});
+  }
+
+  private final String transportMode;
+  private Configuration serverConf;
+  private int port = -1;
+
+  public TestMetastoreMutualTls(String transportMode) {
+    this.transportMode = transportMode;
+  }
+
+  private static String store(String name) {
+    return STORES_DIR + File.separator + name;
+  }
+
+  @After
+  public void tearDown() {
+    if (port > 0) {
+      MetaStoreTestUtils.close(port);
+      port = -1;
+    }
+  }
+
+  private Configuration serverConf(String clientAuth) {
+    Configuration conf = MetastoreConf.newMetastoreConf();
+    MetaStoreTestUtils.setConfForStandloneMode(conf);
+    MetastoreConf.setVar(conf, ConfVars.THRIFT_TRANSPORT_MODE, transportMode);
+    MetastoreConf.setBoolVar(conf, ConfVars.USE_SSL, true);
+    MetastoreConf.setVar(conf, ConfVars.SSL_KEYSTORE_PATH, SERVER_KEYSTORE);
+    MetastoreConf.setVar(conf, ConfVars.SSL_KEYSTORE_PASSWORD, PASSWORD);
+    MetastoreConf.setVar(conf, ConfVars.SSL_KEYSTORE_TYPE, "PKCS12");
+    MetastoreConf.setVar(conf, ConfVars.SSL_CLIENT_AUTH, clientAuth);
+    return conf;
+  }
+
+  private void startServer(String clientAuth) throws Exception {
+    serverConf = serverConf(clientAuth);
+    MetastoreConf.setVar(serverConf, ConfVars.SSL_SERVER_TRUSTSTORE_PATH, 
SERVER_TRUSTSTORE);
+    MetastoreConf.setVar(serverConf, ConfVars.SSL_SERVER_TRUSTSTORE_PASSWORD, 
PASSWORD);
+    MetastoreConf.setVar(serverConf, ConfVars.SSL_SERVER_TRUSTSTORE_TYPE, 
"PKCS12");
+    port = MetaStoreTestUtils.startMetaStoreWithRetry(serverConf);
+  }
+
+  private Configuration clientConf(String clientKeyStore) {
+    Configuration conf = MetastoreConf.newMetastoreConf();
+    MetastoreConf.setVar(conf, ConfVars.THRIFT_URIS, "thrift://localhost:" + 
port);
+    MetastoreConf.setVar(conf, 
ConfVars.METASTORE_CLIENT_THRIFT_TRANSPORT_MODE, transportMode);
+    MetastoreConf.setBoolVar(conf, ConfVars.EXECUTE_SET_UGI, false);
+    MetastoreConf.setLongVar(conf, ConfVars.THRIFT_CONNECTION_RETRIES, 1);
+    MetastoreConf.setBoolVar(conf, ConfVars.USE_SSL, true);
+    MetastoreConf.setVar(conf, ConfVars.SSL_TRUSTSTORE_PATH, 
CLIENT_TRUSTSTORE);
+    MetastoreConf.setVar(conf, ConfVars.SSL_TRUSTSTORE_PASSWORD, PASSWORD);
+    MetastoreConf.setVar(conf, ConfVars.SSL_TRUSTSTORE_TYPE, "PKCS12");
+    if (clientKeyStore != null) {
+      MetastoreConf.setVar(conf, ConfVars.SSL_CLIENT_KEYSTORE_PATH, 
clientKeyStore);
+      MetastoreConf.setVar(conf, ConfVars.SSL_CLIENT_KEYSTORE_PASSWORD, 
PASSWORD);
+      MetastoreConf.setVar(conf, ConfVars.SSL_CLIENT_KEYSTORE_TYPE, "PKCS12");
+    }
+    return conf;
+  }
+
+  private void assertConnects(String clientKeyStore) throws Exception {
+    try (HiveMetaStoreClient client = new 
HiveMetaStoreClient(clientConf(clientKeyStore))) {
+      Assert.assertNotNull(client.getAllDatabases());
+    }
+  }
+
+  private void assertRejected(String clientKeyStore) {
+    try (HiveMetaStoreClient client = new 
HiveMetaStoreClient(clientConf(clientKeyStore))) {
+      client.getAllDatabases();
+      Assert.fail("Expected the TLS handshake to be rejected");
+    } catch (Exception expected) {
+      // handshake rejected by the server
+    }
+  }
+
+  @Test
+  public void testNoneAcceptsClientsWithAndWithoutCertificate() throws 
Exception {
+    startServer("NONE");
+    assertConnects(null);
+    assertConnects(CLIENT_KEYSTORE);
+  }
+
+  @Test
+  public void testWantAcceptsClientsWithoutCertificate() throws Exception {
+    startServer("WANT");
+    assertConnects(null);
+    assertConnects(CLIENT_KEYSTORE);
+    // The server only asks for certificates from the issuers in its 
truststore. The client has none from
+    // those issuers, so it connects without sending a certificate, which WANT 
allows.
+    assertConnects(UNTRUSTED_CLIENT_KEYSTORE);
+  }
+
+  @Test
+  public void testNeedRequiresTrustedCertificate() throws Exception {
+    startServer("need");
+    assertConnects(CLIENT_KEYSTORE);
+    assertRejected(null);
+    assertRejected(UNTRUSTED_CLIENT_KEYSTORE);
+  }
+
+  @Test
+  public void testClientAuthWithoutServerTrustStoreFails() throws Throwable {
+    for (String clientAuth : new String[] {"WANT", "NEED"}) {
+      Configuration conf = serverConf(clientAuth);
+      // The metastore truststore is the client side truststore and is not 
used to verify client certificates.
+      MetastoreConf.setVar(conf, ConfVars.SSL_TRUSTSTORE_PATH, 
SERVER_TRUSTSTORE);
+      MetastoreConf.setVar(conf, ConfVars.SSL_TRUSTSTORE_PASSWORD, PASSWORD);
+      int freePort = MetaStoreTestUtils.findFreePort();
+      HadoopThriftAuthBridge bridge = HadoopThriftAuthBridge.getBridge();
+      try {
+        HiveMetaStore.startMetaStore(freePort, bridge, conf);
+        Assert.fail("Expected " + clientAuth + " without a server truststore 
to fail at startup");
+      } catch (IllegalArgumentException e) {
+        Assert.assertEquals(ConfVars.SSL_SERVER_TRUSTSTORE_PATH + " Not 
configured for SSL client authentication",
+            e.getMessage());
+      }
+    }
+  }
+
+  @Test
+  public void testInvalidClientAuthFails() {
+    Configuration conf = MetastoreConf.newMetastoreConf();
+    MetastoreConf.setVar(conf, ConfVars.SSL_CLIENT_AUTH, "REQUIRED");
+    try {
+      MetastoreConf.getSslClientAuth(conf);
+      Assert.fail("Expected an invalid client authentication mode to be 
rejected");
+    } catch (IllegalArgumentException e) {
+      Assert.assertEquals("Invalid value 'REQUIRED' for " + 
ConfVars.SSL_CLIENT_AUTH
+          + ", expected one of [NONE, WANT, NEED]", e.getMessage());
+    }
+  }
+}
diff --git 
a/standalone-metastore/metastore-server/src/test/resources/mtls_client_keystore.p12
 
b/standalone-metastore/metastore-server/src/test/resources/mtls_client_keystore.p12
new file mode 100644
index 00000000000..8f72d9f728d
Binary files /dev/null and 
b/standalone-metastore/metastore-server/src/test/resources/mtls_client_keystore.p12
 differ
diff --git 
a/standalone-metastore/metastore-server/src/test/resources/mtls_client_truststore.p12
 
b/standalone-metastore/metastore-server/src/test/resources/mtls_client_truststore.p12
new file mode 100644
index 00000000000..6fc66f38d0d
Binary files /dev/null and 
b/standalone-metastore/metastore-server/src/test/resources/mtls_client_truststore.p12
 differ
diff --git 
a/standalone-metastore/metastore-server/src/test/resources/mtls_server_keystore.p12
 
b/standalone-metastore/metastore-server/src/test/resources/mtls_server_keystore.p12
new file mode 100644
index 00000000000..4b5068e5829
Binary files /dev/null and 
b/standalone-metastore/metastore-server/src/test/resources/mtls_server_keystore.p12
 differ
diff --git 
a/standalone-metastore/metastore-server/src/test/resources/mtls_server_truststore.p12
 
b/standalone-metastore/metastore-server/src/test/resources/mtls_server_truststore.p12
new file mode 100644
index 00000000000..9f04c0444b1
Binary files /dev/null and 
b/standalone-metastore/metastore-server/src/test/resources/mtls_server_truststore.p12
 differ
diff --git 
a/standalone-metastore/metastore-server/src/test/resources/mtls_untrusted_client_keystore.p12
 
b/standalone-metastore/metastore-server/src/test/resources/mtls_untrusted_client_keystore.p12
new file mode 100644
index 00000000000..78f90d6261b
Binary files /dev/null and 
b/standalone-metastore/metastore-server/src/test/resources/mtls_untrusted_client_keystore.p12
 differ
diff --git 
a/standalone-metastore/metastore-tools/tools-common/src/main/java/org/apache/hadoop/hive/metastore/tools/HMSClient.java
 
b/standalone-metastore/metastore-tools/tools-common/src/main/java/org/apache/hadoop/hive/metastore/tools/HMSClient.java
index 4a915adc0a8..ac2fe874e75 100644
--- 
a/standalone-metastore/metastore-tools/tools-common/src/main/java/org/apache/hadoop/hive/metastore/tools/HMSClient.java
+++ 
b/standalone-metastore/metastore-tools/tools-common/src/main/java/org/apache/hadoop/hive/metastore/tools/HMSClient.java
@@ -445,22 +445,11 @@ private TTransport open(Configuration conf, @NotNull URI 
uri) throws
     if (!useSSL) {
       transport = new TSocket(new TConfiguration(),host, port, 
clientSocketTimeout, connectionTimeout);
     } else {
-      String trustStorePath = MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_TRUSTSTORE_PATH).trim();
-      if (trustStorePath.isEmpty()) {
-        throw new 
IllegalArgumentException(MetastoreConf.ConfVars.SSL_TRUSTSTORE_PATH.toString()
-            + " Not configured for SSL connection");
-      }
-      String trustStorePassword =
-          MetastoreConf.getPassword(conf, 
MetastoreConf.ConfVars.SSL_TRUSTSTORE_PASSWORD);
-      String trustStoreType =
-              MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_TRUSTSTORE_TYPE).trim();
-      String trustStoreAlgorithm =
-              MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_TRUSTMANAGERFACTORY_ALGORITHM).trim();
       String includeProtocols = MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_INCLUDE_PROTOCOLS);
       String includeCipherSuites = MetastoreConf.getVar(conf, 
MetastoreConf.ConfVars.SSL_INCLUDE_CIPHERSUITES);
       // Create an SSL socket and connect
       transport = SecurityUtils.getSSLSocket(host, port, clientSocketTimeout, 
connectionTimeout,
-          trustStorePath, trustStorePassword, trustStoreType, 
trustStoreAlgorithm,
+          MetastoreConf.getSslClientKeyStore(conf), 
MetastoreConf.getSslClientTrustStore(conf),
           includeProtocols, includeCipherSuites);
       LOG.info("Opened an SSL connection to metastore, current connections");
     }

Reply via email to