-
Notifications
You must be signed in to change notification settings - Fork 624
[LIVY-552][WIP] Add JWTFilter to validate incoming JWT tokens #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
server/src/main/scala/org/apache/livy/server/jwt/JWSVerifierProvider.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * 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.livy.server.jwt | ||
|
|
||
| import java.io.{ByteArrayInputStream, File, InputStream} | ||
| import java.security.cert.{CertificateFactory, X509Certificate} | ||
| import java.security.interfaces.RSAPublicKey | ||
|
|
||
| import com.nimbusds.jose.JWSVerifier | ||
| import com.nimbusds.jose.crypto.RSASSAVerifier | ||
| import org.apache.commons.io.FileUtils | ||
|
|
||
| import org.apache.livy.LivyConf | ||
| import org.apache.livy.LivyConf.JWT_SIGNATURE_PUBLIC_KEY_PATH | ||
|
|
||
| /** | ||
| * A provider of JWSVerifier instances based on the given LivyConf. | ||
| * @param livyConf Used to determine the public key path to use for the verifier | ||
| */ | ||
| class JWSVerifierProvider(livyConf: LivyConf) { | ||
|
|
||
| private[this] lazy val verifier = { | ||
| val publicKeyFile = livyConf.get(JWT_SIGNATURE_PUBLIC_KEY_PATH) | ||
| require(publicKeyFile != null, | ||
| s"${JWT_SIGNATURE_PUBLIC_KEY_PATH.key} must be set to verify JWT signatures.") | ||
| val publicKey = parseRSAPublicKey(publicKeyFile) | ||
| new RSASSAVerifier(publicKey) | ||
| } | ||
|
|
||
| def get(): JWSVerifier = verifier | ||
|
|
||
| private[this] def parseRSAPublicKey(publicKeyFile: String): RSAPublicKey = { | ||
| val is = new ByteArrayInputStream(FileUtils.readFileToByteArray( | ||
| new File(publicKeyFile))) | ||
| try { | ||
| parseRSAPublicKeyFromInputStream(is) | ||
| } finally { | ||
| is.close() | ||
| } | ||
| } | ||
|
|
||
| private[this] def parseRSAPublicKeyFromInputStream(inputStream: InputStream): RSAPublicKey = { | ||
| val factory = CertificateFactory.getInstance("X.509") | ||
| val certificate = factory.generateCertificate(inputStream).asInstanceOf[X509Certificate] | ||
| certificate.getPublicKey.asInstanceOf[RSAPublicKey] | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
server/src/main/scala/org/apache/livy/server/jwt/JWTFilter.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * 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.livy.server.jwt | ||
|
|
||
| import javax.servlet.{Filter, FilterChain, FilterConfig, ServletRequest, ServletResponse} | ||
| import javax.servlet.http.{HttpServletRequest, HttpServletResponse} | ||
|
|
||
| import scala.util.control.NonFatal | ||
|
|
||
| import org.apache.livy.{LivyConf, Logging} | ||
| import org.apache.livy.LivyConf.JWT_HEADER_NAME | ||
|
|
||
| /** | ||
| * A JWTFilter that utilizes a JWTValidator to determine if a JWT token included in a request | ||
| * is valid and accept/reject the request. | ||
| * @param jwtValidator validator used to determine the validity of the jwt token | ||
| */ | ||
| class JWTFilter(jwtValidator: JWTValidator, livyConf: LivyConf) extends Filter with Logging { | ||
|
|
||
| private val headerName = livyConf.get(JWT_HEADER_NAME) | ||
|
|
||
| override def init(filterConfig: FilterConfig): Unit = {} | ||
|
|
||
| override def destroy(): Unit = {} | ||
|
|
||
| override def doFilter(servletRequest: ServletRequest, | ||
| servletResponse: ServletResponse, | ||
| filterChain: FilterChain): Unit = { | ||
| val httpRequest = servletRequest.asInstanceOf[HttpServletRequest] | ||
| val httpServletResponse = servletResponse.asInstanceOf[HttpServletResponse] | ||
|
|
||
| val jwtToken = httpRequest.getHeader(headerName) | ||
| if (jwtToken == null) { | ||
| httpServletResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, | ||
| s"Header: ${headerName} is missing in the request.") | ||
| } else { | ||
| if (validateToken(jwtToken)) { | ||
| filterChain.doFilter(httpRequest, servletResponse) | ||
| } else { | ||
| httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, | ||
| "JWT token included in request failed validation.") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private def validateToken(jwtToken: String): Boolean = { | ||
| try { | ||
| jwtValidator.isValid(jwtToken) | ||
| } catch { | ||
| case NonFatal(e) => | ||
| warn("Exception while validating JWTToken", e) | ||
| false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| object JWTFilter { | ||
|
|
||
| def apply(livyConf: LivyConf): JWTFilter = { | ||
| val jwsVerifier = new JWSVerifierProvider(livyConf).get() | ||
| val jwtValidator = new JWTValidator(jwsVerifier) | ||
| new JWTFilter(jwtValidator, livyConf) | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
server/src/main/scala/org/apache/livy/server/jwt/JWTValidator.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * 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.livy.server.jwt | ||
|
|
||
| import java.util.Date | ||
|
|
||
| import com.nimbusds.jose.JWSVerifier | ||
| import com.nimbusds.jwt.SignedJWT | ||
|
|
||
| import org.apache.livy.Logging | ||
|
|
||
| /** | ||
| * A validator for JSON Web Tokens (JWT). | ||
| * @param jwsVerifier JWSVerifier used to verify the JWT token's signature | ||
| */ | ||
| class JWTValidator(jwsVerifier: JWSVerifier) extends Logging { | ||
|
|
||
| def isValid(token: String): Boolean = { | ||
| val signedJwt = SignedJWT.parse(token) | ||
|
|
||
| if (!isSignatureValid(signedJwt)) { | ||
| warn("Signature of JWT token could not be verified.") | ||
| false | ||
| } else if (!isExpired(signedJwt)) { | ||
| warn("Expiration time validation of JWT token failed.") | ||
| false | ||
| } else true | ||
| } | ||
|
|
||
| private def isSignatureValid(signedJWT: SignedJWT): Boolean = { | ||
| signedJWT.getSignature != null && signedJWT.verify(jwsVerifier) | ||
| } | ||
|
|
||
| private def isExpired(signedJWT: SignedJWT): Boolean = { | ||
| val expires = signedJWT.getJWTClaimsSet.getExpirationTime | ||
| expires == null || new Date().before(expires) | ||
| } | ||
| } |
73 changes: 73 additions & 0 deletions
73
server/src/test/scala/org/apache/livy/server/jwt/BaseJWTSpec.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * 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.livy.server.jwt | ||
|
|
||
| import java.security.interfaces.{RSAPrivateKey, RSAPublicKey} | ||
| import java.util.Date | ||
|
|
||
| import com.nimbusds.jose.{JWSAlgorithm, JWSHeader} | ||
| import com.nimbusds.jose.crypto.RSASSASigner | ||
| import com.nimbusds.jwt.{JWTClaimsSet, SignedJWT} | ||
| import org.joda.time.DateTime | ||
| import sun.security.tools.keytool.CertAndKeyGen | ||
| import sun.security.x509.X500Name | ||
|
|
||
| trait BaseJWTSpec { | ||
|
|
||
| private final val BIT_STRENGTH = 512 | ||
| private final val EXPIRATION = 60 * 60 * 24 // One Day | ||
| lazy val generator = createGenerator() | ||
| lazy val cert = generator.getSelfCertificate( | ||
| new X500Name("CN=LivyServer,O=Apache,L=ORG,C=DE"), EXPIRATION) | ||
| lazy val privateKey = generator.getPrivateKey.asInstanceOf[RSAPrivateKey] | ||
| lazy val publicKey = generator.getPublicKeyAnyway.asInstanceOf[RSAPublicKey] | ||
| lazy val signer = new RSASSASigner(privateKey) | ||
| lazy val unexpiredToken = createToken(new DateTime().plusDays(1), sign = true) | ||
| lazy val expiredToken = createToken(new DateTime().minusDays(1), sign = true) | ||
| lazy val unsignedToken = createToken(new DateTime().plusDays(1), sign = false) | ||
| lazy val unexpiredTokenWithNoExpiration = createToken(null, sign = true) | ||
|
|
||
| def createNewPublicKey(): RSAPublicKey = { | ||
| val newGenerator = createGenerator() | ||
| newGenerator.getPublicKeyAnyway.asInstanceOf[RSAPublicKey] | ||
| } | ||
|
|
||
| def createClaimsSet(issuer: String, expirationTime: Date): JWTClaimsSet = { | ||
| val builder = new JWTClaimsSet.Builder().issuer(issuer) | ||
| if (expirationTime != null) { | ||
| builder.expirationTime(expirationTime) | ||
| } | ||
| builder.build() | ||
| } | ||
|
|
||
| def createToken(expirationTime: DateTime, sign: Boolean): SignedJWT = { | ||
| val expiration = if (expirationTime == null) null else expirationTime.toDate | ||
| val signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.RS256), | ||
| createClaimsSet("jwt-test", expiration)) | ||
| if (sign) { | ||
| signedJWT.sign(signer) | ||
| } | ||
| signedJWT | ||
| } | ||
|
|
||
| private def createGenerator(): CertAndKeyGen = { | ||
| val newGenerator = new CertAndKeyGen("RSA", "SHA256WithRSA", null) | ||
| newGenerator.generate(BIT_STRENGTH) | ||
| newGenerator | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any chances we need to support multiple public keys for key rotation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean allowing users to specify a list of public keys to workaround having to restart livy when a particular key is rotated? I think it's an interesting idea, but would require other changes to work (currently the key is read in once from the path and not refreshed, this would require some refresh mechanism). Anyways, I think this can be a separate feature request/JIRA after this one gets in.