You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScannerWsClientProviderTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.scanner.bootstrap;
  21. import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
  22. import java.nio.charset.StandardCharsets;
  23. import java.util.Base64;
  24. import java.util.Collections;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. import java.util.Properties;
  28. import org.junit.jupiter.api.BeforeEach;
  29. import org.junit.jupiter.api.Tag;
  30. import org.junit.jupiter.api.Test;
  31. import org.junit.jupiter.api.TestInfo;
  32. import org.junit.jupiter.api.extension.RegisterExtension;
  33. import org.sonar.api.notifications.AnalysisWarnings;
  34. import org.sonar.api.utils.System2;
  35. import org.sonar.batch.bootstrapper.EnvironmentInformation;
  36. import org.sonarqube.ws.client.GetRequest;
  37. import org.sonarqube.ws.client.HttpConnector;
  38. import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
  39. import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
  40. import static com.github.tomakehurst.wiremock.client.WireMock.get;
  41. import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
  42. import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
  43. import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
  44. import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
  45. import static com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED;
  46. import static org.assertj.core.api.Assertions.assertThat;
  47. import static org.junit.Assert.assertThrows;
  48. import static org.mockito.Mockito.mock;
  49. import static org.mockito.Mockito.when;
  50. class ScannerWsClientProviderTest {
  51. public static final GlobalAnalysisMode GLOBAL_ANALYSIS_MODE = new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()));
  52. public static final AnalysisWarnings ANALYSIS_WARNINGS = warning -> {
  53. };
  54. private final Map<String, String> scannerProps = new HashMap<>();
  55. private final ScannerWsClientProvider underTest = new ScannerWsClientProvider();
  56. private final EnvironmentInformation env = new EnvironmentInformation("Maven Plugin", "2.3");
  57. public static final String PROXY_AUTH_ENABLED = "proxy-auth";
  58. @RegisterExtension
  59. static WireMockExtension sonarqubeMock = WireMockExtension.newInstance()
  60. .options(wireMockConfig().dynamicPort())
  61. .build();
  62. @RegisterExtension
  63. static WireMockExtension proxyMock = WireMockExtension.newInstance()
  64. .options(wireMockConfig().dynamicPort())
  65. .build();
  66. private final System2 system2 = mock(System2.class);
  67. private final Properties systemProps = new Properties();
  68. @BeforeEach
  69. void configureMocks(TestInfo info) {
  70. when(system2.properties()).thenReturn(systemProps);
  71. if (info.getTags().contains(PROXY_AUTH_ENABLED)) {
  72. proxyMock.stubFor(get(urlMatching("/api/plugins/.*"))
  73. .inScenario("Proxy Auth")
  74. .whenScenarioStateIs(STARTED)
  75. .willReturn(aResponse()
  76. .withStatus(407)
  77. .withHeader("Proxy-Authenticate", "Basic realm=\"Access to the proxy\""))
  78. .willSetStateTo("Challenge returned"));
  79. proxyMock.stubFor(get(urlMatching("/api/plugins/.*"))
  80. .inScenario("Proxy Auth")
  81. .whenScenarioStateIs("Challenge returned")
  82. .willReturn(aResponse().proxiedFrom(sonarqubeMock.baseUrl())));
  83. } else {
  84. proxyMock.stubFor(get(urlMatching("/api/plugins/.*")).willReturn(aResponse().proxiedFrom(sonarqubeMock.baseUrl())));
  85. }
  86. }
  87. @Test
  88. void provide_client_with_default_settings() {
  89. DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS);
  90. assertThat(client).isNotNull();
  91. assertThat(client.baseUrl()).isEqualTo("http://localhost:9000/");
  92. HttpConnector httpConnector = (HttpConnector) client.wsConnector();
  93. assertThat(httpConnector.baseUrl()).isEqualTo("http://localhost:9000/");
  94. assertThat(httpConnector.okHttpClient().proxy()).isNull();
  95. assertThat(httpConnector.okHttpClient().connectTimeoutMillis()).isEqualTo(5_000);
  96. assertThat(httpConnector.okHttpClient().readTimeoutMillis()).isEqualTo(60_000);
  97. // Proxy is not accessed
  98. assertThat(proxyMock.findAllUnmatchedRequests()).isEmpty();
  99. }
  100. @Test
  101. void provide_client_with_custom_settings() {
  102. scannerProps.put("sonar.host.url", "https://here/sonarqube");
  103. scannerProps.put("sonar.token", "testToken");
  104. scannerProps.put("sonar.ws.timeout", "42");
  105. DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS);
  106. assertThat(client).isNotNull();
  107. HttpConnector httpConnector = (HttpConnector) client.wsConnector();
  108. assertThat(httpConnector.baseUrl()).isEqualTo("https://here/sonarqube/");
  109. assertThat(httpConnector.okHttpClient().proxy()).isNull();
  110. }
  111. @Test
  112. void it_should_timeout_on_long_response() {
  113. scannerProps.put("sonar.host.url", sonarqubeMock.baseUrl());
  114. scannerProps.put("sonar.scanner.responseTimeout", "PT0.2S");
  115. DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS);
  116. sonarqubeMock.stubFor(get("/api/plugins/installed")
  117. .willReturn(aResponse().withStatus(200)
  118. .withFixedDelay(2000)
  119. .withBody("Success")));
  120. HttpConnector httpConnector = (HttpConnector) client.wsConnector();
  121. var getRequest = new GetRequest("api/plugins/installed");
  122. var thrown = assertThrows(IllegalStateException.class, () -> httpConnector.call(getRequest));
  123. assertThat(thrown).hasStackTraceContaining("timeout");
  124. }
  125. @Test
  126. void it_should_timeout_on_slow_response() {
  127. scannerProps.put("sonar.host.url", sonarqubeMock.baseUrl());
  128. scannerProps.put("sonar.scanner.socketTimeout", "PT0.2S");
  129. DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS);
  130. sonarqubeMock.stubFor(get("/api/plugins/installed")
  131. .willReturn(aResponse().withStatus(200)
  132. .withChunkedDribbleDelay(2, 2000)
  133. .withBody("Success")));
  134. HttpConnector httpConnector = (HttpConnector) client.wsConnector();
  135. var getRequest = new GetRequest("api/plugins/installed");
  136. var thrown = assertThrows(IllegalStateException.class, () -> httpConnector.call(getRequest));
  137. assertThat(thrown).hasStackTraceContaining("timeout");
  138. }
  139. @Test
  140. void it_should_honor_scanner_proxy_settings() {
  141. scannerProps.put("sonar.host.url", sonarqubeMock.baseUrl());
  142. scannerProps.put("sonar.scanner.proxyHost", "localhost");
  143. scannerProps.put("sonar.scanner.proxyPort", "" + proxyMock.getPort());
  144. DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS);
  145. sonarqubeMock.stubFor(get("/api/plugins/installed")
  146. .willReturn(aResponse().withStatus(200).withBody("Success")));
  147. HttpConnector httpConnector = (HttpConnector) client.wsConnector();
  148. try (var r = httpConnector.call(new GetRequest("api/plugins/installed"))) {
  149. assertThat(r.code()).isEqualTo(200);
  150. assertThat(r.content()).isEqualTo("Success");
  151. }
  152. proxyMock.verify(getRequestedFor(urlEqualTo("/api/plugins/installed")));
  153. }
  154. @Test
  155. void it_should_throw_if_invalid_proxy_port() {
  156. scannerProps.put("sonar.host.url", sonarqubeMock.baseUrl());
  157. scannerProps.put("sonar.scanner.proxyHost", "localhost");
  158. scannerProps.put("sonar.scanner.proxyPort", "not_a_number");
  159. var scannerPropertiesBean = new ScannerProperties(scannerProps);
  160. assertThrows(IllegalArgumentException.class, () -> underTest.provide(scannerPropertiesBean, env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS));
  161. }
  162. @Test
  163. @Tag(PROXY_AUTH_ENABLED)
  164. void it_should_honor_scanner_proxy_settings_with_auth() {
  165. var proxyLogin = "proxyLogin";
  166. var proxyPassword = "proxyPassword";
  167. scannerProps.put("sonar.host.url", sonarqubeMock.baseUrl());
  168. scannerProps.put("sonar.scanner.proxyHost", "localhost");
  169. scannerProps.put("sonar.scanner.proxyPort", "" + proxyMock.getPort());
  170. scannerProps.put("sonar.scanner.proxyUser", proxyLogin);
  171. scannerProps.put("sonar.scanner.proxyPassword", proxyPassword);
  172. DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS);
  173. sonarqubeMock.stubFor(get("/api/plugins/installed")
  174. .willReturn(aResponse().withStatus(200).withBody("Success")));
  175. HttpConnector httpConnector = (HttpConnector) client.wsConnector();
  176. try (var r = httpConnector.call(new GetRequest("api/plugins/installed"))) {
  177. assertThat(r.code()).isEqualTo(200);
  178. assertThat(r.content()).isEqualTo("Success");
  179. }
  180. proxyMock.verify(getRequestedFor(urlEqualTo("/api/plugins/installed"))
  181. .withHeader("Proxy-Authorization", equalTo("Basic " + Base64.getEncoder().encodeToString((proxyLogin + ":" + proxyPassword).getBytes(StandardCharsets.UTF_8)))));
  182. }
  183. @Test
  184. @Tag(PROXY_AUTH_ENABLED)
  185. void it_should_honor_old_jvm_proxy_auth_properties() {
  186. var proxyLogin = "proxyLogin";
  187. var proxyPassword = "proxyPassword";
  188. scannerProps.put("sonar.host.url", sonarqubeMock.baseUrl());
  189. scannerProps.put("sonar.scanner.proxyHost", "localhost");
  190. scannerProps.put("sonar.scanner.proxyPort", "" + proxyMock.getPort());
  191. systemProps.put("http.proxyUser", proxyLogin);
  192. systemProps.put("http.proxyPassword", proxyPassword);
  193. DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS);
  194. sonarqubeMock.stubFor(get("/api/plugins/installed")
  195. .willReturn(aResponse().withStatus(200).withBody("Success")));
  196. HttpConnector httpConnector = (HttpConnector) client.wsConnector();
  197. try (var r = httpConnector.call(new GetRequest("api/plugins/installed"))) {
  198. assertThat(r.code()).isEqualTo(200);
  199. assertThat(r.content()).isEqualTo("Success");
  200. }
  201. proxyMock.verify(getRequestedFor(urlEqualTo("/api/plugins/installed"))
  202. .withHeader("Proxy-Authorization", equalTo("Basic " + Base64.getEncoder().encodeToString((proxyLogin + ":" + proxyPassword).getBytes(StandardCharsets.UTF_8)))));
  203. }
  204. }