Skip to content

Commit

Permalink
Added xsc api and receiving agent and not taking only default.
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalk007 committed Sep 11, 2024
1 parent 65d667d commit a097ec6
Show file tree
Hide file tree
Showing 15 changed files with 332 additions and 14 deletions.
24 changes: 23 additions & 1 deletion src/main/java/com/jfrog/xray/client/impl/XrayClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.http.util.EntityUtils;
import org.jfrog.build.api.util.Log;
import org.jfrog.build.client.PreemptiveHttpClient;
import com.jfrog.xray.client.impl.xsc.XscClient;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -45,16 +46,24 @@
* @author Roman Gurevitch
*/
public class XrayClient extends PreemptiveHttpClient implements Xray {
private XscClient xscClient;
private static final ObjectMapper mapper = ObjectMapperHelper.get();
private static final String API_BASE = "/api/v1/";

private final String baseApiUrl;
private final Log log;

public XrayClient(PoolingHttpClientConnectionManager connectionManager, BasicCredentialsProvider credentialsProvider, String accessToken, AuthCache authCache, HttpClientBuilder clientBuilder, int connectionRetries, Log log, String url) {
super(connectionManager, credentialsProvider, accessToken, authCache, clientBuilder, connectionRetries, log);

this.baseApiUrl = URIUtil.concatUrl(url, API_BASE);
this.log = log;


this.xscClient = new XscClient(connectionManager, credentialsProvider, accessToken, authCache, clientBuilder, connectionRetries, log, url);
}

public XscClient getXscClient() {
return this.xscClient;
}

private static boolean statusNotOk(int statusCode) {
Expand Down Expand Up @@ -114,6 +123,19 @@ public CloseableHttpResponse post(String uri, Object payload, ObjectMapper mappe
return setHeadersAndExecute(postRequest);
}

public CloseableHttpResponse put(String uri, Object payload) throws IOException {
return put(uri, payload, mapper);
}

public CloseableHttpResponse put(String uri, Object payload, ObjectMapper mapper) throws IOException {
HttpPut putRequest = new HttpPut(createUrl(uri));
byte[] body = mapper.writeValueAsBytes(payload);
log.debug("PUT" + putRequest.getURI() + "\n" + new String(body, StandardCharsets.UTF_8));
HttpEntity requestEntity = new ByteArrayEntity(body, ContentType.APPLICATION_JSON);
putRequest.setEntity(requestEntity);
return setHeadersAndExecute(putRequest);
}

private String createUrl(String queryPath) {
return URIUtil.concatUrl(baseApiUrl, queryPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ public class XrayClientBuilder extends PreemptiveHttpClientBuilder {

private String url = StringUtils.EMPTY;

public XrayClientBuilder() {
public XrayClientBuilder(String userAgent ) {
String userAgentHeader = DEFAULT_USER_AGENT;
if(userAgent != null && !userAgent.isEmpty()) {
userAgentHeader = userAgent;
}

setTimeout(CONNECTION_TIMEOUT_MILLISECONDS);
setUserAgent(DEFAULT_USER_AGENT);
setUserAgent(userAgentHeader);
setLog(new NullLog());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,29 @@ public static ObjectMapper createFilteredObjectMapper() {
}

@Override
public GraphResponse graph(DependencyTree dependencies, XrayScanProgress progress, Runnable checkCanceled, String projectKey, String[] watches) throws IOException, InterruptedException {
public GraphResponse graph(DependencyTree dependencies, XrayScanProgress progress, Runnable checkCanceled, String projectKey, String[] watches, String msi) throws IOException, InterruptedException {
StringBuilder params = new StringBuilder();

if (msi != null) {
params.append("multi_scan_id=").append(msi);
}
if (StringUtils.isNotBlank(projectKey)) {
return this.post("?project=" + projectKey, dependencies, progress, checkCanceled);
if (params.length() > 0) {
params.append("&");
}
params.append("project=").append(projectKey);
}

if (ArrayUtils.isNotEmpty(watches)) {
String watchesStr = "?watch=" + String.join("&watch=", watches);
return this.post(watchesStr, dependencies, progress, checkCanceled);
if (params.length() > 0) {
params.append("&");
}
params.append("watch=").append(String.join("&watch=", watches));
}
return graph(dependencies, progress, checkCanceled);
String paramsString = params.toString();

return !paramsString.isEmpty() ? this.post(paramsString, dependencies, progress, checkCanceled) : graph(dependencies, progress, checkCanceled);

}

@Override
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/jfrog/xray/client/impl/xsc/XcsSystemClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.jfrog.xray.client.impl.xsc;

import java.io.IOException;

public class XcsSystemClient {
private final String SYSTEM_ENDPOINT = "/system/version";
private final XscHttpClient httpClient;
public XcsSystemClient(XscHttpClient httpClient) {
this.httpClient = httpClient;
}

public String getXscVersion() {
try {
String response = this.httpClient.get(SYSTEM_ENDPOINT).toString();
return this.httpClient.extractValueFromResponse(response, "xsc_version");
}
catch(IOException e) {
System.out.println(e.getMessage());
return "";
}
}





}
33 changes: 33 additions & 0 deletions src/main/java/com/jfrog/xray/client/impl/xsc/XscClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.jfrog.xray.client.impl.xsc;
import org.apache.http.client.AuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.jfrog.build.api.util.Log;

public class XscClient {

private XscHttpClient httpClient;
private XcsSystemClient xscSystemClient;
private XscEventClient xscEventClient;

public XscClient(PoolingHttpClientConnectionManager connectionManager, BasicCredentialsProvider credentialsProvider, String accessToken, AuthCache authCache, HttpClientBuilder clientBuilder, int connectionRetries, Log log,String url) {
this.httpClient = new XscHttpClient(connectionManager, credentialsProvider, accessToken, authCache, clientBuilder, connectionRetries, log, url);
this.xscSystemClient = new XcsSystemClient(this.httpClient);
this.xscEventClient = new XscEventClient(this.httpClient);
}


public XscEventClient scan(){
return this.xscEventClient;
}

public XcsSystemClient system(){
return this.xscSystemClient;
}





}
33 changes: 33 additions & 0 deletions src/main/java/com/jfrog/xray/client/impl/xsc/XscEventClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.jfrog.xray.client.impl.xsc;

import com.jfrog.xray.client.impl.xsc.types.EndScanRequest;
import com.jfrog.xray.client.impl.xsc.types.ScanRequest;

public class XscEventClient {
private final String EVENT_ENDPOINT = "/event";
private final XscHttpClient httpClient;

public XscEventClient(XscHttpClient httpClient) {
this.httpClient = httpClient;
}

public String startScan(ScanRequest scanRequest){
try {
String response = httpClient.post(EVENT_ENDPOINT, scanRequest).toString();
return httpClient.extractValueFromResponse(response, "multi_scan_id");
}catch (Exception e){
return "";
}
}

public void endScan(EndScanRequest scanRequest){
try {
httpClient.put(EVENT_ENDPOINT, scanRequest);
} catch (Exception e) {
throw new RuntimeException(e);
}
}



}
31 changes: 31 additions & 0 deletions src/main/java/com/jfrog/xray/client/impl/xsc/XscHttpClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.jfrog.xray.client.impl.xsc;
import com.jfrog.xray.client.impl.XrayClient;
import org.apache.http.client.AuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.jfrog.build.api.util.Log;


public class XscHttpClient extends XrayClient {
private static final String XSC_BASE_URL = "/xsc/api/v1";


public XscHttpClient(PoolingHttpClientConnectionManager connectionManager, BasicCredentialsProvider credentialsProvider, String accessToken, AuthCache authCache, HttpClientBuilder clientBuilder, int connectionRetries, Log log,String url) {
super(connectionManager, credentialsProvider, accessToken, authCache, clientBuilder, connectionRetries, log, url+ XSC_BASE_URL );
}

public String extractValueFromResponse(String json, String key) {
String searchKey = "\"" + key + "\":\"";
int startIndex = json.indexOf(searchKey);
if (startIndex == -1) {
return null; // Key not found
}
startIndex += searchKey.length();
int endIndex = json.indexOf("\"", startIndex);
if (endIndex == -1) {
return null;
}
return json.substring(startIndex, endIndex);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.jfrog.xray.client.impl.xsc.types;

public class EndScanRequest extends ScanRequest {
private Integer totalFindings;
private Integer totalIgnoredFindings;
private Integer totalScanDuration; // Changed to String
private String multiScanId;

public EndScanRequest(
ScanEventType eventType,
ScanEventStatus eventStatus,
String product,
String productVersion,
String jpdVersion,
String jfrogUser,
String osPlatform,
String osArchitecture,
String machineId,
String analyzerManagerVersion,
Boolean isDefaultConfig,
Integer totalFindings,
Integer totalIgnoredFindings,
Integer totalScanDuration,
String multiScanId) {
super(eventType, eventStatus, product, productVersion, jpdVersion, jfrogUser, osPlatform, osArchitecture, machineId, analyzerManagerVersion, isDefaultConfig);
this.totalScanDuration = totalScanDuration;
this.totalFindings = totalFindings;
this.totalIgnoredFindings = totalIgnoredFindings;
this.multiScanId = multiScanId;
}

@Override
public String toString() {
return "{" +
"\"event_type\":" + this.eventType.getType() + "," +
"\"event_status\":\"" + this.eventStatus.getStatus() + "\"," +
"\"product\":\"" + this.product + "\"," +
"\"product_version\":\"" + this.productVersion + "\"," +
"\"jpd_version\":\"" + this.jpdVersion + "\"," +
"\"jfrog_user\":\"" + this.jfrogUser + "\"," +
"\"os_platform\":\"" + this.osPlatform + "\"," +
"\"os_architecture\":\"" + this.osArchitecture + "\"," +
"\"is_default_config\":" + this.isDefaultConfig + "," +
"\"machine_id\":\"" + this.machineId + "\"," +
"\"analyzer_manager_version\":\"" + this.analyzerManagerVersion + "\"," +
"\"total_findings\":" + totalFindings + "," +
"\"total_ignored_findings\":" + totalIgnoredFindings + "," +
"\"total_scan_duration\":\"" + totalScanDuration + "\"," +
"\"multi_scan_id\":\"" + multiScanId + "\"" +
"}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.jfrog.xray.client.impl.xsc.types;


public enum ScanEventStatus {
STARTED("started"),
COMPLETED("completed"),
CANCELLED("cancelled"),
FAILED("failed");

private final String status;

ScanEventStatus(String status) {
this.status = status;
}

public String getStatus() {
return status;
}

@Override
public String toString() {
return status;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.jfrog.xray.client.impl.xsc.types;


public enum ScanEventType {
SOURCE_CODE(1);

private final int type;

ScanEventType(int type) {
this.type = type;
}

public int getType() {
return type;
}

@Override
public String toString() {
return Integer.toString(type);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.jfrog.xray.client.impl.xsc.types;



public class ScanRequest {

protected ScanEventType eventType;
protected ScanEventStatus eventStatus;
protected String product;
protected String productVersion;
protected String jpdVersion;
protected String jfrogUser;
protected String osPlatform;
protected String osArchitecture;
protected String machineId;
protected String analyzerManagerVersion;
protected Boolean isDefaultConfig;


public ScanRequest(ScanEventType eventType, ScanEventStatus eventStatus, String product,
String productVersion, String jpdVersion, String jfrogUser,
String osPlatform, String osArchitecture, String machineId,
String analyzerManagerVersion, Boolean isDefaultConfig) {
this.eventType = eventType;
this.eventStatus = eventStatus;
this.product = product;
this.productVersion = productVersion;
this.jpdVersion = jpdVersion;
this.jfrogUser = jfrogUser;
this.osPlatform = osPlatform;
this.osArchitecture = osArchitecture;
this.machineId = machineId;
this.analyzerManagerVersion = analyzerManagerVersion;
this.isDefaultConfig = isDefaultConfig;
}


@Override
public String toString() {
return "{" +
"\"event_type\":" + this.eventType.getType() + "," +
"\"event_status\":\"" + this.eventStatus.getStatus() + "\"," +
"\"product\":\"" + this.product + "\"," +
"\"product_version\":\"" + this.productVersion + "\"," +
"\"jpd_version\":\"" + this.jpdVersion + "\"," +
"\"jfrog_user\":\"" + this.jfrogUser + "\"," +
"\"os_platform\":\"" + this.osPlatform + "\"," +
"\"os_architecture\":\"" + this.osArchitecture + "\"," +
"\"is_default_config\":" + this.isDefaultConfig + "," +
"\"machine_id\":\"" + this.machineId + "\"," +
"\"analyzer_manager_version\":\"" + this.analyzerManagerVersion + "\"" +
"}";
}
}
Loading

0 comments on commit a097ec6

Please sign in to comment.