2020package org .sonarsource .scanner .lib ;
2121
2222import java .net .InetSocketAddress ;
23- import java .nio .file .Files ;
2423import java .nio .file .Path ;
2524import java .nio .file .Paths ;
2625import java .time .temporal .ChronoUnit ;
5352import org .sonarsource .scanner .lib .internal .util .System2 ;
5453import org .sonarsource .scanner .lib .internal .util .VersionUtils ;
5554
56- import static java .util .Optional .ofNullable ;
5755import static org .sonarsource .scanner .lib .EnvironmentConfig .TOKEN_ENV_VARIABLE ;
5856import static org .sonarsource .scanner .lib .ScannerProperties .SCANNER_ARCH ;
5957import static org .sonarsource .scanner .lib .ScannerProperties .SCANNER_OS ;
6361import static org .sonarsource .scanner .lib .ScannerProperties .SONAR_SCANNER_TRUSTSTORE_PASSWORD ;
6462import static org .sonarsource .scanner .lib .ScannerProperties .SONAR_SCANNER_TRUSTSTORE_PATH ;
6563import static org .sonarsource .scanner .lib .ScannerProperties .SONAR_TOKEN ;
64+ import static org .sonarsource .scanner .lib .internal .JvmProperties .HTTPS_PROXY_HOST ;
65+ import static org .sonarsource .scanner .lib .internal .JvmProperties .HTTPS_PROXY_PORT ;
66+ import static org .sonarsource .scanner .lib .internal .JvmProperties .HTTP_PROXY_HOST ;
67+ import static org .sonarsource .scanner .lib .internal .JvmProperties .HTTP_PROXY_PORT ;
68+ import static org .sonarsource .scanner .lib .internal .JvmProperties .JAVAX_NET_SSL_KEY_STORE ;
69+ import static org .sonarsource .scanner .lib .internal .JvmProperties .JAVAX_NET_SSL_KEY_STORE_PASSWORD ;
70+ import static org .sonarsource .scanner .lib .internal .JvmProperties .JAVAX_NET_SSL_TRUST_STORE ;
71+ import static org .sonarsource .scanner .lib .internal .JvmProperties .JAVAX_NET_SSL_TRUST_STORE_PASSWORD ;
6672
6773/**
6874 * Entry point to run a Sonar analysis programmatically.
@@ -75,10 +81,6 @@ public class ScannerEngineBootstrapper {
7581 private static final String SONARCLOUD_REST_API = "https://api.sonarcloud.io" ;
7682 static final String SQ_VERSION_NEW_BOOTSTRAPPING = "10.6" ;
7783 static final String SQ_VERSION_TOKEN_AUTHENTICATION = "10.0" ;
78- private static final String JAVAX_NET_SSL_TRUST_STORE = "javax.net.ssl.trustStore" ;
79- private static final String JAVAX_NET_SSL_TRUST_STORE_PASSWORD = "javax.net.ssl.trustStorePassword" ;
80- private static final String JAVAX_NET_SSL_KEY_STORE = "javax.net.ssl.keyStore" ;
81- private static final String JAVAX_NET_SSL_KEY_STORE_PASSWORD = "javax.net.ssl.keyStorePassword" ;
8284
8385 private final IsolatedLauncherFactory launcherFactory ;
8486 private final ScannerEngineLauncherFactory scannerEngineLauncherFactory ;
@@ -124,11 +126,11 @@ public ScannerEngineBootstrapResult bootstrap() {
124126 LOG .debug ("Scanner max available memory: {}" , FileUtils .byteCountToDisplaySize (Runtime .getRuntime ().maxMemory ()));
125127 }
126128 initBootstrapDefaultValues ();
127- adaptJvmSslPropertiesToScannerProperties (bootstrapProperties , system );
128129 var immutableProperties = Map .copyOf (bootstrapProperties );
130+ var sonarUserHome = resolveSonarUserHome (immutableProperties );
131+ var httpConfig = new HttpConfig (immutableProperties , sonarUserHome , system );
129132 var isSonarCloud = isSonarCloud (immutableProperties );
130133 var isSimulation = immutableProperties .containsKey (InternalProperties .SCANNER_DUMP_TO_FILE );
131- var sonarUserHome = resolveSonarUserHome (immutableProperties );
132134 var fileCache = FileCache .create (sonarUserHome );
133135
134136 if (isSimulation ) {
@@ -138,7 +140,6 @@ public ScannerEngineBootstrapResult bootstrap() {
138140
139141 // No HTTP call should be made before this point
140142 try {
141- var httpConfig = new HttpConfig (immutableProperties , sonarUserHome );
142143 scannerHttpClient .init (httpConfig );
143144
144145 var serverVersion = !isSonarCloud ? getServerVersion (scannerHttpClient ) : null ;
@@ -151,7 +152,10 @@ public ScannerEngineBootstrapResult bootstrap() {
151152 ScannerEngineFacade scannerFacade ;
152153 if (isSonarCloud || VersionUtils .isAtLeastIgnoringQualifier (serverVersion , SQ_VERSION_NEW_BOOTSTRAPPING )) {
153154 var launcher = scannerEngineLauncherFactory .createLauncher (scannerHttpClient , fileCache , immutableProperties );
154- scannerFacade = new NewScannerEngineFacade (immutableProperties , launcher , isSonarCloud , serverVersion );
155+
156+ var adaptedProperties = adaptSslPropertiesToScannerProperties (immutableProperties , httpConfig );
157+
158+ scannerFacade = new NewScannerEngineFacade (adaptedProperties , launcher , isSonarCloud , serverVersion );
155159 } else {
156160 var launcher = launcherFactory .createLauncher (scannerHttpClient , fileCache );
157161 var adaptedProperties = adaptDeprecatedPropertiesForInProcessBootstrapping (immutableProperties , httpConfig );
@@ -215,11 +219,12 @@ Map<String, String> adaptDeprecatedPropertiesForInProcessBootstrapping(Map<Strin
215219 }
216220 var proxy = httpConfig .getProxy ();
217221 if (proxy != null ) {
218- setSystemPropertyIfNotAlreadySet ("http.proxyHost" , ((InetSocketAddress ) proxy .address ()).getHostString ());
219- setSystemPropertyIfNotAlreadySet ("https.proxyHost" , ((InetSocketAddress ) proxy .address ()).getHostString ());
220- setSystemPropertyIfNotAlreadySet ("http.proxyPort" , "" + (( InetSocketAddress ) proxy .address ()).getPort ());
221- setSystemPropertyIfNotAlreadySet ("https.proxyPort" , "" + (( InetSocketAddress ) proxy .address ()).getPort ());
222+ setSystemPropertyIfNotAlreadySet (HTTP_PROXY_HOST , ((InetSocketAddress ) proxy .address ()).getHostString ());
223+ setSystemPropertyIfNotAlreadySet (HTTPS_PROXY_HOST , ((InetSocketAddress ) proxy .address ()).getHostString ());
224+ setSystemPropertyIfNotAlreadySet (HTTP_PROXY_PORT , String . valueOf ((( InetSocketAddress ) proxy .address ()).getPort () ));
225+ setSystemPropertyIfNotAlreadySet (HTTPS_PROXY_PORT , String . valueOf ((( InetSocketAddress ) proxy .address ()).getPort () ));
222226 }
227+ // Those are not standard JVM properties, but they are supported by the Scanner Engine.
223228 setSystemPropertyIfNotAlreadySet ("http.proxyUser" , httpConfig .getProxyUser ());
224229 setSystemPropertyIfNotAlreadySet ("http.proxyPassword" , httpConfig .getProxyPassword ());
225230
@@ -237,7 +242,7 @@ Map<String, String> adaptDeprecatedPropertiesForInProcessBootstrapping(Map<Strin
237242 return Map .copyOf (adaptedProperties );
238243 }
239244
240- private void setSystemPropertyIfNotAlreadySet (String key , String value ) {
245+ private void setSystemPropertyIfNotAlreadySet (String key , @ Nullable String value ) {
241246 if (system .getProperty (key ) == null && StringUtils .isNotBlank (value )) {
242247 System .setProperty (key , value );
243248 }
@@ -290,30 +295,20 @@ private void initBootstrapDefaultValues() {
290295 * by inserting the trusted certificate in the Scanner JVM truststore, or passing JVM SSL properties
291296 * we need to adapt the properties, at least temporarily, until we have helped most users to migrate.
292297 */
293- static void adaptJvmSslPropertiesToScannerProperties (Map <String , String > bootstrapProperties , System2 system ) {
294- if (!bootstrapProperties .containsKey (SONAR_SCANNER_TRUSTSTORE_PATH )) {
295- var jvmTrustStoreProp = system .getProperty (JAVAX_NET_SSL_TRUST_STORE );
296- if (StringUtils .isBlank (jvmTrustStoreProp )) {
297- var defaultJvmTrustStoreLocation = Paths .get (System .getProperty ("java.home" ), "lib" , "security" , "cacerts" );
298- if (Files .isRegularFile (defaultJvmTrustStoreLocation )) {
299- LOG .debug ("Mapping default scanner JVM truststore location '{}' to new properties" , defaultJvmTrustStoreLocation );
300- bootstrapProperties .put (SONAR_SCANNER_TRUSTSTORE_PATH , defaultJvmTrustStoreLocation .toString ());
301- bootstrapProperties .putIfAbsent (SONAR_SCANNER_TRUSTSTORE_PASSWORD , System .getProperty (JAVAX_NET_SSL_TRUST_STORE_PASSWORD , "changeit" ));
302- }
303- } else {
304- bootstrapProperties .putIfAbsent (SONAR_SCANNER_TRUSTSTORE_PATH , jvmTrustStoreProp );
305- ofNullable (system .getProperty (JAVAX_NET_SSL_TRUST_STORE_PASSWORD ))
306- .ifPresent (password -> bootstrapProperties .putIfAbsent (SONAR_SCANNER_TRUSTSTORE_PASSWORD , password ));
307- }
298+ static Map <String , String > adaptSslPropertiesToScannerProperties (Map <String , String > bootstrapProperties , HttpConfig httpConfig ) {
299+ var result = new HashMap <>(bootstrapProperties );
300+ var keyStore = httpConfig .getSslConfig ().getKeyStore ();
301+ if (keyStore != null && keyStore .isFromJvm ()) {
302+ result .put (SONAR_SCANNER_KEYSTORE_PATH , keyStore .getPath ().toString ());
303+ keyStore .getKeyStorePassword ().ifPresent (password -> result .put (SONAR_SCANNER_KEYSTORE_PASSWORD , password ));
308304 }
309- if (!bootstrapProperties .containsKey (SONAR_SCANNER_KEYSTORE_PATH )) {
310- var keystoreProp = system .getProperty (JAVAX_NET_SSL_KEY_STORE );
311- if (!StringUtils .isBlank (keystoreProp )) {
312- bootstrapProperties .put (SONAR_SCANNER_KEYSTORE_PATH , keystoreProp );
313- ofNullable (system .getProperty (JAVAX_NET_SSL_KEY_STORE_PASSWORD ))
314- .ifPresent (password -> bootstrapProperties .putIfAbsent (SONAR_SCANNER_KEYSTORE_PASSWORD , password ));
315- }
305+
306+ var trustStore = httpConfig .getSslConfig ().getTrustStore ();
307+ if (trustStore != null && trustStore .isFromJvm ()) {
308+ result .put (SONAR_SCANNER_TRUSTSTORE_PATH , trustStore .getPath ().toString ());
309+ trustStore .getKeyStorePassword ().ifPresent (password -> result .put (SONAR_SCANNER_TRUSTSTORE_PASSWORD , password ));
316310 }
311+ return Map .copyOf (result );
317312 }
318313
319314 private String getSonarCloudUrl () {
0 commit comments