aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org/sonar/scanner/http/ScannerWsClientProviderTest.java
blob: e0ee3121accc01a4e3936701a6f7f2b2c5d2fd8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * SonarQube
 * Copyright (C) 2009-2024 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.scanner.http;

import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;
import org.junitpioneer.jupiter.RestoreSystemProperties;
import org.sonar.api.notifications.AnalysisWarnings;
import org.sonar.api.utils.System2;
import org.sonar.batch.bootstrapper.EnvironmentInformation;
import org.sonar.scanner.bootstrap.GlobalAnalysisMode;
import org.sonar.scanner.bootstrap.ScannerProperties;
import org.sonar.scanner.bootstrap.SonarUserHome;
import org.sonarqube.ws.client.GetRequest;
import org.sonarqube.ws.client.HttpConnector;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class ScannerWsClientProviderTest {

  private static final GlobalAnalysisMode GLOBAL_ANALYSIS_MODE = new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap()));
  private static final AnalysisWarnings ANALYSIS_WARNINGS = warning -> {
  };
  @TempDir
  private Path sonarUserHomeDir;
  private final SonarUserHome sonarUserHome = mock(SonarUserHome.class);
  private final Map<String, String> scannerProps = new HashMap<>();

  private final ScannerWsClientProvider underTest = new ScannerWsClientProvider();
  private final EnvironmentInformation env = new EnvironmentInformation("Maven Plugin", "2.3");

  private final System2 system2 = mock(System2.class);
  private final Properties systemProps = new Properties();

  @BeforeEach
  void configureMocks() {
    when(system2.properties()).thenReturn(systemProps);
    when(sonarUserHome.getPath()).thenReturn(sonarUserHomeDir);
  }

  @Test
  void provide_client_with_default_settings() {
    DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS, sonarUserHome);

    assertThat(client).isNotNull();
    assertThat(client.baseUrl()).isEqualTo("http://localhost:9000/");
    HttpConnector httpConnector = (HttpConnector) client.wsConnector();
    assertThat(httpConnector.baseUrl()).isEqualTo("http://localhost:9000/");
    assertThat(httpConnector.okHttpClient().proxy()).isNull();
    assertThat(httpConnector.okHttpClient().connectTimeoutMillis()).isEqualTo(5_000);
    assertThat(httpConnector.okHttpClient().readTimeoutMillis()).isEqualTo(60_000);
  }

  @Test
  void provide_client_with_custom_settings() {
    scannerProps.put("sonar.host.url", "https://here/sonarqube");
    scannerProps.put("sonar.token", "testToken");
    scannerProps.put("sonar.ws.timeout", "42");

    DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS, sonarUserHome);

    assertThat(client).isNotNull();
    HttpConnector httpConnector = (HttpConnector) client.wsConnector();
    assertThat(httpConnector.baseUrl()).isEqualTo("https://here/sonarqube/");
    assertThat(httpConnector.okHttpClient().proxy()).isNull();
  }

  @Nested
  class WithMockHttpSonarQube {

    @RegisterExtension
    static WireMockExtension sonarqubeMock = WireMockExtension.newInstance()
      .options(wireMockConfig().dynamicPort())
      .build();

    @Test
    void it_should_timeout_on_long_response() {
      scannerProps.put("sonar.host.url", sonarqubeMock.baseUrl());
      scannerProps.put("sonar.scanner.responseTimeout", "PT0.2S");

      DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS, sonarUserHome);

      sonarqubeMock.stubFor(get("/api/plugins/installed")
        .willReturn(aResponse().withStatus(200)
          .withFixedDelay(2000)
          .withBody("Success")));

      HttpConnector httpConnector = (HttpConnector) client.wsConnector();

      var getRequest = new GetRequest("api/plugins/installed");
      var thrown = assertThrows(IllegalStateException.class, () -> httpConnector.call(getRequest));

      assertThat(thrown).hasStackTraceContaining("timeout");
    }

    @Test
    void it_should_timeout_on_slow_response() {
      scannerProps.put("sonar.host.url", sonarqubeMock.baseUrl());
      scannerProps.put("sonar.scanner.socketTimeout", "PT0.2S");

      DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS, sonarUserHome);

      sonarqubeMock.stubFor(get("/api/plugins/installed")
        .willReturn(aResponse().withStatus(200)
          .withChunkedDribbleDelay(2, 2000)
          .withBody("Success")));

      HttpConnector httpConnector = (HttpConnector) client.wsConnector();

      var getRequest = new GetRequest("api/plugins/installed");
      var thrown = assertThrows(IllegalStateException.class, () -> httpConnector.call(getRequest));

      assertThat(thrown).hasStackTraceContaining("timeout");
    }

    @Test
    void it_should_throw_if_invalid_proxy_port() {
      scannerProps.put("sonar.host.url", sonarqubeMock.baseUrl());
      scannerProps.put("sonar.scanner.proxyHost", "localhost");
      scannerProps.put("sonar.scanner.proxyPort", "not_a_number");
      var scannerPropertiesBean = new ScannerProperties(scannerProps);

      assertThrows(IllegalArgumentException.class, () -> underTest.provide(scannerPropertiesBean, env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS, sonarUserHome));
    }

    @Nested
    class WithProxy {

      private static final String PROXY_AUTH_ENABLED = "proxy-auth";

      @RegisterExtension
      static WireMockExtension proxyMock = WireMockExtension.newInstance()
        .options(wireMockConfig().dynamicPort())
        .build();

      @BeforeEach
      void configureMocks(TestInfo info) {
        if (info.getTags().contains(PROXY_AUTH_ENABLED)) {
          proxyMock.stubFor(get(urlMatching("/api/plugins/.*"))
            .inScenario("Proxy Auth")
            .whenScenarioStateIs(STARTED)
            .willReturn(aResponse()
              .withStatus(407)
              .withHeader("Proxy-Authenticate", "Basic realm=\"Access to the proxy\""))
            .willSetStateTo("Challenge returned"));
          proxyMock.stubFor(get(urlMatching("/api/plugins/.*"))
            .inScenario("Proxy Auth")
            .whenScenarioStateIs("Challenge returned")
            .willReturn(aResponse().proxiedFrom(sonarqubeMock.baseUrl())));
        } else {
          proxyMock.stubFor(get(urlMatching("/api/plugins/.*")).willReturn(aResponse().proxiedFrom(sonarqubeMock.baseUrl())));
        }
      }

      @Test
      void it_should_honor_scanner_proxy_settings() {
        scannerProps.put("sonar.host.url", sonarqubeMock.baseUrl());
        scannerProps.put("sonar.scanner.proxyHost", "localhost");
        scannerProps.put("sonar.scanner.proxyPort", "" + proxyMock.getPort());

        DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS, sonarUserHome);

        sonarqubeMock.stubFor(get("/api/plugins/installed")
          .willReturn(aResponse().withStatus(200).withBody("Success")));

        HttpConnector httpConnector = (HttpConnector) client.wsConnector();
        try (var r = httpConnector.call(new GetRequest("api/plugins/installed"))) {
          assertThat(r.code()).isEqualTo(200);
          assertThat(r.content()).isEqualTo("Success");
        }

        proxyMock.verify(getRequestedFor(urlEqualTo("/api/plugins/installed")));
      }

      @Test
      @Tag(PROXY_AUTH_ENABLED)
      void it_should_honor_scanner_proxy_settings_with_auth() {
        var proxyLogin = "proxyLogin";
        var proxyPassword = "proxyPassword";
        scannerProps.put("sonar.host.url", sonarqubeMock.baseUrl());
        scannerProps.put("sonar.scanner.proxyHost", "localhost");
        scannerProps.put("sonar.scanner.proxyPort", "" + proxyMock.getPort());
        scannerProps.put("sonar.scanner.proxyUser", proxyLogin);
        scannerProps.put("sonar.scanner.proxyPassword", proxyPassword);

        DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS, sonarUserHome);

        sonarqubeMock.stubFor(get("/api/plugins/installed")
          .willReturn(aResponse().withStatus(200).withBody("Success")));

        HttpConnector httpConnector = (HttpConnector) client.wsConnector();
        try (var r = httpConnector.call(new GetRequest("api/plugins/installed"))) {
          assertThat(r.code()).isEqualTo(200);
          assertThat(r.content()).isEqualTo("Success");
        }

        proxyMock.verify(getRequestedFor(urlEqualTo("/api/plugins/installed"))
          .withHeader("Proxy-Authorization", equalTo("Basic " + Base64.getEncoder().encodeToString((proxyLogin + ":" + proxyPassword).getBytes(StandardCharsets.UTF_8)))));

      }

      @Test
      @Tag(PROXY_AUTH_ENABLED)
      void it_should_honor_old_jvm_proxy_auth_properties() {
        var proxyLogin = "proxyLogin";
        var proxyPassword = "proxyPassword";
        scannerProps.put("sonar.host.url", sonarqubeMock.baseUrl());
        scannerProps.put("sonar.scanner.proxyHost", "localhost");
        scannerProps.put("sonar.scanner.proxyPort", "" + proxyMock.getPort());
        systemProps.put("http.proxyUser", proxyLogin);
        systemProps.put("http.proxyPassword", proxyPassword);

        DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS, sonarUserHome);

        sonarqubeMock.stubFor(get("/api/plugins/installed")
          .willReturn(aResponse().withStatus(200).withBody("Success")));

        HttpConnector httpConnector = (HttpConnector) client.wsConnector();
        try (var r = httpConnector.call(new GetRequest("api/plugins/installed"))) {
          assertThat(r.code()).isEqualTo(200);
          assertThat(r.content()).isEqualTo("Success");
        }

        proxyMock.verify(getRequestedFor(urlEqualTo("/api/plugins/installed"))
          .withHeader("Proxy-Authorization", equalTo("Basic " + Base64.getEncoder().encodeToString((proxyLogin + ":" + proxyPassword).getBytes(StandardCharsets.UTF_8)))));

      }
    }

  }

  @Nested
  class WithMockHttpsSonarQube {

    public static final String KEYSTORE_PWD = "pwdServerP12";

    @RegisterExtension
    static WireMockExtension sonarqubeMock = WireMockExtension.newInstance()
      .options(wireMockConfig().dynamicHttpsPort().httpDisabled(true)
        .keystoreType("pkcs12")
        .keystorePath(toPath(requireNonNull(ScannerWsClientProviderTest.class.getResource("/ssl/server.p12"))).toString())
        .keystorePassword(KEYSTORE_PWD)
        .keyManagerPassword(KEYSTORE_PWD))
      .build();

    @BeforeEach
    void mockResponse() {
      sonarqubeMock.stubFor(get("/api/plugins/installed")
        .willReturn(aResponse().withStatus(200).withBody("Success")));
    }

    @Test
    void it_should_not_trust_server_self_signed_certificate_by_default() {
      scannerProps.put("sonar.host.url", sonarqubeMock.baseUrl());

      DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS, sonarUserHome);

      HttpConnector httpConnector = (HttpConnector) client.wsConnector();
      var getRequest = new GetRequest("api/plugins/installed");
      var thrown = assertThrows(IllegalStateException.class, () -> httpConnector.call(getRequest));

      assertThat(thrown).hasStackTraceContaining("CertificateException");
    }

    @Test
    void it_should_trust_server_self_signed_certificate_when_certificate_is_in_truststore() {
      scannerProps.put("sonar.host.url", sonarqubeMock.baseUrl());
      scannerProps.put("sonar.scanner.truststorePath", toPath(requireNonNull(ScannerWsClientProviderTest.class.getResource("/ssl/client-truststore.p12"))).toString());
      scannerProps.put("sonar.scanner.truststorePassword", "pwdClientWithServerCA");

      DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS, sonarUserHome);

      HttpConnector httpConnector = (HttpConnector) client.wsConnector();
      try (var r = httpConnector.call(new GetRequest("api/plugins/installed"))) {
        assertThat(r.code()).isEqualTo(200);
        assertThat(r.content()).isEqualTo("Success");
      }
    }
  }

  @Nested
  class WithMockHttpsSonarQubeAndClientCertificates {

    public static final String KEYSTORE_PWD = "pwdServerP12";

    @RegisterExtension
    static WireMockExtension sonarqubeMock = WireMockExtension.newInstance()
      .options(wireMockConfig().dynamicHttpsPort().httpDisabled(true)
        .keystoreType("pkcs12")
        .keystorePath(toPath(requireNonNull(ScannerWsClientProviderTest.class.getResource("/ssl/server.p12"))).toString())
        .keystorePassword(KEYSTORE_PWD)
        .keyManagerPassword(KEYSTORE_PWD)
        .needClientAuth(true)
        .trustStoreType("pkcs12")
        .trustStorePath(toPath(requireNonNull(ScannerWsClientProviderTest.class.getResource("/ssl/server-with-client-ca.p12"))).toString())
        .trustStorePassword("pwdServerWithClientCA"))
      .build();

    @BeforeEach
    void mockResponse() {
      sonarqubeMock.stubFor(get("/api/plugins/installed")
        .willReturn(aResponse().withStatus(200).withBody("Success")));
    }

    @Test
    void it_should_fail_if_client_certificate_not_provided() {
      scannerProps.put("sonar.host.url", sonarqubeMock.baseUrl());
      scannerProps.put("sonar.scanner.truststorePath", toPath(requireNonNull(ScannerWsClientProviderTest.class.getResource("/ssl/client-truststore.p12"))).toString());
      scannerProps.put("sonar.scanner.truststorePassword", "pwdClientWithServerCA");

      DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS, sonarUserHome);

      HttpConnector httpConnector = (HttpConnector) client.wsConnector();
      var getRequest = new GetRequest("api/plugins/installed");
      var thrown = assertThrows(IllegalStateException.class, () -> httpConnector.call(getRequest));

      assertThat(thrown).satisfiesAnyOf(
        e -> assertThat(e).hasStackTraceContaining("SSLHandshakeException"),
        // Exception is flaky because of https://bugs.openjdk.org/browse/JDK-8172163
        e -> assertThat(e).hasStackTraceContaining("Broken pipe"));
    }

    @Test
    void it_should_authenticate_using_certificate_in_keystore() {
      scannerProps.put("sonar.host.url", sonarqubeMock.baseUrl());

      scannerProps.put("sonar.scanner.truststorePath", toPath(requireNonNull(ScannerWsClientProviderTest.class.getResource("/ssl/client-truststore.p12"))).toString());
      scannerProps.put("sonar.scanner.truststorePassword", "pwdClientWithServerCA");
      scannerProps.put("sonar.scanner.keystorePath", toPath(requireNonNull(ScannerWsClientProviderTest.class.getResource("/ssl/client.p12"))).toString());
      scannerProps.put("sonar.scanner.keystorePassword", "pwdClientCertP12");

      DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS, sonarUserHome);

      HttpConnector httpConnector = (HttpConnector) client.wsConnector();
      try (var r = httpConnector.call(new GetRequest("api/plugins/installed"))) {
        assertThat(r.code()).isEqualTo(200);
        assertThat(r.content()).isEqualTo("Success");
      }
    }

    @RestoreSystemProperties
    @Test
    void it_should_support_jvm_system_properties() {
      scannerProps.put("sonar.host.url", sonarqubeMock.baseUrl());
      System.setProperty("javax.net.ssl.trustStore", toPath(requireNonNull(ScannerWsClientProviderTest.class.getResource("/ssl/client-truststore.p12"))).toString());
      System.setProperty("javax.net.ssl.trustStorePassword", "pwdClientWithServerCA");
      System.setProperty("javax.net.ssl.keyStore", toPath(requireNonNull(ScannerWsClientProviderTest.class.getResource("/ssl/client.p12"))).toString());
      systemProps.setProperty("javax.net.ssl.keyStore", "any value is fine here");
      System.setProperty("javax.net.ssl.keyStorePassword", "pwdClientCertP12");

      DefaultScannerWsClient client = underTest.provide(new ScannerProperties(scannerProps), env, GLOBAL_ANALYSIS_MODE, system2, ANALYSIS_WARNINGS, sonarUserHome);

      HttpConnector httpConnector = (HttpConnector) client.wsConnector();
      try (var r = httpConnector.call(new GetRequest("api/plugins/installed"))) {
        assertThat(r.code()).isEqualTo(200);
        assertThat(r.content()).isEqualTo("Success");
      }
    }
  }

  private static Path toPath(URL url) {
    try {
      return Paths.get(url.toURI());
    } catch (URISyntaxException e) {
      throw new RuntimeException(e);
    }
  }
}