Skip to content

Commit 94cc562

Browse files
author
Vladimir Kotal
committed
add test to cover configuration listener
1 parent 1c441e8 commit 94cc562

File tree

2 files changed

+64
-19
lines changed

2 files changed

+64
-19
lines changed

opengrok-web/src/main/java/org/opengrok/web/api/v1/filter/IncomingFilter.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
@PreMatching
6060
public class IncomingFilter implements ContainerRequestFilter, ConfigurationChangedListener {
6161

62-
private static final Logger logger = LoggerFactory.getLogger(IncomingFilter.class);
62+
private static final Logger LOGGER = LoggerFactory.getLogger(IncomingFilter.class);
6363

6464
/**
6565
* Endpoint paths that are exempted from this filter.
@@ -99,7 +99,7 @@ public void init() {
9999
localAddresses.add(inetAddress.getHostAddress());
100100
}
101101
} catch (IOException e) {
102-
logger.log(Level.SEVERE, "Could not get localhost addresses", e);
102+
LOGGER.log(Level.SEVERE, "Could not get localhost addresses", e);
103103
}
104104

105105
// Cache the tokens to avoid locking.
@@ -110,6 +110,7 @@ public void init() {
110110

111111
@Override
112112
public void onConfigurationChanged() {
113+
LOGGER.log(Level.FINER, "refreshing token cache");
113114
setTokens(RuntimeEnvironment.getInstance().getAuthenticationTokens());
114115
}
115116

@@ -126,28 +127,28 @@ public void filter(final ContainerRequestContext context) {
126127
if (authHeaderValue != null && authHeaderValue.startsWith(BEARER)) {
127128
String tokenValue = authHeaderValue.substring(BEARER.length());
128129
if (getTokens().contains(tokenValue)) {
129-
logger.log(Level.FINEST, "allowing request to {0} based on authentication header token", path);
130+
LOGGER.log(Level.FINEST, "allowing request to {0} based on authentication header token", path);
130131
return;
131132
}
132133
}
133134
}
134135

135136
if (allowedPaths.contains(path)) {
136-
logger.log(Level.FINEST, "allowing request to {0} based on whitelisted path", path);
137+
LOGGER.log(Level.FINEST, "allowing request to {0} based on whitelisted path", path);
137138
return;
138139
}
139140

140141
// In a reverse proxy environment the connection appears to be coming from localhost.
141142
// These request should really be using tokens.
142143
if (request.getHeader("X-Forwarded-For") != null || request.getHeader("Forwarded") != null) {
143-
logger.log(Level.FINEST, "denying request to {0} due to existence of forwarded header in the request",
144+
LOGGER.log(Level.FINEST, "denying request to {0} due to existence of forwarded header in the request",
144145
path);
145146
context.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
146147
return;
147148
}
148149

149150
if (localAddresses.contains(request.getRemoteAddr())) {
150-
logger.log(Level.FINEST, "allowing request to {0} based on localhost IP address", path);
151+
LOGGER.log(Level.FINEST, "allowing request to {0} based on localhost IP address", path);
151152
return;
152153
}
153154

opengrok-web/src/test/java/org/opengrok/web/api/v1/filter/IncomingFilterTest.java

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.web.api.v1.filter;
2424

25+
import org.junit.Before;
2526
import org.junit.Test;
2627
import org.mockito.ArgumentCaptor;
28+
import org.opengrok.indexer.configuration.CommandTimeoutType;
2729
import org.opengrok.indexer.configuration.RuntimeEnvironment;
2830

2931
import javax.servlet.http.HttpServletRequest;
@@ -39,32 +41,74 @@
3941
import java.util.TreeMap;
4042

4143
import static org.junit.Assert.assertEquals;
42-
import static org.mockito.Mockito.mock;
43-
import static org.mockito.Mockito.never;
44-
import static org.mockito.Mockito.verify;
45-
import static org.mockito.Mockito.when;
44+
import static org.junit.jupiter.api.Assertions.assertTrue;
45+
import static org.mockito.Mockito.*;
4646

4747
public class IncomingFilterTest {
48+
@Before
49+
public void beforeTest() {
50+
RuntimeEnvironment.getInstance().setAuthenticationTokens(new HashSet<>());
51+
}
52+
4853
@Test
4954
public void nonLocalhostTestWithValidToken() throws Exception {
50-
nonLocalhostTestWithToken(true);
55+
String allowedToken = "foo";
56+
57+
Set<String> tokens = new HashSet<>();
58+
tokens.add(allowedToken);
59+
RuntimeEnvironment.getInstance().setAuthenticationTokens(tokens);
60+
61+
nonLocalhostTestWithToken(true, allowedToken);
5162
}
5263

5364
@Test
5465
public void nonLocalhostTestWithInvalidToken() throws Exception {
55-
nonLocalhostTestWithToken(false);
56-
}
57-
58-
private void nonLocalhostTestWithToken(boolean allowed) throws Exception {
59-
String allowedToken = "foo";
66+
String allowedToken = "bar";
6067

6168
Set<String> tokens = new HashSet<>();
6269
tokens.add(allowedToken);
6370
RuntimeEnvironment.getInstance().setAuthenticationTokens(tokens);
6471

72+
nonLocalhostTestWithToken(false, allowedToken + "_");
73+
}
74+
75+
@Test
76+
public void nonLocalhostTestWithTokenChange() throws Exception {
77+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
78+
79+
String token = "foobar";
80+
81+
Map<String, String> headers = new TreeMap<>();
82+
final String authHeaderValue = IncomingFilter.BEARER + token;
83+
headers.put(HttpHeaders.AUTHORIZATION, authHeaderValue);
84+
assertTrue(env.getAuthenticationTokens().isEmpty());
85+
IncomingFilter filter = mockWithRemoteAddress("192.168.1.1", headers, true);
86+
87+
ContainerRequestContext context = mockContainerRequestContext("test");
88+
ArgumentCaptor<Response> captor = ArgumentCaptor.forClass(Response.class);
89+
90+
// No tokens configured.
91+
filter.filter(context);
92+
verify(context).abortWith(captor.capture());
93+
94+
// Setting tokens without refreshing configuration should have no effect.
95+
Set<String> tokens = new HashSet<>();
96+
tokens.add(token);
97+
env.setAuthenticationTokens(tokens);
98+
filter.filter(context);
99+
verify(context, times(2)).abortWith(captor.capture());
100+
101+
// The request should pass only after applyConfig().
102+
env.applyConfig(false, CommandTimeoutType.RESTFUL);
103+
context = mockContainerRequestContext("test");
104+
filter.filter(context);
105+
verify(context, never()).abortWith(captor.capture());
106+
}
107+
108+
private void nonLocalhostTestWithToken(boolean allowed, String token) throws Exception {
65109
Map<String, String> headers = new TreeMap<>();
66-
final String authHeaderValue = IncomingFilter.BEARER + allowedToken;
67-
headers.put(HttpHeaders.AUTHORIZATION, allowed ? authHeaderValue : authHeaderValue + "_");
110+
final String authHeaderValue = IncomingFilter.BEARER + token;
111+
headers.put(HttpHeaders.AUTHORIZATION, authHeaderValue);
68112
IncomingFilter filter = mockWithRemoteAddress("192.168.1.1", headers, true);
69113

70114
ContainerRequestContext context = mockContainerRequestContext("test");

0 commit comments

Comments
 (0)