|
18 | 18 | */
|
19 | 19 |
|
20 | 20 | /*
|
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. |
22 | 22 | */
|
23 | 23 | package org.opengrok.web.api.v1.filter;
|
24 | 24 |
|
| 25 | +import org.junit.Before; |
25 | 26 | import org.junit.Test;
|
26 | 27 | import org.mockito.ArgumentCaptor;
|
| 28 | +import org.opengrok.indexer.configuration.CommandTimeoutType; |
27 | 29 | import org.opengrok.indexer.configuration.RuntimeEnvironment;
|
28 | 30 |
|
29 | 31 | import javax.servlet.http.HttpServletRequest;
|
|
39 | 41 | import java.util.TreeMap;
|
40 | 42 |
|
41 | 43 | 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.*; |
46 | 46 |
|
47 | 47 | public class IncomingFilterTest {
|
| 48 | + @Before |
| 49 | + public void beforeTest() { |
| 50 | + RuntimeEnvironment.getInstance().setAuthenticationTokens(new HashSet<>()); |
| 51 | + } |
| 52 | + |
48 | 53 | @Test
|
49 | 54 | 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); |
51 | 62 | }
|
52 | 63 |
|
53 | 64 | @Test
|
54 | 65 | 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"; |
60 | 67 |
|
61 | 68 | Set<String> tokens = new HashSet<>();
|
62 | 69 | tokens.add(allowedToken);
|
63 | 70 | RuntimeEnvironment.getInstance().setAuthenticationTokens(tokens);
|
64 | 71 |
|
| 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 { |
65 | 109 | 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); |
68 | 112 | IncomingFilter filter = mockWithRemoteAddress("192.168.1.1", headers, true);
|
69 | 113 |
|
70 | 114 | ContainerRequestContext context = mockContainerRequestContext("test");
|
|
0 commit comments