Skip to content

Commit

Permalink
optimization of time, adding config API
Browse files Browse the repository at this point in the history
  • Loading branch information
siewer committed Jul 28, 2024
1 parent 0ac3492 commit 4e64d1b
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.mixeway.api.project.model.AssetDashboardModel;
import io.mixeway.api.project.model.EditProjectAssetModel;
import io.mixeway.api.project.model.NewVulnerabilityModel;
import io.mixeway.api.project.model.ProjectVulnTrendChart;
import io.mixeway.api.project.model.*;
import io.mixeway.api.project.service.OperateOnAssetsService;

import io.mixeway.api.protocol.cioperations.PrepareCIOperation;
Expand Down Expand Up @@ -121,4 +118,10 @@ public ResponseEntity<List<ProjectVulnerability>> getAssetVulnerabilities(@PathV
return operateOnAssetsService.getAssetVulnerabilities(id, type, principal);
}

@PreAuthorize("hasAuthority('ROLE_USER')")
@GetMapping("/{id}/{type}/cicd/config")
public ResponseEntity<MixewayYamlConfigDto> generateCondig(@PathVariable("id") Long id, @PathVariable("type") String type,Principal principal) throws IOException {
return operateOnAssetsService.generateCondig(id, type, principal);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.mixeway.api.project.model;

import io.mixeway.db.entity.CodeProject;
import liquibase.pro.packaged.A;
import lombok.Data;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;

@Data
@Log4j2
public class MixewayYamlConfigDto {
Code code;
WA webapp;

public MixewayYamlConfigDto buildCodeResponse(CodeProject codeProject, List<CodeProject> childs) {
try {
this.code = new Code();
this.code.id = codeProject.getParent()==null ? codeProject.getId() : codeProject.getParent().getId();
this.code.name = codeProject.getParent()==null ? codeProject.getName() : codeProject.getParent().getName();
List<App> apps = new ArrayList<>();
for (CodeProject cp : childs) {
App app = new App();
app.setId(cp.getId());
app.setName(cp.getName());
app.setPath(cp.getPath());
app.setSca_name(cp.getRemotename());
apps.add(app);
}
this.code.apps = apps;
if (code.apps.isEmpty()){
this.code.sca_name = codeProject.getRemotename();
}

} catch (NullPointerException e ){
log.warn("generating mixeway.yaml for child");
}
return this;
}

@Data
public static class Code {
Long id;
String name;
String sca_name;
List<App> apps;
}
@Data
public static class App {
Long id;
String name;
String path;
String sca_name;
}
@Data
public static class WA {
Long id;
String scanType;
String backendUrl;
String openApiFilePath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class ProjectAssetModel {
String branch;
String type;
String path;
String parent;
String[] scope;
AssetVulns vulnerabilities;

Expand All @@ -39,6 +40,7 @@ public ProjectAssetModel convertCodeProject(CodeProject codeProject, int crit, i
sscope.add("sast");
}
}

AssetVulns assetVulns = new AssetVulns();
assetVulns.setCritical(crit);
assetVulns.setMedium(medium);
Expand All @@ -52,6 +54,9 @@ public ProjectAssetModel convertCodeProject(CodeProject codeProject, int crit, i
projectAssetModel.setPath(codeProject.getPath());
projectAssetModel.setVulnerabilities(assetVulns);
projectAssetModel.setScope(sscope.toArray(new String[0]));
if (codeProject.getParent() != null){
projectAssetModel.setParent(codeProject.getParent().getName());
}
return projectAssetModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -148,31 +149,38 @@ public Interface createInterface(JsonNode rootNode, Project project, Principal p
public List<ProjectAssetModel> getAssetsForProject(Project project) {
List<ProjectAssetModel> projectAssetModels = new ArrayList<>();

findCodeProjectService.findByProject(project).forEach(cp -> {
AssetHistory assetHistory = findAssetHistoryService.getAssetHistory(cp).stream()
.max(Comparator.comparing(AssetHistory::getInserted)).orElse(new AssetHistory());

projectAssetModels.add(new ProjectAssetModel().convertCodeProject(cp, assetHistory.getCrit() + assetHistory.getHigh(),
assetHistory.getMedium(), assetHistory.getLow()));
});

findWebAppService.findByProject(project).forEach(wa -> {
AssetHistory assetHistory = findAssetHistoryService.getAssetHistory(wa).stream()
.max(Comparator.comparing(AssetHistory::getInserted)).orElse(new AssetHistory());
projectAssetModels.add(new ProjectAssetModel().convertWebApp(wa,
assetHistory.getCrit() + assetHistory.getHigh(),
assetHistory.getMedium(), assetHistory.getLow(), (assetHistory.getCrit() + assetHistory.getHigh() + assetHistory.getMedium() + assetHistory.getLow()) > 0));
});

findInterfaceService.findByAssetIn(new ArrayList<>(project.getAssets())).forEach(intf -> {
AssetHistory assetHistory = findAssetHistoryService.getAssetHistory(intf).stream()
.max(Comparator.comparing(AssetHistory::getInserted)).orElse(new AssetHistory());
projectAssetModels.add(new ProjectAssetModel().convertInterface(intf, assetHistory.getCrit() + assetHistory.getHigh(),
assetHistory.getMedium(), assetHistory.getLow(), (assetHistory.getCrit() + assetHistory.getHigh() + assetHistory.getMedium() + assetHistory.getLow()) > 0));
});
// Combine all find service calls into parallel streams to leverage concurrency
Stream.concat(Stream.concat(
findCodeProjectService.findByProject(project).parallelStream().map(cp -> {
AssetHistory assetHistory = findAssetHistoryService.getAssetHistory(cp).stream()
.max(Comparator.comparing(AssetHistory::getInserted)).orElse(new AssetHistory());

return new ProjectAssetModel().convertCodeProject(cp, assetHistory.getCrit() + assetHistory.getHigh(),
assetHistory.getMedium(), assetHistory.getLow());
}),
findWebAppService.findByProject(project).parallelStream().map(wa -> {
AssetHistory assetHistory = findAssetHistoryService.getAssetHistory(wa).stream()
.max(Comparator.comparing(AssetHistory::getInserted)).orElse(new AssetHistory());

return new ProjectAssetModel().convertWebApp(wa,
assetHistory.getCrit() + assetHistory.getHigh(),
assetHistory.getMedium(), assetHistory.getLow(),
(assetHistory.getCrit() + assetHistory.getHigh() + assetHistory.getMedium() + assetHistory.getLow()) > 0);
})),
findInterfaceService.findByAssetIn(new ArrayList<>(project.getAssets())).parallelStream().map(intf -> {
AssetHistory assetHistory = findAssetHistoryService.getAssetHistory(intf).stream()
.max(Comparator.comparing(AssetHistory::getInserted)).orElse(new AssetHistory());

return new ProjectAssetModel().convertInterface(intf, assetHistory.getCrit() + assetHistory.getHigh(),
assetHistory.getMedium(), assetHistory.getLow(),
(assetHistory.getCrit() + assetHistory.getHigh() + assetHistory.getMedium() + assetHistory.getLow()) > 0);
})
).forEach(projectAssetModels::add);

return projectAssetModels;
}


public ResponseEntity<?> editCodeProject(EditProjectAssetModel editProjectAssetModel, Optional<CodeProject> codeProject, Principal principal) {
if (codeProject.isPresent()) {
if (permissionFactory.canUserAccessProject(principal, codeProject.get().getProject())) {
Expand Down Expand Up @@ -442,4 +450,35 @@ public ResponseEntity<List<ProjectVulnerability>> getAssetVulnerabilities(Long i
pv -> !pv.getStatus().getName().equals(vulnTemplate.STATUS_REMOVED.getName())).collect(Collectors.toList()),
HttpStatus.OK);
}

public ResponseEntity<MixewayYamlConfigDto> generateCondig(Long id, String type,Principal principal) {
MixewayYamlConfigDto mixewayYamlConfigDto = new MixewayYamlConfigDto();
switch (type) {
case "codeProject":
Optional<CodeProject> codeProject = findCodeProjectService.findById(id);
if (codeProject.isPresent() && permissionFactory.canUserAccessProject(principal, codeProject.get().getProject())) {
mixewayYamlConfigDto = mixewayYamlConfigDto.buildCodeResponse(codeProject.get(), findCodeProjectService.getProjectChilds(codeProject.get()));
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
break;
case "webApp":
Optional<WebApp> webApp = findWebAppService.findById(id);
if (webApp.isPresent() && permissionFactory.canUserAccessProject(principal, webApp.get().getProject())) {
MixewayYamlConfigDto.WA wa = new MixewayYamlConfigDto.WA();
wa.setId(webApp.get().getId());
wa.setBackendUrl(webApp.get().getUrl());
mixewayYamlConfigDto.setWebapp(wa);

} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
break;
default:
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(mixewayYamlConfigDto, HttpStatus.OK);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ public interface CodeProjectRepository extends JpaRepository<CodeProject, Long>
void updateCodeProjectBranch(@Param("id") Long id, @Param("branch") String branch);

List<CodeProject> findByProjectIn(List<Project> enabledVulnManageProjects);

List<CodeProject> findByParent(CodeProject codeProject);
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ public List<CodeProject> getCodeProjectsInListOfProjects(List<Project> enabledVu
public List<CodeProject> findAll() {
return codeProjectRepository.findAll();
}

public List<CodeProject> getProjectChilds(CodeProject codeProject) {
return codeProjectRepository.findByParent(codeProject);
}
}

0 comments on commit 4e64d1b

Please sign in to comment.