From c6334cee567cd85cb4afeb6ae8e54e720c7fa3e2 Mon Sep 17 00:00:00 2001 From: PushM <584406942@qq.com> Date: Wed, 17 Apr 2024 22:04:29 +0800 Subject: [PATCH 1/2] =?UTF-8?q?1=E3=80=81TaskCommandIndo=E5=88=A0=E5=8E=BB?= =?UTF-8?q?=E4=BA=86depart=EF=BC=8C=E4=BD=BF=E7=94=A8taskCreateDepart?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9mapper=E4=B9=9F=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8taskCreateDepart=202=E3=80=81=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E9=9D=99=E6=80=81=E8=A7=84=E5=88=99=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E5=AE=A1=E6=A0=B8=E5=8A=9F=E8=83=BD=EF=BC=8Cpost=E8=AF=B7?= =?UTF-8?q?=E6=B1=82body=E4=B8=BAid=E5=92=8CauditStatus=E7=9A=84Map?= =?UTF-8?q?=EF=BC=8C=E5=85=A5=E5=BA=93=E4=BD=BF=E7=94=A8sqlSessionWrapper?= =?UTF-8?q?=EF=BC=8Ctest=E5=B7=B2=E9=80=9A=E8=BF=87=203=E3=80=81=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E4=BB=BB=E5=8A=A1=E6=8E=A5=E6=94=B6alertmessage?= =?UTF-8?q?=EF=BC=8C=E6=9F=A5=E8=AF=A2DynamicTaskInfos=E7=94=9F=E6=88=90co?= =?UTF-8?q?mmand=E6=97=B6=EF=BC=8C=E6=96=B0=E5=A2=9E=E5=85=B3=E8=81=94t=5F?= =?UTF-8?q?strategy=5Ftemplate=E8=A1=A8=EF=BC=8Cselect=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=9F=A5=E8=AF=A2=E5=B1=80=E7=82=B9dynamic?= =?UTF-8?q?=5Frule=5Frange=E3=80=81=E4=BA=8B=E4=BB=B6=E7=B1=BB=E5=9E=8Bstr?= =?UTF-8?q?ategy=5Ftemplate=5Fname=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entity/task/TaskCommandInfo.java | 3 +- .../alertmessage/AlertMessageService.java | 2 +- .../rule/staticrule/StaticRuleController.java | 29 ++++++++++++++++++- .../rule/staticrule/StaticRuleMapper.java | 3 ++ .../rule/staticrule/StaticRuleService.java | 29 ++++++++++++++++++- .../resources/mappers/AlertMessageMapper.xml | 11 ++++++- src/main/resources/mappers/CommandMapper.xml | 2 +- .../resources/mappers/StaticRuleMapper.xml | 14 +++++++++ src/main/resources/mappers/TaskMapper.xml | 2 +- .../server/alertmessage/AlertMessageTest.java | 3 +- .../rule/dynamic/DynamicRuleServiceTest.java | 2 +- .../staticrule/StaticRuleServiceTest.java | 10 +++++++ 12 files changed, 101 insertions(+), 9 deletions(-) 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 1218dbc..d6b060e 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 @@ -50,7 +50,7 @@ public class TaskCommandInfo { @Schema(description = "局点", example = "123456") @JsonProperty("distribute_point") - private Integer distributePoint; + private String distributePoint; @Schema(description = "事件类型(策略模板名称)", example = "DDOS") @JsonProperty("event_type") @@ -123,6 +123,7 @@ public class TaskCommandInfo { this.taskType = original.taskType; this.taskAct = original.taskAct; this.distributePoint = original.distributePoint; + this.eventType = original.eventType; this.frequency = original.frequency; this.startTime = original.startTime; this.endTime = original.endTime; 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 c93897d..bf6455b 100644 --- a/src/main/java/com/realtime/protection/server/alertmessage/AlertMessageService.java +++ b/src/main/java/com/realtime/protection/server/alertmessage/AlertMessageService.java @@ -32,7 +32,7 @@ public class AlertMessageService { public void processAlertMessage(AlertMessage alertMessage) { //根据告警信息——>生成指令 List dynamicTaskCommandInfoList = generateDynamicCommand(alertMessage); - //获取任务状态,设置指令的isValid字段,且是否生成指令入库(除了RUNING\PAUSED状态,其他都不入库)。 + //获取任务状态,设置指令的isValid字段,且是否生成指令入库(除了RUNING\PAUSED状态,其他都不入command库)。 Integer taskStatus = dynamicTaskCommandInfoList.get(0).getTaskStatus(); //获取任务类型,设置指令的isJudged字段。 Integer taskType = dynamicTaskCommandInfoList.get(0).getTaskType(); 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 9d6ff73..838a69e 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 @@ -14,7 +14,9 @@ import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.List; +import java.util.Map; @RestController @@ -163,7 +165,7 @@ public class StaticRuleController implements StaticRuleControllerApi { public ResponseResult updateStaticRuleAuditStatus(@PathVariable Integer id, @PathVariable Integer auditStatus) { if (id <= 0 || auditStatus < 0 || auditStatus > 2) { return new ResponseResult(400, "id or status is invalid") - .setData("whiteobj_id", id) + .setData("staticRule_id", id) .setData("success", false); } return ResponseResult.ok() @@ -173,4 +175,29 @@ public class StaticRuleController implements StaticRuleControllerApi { } + /** + * 批量修改审核状态 + */ + @PostMapping("/auditbatch") + public ResponseResult updateStaticRuleAuditStatusBatch(@RequestBody Map idsWithAuditStatusMap) { + List errorIds = new ArrayList(); + for (Map.Entry entry: idsWithAuditStatusMap.entrySet()) { + Integer id = entry.getKey(); + Integer auditStatus = entry.getValue(); + if (id <= 0 || auditStatus < 0 || auditStatus > 2) { + errorIds.add(id); + } + } + if (!errorIds.isEmpty()){ + return new ResponseResult(400, "id or status is invalid") + .setData("staticRule_id", errorIds) + .setData("success", false); + } + + return ResponseResult.ok() + .setData("success", staticRuleService.updateAuditStatusBatch(idsWithAuditStatusMap)); + } + + + } 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 d0d71c0..8cdc9e1 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 @@ -6,6 +6,7 @@ import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.util.List; +import java.util.Map; @Mapper public interface StaticRuleMapper { @@ -43,4 +44,6 @@ public interface StaticRuleMapper { String static_rule_create_username, String ip); List queryStaticRuleByIds(List ids); + + void updateAuditStatusByIdBatch(@Param("idWithAuditStatusBatch")Map idWithAuditStatusBatch); } 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 1e42c9c..17408d4 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 @@ -131,7 +131,7 @@ public class StaticRuleService { } List StaticRuleBatch = ListUtils.newArrayListWithExpectedSize(100); - for (StaticRuleObject staticRule : staticRuleList) { + for (StaticRuleObject staticRule : list) { staticRule.setStaticRuleCreateTime(LocalDateTime.now()); StaticRuleBatch.add(staticRule); if (StaticRuleBatch.size() < 100) { @@ -182,4 +182,31 @@ public class StaticRuleService { public List queryStaticRuleByIds(List ids) { return staticRuleMapper.queryStaticRuleByIds(ids); } + + public Object updateAuditStatusBatch(Map idsWithAuditStatusMap) { + + Function, Boolean>> updateStaticRuleAuditStatusFunction = + mapper -> map -> { + if (map == null || map.isEmpty()) { + return false; + } + + Map idWithAuditStatusBatch = new HashMap<>(); + for (Map.Entry item : map.entrySet()) { + idWithAuditStatusBatch.put(item.getKey(), item.getValue()); + if (idWithAuditStatusBatch.size() < 100) { + continue; + } + //mapper指的就是外层函数输入的参数,也就是WhiteListMapper + mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch); + idWithAuditStatusBatch.clear(); + } + if (!idWithAuditStatusBatch.isEmpty()) { + mapper.updateAuditStatusByIdBatch(idWithAuditStatusBatch); + } + return true; + }; + //实现事务操作 + return sqlSessionWrapper.startBatchSession(StaticRuleMapper.class, updateStaticRuleAuditStatusFunction, idsWithAuditStatusMap); + } } diff --git a/src/main/resources/mappers/AlertMessageMapper.xml b/src/main/resources/mappers/AlertMessageMapper.xml index 39f68d3..e4376d2 100644 --- a/src/main/resources/mappers/AlertMessageMapper.xml +++ b/src/main/resources/mappers/AlertMessageMapper.xml @@ -20,6 +20,10 @@ + + + + @@ -149,12 +153,17 @@ t_task.task_end_time, t_dr.template_id, + t_dr.dynamic_rule_range, t_dr.dynamic_rule_protect_level, - t_task.task_status + t_task.task_status, + + t_tmplate.strategy_template_name from t_task left join realtime_protection.t_dynamic_rule t_dr on (t_task.task_id = t_dr.dynamic_rule_used_task_id and t_dr.dynamic_rule_id = #{dynamicRuleId}) + left join realtime_protection.t_strategy_template t_tmplate on + ( t_dr.template_id = t_tmplate.strategy_template_id ) where t_task.task_id = #{taskId} diff --git a/src/main/resources/mappers/CommandMapper.xml b/src/main/resources/mappers/CommandMapper.xml index 092d9fe..d65fa20 100644 --- a/src/main/resources/mappers/CommandMapper.xml +++ b/src/main/resources/mappers/CommandMapper.xml @@ -9,7 +9,7 @@ MASK_SRC_IP, MASK_SRC_PORT, MASK_DST_IP, MASK_DST_PORT, MASK_PROTOCOL, VALID_TIME, INVALID_TIME, IS_VALID, IS_JUDGED, SEND_TIMES, SUCCESS_TIMES, CREATE_TIME, LAST_UPDATE, IS_DELETED) - values (#{info.UUID}, #{info.taskId}, #{info.taskAct}, #{info.taskName}, #{info.eventType}, #{info.department}, #{info.distributePoint}, + values (#{info.UUID}, #{info.taskId}, #{info.taskAct}, #{info.taskName}, #{info.eventType}, #{info.taskCreateDepart}, #{info.distributePoint}, #{info.frequency}, DEFAULT, #{info.fiveTupleWithMask.sourceIP}, #{info.fiveTupleWithMask.sourcePort}, diff --git a/src/main/resources/mappers/StaticRuleMapper.xml b/src/main/resources/mappers/StaticRuleMapper.xml index 4fe88cd..2025944 100644 --- a/src/main/resources/mappers/StaticRuleMapper.xml +++ b/src/main/resources/mappers/StaticRuleMapper.xml @@ -84,6 +84,20 @@ set static_rule_audit_status = #{auditStatus} where static_rule_id = #{id} + + update t_static_rule + set static_rule_audit_status = CASE static_rule_id + + WHEN #{id} THEN #{auditStatus} + + ELSE static_rule_audit_status + END + WHERE static_rule_id IN + + #{id} + + + delete from t_static_rule where static_rule_id in diff --git a/src/main/resources/mappers/TaskMapper.xml b/src/main/resources/mappers/TaskMapper.xml index 9b23f76..3daee2e 100644 --- a/src/main/resources/mappers/TaskMapper.xml +++ b/src/main/resources/mappers/TaskMapper.xml @@ -161,7 +161,7 @@ - + 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 56ae63b..b5f4870 100644 --- a/src/test/java/com/realtime/protection/server/alertmessage/AlertMessageTest.java +++ b/src/test/java/com/realtime/protection/server/alertmessage/AlertMessageTest.java @@ -57,6 +57,7 @@ public class AlertMessageTest { object.setDynamicRulePriority(1); object.setDynamicRuleRange("北京"); object.setDynamicRuleProtectLevel(1); + object.setLogRuleId(1L); object.setTemplateId(templates.get(0).getTemplateId()); object.setProtectObjectIds(List.of(new Integer[]{protectObject.get(0).getProtectObjectId()})); Integer dynamicRuleId = dynamicRuleService.newDynamicRuleObject(object); @@ -81,7 +82,7 @@ public class AlertMessageTest { //启动任务 stateChangeService.changeState(2, taskId, false); - for (int i = 0 ; i< 10; i++) { + for (int i = 0 ; i< 5; i++) { AlertMessage alert = new AlertMessage(); FiveTupleWithMask fiveTupleWithMask = new FiveTupleWithMask(); fiveTupleWithMask.setSourceIP("111.1.1." + i); diff --git a/src/test/java/com/realtime/protection/server/rule/dynamic/DynamicRuleServiceTest.java b/src/test/java/com/realtime/protection/server/rule/dynamic/DynamicRuleServiceTest.java index 7b9138c..c48bd61 100644 --- a/src/test/java/com/realtime/protection/server/rule/dynamic/DynamicRuleServiceTest.java +++ b/src/test/java/com/realtime/protection/server/rule/dynamic/DynamicRuleServiceTest.java @@ -49,7 +49,7 @@ public class DynamicRuleServiceTest extends ProtectionApplicationTests { object.setDynamicRuleRange("北京"); object.setDynamicRuleProtectLevel(2); object.setTemplateId(templates.get(0).getTemplateId()); - object.setLogRuleId(1); + object.setLogRuleId(1L); object.setProtectObjectIds(List.of(new Integer[]{protectObject.get(0).getProtectObjectId()})); Integer dynamicRuleId = dynamicRuleService.newDynamicRuleObject(object); } 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 c829b99..ec25643 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 @@ -8,7 +8,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -101,7 +103,15 @@ public class StaticRuleServiceTest extends ProtectionApplicationTests { List staticRuleObjects = staticRuleService.queryStaticRule(null, null, null, null, 1, 10); System.out.println(staticRuleObjects); } + @Test + void testUpdateStaticRuleAuditStatusBatch(){ + Map map = new HashMap<>(); + map.put(1299, 0); + map.put(1300, 1); + map.put(1301, 1); + System.out.println(staticRuleService.updateAuditStatusBatch(map)); + } } From da560584698c2b0860bb259163cd44d8229f48af Mon Sep 17 00:00:00 2001 From: PushM <584406942@qq.com> Date: Wed, 17 Apr 2024 22:05:32 +0800 Subject: [PATCH 2/2] =?UTF-8?q?1=E3=80=81TaskCommandIndo=E5=88=A0=E5=8E=BB?= =?UTF-8?q?=E4=BA=86depart=EF=BC=8C=E4=BD=BF=E7=94=A8taskCreateDepart?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9mapper=E4=B9=9F=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8taskCreateDepart=202=E3=80=81=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E9=9D=99=E6=80=81=E8=A7=84=E5=88=99=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E5=AE=A1=E6=A0=B8=E5=8A=9F=E8=83=BD=EF=BC=8Cpost=E8=AF=B7?= =?UTF-8?q?=E6=B1=82body=E4=B8=BAid=E5=92=8CauditStatus=E7=9A=84Map?= =?UTF-8?q?=EF=BC=8C=E5=85=A5=E5=BA=93=E4=BD=BF=E7=94=A8sqlSessionWrapper?= =?UTF-8?q?=EF=BC=8Ctest=E5=B7=B2=E9=80=9A=E8=BF=87=203=E3=80=81=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E4=BB=BB=E5=8A=A1=E6=8E=A5=E6=94=B6alertmessage?= =?UTF-8?q?=EF=BC=8C=E6=9F=A5=E8=AF=A2DynamicTaskInfos=E7=94=9F=E6=88=90co?= =?UTF-8?q?mmand=E6=97=B6=EF=BC=8C=E6=96=B0=E5=A2=9E=E5=85=B3=E8=81=94t=5F?= =?UTF-8?q?strategy=5Ftemplate=E8=A1=A8=EF=BC=8Cselect=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=9F=A5=E8=AF=A2=E5=B1=80=E7=82=B9dynamic?= =?UTF-8?q?=5Frule=5Frange=E3=80=81=E4=BA=8B=E4=BB=B6=E7=B1=BB=E5=9E=8Bstr?= =?UTF-8?q?ategy=5Ftemplate=5Fname=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../protection/server/alertmessage/AlertMessageService.java | 4 ++-- .../server/rule/staticrule/StaticRuleController.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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 bf6455b..cb6cbcf 100644 --- a/src/main/java/com/realtime/protection/server/alertmessage/AlertMessageService.java +++ b/src/main/java/com/realtime/protection/server/alertmessage/AlertMessageService.java @@ -89,7 +89,7 @@ public class AlertMessageService { alertMessage.getProtectIsSrcOrDst(), templateProtectLevel); //根据fiveTuple生成动态指令信息 - List dynamicCommandInfoList = new ArrayList(); + List dynamicCommandInfoList = new ArrayList<>(); if (fiveTupleWithMaskNew.size() == 2){ TaskCommandInfo dynamicCommandInfo_bi = new TaskCommandInfo(); dynamicCommandInfo_bi.copyTaskCommandInfo(dynamicCommandInfo); @@ -203,7 +203,7 @@ public class AlertMessageService { peer.setPort(null); peer.setMaskPort(null); } - List newFiveTupleWithMask = new ArrayList(); + List newFiveTupleWithMask = new ArrayList<>(); //生成指令 FiveTupleWithMask command1 = new FiveTupleWithMask(); command1.setSourceIP(peer.getIP()); 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 838a69e..0a95937 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 @@ -180,7 +180,7 @@ public class StaticRuleController implements StaticRuleControllerApi { */ @PostMapping("/auditbatch") public ResponseResult updateStaticRuleAuditStatusBatch(@RequestBody Map idsWithAuditStatusMap) { - List errorIds = new ArrayList(); + List errorIds = new ArrayList<>(); for (Map.Entry entry: idsWithAuditStatusMap.entrySet()) { Integer id = entry.getKey(); Integer auditStatus = entry.getValue();