Skip to content

Commit 52c4e6d

Browse files
SCANMAVEN-238 Remove unauthenticated REST API calls to SonarQube during IT (#247)
1 parent 9172a40 commit 52c4e6d

File tree

5 files changed

+27
-73
lines changed

5 files changed

+27
-73
lines changed

its/src/test/java/com/sonar/maven/it/suite/AbstractMavenTest.java

+10-25
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public abstract class AbstractMavenTest {
8181
.addPlugin(FileLocation.of("../property-dump-plugin/target/property-dump-plugin-1-SNAPSHOT.jar"))
8282
.build();
8383

84-
protected HttpConnector wsConnector;
8584
protected WsClient wsClient;
8685

8786
@BeforeAll
@@ -125,11 +124,11 @@ protected static String[] cleanPackageSonarGoal() {
125124

126125
@BeforeEach
127126
public void setUpWsClient() {
128-
wsConnector = HttpConnector.newBuilder()
127+
wsClient = WsClientFactories.getDefault()
128+
.newClient(HttpConnector.newBuilder()
129129
.url(ORCHESTRATOR.getServer().getUrl())
130130
.credentials(Server.ADMIN_LOGIN, Server.ADMIN_PASSWORD)
131-
.build();
132-
wsClient = WsClientFactories.getDefault().newClient(wsConnector);
131+
.build());
133132
}
134133

135134
protected static Version mojoVersion() {
@@ -152,24 +151,24 @@ protected static Version mojoVersion() {
152151
}
153152

154153
@CheckForNull
155-
static Measure getMeasure(String componentKey, String metricKey) {
156-
Measures.ComponentWsResponse response = newWsClient().measures().component(new ComponentRequest()
154+
Measure getMeasure(String componentKey, String metricKey) {
155+
Measures.ComponentWsResponse response = wsClient.measures().component(new ComponentRequest()
157156
.setComponent(componentKey)
158157
.setMetricKeys(singletonList(metricKey)));
159158
List<Measure> measures = response.getComponent().getMeasuresList();
160159
return measures.size() == 1 ? measures.get(0) : null;
161160
}
162161

163162
@CheckForNull
164-
static Integer getMeasureAsInteger(String componentKey, String metricKey) {
163+
Integer getMeasureAsInteger(String componentKey, String metricKey) {
165164
Measure measure = getMeasure(componentKey, metricKey);
166165
return (measure == null) ? null : Integer.parseInt(measure.getValue());
167166
}
168167

169168
@CheckForNull
170-
static Component getComponent(String componentKey) {
169+
Component getComponent(String componentKey) {
171170
try {
172-
return newWsClient().components().show(new ShowRequest().setComponent(componentKey)).getComponent();
171+
return wsClient.components().show(new ShowRequest().setComponent(componentKey)).getComponent();
173172
} catch (HttpException e) {
174173
if (e.code() == 404) {
175174
return null;
@@ -178,19 +177,6 @@ static Component getComponent(String componentKey) {
178177
}
179178
}
180179

181-
static WsClient newWsClient() {
182-
return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder()
183-
.url(ORCHESTRATOR.getServer().getUrl())
184-
.build());
185-
}
186-
187-
static WsClient newAuthenticatedWsClient() {
188-
return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder()
189-
.url(ORCHESTRATOR.getServer().getUrl())
190-
.credentials(Server.ADMIN_LOGIN, Server.ADMIN_PASSWORD)
191-
.build());
192-
}
193-
194180
Version mavenVersion = null;
195181

196182
protected Version getMavenVersion() {
@@ -222,7 +208,7 @@ public BuildResult executeBuildAndValidateWithCE(Build<?> build) {
222208
return validateBuildWithCE(ORCHESTRATOR.executeBuild(build));
223209
}
224210

225-
public static BuildResult validateBuildWithCE(BuildResult result) {
211+
public BuildResult validateBuildWithCE(BuildResult result) {
226212
assertBuildResultStatuses(result, 0);
227213
List<String> ceTaskIds = extractCETaskIds(result);
228214
if (ceTaskIds.isEmpty()) {
@@ -272,10 +258,9 @@ public static List<String> extractCETaskIds(BuildResult result) {
272258
private static final long POLLING_TIME = 500; // 0.5 second
273259
private static final long MAX_WAIT_TIME = 20_000; // 20 seconds
274260

275-
private static void waitForCeTaskToBeFinished(String ceTaskId) {
261+
private void waitForCeTaskToBeFinished(String ceTaskId) {
276262
LOG.info("Waiting for CE task {} to be finished", ceTaskId);
277263
try {
278-
WsClient wsClient = newAuthenticatedWsClient();
279264
long start = System.currentTimeMillis();
280265
while (true) {
281266
TaskStatus status = wsClient.ce().task(new TaskRequest().setId(ceTaskId)).getTask().getStatus();

its/src/test/java/com/sonar/maven/it/suite/JavaTest.java

+1-11
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,11 @@
2424
import com.sonar.orchestrator.version.Version;
2525
import java.io.File;
2626
import java.io.FileInputStream;
27-
import java.io.FileNotFoundException;
2827
import java.io.IOException;
2928
import java.nio.file.Path;
3029
import java.util.Properties;
31-
32-
import org.junit.jupiter.api.AfterEach;
3330
import org.junit.jupiter.api.Test;
3431
import org.junit.jupiter.api.io.TempDir;
35-
import org.sonarqube.ws.client.settings.SetRequest;
3632

3733
import static org.assertj.core.api.Assertions.assertThat;
3834
import static org.assertj.core.data.MapEntry.entry;
@@ -43,11 +39,6 @@ class JavaTest extends AbstractMavenTest {
4339
@TempDir
4440
public Path temp;
4541

46-
@AfterEach
47-
public void cleanup() {
48-
wsClient.settings().set(new SetRequest().setKey("sonar.forceAuthentication").setValue("false"));
49-
}
50-
5142
// MSONAR-83
5243
@Test
5344
void shouldPopulateLibraries() throws IOException {
@@ -203,8 +194,7 @@ void takeFirstToolchainIfMultipleExecutions() throws IOException {
203194
assertThat(props).contains(entry("sonar.java.jdkHome", "fake_jdk_9"));
204195
}
205196

206-
private Properties getProps(File outputProps)
207-
throws FileNotFoundException, IOException {
197+
private Properties getProps(File outputProps) throws IOException {
208198
Properties props = new Properties();
209199
try (FileInputStream fis = new FileInputStream(outputProps)) {
210200
props.load(fis);

its/src/test/java/com/sonar/maven/it/suite/LinksTest.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,9 @@
2222
import com.sonar.maven.it.ItUtils;
2323
import com.sonar.orchestrator.build.MavenBuild;
2424
import com.sonar.orchestrator.container.Server;
25-
2625
import org.junit.jupiter.api.Test;
2726
import org.sonarqube.ws.ProjectLinks;
2827
import org.sonarqube.ws.ProjectLinks.SearchWsResponse;
29-
import org.sonarqube.ws.client.HttpConnector;
30-
import org.sonarqube.ws.client.WsClient;
31-
import org.sonarqube.ws.client.WsClientFactories;
3228
import org.sonarqube.ws.client.projectlinks.SearchRequest;
3329

3430
import static org.assertj.core.api.Assertions.assertThat;
@@ -51,8 +47,7 @@ void shouldUseLinkPropertiesOverPomLinksInMaven() {
5147

5248
private void checkLinks() {
5349
Server server = ORCHESTRATOR.getServer();
54-
WsClient client = newAuthenticatedWsClient();
55-
SearchWsResponse response = client.projectLinks().search(new SearchRequest().setProjectKey("com.sonarsource.it.samples:simple-sample"));
50+
SearchWsResponse response = wsClient.projectLinks().search(new SearchRequest().setProjectKey("com.sonarsource.it.samples:simple-sample"));
5651
if (server.version().isGreaterThanOrEquals(7, 1)) {
5752
// SONAR-10299
5853
assertThat(response.getLinksList())

its/src/test/java/com/sonar/maven/it/suite/MavenTest.java

+8-21
Original file line numberDiff line numberDiff line change
@@ -24,50 +24,40 @@
2424
import com.sonar.orchestrator.build.BuildRunner;
2525
import com.sonar.orchestrator.build.MavenBuild;
2626
import java.io.File;
27+
import java.nio.file.Files;
2728
import java.nio.file.Path;
2829
import java.util.Map;
29-
import org.apache.commons.io.FileUtils;
30-
import org.junit.jupiter.api.AfterEach;
3130
import org.junit.jupiter.api.Assertions;
3231
import org.junit.jupiter.api.Test;
3332
import org.junit.jupiter.api.io.TempDir;
3433
import org.sonarqube.ws.Components;
3534
import org.sonarqube.ws.client.components.ComponentsService;
3635
import org.sonarqube.ws.client.components.ShowRequest;
37-
import org.sonarqube.ws.client.settings.SetRequest;
3836
import org.sonarqube.ws.client.users.CreateRequest;
3937

38+
import static java.nio.charset.StandardCharsets.UTF_8;
4039
import static org.assertj.core.api.Assertions.assertThat;
4140

4241
class MavenTest extends AbstractMavenTest {
4342

4443
private static final String MODULE_START = "------------- Run sensors on module ";
4544

46-
@TempDir
47-
public Path temp;
48-
49-
@AfterEach
50-
public void cleanup() {
51-
wsClient.settings().set(new SetRequest().setKey("sonar.forceAuthentication").setValue("false"));
52-
}
53-
5445
/**
5546
* See MSONAR-129
5647
*/
5748
@Test
58-
void useUserPropertiesGlobalConfig() throws Exception {
49+
void useUserPropertiesGlobalConfig(@TempDir Path temp) throws Exception {
5950
BuildRunner runner = new BuildRunner(ORCHESTRATOR.getConfiguration());
6051
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/maven-only-test-dir"))
6152
.setGoals(cleanSonarGoal());
6253

63-
File settingsXml = temp.resolve("settings.xml").toFile();
64-
settingsXml.createNewFile();
54+
Path settingsXml = temp.resolve("settings.xml").toAbsolutePath();
6555
Map<String, String> props = ORCHESTRATOR.getDatabase().getSonarProperties();
6656
props.put("sonar.host.url", ORCHESTRATOR.getServer().getUrl());
6757
props.put("sonar.login", ORCHESTRATOR.getDefaultAdminToken());
68-
FileUtils.write(settingsXml, ItUtils.createSettingsXml(props));
58+
Files.write(settingsXml, ItUtils.createSettingsXml(props).getBytes(UTF_8));
6959

70-
build.addArgument("--settings=" + settingsXml.getAbsolutePath());
60+
build.addArgument("--settings=" + settingsXml);
7161
build.addArgument("-Psonar");
7262
// we build without sonarqube server settings, it will need to fetch it from the profile defined in the settings xml file
7363
BuildResult result = runner.run(null, build);
@@ -420,9 +410,8 @@ void shouldSkipWithEnvVar() {
420410
* MSONAR-141
421411
*/
422412
@Test
423-
void supportMavenEncryption() throws Exception {
413+
void supportMavenEncryption() {
424414
Assertions.assertDoesNotThrow(() -> {
425-
wsClient.settings().set(new SetRequest().setKey("sonar.forceAuthentication").setValue("true"));
426415
wsClient.users().create(new CreateRequest().setLogin("julien").setName("Julien").setPassword("123abc"));
427416

428417
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/maven-only-test-dir"))
@@ -441,10 +430,9 @@ void supportMavenEncryption() throws Exception {
441430
}
442431

443432
@Test
444-
void supportMavenEncryptionWithDefaultSecuritySettings() throws Exception {
433+
void supportMavenEncryptionWithDefaultSecuritySettings() {
445434
// Should fail because settings-security.xml is missing
446435
Assertions.assertThrows(Exception.class, () -> {
447-
wsClient.settings().set(new SetRequest().setKey("sonar.forceAuthentication").setValue("true"));
448436
wsClient.users().create(new CreateRequest().setLogin("julien3").setName("Julien3").setPassword("123abc"));
449437

450438
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/maven-only-test-dir"))
@@ -460,7 +448,6 @@ void supportMavenEncryptionWithDefaultSecuritySettings() throws Exception {
460448
});
461449

462450
Assertions.assertDoesNotThrow(() -> {
463-
wsClient.settings().set(new SetRequest().setKey("sonar.forceAuthentication").setValue("true"));
464451
wsClient.users().create(new CreateRequest().setLogin("julien2").setName("Julien2").setPassword("123abc"));
465452

466453
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/maven-only-test-dir"))

its/src/test/java/com/sonar/maven/it/suite/ProxyTest.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
*/
2020
package com.sonar.maven.it.suite;
2121

22-
import static org.assertj.core.api.Assertions.assertThat;
23-
22+
import com.sonar.maven.it.ItUtils;
23+
import com.sonar.maven.it.Proxy;
24+
import com.sonar.orchestrator.build.BuildResult;
25+
import com.sonar.orchestrator.build.MavenBuild;
2426
import java.io.IOException;
2527
import java.net.URISyntaxException;
2628
import java.nio.charset.StandardCharsets;
@@ -29,20 +31,15 @@
2931
import java.nio.file.Paths;
3032
import java.util.List;
3133
import java.util.stream.Collectors;
32-
33-
import com.sonar.maven.it.ItUtils;
34-
import com.sonar.maven.it.Proxy;
35-
import com.sonar.orchestrator.build.BuildResult;
36-
import com.sonar.orchestrator.build.MavenBuild;
3734
import org.junit.jupiter.api.AfterEach;
3835
import org.junit.jupiter.api.BeforeEach;
3936
import org.junit.jupiter.api.Test;
4037
import org.junit.jupiter.api.io.TempDir;
4138

39+
import static org.assertj.core.api.Assertions.assertThat;
40+
4241
class ProxyTest extends AbstractMavenTest {
4342
private Proxy proxy;
44-
@TempDir
45-
public Path temp;
4643

4744
@BeforeEach
4845
public void prepare() throws Exception {
@@ -58,7 +55,7 @@ public void after() throws Exception {
5855
}
5956

6057
@Test
61-
void useActiveProxyInSettings() throws IOException, URISyntaxException, InterruptedException {
58+
void useActiveProxyInSettings(@TempDir Path temp) throws IOException, URISyntaxException, InterruptedException {
6259
Thread.sleep(2000);
6360
Path proxyXml = Paths.get(this.getClass().getResource("/proxy-settings.xml").toURI());
6461
Path proxyXmlPatched = temp.resolve("settings.xml");

0 commit comments

Comments
 (0)