fix: ASW-165 调整文件目录,保持规范

This commit is contained in:
zhangshuai
2024-11-14 17:02:03 +08:00
parent acacbaec72
commit 2db15d0f4e
11 changed files with 107 additions and 56 deletions

View File

@@ -20,10 +20,6 @@ public class Constants {
*/
public static final String TEMP_PATH = System.getProperty("user.dir") + File.separator + "tmp";
/**
* playbook dir
*/
public static File PLAYBOOK_FILES_DIR = T.FileUtil.file(T.WebPathUtil.getRootPath(), "playbook_files");
/**
* 国际化语言列表
@@ -139,4 +135,27 @@ public class Constants {
return id;
}
}
/**
* 文件类型
*/
public static enum FileTypeEnum {
PCAP("pcap"),
PACKAGE("package"),
PLAYBOOK("playbook"),
JOB("job");
private String type;
FileTypeEnum(String type) {
this.type = type;
}
public String getType() {
return type;
}
}
}

View File

@@ -0,0 +1,14 @@
package net.geedge.asw.common.util;
import java.io.File;
public class FileResourceUtil {
private static String ROOT_PATH = T.WebPathUtil.getRootPath();
public static File createFile(String resourcePath, String workspaceId, String type, String id, String name) {
String sub = T.StrUtil.sub(id, 0, 2);
File file = T.FileUtil.file(ROOT_PATH, resourcePath, workspaceId, type, sub, id, name);
return file;
}
}

View File

@@ -7,10 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.geedge.asw.common.config.Query;
import net.geedge.asw.common.util.ASWException;
import net.geedge.asw.common.util.Constants;
import net.geedge.asw.common.util.RCode;
import net.geedge.asw.common.util.T;
import net.geedge.asw.common.util.*;
import net.geedge.asw.module.app.dao.PackageDao;
import net.geedge.asw.module.app.entity.PackageEntity;
import net.geedge.asw.module.app.service.IPackageService;
@@ -25,6 +22,7 @@ import net.geedge.asw.module.workbook.service.IWorkbookResourceService;
import net.geedge.asw.module.workbook.util.WorkbookConstant;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -48,6 +46,9 @@ public class PackageServiceImpl extends ServiceImpl<PackageDao, PackageEntity> i
@Autowired
private IJobService jobService;
@Value("${asw.resources.path:resources}")
private String resources;
@Override
public PackageEntity queryInfo(String id) {
PackageEntity entity = this.getById(id);
@@ -85,7 +86,7 @@ public class PackageServiceImpl extends ServiceImpl<PackageDao, PackageEntity> i
throw new ASWException(RCode.PACKAGE_FILE_TYPE_ERROR);
}
String saveFileName = pkgId + "." + suffix;
File destination = T.FileUtil.file(PkgConstant.APK_FILES_DIR, saveFileName);
File destination = FileResourceUtil.createFile(resources, workspaceId, Constants.FileTypeEnum.PACKAGE.getType(), pkgId, saveFileName);
PackageEntity entity = new PackageEntity();
ApkUtil apkUtil = new ApkUtil();
apkUtil.setAaptToolPath(Path.of(T.WebPathUtil.getRootPath(), "lib", "aapt").toString());

View File

