Skip to content

Commit fe8416a

Browse files
committed
SCANJLIB-241 Add property to skip loading of OS-level SSL certificates
1 parent 723eb33 commit fe8416a

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

lib/src/main/java/org/sonarsource/scanner/lib/ScannerProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private ScannerProperties() {
8484
public static final String SONAR_SCANNER_KEYSTORE_PASSWORD = "sonar.scanner.keystorePassword";
8585
public static final String SONAR_SCANNER_TRUSTSTORE_PATH = "sonar.scanner.truststorePath";
8686
public static final String SONAR_SCANNER_TRUSTSTORE_PASSWORD = "sonar.scanner.truststorePassword";
87-
87+
public static final String SONAR_SCANNER_SKIP_SYSTEM_TRUSTSTORE = "sonar.scanner.skipSystemTruststore";
8888
/**
8989
* Skip analysis.
9090
*/

lib/src/main/java/org/sonarsource/scanner/lib/internal/http/HttpConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import static java.lang.Integer.parseInt;
4040
import static java.lang.String.format;
4141
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
42+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_SKIP_SYSTEM_TRUSTSTORE;
4243
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_CONNECT_TIMEOUT;
4344
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_KEYSTORE_PASSWORD;
4445
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_KEYSTORE_PATH;
@@ -79,6 +80,7 @@ public class HttpConfig {
7980
private final String proxyUser;
8081
private final String proxyPassword;
8182
private final String userAgent;
83+
private final boolean skipSystemTrustMaterial;
8284

8385
public HttpConfig(Map<String, String> bootstrapProperties, Path sonarUserHome) {
8486
this.webApiBaseUrl = StringUtils.removeEnd(bootstrapProperties.get(ScannerProperties.HOST_URL), "/");
@@ -94,6 +96,7 @@ public HttpConfig(Map<String, String> bootstrapProperties, Path sonarUserHome) {
9496
this.proxy = loadProxy(bootstrapProperties);
9597
this.proxyUser = loadProxyUser(bootstrapProperties);
9698
this.proxyPassword = loadProxyPassword(bootstrapProperties);
99+
this.skipSystemTrustMaterial = Boolean.parseBoolean(defaultIfBlank(bootstrapProperties.get(SONAR_SCANNER_SKIP_SYSTEM_TRUSTSTORE), "false"));
97100
}
98101

99102
private static String loadProxyPassword(Map<String, String> bootstrapProperties) {
@@ -249,4 +252,8 @@ public String getProxyUser() {
249252
public String getProxyPassword() {
250253
return proxyPassword;
251254
}
255+
256+
public boolean skipSystemTruststore() {
257+
return skipSystemTrustMaterial;
258+
}
252259
}

lib/src/main/java/org/sonarsource/scanner/lib/internal/http/OkHttpClientFactory.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import static java.nio.charset.StandardCharsets.UTF_8;
5151
import static java.util.Arrays.asList;
5252
import static org.apache.commons.lang3.StringUtils.isNotBlank;
53+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_SKIP_SYSTEM_TRUSTSTORE;
5354

5455
public class OkHttpClientFactory {
5556

@@ -72,7 +73,7 @@ private OkHttpClientFactory() {
7273

7374
static OkHttpClient create(HttpConfig httpConfig) {
7475

75-
var sslContext = configureSsl(httpConfig.getSslConfig());
76+
var sslContext = configureSsl(httpConfig.getSslConfig(), httpConfig.skipSystemTruststore());
7677

7778
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder()
7879
.connectTimeout(httpConfig.getConnectTimeout().toMillis(), TimeUnit.MILLISECONDS)
@@ -112,10 +113,14 @@ static OkHttpClient create(HttpConfig httpConfig) {
112113
return okHttpClientBuilder.build();
113114
}
114115

115-
private static SSLFactory configureSsl(SslConfig sslConfig) {
116+
private static SSLFactory configureSsl(SslConfig sslConfig, boolean skipSystemTrustMaterial) {
116117
var sslFactoryBuilder = SSLFactory.builder()
117-
.withDefaultTrustMaterial()
118-
.withSystemTrustMaterial();
118+
.withDefaultTrustMaterial();
119+
if (!skipSystemTrustMaterial) {
120+
LOG.debug("Loading OS trusted SSL certificates...");
121+
LOG.debug("This operation might be slow or even get stuck. You can skip it by passing the scanner property '{}=true'", SONAR_SCANNER_SKIP_SYSTEM_TRUSTSTORE);
122+
sslFactoryBuilder.withSystemTrustMaterial();
123+
}
119124
if (System.getProperties().containsKey("javax.net.ssl.keyStore")) {
120125
sslFactoryBuilder.withSystemPropertyDerivedIdentityMaterial();
121126
}

lib/src/test/java/org/sonarsource/scanner/lib/internal/http/OkHttpClientFactoryTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import org.junit.jupiter.params.ParameterizedTest;
4646
import org.junit.jupiter.params.provider.CsvSource;
4747
import org.junitpioneer.jupiter.RestoreSystemProperties;
48+
import org.slf4j.event.Level;
49+
import testutils.LogTester;
4850

4951
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
5052
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
@@ -68,6 +70,9 @@ class OkHttpClientFactoryTest {
6870

6971
private final Map<String, String> bootstrapProperties = new HashMap<>();
7072

73+
@RegisterExtension
74+
private LogTester logTester = new LogTester();
75+
7176
@TempDir
7277
private Path sonarUserHomeDir;
7378
private Path sonarUserHome;
@@ -131,6 +136,25 @@ void it_should_fail_if_invalid_keystore_password(String keystore, @Nullable Stri
131136
}
132137
}
133138

139+
@Test
140+
void should_load_os_certificates_by_default() {
141+
logTester.setLevel(Level.DEBUG);
142+
143+
OkHttpClientFactory.create(new HttpConfig(bootstrapProperties, sonarUserHome));
144+
145+
assertThat(logTester.logs(Level.DEBUG)).contains("Loading OS trusted SSL certificates...");
146+
}
147+
148+
@Test
149+
void should_skip_load_of_os_certificates_if_props_set() {
150+
logTester.setLevel(Level.DEBUG);
151+
bootstrapProperties.put("sonar.scanner.skipSystemTruststore", "true");
152+
153+
OkHttpClientFactory.create(new HttpConfig(bootstrapProperties, sonarUserHome));
154+
155+
assertThat(logTester.logs(Level.DEBUG)).doesNotContain("Loading OS trusted SSL certificates...");
156+
}
157+
134158
@Nested
135159
// Workaround until we move to Java 17+ and can make Wiremock extension static
136160
@TestInstance(TestInstance.Lifecycle.PER_CLASS)

0 commit comments

Comments
 (0)