sourabh912 commented on a change in pull request #3006:
URL: https://github.com/apache/hive/pull/3006#discussion_r832664780



##########
File path: service/src/java/org/apache/hive/service/auth/jwt/JWTValidator.java
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.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;
+
+/**
+ * This class is used to validate JWT. JWKS is fetched during instantiation 
and kept in the memory.
+ * We disallow JWT signature verification with symmetric key, because that 
means anyone can get the same key
+ * and use it to sign a JWT.
+ */
+public class JWTValidator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(JWTValidator.class.getName());
+  private static final DefaultJWSVerifierFactory JWS_VERIFIER_FACTORY = new 
DefaultJWSVerifierFactory();
+
+  private final URLBasedJWKSProvider jwksProvider;
+
+  public JWTValidator(HiveConf conf) throws IOException, ParseException {
+    this.jwksProvider = new URLBasedJWKSProvider(conf);
+  }
+
+  public String validateJWTAndExtractUser(String signedJwt) throws 
ParseException, AuthenticationException {
+    Preconditions.checkNotNull(jwksProvider);
+    Preconditions.checkNotNull(signedJwt, "No token found");
+    final SignedJWT parsedJwt = SignedJWT.parse(signedJwt);
+    List<JWK> matchedJWKS = jwksProvider.getJWKs(parsedJwt.getHeader());
+    if (matchedJWKS.isEmpty()) {
+      throw new AuthenticationException("Failed to find matched JWKs with the 
JWT header: " + parsedJwt.getHeader());
+    }
+
+    // verify signature
+    Exception lastException = null;
+    for (JWK matchedJWK : matchedJWKS) {
+      try {
+        JWSVerifier verifier = getVerifier(parsedJwt.getHeader(), matchedJWK);
+        if (parsedJwt.verify(verifier)) {
+          break;

Review comment:
       nit: would be good to add a debug log with with matchedJWK's and 
signedJWT's basic info.

##########
File path: service/src/java/org/apache/hive/service/auth/jwt/JWTValidator.java
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.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;
+
+/**
+ * This class is used to validate JWT. JWKS is fetched during instantiation 
and kept in the memory.
+ * We disallow JWT signature verification with symmetric key, because that 
means anyone can get the same key
+ * and use it to sign a JWT.
+ */
+public class JWTValidator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(JWTValidator.class.getName());
+  private static final DefaultJWSVerifierFactory JWS_VERIFIER_FACTORY = new 
DefaultJWSVerifierFactory();
+
+  private final URLBasedJWKSProvider jwksProvider;
+
+  public JWTValidator(HiveConf conf) throws IOException, ParseException {
+    this.jwksProvider = new URLBasedJWKSProvider(conf);
+  }
+
+  public String validateJWTAndExtractUser(String signedJwt) throws 
ParseException, AuthenticationException {
+    Preconditions.checkNotNull(jwksProvider);
+    Preconditions.checkNotNull(signedJwt, "No token found");
+    final SignedJWT parsedJwt = SignedJWT.parse(signedJwt);
+    List<JWK> matchedJWKS = jwksProvider.getJWKs(parsedJwt.getHeader());
+    if (matchedJWKS.isEmpty()) {
+      throw new AuthenticationException("Failed to find matched JWKs with the 
JWT header: " + parsedJwt.getHeader());
+    }
+
+    // verify signature
+    Exception lastException = null;
+    for (JWK matchedJWK : matchedJWKS) {
+      try {
+        JWSVerifier verifier = getVerifier(parsedJwt.getHeader(), matchedJWK);
+        if (parsedJwt.verify(verifier)) {
+          break;
+        }
+      } catch (Exception e) {
+        lastException = e;
+        LOG.warn("Failed to verify JWT {} by JWK {}", parsedJwt.getPayload(), 
matchedJWK, e);

Review comment:
       nit: `matchedJWK` toString() would print a lot of information. Assuming 
that signed JWT would only match to one of matchedJWKS, I think we should only 
log basic info instead of the whole string like algo, keyId (whatever the 
matching criteria was). Otherwise the log file would be cluttered with lots of 
repeated logs. 

##########
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:
       @hsnusonic @kgyrtkirk @nrg4878 : I will file a JIRA so that we can 
investigate it. Let me know if you have any thoughts. 




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to