3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.authentication.event;
22 import com.google.common.base.Joiner;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.slf4j.event.Level;
30 import org.sonar.api.server.http.HttpRequest;
31 import org.sonar.api.testfixtures.log.LogTester;
32 import org.sonar.api.utils.log.LoggerLevel;
34 import static java.util.Arrays.asList;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.assertj.core.api.Assertions.assertThatThrownBy;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.verifyNoInteractions;
39 import static org.mockito.Mockito.when;
40 import static org.sonar.server.authentication.event.AuthenticationEvent.Method;
41 import static org.sonar.server.authentication.event.AuthenticationEvent.Source;
42 import static org.sonar.server.authentication.event.AuthenticationException.newBuilder;
44 public class AuthenticationEventImplTest {
45 private static final String LOGIN_129_CHARS = "012345678901234567890123456789012345678901234567890123456789" +
46 "012345678901234567890123456789012345678901234567890123456789012345678";
49 public LogTester logTester = new LogTester();
51 private final AuthenticationEventImpl underTest = new AuthenticationEventImpl();
55 logTester.setLevel(LoggerLevel.DEBUG);
59 public void login_success_fails_with_NPE_if_request_is_null() {
60 logTester.setLevel(LoggerLevel.INFO);
62 Source sso = Source.sso();
63 assertThatThrownBy(() -> underTest.loginSuccess(null, "login", sso))
64 .isInstanceOf(NullPointerException.class)
65 .hasMessage("request can't be null");
69 public void login_success_fails_with_NPE_if_source_is_null() {
70 logTester.setLevel(LoggerLevel.INFO);
72 assertThatThrownBy(() -> underTest.loginSuccess(mock(HttpRequest.class), "login", null))
73 .isInstanceOf(NullPointerException.class)
74 .hasMessage("source can't be null");
78 public void login_success_does_not_interact_with_request_if_log_level_is_above_DEBUG() {
79 HttpRequest request = mock(HttpRequest.class);
80 logTester.setLevel(LoggerLevel.INFO);
82 underTest.loginSuccess(request, "login", Source.sso());
84 assertThat(logTester.logs()).isEmpty();
88 public void login_success_message_is_sanitized() {
89 logTester.setLevel(LoggerLevel.DEBUG);
91 underTest.loginSuccess(mockRequest("1.2.3.4"), "login with \n malicious line \r return", Source.sso());
93 assertThat(logTester.logs()).isNotEmpty()
94 .contains("login success [method|SSO][provider|SSO|sso][IP|1.2.3.4|][login|login with _ malicious line _ return]");
98 public void login_success_creates_DEBUG_log_with_empty_login_if_login_argument_is_null() {
99 underTest.loginSuccess(mockRequest(), null, Source.sso());
101 verifyLog("login success [method|SSO][provider|SSO|sso][IP||][login|]");
105 public void login_success_creates_DEBUG_log_with_method_provider_and_login() {
106 underTest.loginSuccess(mockRequest(), "foo", Source.realm(Method.BASIC, "some provider name"));
108 verifyLog("login success [method|BASIC][provider|REALM|some provider name][IP||][login|foo]");
112 public void login_success_prevents_log_flooding_on_login_starting_from_128_chars() {
113 underTest.loginSuccess(mockRequest(), LOGIN_129_CHARS, Source.realm(Method.BASIC, "some provider name"));
115 verifyLog("login success [method|BASIC][provider|REALM|some provider name][IP||][login|012345678901234567890123456789012345678901234567890123456789" +
116 "01234567890123456789012345678901234567890123456789012345678901234567...(129)]");
120 public void login_success_logs_remote_ip_from_request() {
121 underTest.loginSuccess(mockRequest("1.2.3.4"), "foo", Source.realm(Method.EXTERNAL, "bar"));
123 verifyLog("login success [method|EXTERNAL][provider|REALM|bar][IP|1.2.3.4|][login|foo]");
127 public void login_success_logs_X_Forwarded_For_header_from_request() {
128 HttpRequest request = mockRequest("1.2.3.4", asList("2.3.4.5"));
129 underTest.loginSuccess(request, "foo", Source.realm(Method.EXTERNAL, "bar"));
131 verifyLog("login success [method|EXTERNAL][provider|REALM|bar][IP|1.2.3.4|2.3.4.5][login|foo]");
135 public void login_success_logs_X_Forwarded_For_header_from_request_and_supports_multiple_headers() {
136 HttpRequest request = mockRequest("1.2.3.4", asList("2.3.4.5", "6.5.4.3"), asList("9.5.6.7"), asList("6.3.2.4"));
137 underTest.loginSuccess(request, "foo", Source.realm(Method.EXTERNAL, "bar"));
139 verifyLog("login success [method|EXTERNAL][provider|REALM|bar][IP|1.2.3.4|2.3.4.5,6.5.4.3,9.5.6.7,6.3.2.4][login|foo]");
143 public void login_failure_fails_with_NPE_if_request_is_null() {
144 logTester.setLevel(LoggerLevel.INFO);
146 AuthenticationException exception = newBuilder().setSource(Source.sso()).build();
147 assertThatThrownBy(() -> underTest.loginFailure(null, exception))
148 .isInstanceOf(NullPointerException.class)
149 .hasMessage("request can't be null");
153 public void login_failure_fails_with_NPE_if_AuthenticationException_is_null() {
154 logTester.setLevel(LoggerLevel.INFO);
156 assertThatThrownBy(() -> underTest.loginFailure(mock(HttpRequest.class), null))
157 .isInstanceOf(NullPointerException.class)
158 .hasMessage("AuthenticationException can't be null");
162 public void login_failure_does_not_interact_with_arguments_if_log_level_is_above_DEBUG() {
163 HttpRequest request = mock(HttpRequest.class);
164 AuthenticationException exception = mock(AuthenticationException.class);
165 logTester.setLevel(LoggerLevel.INFO);
167 underTest.loginFailure(request, exception);
169 verifyNoInteractions(request, exception);
173 public void login_failure_creates_DEBUG_log_with_empty_login_if_AuthenticationException_has_no_login() {
174 AuthenticationException exception = newBuilder().setSource(Source.sso()).setMessage("message").build();
175 underTest.loginFailure(mockRequest(), exception);
177 verifyLog("login failure [cause|message][method|SSO][provider|SSO|sso][IP||][login|]");
181 public void login_failure_creates_DEBUG_log_with_empty_cause_if_AuthenticationException_has_no_message() {
182 AuthenticationException exception = newBuilder().setSource(Source.sso()).setLogin("FoO").build();
183 underTest.loginFailure(mockRequest(), exception);
185 verifyLog("login failure [cause|][method|SSO][provider|SSO|sso][IP||][login|FoO]");
189 public void login_failure_creates_DEBUG_log_with_method_provider_and_login() {
190 AuthenticationException exception = newBuilder()
191 .setSource(Source.realm(Method.BASIC, "some provider name"))
192 .setMessage("something got terribly wrong")
195 underTest.loginFailure(mockRequest(), exception);
197 verifyLog("login failure [cause|something got terribly wrong][method|BASIC][provider|REALM|some provider name][IP||][login|BaR]");
201 public void login_failure_prevents_log_flooding_on_login_starting_from_128_chars() {
202 AuthenticationException exception = newBuilder()
203 .setSource(Source.realm(Method.BASIC, "some provider name"))
205 .setLogin(LOGIN_129_CHARS)
207 underTest.loginFailure(mockRequest(), exception);
209 verifyLog("login failure [cause|pop][method|BASIC][provider|REALM|some provider name][IP||][login|012345678901234567890123456789012345678901234567890123456789" +
210 "01234567890123456789012345678901234567890123456789012345678901234567...(129)]");
214 public void login_failure_logs_remote_ip_from_request() {
215 AuthenticationException exception = newBuilder()
216 .setSource(Source.realm(Method.EXTERNAL, "bar"))
217 .setMessage("Damn it!")
220 underTest.loginFailure(mockRequest("1.2.3.4"), exception);
222 verifyLog("login failure [cause|Damn it!][method|EXTERNAL][provider|REALM|bar][IP|1.2.3.4|][login|Baaad]");
226 public void login_failure_logs_X_Forwarded_For_header_from_request() {
227 AuthenticationException exception = newBuilder()
228 .setSource(Source.realm(Method.EXTERNAL, "bar"))
229 .setMessage("Hop la!")
232 HttpRequest request = mockRequest("1.2.3.4", asList("2.3.4.5"));
233 underTest.loginFailure(request, exception);
235 verifyLog("login failure [cause|Hop la!][method|EXTERNAL][provider|REALM|bar][IP|1.2.3.4|2.3.4.5][login|foo]");
239 public void login_failure_logs_X_Forwarded_For_header_from_request_and_supports_multiple_headers() {
240 AuthenticationException exception = newBuilder()
241 .setSource(Source.realm(Method.EXTERNAL, "bar"))
245 HttpRequest request = mockRequest("1.2.3.4", asList("2.3.4.5", "6.5.4.3"), asList("9.5.6.7"), asList("6.3.2.4"));
246 underTest.loginFailure(request, exception);
248 verifyLog("login failure [cause|Boom!][method|EXTERNAL][provider|REALM|bar][IP|1.2.3.4|2.3.4.5,6.5.4.3,9.5.6.7,6.3.2.4][login|foo]");
252 public void logout_success_fails_with_NPE_if_request_is_null() {
253 logTester.setLevel(LoggerLevel.INFO);
255 assertThatThrownBy(() -> underTest.logoutSuccess(null, "foo"))
256 .isInstanceOf(NullPointerException.class)
257 .hasMessage("request can't be null");
261 public void logout_success_does_not_interact_with_request_if_log_level_is_above_DEBUG() {
262 HttpRequest request = mock(HttpRequest.class);
263 logTester.setLevel(LoggerLevel.INFO);
265 underTest.logoutSuccess(request, "foo");
267 verifyNoInteractions(request);
271 public void logout_success_creates_DEBUG_log_with_empty_login_if_login_argument_is_null() {
272 underTest.logoutSuccess(mockRequest(), null);
274 verifyLog("logout success [IP||][login|]");
278 public void logout_success_creates_DEBUG_log_with_login() {
279 underTest.logoutSuccess(mockRequest(), "foo");
281 verifyLog("logout success [IP||][login|foo]");
285 public void logout_success_logs_remote_ip_from_request() {
286 underTest.logoutSuccess(mockRequest("1.2.3.4"), "foo");
288 verifyLog("logout success [IP|1.2.3.4|][login|foo]");
292 public void logout_success_logs_X_Forwarded_For_header_from_request() {
293 HttpRequest request = mockRequest("1.2.3.4", asList("2.3.4.5"));
294 underTest.logoutSuccess(request, "foo");
296 verifyLog("logout success [IP|1.2.3.4|2.3.4.5][login|foo]");
300 public void logout_success_logs_X_Forwarded_For_header_from_request_and_supports_multiple_headers() {
301 HttpRequest request = mockRequest("1.2.3.4", asList("2.3.4.5", "6.5.4.3"), asList("9.5.6.7"), asList("6.3.2.4"));
302 underTest.logoutSuccess(request, "foo");
304 verifyLog("logout success [IP|1.2.3.4|2.3.4.5,6.5.4.3,9.5.6.7,6.3.2.4][login|foo]");
308 public void logout_failure_with_NPE_if_request_is_null() {
309 logTester.setLevel(LoggerLevel.INFO);
311 assertThatThrownBy(() -> underTest.logoutFailure(null, "bad csrf"))
312 .isInstanceOf(NullPointerException.class)
313 .hasMessage("request can't be null");
317 public void login_fails_with_NPE_if_error_message_is_null() {
318 logTester.setLevel(LoggerLevel.INFO);
320 assertThatThrownBy(() -> underTest.logoutFailure(mock(HttpRequest.class), null))
321 .isInstanceOf(NullPointerException.class)
322 .hasMessage("error message can't be null");
326 public void logout_does_not_interact_with_request_if_log_level_is_above_DEBUG() {
327 HttpRequest request = mock(HttpRequest.class);
328 logTester.setLevel(LoggerLevel.INFO);
330 underTest.logoutFailure(request, "bad csrf");
332 verifyNoInteractions(request);
336 public void logout_creates_DEBUG_log_with_error() {
337 underTest.logoutFailure(mockRequest(), "bad token");
339 verifyLog("logout failure [error|bad token][IP||]");
343 public void logout_logs_remote_ip_from_request() {
344 underTest.logoutFailure(mockRequest("1.2.3.4"), "bad token");
346 verifyLog("logout failure [error|bad token][IP|1.2.3.4|]");
350 public void logout_logs_X_Forwarded_For_header_from_request() {
351 HttpRequest request = mockRequest("1.2.3.4", asList("2.3.4.5"));
352 underTest.logoutFailure(request, "bad token");
354 verifyLog("logout failure [error|bad token][IP|1.2.3.4|2.3.4.5]");
358 public void logout_logs_X_Forwarded_For_header_from_request_and_supports_multiple_headers() {
359 HttpRequest request = mockRequest("1.2.3.4", asList("2.3.4.5", "6.5.4.3"), asList("9.5.6.7"), asList("6.3.2.4"));
360 underTest.logoutFailure(request, "bad token");
362 verifyLog("logout failure [error|bad token][IP|1.2.3.4|2.3.4.5,6.5.4.3,9.5.6.7,6.3.2.4]");
365 private void verifyLog(String expected) {
366 assertThat(logTester.logs()).hasSize(1);
367 assertThat(logTester.logs(Level.DEBUG))
368 .containsOnly(expected);
371 private static HttpRequest mockRequest() {
372 return mockRequest("");
375 private static HttpRequest mockRequest(String remoteAddr, List<String>... remoteIps) {
376 HttpRequest res = mock(HttpRequest.class);
377 when(res.getRemoteAddr()).thenReturn(remoteAddr);
378 when(res.getHeaders("X-Forwarded-For"))
379 .thenReturn(Collections.enumeration(
380 Arrays.stream(remoteIps)
381 .map(Joiner.on(",")::join)