-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NIFI-12813 Corrected Username handling in HTTP Request Log
- Corrected Jetty AuthenticationState interface reference for authenticated user attribute mapping - Added unit test verifying expected attribute values Signed-off-by: Pierre Villard <pierre.villard.fr@gmail.com> This closes #8423.
- Loading branch information
1 parent
2007d79
commit 9a81f66
Showing
2 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
93 changes: 93 additions & 0 deletions
93
...i-jetty/src/test/java/org/apache/nifi/web/server/log/RequestAuthenticationFilterTest.java
This file contains 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,93 @@ | ||
/* | ||
* 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.nifi.web.server.log; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.apache.nifi.web.security.log.AuthenticationUserAttribute; | ||
import org.eclipse.jetty.server.Request; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Captor; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.io.IOException; | ||
import java.security.Principal; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class RequestAuthenticationFilterTest { | ||
|
||
private static final String USERNAME = UserPrincipal.class.getSimpleName(); | ||
|
||
private static final String AUTHENTICATION_STATE = Request.AuthenticationState.class.getName(); | ||
|
||
@Mock | ||
HttpServletRequest request; | ||
|
||
@Mock | ||
HttpServletResponse response; | ||
|
||
@Mock | ||
FilterChain filterChain; | ||
|
||
@Captor | ||
ArgumentCaptor<Request.AuthenticationState> authenticationStateCaptor; | ||
|
||
private RequestAuthenticationFilter filter; | ||
|
||
@BeforeEach | ||
void setFilter() { | ||
filter = new RequestAuthenticationFilter(); | ||
} | ||
|
||
@Test | ||
void testDoFilterInternalUsernameNotFound() throws ServletException, IOException { | ||
when(request.getAttribute(eq(AuthenticationUserAttribute.USERNAME.getName()))).thenReturn(null); | ||
|
||
filter.doFilterInternal(request, response, filterChain); | ||
|
||
verify(request).getRemoteAddr(); | ||
verifyNoMoreInteractions(request); | ||
} | ||
|
||
@Test | ||
void testDoFilterInternalUsernameFound() throws ServletException, IOException { | ||
when(request.getAttribute(eq(AuthenticationUserAttribute.USERNAME.getName()))).thenReturn(USERNAME); | ||
|
||
filter.doFilterInternal(request, response, filterChain); | ||
|
||
verify(request).setAttribute(eq(AUTHENTICATION_STATE), authenticationStateCaptor.capture()); | ||
|
||
final Request.AuthenticationState authenticationState = authenticationStateCaptor.getValue(); | ||
assertNotNull(authenticationState); | ||
final Principal principal = authenticationState.getUserPrincipal(); | ||
assertNotNull(principal); | ||
assertEquals(USERNAME, principal.getName()); | ||
} | ||
} |