Skip to content

Commit b229bba

Browse files
SCANJLIB-230 Add warning when sonar.login (and sonar.token simultaneously) is used
1 parent 754c3c1 commit b229bba

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@
5656
import static java.util.Optional.ofNullable;
5757
import static org.sonarsource.scanner.lib.ScannerProperties.SCANNER_ARCH;
5858
import static org.sonarsource.scanner.lib.ScannerProperties.SCANNER_OS;
59+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_LOGIN;
5960
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_KEYSTORE_PASSWORD;
6061
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_KEYSTORE_PATH;
6162
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_TRUSTSTORE_PASSWORD;
6263
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_TRUSTSTORE_PATH;
64+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_TOKEN;
6365

6466
/**
6567
* Entry point to run a Sonar analysis programmatically.
@@ -71,6 +73,7 @@ public class ScannerEngineBootstrapper {
7173
private static final String SONARCLOUD_HOST = "https://sonarcloud.io";
7274
private static final String SONARCLOUD_REST_API = "https://api.sonarcloud.io";
7375
static final String SQ_VERSION_NEW_BOOTSTRAPPING = "10.6";
76+
static final String SQ_VERSION_TOKEN_AUTHENTICATION = "10.0";
7477
private static final String JAVAX_NET_SSL_TRUST_STORE = "javax.net.ssl.trustStore";
7578
private static final String JAVAX_NET_SSL_TRUST_STORE_PASSWORD = "javax.net.ssl.trustStorePassword";
7679
private static final String JAVAX_NET_SSL_KEY_STORE = "javax.net.ssl.keyStore";
@@ -138,6 +141,11 @@ public ScannerEngineBootstrapResult bootstrap() {
138141
scannerHttpClient.init(httpConfig);
139142

140143
var serverVersion = !isSonarCloud ? getServerVersion(scannerHttpClient) : null;
144+
145+
if (!isSonarCloud && VersionUtils.isAtLeastIgnoringQualifier(serverVersion, SQ_VERSION_TOKEN_AUTHENTICATION) && Objects.nonNull(httpConfig.getLogin())) {
146+
LOG.warn("Use of {} property has been deprecated in favor of {}. Please use the {} property when passing a token.", SONAR_LOGIN, SONAR_TOKEN, SONAR_TOKEN);
147+
}
148+
141149
ScannerEngineFacade scannerFacade;
142150
if (isSonarCloud || VersionUtils.isAtLeastIgnoringQualifier(serverVersion, SQ_VERSION_NEW_BOOTSTRAPPING)) {
143151
var launcher = scannerEngineLauncherFactory.createLauncher(scannerHttpClient, fileCache, immutableProperties);

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

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.time.Duration;
2828
import java.time.format.DateTimeParseException;
2929
import java.util.Map;
30+
import java.util.Objects;
3031
import javax.annotation.Nullable;
3132
import org.apache.commons.lang3.StringUtils;
3233
import org.slf4j.Logger;
@@ -87,6 +88,10 @@ public HttpConfig(Map<String, String> bootstrapProperties, Path sonarUserHome) {
8788
this.restApiBaseUrl = StringUtils.removeEnd(bootstrapProperties.get(ScannerProperties.API_BASE_URL), "/");
8889
this.token = bootstrapProperties.get(ScannerProperties.SONAR_TOKEN);
8990
this.login = bootstrapProperties.get(ScannerProperties.SONAR_LOGIN);
91+
if (Objects.nonNull(this.login) && Objects.nonNull(this.token)) {
92+
LOG.warn("Both sonar.token and sonar.login properties are set, but only sonar.token will be used.");
93+
}
94+
9095
this.password = bootstrapProperties.get(ScannerProperties.SONAR_PASSWORD);
9196
this.userAgent = format("%s/%s", bootstrapProperties.get(InternalProperties.SCANNER_APP), bootstrapProperties.get(InternalProperties.SCANNER_APP_VERSION));
9297
this.socketTimeout = loadDuration(bootstrapProperties, SONAR_SCANNER_SOCKET_TIMEOUT, READ_TIMEOUT_SEC_PROPERTY, DEFAULT_READ_TIMEOUT_SEC);

lib/src/test/java/org/sonarsource/scanner/lib/ScannerEngineBootstrapperTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import static org.mockito.Mockito.verify;
6666
import static org.mockito.Mockito.when;
6767
import static org.sonarsource.scanner.lib.ScannerEngineBootstrapper.SQ_VERSION_NEW_BOOTSTRAPPING;
68+
import static org.sonarsource.scanner.lib.ScannerEngineBootstrapper.SQ_VERSION_TOKEN_AUTHENTICATION;
6869

6970
class ScannerEngineBootstrapperTest {
7071

@@ -122,6 +123,29 @@ void should_use_new_bootstrapping_with_sonarqube_10_6() throws Exception {
122123
assertThat(bootstrapResult.getEngineFacade().isSonarCloud()).isFalse();
123124
verifySonarQubeServerTypeLogged(SQ_VERSION_NEW_BOOTSTRAPPING);
124125
assertThat(bootstrapResult.getEngineFacade().getServerVersion()).isEqualTo(SQ_VERSION_NEW_BOOTSTRAPPING);
126+
assertThat(logTester.logs(Level.WARN)).isEmpty();
127+
}
128+
}
129+
130+
@Test
131+
void should_issue_deprecation_warning_for_sonar_login_property_sonarqube_10_0() throws Exception {
132+
IsolatedLauncherFactory launcherFactory = mock(IsolatedLauncherFactory.class);
133+
when(launcherFactory.createLauncher(eq(scannerHttpClient), any(FileCache.class)))
134+
.thenReturn(mock(IsolatedLauncherFactory.IsolatedLauncherAndClassloader.class));
135+
136+
ScannerEngineBootstrapper bootstrapper = new ScannerEngineBootstrapper("Gradle", "3.1", system, scannerHttpClient,
137+
launcherFactory, scannerEngineLauncherFactory);
138+
when(scannerHttpClient.callRestApi("/analysis/version")).thenThrow(new HttpException(URI.create("http://myserver").toURL(), 404, "Not Found", null));
139+
when(scannerHttpClient.callWebApi("/api/server/version")).thenReturn(SQ_VERSION_TOKEN_AUTHENTICATION);
140+
141+
try (var bootstrapResult = bootstrapper.setBootstrapProperty(ScannerProperties.HOST_URL, "http://localhost").setBootstrapProperty(ScannerProperties.SONAR_LOGIN,
142+
"mockTokenValue").bootstrap()) {
143+
verify(launcherFactory).createLauncher(eq(scannerHttpClient), any(FileCache.class));
144+
assertThat(bootstrapResult.getEngineFacade().isSonarCloud()).isFalse();
145+
assertThat(logTester.logs(Level.WARN)).contains("Use of sonar.login property has been deprecated in favor of sonar.token. Please use the sonar.token property when passing " +
146+
"a token.");
147+
verifySonarQubeServerTypeLogged(SQ_VERSION_TOKEN_AUTHENTICATION);
148+
assertThat(bootstrapResult.getEngineFacade().getServerVersion()).isEqualTo(SQ_VERSION_TOKEN_AUTHENTICATION);
125149
}
126150
}
127151

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

+15
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
import java.util.Map;
2727
import org.junit.jupiter.api.BeforeEach;
2828
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.extension.RegisterExtension;
2930
import org.junit.jupiter.api.io.TempDir;
31+
import org.slf4j.event.Level;
32+
import testutils.LogTester;
3033

3134
import static org.assertj.core.api.Assertions.assertThat;
3235
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -37,6 +40,9 @@ class HttpConfigTest {
3740

3841
private final Map<String, String> bootstrapProperties = new HashMap<>();
3942

43+
@RegisterExtension
44+
private final LogTester logTester = new LogTester();
45+
4046
@TempDir
4147
private Path sonarUserHomeDir;
4248
private Path sonarUserHome;
@@ -74,5 +80,14 @@ void it_should_throw_if_invalid_proxy_port() {
7480
.hasMessage("sonar.scanner.proxyPort is not a valid integer: not_a_number");
7581
}
7682

83+
@Test
84+
void should_warn_if_both_login_and_token_properties_set() {
85+
bootstrapProperties.put("sonar.login", "mockTokenValue");
86+
bootstrapProperties.put("sonar.token", "mockTokenValue");
87+
88+
new HttpConfig(bootstrapProperties, sonarUserHome);
89+
90+
assertThat(logTester.logs(Level.WARN)).contains("Both sonar.token and sonar.login properties are set, but only sonar.token will be used.");
91+
}
7792

7893
}

0 commit comments

Comments
 (0)