Skip to content

Commit f0791e2

Browse files
committed
Add support of auth with sonar.login/sonar.password
1 parent 24971b2 commit f0791e2

3 files changed

Lines changed: 56 additions & 7 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,14 @@ public interface ScannerProperties {
4646
*/
4747
String SONAR_TOKEN = "sonar.token";
4848

49+
/**
50+
* Authentication username for connecting to the Sonar server.
51+
*/
52+
String SONAR_LOGIN = "sonar.login";
53+
54+
/**
55+
* Authentication password for connecting to the Sonar server.
56+
*/
57+
String SONAR_PASSWORD = "sonar.password";
58+
4959
}

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@
3939
import org.sonarsource.scanner.lib.internal.cache.Logger;
4040

4141
import static java.lang.String.format;
42-
import static java.nio.charset.StandardCharsets.UTF_8;
4342

4443
public class ServerConnection {
4544

4645
private final String baseUrlWithoutTrailingSlash;
4746
private final String userAgent;
4847
private final OkHttpClient httpClient;
4948

50-
private final String token;
49+
@Nullable
50+
private final String credentials;
5151
private final Logger logger;
5252

53-
ServerConnection(String baseUrl, String userAgent, @Nullable String token, Logger logger, Map<String, String> bootstrapProperties, SonarUserHome sonarUserHome) {
54-
this.token = token;
53+
ServerConnection(String baseUrl, String userAgent, @Nullable String credentials, Logger logger, Map<String, String> bootstrapProperties, SonarUserHome sonarUserHome) {
54+
this.credentials = credentials;
5555
this.logger = logger;
5656
this.baseUrlWithoutTrailingSlash = removeTrailingSlash(baseUrl);
5757
this.userAgent = userAgent;
@@ -66,7 +66,12 @@ public static ServerConnection create(Map<String, String> bootstrapProperties, L
6666
String serverUrl = bootstrapProperties.get("sonar.host.url");
6767
String userAgent = format("%s/%s", bootstrapProperties.get(InternalProperties.SCANNER_APP), bootstrapProperties.get(InternalProperties.SCANNER_APP_VERSION));
6868
String token = bootstrapProperties.get(ScannerProperties.SONAR_TOKEN);
69-
return new ServerConnection(serverUrl, userAgent, token, logger, bootstrapProperties, sonarUserHome);
69+
String login = bootstrapProperties.getOrDefault(ScannerProperties.SONAR_LOGIN, token);
70+
String credentials = null;
71+
if (login != null) {
72+
credentials = Credentials.basic(login, bootstrapProperties.getOrDefault(ScannerProperties.SONAR_PASSWORD, ""));
73+
}
74+
return new ServerConnection(serverUrl, userAgent, credentials, logger, bootstrapProperties, sonarUserHome);
7075
}
7176

7277
/**
@@ -118,8 +123,8 @@ private ResponseBody callUrl(String url) throws IOException {
118123
.get()
119124
.url(url)
120125
.addHeader("User-Agent", userAgent);
121-
if (token != null) {
122-
requestBuilder.header("Authorization", Credentials.basic(token, "", UTF_8));
126+
if (credentials != null) {
127+
requestBuilder.header("Authorization", credentials);
123128
}
124129
Request request = requestBuilder.build();
125130
Response response = httpClient.newCall(request).execute();

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.nio.charset.StandardCharsets;
2424
import java.nio.file.Files;
2525
import java.nio.file.Path;
26+
import java.util.Base64;
2627
import java.util.HashMap;
2728
import java.util.Map;
2829
import org.junit.jupiter.api.Test;
@@ -33,7 +34,9 @@
3334

3435
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
3536
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
37+
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
3638
import static com.github.tomakehurst.wiremock.client.WireMock.get;
39+
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
3740
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
3841
import static java.lang.String.format;
3942
import static org.assertj.core.api.Assertions.assertThat;
@@ -129,6 +132,37 @@ void should_support_server_url_with_trailing_slash() throws Exception {
129132
assertThat(content).isEqualTo(HELLO_WORLD);
130133
}
131134

135+
@Test
136+
void should_authenticate_with_token() throws Exception {
137+
Map<String, String> props = new HashMap<>();
138+
props.put("sonar.host.url", sonarqube.baseUrl());
139+
props.put("sonar.token", "some_token");
140+
ServerConnection connection = ServerConnection.create(props, logger, new SonarUserHome(sonarUserHome));
141+
142+
answer(HELLO_WORLD);
143+
String content = connection.downloadString("/batch/index.txt");
144+
assertThat(content).isEqualTo(HELLO_WORLD);
145+
146+
sonarqube.verify(getRequestedFor(anyUrl())
147+
.withHeader("Authorization", equalTo("Basic " + Base64.getEncoder().encodeToString("some_token:".getBytes(StandardCharsets.UTF_8)))));
148+
}
149+
150+
@Test
151+
void should_authenticate_with_username_password() throws Exception {
152+
Map<String, String> props = new HashMap<>();
153+
props.put("sonar.host.url", sonarqube.baseUrl());
154+
props.put("sonar.login", "some_username");
155+
props.put("sonar.password", "some_password");
156+
ServerConnection connection = ServerConnection.create(props, logger, new SonarUserHome(sonarUserHome));
157+
158+
answer(HELLO_WORLD);
159+
String content = connection.downloadString("/batch/index.txt");
160+
assertThat(content).isEqualTo(HELLO_WORLD);
161+
162+
sonarqube.verify(getRequestedFor(anyUrl())
163+
.withHeader("Authorization", equalTo("Basic " + Base64.getEncoder().encodeToString("some_username:some_password".getBytes(StandardCharsets.UTF_8)))));
164+
}
165+
132166
private ServerConnection create() {
133167
return new ServerConnection(sonarqube.baseUrl(), "user-agent", null, logger, Map.of(), new SonarUserHome(sonarUserHome));
134168
}

0 commit comments

Comments
 (0)