Skip to content

Commit 68fcfd2

Browse files
committed
SCANJLIB-261 Add a scanner property to disable SSL config based on JVM properties
1 parent 8e5ebc0 commit 68fcfd2

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
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
@@ -87,7 +87,7 @@ private ScannerProperties() {
8787
public static final String SONAR_SCANNER_TRUSTSTORE_PATH = "sonar.scanner.truststorePath";
8888
public static final String SONAR_SCANNER_TRUSTSTORE_PASSWORD = "sonar.scanner.truststorePassword";
8989
public static final String SONAR_SCANNER_SKIP_SYSTEM_TRUSTSTORE = "sonar.scanner.skipSystemTruststore";
90-
90+
public static final String SONAR_SCANNER_SKIP_JVM_SSL_CONFIG = "sonar.scanner.skipJvmSslConfig";
9191
/**
9292
* Skip analysis.
9393
*/

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_PROXY_PORT;
5454
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_PROXY_USER;
5555
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_RESPONSE_TIMEOUT;
56+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_SKIP_JVM_SSL_CONFIG;
5657
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_SKIP_SYSTEM_TRUSTSTORE;
5758
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_SOCKET_TIMEOUT;
5859
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_TRUSTSTORE_PASSWORD;
@@ -179,43 +180,48 @@ private static int parseIntProperty(String propValue, String propKey) {
179180
}
180181

181182
private SslConfig loadSslConfig(Map<String, String> bootstrapProperties, Path sonarUserHome) {
182-
var keyStore = loadKeyStoreConfig(bootstrapProperties, sonarUserHome);
183-
var trustStore = loadTrustStoreConfig(bootstrapProperties, sonarUserHome);
183+
var skipJvmSslConfig = Boolean.parseBoolean(defaultIfBlank(bootstrapProperties.get(SONAR_SCANNER_SKIP_JVM_SSL_CONFIG), "false"));
184+
var keyStore = loadKeyStoreConfig(bootstrapProperties, sonarUserHome, skipJvmSslConfig);
185+
var trustStore = loadTrustStoreConfig(bootstrapProperties, sonarUserHome, skipJvmSslConfig);
184186
return new SslConfig(keyStore, trustStore);
185187
}
186188

187189
@Nullable
188-
private CertificateStore loadTrustStoreConfig(Map<String, String> bootstrapProperties, Path sonarUserHome) {
190+
private CertificateStore loadTrustStoreConfig(Map<String, String> bootstrapProperties, Path sonarUserHome, boolean skipJvmSslConfig) {
189191
var trustStorePath = parseFileProperty(bootstrapProperties, SONAR_SCANNER_TRUSTSTORE_PATH, "truststore", sonarUserHome.resolve("ssl/truststore.p12"));
190192
if (trustStorePath != null) {
191193
LOG.debug("Using scanner truststore: {}", trustStorePath);
192194
return new CertificateStore(trustStorePath, bootstrapProperties.get(SONAR_SCANNER_TRUSTSTORE_PASSWORD), false);
193195
}
194-
var jvmTrustStoreProp = system.getProperty(JAVAX_NET_SSL_TRUST_STORE);
195-
if (StringUtils.isNotBlank(jvmTrustStoreProp)) {
196-
LOG.debug("Using JVM truststore: {}", jvmTrustStoreProp);
197-
return new CertificateStore(Paths.get(jvmTrustStoreProp), system.getProperty(JAVAX_NET_SSL_TRUST_STORE_PASSWORD), true);
198-
} else {
199-
var defaultJvmTrustStoreLocation = Paths.get(Objects.requireNonNull(system.getProperty("java.home")), "lib", "security", "cacerts");
200-
if (Files.isRegularFile(defaultJvmTrustStoreLocation)) {
201-
LOG.debug("Using JVM default truststore: {}", defaultJvmTrustStoreLocation);
202-
return new CertificateStore(defaultJvmTrustStoreLocation, Optional.ofNullable(system.getProperty(JAVAX_NET_SSL_TRUST_STORE_PASSWORD)).orElse("changeit"), true);
196+
if (!skipJvmSslConfig) {
197+
var jvmTrustStoreProp = system.getProperty(JAVAX_NET_SSL_TRUST_STORE);
198+
if (StringUtils.isNotBlank(jvmTrustStoreProp)) {
199+
LOG.debug("Using JVM truststore: {}", jvmTrustStoreProp);
200+
return new CertificateStore(Paths.get(jvmTrustStoreProp), system.getProperty(JAVAX_NET_SSL_TRUST_STORE_PASSWORD), true);
201+
} else {
202+
var defaultJvmTrustStoreLocation = Paths.get(Objects.requireNonNull(system.getProperty("java.home")), "lib", "security", "cacerts");
203+
if (Files.isRegularFile(defaultJvmTrustStoreLocation)) {
204+
LOG.debug("Using JVM default truststore: {}", defaultJvmTrustStoreLocation);
205+
return new CertificateStore(defaultJvmTrustStoreLocation, Optional.ofNullable(system.getProperty(JAVAX_NET_SSL_TRUST_STORE_PASSWORD)).orElse("changeit"), true);
206+
}
203207
}
204208
}
205209
return null;
206210
}
207211

208212
@Nullable
209-
private CertificateStore loadKeyStoreConfig(Map<String, String> bootstrapProperties, Path sonarUserHome) {
213+
private CertificateStore loadKeyStoreConfig(Map<String, String> bootstrapProperties, Path sonarUserHome, boolean skipJvmSslConfig) {
210214
var keyStorePath = parseFileProperty(bootstrapProperties, SONAR_SCANNER_KEYSTORE_PATH, "keystore", sonarUserHome.resolve("ssl/keystore.p12"));
211215
if (keyStorePath != null) {
212216
LOG.debug("Using scanner keystore: {}", keyStorePath);
213217
return new CertificateStore(keyStorePath, bootstrapProperties.get(SONAR_SCANNER_KEYSTORE_PASSWORD), false);
214218
}
215-
var jvmKeystoreProp = system.getProperty(JAVAX_NET_SSL_KEY_STORE);
216-
if (StringUtils.isNotBlank(jvmKeystoreProp)) {
217-
LOG.debug("Using JVM keystore: {}", jvmKeystoreProp);
218-
return new CertificateStore(Paths.get(jvmKeystoreProp), system.getProperty(JAVAX_NET_SSL_KEY_STORE_PASSWORD), true);
219+
if (!skipJvmSslConfig) {
220+
var jvmKeystoreProp = system.getProperty(JAVAX_NET_SSL_KEY_STORE);
221+
if (StringUtils.isNotBlank(jvmKeystoreProp)) {
222+
LOG.debug("Using JVM keystore: {}", jvmKeystoreProp);
223+
return new CertificateStore(Paths.get(jvmKeystoreProp), system.getProperty(JAVAX_NET_SSL_KEY_STORE_PASSWORD), true);
224+
}
219225
}
220226
return null;
221227
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ void should_set_ssl_config_from_default_jvm_location() {
114114
assertThat(logTester.logs(Level.DEBUG)).contains("Using JVM default truststore: " + cacerts);
115115
}
116116

117+
@Test
118+
void should_skip_ssl_config_from_jvm_if_property_set() {
119+
logTester.setLevel(Level.DEBUG);
120+
bootstrapProperties.put("sonar.scanner.skipJvmSslConfig", "true");
121+
122+
var underTest = new HttpConfig(bootstrapProperties, sonarUserHome, system);
123+
124+
assertThat(underTest.getSslConfig().getTrustStore()).isNull();
125+
assertThat(underTest.getSslConfig().getKeyStore()).isNull();
126+
}
127+
117128
@Test
118129
void should_set_ssl_config_from_jvm_system_properties(@TempDir Path tempDir) throws IOException {
119130
var jvmTruststore = tempDir.resolve("jvmTrust.p12");

0 commit comments

Comments
 (0)