diff --git a/src/main/java/com/realtime/protection/server/whitelist/WhiteListController.java b/src/main/java/com/realtime/protection/server/whitelist/WhiteListController.java index 9796ff3..b235597 100644 --- a/src/main/java/com/realtime/protection/server/whitelist/WhiteListController.java +++ b/src/main/java/com/realtime/protection/server/whitelist/WhiteListController.java @@ -126,14 +126,15 @@ public class WhiteListController { //查询ip是否存在于白名单 - @PostMapping("/exist") - public ResponseResult existWhiteListObject(@RequestBody List ruleIds) { + @RequestMapping ("/exist") + public ResponseResult existWhiteListObject(@RequestParam(value = "ruleId", required = true)Integer ruleId, + @RequestParam(value = "ruleType", required = true)Integer ruleType) { //是请求规则的id,然后判断这个id所属的ip是否在白名单中吗 //静态应该可以,但动态的,动态是实时过来告警信息,不存储规则? 存的话也行,那这里要区分id是静态的还是动态的 //这里先走通静态的,要获取规则的源IP和目的IP,去白名单select看有没有(有的还有IP掩码,暂未实现) - //返回涉及IP在白名单中的id, - List ruleInWhiteListIds = whiteListService.existWhiteListObject(ruleIds); + //返回在白名单中的IP, + List ruleInWhiteListIds = whiteListService.existWhiteListObject(ruleId, ruleType); return ResponseResult.ok() .setData("ip_list", ruleInWhiteListIds); 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 83fde3c..f31c6eb 100644 --- a/src/main/java/com/realtime/protection/server/whitelist/WhiteListMapper.java +++ b/src/main/java/com/realtime/protection/server/whitelist/WhiteListMapper.java @@ -1,6 +1,7 @@ package com.realtime.protection.server.whitelist; import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject; +import com.realtime.protection.configuration.entity.task.Command; import com.realtime.protection.configuration.entity.whitelist.WhiteListObject; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Mapper; @@ -23,7 +24,7 @@ public interface WhiteListMapper { Integer updateWhiteListObject(@Param("object") WhiteListObject object); - String existWhiteListObject(@Param("staticRuleObject")StaticRuleObject staticRuleObject); + List existWhiteListObject(@Param("staticRuleObject")StaticRuleObject staticRuleObject); Integer queryWhiteListObjectAuditStuatusById(Integer id); @@ -32,4 +33,6 @@ public interface WhiteListMapper { void newWhiteListObjects(@Param("whiteListObjects")List whiteListBatch); void deleteWhiteListObjects(@Param("whiteListIds") List whiteListBatch); + + List whiteListCommandJudge(@Param("command") Command command); } 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 600534d..45b8482 100644 --- a/src/main/java/com/realtime/protection/server/whitelist/WhiteListService.java +++ b/src/main/java/com/realtime/protection/server/whitelist/WhiteListService.java @@ -2,6 +2,7 @@ package com.realtime.protection.server.whitelist; import com.alibaba.excel.util.ListUtils; import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject; +import com.realtime.protection.configuration.entity.task.Command; import com.realtime.protection.configuration.entity.whitelist.WhiteListObject; import com.realtime.protection.configuration.utils.status.AuditStatusValidator; import com.realtime.protection.configuration.utils.SqlSessionWrapper; @@ -9,7 +10,6 @@ import com.realtime.protection.server.rule.staticrule.StaticRuleMapper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -113,20 +113,25 @@ public class WhiteListService { return whiteListMapper.queryWhiteListObjectById(id); } - public List existWhiteListObject(List ruleIds) { + @Transactional + public List existWhiteListObject(Integer ruleId, Integer ruleType) { + //应该参数是指令,不管动态静态 - List ip_list = new ArrayList<>(); + //查询ruleId应对的静态or动态规则 + StaticRuleObject staticRuleObject = staticRuleMapper.queryStaticRuleById(ruleId); - for (Integer id : ruleIds) { - StaticRuleObject staticRuleObject = staticRuleMapper.queryStaticRuleById(id); - if (staticRuleObject != null) { - String whiteListIp = whiteListMapper.existWhiteListObject(staticRuleObject); - if (whiteListIp != null) { - ip_list.add(whiteListIp); - } - } + if (staticRuleObject == null) { + throw new IllegalArgumentException("invalid rule id"); } - return ip_list; + // 命中的whitelist列表:每一列包含ip port url + return whiteListMapper.existWhiteListObject(staticRuleObject); + + } + public List whiteListCommandJudge(Command command) { + //参数应该是指令,不管动态静态 + // 命中的whitelist列表:每一列包含ip port url + return whiteListMapper.whiteListCommandJudge(command); + } @Transactional diff --git a/src/main/resources/mappers/WhiteListMapper.xml b/src/main/resources/mappers/WhiteListMapper.xml index 57ea880..f6dbb7d 100644 --- a/src/main/resources/mappers/WhiteListMapper.xml +++ b/src/main/resources/mappers/WhiteListMapper.xml @@ -101,19 +101,98 @@ + + + + + \ No newline at end of file diff --git a/src/test/java/com/realtime/protection/server/whitelist/WhiteListServiceTest.java b/src/test/java/com/realtime/protection/server/whitelist/WhiteListServiceTest.java index 60ee698..1366d2b 100644 --- a/src/test/java/com/realtime/protection/server/whitelist/WhiteListServiceTest.java +++ b/src/test/java/com/realtime/protection/server/whitelist/WhiteListServiceTest.java @@ -1,5 +1,6 @@ package com.realtime.protection.server.whitelist; +import com.realtime.protection.configuration.entity.task.Command; import com.realtime.protection.configuration.entity.whitelist.WhiteListObject; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -63,12 +64,24 @@ class WhiteListServiceTest { @Test void testExistWhiteList() { List ruleIds = List.of(6,7,8); - List ip_list = whiteListService.existWhiteListObject(ruleIds); - System.out.println(ip_list); +// List ip_list = whiteListService.existWhiteListObject(ruleIds); +// System.out.println(ip_list); } @Test void testUpdateWhiteListAuditStatus() { whiteListService.updateWhiteListObjectAuditStatus(7, 1); } + + @Test + void testWhiteListCommandJudge() { + Command command = new Command(); + command.setDestinationIP("128.1.1.123"); + command.setMaskDestinationIP("255.255.255.0"); + command.setDestinationPort("81"); + + List whitelists = whiteListService.whiteListCommandJudge(command); + System.out.println(whitelists); + } + } \ No newline at end of file