@@ -156,7 +156,8 @@ public class PcapController {
HashMap<Object, Object> result = T.MapUtil.newHashMap();
PcapEntity pcap = pcapService.getById(id);
File pcapFile = pcap.getCommonPcapFilePath().toFile();
Map summary = T.JSONUtil.toBean(pcap.getSummary(), Map.class);
File pcapFile = FileUtil.file(T.MapUtil.getStr(summary, "commentPath"));
pcapFile = FileUtil.exist(pcapFile) ? pcapFile : T.FileUtil.file(pcap.getPath());
String uploadFileName = T.StrUtil.concat(true, id, ".", T.FileUtil.getSuffix(pcapFile));
File newFile = FileUtil.copy(pcapFile, FileUtil.file(Constants.TEMP_PATH, uploadFileName), false);

View File

@@ -7,14 +7,15 @@ import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import net.geedge.asw.common.util.T;
import net.geedge.asw.common.util.Constants;
import net.geedge.asw.common.util.FileResourceUtil;
import net.geedge.asw.module.app.entity.ApplicationEntity;
import net.geedge.asw.module.app.entity.PackageEntity;
import net.geedge.asw.module.environment.entity.EnvironmentEntity;
import net.geedge.asw.module.sys.entity.SysUserEntity;
import net.geedge.asw.module.workspace.entity.WorkspaceEntity;
import java.nio.file.Path;
import java.io.File;
@Data
@TableName("pcap")
@@ -55,8 +56,8 @@ public class PcapEntity {
private SysUserEntity createUser;
@JsonIgnore
public Path getCommonPcapFilePath() {
return Path.of(T.WebPathUtil.getRootPath(), this.workspaceId, "pcap_comment", this.id + ".pcapng");
public File getCommonPcapFilePath(String resources) {
return FileResourceUtil.createFile(resources, this.workspaceId, Constants.FileTypeEnum.PCAP.getType(), this.id, this.id + "-comment.pcapng");
}
}

View File

@@ -6,6 +6,7 @@ import cn.hutool.json.JSONObject;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.geedge.asw.common.util.Constants;
import net.geedge.asw.common.util.FileResourceUtil;
import net.geedge.asw.common.util.RCode;
import net.geedge.asw.common.util.T;
import net.geedge.asw.module.app.entity.PackageEntity;
@@ -26,6 +27,7 @@ import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.transaction.annotation.Transactional;
@@ -53,6 +55,9 @@ public class JobPlaybookExecResultChecker extends QuartzJobBean {
@Autowired
private IEnvironmentSessionService environmentSessionService;
@Value("${asw.resources.path:resources}")
private String resources;
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
Thread.currentThread().setName("JobPlaybookExecResultChecker");
@@ -226,8 +231,9 @@ public class JobPlaybookExecResultChecker extends QuartzJobBean {
fileName = T.StrUtil.concat(true, "job", T.StrUtil.DASHED, T.StrUtil.sub(job.getId(), 0, 8), ".pcap");
}
String pcapId = T.StrUtil.uuid();
// 上传 pcap 文件流
File pcapFile = T.FileUtil.file(T.WebPathUtil.getRootPath(), job.getWorkspaceId(), fileName);
File pcapFile = FileResourceUtil.createFile(resources, job.getWorkspaceId(), Constants.FileTypeEnum.PCAP.getType(), pcapId, pcapId + "pcap");
File parentDir = pcapFile.getParentFile();
if (!parentDir.exists()) {
parentDir.mkdirs();
@@ -235,7 +241,6 @@ public class JobPlaybookExecResultChecker extends QuartzJobBean {
inputStream = zipFile.getInputStream(fileHeader);
T.FileUtil.writeFromStream(inputStream, pcapFile);
PcapEntity entity = new PcapEntity();
String pcapId = T.StrUtil.uuid();
entity.setId(pcapId);
entity.setName(fileName);

View File

@@ -146,6 +146,8 @@ public class JobPlaybookExecutor extends QuartzJobBean {
}
private HttpResponse requestEnvPlaybook(JobEntity job, EnvironmentEntity environment) {
File zipFile = null;
try {
String playbookId = job.getPlaybookId();
String packageId = job.getPackageId();
@@ -160,7 +162,7 @@ public class JobPlaybookExecutor extends QuartzJobBean {
JSONObject paramJSONObject = environment.getParamJSONObject();
String url = paramJSONObject.getStr("url");
String token = paramJSONObject.getStr("token");
File zipFile = T.FileUtil.file(Constants.TEMP_PATH, T.StrUtil.concat(true, job.getId(), ".zip"));
zipFile = T.FileUtil.file(Constants.TEMP_PATH, T.StrUtil.concat(true, job.getId(), ".zip"));
HttpRequest request = T.HttpUtil.createPost(String.format("%s/api/v1/env/playbook", url));
T.ZipUtil.zip(zipFile, true, packageFile, playbookFile);
@@ -171,5 +173,8 @@ public class JobPlaybookExecutor extends QuartzJobBean {
HttpResponse response = request.execute();
return response;
} finally {
T.FileUtil.del(zipFile);
}
}
}

View File

@@ -7,9 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.geedge.asw.common.config.Query;
import net.geedge.asw.common.util.ASWException;
import net.geedge.asw.common.util.RCode;
import net.geedge.asw.common.util.T;
import net.geedge.asw.common.util.*;
import net.geedge.asw.module.app.entity.PackageEntity;
import net.geedge.asw.module.app.service.IPackageService;
import net.geedge.asw.module.environment.entity.EnvironmentEntity;
@@ -29,7 +27,7 @@ import net.geedge.asw.module.sys.service.ISysUserService;
import net.geedge.asw.module.workspace.entity.WorkspaceEntity;
import net.geedge.asw.module.workspace.service.IWorkspaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -65,6 +63,9 @@ public class JobServiceImpl extends ServiceImpl<JobDao, JobEntity> implements IJ
@Autowired
private IEnvironmentSessionService environmentSessionService;
@Value("${asw.resources.path:resources}")
private String resources;
/**
* rootPath/result/{jobId}
@@ -140,7 +141,8 @@ public class JobServiceImpl extends ServiceImpl<JobDao, JobEntity> implements IJ
this.save(entity);
// trace log file path
File traceLogFile = T.FileUtil.file(this.getJobResultPath(entity.getId()), "trace.log");
String saveFileName = entity.getId() + "." + "log";
File traceLogFile = FileResourceUtil.createFile(resources, entity.getWorkspaceId(), Constants.FileTypeEnum.JOB.getType(), entity.getId(), saveFileName);
this.update(new LambdaUpdateWrapper<JobEntity>()
.set(JobEntity::getLogPath, traceLogFile.getPath())
.eq(JobEntity::getId, entity.getId()));

View File

@@ -9,17 +9,12 @@ import cn.hutool.log.Log;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.geedge.asw.common.config.Query;
import net.geedge.asw.common.config.SpringContextUtils;
import net.geedge.asw.common.util.ASWException;
import net.geedge.asw.common.util.Constants;
import net.geedge.asw.common.util.RCode;
import net.geedge.asw.common.util.T;
import net.geedge.asw.common.util.*;
import net.geedge.asw.module.app.entity.PackageEntity;
import net.geedge.asw.module.app.service.IApplicationService;
import net.geedge.asw.module.app.service.IPackageService;
import net.geedge.asw.module.environment.entity.EnvironmentEntity;
import net.geedge.asw.module.environment.service.IEnvironmentService;
@@ -37,8 +32,6 @@ import net.geedge.asw.module.runner.util.RunnerConstant;
import net.geedge.asw.module.sys.entity.SysUserEntity;
import net.geedge.asw.module.sys.service.ISysConfigService;
import net.geedge.asw.module.sys.service.ISysUserService;
import net.geedge.asw.module.workbook.service.IWorkbookResourceService;
import net.geedge.asw.module.workbook.util.WorkbookConstant;
import net.geedge.asw.module.workspace.entity.WorkspaceEntity;
import net.geedge.asw.module.workspace.service.IWorkspaceService;
import org.apache.commons.io.FileUtils;
@@ -102,6 +95,9 @@ public class PcapServiceImpl extends ServiceImpl<PcapDao, PcapEntity> implements
@Value("${controller.url:http://127.0.0.1}")
private String aswControllerUrl;
@Value("${asw.resources.path:resources}")
private String resources;
@Override
public PcapEntity queryInfo(String id) {
PcapEntity pcap = this.getById(id);
@@ -160,7 +156,7 @@ public class PcapServiceImpl extends ServiceImpl<PcapDao, PcapEntity> implements
// path
String fileExtName = T.StrUtil.emptyToDefault(T.FileUtil.extName(fileResource.getFilename()), "pcap");
String saveFileName = pcapId + "." + fileExtName;
File destination = T.FileUtil.file(T.WebPathUtil.getRootPath(), workspaceId, saveFileName);
File destination = FileResourceUtil.createFile(resources, workspaceId, Constants.FileTypeEnum.PCAP.getType(), pcapId, saveFileName);
FileUtils.copyInputStreamToFile(fileResource.getInputStream(), destination);
entity.setPath(destination.getPath());
@@ -190,7 +186,8 @@ public class PcapServiceImpl extends ServiceImpl<PcapDao, PcapEntity> implements
PcapEntity pcap = this.getById(id);
// remove file
T.FileUtil.del(pcap.getPath());
T.FileUtil.del(pcap.getCommonPcapFilePath());
Map summary = T.JSONUtil.toBean(pcap.getSummary(), Map.class);
T.FileUtil.del(T.MapUtil.getStr(summary, "commentPath"));
// remove
this.removeById(id);
@@ -206,6 +203,7 @@ public class PcapServiceImpl extends ServiceImpl<PcapDao, PcapEntity> implements
Properties properties = new Properties();
properties.setProperty("sharkdApiHostAddr", this.sharkdApiHostAddr);
properties.setProperty("aswControllerUrl", this.aswControllerUrl);
properties.setProperty("resources", this.resources);
for (String id : ids) {
PcapEntity pcapEntity = this.getById(id);
@@ -273,10 +271,11 @@ public class PcapServiceImpl extends ServiceImpl<PcapDao, PcapEntity> implements
throw new RuntimeException("delete openSearch index error ", e);
}
pcapEntity.setStatus(RunnerConstant.PcapStatus.UPLOADED.getValue());
// del common pcap file
Map summary = T.JSONUtil.toBean(pcapEntity.getSummary(), Map.class);
T.FileUtil.del(T.MapUtil.getStr(summary, "commentPath"));
// reset summary
pcapEntity.setSummary("{}");
// del common pcap file
T.FileUtil.del(pcapEntity.getCommonPcapFilePath().toFile());
this.updateById(pcapEntity);
}
}

View File

@@ -11,6 +11,7 @@ import net.geedge.asw.module.runner.dao.PlaybookDao;
import net.geedge.asw.module.runner.entity.PlaybookEntity;
import net.geedge.asw.module.runner.service.IPlaybookService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
@@ -24,6 +25,9 @@ import java.util.Map;
public class PlaybookServiceImpl extends ServiceImpl<PlaybookDao, PlaybookEntity> implements IPlaybookService {
private final static Log log = Log.get();
@Value("${asw.resources.path:resources}")
private String resources;
@Override
public PlaybookEntity detail(String workspaceId, String id) {
PlaybookEntity playbook = this.baseMapper.queryInfo(workspaceId, id);
@@ -51,7 +55,7 @@ public class PlaybookServiceImpl extends ServiceImpl<PlaybookDao, PlaybookEntity
String playbookId = T.StrUtil.uuid();
String fileExtName = T.StrUtil.emptyToDefault(T.FileUtil.extName(file.getName()), "zip");
String saveFileName = playbookId + "." + fileExtName;
File destination = T.FileUtil.file(Constants.PLAYBOOK_FILES_DIR, saveFileName);
File destination = FileResourceUtil.createFile(resources, workspaceId, Constants.FileTypeEnum.PLAYBOOK.getType(), playbookId, saveFileName);
File unzip = null;
try {
playbook.setId(playbookId);

View File

@@ -106,7 +106,7 @@ public class PcapParserThread implements Runnable {
}
// file path: /{path}/{workspace_id}/pcap_comment/{pcap_id}.pcapng
File destination = pcapEntity.getCommonPcapFilePath().toFile();
File destination = pcapEntity.getCommonPcapFilePath(properties.getProperty("resources"));
T.FileUtil.del(destination);
FileUtils.copyInputStreamToFile(response.body().asInputStream(), destination);
}
@@ -219,7 +219,7 @@ public class PcapParserThread implements Runnable {
.put("sessions", jsonArray.size())
.put("packets", packets)
.put("services", services)
.put("commentPath", pcapEntity.getCommonPcapFilePath().toString())
.put("commentPath", pcapEntity.getCommonPcapFilePath(properties.getProperty("resources")).getAbsolutePath())
.build();
pcapService.update(new LambdaUpdateWrapper<PcapEntity>()
.set(PcapEntity::getSummary, T.JSONUtil.toJsonStr(m))