1、规则删去策略模板、防护对象。增加sdl字段

2、任务增加策略模板、防护对象
3、相应地方修改
This commit is contained in:
PushM
2024-05-28 02:21:58 +08:00
parent df19bef4fa
commit 388201a97f
22 changed files with 428 additions and 359 deletions

View File

@@ -1,8 +1,6 @@
package com.realtime.protection.configuration.entity.rule.dynamicrule;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
import com.realtime.protection.configuration.utils.enums.audit.AuditStatusEnum;
import com.realtime.protection.configuration.utils.enums.audit.AuditStatusValidator;
import io.swagger.v3.oas.annotations.media.Schema;
@@ -46,19 +44,7 @@ public class DynamicRuleObject {
@Schema(description = "动态规则创建用户所属部门", accessMode = Schema.AccessMode.READ_ONLY)
private String dynamicRuleCreateDepart;
//动态规则选择的防护对象ids
@NotNull(message = "防护对象不能为NULL")
@NotEmpty(message = "防护对象不能为空")
@JsonProperty("protect_object_ids")
@Schema(description = "请求时发送防护对象ID", accessMode = Schema.AccessMode.WRITE_ONLY, requiredMode = Schema.RequiredMode.REQUIRED)
private List<Integer> protectObjectIds;
@JsonProperty("protect_objects")
@Schema(description = "响应时回复防护对象完整信息", accessMode = Schema.AccessMode.READ_ONLY)
private List<ProtectObject> protectObjects;
@JsonProperty("strategy_template_name")
@Schema(description = "动态规则选择的策略模板", accessMode = Schema.AccessMode.READ_ONLY)
private String templateName;
//还没有建立外键
@@ -73,40 +59,23 @@ public class DynamicRuleObject {
@JsonProperty("dynamic_rule_source_system")
@Schema(description = "动态规则数据来源系统", example = "bw系统", requiredMode = Schema.RequiredMode.REQUIRED)
private String dynamicRuleSourceSystem;
@JsonProperty("dynamic_rule_event_type")
@Schema(description = "动态规则事件类型", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
private String dynamicRuleEventType;
@NotNull
@JsonProperty("template_id")
@Schema(description = "策略模板id", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
private Integer templateId;
// @NotNull
@JsonProperty("dynamic_rule_protect_level")
@Max(value = 3)
@Min(value = 1)
@Schema(description = "防护等级1代表日常态、2代表应急态、3代表紧急态", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
private Integer dynamicRuleProtectLevel;
@NotNull
// @NotNull
@JsonProperty("dynamic_rule_priority")
@Schema(description = "优先级", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
// @Schema(description = "优先级", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
private Integer dynamicRulePriority;
@NotNull
// @NotNull
// @JsonProperty("dynamic_rule_range")
// @Schema(description = "范围", example = "北京", requiredMode = Schema.RequiredMode.REQUIRED)
// private String dynamicRuleRange;
// @NotNull
@JsonProperty("dynamic_rule_frequency")
@Schema(description = "频率", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
// @Schema(description = "频率", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
private Integer dynamicRuleFrequency;
@NotNull
@JsonProperty("log_rule_id")
@Schema(description = "筛选条件-日志规则id", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
private Long logRuleId;
// @NotNull
@JsonProperty("dynamic_rule_audit_status")
@ExcelIgnore
@Schema(description = "动态规则审核状态0为未审核1为已退回2为审核通过", example = "2", accessMode = Schema.AccessMode.READ_ONLY)
private Integer auditStatus;
@@ -120,6 +89,14 @@ public class DynamicRuleObject {
@Schema(description = "审核用户部门", example = "部门1")
private String auditUserDepart;
@JsonProperty("bw_sql")
@Schema(description = "BW系统所需sql", example = "select * from table1 where rule_id=12222")
private String bwSql;
@JsonProperty("description")
@Schema(description = "动态规则描述", example = "动态规则描述")
private String description;
public void checkAuditStatusValidate(AuditStatusEnum newAuditStatus) {
if (!List.of(AuditStatusEnum.AUDITED.getNum(), AuditStatusEnum.USING.getNum()).contains(this.getAuditStatus())) {
throw new IllegalArgumentException("规则《" + this.getDynamicRuleName() + "》原审批状态非法");

View File

@@ -138,8 +138,8 @@ public class StaticRuleObject {
@ExcelProperty("优先级")
@Max(value = 3)
@Min(value = 1)
@Schema(description = "优先级,1代表高2代表中3代表低", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
private Integer staticRulePriority;
@Schema(description = "优先级,1代表高2代表中3代表低", example = "1")
private Integer staticRulePriority = 1;
// @JsonProperty("static_rule_range")
// @ExcelProperty("范围")
// @Schema(description = "范围", example = "北京", requiredMode = Schema.RequiredMode.REQUIRED)
@@ -147,7 +147,7 @@ public class StaticRuleObject {
@JsonProperty("static_rule_frequency")
@ExcelIgnore
@Min(value = 1)
@Schema(description = "频率最低为1", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
@Schema(description = "频率最低为1", example = "1")
private Integer staticRuleFrequency = 2;
@JsonProperty("audit_user_name")

View File

@@ -1,6 +1,7 @@
package com.realtime.protection.configuration.entity.task;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Future;
import jakarta.validation.constraints.NotNull;
@@ -101,4 +102,40 @@ public class Task {
@JsonProperty("audit_user_depart")
@Schema(description = "审核用户部门", example = "部门1")
private String auditUserDepart;
/*
策略模板(事件类型、防护等级)都移动到任务表中
*/
@JsonProperty("event_type")
@Schema(description = "事件类型", example = "反射型ddos")
private String eventType;
@JsonProperty("protect_level")
@Schema(description = "防护等级", example = "低=1中=2高=3")
private Integer protectLevel;
@JsonProperty("template_id")
@Schema(description = "策略模板ID", example = "1")
private Integer templateId;
@JsonProperty("template_name")
@Schema(description = "策略模板名称", example = "策略模板1")
private String templateName;
@JsonProperty("source_system")
@Schema(description = "数据来源系统", example = "bw系统")
private String sourceSystem;
@JsonProperty("protect_object_ids")
@Schema(description = "防护对象ID列表", example = "[1, 2, 3]")
private List<Integer> protectObjectIds;
@JsonProperty("protect_objects")
@Schema(description = "完整防护对象列表", example = "[{protect_object_id: 1, protect_object_name: '北京'}, {protect_object_id: 2, protect_object_name: '上海'}]",
accessMode = Schema.AccessMode.READ_ONLY)
private List<ProtectObject> protectObjects;
}

View File

@@ -61,9 +61,9 @@ public class TaskCommandInfo {
private String eventType;
@Schema(description = "指令下发频率", example = "30")
@NotNull(message = "指令下发频率不能为空。")
// @NotNull(message = "指令下发频率不能为空。")
@JsonProperty("frequency")
private Integer frequency;
private Integer frequency = 3;
@Schema(description = "任务开始时间", example = "2025-10-14T10:23:33")
@NotNull(message = "任务开始时间不能为空。")

View File

@@ -22,8 +22,7 @@ public class AlertMessageService {
private final AlertMessageMapper alertMessageMapper;
public AlertMessageService(
CommandService commandService,
AlertMessageMapper alertMessageMapper) {
CommandService commandService, AlertMessageMapper alertMessageMapper) {
this.commandService = commandService;
this.alertMessageMapper = alertMessageMapper;
}

View File

@@ -15,14 +15,10 @@ public interface DynamicRuleMapper {
//新建动态规则
void newDynamicRule(@Param("object") DynamicRuleObject dynamicRuleObject);
//新建动态规则与保护对象关联
Boolean newDynamicRulProtectObjectConcat(long dynamicRuleId, Integer protectObjectId);
void deleteDynamicRuleObject(Integer dynamicRuleId);
DynamicRuleObject queryDynamicRuleById(Integer dynamicRuleId);
List<ProtectObject> queryProtectObjectByRuleId(Integer dynamicRuleId);
Boolean updateDynamicRuleObject(@Param("dynamicRuleId") Integer dynamicRuleId, @Param("object") DynamicRuleObject dynamicRuleObject);
@@ -36,12 +32,6 @@ public interface DynamicRuleMapper {
String auditUserName, String createDate,
Integer page, Integer pageSize);
void deleteDynamicRuleProtectObjectConcat(Integer dynamicRuleId);
TemplateNew queryTemplateByRuleId(Integer dynamicRuleId);
boolean newDynamicRulProtectObjectsConcat(Integer dynamicRuleId, List<Integer> protectObjectIds);
boolean queryProtectObjectById(Integer protectObjectId);
Integer queryDynamicRuleTotalNum(String dynamicRuleName, Integer dynamicRuleId,

View File

@@ -1,7 +1,6 @@
package com.realtime.protection.server.rule.dynamicrule;
import com.alibaba.excel.util.ListUtils;
import com.realtime.protection.configuration.entity.defense.template.TemplateNew;
import com.realtime.protection.configuration.entity.rule.dynamicrule.DynamicRuleObject;
import com.realtime.protection.configuration.utils.Counter;
import com.realtime.protection.configuration.utils.SqlSessionWrapper;
@@ -33,13 +32,6 @@ public class DynamicRuleService {
@Transactional
public Integer newDynamicRuleObject(DynamicRuleObject dynamicRule) {
//判断protectObject id是否有效
boolean ProtectObjIdValid = dynamicRule.getProtectObjectIds().stream()
.allMatch(dynamicRuleMapper::queryProtectObjectById);
if (!ProtectObjIdValid) {
throw new IllegalArgumentException("protect object id is invalid");
}
dynamicRule.setDynamicRuleDisplayId(
"DTGZ-"
+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
@@ -48,11 +40,8 @@ public class DynamicRuleService {
);
dynamicRuleMapper.newDynamicRule(dynamicRule);
Integer dynamicRuleId = dynamicRule.getDynamicRuleId();
dynamicRule.getProtectObjectIds().forEach(
protectObjectId -> dynamicRuleMapper.newDynamicRulProtectObjectConcat(dynamicRuleId, protectObjectId));
return dynamicRuleId;
return dynamicRule.getDynamicRuleId();
}
//批量新建多个动态规则
@@ -117,20 +106,7 @@ public class DynamicRuleService {
//查询DynamicRule
DynamicRuleObject dynamicRuleObject = dynamicRuleMapper.queryDynamicRuleById(dynamicRuleId);
if (dynamicRuleObject == null){
throw new IllegalArgumentException("dynamic rule id is invalid");
}
//查询DynamicRule关联的ProtectObject
dynamicRuleObject.setProtectObjects(dynamicRuleMapper.queryProtectObjectByRuleId(dynamicRuleId));
//查询DynamicRule关联的template详细信息
TemplateNew template = dynamicRuleMapper.queryTemplateByRuleId(dynamicRuleId);
if (template == null){
//template在表中删除了需要重新设置template感觉这种情况不多见
dynamicRuleObject.setDynamicRuleSourceSystem(null);
dynamicRuleObject.setDynamicRuleEventType(null);
}else{
dynamicRuleObject.setDynamicRuleSourceSystem(template.getSourceSystem());
dynamicRuleObject.setDynamicRuleEventType(template.getTemplateName());
dynamicRuleObject.setDynamicRuleProtectLevel(Integer.valueOf(template.getProtectLevel()));
throw new IllegalArgumentException("动态规则不存在");
}
return dynamicRuleObject;
@@ -139,22 +115,24 @@ public class DynamicRuleService {
@Transactional
public boolean updateDynamicRuleObject(Integer dynamicRuleId, DynamicRuleObject dynamicRuleObject) {
//更新DynamicRule
dynamicRuleObject.setDynamicRuleModifyTime(LocalDateTime.now());
// dynamicRuleMapper
Boolean idValid = dynamicRuleMapper.updateDynamicRuleObject(dynamicRuleId, dynamicRuleObject);
if (!idValid) {
throw new IllegalArgumentException("dynamic rule id is invalid");
throw new IllegalArgumentException("动态规则id无效");
}
/*
//删除DynamicRule关联的ProtectObject
dynamicRuleMapper.deleteDynamicRuleProtectObjectConcat(dynamicRuleId);
//新增DynamicRule关联的ProtectObject
boolean ProtectObjIdValid = dynamicRuleObject.getProtectObjectIds().stream()
.allMatch(dynamicRuleMapper::queryProtectObjectById);
if (!ProtectObjIdValid) {
throw new IllegalArgumentException("protect object id is invalid");
throw new IllegalArgumentException("防护对象id无效");
}
return dynamicRuleMapper.newDynamicRulProtectObjectsConcat(dynamicRuleId, dynamicRuleObject.getProtectObjectIds());
*/
return idValid;
}
// 批量删除
@@ -233,11 +211,7 @@ public class DynamicRuleService {
return resultMap;
}
/**
* 用于任务状态修改时,内部批量更新任务所属动态规则的状态,不需要修改审批人信息
* @param idsWithAuditStatusMap
* @return
*/
public Object updateAuditStatusBatch(Map<Integer, Integer> idsWithAuditStatusMap) {
//校验id和status是否合法
List<Integer> originalAuditStatusList = dynamicRuleMapper.queryAuditStatusByIds(idsWithAuditStatusMap);

View File

@@ -98,6 +98,7 @@ public class TaskController implements TaskControllerApi {
@RequestParam(value = "event_type", required = false) String eventType,
@RequestParam(value = "create_time", required = false) LocalDate createTime,
@RequestParam(value = "start_time", required = false) LocalDate startTime,
@RequestParam(value = "protect_level", required = false) Integer protectLevel,
@RequestParam("page") @Min(1) Integer page,
@RequestParam("page_size") @Min(1) Integer pageSize) {
@@ -112,11 +113,11 @@ public class TaskController implements TaskControllerApi {
}
List<Task> tasks = taskService.queryTasks(taskStatus, taskType, taskName, taskCreator, auditStatus,
taskAct, taskAuditor, taskSource, ruleName,
eventType, createDateStr, startTimeStr, page, pageSize);
eventType, createDateStr, startTimeStr,protectLevel, page, pageSize);
return ResponseResult.ok()
.setData("task_list", tasks)
.setData("total_num", taskService.queryTaskTotalNum(taskStatus, taskType, taskName, taskCreator, auditStatus,
taskAct, taskAuditor, taskSource, ruleName,eventType, createDateStr, startTimeStr));
taskAct, taskAuditor, taskSource, ruleName,eventType, createDateStr, startTimeStr,protectLevel));
}
@Override
@@ -140,6 +141,8 @@ public class TaskController implements TaskControllerApi {
.setData("success", taskService.updateTask(task));
}
@Override
@GetMapping("/{taskId}/audit/{auditStatus}")
public ResponseResult changeTaskAuditStatus(@PathVariable @NotNull @Max(10) Integer auditStatus,
@@ -254,11 +257,11 @@ public class TaskController implements TaskControllerApi {
public ResponseResult statistics() {
return ResponseResult.ok()
.setData("total_num", taskService.queryTaskTotalNum(null, null, null, null, null,
null, null, null, null,null,null,null))
null, null, null, null,null,null,null,null))
.setData("running_num", taskService.queryTaskTotalNum(StateEnum.RUNNING.getStateNum(), null, null, null, null,
null, null, null, null,null,null,null))
null, null, null, null,null,null,null,null))
.setData("finished_num", taskService.queryTaskTotalNum(StateEnum.FINISHED.getStateNum(), null, null, null, null,
null, null, null, null,null,null,null))
null, null, null, null,null,null,null,null))
.setData("unaudit_num", taskService.queryAuditTaskTotalNum(
AuditStatusEnum.PENDING.getNum()
));

View File

@@ -193,6 +193,7 @@ public interface TaskControllerApi {
@Parameter(name = "task_source", description = "任务来源系统"),
@Parameter(name = "rule_name", description = "规则名称"),
@Parameter(name = "event_type", description = "事件类型"),
@Parameter(name = "protect_level", description = "防护等级"),
@Parameter(name = "create_time", description = "任务创建时间",example = "2021-01-01" ),
@Parameter(name = "start_time", description = "任务开始时间",example = "2024-04-25" ),
@@ -212,6 +213,7 @@ public interface TaskControllerApi {
@RequestParam(value = "event_type", required = false) String eventType,
@RequestParam(value = "create_time", required = false) LocalDate createTime,
@RequestParam(value = "start_time", required = false) LocalDate startTime,
@RequestParam(value = "protect_level", required = false) Integer protectLevel,
@RequestParam("page") @Min(1) Integer page,
@RequestParam("page_size") @Min(1) Integer pageSize);

View File

@@ -1,5 +1,6 @@
package com.realtime.protection.server.task;
import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
import com.realtime.protection.configuration.entity.task.DynamicTaskInfo;
import com.realtime.protection.configuration.entity.task.Task;
import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
@@ -31,6 +32,7 @@ public interface TaskMapper {
@Param("event_type")String eventType,
@Param("create_date") String createDateStr,
@Param("start_date")String startDateStr,
@Param("protect_level")Integer protectLevel,
@Param("page") Integer page, @Param("page_size") Integer pageSize);
Task queryTask(@Param("task_id") Long taskId);
@@ -78,7 +80,8 @@ public interface TaskMapper {
@Param("task_create_time") LocalDateTime taskCreateTime,
@Param("event_type")String eventType,
@Param("create_date")String createDate,
@Param("start_date")String startDate);
@Param("start_date")String startDate,
@Param("protect_level")Integer protectLevel);
void updateAuditStatusByIdBatch(@Param("idWithAuditStatusBatch") Map<Integer, Integer> idWithAuditStatusBatch);
@@ -94,4 +97,11 @@ public interface TaskMapper {
@Param("auditUserName")String auditUserName,
@Param("auditUserId")String auditUserId,
@Param("auditUserDepart")String auditUserDepart);
void newTaskProtectObjectConcat(@Param("taskId")Long taskId,
@Param("protectObjectIds")List<Integer> protectObjectIds);
void deleteTaskProtectObjectConcat(Long taskId);
List<ProtectObject> queryProtectObjectsByTaskId(Long id);
}

View File

@@ -2,6 +2,7 @@ package com.realtime.protection.server.task;
import com.alibaba.excel.util.MapUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.realtime.protection.configuration.entity.defense.object.ProtectObject;
import com.realtime.protection.configuration.entity.rule.dynamicrule.DynamicRuleObject;
import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject;
import com.realtime.protection.configuration.entity.task.DynamicTaskInfo;
@@ -10,6 +11,7 @@ import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
import com.realtime.protection.configuration.utils.Counter;
import com.realtime.protection.configuration.utils.SqlSessionWrapper;
import com.realtime.protection.configuration.utils.enums.StateEnum;
import com.realtime.protection.configuration.utils.enums.TaskTypeEnum;
import com.realtime.protection.configuration.utils.enums.audit.AuditStatusEnum;
import com.realtime.protection.configuration.utils.enums.audit.AuditStatusValidator;
import com.realtime.protection.server.rule.dynamicrule.DynamicRuleMapper;
@@ -54,6 +56,7 @@ public class TaskService {
// task.setTaskCreateUsername("xxx");
// task.setTaskCreateDepart("xxx");
task.setTaskDisplayId(
"RW-"
+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
@@ -61,7 +64,6 @@ public class TaskService {
+ String.format("%06d", counter.generateId("task")));
taskMapper.newTask(task);
if (task.getStaticRuleIds() != null && !task.getStaticRuleIds().isEmpty()) {
staticRuleMapper.queryStaticRuleByIds(task.getStaticRuleIds()).forEach(staticRuleObject -> {
if (!staticRuleObject.getAuditStatus().equals(AuditStatusEnum.AUDITED.getNum())) {
@@ -86,6 +88,17 @@ public class TaskService {
});
taskMapper.newTaskDynamicRuleConcat(task.getTaskId(), task.getDynamicRuleIds());
}
if (task.getTaskType() != TaskTypeEnum.STATIC.getTaskType()){
//校验防护对象是否存在
boolean ProtectObjIdValid = task.getProtectObjectIds().stream()
.allMatch(dynamicRuleMapper::queryProtectObjectById);
if (!ProtectObjIdValid) {
throw new IllegalArgumentException("部分防护对象不存在");
}
//任务和防护对象多对多关联建立
taskMapper.newTaskProtectObjectConcat(task.getTaskId(), task.getProtectObjectIds());
}
return task.getTaskId();
}
@@ -219,10 +232,11 @@ public class TaskService {
String taskAct, String taskAuditor,
String taskSource, String ruleName,
String eventType,String createDateStr, String startDateStr,
Integer protectLevel,
Integer page, Integer pageSize) {
List<Task> tasks = taskMapper.queryTasks(taskStatus, taskType, taskName, taskCreator, auditStatus,
taskAct, taskAuditor, taskSource, ruleName,eventType, createDateStr, startDateStr, page, pageSize);
taskAct, taskAuditor, taskSource, ruleName,eventType, createDateStr, startDateStr,protectLevel, page, pageSize);
for (Task task : tasks) {
if (task == null) {
continue;
@@ -243,6 +257,8 @@ public class TaskService {
if (task == null) {
return null;
}
List<ProtectObject> protectObjects = taskMapper.queryProtectObjectsByTaskId(id);
task.setProtectObjects(protectObjects);
task.setStaticRuleIds(taskMapper.queryStaticRuleIdsFromTaskId(task.getTaskId(),
List.of(AuditStatusEnum.AUDITED.getNum(), AuditStatusEnum.USING.getNum())));
@@ -260,7 +276,20 @@ public class TaskService {
task.setTaskAuditStatus(AuditStatusEnum.PENDING.getNum());
//校验防护对象是否存在
boolean ProtectObjIdValid = task.getProtectObjectIds().stream()
.allMatch(dynamicRuleMapper::queryProtectObjectById);
if (!ProtectObjIdValid) {
throw new IllegalArgumentException("部分防护对象不存在");
}
//删除task关联的protectObjects
taskMapper.deleteTaskProtectObjectConcat(task.getTaskId());
//更新task
taskMapper.updateTask(task);
//重新关联task和protectObjects
taskMapper.newTaskProtectObjectConcat(task.getTaskId(), task.getProtectObjectIds());
taskMapper.clearTaskConnectedStaticRule(task.getTaskId());
taskMapper.clearTaskConnectedDynamicRule(task.getTaskId());
@@ -354,9 +383,9 @@ public class TaskService {
public Integer queryTaskTotalNum(Integer taskStatus, Integer taskType, String taskName, String taskCreator, Integer auditStatus
,String taskAct, String taskAuditor, String taskSource, String ruleName,
String eventType, String createDate, String startDate) {
String eventType, String createDate, String startDate,Integer protectLevel) {
return taskMapper.queryTaskTotalNum(taskStatus, taskType, taskName, taskCreator, auditStatus,
taskAct, taskAuditor, taskSource, ruleName,null, eventType, createDate, startDate);
taskAct, taskAuditor, taskSource, ruleName,null, eventType, createDate, startDate, protectLevel);
}
public Boolean updateAuditStatusBatch(Map<Integer, Integer> idsWithAuditStatusMap) {