Skip to content

Commit

Permalink
code refactoring #2504
Browse files Browse the repository at this point in the history
- renamed classes and methods
- add comments
  • Loading branch information
lorriborri committed Oct 11, 2023
1 parent 519a559 commit c984c56
Show file tree
Hide file tree
Showing 42 changed files with 421 additions and 420 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mercedesbenz.sechub.xraywrapper.http;
package com.mercedesbenz.sechub.xraywrapper.api;

import java.net.HttpURLConnection;

Expand All @@ -10,24 +10,24 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mercedesbenz.sechub.xraywrapper.cli.XrayWrapperExitCode;
import com.mercedesbenz.sechub.xraywrapper.cli.XrayWrapperRuntimeException;
import com.mercedesbenz.sechub.xraywrapper.config.XrayArtifact;
import com.mercedesbenz.sechub.xraywrapper.config.XrayConfiguration;
import com.mercedesbenz.sechub.xraywrapper.config.XrayWrapperArtifact;
import com.mercedesbenz.sechub.xraywrapper.config.XrayWrapperConfiguration;

public class XrayArtifactoryClient {
public class XrayAPIArtifactoryClient {

private static final Logger LOG = LoggerFactory.getLogger(XrayArtifactoryClient.class);
private static final Logger LOG = LoggerFactory.getLogger(XrayAPIArtifactoryClient.class);

private XrayArtifact artifact;
private XrayWrapperArtifact artifact;

private XrayConfiguration xrayConfiguration;
private XrayWrapperConfiguration xrayWrapperConfiguration;

public XrayArtifactoryClient(XrayArtifact artifact, XrayConfiguration xrayConfiguration) {
public XrayAPIArtifactoryClient(XrayWrapperArtifact artifact, XrayWrapperConfiguration xrayWrapperConfiguration) {
this.artifact = artifact;
this.xrayConfiguration = xrayConfiguration;
this.xrayWrapperConfiguration = xrayWrapperConfiguration;
}

public String getXrayVersion() throws XrayWrapperRuntimeException {
XrayAPIRequest request = XrayHttpRequestBuilder.buildGetXrayVersion(xrayConfiguration.getArtifactory());
XrayAPIRequest request = XrayAPIRequestBuilder.buildGetXrayVersion(xrayWrapperConfiguration.getArtifactory());
XrayAPIResponse response = send(request);
if (isErrorResponse(response)) {
throw new XrayWrapperRuntimeException("Artifactory not reachable", XrayWrapperExitCode.ARTIFACTORY_NOT_REACHABLE);
Expand All @@ -38,7 +38,8 @@ public String getXrayVersion() throws XrayWrapperRuntimeException {
}

public boolean checkArtifactoryUpload() throws XrayWrapperRuntimeException {
XrayAPIRequest request = XrayHttpRequestBuilder.buildCheckArtifactUpload(xrayConfiguration.getArtifactory(), artifact, xrayConfiguration.getRegister());
XrayAPIRequest request = XrayAPIRequestBuilder.buildCheckArtifactUpload(xrayWrapperConfiguration.getArtifactory(), artifact,
xrayWrapperConfiguration.getRegister());
XrayAPIResponse response = send(request);
if (isErrorResponse(response)) {
LOG.error("Error: artifact was not uploaded to artifactory");
Expand All @@ -48,7 +49,8 @@ public boolean checkArtifactoryUpload() throws XrayWrapperRuntimeException {
}

public String getScanStatus() throws XrayWrapperRuntimeException {
XrayAPIRequest request = XrayHttpRequestBuilder.buildGetScanStatus(xrayConfiguration.getArtifactory(), artifact, xrayConfiguration.getRegister());
XrayAPIRequest request = XrayAPIRequestBuilder.buildGetScanStatus(xrayWrapperConfiguration.getArtifactory(), artifact,
xrayWrapperConfiguration.getRegister());
XrayAPIResponse response = send(request);
if (isErrorResponse(response)) {
LOG.error("Error: scan status could not be retrieved");
Expand All @@ -59,7 +61,7 @@ public String getScanStatus() throws XrayWrapperRuntimeException {
}

public boolean requestScanReports() throws XrayWrapperRuntimeException {
XrayAPIRequest request = XrayHttpRequestBuilder.buildGetScanReports(xrayConfiguration.getArtifactory(), artifact);
XrayAPIRequest request = XrayAPIRequestBuilder.buildGetScanReports(xrayWrapperConfiguration.getArtifactory(), artifact);
XrayAPIResponse response = send(request);
if (isErrorResponse(response)) {
LOG.error("Could not get report from artifactory");
Expand All @@ -69,7 +71,8 @@ public boolean requestScanReports() throws XrayWrapperRuntimeException {
}

public String startScanArtifact() throws XrayWrapperRuntimeException {
XrayAPIRequest request = XrayHttpRequestBuilder.buildScanArtifact(xrayConfiguration.getArtifactory(), artifact, xrayConfiguration.getRegister());
XrayAPIRequest request = XrayAPIRequestBuilder.buildScanArtifact(xrayWrapperConfiguration.getArtifactory(), artifact,
xrayWrapperConfiguration.getRegister());
XrayAPIResponse response = send(request);
if (isErrorResponse(response)) {
LOG.error("Could not start external Xray scan");
Expand All @@ -82,7 +85,8 @@ public String startScanArtifact() throws XrayWrapperRuntimeException {
public void deleteArtifact() throws XrayWrapperRuntimeException {
// Xray deletes empty folders with auto cleanup
// deletes artifact folder in artifactory
XrayAPIRequest request = XrayHttpRequestBuilder.buildDeleteArtifact(xrayConfiguration.getArtifactory(), artifact, xrayConfiguration.getRegister());
XrayAPIRequest request = XrayAPIRequestBuilder.buildDeleteArtifact(xrayWrapperConfiguration.getArtifactory(), artifact,
xrayWrapperConfiguration.getRegister());
XrayAPIResponse response = send(request);
if (isErrorResponse(response)) {
LOG.error("Could not delete artifact from artifactory");
Expand All @@ -92,7 +96,8 @@ public void deleteArtifact() throws XrayWrapperRuntimeException {

public void deleteUploads() throws XrayWrapperRuntimeException {
// deletes _uploads folder in artifactory
XrayAPIRequest request = XrayHttpRequestBuilder.buildDeleteUploads(xrayConfiguration.getArtifactory(), artifact, xrayConfiguration.getRegister());
XrayAPIRequest request = XrayAPIRequestBuilder.buildDeleteUploads(xrayWrapperConfiguration.getArtifactory(), artifact,
xrayWrapperConfiguration.getRegister());
XrayAPIResponse response = send(request);
if (isErrorResponse(response)) {
LOG.error("Could not delete _uploads from artifactory");
Expand All @@ -105,19 +110,19 @@ JsonNode getBodyAsNode(String body) throws XrayWrapperRuntimeException {
try {
return mapper.readTree(body);
} catch (JsonProcessingException e) {
throw new XrayWrapperRuntimeException("Error: can not read file as json tree", e, XrayWrapperExitCode.JSON_NOT_PROCESSABLE);
throw new XrayWrapperRuntimeException("Can not read file as json tree", e, XrayWrapperExitCode.JSON_NOT_PROCESSABLE);
}
}

XrayAPIResponse send(XrayAPIRequest request) throws XrayWrapperRuntimeException {
HttpURLConnection con = XrayHttpRequestExecutor.setUpGetConnection(request);
return XrayHttpResponseBuilder.getHttpResponseFromConnection(con, xrayConfiguration.getZip_directory());
HttpURLConnection con = XrayAPIRequestExecutor.setUpGetConnection(request);
return XrayAPIResponseBuilder.getHttpResponseFromConnection(con, xrayWrapperConfiguration.getZip_directory());
}

private boolean isErrorResponse(XrayAPIResponse response) {
int statusCode = response.getStatus_code();
if (statusCode > 299) {
LOG.error("Error: received Error Message from artifactory: {}", statusCode);
LOG.error("Received Error Message from artifactory: {}", statusCode);
LOG.error("Response Headers: {}", response.getHeaders().toString());
LOG.error("Response Body: {}", response.getBody());
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mercedesbenz.sechub.xraywrapper.http;
package com.mercedesbenz.sechub.xraywrapper.api;

import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -8,7 +8,7 @@ public enum RequestMethodEnum {
GET, POST, DELETE;
}

private String baseUrl;
private String stringUrl;

private URL url;

Expand All @@ -21,8 +21,8 @@ public enum RequestMethodEnum {
public XrayAPIRequest() {
}

public XrayAPIRequest(String baseUrl, RequestMethodEnum requestMethodEnum, boolean authentication, String data) {
this.baseUrl = baseUrl;
public XrayAPIRequest(String stringUrl, RequestMethodEnum requestMethodEnum, boolean authentication, String data) {
this.stringUrl = stringUrl;
this.requestMethodEnum = requestMethodEnum;
this.authentication = authentication;
this.data = data;
Expand All @@ -36,12 +36,12 @@ public RequestMethodEnum getRequestMethodEnum() {
return requestMethodEnum;
}

public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
public void setStringUrl(String stringUrl) {
this.stringUrl = stringUrl;
}

public String getBaseUrl() {
return baseUrl;
public String getStringUrl() {
return stringUrl;
}

public void setAuthentication(boolean authentication) {
Expand Down Expand Up @@ -72,6 +72,6 @@ public void setUrl(URL url) {
}

private URL stringToUrl() throws MalformedURLException {
return new URL(this.baseUrl);
return new URL(this.stringUrl);
}
}
Original file line number Diff line number Diff line change
@@ -1,91 +1,67 @@
package com.mercedesbenz.sechub.xraywrapper.http;
package com.mercedesbenz.sechub.xraywrapper.api;

import com.mercedesbenz.sechub.xraywrapper.config.XrayArtifact;
import com.mercedesbenz.sechub.xraywrapper.config.XrayWrapperArtifact;

// This class creates POST and GET request to access the JFrog Artifactory and Xray
public class XrayHttpRequestBuilder {
public class XrayAPIRequestBuilder {

/**
* Creates GET request to get the JFrog Xray version
*
* @param baseUrl factory URL
* @return XrayAPIRequest
*/
public static XrayAPIRequest buildGetXrayVersion(String baseUrl) {
XrayAPIRequest request = new XrayAPIRequest();
request.setBaseUrl(baseUrl + "/xray/api/v1/system/version");
request.setStringUrl(baseUrl + "/xray/api/v1/system/version");
request.setRequestMethodEnum(XrayAPIRequest.RequestMethodEnum.GET);
return request;
}

/**
* Creates POST request to see if an artifact was uploaded successful
*
* @param baseUrl factory URL
* @param artifact Artifact to scan
* @param repository artifactory repository
* @return XrayAPIRequest
*/
public static XrayAPIRequest buildCheckArtifactUpload(String baseUrl, XrayArtifact artifact, String repository) {
public static XrayAPIRequest buildCheckArtifactUpload(String baseUrl, XrayWrapperArtifact artifact, String repository) {
String url = baseUrl + "/artifactory/api/storage/" + repository + "/" + artifact.getName() + "/" + artifact.getTag() + "/manifest.json";
String data = "";
return new XrayAPIRequest(url, XrayAPIRequest.RequestMethodEnum.GET, true, data);
}

/**
* Creates POST request to scan an artifact with Xray
*
* @param baseUrl factory URL
* @param artifact Artifact to scan
* @param repository artifactory repository
* @return XrayAPIRequest
*/
public static XrayAPIRequest buildScanArtifact(String baseUrl, XrayArtifact artifact, String repository) {
public static XrayAPIRequest buildScanArtifact(String baseUrl, XrayWrapperArtifact artifact, String repository) {
String url = baseUrl + "/xray/api/v1/scanArtifact";
String data = "{\"componentID\": \"" + artifact.getArtifactType() + "://" + artifact.getName() + ":" + artifact.getTag() + "\"," + "\"path\": \""
+ repository + "/" + artifact.getName() + "/" + artifact.getTag() + "/manifest.json\"}";
return new XrayAPIRequest(url, XrayAPIRequest.RequestMethodEnum.POST, true, data);
}

/**
* Creates POST request to get the status of an artifact
*
* @param baseUrl factory URL
* @param artifact Artifact to scan
* @param repository artifactory repository
* @return XrayAPIRequest
*/
public static XrayAPIRequest buildGetScanStatus(String baseUrl, XrayArtifact artifact, String repository) {
public static XrayAPIRequest buildGetScanStatus(String baseUrl, XrayWrapperArtifact artifact, String repository) {
String url = baseUrl + "/xray/api/v1/scan/status/artifact";
String data = "{\"path\": \"" + repository + "/" + artifact.getName() + "/" + artifact.getTag() + "/manifest.json\", \"repository_pkg_type\":\""
String data = "{\"path\": \"" + repository + "/" + artifact.getName() + "/" + artifact.getTag() + "/manifest.json\", " + "\"repository_pkg_type\":\""
+ artifact.getArtifactType() + "\", \"sha256\": \"" + artifact.getSha256() + "\"}";
return new XrayAPIRequest(url, XrayAPIRequest.RequestMethodEnum.POST, true, data);
}

/**
* Creates POST request to download the reports
*
* @param baseUrl factory URL
* @param artifact Artifact to scan
* @return XrayAPIRequest
*/
public static XrayAPIRequest buildGetScanReports(String baseUrl, XrayArtifact artifact) {
public static XrayAPIRequest buildGetScanReports(String baseUrl, XrayWrapperArtifact artifact) {
String url = baseUrl + "/xray/api/v1/component/exportDetails";
String data = "{\"component_name\": \"" + artifact.getName() + ":" + artifact.getTag() + "\"," + "\"package_type\": \"" + artifact.getArtifactType()
+ "\"," + "\"sha_256\" : \"" + artifact.getSha256() + "\"," + "\"violations\": true," + "\"include_ignored_violations\": true,"
+ "\"license\": true," + "\"exclude_unknown\": true," + "\"security\": true," + "\"malicious_code\": true," + "\"iac\": true,"
+ "\"services\": true," + "\"applications\": true," + "\"output_format\": \"json\"," + "\"spdx\": true," + "\"spdx_format\": \"json\","
+ "\"cyclonedx\": true," + "\"cyclonedx_format\": \"json\"}";
String data = """
{"component_name": \"""" + artifact.getName() + ":" + artifact.getTag() + """
","package_type": \"""" + artifact.getArtifactType() + """
","sha_256": \"""" + artifact.getSha256() + """
","violations": true,\
"include_ignored_violations": true,\
"license": true,\
"exclude_unknown": true,\
"security": true,\
"malicious_code": true,\
"iac": true,\
"services": true,\
"applications": true,\
"output_format": "json",\
"spdx": true,\
"spdx_format": "json",\
"cyclonedx": true,\
"cyclonedx_format": "json"}""";
return new XrayAPIRequest(url, XrayAPIRequest.RequestMethodEnum.POST, true, data);
}

public static XrayAPIRequest buildDeleteArtifact(String baseUrl, XrayArtifact artifact, String repository) {
public static XrayAPIRequest buildDeleteArtifact(String baseUrl, XrayWrapperArtifact artifact, String repository) {
String url = baseUrl + "/artifactory/" + repository + "/" + artifact.getName() + "/" + artifact.getTag();
String data = "";
return new XrayAPIRequest(url, XrayAPIRequest.RequestMethodEnum.DELETE, true, data);
}

public static XrayAPIRequest buildDeleteUploads(String baseUrl, XrayArtifact artifact, String repository) {
public static XrayAPIRequest buildDeleteUploads(String baseUrl, XrayWrapperArtifact artifact, String repository) {
String url = baseUrl + "/artifactory/" + repository + "/" + artifact.getName() + "/_uploads";
String data = "";
return new XrayAPIRequest(url, XrayAPIRequest.RequestMethodEnum.DELETE, true, data);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mercedesbenz.sechub.xraywrapper.http;
package com.mercedesbenz.sechub.xraywrapper.api;

import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -10,21 +10,23 @@

import com.mercedesbenz.sechub.xraywrapper.cli.XrayWrapperExitCode;
import com.mercedesbenz.sechub.xraywrapper.cli.XrayWrapperRuntimeException;
import com.mercedesbenz.sechub.xraywrapper.util.XrayAuthenticationHeader;
import com.mercedesbenz.sechub.xraywrapper.util.XrayAPIAuthenticationHeader;

public class XrayHttpRequestExecutor {
public class XrayAPIRequestExecutor {

static String authenticate() {
return XrayAuthenticationHeader.buildAuthHeader();
return XrayAPIAuthenticationHeader.buildAuthHeader();
}

/**
* Creates and Http get connection and sends request
* sets up an httpURL connection to the jfrog server
*
* @throws IOException
* @param request https API request
* @return https connection
* @throws XrayWrapperRuntimeException
*/
public static HttpURLConnection setUpGetConnection(XrayAPIRequest request) throws XrayWrapperRuntimeException {
URL url = null;
URL url;
try {
url = request.getUrl();
} catch (MalformedURLException e) {
Expand All @@ -34,7 +36,7 @@ public static HttpURLConnection setUpGetConnection(XrayAPIRequest request) throw
try {
con = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
throw new XrayWrapperRuntimeException("Error: could not open connection", e, XrayWrapperExitCode.IO_ERROR);
throw new XrayWrapperRuntimeException("Could not open https connection", e, XrayWrapperExitCode.IO_ERROR);
}
try {
con.setRequestMethod(request.getRequestMethodEnum().toString());
Expand All @@ -54,18 +56,18 @@ public static HttpURLConnection setUpGetConnection(XrayAPIRequest request) throw
try {
os = con.getOutputStream();
} catch (IOException e) {
throw new XrayWrapperRuntimeException("Could not get Output Stream for http connection", e, XrayWrapperExitCode.IO_ERROR);
throw new XrayWrapperRuntimeException("Could not get Output Stream for api connection", e, XrayWrapperExitCode.IO_ERROR);
}
byte[] input = new byte[0];
try {
input = request.getData().getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
throw new XrayWrapperRuntimeException("Could not get Encrypt http request", e, XrayWrapperExitCode.UNSUPPORTED_ENCRYPTION);
throw new XrayWrapperRuntimeException("Could not get Encrypt api request", e, XrayWrapperExitCode.UNSUPPORTED_ENCRYPTION);
}
try {
os.write(input, 0, input.length);
} catch (IOException e) {
throw new XrayWrapperRuntimeException("Could not write Output Stream for http connection", e, XrayWrapperExitCode.IO_ERROR);
throw new XrayWrapperRuntimeException("Could not write Output Stream for api connection", e, XrayWrapperExitCode.IO_ERROR);
}
return con;
} else {
Expand All @@ -74,7 +76,7 @@ public static HttpURLConnection setUpGetConnection(XrayAPIRequest request) throw
try {
con.connect();
} catch (IOException e) {
throw new XrayWrapperRuntimeException("Could not open http connection", e, XrayWrapperExitCode.IO_ERROR);
throw new XrayWrapperRuntimeException("Could not open api connection", e, XrayWrapperExitCode.IO_ERROR);
}
return con;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mercedesbenz.sechub.xraywrapper.http;
package com.mercedesbenz.sechub.xraywrapper.api;

import java.util.Collections;
import java.util.List;
Expand Down
Loading

0 comments on commit c984c56

Please sign in to comment.