diff --git a/src/main/java/com/realtime/protection/configuration/entity/alert/AlertMessage.java b/src/main/java/com/realtime/protection/configuration/entity/alert/AlertMessage.java index aad73c3..907ee2d 100644 --- a/src/main/java/com/realtime/protection/configuration/entity/alert/AlertMessage.java +++ b/src/main/java/com/realtime/protection/configuration/entity/alert/AlertMessage.java @@ -55,7 +55,7 @@ public class AlertMessage { @JsonProperty("c_priority") private Integer c_priority; @JsonProperty("c_time") - private LocalDateTime c_time; + private Long c_time; @JsonProperty("c_flowid") private String c_flowid; @JsonProperty("c_src_ipv4") @@ -80,8 +80,8 @@ public class AlertMessage { private Integer c_d_tunnel_port; @JsonProperty("c_proto_type") private Integer c_proto_type; - @JsonProperty("c_return_info") - private String c_return_info; + + @JsonProperty("c_s_boundary") private Long c_s_boundary; @JsonProperty("c_s_region") @@ -108,8 +108,8 @@ public class AlertMessage { private String c_d_owner; @JsonProperty("c_ret_file_type") private Integer c_ret_file_type; - @JsonProperty("c_ret_filename") - private String c_ret_filename; + @JsonProperty("c_ret_file_name") + private String c_ret_file_name; @JsonProperty("c_ret_file") private String c_ret_file; @JsonProperty("c_url") @@ -134,6 +134,15 @@ public class AlertMessage { private Long c_d_mark4; @JsonProperty("c_d_mark5") private Long c_d_mark5; + + @JsonProperty("display_id") + private String display_id; + + @JsonProperty("c_return_info") + private String c_return_info; + + + // "c_priority": 0, // "c_time": 1714528212, // "c_flowid": "ca0c192021", diff --git a/src/main/java/com/realtime/protection/configuration/entity/task/TaskCommandInfo.java b/src/main/java/com/realtime/protection/configuration/entity/task/TaskCommandInfo.java index 32d9900..29eff5f 100644 --- a/src/main/java/com/realtime/protection/configuration/entity/task/TaskCommandInfo.java +++ b/src/main/java/com/realtime/protection/configuration/entity/task/TaskCommandInfo.java @@ -115,6 +115,9 @@ public class TaskCommandInfo { @Schema(description = "指令所属任务的运行状态", accessMode = Schema.AccessMode.READ_ONLY) private Integer taskStatus; + @Schema(description = "指令展示id", accessMode = Schema.AccessMode.READ_ONLY) + private String displayId; + // 复制构造函数 public void copyTaskCommandInfo(TaskCommandInfo original) { this.UUID = original.UUID; diff --git a/src/main/java/com/realtime/protection/server/alertmessage/AlertMessageService.java b/src/main/java/com/realtime/protection/server/alertmessage/AlertMessageService.java index 585fe03..1eb2852 100644 --- a/src/main/java/com/realtime/protection/server/alertmessage/AlertMessageService.java +++ b/src/main/java/com/realtime/protection/server/alertmessage/AlertMessageService.java @@ -5,12 +5,15 @@ import com.realtime.protection.configuration.entity.alert.AlertMessage; import com.realtime.protection.configuration.entity.defense.template.ProtectLevel; import com.realtime.protection.configuration.entity.task.FiveTupleWithMask; import com.realtime.protection.configuration.entity.task.TaskCommandInfo; +import com.realtime.protection.configuration.utils.Counter; import com.realtime.protection.configuration.utils.enums.StateEnum; import com.realtime.protection.configuration.utils.enums.TaskTypeEnum; import com.realtime.protection.server.command.CommandService; import lombok.Data; import org.springframework.stereotype.Service; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -20,11 +23,13 @@ import java.util.UUID; public class AlertMessageService { private final CommandService commandService; private final AlertMessageMapper alertMessageMapper; + private final Counter counter; public AlertMessageService( - CommandService commandService, AlertMessageMapper alertMessageMapper) { + CommandService commandService, AlertMessageMapper alertMessageMapper, Counter counter) { this.commandService = commandService; this.alertMessageMapper = alertMessageMapper; + this.counter = counter; } @DSTransactional @@ -119,9 +124,17 @@ public class AlertMessageService { String commandUUID = commandService.createCommand(dynamicTaskCommandInfo); //alertmessage入库 + alertMessage.setCommandUUID(commandUUID); String alertMessageUUID = UUID.randomUUID().toString(); alertMessage.setAlertMessageUUID(alertMessageUUID); + alertMessage.setDisplay_id( + "GJ-" + + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + + "-" + + String.format("%06d", counter.generateId("alert_message")) + + ); alertMessageMapper.insertAlertMessage(alertMessage); } @@ -131,6 +144,13 @@ public class AlertMessageService { alertMessage.setCommandUUID(null); String alertMessageUUID = UUID.randomUUID().toString(); alertMessage.setAlertMessageUUID(alertMessageUUID); + alertMessage.setDisplay_id( + "GJ-" + + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + + "-" + + String.format("%06d", counter.generateId("alert_message")) + + ); alertMessageMapper.insertAlertMessage(alertMessage); return alertMessageUUID; diff --git a/src/main/java/com/realtime/protection/server/command/CommandService.java b/src/main/java/com/realtime/protection/server/command/CommandService.java index 9174954..2108fba 100644 --- a/src/main/java/com/realtime/protection/server/command/CommandService.java +++ b/src/main/java/com/realtime/protection/server/command/CommandService.java @@ -4,10 +4,13 @@ import com.alibaba.excel.util.ListUtils; import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.dynamic.datasource.annotation.DSTransactional; import com.realtime.protection.configuration.entity.task.TaskCommandInfo; +import com.realtime.protection.configuration.utils.Counter; import com.realtime.protection.configuration.utils.SqlSessionWrapper; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.List; import java.util.UUID; import java.util.function.Function; @@ -19,11 +22,13 @@ public class CommandService { private final CommandMapper commandMapper; private final SqlSessionWrapper sqlSessionWrapper; + private final Counter counter; private static final int BatchSize = 100; - public CommandService(CommandMapper commandMapper, SqlSessionWrapper sqlSessionWrapper) { + public CommandService(CommandMapper commandMapper, SqlSessionWrapper sqlSessionWrapper, Counter counter) { this.commandMapper = commandMapper; this.sqlSessionWrapper = sqlSessionWrapper; + this.counter = counter; } @DSTransactional @@ -32,6 +37,12 @@ public class CommandService { if (uuid != null) { return uuid; } + commandInfo.setDisplayId( + "ZL-" + + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + + "-" + + String.format("%06d", counter.generateId("command")) + ); commandInfo.setUUID(UUID.randomUUID().toString()); commandMapper.createCommand(commandInfo); diff --git a/src/main/java/com/realtime/protection/server/defense/object/ProtectObjectMapper.java b/src/main/java/com/realtime/protection/server/defense/object/ProtectObjectMapper.java index 4c07bcd..27f1604 100644 --- a/src/main/java/com/realtime/protection/server/defense/object/ProtectObjectMapper.java +++ b/src/main/java/com/realtime/protection/server/defense/object/ProtectObjectMapper.java @@ -5,6 +5,7 @@ import com.realtime.protection.configuration.response.ResponseResult; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -60,4 +61,12 @@ public interface ProtectObjectMapper { Boolean updateAuditInfo(@Param("ids")List ids, @Param("auditInfo")String auditInfo); String queryProtectObjectAuditInfo(Integer id); + + void insertStaticRuleStatusLog(Integer id); + + void updateStaticRuleStatusLogExpireTime(Integer id); + + void insertStaticRuleStatusLogBatch(List ids); + + void updateStaticRuleStatusLogExpireTimeBatch(List ids); } diff --git a/src/main/java/com/realtime/protection/server/defense/object/ProtectObjectService.java b/src/main/java/com/realtime/protection/server/defense/object/ProtectObjectService.java index d194428..68f6b8a 100644 --- a/src/main/java/com/realtime/protection/server/defense/object/ProtectObjectService.java +++ b/src/main/java/com/realtime/protection/server/defense/object/ProtectObjectService.java @@ -2,21 +2,17 @@ package com.realtime.protection.server.defense.object; import com.alibaba.excel.util.ListUtils; import com.realtime.protection.configuration.entity.defense.object.ProtectObject; -import com.realtime.protection.configuration.response.ResponseResult; +import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject; import com.realtime.protection.configuration.utils.Counter; import com.realtime.protection.configuration.utils.SqlSessionWrapper; import com.realtime.protection.configuration.utils.enums.audit.AuditStatusEnum; import com.realtime.protection.configuration.utils.enums.audit.AuditStatusValidator; -import com.realtime.protection.server.whitelist.WhiteListMapper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.function.Function; @Service @@ -45,6 +41,7 @@ public class ProtectObjectService { if (protectObject.getProtectObjectId() == null) { return 0; } + insertStaticRuleStatusLog(protectObject.getProtectObjectId()); return protectObject.getProtectObjectId(); } @@ -145,6 +142,7 @@ public class ProtectObjectService { } Boolean success = protectObjectMapper.changeProtectObjectAuditStatus(protectObjectId, auditStatus); + insertStaticRuleStatusLog(protectObjectId); Integer auditStatusNow = protectObjectMapper.queryProtectObject(protectObjectId).getProtectObjectAuditStatus(); Map resultMap = new HashMap<>(); resultMap.put("success", success); @@ -204,10 +202,12 @@ public class ProtectObjectService { } //mapper指的就是外层函数输入的参数,也就是WhiteListMapper mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch); + insertStaticRuleStatusLog(idWithAuditStatusBatch); idWithAuditStatusBatch.clear(); } if (!idWithAuditStatusBatch.isEmpty()) { mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch); + insertStaticRuleStatusLog(idWithAuditStatusBatch); } return true; }; @@ -223,4 +223,27 @@ public class ProtectObjectService { public String queryAuditInfo(Integer id) { return protectObjectMapper.queryProtectObjectAuditInfo(id); } + + public void insertStaticRuleStatusLog(Integer id) { + //先把之前的状态日志过期,再插入新的状态日志 + protectObjectMapper.updateStaticRuleStatusLogExpireTime(id); + protectObjectMapper.insertStaticRuleStatusLog(id); + } + public void insertStaticRuleStatusLog(Map idWithAuditStatusBatch) { + Set keys = idWithAuditStatusBatch.keySet(); + ArrayList ids = new ArrayList<>(keys); + + protectObjectMapper.updateStaticRuleStatusLogExpireTimeBatch(ids); + protectObjectMapper.insertStaticRuleStatusLogBatch(ids); + } + + public void insertStaticRuleStatusLog(List objects) { + List ids = new ArrayList<>(); + for (StaticRuleObject staticRuleObject : objects) { + ids.add(staticRuleObject.getStaticRuleId()); + } + + protectObjectMapper.updateStaticRuleStatusLogExpireTimeBatch(ids); + protectObjectMapper.insertStaticRuleStatusLogBatch(ids); + } } diff --git a/src/main/java/com/realtime/protection/server/defense/templatenew/TemplateMapper.java b/src/main/java/com/realtime/protection/server/defense/templatenew/TemplateMapper.java index 76fb5a5..3c51286 100644 --- a/src/main/java/com/realtime/protection/server/defense/templatenew/TemplateMapper.java +++ b/src/main/java/com/realtime/protection/server/defense/templatenew/TemplateMapper.java @@ -5,6 +5,7 @@ import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -61,4 +62,11 @@ public interface TemplateMapper { String queryAuditInfo(Integer id); + void updateStatusLogExpireTime(Integer id); + + void insertStatusLog(Integer id); + + void updateStatusLogExpireTimeBatch(List ids); + + void insertStatusLogBatch(List ids); } diff --git a/src/main/java/com/realtime/protection/server/defense/templatenew/TemplateService.java b/src/main/java/com/realtime/protection/server/defense/templatenew/TemplateService.java index 1a303b7..d9f1442 100644 --- a/src/main/java/com/realtime/protection/server/defense/templatenew/TemplateService.java +++ b/src/main/java/com/realtime/protection/server/defense/templatenew/TemplateService.java @@ -1,6 +1,7 @@ package com.realtime.protection.server.defense.templatenew; import com.realtime.protection.configuration.entity.defense.template.TemplateNew; +import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject; import com.realtime.protection.configuration.utils.Counter; import com.realtime.protection.configuration.utils.SqlSessionWrapper; import com.realtime.protection.configuration.utils.enums.audit.AuditStatusValidator; @@ -8,10 +9,7 @@ import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.function.Function; @Service @@ -41,6 +39,7 @@ public class TemplateService { if (template.getTemplateId() == null) { return 0; } + insertStatusLog(template.getTemplateId()); return template.getTemplateId(); } @@ -103,7 +102,7 @@ public class TemplateService { throw new IllegalArgumentException("invalid audit status"); } Boolean success = templateMapper.updateAuditStatusById(id, auditStatus); - + insertStatusLog(id); Map resultMap = new HashMap<>(); resultMap.put("success", success); resultMap.put("audit_status", auditStatus); @@ -145,10 +144,12 @@ public class TemplateService { } //mapper指的就是外层函数输入的参数,也就是WhiteListMapper mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch); + insertStatusLog(idWithAuditStatusBatch); idWithAuditStatusBatch.clear(); } if (!idWithAuditStatusBatch.isEmpty()) { mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch); + insertStatusLog(idWithAuditStatusBatch); } return true; }; @@ -165,4 +166,26 @@ public class TemplateService { public String queryAuditInfo(Integer id) { return templateMapper.queryAuditInfo(id); } + + public void insertStatusLog(Integer id) { + templateMapper.updateStatusLogExpireTime(id); + templateMapper.insertStatusLog(id); + } + public void insertStatusLog(Map idWithAuditStatusBatch) { + Set keys = idWithAuditStatusBatch.keySet(); + ArrayList ids = new ArrayList<>(keys); + + templateMapper.updateStatusLogExpireTimeBatch(ids); + templateMapper.insertStatusLogBatch(ids); + } + + public void insertStatusLog(List objects) { + List ids = new ArrayList<>(); + for (StaticRuleObject staticRuleObject : objects) { + ids.add(staticRuleObject.getStaticRuleId()); + } + + templateMapper.updateStatusLogExpireTimeBatch(ids); + templateMapper.insertStatusLogBatch(ids); + } } diff --git a/src/main/java/com/realtime/protection/server/rule/dynamicrule/DynamicRuleMapper.java b/src/main/java/com/realtime/protection/server/rule/dynamicrule/DynamicRuleMapper.java index ff2d536..0de23f0 100644 --- a/src/main/java/com/realtime/protection/server/rule/dynamicrule/DynamicRuleMapper.java +++ b/src/main/java/com/realtime/protection/server/rule/dynamicrule/DynamicRuleMapper.java @@ -6,6 +6,7 @@ import com.realtime.protection.configuration.entity.rule.dynamicrule.DynamicRule import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -64,4 +65,12 @@ public interface DynamicRuleMapper { @Param("auditUserName")String auditUserName, @Param("auditUserId")String auditUserId, @Param("auditUserDepart")String auditUserDepart); + + void insertStatusLog(Integer id); + + void updateStatusLogExpireTime(Integer id); + + void insertStatusLogBatch(List ids); + + void updateStatusLogExpireTimeBatch(List ids); } diff --git a/src/main/java/com/realtime/protection/server/rule/dynamicrule/DynamicRuleService.java b/src/main/java/com/realtime/protection/server/rule/dynamicrule/DynamicRuleService.java index 5b4ee73..b260fa0 100644 --- a/src/main/java/com/realtime/protection/server/rule/dynamicrule/DynamicRuleService.java +++ b/src/main/java/com/realtime/protection/server/rule/dynamicrule/DynamicRuleService.java @@ -2,6 +2,7 @@ package com.realtime.protection.server.rule.dynamicrule; import com.alibaba.excel.util.ListUtils; import com.realtime.protection.configuration.entity.rule.dynamicrule.DynamicRuleObject; +import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject; import com.realtime.protection.configuration.utils.Counter; import com.realtime.protection.configuration.utils.SqlSessionWrapper; import com.realtime.protection.configuration.utils.enums.StateEnum; @@ -11,10 +12,7 @@ import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.function.Function; @Service @@ -40,7 +38,7 @@ public class DynamicRuleService { ); dynamicRuleMapper.newDynamicRule(dynamicRule); - + insertStatusLog(dynamicRule.getDynamicRuleId()); return dynamicRule.getDynamicRuleId(); } @@ -205,6 +203,8 @@ public class DynamicRuleService { } Boolean success = dynamicRuleMapper.updateAuditStatusById(id, auditStatus ,auditUserName, auditUserId, auditUserDepart); + insertStatusLog(id); + Map resultMap = new HashMap<>(); resultMap.put("success", success); resultMap.put("audit_status", auditStatus); @@ -250,10 +250,12 @@ public class DynamicRuleService { } //mapper指的就是外层函数输入的参数,也就是WhiteListMapper mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch); + insertStatusLog(idWithAuditStatusBatch); idWithAuditStatusBatch.clear(); } if (!idWithAuditStatusBatch.isEmpty()) { mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch); + insertStatusLog(idWithAuditStatusBatch); } return true; }; @@ -309,10 +311,13 @@ public class DynamicRuleService { } //mapper指的就是外层函数输入的参数,也就是WhiteListMapper mapper.updateAuditStatusWithAuditorByIdBatch(idWithAuditStatusBatch, auditUserName, auditUserId, auditUserDepart); + insertStatusLog(idWithAuditStatusBatch); + idWithAuditStatusBatch.clear(); } if (!idWithAuditStatusBatch.isEmpty()) { mapper.updateAuditStatusWithAuditorByIdBatch(idWithAuditStatusBatch, auditUserName, auditUserId, auditUserDepart); + insertStatusLog(idWithAuditStatusBatch); } return true; }; @@ -331,4 +336,30 @@ public class DynamicRuleService { public String queryAuditInfo(Integer id) { return dynamicRuleMapper.queryAuditInfo(id); } + + public void insertStatusLog(Integer id) { + + dynamicRuleMapper.updateStatusLogExpireTime(id); + dynamicRuleMapper.insertStatusLog(id); + } + public void insertStatusLog(Map idWithAuditStatusBatch) { + Set keys = idWithAuditStatusBatch.keySet(); + ArrayList ids = new ArrayList<>(keys); + + dynamicRuleMapper.updateStatusLogExpireTimeBatch(ids); + dynamicRuleMapper.insertStatusLogBatch(ids); + + } + + public void insertSStatusLog(List objects) { + List ids = new ArrayList<>(); + for (StaticRuleObject staticRuleObject : objects) { + ids.add(staticRuleObject.getStaticRuleId()); + } + + dynamicRuleMapper.updateStatusLogExpireTimeBatch(ids); + dynamicRuleMapper.insertStatusLogBatch(ids); + } + + } diff --git a/src/main/java/com/realtime/protection/server/rule/staticrule/StaticRuleController.java b/src/main/java/com/realtime/protection/server/rule/staticrule/StaticRuleController.java index 9079768..3b16615 100644 --- a/src/main/java/com/realtime/protection/server/rule/staticrule/StaticRuleController.java +++ b/src/main/java/com/realtime/protection/server/rule/staticrule/StaticRuleController.java @@ -52,6 +52,7 @@ public class StaticRuleController implements StaticRuleControllerApi { object.setStaticRuleCreateUserId(Integer.valueOf(user.uid)); object.setStaticRuleCreateDepart(user.getOrgName()); } + //调用service新增 staticRuleService.newStaticRuleObject(object); return ResponseResult.ok() diff --git a/src/main/java/com/realtime/protection/server/rule/staticrule/StaticRuleMapper.java b/src/main/java/com/realtime/protection/server/rule/staticrule/StaticRuleMapper.java index cdf7b69..c1512f0 100644 --- a/src/main/java/com/realtime/protection/server/rule/staticrule/StaticRuleMapper.java +++ b/src/main/java/com/realtime/protection/server/rule/staticrule/StaticRuleMapper.java @@ -5,7 +5,6 @@ import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; -import java.time.LocalDateTime; import java.util.List; import java.util.Map; @@ -37,7 +36,7 @@ public interface StaticRuleMapper { void deleteStaticRules(@Param("staticRuleIds") List staticRuleBatch); - void newStaticRules(List staticRuleBatch); + void newStaticRules(List list); Integer queryAuditStatusById(Integer id); @@ -69,4 +68,12 @@ public interface StaticRuleMapper { Boolean updateAuditInfo(@Param("ids") List ids, @Param("auditInfo") String auditInfo); String queryAuditInfo(Integer id); + + void insertStaticRuleStatusLog(Integer id); + + void updateStaticRuleStatusLogExpireTime(Integer id); + + void insertStaticRuleStatusLogBatch(List ids); + + void updateStaticRuleStatusLogExpireTimeBatch(List ids); } diff --git a/src/main/java/com/realtime/protection/server/rule/staticrule/StaticRuleService.java b/src/main/java/com/realtime/protection/server/rule/staticrule/StaticRuleService.java index 8aaa9c9..910225a 100644 --- a/src/main/java/com/realtime/protection/server/rule/staticrule/StaticRuleService.java +++ b/src/main/java/com/realtime/protection/server/rule/staticrule/StaticRuleService.java @@ -99,6 +99,8 @@ public class StaticRuleService { staticRuleMapper.newStaticRuleObject(object); + insertStaticRuleStatusLog(object.getStaticRuleId()); + return object.getStaticRuleId(); } @@ -210,10 +212,13 @@ public class StaticRuleService { } //mapper指的就是外层函数输入的参数,也就是WhiteListMapper mapper.newStaticRules(StaticRuleBatch); + //todo: 批量插入静态规则,mybatis未返回主键id,待解决 +// insertStaticRuleStatusLog(StaticRuleBatch); StaticRuleBatch.clear(); } if (!StaticRuleBatch.isEmpty()) { mapper.newStaticRules(StaticRuleBatch); +// insertStaticRuleStatusLog(StaticRuleBatch); } return true; }; @@ -237,7 +242,8 @@ public class StaticRuleService { throw new IllegalArgumentException("invalid audit status"); } Boolean success = staticRuleMapper.updateAuditStatusById(id, auditStatus, auditUserName, auditUserId, auditUserDepart); - + //记录状态历史 + insertStaticRuleStatusLog(id); Map resultMap = new HashMap<>(); resultMap.put("success", success); resultMap.put("audit_status", auditStatus); @@ -309,11 +315,13 @@ public class StaticRuleService { //mapper指的就是外层函数输入的参数,也就是WhiteListMapper mapper.updateAuditStatusWithAuditorByIdBatch(idWithAuditStatusBatch, auditUserName, auditUserId, auditUserDepart); + insertStaticRuleStatusLog(idWithAuditStatusBatch); idWithAuditStatusBatch.clear(); } if (!idWithAuditStatusBatch.isEmpty()) { mapper.updateAuditStatusWithAuditorByIdBatch(idWithAuditStatusBatch, auditUserName, auditUserId, auditUserDepart); + insertStaticRuleStatusLog(idWithAuditStatusBatch); } return true; }; @@ -358,10 +366,12 @@ public class StaticRuleService { } //mapper指的就是外层函数输入的参数,也就是WhiteListMapper mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch); + insertStaticRuleStatusLog(idWithAuditStatusBatch); idWithAuditStatusBatch.clear(); } if (!idWithAuditStatusBatch.isEmpty()) { mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch); + insertStaticRuleStatusLog(idWithAuditStatusBatch); } return true; }; @@ -388,4 +398,26 @@ public class StaticRuleService { public String queryAuditInfo(Integer id) { return staticRuleMapper.queryAuditInfo(id); } + + public void insertStaticRuleStatusLog(Integer id) { + staticRuleMapper.updateStaticRuleStatusLogExpireTime(id); + staticRuleMapper.insertStaticRuleStatusLog(id); + } + public void insertStaticRuleStatusLog(Map idWithAuditStatusBatch) { + Set keys = idWithAuditStatusBatch.keySet(); + ArrayList ids = new ArrayList<>(keys); + + staticRuleMapper.updateStaticRuleStatusLogExpireTimeBatch(ids); + staticRuleMapper.insertStaticRuleStatusLogBatch(ids); + } + + public void insertStaticRuleStatusLog(List objects) { + List ids = new ArrayList<>(); + for (StaticRuleObject staticRuleObject : objects) { + ids.add(staticRuleObject.getStaticRuleId()); + } + + staticRuleMapper.updateStaticRuleStatusLogExpireTimeBatch(ids); + staticRuleMapper.insertStaticRuleStatusLogBatch(ids); + } } diff --git a/src/main/java/com/realtime/protection/server/task/TaskMapper.java b/src/main/java/com/realtime/protection/server/task/TaskMapper.java index a13b0ce..18399f7 100644 --- a/src/main/java/com/realtime/protection/server/task/TaskMapper.java +++ b/src/main/java/com/realtime/protection/server/task/TaskMapper.java @@ -104,4 +104,12 @@ public interface TaskMapper { void deleteTaskProtectObjectConcat(Long taskId); List queryProtectObjectsByTaskId(Long id); + + void insertTaskStatusLog(Long taskId); + + void updateTaskStatusLogExpireTime(Long taskId); + + void insertTaskStatusLogBatch(List idWithAuditStatusBatch); + + void updateTaskStatusLogExpireTimeBatch(List taskIds); } diff --git a/src/main/java/com/realtime/protection/server/task/TaskService.java b/src/main/java/com/realtime/protection/server/task/TaskService.java index 89a2548..da093c3 100644 --- a/src/main/java/com/realtime/protection/server/task/TaskService.java +++ b/src/main/java/com/realtime/protection/server/task/TaskService.java @@ -101,7 +101,7 @@ public class TaskService { } } - + insertTaskStatusLog(task.getTaskId()); return task.getTaskId(); } @@ -316,7 +316,7 @@ public class TaskService { if (AuditStatusValidator.setOriginal(originalAuditStatus).checkValidate(taskAuditStatus)) taskMapper.changeTaskAuditStatusWithAudior(taskId, taskAuditStatus, auditUserName, auditUserId, auditUserDepart); else return false; - + insertTaskStatusLog(taskId); return true; } @Transactional @@ -329,6 +329,7 @@ public class TaskService { if (AuditStatusValidator.setOriginal(originalAuditStatus).checkValidate(taskAuditStatus)) taskMapper.changeTaskAuditStatus(taskId, taskAuditStatus); else return false; + insertTaskStatusLog(taskId); return true; } @@ -427,10 +428,13 @@ public class TaskService { } //mapper指的就是外层函数输入的参数,也就是WhiteListMapper mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch); + //记录状态日志 + insertTaskStatusLog(idWithAuditStatusBatch); idWithAuditStatusBatch.clear(); } if (!idWithAuditStatusBatch.isEmpty()) { mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch); + insertTaskStatusLog(idWithAuditStatusBatch); } return true; }; @@ -507,4 +511,16 @@ public class TaskService { public String queryAuditInfo(Integer id) { return taskMapper.queryAuditInfo(id); } + + public void insertTaskStatusLog(Long taskId) { + taskMapper.updateTaskStatusLogExpireTime(taskId); + taskMapper.insertTaskStatusLog(taskId); + } + public void insertTaskStatusLog(Map idWithAuditStatusBatch) { + Set keys = idWithAuditStatusBatch.keySet(); + ArrayList taskIds = new ArrayList<>(keys); + + taskMapper.updateTaskStatusLogExpireTimeBatch(taskIds); + taskMapper.insertTaskStatusLogBatch(taskIds); + } } diff --git a/src/main/java/com/realtime/protection/server/task/status/StateChangeService.java b/src/main/java/com/realtime/protection/server/task/status/StateChangeService.java index c83774a..f8ebefe 100644 --- a/src/main/java/com/realtime/protection/server/task/status/StateChangeService.java +++ b/src/main/java/com/realtime/protection/server/task/status/StateChangeService.java @@ -70,8 +70,15 @@ public class StateChangeService { originalState.getClass().getSimpleName(), newState.getClass().getSimpleName())); + // 这里一定是handle成功的状态,我们再进行task status的修改,如果handle失败,要么返回false,要么抛出异常,不会进入此处 - return taskService.changeTaskStatus(taskId, stateNum); + Boolean isChanged = taskService.changeTaskStatus(taskId, stateNum); + //记录状态日志 + if (isChanged) { + taskService.insertTaskStatusLog(taskId); + } + + return isChanged; } private Boolean checkState(State originalState, State newState) { diff --git a/src/main/java/com/realtime/protection/server/whitelist/WhiteListMapper.java b/src/main/java/com/realtime/protection/server/whitelist/WhiteListMapper.java index 00b0dd2..249ada7 100644 --- a/src/main/java/com/realtime/protection/server/whitelist/WhiteListMapper.java +++ b/src/main/java/com/realtime/protection/server/whitelist/WhiteListMapper.java @@ -8,6 +8,7 @@ import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -59,4 +60,12 @@ public interface WhiteListMapper { Boolean updateAuditInfo(@Param("ids")List ids, @Param("auditInfo")String auditInfo); String queryWhiteListObjectAuditInfo(Integer id); + + void updateStatusLogExpireTime(Integer id); + + void insertStatusLog(Integer id); + + void updateStatusLogExpireTimeBatch(List ids); + + void insertStatusLogBatch(List ids); } diff --git a/src/main/java/com/realtime/protection/server/whitelist/WhiteListService.java b/src/main/java/com/realtime/protection/server/whitelist/WhiteListService.java index dc7ca00..57b9883 100644 --- a/src/main/java/com/realtime/protection/server/whitelist/WhiteListService.java +++ b/src/main/java/com/realtime/protection/server/whitelist/WhiteListService.java @@ -14,10 +14,7 @@ import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.function.Function; @Service @@ -50,6 +47,7 @@ public class WhiteListService { ); whiteListMapper.newWhiteListObject(object); + insertStatusLog(object.getWhiteListId()); return object.getWhiteListId(); } @@ -200,7 +198,7 @@ public class WhiteListService { } Boolean success = whiteListMapper.updateWhiteListObjectAuditStatus(id, auditStatus); - + insertStatusLog(id); Map resultMap = new HashMap<>(); resultMap.put("success", success); resultMap.put("audit_status", auditStatus); @@ -253,10 +251,12 @@ public class WhiteListService { } //mapper指的就是外层函数输入的参数,也就是WhiteListMapper mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch); + insertStatusLog(idWithAuditStatusBatch); idWithAuditStatusBatch.clear(); } if (!idWithAuditStatusBatch.isEmpty()) { mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch); + insertStatusLog(idWithAuditStatusBatch); } return true; }; @@ -272,4 +272,26 @@ public class WhiteListService { public String queryAuditInfo(Integer id) { return whiteListMapper.queryWhiteListObjectAuditInfo(id); } + + public void insertStatusLog(Integer id) { + whiteListMapper.updateStatusLogExpireTime(id); + whiteListMapper.insertStatusLog(id); + } + public void insertStatusLog(Map idWithAuditStatusBatch) { + Set keys = idWithAuditStatusBatch.keySet(); + List ids = new ArrayList<>(keys); + + whiteListMapper.updateStatusLogExpireTimeBatch(ids); + whiteListMapper.insertStatusLogBatch(ids); + } + + public void insertStatusLog(List objects) { + List ids = new ArrayList<>(); + for (StaticRuleObject staticRuleObject : objects) { + ids.add(staticRuleObject.getStaticRuleId()); + } + + whiteListMapper.updateStatusLogExpireTimeBatch(ids); + whiteListMapper.insertStatusLogBatch(ids); + } } diff --git a/src/main/resources/mappers/DynamicRuleMapper.xml b/src/main/resources/mappers/DynamicRuleMapper.xml index ae37f33..4f9908b 100644 --- a/src/main/resources/mappers/DynamicRuleMapper.xml +++ b/src/main/resources/mappers/DynamicRuleMapper.xml @@ -41,7 +41,97 @@ ) - + + insert into t_dynamic_rule_status_log + ( + dynamic_rule_id, + effective_time,expire_time, + dynamic_rule_used_task_id, + dynamic_rule_name, + create_time, + modify_time, + dynamic_rule_create_username, + dynamic_rule_create_depart, + dynamic_rule_create_user_id, + audit_status, + dynamic_rule_display_id, + dynamic_rule_audit_info, + audit_user_name, + audit_user_id, + audit_user_depart, + bw_sql, + description, + source_system + ) + select + dynamic_rule_id, + NOW(), NULL, + dynamic_rule_used_task_id, + dynamic_rule_name, + create_time, + modify_time, + dynamic_rule_create_username, + dynamic_rule_create_depart, + dynamic_rule_create_user_id, + audit_status, + dynamic_rule_display_id, + dynamic_rule_audit_info, + audit_user_name, + audit_user_id, + audit_user_depart, + bw_sql, + description, + source_system + from t_dynamic_rule + where dynamic_rule_id = #{id} + + + insert into t_dynamic_rule_status_log + ( + dynamic_rule_id, + effective_time,expire_time, + dynamic_rule_used_task_id, + dynamic_rule_name, + create_time, + modify_time, + dynamic_rule_create_username, + dynamic_rule_create_depart, + dynamic_rule_create_user_id, + audit_status, + dynamic_rule_display_id, + dynamic_rule_audit_info, + audit_user_name, + audit_user_id, + audit_user_depart, + bw_sql, + description, + source_system + ) + select + dynamic_rule_id, + NOW() NULL, + dynamic_rule_used_task_id, + dynamic_rule_name, + create_time, + modify_time, + dynamic_rule_create_username, + dynamic_rule_create_depart, + dynamic_rule_create_user_id, + audit_status, + dynamic_rule_display_id, + dynamic_rule_audit_info, + audit_user_name, + audit_user_id, + audit_user_depart, + bw_sql, + description, + source_system + from t_dynamic_rule + where dynamic_rule_id in + + #{id} + + @@ -129,6 +219,20 @@ #{id} + + update t_dynamic_rule_status_log + set expire_time = NOW() + where dynamic_rule_id = #{id} and expire_time is null + + + update t_dynamic_rule_status_log + set expire_time = NOW() + where dynamic_rule_id in + + #{id} + + and expire_time is null + + + INSERT INTO t_protect_object_status_log( + protect_object_id, + effective_time,expire_time, + protect_object_name, + protect_object_system_name, + protect_object_ip, + protect_object_port, + protect_object_url, + protect_object_protocol, + protect_object_audit_status, + protect_object_create_username, + protect_object_create_depart, + protect_object_create_user_id, + create_time, + modify_time, + protect_object_display_id, + protect_object_audit_info, + audit_user_name, + audit_user_id, + audit_user_deaprt + ) + SELECT + protect_object_id, + NOW(), NULL, + protect_object_name, + protect_object_system_name, + protect_object_ip, + protect_object_port, + protect_object_url, + protect_object_protocol, + protect_object_audit_status, + protect_object_create_username, + protect_object_create_depart, + protect_object_create_user_id, + create_time, + modify_time, + protect_object_display_id, + protect_object_audit_info, + audit_user_name, + audit_user_id, + audit_user_deaprt + FROM t_protect_object + WHERE protect_object_id = #{id} + + + INSERT INTO t_protect_object_status_log( + protect_object_id, + effective_time,expire_time, + protect_object_name, + protect_object_system_name, + protect_object_ip, + protect_object_port, + protect_object_url, + protect_object_protocol, + protect_object_audit_status, + protect_object_create_username, + protect_object_create_depart, + protect_object_create_user_id, + create_time, + modify_time, + protect_object_display_id, + protect_object_audit_info, + audit_user_name, + audit_user_id, + audit_user_deaprt + ) + SELECT + protect_object_id, + NOW(), NULL, + protect_object_name, + protect_object_system_name, + protect_object_ip, + protect_object_port, + protect_object_url, + protect_object_protocol, + protect_object_audit_status, + protect_object_create_username, + protect_object_create_depart, + protect_object_create_user_id, + create_time, + modify_time, + protect_object_display_id, + protect_object_audit_info, + audit_user_name, + audit_user_id, + audit_user_deaprt + FROM t_protect_object + WHERE protect_object_id IN + + #{id} + + @@ -215,4 +308,18 @@ #{id} + + UPDATE t_protect_object_status_log + SET expire_time = NOW() + WHERE protect_object_id = #{id} AND expire_time IS NULL + + + UPDATE t_protect_object_status_log + SET expire_time = NOW() + WHERE protect_object_id IN + + #{id} + + AND expire_time IS NULL + \ No newline at end of file diff --git a/src/main/resources/mappers/StaticRuleMapper.xml b/src/main/resources/mappers/StaticRuleMapper.xml index c3dad04..4ee627a 100644 --- a/src/main/resources/mappers/StaticRuleMapper.xml +++ b/src/main/resources/mappers/StaticRuleMapper.xml @@ -25,7 +25,8 @@ #{object.auditStatus}, #{object.staticRuleDisplayId},#{object.eventType}) - + insert into t_static_rule(static_rule_name, static_rule_create_time, static_rule_create_username, static_rule_create_depart, static_rule_create_user_id, static_rule_sip, static_rule_msip, @@ -37,7 +38,7 @@ static_rule_display_id,event_type ) values - + (#{object.staticRuleName}, NOW(), #{object.staticRuleCreateUsername}, #{object.staticRuleCreateDepart}, #{object.staticRuleCreateUserId}, INET_ATON(#{object.staticRuleSip}), INET_ATON(#{object.staticRuleMsip}), #{object.staticRuleSport}, #{object.staticRuleMsport}, @@ -49,6 +50,143 @@ ) + + insert into t_static_rule_status_log( + static_rule_id, + static_rule_name, + effective_time,expire_time, + static_rule_create_time, + static_rule_create_username, + static_rule_audit_status, + static_rule_create_depart, + static_rule_create_user_id, + audit_user_name, + audit_user_id, + audit_user_depart, + static_rule_used_task_id, + static_rule_sip, + static_rule_msip, + static_rule_sport, + static_rule_msport, + static_rule_dip, + static_rule_mdip, + static_rule_dport, + static_rule_mdport, + static_rule_protocol, + static_rule_mprotocol, + static_rule_dns, + static_rule_url, + static_rule_priority, + static_rule_frequency, + static_rule_modify_time, + static_rule_display_id, + static_rule_audit_info, + event_type + ) + select + static_rule_id, + static_rule_name, + NOW(),NULL, + static_rule_create_time, + static_rule_create_username, + static_rule_audit_status, + static_rule_create_depart, + static_rule_create_user_id, + audit_user_name, + audit_user_id, + audit_user_depart, + static_rule_used_task_id, + static_rule_sip, + static_rule_msip, + static_rule_sport, + static_rule_msport, + static_rule_dip, + static_rule_mdip, + static_rule_dport, + static_rule_mdport, + static_rule_protocol, + static_rule_mprotocol, + static_rule_dns, + static_rule_url, + static_rule_priority, + static_rule_frequency, + static_rule_modify_time, + static_rule_display_id, + static_rule_audit_info, + event_type + from t_static_rule + where static_rule_id = #{id} + + + insert into t_static_rule_status_log( + static_rule_id, + static_rule_name, + effective_time,expire_time, + static_rule_create_time, + static_rule_create_username, + static_rule_audit_status, + static_rule_create_depart, + static_rule_create_user_id, + audit_user_name, + audit_user_id, + audit_user_depart, + static_rule_used_task_id, + static_rule_sip, + static_rule_msip, + static_rule_sport, + static_rule_msport, + static_rule_dip, + static_rule_mdip, + static_rule_dport, + static_rule_mdport, + static_rule_protocol, + static_rule_mprotocol, + static_rule_dns, + static_rule_url, + static_rule_priority, + static_rule_frequency, + static_rule_modify_time, + static_rule_display_id, + static_rule_audit_info, + event_type + ) + select + static_rule_id, + static_rule_name, + NOW(),NULL, + static_rule_create_time, + static_rule_create_username, + static_rule_audit_status, + static_rule_create_depart, + static_rule_create_user_id, + audit_user_name, + audit_user_id, + audit_user_depart, + static_rule_used_task_id, + static_rule_sip, + static_rule_msip, + static_rule_sport, + static_rule_msport, + static_rule_dip, + static_rule_mdip, + static_rule_dport, + static_rule_mdport, + static_rule_protocol, + static_rule_mprotocol, + static_rule_dns, + static_rule_url, + static_rule_priority, + static_rule_frequency, + static_rule_modify_time, + static_rule_display_id, + static_rule_audit_info, + event_type + from t_static_rule + where static_rule_id in + + #{id} + + @@ -138,6 +276,20 @@ #{id} + + update t_static_rule_status_log + set expire_time = NOW() + where static_rule_id = #{id} AND expire_time IS NULL + + + update t_static_rule_status_log + set expire_time = NOW() + where static_rule_id in + + #{id} + + AND expire_time IS NULL + delete from t_static_rule diff --git a/src/main/resources/mappers/TaskMapper.xml b/src/main/resources/mappers/TaskMapper.xml index f64292e..d4dc455 100644 --- a/src/main/resources/mappers/TaskMapper.xml +++ b/src/main/resources/mappers/TaskMapper.xml @@ -60,6 +60,13 @@ + + + + + + + @@ -76,15 +83,11 @@ SELECT t_task.task_name, @@ -422,13 +552,13 @@ select CONCAT(INET_NTOA(white_list_ip)," ", CAST(white_list_port)," ", white_list_url) diff --git a/src/test/java/com/realtime/protection/server/alertmessage/AlertMessageTest.java b/src/test/java/com/realtime/protection/server/alertmessage/AlertMessageTest.java index d459434..4593e12 100644 --- a/src/test/java/com/realtime/protection/server/alertmessage/AlertMessageTest.java +++ b/src/test/java/com/realtime/protection/server/alertmessage/AlertMessageTest.java @@ -58,7 +58,7 @@ public class AlertMessageTest { null, null, null, null, null, null, null, 1, 1); DynamicRuleObject object = new DynamicRuleObject(); - object.setDynamicRuleName("测试动态任务规则"+n); + object.setDynamicRuleName("测试display_id"+n); object.setDescription("动态规则新建测试"); object.setDynamicRuleSourceSystem("BW"); object.setBwSql("select * from test where event_id = 1"); diff --git a/src/test/java/com/realtime/protection/server/rule/staticrule/StaticRuleServiceTest.java b/src/test/java/com/realtime/protection/server/rule/staticrule/StaticRuleServiceTest.java index 6fc962e..4ac5efe 100644 --- a/src/test/java/com/realtime/protection/server/rule/staticrule/StaticRuleServiceTest.java +++ b/src/test/java/com/realtime/protection/server/rule/staticrule/StaticRuleServiceTest.java @@ -26,7 +26,7 @@ public class StaticRuleServiceTest extends ProtectionApplicationTests { @BeforeEach void setUp() { staticRuleTest = new StaticRuleObject(); - staticRuleTest.setStaticRuleName("test_staticrule"); + staticRuleTest.setStaticRuleName("test_stasticrule"); staticRuleTest.setStaticRuleCreateUsername("mh"); staticRuleTest.setStaticRuleCreateDepart("mmeess"); staticRuleTest.setStaticRuleCreateUserId(2); @@ -64,11 +64,20 @@ public class StaticRuleServiceTest extends ProtectionApplicationTests { @Test void testNewStaticRules(){ List staticRuleObjects = new ArrayList<>(); - for (int i = 0; i < 2; i++) { + for (int i = 1; i < 10; i++) { + staticRuleTest = new StaticRuleObject(); + staticRuleTest.setStaticRuleName("test_staticrule"+i); + staticRuleTest.setStaticRuleCreateUsername("NSADD管理员"); + staticRuleTest.setStaticRuleCreateDepart("组织树"); + staticRuleTest.setStaticRuleCreateUserId(22222222); + staticRuleTest.setAuditStatus(0); + + staticRuleTest.setStaticRuleSip("32.2.3."+i); + staticRuleTest.setStaticRuleDip(i+".2.3.2"); staticRuleObjects.add(staticRuleTest); } -// Boolean success = staticRuleService.newStaticRuleObjects(staticRuleObjects); -// assertTrue(success); + Boolean success = staticRuleService.newStaticRuleObjects(staticRuleObjects); + assertTrue(success); } @Test @@ -116,13 +125,13 @@ public class StaticRuleServiceTest extends ProtectionApplicationTests { } @Test void testUpdateStaticRuleAuditStatusBatch(){ -// Map map = new HashMap<>(); -// map.put(1300, 0); -// map.put(1326, 1); -// map.put(1328, 1); -// -// -// System.out.println(staticRuleService.updateAuditStatusBatch(map)); + Map map = new HashMap<>(); + map.put(1398, 2); + map.put(1399, 2); + map.put(1400, 2); + + + System.out.println(staticRuleService.updateAuditStatusBatch(map)); } @Test void testQueryAuditStatusByIds(){ diff --git a/src/test/java/com/realtime/protection/server/task/TaskServiceTest.java b/src/test/java/com/realtime/protection/server/task/TaskServiceTest.java index dbf3e39..e9ac282 100644 --- a/src/test/java/com/realtime/protection/server/task/TaskServiceTest.java +++ b/src/test/java/com/realtime/protection/server/task/TaskServiceTest.java @@ -145,8 +145,10 @@ class TaskServiceTest extends ProtectionApplicationTests { // } // // - List tasks = taskService.queryTasks(null, null, null, null, null, null, null, null, null,null,null,null,null,1, 10); - assertEquals(10, tasks.size()); + List tasks = taskService.queryTasks(null, null, null, null, null, null, null, null, "eee",null,null,null,null,1, 10); + assertEquals(1, tasks.size()); + Integer tasksnum = taskService.queryTaskTotalNum(null, null, "", "", null, "", "", "", "上述",null,null,null,null); + System.out.println(tasksnum); // // tasks = taskService.queryTasks(0, null, null, null, null,null, null, null, null, 1, 10); // assertEquals(10, tasks.size());