Skip to content

Commit

Permalink
removed the duplicated code from the Login, missing the TryToConnect …
Browse files Browse the repository at this point in the history
…part.
  • Loading branch information
Pedro Portilha committed Feb 27, 2024
1 parent e7167f7 commit c35bb0d
Showing 1 changed file with 19 additions and 34 deletions.
53 changes: 19 additions & 34 deletions Checkmarx.API/CxClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ private HttpClient Login(string baseURL = "http://localhost/cxrestapi/",
catch (System.ServiceModel.Security.SecurityNegotiationException ex)

Check warning on line 596 in Checkmarx.API/CxClient.cs

View workflow job for this annotation

GitHub Actions / build, pack & publish

The variable 'ex' is declared but never used
{
ignoreCertificate = true;
Console.WriteLine($"The endpoint {baseURL} is throwing an SecurityNegotiationException. That usualy means there is a certificate issue. Please check this issue and reach out to Cloud Operations if necessary.");
Console.WriteLine($"The endpoint {baseURL} is throwing an SecurityNegotiationException.");
}

var webServer = new Uri(baseURL);
Expand Down Expand Up @@ -635,25 +635,20 @@ private HttpClient Login(string baseURL = "http://localhost/cxrestapi/",

Console.WriteLine("Checkmarx " + _version.ToString());

HttpClient httpClient = new HttpClient()
{
BaseAddress = webServer,
Timeout = TimeSpan.FromMinutes(20),
}; ;
HttpClientHandler httpClientHandler = new();

// Ignore certificate for http client
if (ignoreCertificate)
{
// Ignore certificate for http client
HttpClientHandler httpClientHandler = new HttpClientHandler();
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };

httpClient = new HttpClient(httpClientHandler)
{
BaseAddress = webServer,
Timeout = TimeSpan.FromMinutes(20),
};
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
}

httpClient = new HttpClient(httpClientHandler)
{
BaseAddress = webServer,
Timeout = TimeSpan.FromMinutes(20)
};

if (httpClient.BaseAddress.LocalPath != "cxrestapi")
{
httpClient.BaseAddress = new Uri(webServer, "/cxrestapi/");
Expand Down Expand Up @@ -784,7 +779,7 @@ public HttpResponseMessage TestConnection(string baseURL = "http://localhost/cxr
catch (System.ServiceModel.Security.SecurityNegotiationException ex)

Check warning on line 779 in Checkmarx.API/CxClient.cs

View workflow job for this annotation

GitHub Actions / build, pack & publish

The variable 'ex' is declared but never used
{
ignoreCertificate = true;
Console.WriteLine($"The endpoint {baseURL} is throwing an SecurityNegotiationException. That usualy means there is a certificate issue. Please check this issue and reach out to Cloud Operations if necessary.");
Console.WriteLine($"The endpoint {baseURL} is throwing an SecurityNegotiationException. That usually means there is a certificate issue.");
}

var webServer = new Uri(baseURL);
Expand Down Expand Up @@ -1663,8 +1658,6 @@ public void SetPreset(long projectId, long presetId)
public long? RunSASTScan(long projectId, string comment = "", bool forceScan = true, byte[] sourceCodeZipContent = null,
bool useLastScanPreset = false, int? presetId = null, int? configurationId = null, bool runPublicScan = true, bool forceLocal = false, CxClient cxClient2 = null)
{
long? scanId = null;

checkConnection();

var projectConfig = GetProjectConfigurations(projectId);
Expand Down Expand Up @@ -1793,11 +1786,11 @@ public void SetPreset(long projectId, long presetId)
}

if (cxClient2 == null)
scanId = RunScan(projectId, forceScan, runPublicScan, comment);
return triggerNewScan(projectId, forceScan, runPublicScan, comment);
else
scanId = cxClient2.RunScan(projectId, forceScan, runPublicScan, comment);
return cxClient2.triggerNewScan(projectId, forceScan, runPublicScan, comment);


return scanId;
}

private void UploadSourceCode(long projectId, byte[] sourceCodeZipContent)
Expand All @@ -1824,9 +1817,8 @@ private void UploadSourceCode(long projectId, byte[] sourceCodeZipContent)
}
}

public long? RunScan(long projectId, bool forceScan, bool runPublicScan, string comment)
private long? triggerNewScan(long projectId, bool forceScan, bool runPublicScan, string comment)
{
long? scanId = null;
using (var request = new HttpRequestMessage(HttpMethod.Post, "sast/scans"))
{
request.Headers.Add("Accept", "application/json;v=1.0");
Expand All @@ -1848,21 +1840,14 @@ private void UploadSourceCode(long projectId, byte[] sourceCodeZipContent)

if (response.StatusCode != HttpStatusCode.Created)
{
throw new NotSupportedException(response.Content.ReadAsStringAsync().Result);
throw new ApplicationException(response.Content.ReadAsStringAsync().Result);
}

try
{
var fetchScanId = response.Headers.Location.ToString().Split("/").Last();
scanId = Convert.ToInt64(fetchScanId);
}
catch (Exception ex)
{
Console.WriteLine($"Scan triggered successfuly, but it was not possible to fetch the scan id.");
}
// Doesn't this come in the response??
var fetchScanId = response.Headers.Location.ToString().Split("/").Last();
return Convert.ToInt64(fetchScanId);
}
}
return scanId;
}

private void checkSoapResponse(cxPortalWebService93.CxWSBasicRepsonse result)
Expand Down

0 comments on commit c35bb0d

Please sign in to comment.