Adding logic to the GitHubService to check for the presence of a Cx Config file to override properties with
@@ -4,9 +4,13 @@ import com.checkmarx.flow.config.FlowProperties;
import com.checkmarx.flow.config.GitHubProperties;
import com.checkmarx.flow.dto.RepoIssue;
import com.checkmarx.flow.dto.ScanRequest;
+import com.checkmarx.flow.dto.Sources;
import com.checkmarx.flow.exception.GitHubClientException;
-import com.checkmarx.flow.utils.ScanUtils;
+import com.checkmarx.sdk.dto.CxConfig;
import com.checkmarx.sdk.dto.ScanResults;
+import com.checkmarx.sdk.exception.CheckmarxException;
+import com.checkmarx.flow.utils.ScanUtils;
+import org.apache.commons.lang3.exception.ExceptionUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -16,25 +20,24 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
-import java.beans.ConstructorProperties;
+import java.util.Base64;
import java.util.List;
import java.util.Map;
@Service
-public class GitHubService {
+public class GitHubService extends RepoService {
private static final Logger log = LoggerFactory.getLogger(GitHubService.class);
private final RestTemplate restTemplate;
private final GitHubProperties properties;
private final FlowProperties flowProperties;
+ private static final String FILE_CONENT = "/{namespace}/{repo}/contents/{config}?ref={branch}";
- @ConstructorProperties({"restTemplate", "properties", "flowProperties"})
public GitHubService(@Qualifier("flowRestTemplate") RestTemplate restTemplate, GitHubProperties properties, FlowProperties flowProperties) {
this.restTemplate = restTemplate;
this.properties = properties;
this.flowProperties = flowProperties;
}
-
private HttpHeaders createAuthHeaders(){
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set(HttpHeaders.AUTHORIZATION, "token ".concat(properties.getToken()));
@@ -118,4 +121,50 @@ public class GitHubService {
requestBody.put("context", "checkmarx");
return requestBody;
}
+
+ @Override
+ public Sources getRepoContent() {
+ return null;
+ }
+
+ @Override
+ public CxConfig getCxConfigOverride(ScanRequest request) throws CheckmarxException {
+ //"/{namespace}/{repo}/contents/{config}?ref={branch}"
+ HttpHeaders headers = createAuthHeaders();
+ try {
+ ResponseEntity<String> response = restTemplate.exchange(
+ properties.getApiUrl().concat(FILE_CONENT),
+ HttpMethod.GET,
+ new HttpEntity(headers),
+ String.class,
+ request.getNamespace(),
+ request.getRepoName(),
+ properties.getConfigAsCode(),
+ request.getBranch()
+ );
+ if(response.getBody() == null){
+ log.warn("HTTP Body is null for content api ");
+ }
+ else {
+ JSONObject json = new JSONObject(response.getBody());
+ String content = json.getString("content");
+ if(ScanUtils.empty(content)){
+ log.warn("Content not found in JSON response");
+ return null;
+ }
+ String decodedContent = new String(Base64.getDecoder().decode(content.trim()));
+ return com.checkmarx.sdk.utils.ScanUtils.getConfigAsCode(decodedContent);
+ }
+ }catch (NullPointerException e){
+ log.warn("Content not found in JSON response");
+ }catch (HttpClientErrorException.NotFound e){
+ log.info("No Config As code was found [{}]", properties.getConfigAsCode());
+ }catch (HttpClientErrorException e){
+ log.error(ExceptionUtils.getRootCauseMessage(e));
+ }
+ catch (Exception e){
+ log.error(ExceptionUtils.getRootCauseMessage(e));
+ }
+ return null;
+ }
}
\ No newline at end of file
