水木接口对接,配置文件修改

This commit is contained in:
even 2025-07-03 20:56:55 +08:00
parent 6dc52b2569
commit 87a1099920
12 changed files with 496 additions and 4 deletions

View File

@ -0,0 +1,17 @@
package cd.casic.ci.process.constant;
public class ShuiMuUrlConstant {
// 获取检测镜像
public static final String imageList = "/api/image/list";
public static final String getToken = "/oauth/token";
// 创建项目
public static final String createProject = "/api/driverGeneration/createDriverProject";
// 获取检测配置
public static final String readWfuzzJsonFile = "/api/driverGeneration/readWfuzzJsonFile";
// 获取检测 构建配置需要填写的信息
public static final String readHelpersJsonFile = "/api/driverGeneration/readHelpersJsonFile";
public static final String getProjectVersion = "/api/project/code/getProjectVersion";
public static final String buildAndSaveConfig = "/api/driverGeneration/buildAndSaveConfig";
public static final String getProjectById = "/api/project/";
public static final String uploadWfuzzJson = "/api/project/code/uploadWfuzzJson";
}

View File

@ -0,0 +1,10 @@
package cd.casic.ci.process.dto.req.shuimu;
import lombok.Data;
@Data
public class ShuiMuBuildAndSaveReq {
private String projectId;
private String env;
private String fileName;
}

View File

@ -0,0 +1,23 @@
package cd.casic.ci.process.dto.req.shuimu;
import lombok.Data;
@Data
public class ShuiMuProjectCreateReq {
/**
* 语言 例如 java
* */
private String language;
/**
* 应用分析资源
* */
private String env;
/**
* 2c4g
* */
private String envSpecs;
/**
* 流水线id
* */
private String pipelineId;
}

View File

@ -0,0 +1,9 @@
package cd.casic.ci.process.dto.req.shuimu;
import lombok.Data;
@Data
public class ShuiMuUpdateWFuzzJsonReq {
private String projectId;
private String build;
}

View File

@ -0,0 +1,18 @@
package cd.casic.ci.process.dto.resp.shuimu;
import lombok.Data;
@Data
public class ShuiMuProjectInfoResp {
private Integer id;
private String name;
private String language;
private String description;
private String product;
private String ownerId;
private Integer createBy;
private Long createAt;
private Long updateAt;
private Long deleteAt;
private Integer projectFlag;
}

View File

@ -0,0 +1,68 @@
package cd.casic.ci.process.dto.resp.shuimu;
import com.alibaba.fastjson.JSON;
import lombok.Data;
@Data
public class ShuiMuProjectVersionResp {
private Integer id;
private String vcs;
private String versionCode;
private Repo repo;
private Integer projectId;
private Integer createBy;
private Long createAt;
private Long updateAt;
private Long deleteAt;
private String ownerId;
private String workSpaceState;
private BuildState buildState;
private Boolean containsNeedFile;
private String buildResult;
private String buildLog;
private VerifyState verifyState;
private String verifyLog;
public void setRepo(Object repo) {
if (repo instanceof String) {
this.repo = JSON.parseObject((String) repo, Repo.class);
} else if (repo instanceof Repo) {
this.repo = (Repo) repo;
}
}
public void setBuildState(Object buildState) {
if (buildState instanceof String) {
this.buildState = JSON.parseObject((String) buildState, BuildState.class);
} else if (buildState instanceof BuildState) {
this.buildState = (BuildState) buildState;
}
}
public void setVerifyState(Object verifyState) {
if (verifyState instanceof String) {
this.verifyState = JSON.parseObject((String) verifyState, VerifyState.class);
} else if (verifyState instanceof VerifyState) {
this.verifyState = (VerifyState) verifyState;
}
}
@Data
public static class Repo {
private String env;
private String envSpecs;
private String filename;
private String filesize;
private String hash;
private String totalCount;
}
@Data
public static class BuildState {
private Integer state;
}
@Data
public static class VerifyState {
private Integer state;
}
}

View File

@ -0,0 +1,12 @@
package cd.casic.ci.process.dto.resp.shuimu;
import lombok.Data;
@Data
public class ShuiMuTokenResp {
private String accessToken;
private Integer expiresIn;
private Integer refreshExpiresIn;
private String refreshToken;
private String tokenType;
}

View File

