You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@hive.apache.org by "ASF GitHub Bot (Jira)" <ji...@apache.org> on 2022/03/01 06:19:00 UTC

[jira] [Work logged] (HIVE-25575) Add support for JWT authentication in HTTP mode

     [ https://issues.apache.org/jira/browse/HIVE-25575?focusedWorklogId=734357&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-734357 ]

ASF GitHub Bot logged work on HIVE-25575:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 01/Mar/22 06:18
            Start Date: 01/Mar/22 06:18
    Worklog Time Spent: 10m 
      Work Description: sourabh912 commented on a change in pull request #3006:
URL: https://github.com/apache/hive/pull/3006#discussion_r816156182



##########
File path: jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java
##########
@@ -804,6 +811,34 @@ protected boolean requestIsAborted(final HttpRequest request) {
     return httpClientBuilder.build();
   }
 
+  private String getJWT() {
+    String jwtCredential = getJWTStringFromSession();
+    if (jwtCredential == null) {

Review comment:
       check for non empty string as well ? 

##########
File path: common/src/java/org/apache/hadoop/hive/conf/HiveConf.java
##########
@@ -4127,7 +4127,8 @@ private static void populateLlapDaemonVarsSet(Set<String> llapDaemonVarsSetLocal
         "          (Use with property hive.server2.custom.authentication.class)\n" +
         "  PAM: Pluggable authentication module\n" +
         "  NOSASL:  Raw transport\n" +
-        "  SAML: SAML 2.0 compliant authentication. This is only supported in http transport mode."),
+        "  SAML: SAML 2.0 compliant authentication. This is only supported in http transport mode\n" +
+        "  JWT: JWT based authentication, JWT needs to contain the user as subject"),

Review comment:
       Isn't JWT only supported in http mode? 

##########
File path: jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java
##########
@@ -804,6 +811,34 @@ protected boolean requestIsAborted(final HttpRequest request) {
     return httpClientBuilder.build();
   }
 
+  private String getJWT() {
+    String jwtCredential = getJWTStringFromSession();
+    if (jwtCredential == null) {
+      jwtCredential = getJWTStringFromEnv();
+    }
+    return jwtCredential;
+  }
+
+  private String getJWTStringFromEnv() {
+    String jwtCredential = System.getenv(JdbcConnectionParams.AUTH_JWT_ENV);
+    if (jwtCredential == null) {

Review comment:
       same as previous comment, check for non empty string as well? 

##########
File path: jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java
##########
@@ -804,6 +811,34 @@ protected boolean requestIsAborted(final HttpRequest request) {
     return httpClientBuilder.build();
   }
 
+  private String getJWT() {
+    String jwtCredential = getJWTStringFromSession();
+    if (jwtCredential == null) {
+      jwtCredential = getJWTStringFromEnv();
+    }
+    return jwtCredential;
+  }
+
+  private String getJWTStringFromEnv() {
+    String jwtCredential = System.getenv(JdbcConnectionParams.AUTH_JWT_ENV);
+    if (jwtCredential == null) {
+      LOG.debug("No JWT is specified in env variable {}", JdbcConnectionParams.AUTH_JWT_ENV);
+    } else {
+      LOG.debug("Fetched JWT from the env.");
+    }
+    return jwtCredential;
+  }
+
+  private String getJWTStringFromSession() {
+    String jwtCredential = sessConfMap.get(JdbcConnectionParams.AUTH_TYPE_JWT_KEY);
+    if (jwtCredential == null) {
+      LOG.debug("No JWT is specified in connection string.");
+    } else {
+      LOG.debug("Fetched JWT from the session.");

Review comment:
       shall we log the JWT token for debugging purpose? 

##########
File path: service/src/java/org/apache/hive/service/cli/thrift/ThriftHttpServlet.java
##########
@@ -213,6 +221,8 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
             } else {
               clientUserName = doKerberosAuth(request);
             }
+          } else if (authType.isEnabled(HiveAuthConstants.AuthTypes.JWT) && hasJWT(request)) {

Review comment:
       why do we have `hasJWT(request)` check? If this check fails does that mean we fallback to other type of auth mechanism? 

##########
File path: service/src/java/org/apache/hive/service/cli/thrift/ThriftHttpServlet.java
##########
@@ -264,8 +274,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
         LOG.info("Cookie added for clientUserName " + clientUserName);

Review comment:
       I am thinking about a following scenario: 
   
   Lets say cookieAuth is enabled and max age of cookie is 5 hrs. Now the first time a JWT token gets authenticated, HS2 would generate a cookie for it. But lets say the expiration time of JWT is 2 hrs. 
   Since the cookie is valid for 5 hrs does this mean HS2 would keep successfully authenticating the user even after the JWT token has already expired? 
   

##########
File path: service/src/java/org/apache/hive/service/auth/jwt/JWTValidator.java
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.hive.service.auth.jwt;
+
+import com.google.common.base.Preconditions;
+import com.nimbusds.jose.JOSEException;
+import com.nimbusds.jose.JWSHeader;
+import com.nimbusds.jose.JWSObject;
+import com.nimbusds.jose.JWSVerifier;
+import com.nimbusds.jose.KeyTypeException;
+import com.nimbusds.jose.crypto.ECDSAVerifier;
+import com.nimbusds.jose.crypto.MACVerifier;
+import com.nimbusds.jose.crypto.RSASSAVerifier;
+import com.nimbusds.jose.jwk.JWK;
+import com.nimbusds.jose.jwk.RSAKey;
+import com.nimbusds.jwt.JWTClaimsSet;
+import com.nimbusds.jwt.SignedJWT;
+import org.apache.hadoop.hive.conf.HiveConf;
+
+import javax.crypto.SecretKey;
+import java.io.IOException;
+import java.security.interfaces.ECPublicKey;
+import java.security.interfaces.RSAPublicKey;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.List;
+
+public class JWTValidator {
+
+  private final URLBasedJWKSProvider jwksProvider;
+
+  public JWTValidator(HiveConf conf) throws IOException, ParseException {
+    this.jwksProvider = new URLBasedJWKSProvider(conf);
+  }
+
+  public String validateJWTAndExtractUser(String signedJwt) throws Exception {
+    final SignedJWT parsedJwt = SignedJWT.parse(signedJwt);
+    List<JWK> matchedJWKS = jwksProvider.getJWKs(parsedJwt.getHeader());
+
+    // verify signature
+    boolean signatureVerificationSuccessful = false;
+    for (JWK matchedJWK : matchedJWKS) {
+      JWSVerifier verifier = getVerifier(parsedJwt, matchedJWK);
+      if (parsedJwt.verify(verifier)) {
+        signatureVerificationSuccessful = true;
+        break;
+      }
+    }
+    Preconditions.checkState(signatureVerificationSuccessful, "Unable to verify incoming JWT Signature");
+
+    // verify claims
+    JWTClaimsSet claimsSet = parsedJwt.getJWTClaimsSet();
+    Date expirationTime = claimsSet.getExpirationTime();
+    if (expirationTime != null) {
+      Date now = new Date();
+      Preconditions.checkState(now.before(expirationTime), "JWT has been expired");
+    }
+
+    // We assume the subject of claims is the query user
+    return claimsSet.getSubject();
+  }
+
+
+
+  // TODO Same logic as DefaultJWSVerifierFactory#createJWSVerifier -- This can be written more elegantly, check AGAIN
+  private static JWSVerifier getVerifier(JWSObject parsedJWT, JWK key) throws JOSEException {

Review comment:
       If the logic is exactly the same then why don't we reuse the DefaultJWSVerifierFactory here? We can override it later if there is a need. 

##########
File path: service/src/java/org/apache/hive/service/cli/thrift/ThriftHttpServlet.java
##########
@@ -302,6 +310,25 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
     }
   }
 
+  private String validateJWT(HttpServletRequest request, HttpServletResponse response)
+      throws HttpAuthenticationException {
+    Preconditions.checkState(jwtValidator != null, "JWT validator should have been set");
+    String signedJwt = extractBearerToken(request, response);
+    if (signedJwt == null) {
+      LOG.debug("No token found with the request {}", request);
+      return null;
+    }
+    String user = null;
+    try {
+      user = jwtValidator.validateJWTAndExtractUser(signedJwt);

Review comment:
       Should we check for non empty user value ? 

##########
File path: service/src/java/org/apache/hive/service/auth/jwt/JWTValidator.java
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.hive.service.auth.jwt;
+
+import com.google.common.base.Preconditions;
+import com.nimbusds.jose.JOSEException;
+import com.nimbusds.jose.JWSHeader;
+import com.nimbusds.jose.JWSObject;
+import com.nimbusds.jose.JWSVerifier;
+import com.nimbusds.jose.KeyTypeException;
+import com.nimbusds.jose.crypto.ECDSAVerifier;
+import com.nimbusds.jose.crypto.MACVerifier;
+import com.nimbusds.jose.crypto.RSASSAVerifier;
+import com.nimbusds.jose.jwk.JWK;
+import com.nimbusds.jose.jwk.RSAKey;
+import com.nimbusds.jwt.JWTClaimsSet;
+import com.nimbusds.jwt.SignedJWT;
+import org.apache.hadoop.hive.conf.HiveConf;
+
+import javax.crypto.SecretKey;
+import java.io.IOException;
+import java.security.interfaces.ECPublicKey;
+import java.security.interfaces.RSAPublicKey;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.List;
+
+public class JWTValidator {
+
+  private final URLBasedJWKSProvider jwksProvider;
+
+  public JWTValidator(HiveConf conf) throws IOException, ParseException {
+    this.jwksProvider = new URLBasedJWKSProvider(conf);
+  }
+
+  public String validateJWTAndExtractUser(String signedJwt) throws Exception {
+    final SignedJWT parsedJwt = SignedJWT.parse(signedJwt);
+    List<JWK> matchedJWKS = jwksProvider.getJWKs(parsedJwt.getHeader());

Review comment:
       add a preconditions check for jwksProvider to be not null ? 

##########
File path: service/src/java/org/apache/hive/service/auth/jwt/JWTValidator.java
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.hive.service.auth.jwt;
+
+import com.google.common.base.Preconditions;
+import com.nimbusds.jose.JOSEException;
+import com.nimbusds.jose.JWSHeader;
+import com.nimbusds.jose.JWSObject;
+import com.nimbusds.jose.JWSVerifier;
+import com.nimbusds.jose.KeyTypeException;
+import com.nimbusds.jose.crypto.ECDSAVerifier;
+import com.nimbusds.jose.crypto.MACVerifier;
+import com.nimbusds.jose.crypto.RSASSAVerifier;
+import com.nimbusds.jose.jwk.JWK;
+import com.nimbusds.jose.jwk.RSAKey;
+import com.nimbusds.jwt.JWTClaimsSet;
+import com.nimbusds.jwt.SignedJWT;
+import org.apache.hadoop.hive.conf.HiveConf;
+
+import javax.crypto.SecretKey;
+import java.io.IOException;
+import java.security.interfaces.ECPublicKey;
+import java.security.interfaces.RSAPublicKey;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.List;
+
+public class JWTValidator {
+
+  private final URLBasedJWKSProvider jwksProvider;
+
+  public JWTValidator(HiveConf conf) throws IOException, ParseException {
+    this.jwksProvider = new URLBasedJWKSProvider(conf);
+  }
+
+  public String validateJWTAndExtractUser(String signedJwt) throws Exception {

Review comment:
       Instead of a generic exception, the method should throw specific exceptions so that caller can act accordingly.

##########
File path: service/src/java/org/apache/hive/service/auth/jwt/JWTValidator.java
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.hive.service.auth.jwt;
+
+import com.google.common.base.Preconditions;
+import com.nimbusds.jose.JOSEException;
+import com.nimbusds.jose.JWSHeader;
+import com.nimbusds.jose.JWSObject;
+import com.nimbusds.jose.JWSVerifier;
+import com.nimbusds.jose.KeyTypeException;
+import com.nimbusds.jose.crypto.ECDSAVerifier;
+import com.nimbusds.jose.crypto.MACVerifier;
+import com.nimbusds.jose.crypto.RSASSAVerifier;
+import com.nimbusds.jose.jwk.JWK;
+import com.nimbusds.jose.jwk.RSAKey;
+import com.nimbusds.jwt.JWTClaimsSet;
+import com.nimbusds.jwt.SignedJWT;
+import org.apache.hadoop.hive.conf.HiveConf;
+
+import javax.crypto.SecretKey;
+import java.io.IOException;
+import java.security.interfaces.ECPublicKey;
+import java.security.interfaces.RSAPublicKey;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.List;
+
+public class JWTValidator {
+
+  private final URLBasedJWKSProvider jwksProvider;
+
+  public JWTValidator(HiveConf conf) throws IOException, ParseException {
+    this.jwksProvider = new URLBasedJWKSProvider(conf);
+  }
+
+  public String validateJWTAndExtractUser(String signedJwt) throws Exception {
+    final SignedJWT parsedJwt = SignedJWT.parse(signedJwt);
+    List<JWK> matchedJWKS = jwksProvider.getJWKs(parsedJwt.getHeader());
+
+    // verify signature
+    boolean signatureVerificationSuccessful = false;
+    for (JWK matchedJWK : matchedJWKS) {
+      JWSVerifier verifier = getVerifier(parsedJwt, matchedJWK);
+      if (parsedJwt.verify(verifier)) {
+        signatureVerificationSuccessful = true;
+        break;
+      }
+    }
+    Preconditions.checkState(signatureVerificationSuccessful, "Unable to verify incoming JWT Signature");

Review comment:
       Instead of a preconditions check, should we throw custom exception like SignatureVerficationFailedException? 

##########
File path: service/src/java/org/apache/hive/service/auth/jwt/JWTValidator.java
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.hive.service.auth.jwt;
+
+import com.google.common.base.Preconditions;
+import com.nimbusds.jose.JOSEException;
+import com.nimbusds.jose.JWSHeader;
+import com.nimbusds.jose.JWSObject;
+import com.nimbusds.jose.JWSVerifier;
+import com.nimbusds.jose.KeyTypeException;
+import com.nimbusds.jose.crypto.ECDSAVerifier;
+import com.nimbusds.jose.crypto.MACVerifier;
+import com.nimbusds.jose.crypto.RSASSAVerifier;
+import com.nimbusds.jose.jwk.JWK;
+import com.nimbusds.jose.jwk.RSAKey;
+import com.nimbusds.jwt.JWTClaimsSet;
+import com.nimbusds.jwt.SignedJWT;
+import org.apache.hadoop.hive.conf.HiveConf;
+
+import javax.crypto.SecretKey;
+import java.io.IOException;
+import java.security.interfaces.ECPublicKey;
+import java.security.interfaces.RSAPublicKey;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.List;
+
+public class JWTValidator {
+
+  private final URLBasedJWKSProvider jwksProvider;
+
+  public JWTValidator(HiveConf conf) throws IOException, ParseException {
+    this.jwksProvider = new URLBasedJWKSProvider(conf);
+  }
+
+  public String validateJWTAndExtractUser(String signedJwt) throws Exception {
+    final SignedJWT parsedJwt = SignedJWT.parse(signedJwt);
+    List<JWK> matchedJWKS = jwksProvider.getJWKs(parsedJwt.getHeader());
+
+    // verify signature
+    boolean signatureVerificationSuccessful = false;
+    for (JWK matchedJWK : matchedJWKS) {
+      JWSVerifier verifier = getVerifier(parsedJwt, matchedJWK);
+      if (parsedJwt.verify(verifier)) {
+        signatureVerificationSuccessful = true;
+        break;
+      }
+    }
+    Preconditions.checkState(signatureVerificationSuccessful, "Unable to verify incoming JWT Signature");
+
+    // verify claims
+    JWTClaimsSet claimsSet = parsedJwt.getJWTClaimsSet();
+    Date expirationTime = claimsSet.getExpirationTime();
+    if (expirationTime != null) {
+      Date now = new Date();

Review comment:
       should we make this a class member variable so that it is not instantiated on every call to validateJWTAndExtractUser() ? 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 734357)
    Time Spent: 1h 20m  (was: 1h 10m)

> Add support for JWT authentication in HTTP mode
> -----------------------------------------------
>
>                 Key: HIVE-25575
>                 URL: https://issues.apache.org/jira/browse/HIVE-25575
>             Project: Hive
>          Issue Type: New Feature
>          Components: HiveServer2, JDBC
>    Affects Versions: 4.0.0
>            Reporter: Shubham Chaurasia
>            Assignee: Yu-Wen Lai
>            Priority: Major
>              Labels: pull-request-available
>          Time Spent: 1h 20m
>  Remaining Estimate: 0h
>
> It would be good to support JWT auth mechanism in hive. In order to implement it, we would need the following - 
> On HS2 side -
> 1. Accept JWT in Authorization: Bearer header.
> 2. Fetch JWKS from a public endpoint to verify JWT signature, to start with we can fetch on HS2 start up.
> 3. Verify JWT Signature.
> On JDBC Client side - 
> 1. Hive jdbc client should be able to accept jwt in JDBC url. (will add more details)
> 2. Client should also be able to pick up JWT from an env var if it's defined.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)