diff --git a/extensions-core/druid-pac4j/pom.xml b/extensions-core/druid-pac4j/pom.xml index 403639021aff..0628dd07969c 100644 --- a/extensions-core/druid-pac4j/pom.xml +++ b/extensions-core/druid-pac4j/pom.xml @@ -36,10 +36,10 @@ 4.5.7 - + 1.7 - 7.9 - 6.5 + 8.22.1 + 8.22 @@ -145,6 +145,11 @@ easymock test + + org.mockito + mockito-core + test + diff --git a/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/Pac4jFilter.java b/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/Pac4jFilter.java index 452a22609460..0495242835c4 100644 --- a/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/Pac4jFilter.java +++ b/extensions-core/druid-pac4j/src/main/java/org/apache/druid/security/pac4j/Pac4jFilter.java @@ -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; @@ -48,11 +47,9 @@ public class Pac4jFilter implements Filter { private static final Logger LOGGER = new Logger(Pac4jFilter.class); - private static final HttpActionAdapter NOOP_HTTP_ACTION_ADAPTER = (HttpAction code, JEEContext ctx) -> null; - private final Config pac4jConfig; - private final SecurityLogic securityLogic; - private final CallbackLogic callbackLogic; + private final SecurityLogic securityLogic; + private final CallbackLogic callbackLogic; private final SessionStore sessionStore; private final String name; @@ -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 profiles, Object... parameters) -> { @@ -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); } diff --git a/extensions-core/druid-pac4j/src/test/java/org/apache/druid/security/pac4j/Pac4jFilterTest.java b/extensions-core/druid-pac4j/src/test/java/org/apache/druid/security/pac4j/Pac4jFilterTest.java new file mode 100644 index 000000000000..0523c970178d --- /dev/null +++ b/extensions-core/druid-pac4j/src/test/java/org/apache/druid/security/pac4j/Pac4jFilterTest.java @@ -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); + } + +} diff --git a/licenses.yaml b/licenses.yaml index 940e7cf2a49e..48df6b1a157c 100644 --- a/licenses.yaml +++ b/licenses.yaml @@ -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