Skip to content

Commit

Permalink
bugid: 1872 - Fix for deleting properties on 7.x SonarQube
Browse files Browse the repository at this point in the history
CR_by: n/a
  • Loading branch information
ilandn committed Mar 23, 2021
1 parent 4208b7c commit e696418
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>checkmarx.com</groupId>
<artifactId>com.checkmarx.sonar.cxplugin</artifactId>
<packaging>sonar-plugin</packaging>
<version>2021.0.0</version>
<version>2021.0.3</version>
<name>Checkmarx plugin</name>
<description>Checkmarx plugin</description>

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/checkmarx/sonar/sensor/utils/CxConfigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class CxConfigHelper {
public static final String SETTINGS_API_PATH = "api/settings";
public static final String SETTINGS_API_SET_PATH = SETTINGS_API_PATH + "/set";
public static final String SETTINGS_API_GET_PATH = SETTINGS_API_PATH + "/values";
public static final String SETTINGS_API_RESET_PATH = SETTINGS_API_PATH + "/reset";

private Logger log;
private final ObjectMapper objectMapper = new ObjectMapper();
Expand Down Expand Up @@ -103,11 +104,13 @@ public CxFullCredentials getCredentialsWithoutPassword(RestEndpointContext conte
private CxFullCredentials getStoredCredentials(PropertyApiClient client) throws IOException {
CxFullCredentials result;
String credentialsJson = client.getProperty(CxProperties.CREDENTIALS_KEY);
log.info("Stored credentials json returned.");
if (StringUtils.isNotEmpty(credentialsJson)) {
result = objectMapper.readValue(credentialsJson, CxFullCredentials.class);
} else {
result = new CxFullCredentials();
}
log.info("Stored credentials username: " + result.getCxUsername() + ", encrypted password: " + result.getCxPassword());
return result;
}

Expand Down Expand Up @@ -234,6 +237,7 @@ private String getSonarPropertyHttp(String propertyName, Configuration config) {
}
return "";
} catch (IOException e) {
log.error("");
return null;
} finally {
if (response != null) {
Expand Down Expand Up @@ -322,6 +326,7 @@ public void updateCredentials(RestEndpointContext context, CxFullCredentials cre
CxFullCredentials storedCredentials = getStoredCredentials(client);
storedCredentials.setCxServerUrl(credentials.getCxServerUrl());
storedCredentials.setCxUsername(credentials.getCxUsername());
log.info("Updating username: " + credentials.getCxUsername());

if (StringUtils.isNotEmpty(credentials.getCxPassword())) {
String encryptedPassword = encrypt(credentials.getCxPassword());
Expand All @@ -333,6 +338,19 @@ public void updateCredentials(RestEndpointContext context, CxFullCredentials cre
client.setProperty(CxProperties.CREDENTIALS_KEY, credentialsJson);
}

public static String resetPropertyUrl(String sonarBaseUrl, String propertyName, String componentKey) {
String result = null;
try {
result = String.format("%s/%s?keys=%s&component=%s",
sonarBaseUrl,
SETTINGS_API_RESET_PATH,
URLEncoder.encode(propertyName, ENCODING),
URLEncoder.encode(componentKey, ENCODING));
} catch (UnsupportedEncodingException ignored) {
}
return result;
}

public static String getPropertyUrl(String sonarBaseUrl, String propertyName, String componentKey) {
String result = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public void deleteProperty(String name) throws IOException {
HttpUriRequest request = new HttpDelete(requestUrl);

HttpResponse response = getResponse(request);
if (response.getStatusLine().getStatusCode() != Response.Status.NO_CONTENT.getStatusCode()) {
requestUrl = CxConfigHelper.resetPropertyUrl(getSonarBaseUrl(), name, getComponentKey());
response = getResponse(new HttpPost(requestUrl));
}
throwOnFailure(response, name);
}

Expand Down Expand Up @@ -157,6 +161,7 @@ private void addAuthHeaders(HttpUriRequest request, CookieStore cookieStore) {

private void throwOnFailure(HttpResponse response, String propertyName) throws IOException {
int statusCode = response.getStatusLine().getStatusCode();
logger.info("Response code for '" + propertyName + "': " + statusCode);
if (statusCode != Response.Status.NO_CONTENT.getStatusCode()) {
throw new IOException(String.format("Error setting property: %s, HTTP status code: %d", propertyName, statusCode));
}
Expand Down

0 comments on commit e696418

Please sign in to comment.