-
Notifications
You must be signed in to change notification settings - Fork 1.1k
RANGER-5533:Verify JWT Issuer Claims if present #901
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
Open
ChinmayHegde24
wants to merge
5
commits into
apache:master
Choose a base branch
from
ChinmayHegde24:RANGER-5533
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c85fa10
RANGER-5533:Validating ISS claim in JWT
ChinmayHegde24 5dd1ce0
RANGER-5533: Refactor JWT issuer validation to support single-issuer …
ChinmayHegde24 ca45df4
RANGER-5533: Addressed review comments
ChinmayHegde24 13bb8db
RANGER-5533 : Unit test for RangerJwtAuthHandler
ChinmayHegde24 ddd267d
RANGER-5533: Removed redundant test case from TestRangerSSOAuthentica…
ChinmayHegde24 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
109 changes: 109 additions & 0 deletions
109
ranger-authn/src/test/java/org/apache/ranger/authz/handler/jwt/TestRangerJwtAuthHandler.java
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,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.ranger.authz.handler.jwt; | ||
|
|
||
| import com.nimbusds.jose.JWSAlgorithm; | ||
| import com.nimbusds.jose.JWSHeader; | ||
| import com.nimbusds.jose.proc.JWSKeySelector; | ||
| import com.nimbusds.jose.proc.SecurityContext; | ||
| import com.nimbusds.jwt.JWTClaimsSet; | ||
| import com.nimbusds.jwt.SignedJWT; | ||
| import com.nimbusds.jwt.proc.ConfigurableJWTProcessor; | ||
| import org.apache.ranger.authz.handler.RangerAuth; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import javax.servlet.http.HttpServletRequest; | ||
|
|
||
| import java.util.Date; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| public class TestRangerJwtAuthHandler { | ||
| static class TestHandler extends RangerJwtAuthHandler { | ||
| @Override | ||
| public ConfigurableJWTProcessor<SecurityContext> getJwtProcessor(JWSKeySelector<SecurityContext> keySelector) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public RangerAuth authenticate(HttpServletRequest request) { | ||
| return null; | ||
| } | ||
|
|
||
| boolean callValidateIssuer(SignedJWT jwt) { | ||
| return validateIssuer(jwt); | ||
| } | ||
| } | ||
|
|
||
| private static SignedJWT jwtWithIssuer(String issuer) { | ||
| JWTClaimsSet claims = new JWTClaimsSet.Builder() | ||
| .issuer(issuer) | ||
| .subject("user") | ||
| .expirationTime(new Date(System.currentTimeMillis() + 60_000)) | ||
| .build(); | ||
|
|
||
| // Header alg value doesn't matter for validateIssuer() | ||
| return new SignedJWT(new JWSHeader(JWSAlgorithm.RS256), claims); | ||
| } | ||
|
|
||
| @Test | ||
| void validateIssuerTrue_whenIssuerNotConfigured() { | ||
| TestHandler handler = new TestHandler(); | ||
| handler.issuer = null; // StringUtils.isBlank(null) => true | ||
|
|
||
| SignedJWT jwt = jwtWithIssuer("any-issuer"); | ||
|
|
||
| assertTrue(handler.callValidateIssuer(jwt)); | ||
| } | ||
|
|
||
| @Test | ||
| void validateIssuerTrue_whenIssuerMatches() { | ||
| TestHandler handler = new TestHandler(); | ||
| handler.issuer = "expected-issuer"; | ||
|
|
||
| SignedJWT jwt = jwtWithIssuer("expected-issuer"); | ||
|
|
||
| assertTrue(handler.callValidateIssuer(jwt)); | ||
| } | ||
|
|
||
| @Test | ||
| void validateIssuerFalse_whenIssuerDoesNotMatch() { | ||
| TestHandler handler = new TestHandler(); | ||
| handler.issuer = "expected-issuer"; | ||
|
|
||
| SignedJWT jwt = jwtWithIssuer("different-issuer"); | ||
|
|
||
| assertFalse(handler.callValidateIssuer(jwt)); | ||
| } | ||
|
|
||
| @Test | ||
| void validateIssuerFalse_whenJwtClaimsCannotBeParsed() throws Exception { | ||
| TestHandler handler = new TestHandler(); | ||
| handler.issuer = "expected-issuer"; | ||
|
|
||
| String header = "eyJhbGciOiJIUzI1NiJ9"; // Header: {"alg":"HS256"} (valid JWS header for SignedJWT) | ||
| String payload = "buyevwv678"; // Payload: "not-json" (NOT a JSON object => getJWTClaimsSet() will throw ParseException) | ||
| String signature = "abcd"; // Signature: "sig" (any base64url string works for parsing) | ||
|
|
||
| SignedJWT badJwt = SignedJWT.parse(header + "." + payload + "." + signature); | ||
|
|
||
| assertFalse(handler.callValidateIssuer(badJwt)); | ||
| } | ||
| } |
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
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.