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

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

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



##########
File path: service/src/java/org/apache/hive/service/auth/jwt/JWTValidator.java
##########
@@ -0,0 +1,94 @@
+/*
+ * 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.nimbusds.jose.JOSEException;
+import com.nimbusds.jose.JWSHeader;
+import com.nimbusds.jose.JWSObject;
+import com.nimbusds.jose.JWSVerifier;
+import com.nimbusds.jose.crypto.factories.DefaultJWSVerifierFactory;
+import com.nimbusds.jose.jwk.AsymmetricJWK;
+import com.nimbusds.jose.jwk.JWK;
+import com.nimbusds.jwt.JWTClaimsSet;
+import com.nimbusds.jwt.SignedJWT;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.security.sasl.AuthenticationException;
+import java.io.IOException;
+import java.security.Key;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.List;
+
+public class JWTValidator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(JWTValidator.class.getName());
+  private final URLBasedJWKSProvider jwksProvider;
+  private static final DefaultJWSVerifierFactory verifierFactory = new 
DefaultJWSVerifierFactory();
+
+  public JWTValidator(HiveConf conf) throws IOException, ParseException {
+    this.jwksProvider = new URLBasedJWKSProvider(conf);
+  }
+
+  public String validateJWTAndExtractUser(String signedJwt) throws 
ParseException, AuthenticationException {
+    final SignedJWT parsedJwt = SignedJWT.parse(signedJwt);
+    List<JWK> matchedJWKS = jwksProvider.getJWKs(parsedJwt.getHeader());
+
+    // verify signature
+    for (JWK matchedJWK : matchedJWKS) {
+      try {
+        JWSVerifier verifier = getVerifier(parsedJwt.getHeader(), matchedJWK);
+        if (parsedJwt.verify(verifier)) {
+          break;
+        }
+      } catch (JOSEException e) {
+        LOG.info("Failed to verify JWT {} by JWK {} because {}", 
parsedJwt.getHeader(), matchedJWK.getKeyID(),
+            e.getMessage());
+      }
+    }
+    if (parsedJwt.getState() != JWSObject.State.VERIFIED) {
+      throw new AuthenticationException("Failed to verify JWT signature");
+    }
+
+    // verify claims
+    JWTClaimsSet claimsSet = parsedJwt.getJWTClaimsSet();
+    Date expirationTime = claimsSet.getExpirationTime();
+    if (expirationTime != null) {
+      Date now = new Date();
+      if (now.after(expirationTime)) {
+        throw new AuthenticationException("JWT has been expired");
+      }
+    }
+
+    // We assume the subject of claims is the query user
+    return claimsSet.getSubject();
+  }
+
+  private static JWSVerifier getVerifier(JWSHeader header, JWK jwk) throws 
JOSEException {
+    Key key = null;
+    if (jwk instanceof AsymmetricJWK) {
+      key = ((AsymmetricJWK) jwk).toPublicKey();
+    } else {
+      LOG.debug("Symmetric JWK cannot be used: kid={}, alg={}", 
jwk.getKeyID(), jwk.getAlgorithm());
+    }
+    return key != null ? verifierFactory.createJWSVerifier(header, key) : null;

Review comment:
       Instead of returning a null, shall we throw an error here?  Otherwise we 
would get a NPE when is the code:
   `JWSVerifier verifier = getVerifier(parsedJwt.getHeader(), matchedJWK);`

##########
File path: service/src/java/org/apache/hive/service/auth/jwt/JWTValidator.java
##########
@@ -0,0 +1,94 @@
+/*
+ * 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.nimbusds.jose.JOSEException;
+import com.nimbusds.jose.JWSHeader;
+import com.nimbusds.jose.JWSObject;
+import com.nimbusds.jose.JWSVerifier;
+import com.nimbusds.jose.crypto.factories.DefaultJWSVerifierFactory;
+import com.nimbusds.jose.jwk.AsymmetricJWK;
+import com.nimbusds.jose.jwk.JWK;
+import com.nimbusds.jwt.JWTClaimsSet;
+import com.nimbusds.jwt.SignedJWT;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.security.sasl.AuthenticationException;
+import java.io.IOException;
+import java.security.Key;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.List;
+
+public class JWTValidator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(JWTValidator.class.getName());
+  private final URLBasedJWKSProvider jwksProvider;
+  private static final DefaultJWSVerifierFactory verifierFactory = new 
DefaultJWSVerifierFactory();
+
+  public JWTValidator(HiveConf conf) throws IOException, ParseException {
+    this.jwksProvider = new URLBasedJWKSProvider(conf);
+  }
+
+  public String validateJWTAndExtractUser(String signedJwt) throws 
ParseException, AuthenticationException {
+    final SignedJWT parsedJwt = SignedJWT.parse(signedJwt);
+    List<JWK> matchedJWKS = jwksProvider.getJWKs(parsedJwt.getHeader());
+
+    // verify signature
+    for (JWK matchedJWK : matchedJWKS) {
+      try {
+        JWSVerifier verifier = getVerifier(parsedJwt.getHeader(), matchedJWK);
+        if (parsedJwt.verify(verifier)) {
+          break;
+        }
+      } catch (JOSEException e) {
+        LOG.info("Failed to verify JWT {} by JWK {} because {}", 
parsedJwt.getHeader(), matchedJWK.getKeyID(),
+            e.getMessage());
+      }
+    }
+    if (parsedJwt.getState() != JWSObject.State.VERIFIED) {
+      throw new AuthenticationException("Failed to verify JWT signature");

Review comment:
       Should we capture the last e.getMessage() about and append it to the 
exception here? That would make the error message of `AuthenticationException` 
more descriptive. 

##########
File path: service/src/java/org/apache/hive/service/auth/jwt/JWTValidator.java
##########
@@ -0,0 +1,94 @@
+/*
+ * 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.nimbusds.jose.JOSEException;
+import com.nimbusds.jose.JWSHeader;
+import com.nimbusds.jose.JWSObject;
+import com.nimbusds.jose.JWSVerifier;
+import com.nimbusds.jose.crypto.factories.DefaultJWSVerifierFactory;
+import com.nimbusds.jose.jwk.AsymmetricJWK;
+import com.nimbusds.jose.jwk.JWK;
+import com.nimbusds.jwt.JWTClaimsSet;
+import com.nimbusds.jwt.SignedJWT;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.security.sasl.AuthenticationException;
+import java.io.IOException;
+import java.security.Key;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.List;
+
+public class JWTValidator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(JWTValidator.class.getName());
+  private final URLBasedJWKSProvider jwksProvider;
+  private static final DefaultJWSVerifierFactory verifierFactory = new 
DefaultJWSVerifierFactory();
+
+  public JWTValidator(HiveConf conf) throws IOException, ParseException {
+    this.jwksProvider = new URLBasedJWKSProvider(conf);
+  }
+
+  public String validateJWTAndExtractUser(String signedJwt) throws 
ParseException, AuthenticationException {
+    final SignedJWT parsedJwt = SignedJWT.parse(signedJwt);
+    List<JWK> matchedJWKS = jwksProvider.getJWKs(parsedJwt.getHeader());
+
+    // verify signature
+    for (JWK matchedJWK : matchedJWKS) {
+      try {
+        JWSVerifier verifier = getVerifier(parsedJwt.getHeader(), matchedJWK);
+        if (parsedJwt.verify(verifier)) {
+          break;
+        }
+      } catch (JOSEException e) {
+        LOG.info("Failed to verify JWT {} by JWK {} because {}", 
parsedJwt.getHeader(), matchedJWK.getKeyID(),
+            e.getMessage());
+      }
+    }
+    if (parsedJwt.getState() != JWSObject.State.VERIFIED) {
+      throw new AuthenticationException("Failed to verify JWT signature");
+    }
+
+    // verify claims
+    JWTClaimsSet claimsSet = parsedJwt.getJWTClaimsSet();
+    Date expirationTime = claimsSet.getExpirationTime();
+    if (expirationTime != null) {
+      Date now = new Date();
+      if (now.after(expirationTime)) {
+        throw new AuthenticationException("JWT has been expired");
+      }
+    }
+
+    // We assume the subject of claims is the query user
+    return claimsSet.getSubject();
+  }
+
+  private static JWSVerifier getVerifier(JWSHeader header, JWK jwk) throws 
JOSEException {
+    Key key = null;
+    if (jwk instanceof AsymmetricJWK) {

Review comment:
       @hsnusonic : Couple of questions: 
   1. Can you explain the rationale behind this check `jwk instanceof 
AsymmetricJWK`? 
   2. Why is this method static? 

##########
File path: service/src/java/org/apache/hive/service/auth/jwt/JWTValidator.java
##########
@@ -0,0 +1,94 @@
+/*
+ * 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.nimbusds.jose.JOSEException;
+import com.nimbusds.jose.JWSHeader;
+import com.nimbusds.jose.JWSObject;
+import com.nimbusds.jose.JWSVerifier;
+import com.nimbusds.jose.crypto.factories.DefaultJWSVerifierFactory;
+import com.nimbusds.jose.jwk.AsymmetricJWK;
+import com.nimbusds.jose.jwk.JWK;
+import com.nimbusds.jwt.JWTClaimsSet;
+import com.nimbusds.jwt.SignedJWT;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.security.sasl.AuthenticationException;
+import java.io.IOException;
+import java.security.Key;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.List;
+
+public class JWTValidator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(JWTValidator.class.getName());
+  private final URLBasedJWKSProvider jwksProvider;
+  private static final DefaultJWSVerifierFactory verifierFactory = new 
DefaultJWSVerifierFactory();
+
+  public JWTValidator(HiveConf conf) throws IOException, ParseException {
+    this.jwksProvider = new URLBasedJWKSProvider(conf);
+  }
+
+  public String validateJWTAndExtractUser(String signedJwt) throws 
ParseException, AuthenticationException {
+    final SignedJWT parsedJwt = SignedJWT.parse(signedJwt);
+    List<JWK> matchedJWKS = jwksProvider.getJWKs(parsedJwt.getHeader());
+
+    // verify signature
+    for (JWK matchedJWK : matchedJWKS) {
+      try {
+        JWSVerifier verifier = getVerifier(parsedJwt.getHeader(), matchedJWK);
+        if (parsedJwt.verify(verifier)) {
+          break;
+        }
+      } catch (JOSEException e) {
+        LOG.info("Failed to verify JWT {} by JWK {} because {}", 
parsedJwt.getHeader(), matchedJWK.getKeyID(),

Review comment:
       nit: Should the log level be `warn` ? 




-- 
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-unsubscr...@hive.apache.org

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


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

    Worklog Id:     (was: 738994)
    Time Spent: 2h 40m  (was: 2.5h)

> 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: 2h 40m
>  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)

Reply via email to