@ -0,0 +1,32 @@
package cd.casic.ci.process.process.service.shuimu;
import cd.casic.ci.process.dto.req.shuimu.ShuiMuBuildAndSaveReq;
import cd.casic.ci.process.dto.req.shuimu.ShuiMuProjectCreateReq;
import cd.casic.ci.process.dto.req.shuimu.ShuiMuUpdateWFuzzJsonReq;
import cd.casic.ci.process.dto.resp.shuimu.ShuiMuProjectInfoResp;
import cd.casic.ci.process.dto.resp.shuimu.ShuiMuProjectVersionResp;
import java.util.List;
public interface ShuiMuService {
String getToken();
/**
* 获取编译环境
* */
List<String> imageList(String label);
void createProject(ShuiMuProjectCreateReq req);
/**
* 获取检测配置
* */
String readWfuzzJsonFile(String projectId);
/**
* 获取需要填写的表单项信息
* */
String readHelpersJsonFile(String projectId);
ShuiMuProjectVersionResp getProjectVersion(String projectId);
ShuiMuProjectInfoResp getProjectInfo(String projectId);
void buildAndSaveConfig(ShuiMuBuildAndSaveReq req);
void uploadWfuzzJson(ShuiMuUpdateWFuzzJsonReq req);
}

View File

