Skip to content

Commit

Permalink
SCANMAVEN-238 Remove unauthenticated REST API calls to SonarQube duri…
Browse files Browse the repository at this point in the history
…ng IT (#247)
  • Loading branch information
alban-auzeill authored Oct 28, 2024
1 parent 9172a40 commit 52c4e6d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 73 deletions.
35 changes: 10 additions & 25 deletions its/src/test/java/com/sonar/maven/it/suite/AbstractMavenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public abstract class AbstractMavenTest {
.addPlugin(FileLocation.of("../property-dump-plugin/target/property-dump-plugin-1-SNAPSHOT.jar"))
.build();

protected HttpConnector wsConnector;
protected WsClient wsClient;

@BeforeAll
Expand Down Expand Up @@ -125,11 +124,11 @@ protected static String[] cleanPackageSonarGoal() {

@BeforeEach
public void setUpWsClient() {
wsConnector = HttpConnector.newBuilder()
wsClient = WsClientFactories.getDefault()
.newClient(HttpConnector.newBuilder()
.url(ORCHESTRATOR.getServer().getUrl())
.credentials(Server.ADMIN_LOGIN, Server.ADMIN_PASSWORD)
.build();
wsClient = WsClientFactories.getDefault().newClient(wsConnector);
.build());
}

protected static Version mojoVersion() {
Expand All @@ -152,24 +151,24 @@ protected static Version mojoVersion() {
}

@CheckForNull
static Measure getMeasure(String componentKey, String metricKey) {
Measures.ComponentWsResponse response = newWsClient().measures().component(new ComponentRequest()
Measure getMeasure(String componentKey, String metricKey) {
Measures.ComponentWsResponse response = wsClient.measures().component(new ComponentRequest()
.setComponent(componentKey)
.setMetricKeys(singletonList(metricKey)));
List<Measure> measures = response.getComponent().getMeasuresList();
return measures.size() == 1 ? measures.get(0) : null;
}

@CheckForNull
static Integer getMeasureAsInteger(String componentKey, String metricKey) {
Integer getMeasureAsInteger(String componentKey, String metricKey) {
Measure measure = getMeasure(componentKey, metricKey);
return (measure == null) ? null : Integer.parseInt(measure.getValue());
}

@CheckForNull
static Component getComponent(String componentKey) {
Component getComponent(String componentKey) {
try {
return newWsClient().components().show(new ShowRequest().setComponent(componentKey)).getComponent();
return wsClient.components().show(new ShowRequest().setComponent(componentKey)).getComponent();
} catch (HttpException e) {
if (e.code() == 404) {
return null;
Expand All @@ -178,19 +177,6 @@ static Component getComponent(String componentKey) {
}
}

static WsClient newWsClient() {
return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder()
.url(ORCHESTRATOR.getServer().getUrl())
.build());
}

static WsClient newAuthenticatedWsClient() {
return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder()
.url(ORCHESTRATOR.getServer().getUrl())
.credentials(Server.ADMIN_LOGIN, Server.ADMIN_PASSWORD)
.build());
}

Version mavenVersion = null;

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

public static BuildResult validateBuildWithCE(BuildResult result) {
public BuildResult validateBuildWithCE(BuildResult result) {
assertBuildResultStatuses(result, 0);
List<String> ceTaskIds = extractCETaskIds(result);
if (ceTaskIds.isEmpty()) {
Expand Down Expand Up @@ -272,10 +258,9 @@ public static List<String> extractCETaskIds(BuildResult result) {
private static final long POLLING_TIME = 500; // 0.5 second
private static final long MAX_WAIT_TIME = 20_000; // 20 seconds

private static void waitForCeTaskToBeFinished(String ceTaskId) {
private void waitForCeTaskToBeFinished(String ceTaskId) {
LOG.info("Waiting for CE task {} to be finished", ceTaskId);
try {
WsClient wsClient = newAuthenticatedWsClient();
long start = System.currentTimeMillis();
while (true) {
TaskStatus status = wsClient.ce().task(new TaskRequest().setId(ceTaskId)).getTask().getStatus();
Expand Down
12 changes: 1 addition & 11 deletions its/src/test/java/com/sonar/maven/it/suite/JavaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,11 @@
import com.sonar.orchestrator.version.Version;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Properties;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.sonarqube.ws.client.settings.SetRequest;

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

@AfterEach
public void cleanup() {
wsClient.settings().set(new SetRequest().setKey("sonar.forceAuthentication").setValue("false"));
}

// MSONAR-83
@Test
void shouldPopulateLibraries() throws IOException {
Expand Down Expand Up @@ -203,8 +194,7 @@ void takeFirstToolchainIfMultipleExecutions() throws IOException {
assertThat(props).contains(entry("sonar.java.jdkHome", "fake_jdk_9"));
}

private Properties getProps(File outputProps)
throws FileNotFoundException, IOException {
private Properties getProps(File outputProps) throws IOException {
Properties props = new Properties();
try (FileInputStream fis = new FileInputStream(outputProps)) {
props.load(fis);
Expand Down
7 changes: 1 addition & 6 deletions its/src/test/java/com/sonar/maven/it/suite/LinksTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@
import com.sonar.maven.it.ItUtils;
import com.sonar.orchestrator.build.MavenBuild;
import com.sonar.orchestrator.container.Server;

import org.junit.jupiter.api.Test;
import org.sonarqube.ws.ProjectLinks;
import org.sonarqube.ws.ProjectLinks.SearchWsResponse;
import org.sonarqube.ws.client.HttpConnector;
import org.sonarqube.ws.client.WsClient;
import org.sonarqube.ws.client.WsClientFactories;
import org.sonarqube.ws.client.projectlinks.SearchRequest;

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

private void checkLinks() {
Server server = ORCHESTRATOR.getServer();
WsClient client = newAuthenticatedWsClient();
SearchWsResponse response = client.projectLinks().search(new SearchRequest().setProjectKey("com.sonarsource.it.samples:simple-sample"));
SearchWsResponse response = wsClient.projectLinks().search(new SearchRequest().setProjectKey("com.sonarsource.it.samples:simple-sample"));
if (server.version().isGreaterThanOrEquals(7, 1)) {
// SONAR-10299
assertThat(response.getLinksList())
Expand Down
29 changes: 8 additions & 21 deletions its/src/test/java/com/sonar/maven/it/suite/MavenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,50 +24,40 @@
import com.sonar.orchestrator.build.BuildRunner;
import com.sonar.orchestrator.build.MavenBuild;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.sonarqube.ws.Components;
import org.sonarqube.ws.client.components.ComponentsService;
import org.sonarqube.ws.client.components.ShowRequest;
import org.sonarqube.ws.client.settings.SetRequest;
import org.sonarqube.ws.client.users.CreateRequest;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;

class MavenTest extends AbstractMavenTest {

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

@TempDir
public Path temp;

@AfterEach
public void cleanup() {
wsClient.settings().set(new SetRequest().setKey("sonar.forceAuthentication").setValue("false"));
}

/**
* See MSONAR-129
*/
@Test
void useUserPropertiesGlobalConfig() throws Exception {
void useUserPropertiesGlobalConfig(@TempDir Path temp) throws Exception {
BuildRunner runner = new BuildRunner(ORCHESTRATOR.getConfiguration());
MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/maven-only-test-dir"))
.setGoals(cleanSonarGoal());

File settingsXml = temp.resolve("settings.xml").toFile();
settingsXml.createNewFile();
Path settingsXml = temp.resolve("settings.xml").toAbsolutePath();
Map<String, String> props = ORCHESTRATOR.getDatabase().getSonarProperties();
props.put("sonar.host.url", ORCHESTRATOR.getServer().getUrl());
props.put("sonar.login", ORCHESTRATOR.getDefaultAdminToken());
FileUtils.write(settingsXml, ItUtils.createSettingsXml(props));
Files.write(settingsXml, ItUtils.createSettingsXml(props).getBytes(UTF_8));

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

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

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

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

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

MavenBuild build = MavenBuild.create(ItUtils.locateProjectPom("maven/maven-only-test-dir"))
Expand Down
17 changes: 7 additions & 10 deletions its/src/test/java/com/sonar/maven/it/suite/ProxyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
*/
package com.sonar.maven.it.suite;

import static org.assertj.core.api.Assertions.assertThat;

import com.sonar.maven.it.ItUtils;
import com.sonar.maven.it.Proxy;
import com.sonar.orchestrator.build.BuildResult;
import com.sonar.orchestrator.build.MavenBuild;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
Expand All @@ -29,20 +31,15 @@
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

import com.sonar.maven.it.ItUtils;
import com.sonar.maven.it.Proxy;
import com.sonar.orchestrator.build.BuildResult;
import com.sonar.orchestrator.build.MavenBuild;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.assertj.core.api.Assertions.assertThat;

class ProxyTest extends AbstractMavenTest {
private Proxy proxy;
@TempDir
public Path temp;

@BeforeEach
public void prepare() throws Exception {
Expand All @@ -58,7 +55,7 @@ public void after() throws Exception {
}

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

0 comments on commit 52c4e6d

Please sign in to comment.