Skip to content

Commit 5db6b5e

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

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class EnvironmentConfig {
4040
private static final String GENERIC_ENV_PREFIX = "SONAR_SCANNER_";
4141
private static final String SONAR_HOST_URL_ENV_VAR = "SONAR_HOST_URL";
4242
private static final String SONAR_USER_HOME_ENV_VAR = "SONAR_USER_HOME";
43-
static final String TOKEN_ENV_VARIABLE = "SONAR_TOKEN";
43+
public static final String TOKEN_ENV_VARIABLE = "SONAR_TOKEN";
4444

4545
private EnvironmentConfig() {
4646
// only static methods

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,15 @@
5454
import org.sonarsource.scanner.lib.internal.util.VersionUtils;
5555

5656
import static java.util.Optional.ofNullable;
57+
import static org.sonarsource.scanner.lib.EnvironmentConfig.TOKEN_ENV_VARIABLE;
5758
import static org.sonarsource.scanner.lib.ScannerProperties.SCANNER_ARCH;
5859
import static org.sonarsource.scanner.lib.ScannerProperties.SCANNER_OS;
60+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_LOGIN;
5961
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_KEYSTORE_PASSWORD;
6062
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_KEYSTORE_PATH;
6163
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_TRUSTSTORE_PASSWORD;
6264
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_TRUSTSTORE_PATH;
65+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_TOKEN;
6366

6467
/**
6568
* Entry point to run a Sonar analysis programmatically.
@@ -71,6 +74,7 @@ public class ScannerEngineBootstrapper {
7174
private static final String SONARCLOUD_HOST = "https://sonarcloud.io";
7275
private static final String SONARCLOUD_REST_API = "https://api.sonarcloud.io";
7376
static final String SQ_VERSION_NEW_BOOTSTRAPPING = "10.6";
77+
static final String SQ_VERSION_TOKEN_AUTHENTICATION = "10.0";
7478
private static final String JAVAX_NET_SSL_TRUST_STORE = "javax.net.ssl.trustStore";
7579
private static final String JAVAX_NET_SSL_TRUST_STORE_PASSWORD = "javax.net.ssl.trustStorePassword";
7680
private static final String JAVAX_NET_SSL_KEY_STORE = "javax.net.ssl.keyStore";
@@ -138,6 +142,12 @@ public ScannerEngineBootstrapResult bootstrap() {
138142
scannerHttpClient.init(httpConfig);
139143

140144
var serverVersion = !isSonarCloud ? getServerVersion(scannerHttpClient) : null;
145+
146+
if (!isSonarCloud && VersionUtils.isAtLeastIgnoringQualifier(serverVersion, SQ_VERSION_TOKEN_AUTHENTICATION) && Objects.nonNull(httpConfig.getLogin())) {
147+
LOG.warn("Use of '{}' property has been deprecated in favor of '{}' (or the env variable alternative '{}'). Please use the latter when passing a token.", SONAR_LOGIN,
148+
SONAR_TOKEN, TOKEN_ENV_VARIABLE);
149+
}
150+
141151
ScannerEngineFacade scannerFacade;
142152
if (isSonarCloud || VersionUtils.isAtLeastIgnoringQualifier(serverVersion, SQ_VERSION_NEW_BOOTSTRAPPING)) {
143153
var launcher = scannerEngineLauncherFactory.createLauncher(scannerHttpClient, fileCache, immutableProperties);
@@ -162,7 +172,7 @@ private static ScannerEngineBootstrapResult handleException(MessageException e)
162172
var code = httpEx.getCode();
163173
if (code == 401 || code == 403) {
164174
var helpMessage = "Please check the property " + ScannerProperties.SONAR_TOKEN +
165-
" or the environment variable " + EnvironmentConfig.TOKEN_ENV_VARIABLE + ".";
175+
" or the environment variable " + TOKEN_ENV_VARIABLE + ".";
166176
message.append(". ").append(helpMessage);
167177
}
168178
if (code == 407) {

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

+8
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;
@@ -39,6 +40,8 @@
3940
import static java.lang.Integer.parseInt;
4041
import static java.lang.String.format;
4142
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
43+
import static org.sonarsource.scanner.lib.EnvironmentConfig.TOKEN_ENV_VARIABLE;
44+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_LOGIN;
4245
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_CONNECT_TIMEOUT;
4346
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_KEYSTORE_PASSWORD;
4447
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_KEYSTORE_PATH;
@@ -51,6 +54,7 @@
5154
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_SOCKET_TIMEOUT;
5255
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_TRUSTSTORE_PASSWORD;
5356
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_SCANNER_TRUSTSTORE_PATH;
57+
import static org.sonarsource.scanner.lib.ScannerProperties.SONAR_TOKEN;
5458

5559
public class HttpConfig {
5660

@@ -87,6 +91,10 @@ public HttpConfig(Map<String, String> bootstrapProperties, Path sonarUserHome) {
8791
this.restApiBaseUrl = StringUtils.removeEnd(bootstrapProperties.get(ScannerProperties.API_BASE_URL), "/");
8892
this.token = bootstrapProperties.get(ScannerProperties.SONAR_TOKEN);
8993
this.login = bootstrapProperties.get(ScannerProperties.SONAR_LOGIN);
94+
if (Objects.nonNull(this.login) && Objects.nonNull(this.token)) {
95+
LOG.warn("Both '{}' and '{}' (or the '{}' env variable) are set, but only the latter will be used.", SONAR_LOGIN, SONAR_TOKEN, TOKEN_ENV_VARIABLE);
96+
}
97+
9098
this.password = bootstrapProperties.get(ScannerProperties.SONAR_PASSWORD);
9199
this.userAgent = format("%s/%s", bootstrapProperties.get(InternalProperties.SCANNER_APP), bootstrapProperties.get(InternalProperties.SCANNER_APP_VERSION));
92100
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

+25-1
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@
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

7172
@RegisterExtension
72-
private LogTester logTester = new LogTester();
73+
private final LogTester logTester = new LogTester();
7374

7475
private final ScannerHttpClient scannerHttpClient = mock(ScannerHttpClient.class);
7576
private final ScannerEngineLauncherFactory scannerEngineLauncherFactory = mock(ScannerEngineLauncherFactory.class);
@@ -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' (or the env variable alternative " +
146+
"'SONAR_TOKEN'). Please use the latter when passing 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.login' and 'sonar.token' (or the 'SONAR_TOKEN' env variable) are set, but only the latter will be used.");
91+
}
7792

7893
}

0 commit comments

Comments
 (0)