Skip to content

Commit 7a2d2da

Browse files
authored
RANGER-5479: Remove dependency on hadoop libs in ranger-authn (#836)
1 parent 98af26f commit 7a2d2da

5 files changed

Lines changed: 16 additions & 37 deletions

File tree

ranger-authn/pom.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,6 @@
6565
<version>${commons.text.version}</version>
6666
</dependency>
6767

68-
<dependency>
69-
<groupId>org.apache.hadoop</groupId>
70-
<artifactId>hadoop-client-api</artifactId>
71-
<version>${hadoop.version}</version>
72-
</dependency>
73-
74-
<dependency>
75-
<groupId>org.apache.hadoop</groupId>
76-
<artifactId>hadoop-client-runtime</artifactId>
77-
<version>${hadoop.version}</version>
78-
</dependency>
79-
8068
<dependency>
8169
<groupId>org.slf4j</groupId>
8270
<artifactId>slf4j-api</artifactId>

ranger-authn/src/main/java/org/apache/ranger/authz/handler/RangerAuth.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818
*/
1919
package org.apache.ranger.authz.handler;
2020

21-
import org.apache.hadoop.security.authentication.server.AuthenticationToken;
22-
2321
public class RangerAuth {
2422
private String userName;
2523
private AuthType type;
2624
private boolean isAuthenticated;
2725

28-
public RangerAuth(final AuthenticationToken authenticationToken, AuthType type) {
29-
this.userName = authenticationToken.getName();
26+
public RangerAuth(String username, AuthType type) {
27+
this.userName = username;
3028
this.isAuthenticated = true;
3129
this.type = type;
3230
}

ranger-authn/src/main/java/org/apache/ranger/authz/handler/jwt/RangerDefaultJwtAuthHandler.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.nimbusds.jwt.proc.DefaultJWTProcessor;
2626
import com.nimbusds.jwt.proc.JWTClaimsSetVerifier;
2727
import org.apache.commons.lang3.StringUtils;
28-
import org.apache.hadoop.security.authentication.server.AuthenticationToken;
2928
import org.apache.ranger.authz.handler.RangerAuth;
3029

3130
import javax.servlet.ServletRequest;
@@ -82,11 +81,10 @@ public RangerAuth authenticate(HttpServletRequest httpServletRequest) {
8281
String jwtAuthHeaderStr = getJwtAuthHeader(httpServletRequest);
8382
String jwtCookieStr = StringUtils.isBlank(jwtAuthHeaderStr) ? getJwtCookie(httpServletRequest) : null;
8483
String doAsUser = httpServletRequest.getParameter(DO_AS_PARAMETER);
84+
String username = authenticate(jwtAuthHeaderStr, jwtCookieStr, doAsUser);
8585

86-
AuthenticationToken authenticationToken = authenticate(jwtAuthHeaderStr, jwtCookieStr, doAsUser);
87-
88-
if (authenticationToken != null) {
89-
rangerAuth = new RangerAuth(authenticationToken, RangerAuth.AuthType.JWT_JWKS);
86+
if (username != null) {
87+
rangerAuth = new RangerAuth(username, RangerAuth.AuthType.JWT_JWKS);
9088
}
9189

9290
return rangerAuth;

ranger-authn/src/main/java/org/apache/ranger/authz/handler/jwt/RangerJwtAuthHandler.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@
2222
import com.nimbusds.jose.JWSObject;
2323
import com.nimbusds.jose.JWSVerifier;
2424
import com.nimbusds.jose.crypto.RSASSAVerifier;
25+
import com.nimbusds.jose.jwk.RSAKey;
2526
import com.nimbusds.jose.jwk.source.JWKSource;
2627
import com.nimbusds.jose.jwk.source.RemoteJWKSet;
2728
import com.nimbusds.jose.proc.BadJOSEException;
2829
import com.nimbusds.jose.proc.JWSKeySelector;
2930
import com.nimbusds.jose.proc.JWSVerificationKeySelector;
3031
import com.nimbusds.jose.proc.SecurityContext;
32+
import com.nimbusds.jose.util.X509CertUtils;
3133
import com.nimbusds.jwt.SignedJWT;
3234
import com.nimbusds.jwt.proc.ConfigurableJWTProcessor;
3335
import org.apache.commons.lang3.StringUtils;
34-
import org.apache.hadoop.security.authentication.server.AuthenticationToken;
35-
import org.apache.hadoop.security.authentication.util.CertificateUtil;
3636
import org.apache.ranger.authz.handler.RangerAuthHandler;
3737
import org.slf4j.Logger;
3838
import org.slf4j.LoggerFactory;
@@ -82,7 +82,7 @@ public void initialize(final Properties config) throws Exception {
8282

8383
// setup JWT provider public key if configured
8484
if (StringUtils.isNotBlank(pemPublicKey)) {
85-
verifier = new RSASSAVerifier(CertificateUtil.parseRSAPublicKey(pemPublicKey));
85+
verifier = new RSASSAVerifier(RSAKey.parse(X509CertUtils.parse(pemPublicKey)));
8686
} else if (StringUtils.isBlank(jwksProviderUrl)) {
8787
throw new Exception("RangerJwtAuthHandler: Mandatory configs ('jwks.provider-url' & 'jwt.public-key') are missing, must provide atleast one.");
8888
}
@@ -106,12 +106,11 @@ public void initialize(final Properties config) throws Exception {
106106

107107
public abstract ConfigurableJWTProcessor<SecurityContext> getJwtProcessor(JWSKeySelector<SecurityContext> keySelector);
108108

109-
protected AuthenticationToken authenticate(final String jwtAuthHeader, final String jwtCookie, final String doAsUser) {
109+
protected String authenticate(final String jwtAuthHeader, final String jwtCookie, final String doAsUser) {
110110
if (LOG.isDebugEnabled()) {
111111
LOG.debug("===>>> RangerJwtAuthHandler.authenticate()");
112112
}
113113

114-
AuthenticationToken token = null;
115114
if (shouldProceedAuth(jwtAuthHeader, jwtCookie)) {
116115
String serializedJWT = getJWT(jwtAuthHeader, jwtCookie);
117116

@@ -132,7 +131,7 @@ protected AuthenticationToken authenticate(final String jwtAuthHeader, final Str
132131
LOG.debug("RangerJwtAuthHandler.authenticate(): Issuing AuthenticationToken for user: [{}]", userName);
133132
LOG.debug("RangerJwtAuthHandler.authenticate(): Authentication successful for user [{}] and doAs user is [{}]", jwtToken.getJWTClaimsSet().getSubject(), doAsUser);
134133
}
135-
token = new AuthenticationToken(userName, userName, TYPE);
134+
return userName;
136135
} else {
137136
LOG.warn("RangerJwtAuthHandler.authenticate(): Validation failed for JWT token: [{}] ", jwtToken.serialize());
138137
}
@@ -148,7 +147,7 @@ protected AuthenticationToken authenticate(final String jwtAuthHeader, final Str
148147
LOG.debug("<<<=== RangerJwtAuthHandler.authenticate()");
149148
}
150149

151-
return token;
150+
return null;
152151
}
153152

154153
protected String getJWT(final String jwtAuthHeader, final String jwtCookie) {
@@ -267,13 +266,11 @@ protected boolean validateAudiences(final SignedJWT jwtToken) {
267266
boolean valid = false;
268267
try {
269268
List<String> tokenAudienceList = jwtToken.getJWTClaimsSet().getAudience();
270-
// if there were no expected audiences configured then just
271-
// consider any audience acceptable
269+
// if there were no expected audiences configured then just consider any audience acceptable
272270
if (audiences == null) {
273271
valid = true;
274272
} else {
275-
// if any of the configured audiences is found then consider it
276-
// acceptable
273+
// if any of the configured audiences is found then consider it acceptable
277274
for (String aud : tokenAudienceList) {
278275
if (audiences.contains(aud)) {
279276
if (LOG.isDebugEnabled()) {
@@ -294,8 +291,8 @@ protected boolean validateAudiences(final SignedJWT jwtToken) {
294291
}
295292

296293
/**
297-
* Validate that the expiration time of the JWT token has not been violated. If
298-
* it has then throw an AuthenticationException. Override this method in
294+
* Validate that the expiration time of the JWT has not been violated. If
295+
* it has, then throw an AuthenticationException. Override this method in
299296
* subclasses in order to customize the expiration validation behavior.
300297
*
301298
* @param jwtToken the token that contains the expiration date to validate

security-admin/src/test/java/org/apache/ranger/security/web/filter/TestRangerJwtAuthFilter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.apache.ranger.security.web.filter;
2020

21-
import org.apache.hadoop.security.authentication.server.AuthenticationToken;
2221
import org.apache.ranger.authz.handler.RangerAuth;
2322
import org.junit.jupiter.api.AfterEach;
2423
import org.junit.jupiter.api.MethodOrderer;
@@ -89,8 +88,7 @@ public void testDoFilter_setsAuthenticationWhenAuthenticateSucceeds() throws Ser
8988
ServletResponse res = Mockito.mock(ServletResponse.class);
9089
FilterChain chain = Mockito.mock(FilterChain.class);
9190

92-
AuthenticationToken token = new AuthenticationToken("alice", "alice", "ranger-jwt");
93-
RangerAuth rangerAuth = new RangerAuth(token, RangerAuth.AuthType.JWT_JWKS);
91+
RangerAuth rangerAuth = new RangerAuth("alice", RangerAuth.AuthType.JWT_JWKS);
9492

9593
doReturn(rangerAuth).when(filter).authenticate(any(HttpServletRequest.class));
9694

0 commit comments

Comments
 (0)