sourabh912 commented on a change in pull request #3006:
URL: https://github.com/apache/hive/pull/3006#discussion_r826430241
##########
File path:
service/src/java/org/apache/hive/service/cli/thrift/ThriftHttpServlet.java
##########
@@ -302,6 +310,27 @@ 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);
+ Preconditions.checkNotNull(user, "JWT needs to contain the user name as
subject");
+ Preconditions.checkState(!user.isEmpty(), "User name should not be
empty");
+ LOG.info("JWT verification successful for user {}", user);
+ } catch (Exception e) {
+ LOG.info("JWT verification failed", e);
Review comment:
nit: It should be LOG.error()
##########
File path: service/src/java/org/apache/hive/service/auth/jwt/JWTValidator.java
##########
@@ -0,0 +1,93 @@
+/*
+ * 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;
+
+public class JWTValidator {
Review comment:
nit: add some description about what this validator does?
##########
File path:
itests/hive-unit/src/test/java/org/apache/hive/service/auth/jwt/TestHttpJwtAuthentication.java
##########
@@ -0,0 +1,219 @@
+/*
+ * 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.github.tomakehurst.wiremock.junit.WireMockRule;
+import com.nimbusds.jose.JWSAlgorithm;
+import com.nimbusds.jose.JWSHeader;
+import com.nimbusds.jose.JWSSigner;
+import com.nimbusds.jose.crypto.RSASSASigner;
+import com.nimbusds.jose.jwk.RSAKey;
+import com.nimbusds.jwt.JWTClaimsSet;
+import com.nimbusds.jwt.SignedJWT;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
+import org.apache.hive.jdbc.HiveConnection;
+import org.apache.hive.jdbc.Utils;
+import org.apache.hive.jdbc.miniHS2.MiniHS2;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+
+import java.io.File;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.TimeUnit;
+
+import static com.github.tomakehurst.wiremock.client.WireMock.get;
+import static com.github.tomakehurst.wiremock.client.WireMock.ok;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+
+public class TestHttpJwtAuthentication {
+ private static final Map<String, String> DEFAULTS = new
HashMap<>(System.getenv());
+ private static Map<String, String> envMap;
+
+ private static final File jwtAuthorizedKeyFile =
+ new File("src/test/resources/auth.jwt/jwt-authorized-key.json");
+ private static final File jwtUnauthorizedKeyFile =
+ new File("src/test/resources/auth.jwt/jwt-unauthorized-key.json");
+ private static final File jwtVerificationJWKSFile =
+ new File("src/test/resources/auth.jwt/jwt-verification-jwks.json");
+
+ public static final String USER_1 = "USER_1";
+
+ private static MiniHS2 miniHS2;
+
+ private static final int MOCK_JWKS_SERVER_PORT = 8089;
+ @ClassRule
+ public static final WireMockRule MOCK_JWKS_SERVER = new
WireMockRule(MOCK_JWKS_SERVER_PORT);
+
+ @BeforeClass
+ public static void makeEnvModifiable() throws Exception {
Review comment:
what is the purpose of this method? Can't we simply set/unset env
variable `Utils.JdbcConnectionParams.AUTH_JWT_ENV` ?
##########
File path: service/src/java/org/apache/hive/service/auth/jwt/JWTValidator.java
##########
@@ -0,0 +1,93 @@
+/*
+ * 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;
+
+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 {
+ Preconditions.checkNotNull(jwksProvider);
+ final SignedJWT parsedJwt = SignedJWT.parse(signedJwt);
+ List<JWK> matchedJWKS = jwksProvider.getJWKs(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);
+ }
+ }
+ if (parsedJwt.getState() != JWSObject.State.VERIFIED) {
+ throw new AuthenticationException("Failed to verify JWT signature",
lastException);
+ }
+
+ // 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 {
+ Preconditions.checkArgument(jwk instanceof AsymmetricJWK, "Secret key is
not allowed.");
Review comment:
nit: would this be a better message?
```JWT signature verification with symmetric key is not allowed. ```
##########
File path: service/src/java/org/apache/hive/service/auth/jwt/JWTValidator.java
##########
@@ -0,0 +1,93 @@
+/*
+ * 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;
+
+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 {
+ Preconditions.checkNotNull(jwksProvider);
+ final SignedJWT parsedJwt = SignedJWT.parse(signedJwt);
+ List<JWK> matchedJWKS = jwksProvider.getJWKs(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);
+ }
+ }
+ if (parsedJwt.getState() != JWSObject.State.VERIFIED) {
+ throw new AuthenticationException("Failed to verify JWT signature",
lastException);
+ }
+
+ // 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 {
+ Preconditions.checkArgument(jwk instanceof AsymmetricJWK, "Secret key is
not allowed.");
Review comment:
Also we should document this somewhere. Atleast add it in method
description.
--
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]