diff --git a/src/main/java/com/realtime/protection/configuration/entity/whitelist/WhiteListObject.java b/src/main/java/com/realtime/protection/configuration/entity/whitelist/WhiteListObject.java index cc6e85f..b58fb8a 100644 --- a/src/main/java/com/realtime/protection/configuration/entity/whitelist/WhiteListObject.java +++ b/src/main/java/com/realtime/protection/configuration/entity/whitelist/WhiteListObject.java @@ -1,33 +1,49 @@ package com.realtime.protection.configuration.entity.whitelist; +import com.alibaba.excel.annotation.ExcelIgnore; +import com.alibaba.excel.annotation.ExcelProperty; import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Pattern; import lombok.Data; @Data public class WhiteListObject { @JsonProperty("whiteobj_id") + @ExcelIgnore private int whiteListId; @NotNull @JsonProperty("whiteobj_name") + @ExcelProperty("名称") private String whiteListName; @JsonProperty("whiteobj_system_name") + @ExcelProperty("系统名称") private String whiteListSystemName; @JsonProperty("whiteobj_ip_address") + @Pattern(regexp = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$", message = "Invalid IPv4 Address") + @ExcelProperty("IP地址") private String whiteListIP; @JsonProperty("whiteobj_port") + @Max(value = 65535, message = "port should not be more than 65535") + @Min(value = 1, message = "port should not be less than 1") + @ExcelProperty("端口") private int whiteListPort; @JsonProperty("whiteobj_url") + @ExcelProperty("URL") private String whiteListUrl; @JsonProperty("whiteobj_protocol") + @ExcelProperty("协议") private String whiteListProtocol; @JsonProperty("audit_status") + @ExcelIgnore private String whiteListAuditStatus; } 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 9ab411f..4afbfa5 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 @@ -38,8 +38,15 @@ public class StaticRuleController { public ResponseResult delete(@PathVariable List ids){ log.info("根据id删除静态规则:{}",ids); //调用service删除 - staticRuleService.deleteStaticRule(ids); - return ResponseResult.ok(); +// if(staticRuleService.deleteStaticRule(ids) == false){ +// return ResponseResult.error() +// .setData("static_rule_id",ids) +// .setData("success",false); +// //有的删了,有的没删,也返回false +// } + return ResponseResult.ok() + .setData("static_rule_id",ids) + .setData("success",true); } /** 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 ee17cd7..1b6b61f 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 @@ -9,22 +9,24 @@ import java.util.List; @Mapper public interface StaticRuleMapper { - /** - * 新建静态规则 - */ + + //新建静态规则 void newStaticRuleObject(@Param("object") StaticRuleObject object); - /** - * 根据主键删除菜品数据 - */ + //根据主键删除静态规则 @Delete("delete from t_static_rule where static_rule_id = #{id}") - void deleteStaticRule(Integer id); + Boolean deleteStaticRule(Integer id); + //修改静态规则 void updateStaticRule(StaticRuleObject object); + //按id查询静态规则 //@Select("select * from t_static_rule where static_rule_id = #{id}") StaticRuleObject queryStaticRuleById(Integer id); + //多页查询静态规则 List queryStaticRule(String static_rule_name, Integer static_rule_id, Integer page, Integer pageSize); + + } 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 fbeae56..8438597 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 @@ -1,16 +1,20 @@ package com.realtime.protection.server.rule.staticrule; import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.List; @Service public class StaticRuleService { - @Autowired - private StaticRuleMapper staticRuleMapper; + + private final StaticRuleMapper staticRuleMapper; + + public StaticRuleService(StaticRuleMapper staticRuleMapper) { + this.staticRuleMapper = staticRuleMapper; + } /* @@ -28,13 +32,16 @@ public class StaticRuleService { return object.getStaticRuleId(); } - public void deleteStaticRule(List ids) { + @Transactional + public Boolean deleteStaticRule(List staticRuleIds) { //判断当前静态规则是否能够删除---是否存在任务选择的静态规则?? //删除静态规则 - for (Integer id : ids) { - staticRuleMapper.deleteStaticRule(id); - } +// for (Integer id : staticRuleIds) { +// staticRuleMapper.deleteStaticRule(id); +// } + return staticRuleIds.stream().allMatch(staticRuleMapper::deleteStaticRule); + } @@ -47,6 +54,7 @@ public class StaticRuleService { } public StaticRuleObject queryStaticRuleById(Integer id) { + return staticRuleMapper.queryStaticRuleById(id); } 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 4031de8..daf8883 100644 --- a/src/main/java/com/realtime/protection/server/whitelist/WhiteListController.java +++ b/src/main/java/com/realtime/protection/server/whitelist/WhiteListController.java @@ -1,10 +1,14 @@ package com.realtime.protection.server.whitelist; +import com.alibaba.excel.EasyExcel; import com.realtime.protection.configuration.entity.whitelist.WhiteListObject; import com.realtime.protection.configuration.response.ResponseResult; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + + +import java.io.IOException; +import java.util.List; @RestController @RequestMapping("/whiteobj") @@ -25,4 +29,117 @@ public class WhiteListController { .setData("whiteobj_id", whiteListObjectId) .setData("success", true); } + + //以excel文件方式批量导入 + @PostMapping("/upload") + public ResponseResult uploadFile(MultipartFile uploadFile) throws IOException { + EasyExcel.read(uploadFile.getInputStream(), WhiteListObject.class, + new WhiteListDataListener(whiteListService)).sheet().doRead(); + return ResponseResult.ok(); + } + + @RequestMapping("/query") + public ResponseResult queryWhiteListObject(@RequestParam(value = "whiteobj_name", required = false)String whiteListName, + @RequestParam(value = "page_size", required = false)Integer whiteListId, + @RequestParam(value = "page", defaultValue = "1")Integer page, + @RequestParam(value = "page_size", defaultValue = "10")Integer pageSize) { + if (page <= 0 || pageSize <= 0) { + return new ResponseResult(400, "page or page_size is invalid") + .setData("whiteobj_list", null); + } + return ResponseResult.ok() + .setData("whiteobj_list", whiteListService.queryWhiteListObject(whiteListName, whiteListId, page, pageSize)); + } + + @RequestMapping("/{id}/query") + public ResponseResult queryWhiteListObjectById(@PathVariable Integer id) { + if (id <= 0) { + return new ResponseResult(400, "id is invalid") + .setData("whiteobj_list", null); + } + + WhiteListObject whiteListObject = whiteListService.queryWhiteListObjectById(id); + if (whiteListObject == null) { + return new ResponseResult(400, "id is invalid") + .setData("whiteobj_list", null); + } + return ResponseResult.ok() + .setData("whiteobj_list", whiteListObject); + } + + //根据id删除 + @DeleteMapping("/{id}/delete") + public ResponseResult deleteWhiteListObjectById(@PathVariable Integer id) { + if (id <= 0) { + return new ResponseResult(400, "id is invalid") + .setData("whiteobj_id", id) + .setData("success", false); + } + Integer num = whiteListService.deleteWhiteListObjectById(id); + if (num == 0) { + return new ResponseResult(400, "id is invalid") + .setData("whiteobj_id", id) + .setData("success", false); + } + return ResponseResult.ok() + .setData("whiteobj_id", id) + .setData("success", true); + //还要return 白名单名称吗,还要在查表看他的名称 + } + + + @PostMapping("/delete") + public ResponseResult deleteWhiteListObjects( @RequestBody List whiteListObjIds) { + //Post不支持body为json。。。 body只能是[9,10] + + return ResponseResult.ok() + .setData("whiteobj_list", whiteListObjIds) + .setData("success", whiteListService.deleteWhiteListObjects(whiteListObjIds)); + } + + @PostMapping ("/{id}/update") + public ResponseResult updateWhiteListObject(@PathVariable Integer id, + @RequestBody WhiteListObject object) { + object.setWhiteListId(id); + Integer num = whiteListService.updateWhiteListObject(object); + if (num == 0) { + return new ResponseResult(400, "id is invalid") + .setData("whiteobj_list", null); + } + return ResponseResult.ok(); + } + + + + //查询ip是否存在于白名单 + @PostMapping("/exist") + public ResponseResult existWhiteListObject(@RequestBody List ruleIds) { + //是请求规则的id,然后判断这个id所属的ip是否在白名单中吗 + //静态应该可以,但动态的,动态是实时过来告警信息,不存储规则? 存的话也行,那这里要区分id是静态的还是动态的 + //这里先走通静态的,要获取规则的源IP和目的IP,去白名单select看有没有(有的还有IP掩码,暂未实现) + + //返回涉及IP在白名单中的id, + List ruleInWhiteListIds = whiteListService.existWhiteListObject(ruleIds); + + return ResponseResult.ok() + .setData("ip_list", ruleInWhiteListIds); + } + + //修改审核状态 + @RequestMapping("/{id}/audit/{status}") + public ResponseResult updateWhiteListObjectAuditStatus(@PathVariable Integer id, + @PathVariable Integer status) { + if (id <= 0 || status < 0 || status > 2) { + return new ResponseResult(400, "id or status is invalid") + .setData("whiteobj_id", id) + .setData("success", false); + } + + return ResponseResult.ok() + .setDataMap(whiteListService.updateWhiteListObjectAuditStatus(id, status)) + .setData("whiteobj_id", id); + } + + + } diff --git a/src/main/java/com/realtime/protection/server/whitelist/WhiteListDataListener.java b/src/main/java/com/realtime/protection/server/whitelist/WhiteListDataListener.java new file mode 100644 index 0000000..ee20ac1 --- /dev/null +++ b/src/main/java/com/realtime/protection/server/whitelist/WhiteListDataListener.java @@ -0,0 +1,51 @@ +package com.realtime.protection.server.whitelist; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.read.listener.ReadListener; +import com.alibaba.excel.util.ListUtils; +import com.realtime.protection.configuration.entity.whitelist.WhiteListObject; +import lombok.extern.slf4j.Slf4j; +//import com.alibaba.fastjson2.JSON; + +import java.util.List; + +@Slf4j +public class WhiteListDataListener implements ReadListener { + + private static final int batchCount = 100; + private final List cachedDataList = ListUtils.newArrayListWithExpectedSize(batchCount); + + private final WhiteListService whiteListService; + + public WhiteListDataListener(WhiteListService whiteListService) { + this.whiteListService = whiteListService; + } + + @Override + public void invoke(WhiteListObject object, AnalysisContext analysisContext) { + log.info("解析到一条数据:{}", object.toString()); + cachedDataList.add(object); + if (cachedDataList.size() > batchCount) { + saveData(); + cachedDataList.clear(); + } + + } + + @Override + public void doAfterAllAnalysed(AnalysisContext analysisContext) { + saveData(); + } + + /** + * 加上存储数据库 + */ + private void saveData() { + log.info("{}条数据,开始存储数据库!", cachedDataList.size()); + Boolean success = whiteListService.newWhiteListObjects(cachedDataList); + log.info("存储数据库成功!"); + if (!success) { + throw new RuntimeException("Error reading data in /proobj/new"); + } + } +} 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 f94c10e..83fde3c 100644 --- a/src/main/java/com/realtime/protection/server/whitelist/WhiteListMapper.java +++ b/src/main/java/com/realtime/protection/server/whitelist/WhiteListMapper.java @@ -1,11 +1,35 @@ package com.realtime.protection.server.whitelist; +import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleObject; import com.realtime.protection.configuration.entity.whitelist.WhiteListObject; +import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; +import java.util.List; + @Mapper public interface WhiteListMapper { + //新建 void newWhiteListObject(@Param("object") WhiteListObject object); + //分页查询 + List queryWhiteListObject(String whiteListName, Integer whiteListId, Integer page, Integer pageSize); + //根据主键查询 + WhiteListObject queryWhiteListObjectById(Integer id); + //根据主键删除 + @Delete("delete from t_white_list where white_list_id = #{id}") + Integer deleteWhiteListObject(Integer id); + + Integer updateWhiteListObject(@Param("object") WhiteListObject object); + + String existWhiteListObject(@Param("staticRuleObject")StaticRuleObject staticRuleObject); + + Integer queryWhiteListObjectAuditStuatusById(Integer id); + + Boolean updateWhiteListObjectAuditStatus(Integer id, Integer status); + + void newWhiteListObjects(@Param("whiteListObjects")List whiteListBatch); + + void deleteWhiteListObjects(@Param("whiteListIds") List whiteListBatch); } 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 ebceacc..81c09c3 100644 --- a/src/main/java/com/realtime/protection/server/whitelist/WhiteListService.java +++ b/src/main/java/com/realtime/protection/server/whitelist/WhiteListService.java @@ -1,21 +1,151 @@ 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.whitelist.WhiteListObject; +import com.realtime.protection.configuration.utils.AuditStatusValidator; +import com.realtime.protection.configuration.utils.SqlSessionWrapper; +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; +import java.util.function.Function; @Service public class WhiteListService { private final WhiteListMapper whiteListMapper; + private final StaticRuleMapper staticRuleMapper; - public WhiteListService(WhiteListMapper whiteListMapper) { + private final SqlSessionWrapper sqlSessionWrapper; + + public WhiteListService(WhiteListMapper whiteListMapper, + StaticRuleMapper staticRuleMapper, + SqlSessionWrapper sqlSessionWrapper) { this.whiteListMapper = whiteListMapper; + this.staticRuleMapper = staticRuleMapper; + this.sqlSessionWrapper = sqlSessionWrapper; } + //新建一个whitelist public Integer newWhiteListObject(WhiteListObject object) { whiteListMapper.newWhiteListObject(object); return object.getWhiteListId(); } + //批量新建多个whitelist + public Boolean newWhiteListObjects(List whiteListObjectList) { + Function, Boolean>> newWhiteListObjectFunction = + mapper -> list -> { + if (list == null || list.isEmpty()) { + return false; + } + + List WhiteListBatch = ListUtils.newArrayListWithExpectedSize(100); + for (WhiteListObject whiteListObject : whiteListObjectList) { + WhiteListBatch.add(whiteListObject); + if (WhiteListBatch.size() < 100) { + continue; + } + //mapper指的就是外层函数输入的参数,也就是WhiteListMapper + mapper.newWhiteListObjects(WhiteListBatch); + WhiteListBatch.clear(); + } + if (!WhiteListBatch.isEmpty()) { + mapper.newWhiteListObjects(WhiteListBatch); + } + return true; + }; + //实现事务操作 + return sqlSessionWrapper.startBatchSession(WhiteListMapper.class, newWhiteListObjectFunction, whiteListObjectList); + } + + public Integer deleteWhiteListObjectById(Integer id) { + return whiteListMapper.deleteWhiteListObject(id); + } + + //@Transactional + public Boolean deleteWhiteListObjects(List whiteListObjIds) { +// for (Integer id : whiteobj_ids) { +// whiteListMapper.deleteWhiteListObject(id); +// } + Function, Boolean>> deleteWhiteListObjectFunction = + mapper -> list -> { + if (list == null || list.isEmpty()) { + return false; + } + + List WhiteListBatch = ListUtils.newArrayListWithExpectedSize(100); + for (Integer whiteListObjId : list) { + WhiteListBatch.add(whiteListObjId); + if (WhiteListBatch.size() < 100) { + continue; + } + //mapper指的就是外层函数输入的参数,也就是WhiteListMapper + mapper.deleteWhiteListObjects(WhiteListBatch); + WhiteListBatch.clear(); + } + if (!WhiteListBatch.isEmpty()) { + mapper.deleteWhiteListObjects(WhiteListBatch); + } + return true; + }; + //实现事务操作 + return sqlSessionWrapper.startBatchSession(WhiteListMapper.class, deleteWhiteListObjectFunction, whiteListObjIds); + + } + + public Integer updateWhiteListObject(WhiteListObject object) { + return whiteListMapper.updateWhiteListObject(object); + } + + public List queryWhiteListObject(String whiteListName, Integer whiteListId, + Integer page, Integer pageSize) { + return whiteListMapper.queryWhiteListObject(whiteListName, whiteListId, page, pageSize); + } + + public WhiteListObject queryWhiteListObjectById(Integer id) { + return whiteListMapper.queryWhiteListObjectById(id); + } + + public List existWhiteListObject(List ruleIds) { + + List ip_list = new ArrayList<>(); + + for (Integer id : ruleIds) { + StaticRuleObject staticRuleObject = staticRuleMapper.queryStaticRuleById(id); + if (staticRuleObject != null) { + String whiteListIp = whiteListMapper.existWhiteListObject(staticRuleObject); + if (whiteListIp != null) { + ip_list.add(whiteListIp); + } + } + } + return ip_list; + } + + @Transactional + public Map updateWhiteListObjectAuditStatus(Integer id, Integer auditStatus) { + //查询目前curStatus + Integer originalAuditStatus = whiteListMapper.queryWhiteListObjectAuditStuatusById(id); + //判断是否可以修改 + if (!AuditStatusValidator.setOriginal(originalAuditStatus).checkValidate(auditStatus)) { + throw new IllegalArgumentException("invalid audit status"); + } + + Boolean success = whiteListMapper.updateWhiteListObjectAuditStatus(id, auditStatus); + + Map resultMap = new HashMap<>(); + resultMap.put("success", success); + resultMap.put("audit_status", auditStatus); + + return resultMap; + } + + } diff --git a/src/main/resources/mappers/WhiteListMapper.xml b/src/main/resources/mappers/WhiteListMapper.xml index ae70902..57ea880 100644 --- a/src/main/resources/mappers/WhiteListMapper.xml +++ b/src/main/resources/mappers/WhiteListMapper.xml @@ -14,4 +14,106 @@ #{object.whiteListUrl}, #{object.whiteListProtocol}, 0) + + + insert into t_white_list(white_list_name, white_list_system_name, + white_list_ip, white_list_port, + white_list_url, white_list_protocol, + white_list_audit_status) + values + + (#{object.whiteListName}, #{object.whiteListSystemName}, + INET_ATON(#{object.whiteListIP}), #{object.whiteListPort}, + #{object.whiteListUrl}, #{object.whiteListProtocol}, + 0) + + + + + delete from t_white_list + where white_list_id in + + #{id} + + + + + + + + + + + + + + + + + + + + + update t_white_list + + + white_list_name = #{object.whiteListName}, + + + white_list_system_name = #{object.whiteListSystemName}, + + + white_list_ip = INET_ATON(#{object.whiteListIP}), + + + white_list_port = #{object.whiteListPort}, + + + white_list_url = #{object.whiteListUrl}, + + + white_list_protocol = #{object.whiteListProtocol}, + + + white_list_audit_status = #{object.whiteListAuditStatus}, + + + where white_list_id = #{object.whiteListId} + + + update t_white_list + set white_list_audit_status = #{status} + where white_list_id = #{id} + + + + + \ 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 4bcc859..60ee698 100644 --- a/src/test/java/com/realtime/protection/server/whitelist/WhiteListServiceTest.java +++ b/src/test/java/com/realtime/protection/server/whitelist/WhiteListServiceTest.java @@ -1,22 +1,37 @@ package com.realtime.protection.server.whitelist; import com.realtime.protection.configuration.entity.whitelist.WhiteListObject; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import java.util.ArrayList; +import java.util.List; + import static org.junit.jupiter.api.Assertions.*; @SpringBootTest class WhiteListServiceTest { private final WhiteListService whiteListService; - + private WhiteListObject whiteListObject; @Autowired WhiteListServiceTest(WhiteListService whiteListService) { this.whiteListService = whiteListService; } + @BeforeEach + void setUp() { + whiteListObject = new WhiteListObject(); + whiteListObject.setWhiteListName("test"); + whiteListObject.setWhiteListSystemName("china"); + whiteListObject.setWhiteListIP("128.1.1.1"); + whiteListObject.setWhiteListPort(80); + whiteListObject.setWhiteListUrl("www.baidu.com"); + whiteListObject.setWhiteListProtocol("TCP"); + } + @Test void testNewWhiteList() { WhiteListObject object = new WhiteListObject(); @@ -25,4 +40,35 @@ class WhiteListServiceTest { Integer objectId = whiteListService.newWhiteListObject(object); assertTrue(objectId > 0); } + + @Test + void newProtectObjects() { + List whiteListObjects = new ArrayList<>(); + for (int i = 0; i < 1000; i++) { + whiteListObjects.add(whiteListObject); + } + Boolean success = whiteListService.newWhiteListObjects(whiteListObjects); + assertTrue(success); + } + + @Test + void testUpdateWhiteList() { + WhiteListObject object = new WhiteListObject(); + object.setWhiteListId(7); + object.setWhiteListName("test_update"); + + whiteListService.updateWhiteListObject(object); + } + + @Test + void testExistWhiteList() { + List ruleIds = List.of(6,7,8); + List ip_list = whiteListService.existWhiteListObject(ruleIds); + System.out.println(ip_list); + } + + @Test + void testUpdateWhiteListAuditStatus() { + whiteListService.updateWhiteListObjectAuditStatus(7, 1); + } } \ No newline at end of file