Skip to content

Commit

Permalink
Sonar integration branch (#270)
Browse files Browse the repository at this point in the history
* Changes to show enginePackVersion in logs

* plugin version

* Removing unnecessary logs

---------

Co-authored-by: Swati Awate <[email protected]>
  • Loading branch information
PravinGadankush and swatiawate1 authored Sep 30, 2024
1 parent 5efe590 commit b97b557
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/cx/restclient/CxSASTClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private CxARMStatus resolveCxARMStatus(CxARMStatus cxARMStatus) throws CxClientE
};


CxSASTClient(CxScanConfig config, Logger log) throws MalformedURLException {
public CxSASTClient(CxScanConfig config, Logger log) throws MalformedURLException {
super(config, log);


Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/cx/restclient/ast/AstClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ protected CxHttpClient createHttpClient(String baseUrl) {
config.isScaProxy(),
config.getScaProxyConfig(),
log,
config.getNTLM());
config.getNTLM(),
config.getPluginVersion());
//initializing Team Path to prevent null pointer in login when called from automation
client.setTeamPathHeader("");

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/cx/restclient/ast/ClientTypeResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ private CxHttpClient getHttpClient(String acBaseUrl) {
config.isScaProxy(),
config.getScaProxyConfig(),
log,
config.getNTLM());
config.getNTLM(),
config.getPluginVersion());
}
return httpClient;
}
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/cx/restclient/configuration/CxScanConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,17 @@ public void setShowCriticalLabel(boolean showCriticalLabel) {
private String engineConfigurationName;
private String projectCustomFields;
private boolean ignoreBenignErrors = false;
private String pluginVersion;

private String osaFolderExclusions;
public String getPluginVersion() {
return pluginVersion;
}

public void setPluginVersion(String pluginVersion) {
this.pluginVersion = pluginVersion;
}

private String osaFolderExclusions;
public String getEngineConfigurationName() {
return engineConfigurationName;
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/cx/restclient/dto/CxVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class CxVersion {
private String version;
private String hotFix;
private String enginePackVersion;

public String getVersion() {
return version;
Expand All @@ -25,4 +26,12 @@ public String getHotFix() {
public void setHotFix(String hotFix) {
this.hotFix = hotFix;
}

public String getEnginePackVersion() {
return enginePackVersion;
}

public void setEnginePackVersion(String enginePackVersion) {
this.enginePackVersion = enginePackVersion;
}
}
25 changes: 22 additions & 3 deletions src/main/java/com/cx/restclient/httpClient/CxHttpClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.cx.restclient.httpClient;

import com.cx.restclient.common.ErrorMessage;
import com.cx.restclient.configuration.CxScanConfig;
import com.cx.restclient.dto.CxVersion;
import com.cx.restclient.dto.LoginSettings;
import com.cx.restclient.dto.ProxyConfig;
import com.cx.restclient.dto.TokenLoginResponse;
Expand Down Expand Up @@ -121,17 +123,20 @@ public class CxHttpClient implements Closeable {
private CookieStore cookieStore = new BasicCookieStore();
private HttpClientBuilder cb = HttpClients.custom();
private final Map<String, String> customHeaders = new HashMap<>();
private CxVersion cxVersion;
private String pluginVersion;


public CxHttpClient(String rootUri, String origin, boolean disableSSLValidation, boolean isSSO, String refreshToken,
boolean isProxy, @Nullable ProxyConfig proxyConfig, Logger log, Boolean useNTLM) throws CxClientException {
boolean isProxy, @Nullable ProxyConfig proxyConfig, Logger log, Boolean useNTLM, String pluginVersion) throws CxClientException {

this.log = log;
this.rootUri = rootUri;
this.refreshToken = refreshToken;
this.cxOrigin = origin;
this.useSSo = isSSO;
this.useNTLM = useNTLM;
this.pluginVersion = pluginVersion;
//create httpclient
cb.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build());
setSSLTls("TLSv1.2", log);
Expand Down Expand Up @@ -182,8 +187,8 @@ public CxHttpClient(String rootUri, String origin, boolean disableSSLValidation,
}

public CxHttpClient(String rootUri, String origin, String originUrl, boolean disableSSLValidation, boolean isSSO, String refreshToken,
boolean isProxy, @Nullable ProxyConfig proxyConfig, Logger log, Boolean useNTLM) throws CxClientException {
this(rootUri, origin, disableSSLValidation, isSSO, refreshToken, isProxy, proxyConfig, log, useNTLM);
boolean isProxy, @Nullable ProxyConfig proxyConfig, Logger log, Boolean useNTLM, String pluginVersion) throws CxClientException {
this(rootUri, origin, disableSSLValidation, isSSO, refreshToken, isProxy, proxyConfig, log, useNTLM, pluginVersion);
this.cxOriginUrl = originUrl;
}

Expand Down Expand Up @@ -635,6 +640,17 @@ public void addCustomHeader(String name, String value) {
log.debug(String.format("Adding a custom header: %s: %s", name, value));
customHeaders.put(name, value);
}

private String getUserAgentValue() {
if (cxOrigin == null) {
log.warn("cxOrigin is null");
cxOrigin = "unknown"; // Or handle as appropriate
}

String version = (pluginVersion != null ) ? pluginVersion : "unknown"; // Ensure cxVersion is not null

return "plugin_name=" + cxOrigin + ";plugin_version=" + version;
}

private <T> T request(HttpRequestBase httpMethod, String contentType, HttpEntity entity, Class<T> responseType, int expectStatus, String failedMsg, boolean isCollection, boolean retry) throws IOException {
//Support unicode characters
Expand All @@ -661,6 +677,9 @@ private <T> T request(HttpRequestBase httpMethod, String contentType, HttpEntity
if (contentType != null) {
httpMethod.addHeader("Content-type", contentType);
}
if (getUserAgentValue() != null) {
httpMethod.addHeader("User-Agent", getUserAgentValue());
}
if (entity != null && httpMethod instanceof HttpEntityEnclosingRequestBase) { //Entity for Post methods
((HttpEntityEnclosingRequestBase) httpMethod).setEntity(entity);
}
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/cx/restclient/osa/dto/ScanConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ public class ScanConfiguration {

private boolean SASTEnabled;
private String cxOrigin;
private String sourceDir;
private String pluginVersion;
public String getPluginVersion() {
return pluginVersion;
}

public void setPluginVersion(String pluginVersion) {
this.pluginVersion = pluginVersion;
}

private String sourceDir;
private String tempDir;
private String reportsDir;
private String username;
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/com/cx/restclient/sast/utils/LegacyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;

import static com.cx.restclient.common.CxPARAM.*;
import static com.cx.restclient.httpClient.utils.ContentType.CONTENT_TYPE_API_VERSION_1_1;
import static com.cx.restclient.httpClient.utils.ContentType.CONTENT_TYPE_APPLICATION_JSON_V1;
import static com.cx.restclient.httpClient.utils.ContentType.CONTENT_TYPE_APPLICATION_JSON_V4;
import static com.cx.restclient.httpClient.utils.HttpClientHelper.convertToJson;
Expand Down Expand Up @@ -252,9 +253,9 @@ private List<Project> getProjectByName(String projectName, String teamId, String
}

private void initHttpClient(CxScanConfig config, Logger log) throws MalformedURLException {

if (!org.apache.commons.lang3.StringUtils.isEmpty(config.getUrl())) {
httpClient = new CxHttpClient(
if (!org.apache.commons.lang3.StringUtils.isEmpty(config.getUrl())) {
httpClient = new CxHttpClient(
UrlUtils.parseURLToString(config.getUrl(), "CxRestAPI/"),
config.getCxOrigin(),
config.getCxOriginUrl(),
Expand All @@ -264,7 +265,8 @@ private void initHttpClient(CxScanConfig config, Logger log) throws MalformedURL
config.isProxy(),
config.getProxyConfig(),
log,
config.getNTLM());
config.getNTLM(),
config.getPluginVersion());
}
}

Expand Down Expand Up @@ -293,7 +295,7 @@ public void initiate() throws CxClientException {
public String getCxVersion() throws IOException, CxClientException {
String version;
try {
config.setCxVersion(httpClient.getRequest(CX_VERSION, CONTENT_TYPE_APPLICATION_JSON_V1, CxVersion.class, 200, "cx Version", false));
config.setCxVersion(httpClient.getRequest(CX_VERSION, CONTENT_TYPE_API_VERSION_1_1, CxVersion.class, 200, "cx Version", false));
String hotfix = "";
try {
if (config.getCxVersion().getHotFix() != null && Integer.parseInt(config.getCxVersion().getHotFix()) > 0) {
Expand All @@ -304,6 +306,7 @@ public String getCxVersion() throws IOException, CxClientException {

version = config.getCxVersion().getVersion();
log.info("Checkmarx server version [" + config.getCxVersion().getVersion() + "]." + hotfix);
log.info("Checkmarx Engine Pack Version [" + config.getCxVersion().getEnginePackVersion() + "].");

} catch (Exception ex) {
version = "lower than 9.0";
Expand Down

0 comments on commit b97b557

Please sign in to comment.