@ -0,0 +1,231 @@
package cd.casic.ci.process.process.service.shuimu.impl;
import cd.casic.ci.process.dto.req.shuimu.ShuiMuBuildAndSaveReq;
import cd.casic.ci.process.dto.req.shuimu.ShuiMuProjectCreateReq;
import cd.casic.ci.process.dto.req.shuimu.ShuiMuUpdateWFuzzJsonReq;
import cd.casic.ci.process.dto.resp.shuimu.ShuiMuProjectInfoResp;
import cd.casic.ci.process.dto.resp.shuimu.ShuiMuProjectVersionResp;
import cd.casic.ci.process.dto.resp.shuimu.ShuiMuTokenResp;
import cd.casic.ci.process.engine.manager.RunContextManager;
import cd.casic.ci.process.process.dataObject.pipeline.PipPipeline;
import cd.casic.ci.process.process.dataObject.target.TargetVersion;
import cd.casic.ci.process.process.service.pipeline.PipelineService;
import cd.casic.ci.process.process.service.shuimu.ShuiMuService;
import cd.casic.ci.process.process.service.target.TargetVersionService;
import cd.casic.ci.process.properties.ShuiMuProperties;
import cd.casic.framework.commons.exception.ServiceException;
import cd.casic.framework.commons.exception.enums.GlobalErrorCodeConstants;
import cd.casic.framework.redis.core.RedisTemplateUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.io.*;
import java.util.*;
import static cd.casic.ci.process.constant.ShuiMuUrlConstant.*;
@Service
public class ShuiMuServiceImpl implements ShuiMuService {
@Resource
private ShuiMuProperties shuiMuProperties;
@Resource
private RestTemplate restTemplate;
@Resource
private RedisTemplateUtils redisTemplateUtils;
private static final String REDIS_SHUI_MU_TOKEN_KEY = "REDIS_SHUI_MU_TOKEN_KEY";
private static final String SHUI_MU_TOKEN_PREFIX = "Bearer ";
private static final String SHUI_MU_IMAGE_LIST_FILTER = "wfuzz-built";
@Resource
private TargetVersionService targetVersionService;
@Resource
private PipelineService pipelineService;
private ShuiMuTokenResp getTokenRemote(){
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String,String>> httpEntity = new HttpEntity<>(buildMapReq(),httpHeaders);
ResponseEntity<String> exchange = restTemplate.exchange(shuiMuProperties.getBaseUrl() +getToken, HttpMethod.POST, httpEntity, String.class, new HashMap<>());
String body = exchange.getBody();
JSONObject bodyObject = JSON.parseObject(body);
ShuiMuTokenResp tokenResp = new ShuiMuTokenResp();
tokenResp.setAccessToken(bodyObject.getString("access_token"));
tokenResp.setExpiresIn(bodyObject.getInteger("expires_in"));
tokenResp.setRefreshToken(bodyObject.getString("refresh_token"));
tokenResp.setTokenType(bodyObject.getString("token_type"));
return tokenResp;
}
private MultiValueMap<String,String> buildMapReq(){
MultiValueMap<String,String> req = new LinkedMultiValueMap<>();
req.add("username",shuiMuProperties.getUsername());
req.add("password",shuiMuProperties.getPassword());
req.add("grant_type",shuiMuProperties.getGrantType());
req.add("client_id",shuiMuProperties.getClientId());
return req;
}
@Override
public String getToken() {
Object o = redisTemplateUtils.get(REDIS_SHUI_MU_TOKEN_KEY);
String token = o instanceof String ? ((String) o) : "";
if (StringUtils.isEmpty(token)) {
synchronized(this){
o = redisTemplateUtils.get(REDIS_SHUI_MU_TOKEN_KEY);
token = o instanceof String ? ((String) o) : "";
if (StringUtils.isEmpty(token)) {
ShuiMuTokenResp tokenRemote = getTokenRemote();
String accessToken = tokenRemote.getAccessToken();
redisTemplateUtils.set(REDIS_SHUI_MU_TOKEN_KEY,accessToken,tokenRemote.getExpiresIn());
token = accessToken;
}
}
}
return SHUI_MU_TOKEN_PREFIX+token;
}
@Override
public List<String> imageList(String label) {
String uriString = UriComponentsBuilder.fromUriString(shuiMuProperties.getBaseUrl()+imageList).queryParam("label", label).toUriString();
HttpEntity<String> entity = new HttpEntity<>(null,getHeaders());
ResponseEntity<String> exchange = restTemplate.exchange(uriString, HttpMethod.GET, entity, String.class, new HashMap<>());
JSONObject jsonObject = JSONObject.parseObject(exchange.getBody());
JSONArray objects = JSONArray.parseArray(jsonObject.getString("data"));
List<String> res = new ArrayList<>(objects.size());
for (int i = 0; i < objects.size(); i++) {
JSONObject item = objects.getJSONObject(i);
String name = item.getString("name");
if (StringUtils.isNotEmpty(name)) {
res.add(name);
}
}
return res.stream().filter(item->item.startsWith(SHUI_MU_IMAGE_LIST_FILTER)).toList();
}
@Override
public void createProject(ShuiMuProjectCreateReq req) {
MultiValueMap<String,Object> body = new LinkedMultiValueMap<>();
body.add("projectName","应用检测_"+ UUID.randomUUID().toString().replaceAll("-",""));
body.add("language",req.getLanguage());
body.add("env",req.getEnv());
body.add("envSpecs",req.getEnvSpecs());
PipPipeline pipeline = pipelineService.getById(req.getPipelineId());
String targetVersionId = pipeline.getTargetVersionId();
TargetVersion targetVersion = targetVersionService.getById(targetVersionId);
File targetFile = new File(targetVersion.getFilePath());
if (!targetFile.exists()) {
throw new ServiceException(GlobalErrorCodeConstants.PIPELINE_ERROR.getCode(),"找不到目标文件");
}
FileSystemResource resource = new FileSystemResource(targetFile);
body.add("file",resource);
HttpEntity<MultiValueMap<String,Object>> entity = new HttpEntity<>(body,getHeaders());
ResponseEntity<String> exchange = restTemplate.exchange(shuiMuProperties.getBaseUrl() + createProject, HttpMethod.POST, entity, String.class, new HashMap<>());
HttpStatusCode statusCode = exchange.getStatusCode();
if (statusCode.isError()) {
throw new ServiceException(GlobalErrorCodeConstants.PIPELINE_ERROR.getCode(),"创建项目失败");
}
}
@Override
public String readWfuzzJsonFile(String projectId) {
HttpEntity<String> entity = new HttpEntity<>(null,getHeaders());
String uriString = UriComponentsBuilder.fromUriString(shuiMuProperties.getBaseUrl() + readWfuzzJsonFile)
.queryParam("projectId", projectId).toUriString();
ResponseEntity<String> exchange = restTemplate.exchange(uriString, HttpMethod.GET, entity, String.class, new HashMap<>());
return exchange.getBody();
}
@Override
public String readHelpersJsonFile(String projectId) {
HttpEntity<String> entity = new HttpEntity<>(null,getHeaders());
String uriString = UriComponentsBuilder.fromUriString(shuiMuProperties.getBaseUrl() + readHelpersJsonFile)
.queryParam("projectId", projectId).toUriString();
ResponseEntity<String> exchange = restTemplate.exchange(uriString, HttpMethod.GET, entity, String.class, new HashMap<>());
return exchange.getBody();
}
@Override
public ShuiMuProjectVersionResp getProjectVersion(String projectId) {
HttpEntity<String> entity = new HttpEntity<>(null,getHeaders());
String uriString = UriComponentsBuilder.fromUriString(shuiMuProperties.getBaseUrl() + getProjectVersion)
.queryParam("projectId", projectId).toUriString();
ResponseEntity<String> exchange = restTemplate.exchange(uriString, HttpMethod.GET, entity, String.class, new HashMap<>());
if (exchange.getStatusCode().isError()) {
throw new ServiceException(GlobalErrorCodeConstants.PIPELINE_ERROR.getCode(),"保存并且构建失败");
}
String data = JSON.parseObject(exchange.getBody()).getString("data");
return JSON.parseObject(data, ShuiMuProjectVersionResp.class);
}
@Override
public ShuiMuProjectInfoResp getProjectInfo(String projectId) {
HttpEntity<String> entity = new HttpEntity<>(null,getHeaders());
String uriString = UriComponentsBuilder.fromUriString(shuiMuProperties.getBaseUrl() + getProjectById +projectId)
.toUriString();
ResponseEntity<String> exchange = restTemplate.exchange(uriString, HttpMethod.GET, entity, String.class, new HashMap<>());
if (exchange.getStatusCode().isError()) {
throw new ServiceException(GlobalErrorCodeConstants.PIPELINE_ERROR.getCode(),"保存并且构建失败");
}
return JSON.parseObject(JSON.parseObject(exchange.getBody()).getString("data"), ShuiMuProjectInfoResp.class);
}
@Override
public void buildAndSaveConfig(ShuiMuBuildAndSaveReq req) {
MultiValueMap<String,String> body = new LinkedMultiValueMap<>();
body.add("projectId",req.getProjectId());
body.add("env",req.getEnv());
body.add("fileName",req.getFileName());
HttpEntity<MultiValueMap<String,String>> entity = new HttpEntity<>(body,getHeaders());
ResponseEntity<String> exchange = restTemplate.exchange(shuiMuProperties.getBaseUrl() + buildAndSaveConfig, HttpMethod.POST, entity, String.class, new HashMap<>());
if (exchange.getStatusCode().isError()) {
throw new ServiceException(GlobalErrorCodeConstants.PIPELINE_ERROR.getCode(),"保存并且构建失败");
}
}
@Override
public void uploadWfuzzJson(ShuiMuUpdateWFuzzJsonReq req) {
ShuiMuProjectInfoResp projectInfo = getProjectInfo(req.getProjectId());
String projectName = projectInfo.getName();
Map<String,String> restReq = new HashMap<>();
restReq.put("project",projectName);
restReq.put("product",projectInfo.getProduct());
restReq.put("language",projectInfo.getLanguage());
restReq.put("build",req.getBuild());
ByteArrayOutputStream bis = new ByteArrayOutputStream();
try {
ObjectOutputStream oos =new ObjectOutputStream(bis);
oos.writeObject(restReq);
File tempFile = File.createTempFile(UUID.randomUUID().toString().replaceAll("-", ""), "");
FileSystemResource resource = new FileSystemResource(tempFile);
MultiValueMap<String,Object> body = new LinkedMultiValueMap<>();
body.add("projectId",req.getProjectId());
body.add("file",resource);
HttpEntity<MultiValueMap<String,Object>> entity = new HttpEntity<>(body,getHeaders());
ResponseEntity<String> exchange = restTemplate.exchange(shuiMuProperties.getBaseUrl() + uploadWfuzzJson, HttpMethod.POST, entity, String.class, new HashMap<>());
if (exchange.getStatusCode().isError()) {
throw new ServiceException(GlobalErrorCodeConstants.PIPELINE_ERROR.getCode(),"更新配置文件失败");
}
tempFile.deleteOnExit();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private HttpHeaders getHeaders(){
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.set("authorization",getToken());
return httpHeaders;
}
}

View File

@ -0,0 +1,23 @@
package cd.casic.ci.process.properties;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "shuimu")
@Component
@Data
public class ShuiMuProperties {
@Value("username")
private String username;
@Value("password")
private String password;
@Value("baseUrl")
private String baseUrl;
@Value("grant_type")
private String grantType;
@Value("client_id")
private String clientId;
}

View File

@ -70,10 +70,10 @@ spring:
# Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
data:
redis:
host: 127.0.0.1 # 地址
port: 16379 # 端口
# host: 192.168.1.120 # 地址
# port: 6379 # 端口
# host: 127.0.0.1 # 地址
# port: 16379 # 端口
host: 192.168.1.120 # 地址
port: 6379 # 端口
database: 0 # 数据库索引
# password: dev # 密码,建议生产环境开启
@ -175,4 +175,10 @@ tartet:
username: roots
password: hnidc0327cn!@#xhh
remoteDir: /home/ops/ops-pro/file/
shuimu:
base-url: http://175.6.27.155:10200
password: saas@2022
username: admin
grant-type: password
client-id: client

View File

@ -0,0 +1,43 @@
package cd.casic.server;
import cd.casic.ci.process.dto.resp.shuimu.ShuiMuProjectVersionResp;
import cd.casic.ci.process.process.service.shuimu.ShuiMuService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
@SpringBootTest(classes = {OpsServerApplication.class})
@ActiveProfiles("local")
@Slf4j
public class ShuiMuTest {
@Resource
ShuiMuService shuiMuService;
@Test
public void getToken(){
System.out.println(shuiMuService.getToken());
}
@Test
public void imageList(){
System.out.println(shuiMuService.imageList("java"));
}
@Test
public void readWfuzzJsonFile(){
System.out.println(shuiMuService.readWfuzzJsonFile("6"));
}
@Test
public void readHelpersJsonFile(){
System.out.println(shuiMuService.readHelpersJsonFile("6"));
}
@Test
public void getProjectVersion(){
ShuiMuProjectVersionResp projectVersion = shuiMuService.getProjectVersion("6");
System.out.println(projectVersion);
System.out.println(projectVersion.getRepo());
}
@Test
public void getProjectInfo(){
System.out.println(shuiMuService.getProjectInfo("4"));
}
}