参数名称修改

This commit is contained in:
even 2025-05-22 09:26:50 +08:00
parent c09ebec3c7
commit 1ee523dc21
3 changed files with 255 additions and 255 deletions

View File

@ -12,7 +12,7 @@ import java.util.concurrent.ConcurrentHashMap;
public class SecondStageRunContext extends BaseRunContext{ public class SecondStageRunContext extends BaseRunContext{
public SecondStageRunContext(PipStage contextDef,Integer childCount, PipelineRunContext parentContext, Map<String, Object> localVariables) { public SecondStageRunContext(PipStage contextDef,Integer childCount, PipelineRunContext parentContext, Map<String, Object> localVariables) {
super(contextDef,childCount, parentContext, LocalDateTime.now(), parentContext.getResourceId(), parentContext.getTargetId(), parentContext.getTargetType(), parentContext.getGlobalVariables(), localVariables, new ConcurrentHashMap<>()); super(contextDef,childCount, parentContext, LocalDateTime.now(), parentContext.getResourceId(), parentContext.getTargetVersionId(), parentContext.getTargetType(), parentContext.getGlobalVariables(), localVariables, new ConcurrentHashMap<>());
} }
@Override @Override

View File

@ -12,7 +12,7 @@ import java.util.Map;
public class TaskRunContext extends BaseRunContext{ public class TaskRunContext extends BaseRunContext{
public TaskRunContext(PipTask contextDef, SecondStageRunContext parentContext,Map<String,Object> localVariable) { public TaskRunContext(PipTask contextDef, SecondStageRunContext parentContext,Map<String,Object> localVariable) {
super(contextDef,0, parentContext, LocalDateTime.now(), parentContext.getResourceId(), parentContext.getTargetId(), parentContext.getTargetType(), parentContext.getGlobalVariables(),localVariable, new HashMap<>()); super(contextDef,0, parentContext, LocalDateTime.now(), parentContext.getResourceId(), parentContext.getTargetVersionId(), parentContext.getTargetType(), parentContext.getGlobalVariables(),localVariable, new HashMap<>());
} }
private TaskRunContext(PipBaseElement contextDef, BaseRunContext parentContext, LocalDateTime startTime, String resourceId, String targetId, String targetType, Map<String, Object> globalVariables, Map<String, Object> localVariables, Map<String, BaseRunContext> childContext) { private TaskRunContext(PipBaseElement contextDef, BaseRunContext parentContext, LocalDateTime startTime, String resourceId, String targetId, String targetType, Map<String, Object> globalVariables, Map<String, Object> localVariables, Map<String, BaseRunContext> childContext) {
super(contextDef,0, parentContext, startTime, resourceId, targetId, targetType, globalVariables, localVariables, childContext); super(contextDef,0, parentContext, startTime, resourceId, targetId, targetType, globalVariables, localVariables, childContext);

View File

@ -1,253 +1,253 @@
package cd.casic.ci.process.engine.worker; //package cd.casic.ci.process.engine.worker;
//
import cd.casic.ci.common.pipeline.annotation.Plugin; //import cd.casic.ci.common.pipeline.annotation.Plugin;
import cd.casic.ci.process.engine.configinfo.ScaSbomConfigInfo; //import cd.casic.ci.process.engine.configinfo.ScaSbomConfigInfo;
import cd.casic.ci.process.engine.runContext.BaseRunContext; //import cd.casic.ci.process.engine.runContext.BaseRunContext;
import cd.casic.ci.process.engine.runContext.TaskRunContext; //import cd.casic.ci.process.engine.runContext.TaskRunContext;
import cd.casic.ci.process.process.dataObject.base.PipBaseElement; //import cd.casic.ci.process.process.dataObject.base.PipBaseElement;
import cd.casic.ci.process.process.dataObject.pipeline.PipPipeline; //import cd.casic.ci.process.process.dataObject.pipeline.PipPipeline;
import cd.casic.ci.process.process.dataObject.target.TargetVersion; //import cd.casic.ci.process.process.dataObject.target.TargetVersion;
import cd.casic.ci.process.process.dataObject.task.PipTask; //import cd.casic.ci.process.process.dataObject.task.PipTask;
import cd.casic.ci.process.process.service.pipeline.PipelineService; //import cd.casic.ci.process.process.service.pipeline.PipelineService;
import cd.casic.ci.process.process.service.target.impl.TargetVersionServiceImpl; //import cd.casic.ci.process.process.service.target.impl.TargetVersionServiceImpl;
import cd.casic.ci.process.process.service.task.impl.TaskServiceImpl; //import cd.casic.ci.process.process.service.task.impl.TaskServiceImpl;
import cd.casic.framework.commons.exception.ServiceException; //import cd.casic.framework.commons.exception.ServiceException;
import cd.casic.framework.commons.exception.enums.GlobalErrorCodeConstants; //import cd.casic.framework.commons.exception.enums.GlobalErrorCodeConstants;
import cn.hutool.core.util.ObjectUtil; //import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON; //import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; //import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource; //import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; //import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; //import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils; //import org.springframework.beans.BeanUtils;
import org.springframework.core.io.FileSystemResource; //import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*; //import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap; //import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; //import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate; //import org.springframework.web.client.RestTemplate;
//
import java.io.File; //import java.io.File;
import java.security.KeyManagementException; //import java.security.KeyManagementException;
import java.security.KeyStoreException; //import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException; //import java.security.NoSuchAlgorithmException;
import java.util.*; //import java.util.*;
//
/** ///**
* @author HopeLi // * @author HopeLi
* @version v1.0 // * @version v1.0
* @ClassName ScaSbomWorker // * @ClassName ScaSbomWorker
* @Date: 2025/5/21 9:30 // * @Date: 2025/5/21 9:30
* @Description: // * @Description:
*/ // */
@Slf4j //@Slf4j
@Plugin(taskType = "ScaSbom") //@Plugin(taskType = "ScaSbom")
public class ScaSbomWorker extends HttpWorker{ //public class ScaSbomWorker extends HttpWorker{
//
private static final int POLLING_INTERVAL = 5000; // 轮询间隔单位毫秒 // private static final int POLLING_INTERVAL = 5000; // 轮询间隔单位毫秒
private static final int MAX_POLLING_TIMES = 100; // 最大退出次数 // private static final int MAX_POLLING_TIMES = 100; // 最大退出次数
//
@Resource // @Resource
private PipelineService pipelineService; // private PipelineService pipelineService;
//
@Resource // @Resource
private TargetVersionServiceImpl targetVersionService; // private TargetVersionServiceImpl targetVersionService;
//
@Resource // @Resource
private TaskServiceImpl pipelineNodeInfoService; // private TaskServiceImpl pipelineNodeInfoService;
//
//
public String work(BaseRunContext workerParam) { // public String work(BaseRunContext workerParam) {
int statusCode = 0; // int statusCode = 0;
//
PipBaseElement contextDef = workerParam.getContextDef(); // PipBaseElement contextDef = workerParam.getContextDef();
log.info("================SCA-SBOM节点执行==================="); // log.info("================SCA-SBOM节点执行===================");
if (ObjectUtil.isEmpty(contextDef)) { // if (ObjectUtil.isEmpty(contextDef)) {
log.error("未查询到节点[{}]配置,taskType = ScaSbom"); // log.error("未查询到节点[{}]配置,taskType = ScaSbom");
return "-1"; // return "-1";
} // }
if (ObjectUtil.isEmpty(contextDef)) { // if (ObjectUtil.isEmpty(contextDef)) {
log.error("未查询到节点[{}]配置,taskType = ScaSbom"); // log.error("未查询到节点[{}]配置,taskType = ScaSbom");
return "-1"; // return "-1";
} // }
//
String filePath = ""; // String filePath = "";
if (contextDef instanceof PipTask pipTask){ // if (contextDef instanceof PipTask pipTask){
// 查询并下载目标文件 // // 查询并下载目标文件
String pipelineId = pipTask.getPipelineId(); // String pipelineId = pipTask.getPipelineId();
//根据流水线id查询流水线信息 // //根据流水线id查询流水线信息
PipPipeline pipeline = pipelineService.getById(pipelineId); // PipPipeline pipeline = pipelineService.getById(pipelineId);
//根据目标id查询目标信息 // //根据目标id查询目标信息
if (StringUtils.isEmpty(pipeline.getTargetVersionId())){ // if (StringUtils.isEmpty(pipeline.getTargetVersionId())){
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"目标文件不存在"); // throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"目标文件不存在");
} // }
TargetVersion targetVersion = targetVersionService.getById(pipeline.getTargetVersionId()); // TargetVersion targetVersion = targetVersionService.getById(pipeline.getTargetVersionId());
filePath = targetVersion.getFilePath(); // filePath = targetVersion.getFilePath();
//
try { // try {
//
//
File file = new File(filePath); // File file = new File(filePath);
if (!file.exists() || !file.canRead()) { // if (!file.exists() || !file.canRead()) {
log.error("目标文件不存在或不可读"); // log.error("目标文件不存在或不可读");
// nodeLogger.appendErrorNow("目标文件不存在或不可读"); //// nodeLogger.appendErrorNow("目标文件不存在或不可读");
return "-1"; // return "-1";
} // }
//
handleUpload(workerParam, contextDef, pipTask.getTaskProperties(), file); // handleUpload(workerParam, contextDef, pipTask.getTaskProperties(), file);
}catch (Exception e){ // }catch (Exception e){
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"SCA-SBOM节点执行失败"); // throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"SCA-SBOM节点执行失败");
} // }
} // }
//
//
return statusCode + ""; // return statusCode + "";
} // }
//
private void handleUpload(BaseRunContext workerParam, PipBaseElement pipelineNodeConfigInfo, // private void handleUpload(BaseRunContext workerParam, PipBaseElement pipelineNodeConfigInfo,
Map<String,Object> scaSbomConfigInfo, File file) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException { // Map<String,Object> scaSbomConfigInfo, File file) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
RestTemplate restTemplate = getRestTemplateWithoutSANCheck(); // RestTemplate restTemplate = getRestTemplateWithoutSANCheck();
String scaUploadUrl = ConstantContextHolder.getScaIp() + "/openapi/v1/sbom/detect-file"; // String scaUploadUrl = ConstantContextHolder.getScaIp() + "/openapi/v1/sbom/detect-file";
MultiValueMap<String, Object> body = buildRequestBody(scaSbomConfigInfo, file); // MultiValueMap<String, Object> body = buildRequestBody(scaSbomConfigInfo, file);
HttpHeaders headers = createHeaders(); // HttpHeaders headers = createHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA); // headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.add("OpenApiUserToken", ConstantContextHolder.getScaToken()); // headers.add("OpenApiUserToken", ConstantContextHolder.getScaToken());
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers); // HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
//
log.info("SCA上传接口:" + scaUploadUrl); // log.info("SCA上传接口:" + scaUploadUrl);
JSONObject response = restTemplate.postForObject(scaUploadUrl, requestEntity, JSONObject.class); // JSONObject response = restTemplate.postForObject(scaUploadUrl, requestEntity, JSONObject.class);
String message = response.getString("message"); // String message = response.getString("message");
//
if (message.equals("success")) { // if (message.equals("success")) {
nodeLogger.appendNow("==================SCA上传成功================="); // nodeLogger.appendNow("==================SCA上传成功=================");
JSONObject data = response.getJSONObject("data"); // JSONObject data = response.getJSONObject("data");
Integer scaTaskId = data.getInteger("scaTaskId"); // Integer scaTaskId = data.getInteger("scaTaskId");
Integer applicationId = data.getInteger("applicationId"); // Integer applicationId = data.getInteger("applicationId");
//
scaSbomConfigInfo.setScaTaskId(scaTaskId); // scaSbomConfigInfo.setScaTaskId(scaTaskId);
scaSbomConfigInfo.setApplicationId(applicationId); // scaSbomConfigInfo.setApplicationId(applicationId);
//更新到节点json数据 // //更新到节点json数据
String nodeJson = JSON.toJSONString(scaSbomConfigInfo); // String nodeJson = JSON.toJSONString(scaSbomConfigInfo);
pipelineNodeConfigInfo.setInfo(nodeJson); // pipelineNodeConfigInfo.setInfo(nodeJson);
PipelineNodeInfoParam pipelineNodeInfoParam = new PipelineNodeInfoParam(); // PipelineNodeInfoParam pipelineNodeInfoParam = new PipelineNodeInfoParam();
BeanUtils.copyProperties(pipelineNodeConfigInfo, pipelineNodeInfoParam); // BeanUtils.copyProperties(pipelineNodeConfigInfo, pipelineNodeInfoParam);
pipelineNodeInfoService.update(pipelineNodeInfoParam); // pipelineNodeInfoService.update(pipelineNodeInfoParam);
workerParam.getPipelineNodeConfigInfo().setInfo(nodeJson); // workerParam.getPipelineNodeConfigInfo().setInfo(nodeJson);
//
pollTaskStatus(restTemplate, scaTaskId); // pollTaskStatus(restTemplate, scaTaskId);
} else if (message.equals("应用已经存在")) { // } else if (message.equals("应用已经存在")) {
Integer oldScaTaskId = scaSbomConfigInfo.getScaTaskId(); // Integer oldScaTaskId = scaSbomConfigInfo.getScaTaskId();
Integer oldApplicationId = scaSbomConfigInfo.getApplicationId(); // Integer oldApplicationId = scaSbomConfigInfo.getApplicationId();
int restartResult = reStartTask(restTemplate, oldApplicationId); // int restartResult = reStartTask(restTemplate, oldApplicationId);
if (restartResult != 0) { // if (restartResult != 0) {
return; // return;
} // }
pollTaskStatus(restTemplate, oldScaTaskId); // pollTaskStatus(restTemplate, oldScaTaskId);
} else { // } else {
nodeLogger.appendNow("==================SCA接口异常调用失败================="); // nodeLogger.appendNow("==================SCA接口异常调用失败=================");
} // }
} // }
//
private MultiValueMap<String, Object> buildRequestBody(ScaSbomConfigInfo scaSbomConfigInfo, File file) { // private MultiValueMap<String, Object> buildRequestBody(ScaSbomConfigInfo scaSbomConfigInfo, File file) {
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); // MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", new FileSystemResource(file)); // body.add("file", new FileSystemResource(file));
body.add("projectName", scaSbomConfigInfo.getProjectName()); // body.add("projectName", scaSbomConfigInfo.getProjectName());
body.add("applicationName", scaSbomConfigInfo.getApplicationName()); // body.add("applicationName", scaSbomConfigInfo.getApplicationName());
body.add("applicationVersion", scaSbomConfigInfo.getApplicationVersion()); // body.add("applicationVersion", scaSbomConfigInfo.getApplicationVersion());
body.add("applicationDescription", scaSbomConfigInfo.getApplicationDescription()); // body.add("applicationDescription", scaSbomConfigInfo.getApplicationDescription());
return body; // return body;
} // }
//
/** // /**
* 创建请求头 // * 创建请求头
* // *
* @return HttpHeaders // * @return HttpHeaders
*/ // */
private HttpHeaders createHeaders() { // private HttpHeaders createHeaders() {
HttpHeaders headers = new HttpHeaders(); // HttpHeaders headers = new HttpHeaders();
headers.add("OpenApiUserToken", ConstantContextHolder.getScaToken()); // headers.add("OpenApiUserToken", ConstantContextHolder.getScaToken());
return headers; // return headers;
} // }
//
/** // /**
* 轮询请求任务状态 // * 轮询请求任务状态
* // *
* @param restTemplate // * @param restTemplate
* @param scaTaskId // * @param scaTaskId
*/ // */
public void pollTaskStatus(RestTemplate restTemplate, Integer scaTaskId) { // public void pollTaskStatus(RestTemplate restTemplate, Integer scaTaskId) {
int currentPollingTimes = 0; // int currentPollingTimes = 0;
while (currentPollingTimes < MAX_POLLING_TIMES) { // while (currentPollingTimes < MAX_POLLING_TIMES) {
try { // try {
HttpHeaders headers = new HttpHeaders(); // HttpHeaders headers = new HttpHeaders();
headers.add("OpenApiUserToken", ConstantContextHolder.getScaToken()); // headers.add("OpenApiUserToken", ConstantContextHolder.getScaToken());
HttpEntity<String> requestEntity = new HttpEntity<>(null, headers); // HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
String scaStatusUrl = ConstantContextHolder.getScaIp() + "/openapi/v1/task/" + scaTaskId; // String scaStatusUrl = ConstantContextHolder.getScaIp() + "/openapi/v1/task/" + scaTaskId;
ResponseEntity<JSONObject> response = restTemplate.exchange(scaStatusUrl, HttpMethod.GET, requestEntity, JSONObject.class); // ResponseEntity<JSONObject> response = restTemplate.exchange(scaStatusUrl, HttpMethod.GET, requestEntity, JSONObject.class);
//
if (Objects.requireNonNull(response.getBody()).getString("message").equals("success")) { // if (Objects.requireNonNull(response.getBody()).getString("message").equals("success")) {
//"status": 5, //状态 0-未审计 1-未检测 2-排队中 3-检测中 4-检测暂停 5-检测完成 6-检测超时 7-手动停止 8-检测异常 9-已删除 10-拉取中 11-停止中 12-下载中 // //"status": 5, //状态 0-未审计 1-未检测 2-排队中 3-检测中 4-检测暂停 5-检测完成 6-检测超时 7-手动停止 8-检测异常 9-已删除 10-拉取中 11-停止中 12-下载中
int status = response.getBody().getJSONObject("data").getInteger("status"); // int status = response.getBody().getJSONObject("data").getInteger("status");
log.info("当前任务状态: " + status); // log.info("当前任务状态: " + status);
if (status == 5) { // if (status == 5) {
System.out.println("任务已完成,停止轮询。"); // System.out.println("任务已完成,停止轮询。");
log.info("任务已完成,停止轮询。"); // log.info("任务已完成,停止轮询。");
break; // break;
} // }
} else { // } else {
log.error("获取任务状态失败: " + response.getBody().getString("message")); // log.error("获取任务状态失败: " + response.getBody().getString("message"));
break; // break;
} // }
} catch (Exception e) { // } catch (Exception e) {
log.error("获取任务状态时发生错误: " + e.getMessage()); // log.error("获取任务状态时发生错误: " + e.getMessage());
} // }
try { // try {
// 轮询间隔 5 // // 轮询间隔 5
Thread.sleep(POLLING_INTERVAL); // Thread.sleep(POLLING_INTERVAL);
} catch (InterruptedException e) { // } catch (InterruptedException e) {
Thread.currentThread().interrupt(); // Thread.currentThread().interrupt();
log.error("轮询被中断: " + e.getMessage()); // log.error("轮询被中断: " + e.getMessage());
} // }
currentPollingTimes++; // currentPollingTimes++;
} // }
System.out.println("停止轮询"); // System.out.println("停止轮询");
} // }
//
/** // /**
* 重新检测接口 // * 重新检测接口
* // *
* @param restTemplate // * @param restTemplate
* @param applicationId // * @param applicationId
*/ // */
public int reStartTask(RestTemplate restTemplate, Integer applicationId) { // public int reStartTask(RestTemplate restTemplate, Integer applicationId) {
try { // try {
String url = ConstantContextHolder.getScaIp() + "/openapi/v1/task/batch/detect"; // String url = ConstantContextHolder.getScaIp() + "/openapi/v1/task/batch/detect";
HttpHeaders headers = new HttpHeaders(); // HttpHeaders headers = new HttpHeaders();
headers.add("OpenApiUserToken", ConstantContextHolder.getScaToken()); // headers.add("OpenApiUserToken", ConstantContextHolder.getScaToken());
headers.setContentType(MediaType.APPLICATION_JSON); // headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Accept", MediaType.APPLICATION_JSON.toString()); // headers.add("Accept", MediaType.APPLICATION_JSON.toString());
Map<String, List<Integer>> param = new HashMap<>(); // Map<String, List<Integer>> param = new HashMap<>();
param.put("applicationIds", Arrays.asList(applicationId)); // param.put("applicationIds", Arrays.asList(applicationId));
String s = JSON.toJSONString(param); // String s = JSON.toJSONString(param);
HttpEntity<String> formEntry = new HttpEntity<>(s, headers); // HttpEntity<String> formEntry = new HttpEntity<>(s, headers);
//
JSONObject res = restTemplate.postForObject(url, formEntry, JSONObject.class); // JSONObject res = restTemplate.postForObject(url, formEntry, JSONObject.class);
if (res.getString("message").equals("success")) { // if (res.getString("message").equals("success")) {
log.info("重新检测成功"); // log.info("重新检测成功");
return 0; // return 0;
} else { // } else {
log.error("重新检测失败"); // log.error("重新检测失败");
log.error(res.getString("message")); // log.error(res.getString("message"));
return -1; // return -1;
} // }
} catch (Exception e) { // } catch (Exception e) {
log.error("重新检测失败"); // log.error("重新检测失败");
log.error(e.getMessage()); // log.error(e.getMessage());
} // }
return 0; // return 0;
} // }
//
@Override // @Override
public void execute(TaskRunContext context) { // public void execute(TaskRunContext context) {
//
} // }
} //}