Skip to content

Commit

Permalink
pac4j: fix incompatible dependencies + authorization regression (#15753)
Browse files Browse the repository at this point in the history
- After upgrading the pac4j version in: #15522. We were not able to access the druid ui. 
- Upgraded the Nimbus libraries version to a compatible version to pac4j.
- In the older pac4j version, when we return RedirectAction there we also update the webcontext Response status code and add the authentication URL to the header. But in the newer pac4j version, we just simply return the RedirectAction. So that's why it was not getting redirected to the generated authentication URL.
- To fix the above, I have updated the NOOP_HTTP_ACTION_ADAPTER to JEE_HTTP_ACTION_ADAPTER and it updates the HTTP Response in context as per the HTTP Action.
  • Loading branch information
Pankaj260100 authored Feb 1, 2024
1 parent 50bae96 commit 65857dc
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 17 deletions.
11 changes: 8 additions & 3 deletions extensions-core/druid-pac4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
<properties>
<pac4j.version>4.5.7</pac4j.version>

<!-- Following must be updated along with any updates to pac4j version -->
<!-- Following must be updated along with any updates to pac4j version. One can find the compatible version of nimbus libraries in org.pac4j:pac4j-oidc dependencies-->
<nimbus.lang.tag.version>1.7</nimbus.lang.tag.version>
<nimbus.jose.jwt.version>7.9</nimbus.jose.jwt.version>
<oauth2.oidc.sdk.version>6.5</oauth2.oidc.sdk.version>
<nimbus.jose.jwt.version>8.22.1</nimbus.jose.jwt.version>
<oauth2.oidc.sdk.version>8.22</oauth2.oidc.sdk.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -145,6 +145,11 @@
<artifactId>easymock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
import org.pac4j.core.engine.DefaultCallbackLogic;
import org.pac4j.core.engine.DefaultSecurityLogic;
import org.pac4j.core.engine.SecurityLogic;
import org.pac4j.core.exception.http.HttpAction;
import org.pac4j.core.http.adapter.HttpActionAdapter;
import org.pac4j.core.http.adapter.JEEHttpActionAdapter;
import org.pac4j.core.profile.UserProfile;

import javax.servlet.Filter;
Expand All @@ -48,11 +47,9 @@ public class Pac4jFilter implements Filter
{
private static final Logger LOGGER = new Logger(Pac4jFilter.class);

private static final HttpActionAdapter<String, JEEContext> NOOP_HTTP_ACTION_ADAPTER = (HttpAction code, JEEContext ctx) -> null;

private final Config pac4jConfig;
private final SecurityLogic<String, JEEContext> securityLogic;
private final CallbackLogic<String, JEEContext> callbackLogic;
private final SecurityLogic<Object, JEEContext> securityLogic;
private final CallbackLogic<Object, JEEContext> callbackLogic;
private final SessionStore<JEEContext> sessionStore;

private final String name;
Expand Down Expand Up @@ -95,11 +92,11 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
callbackLogic.perform(
context,
pac4jConfig,
NOOP_HTTP_ACTION_ADAPTER,
JEEHttpActionAdapter.INSTANCE,
"/",
true, false, false, null);
} else {
String uid = securityLogic.perform(
Object uid = securityLogic.perform(
context,
pac4jConfig,
(JEEContext ctx, Collection<UserProfile> profiles, Object... parameters) -> {
Expand All @@ -110,11 +107,13 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
return profiles.iterator().next().getId();
}
},
NOOP_HTTP_ACTION_ADAPTER,
null, null, null, null);

JEEHttpActionAdapter.INSTANCE,
null, "none", null, null);
// Changed the Authorizer from null to "none".
// In the older version, if it is null, it simply grant access and returns authorized.
// But in the newer pac4j version, it uses CsrfAuthorizer as default, And because of this, It was returning 403 in API calls.
if (uid != null) {
AuthenticationResult authenticationResult = new AuthenticationResult(uid, authorizerName, name, null);
AuthenticationResult authenticationResult = new AuthenticationResult(uid.toString(), authorizerName, name, null);
servletRequest.setAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT, authenticationResult);
filterChain.doFilter(servletRequest, servletResponse);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.druid.security.pac4j;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.pac4j.core.context.JEEContext;
import org.pac4j.core.exception.http.ForbiddenAction;
import org.pac4j.core.exception.http.FoundAction;
import org.pac4j.core.exception.http.HttpAction;
import org.pac4j.core.exception.http.WithLocationAction;
import org.pac4j.core.http.adapter.JEEHttpActionAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.mockito.ArgumentMatchers.any;

@RunWith(MockitoJUnitRunner.class)
public class Pac4jFilterTest
{

@Mock
private HttpServletRequest request;
@Mock
private HttpServletResponse response;
private JEEContext context;

@Before
public void setUp()
{
context = new JEEContext(request, response);
}

@Test
public void testActionAdapterForRedirection()
{
HttpAction httpAction = new FoundAction("testUrl");
Mockito.doReturn(httpAction.getCode()).when(response).getStatus();
Mockito.doReturn(((WithLocationAction) httpAction).getLocation()).when(response).getHeader(any());
JEEHttpActionAdapter.INSTANCE.adapt(httpAction, context);
Assert.assertEquals(response.getStatus(), 302);
Assert.assertEquals(response.getHeader("Location"), "testUrl");
}

@Test
public void testActionAdapterForForbidden()
{
HttpAction httpAction = ForbiddenAction.INSTANCE;
Mockito.doReturn(httpAction.getCode()).when(response).getStatus();
JEEHttpActionAdapter.INSTANCE.adapt(httpAction, context);
Assert.assertEquals(response.getStatus(), HttpServletResponse.SC_FORBIDDEN);
}

}
14 changes: 12 additions & 2 deletions licenses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -809,17 +809,27 @@ name: com.nimbusds nimbus-jose-jwt
license_category: binary
module: extensions/druid-pac4j
license_name: Apache License version 2.0
version: 7.9
version: 8.22.1
libraries:
- com.nimbusds: nimbus-jose-jwt

---

name: com.nimbusds content-type
license_category: binary
module: extensions/druid-pac4j
license_name: Apache License version 2.0
version: 2.1
libraries:
- com.nimbusds: content-type

---

name: com.nimbusds oauth2-oidc-sdk
license_category: binary
module: extensions/druid-pac4j
license_name: Apache License version 2.0
version: 6.5
version: 8.22
libraries:
- com.nimbusds: oauth2-oidc-sdk

Expand Down

0 comments on commit 65857dc

Please sign in to comment.