合并redis到haskafka branch

This commit is contained in:
EnderByEndera
2024-04-29 15:33:09 +08:00
committed by Hao Miao
parent d6c487cd12
commit 57fcf627c0
22 changed files with 218 additions and 41 deletions

View File

@@ -3,12 +3,15 @@ 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.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 org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -19,13 +22,22 @@ public class ProtectObjectService {
private final ProtectObjectMapper protectObjectMapper;
private final SqlSessionWrapper sqlSessionWrapper;
private static final Integer batchSize = 100;
private final Counter counter;
public ProtectObjectService(ProtectObjectMapper protectObjectMapper, SqlSessionWrapper sqlSessionWrapper) {
public ProtectObjectService(ProtectObjectMapper protectObjectMapper, SqlSessionWrapper sqlSessionWrapper, Counter counter) {
this.protectObjectMapper = protectObjectMapper;
this.sqlSessionWrapper = sqlSessionWrapper;
this.counter = counter;
}
public Integer newProtectObject(ProtectObject protectObject) {
protectObject.setProtectObjectDisplayId(
"FHDX-"
+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
+ "-"
+ String.format("%06d", counter.generateId("protect_object"))
);
protectObjectMapper.newProtectObject(protectObject);
if (protectObject.getProtectObjectId() == null) {
@@ -42,6 +54,13 @@ public class ProtectObjectService {
List<ProtectObject> protectObjectBatch = ListUtils.newArrayListWithExpectedSize(batchSize);
for (ProtectObject protectObject : list) {
protectObject.setProtectObjectDisplayId(
"FHDX-"
+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
+ "-"
+ String.format("%06d", counter.generateId("protect_object"))
);
protectObjectBatch.add(protectObject);
if (protectObjectBatch.size() < batchSize) {
continue;

View File

@@ -3,6 +3,7 @@ package com.realtime.protection.server.rule.dynamicrule;
import com.alibaba.excel.util.ListUtils;
import com.realtime.protection.configuration.entity.defense.template.Template;
import com.realtime.protection.configuration.entity.rule.dynamicrule.DynamicRuleObject;
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.audit.AuditStatusValidator;
@@ -10,6 +11,7 @@ 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;
@@ -20,11 +22,12 @@ import java.util.function.Function;
public class DynamicRuleService {
private final DynamicRuleMapper dynamicRuleMapper;
private final SqlSessionWrapper sqlSessionWrapper;
private final Counter counter;
public DynamicRuleService(DynamicRuleMapper dynamicRuleMapper, SqlSessionWrapper sqlSessionWrapper) {
public DynamicRuleService(DynamicRuleMapper dynamicRuleMapper, SqlSessionWrapper sqlSessionWrapper, Counter counter) {
this.sqlSessionWrapper = sqlSessionWrapper;
this.dynamicRuleMapper = dynamicRuleMapper;
this.counter = counter;
}
@Transactional
@@ -37,6 +40,13 @@ public class DynamicRuleService {
throw new IllegalArgumentException("protect object id is invalid");
}
dynamicRule.setDynamicRuleDisplayId(
"DTGZ-"
+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
+ "-"
+ String.format("%06d", counter.generateId("dynamic_rule"))
);
dynamicRuleMapper.newDynamicRule(dynamicRule);
Integer dynamicRuleId = dynamicRule.getDynamicRuleId();
dynamicRule.getProtectObjectIds().forEach(
@@ -56,6 +66,12 @@ public class DynamicRuleService {
List<DynamicRuleObject> DynamicRuleIdBatch = ListUtils.newArrayListWithExpectedSize(100);
for (DynamicRuleObject dynamicRule : DynamicRuleList) {
dynamicRule.setDynamicRuleCreateTime(LocalDateTime.now());
dynamicRule.setDynamicRuleDisplayId(
"DTGZ-"
+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
+ "-"
+ String.format("%06d", counter.generateId("dynamic_rule"))
);
DynamicRuleIdBatch.add(dynamicRule);
if (DynamicRuleIdBatch.size() < 100) {
continue;

View File

@@ -3,6 +3,7 @@ package com.realtime.protection.server.rule.staticrule;
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.Counter;
import com.realtime.protection.configuration.utils.SqlSessionWrapper;
import com.realtime.protection.configuration.utils.enums.RuleEnum;
import com.realtime.protection.configuration.utils.enums.audit.AuditStatusEnum;
@@ -30,12 +31,15 @@ public class StaticRuleService {
private final StaticRuleMapper staticRuleMapper;
private final SqlSessionWrapper sqlSessionWrapper;
private final WhiteListService whiteListService;
private final Counter counter;
public StaticRuleService(StaticRuleMapper staticRuleMapper, SqlSessionWrapper sqlSessionWrapper, WhiteListService whiteListService) {
public StaticRuleService(StaticRuleMapper staticRuleMapper, SqlSessionWrapper sqlSessionWrapper, Counter counter) {
this.staticRuleMapper = staticRuleMapper;
this.sqlSessionWrapper = sqlSessionWrapper;
this.whiteListService = whiteListService;
this.counter = counter;
}
private static int ipToInt(String ip) {
@@ -86,6 +90,14 @@ public class StaticRuleService {
whiteListsHit.forEach(item -> result.append(item.getWhiteListName()).append(" "));
throw new IllegalArgumentException("静态规则与白名单规则冲突,冲突白名单名称:"+result.toString().trim());
}
object.setStaticRuleDisplayId(
"JTGZ-"
+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
+ "-"
+ String.format("%06d", counter.generateId("static_rule"))
);
staticRuleMapper.newStaticRuleObject(object);
return object.getStaticRuleId();
@@ -183,6 +195,12 @@ public class StaticRuleService {
List<StaticRuleObject> StaticRuleBatch = ListUtils.newArrayListWithExpectedSize(100);
for (StaticRuleObject staticRule : list) {
staticRule.setStaticRuleCreateTime(LocalDateTime.now());
staticRule.setStaticRuleDisplayId(
"JTGZ-"
+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
+ "-"
+ String.format("%06d", counter.generateId("static_rule"))
);
StaticRuleBatch.add(staticRule);
if (StaticRuleBatch.size() < 100) {
continue;

View File

@@ -7,6 +7,8 @@ import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -61,7 +63,8 @@ public interface TaskMapper {
Integer queryTaskTotalNum(@Param("task_status") Integer taskStatus, @Param("task_type") Integer task_type,
@Param("task_name") String taskName, @Param("task_creator") String taskCreator,
@Param("audit_status") Integer auditStatus);
@Param("audit_status") Integer auditStatus,
@Param("task_create_time") LocalDateTime taskCreateTime);
void updateAuditStatusByIdBatch(@Param("idWithAuditStatusBatch") Map<Integer, Integer> idWithAuditStatusBatch);

View File

@@ -7,6 +7,7 @@ import com.realtime.protection.configuration.entity.rule.staticrule.StaticRuleOb
import com.realtime.protection.configuration.entity.task.DynamicTaskInfo;
import com.realtime.protection.configuration.entity.task.Task;
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.audit.AuditStatusEnum;
@@ -14,9 +15,18 @@ import com.realtime.protection.configuration.utils.enums.audit.AuditStatusValida
import com.realtime.protection.server.rule.dynamicrule.DynamicRuleMapper;
import com.realtime.protection.server.rule.staticrule.StaticRuleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Isolation;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -30,20 +40,29 @@ public class TaskService {
private final SqlSessionWrapper sqlSessionWrapper;
private static final int BATCH_SIZE = 100;
private final DynamicRuleMapper dynamicRuleMapper;
private final Counter counter;
public TaskService(TaskMapper taskMapper, StaticRuleMapper staticRuleMapper, SqlSessionWrapper sqlSessionWrapper, DynamicRuleMapper dynamicRuleMapper) {
public TaskService(TaskMapper taskMapper, StaticRuleMapper staticRuleMapper, SqlSessionWrapper sqlSessionWrapper, DynamicRuleMapper dynamicRuleMapper, Counter counter) {
this.taskMapper = taskMapper;
this.staticRuleMapper = staticRuleMapper;
this.sqlSessionWrapper = sqlSessionWrapper;
this.dynamicRuleMapper = dynamicRuleMapper;
this.counter = counter;
}
@Transactional
public Long newTask(Task task) {
// todo: 目前获取方式还不确定,以后再确定
task.setTaskCreateUserId(1);
task.setTaskCreateUsername("xxx");
task.setTaskCreateDepart("xxx");
task.setTaskDisplayId(
"RW-"
+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
+ "-"
+ String.format("%06d", counter.generateId("task")));
taskMapper.newTask(task);
if (task.getStaticRuleIds() != null && !task.getStaticRuleIds().isEmpty()) {
@@ -317,7 +336,7 @@ public class TaskService {
}
public Integer queryTaskTotalNum(Integer taskStatus, Integer taskType, String taskName, String taskCreator, Integer auditStatus) {
return taskMapper.queryTaskTotalNum(taskStatus, taskType, taskName, taskCreator, auditStatus);
return taskMapper.queryTaskTotalNum(taskStatus, taskType, taskName, taskCreator, auditStatus, null);
}
public Object updateAuditStatusBatch(Map<Integer, Integer> idsWithAuditStatusMap) {

View File

@@ -135,7 +135,7 @@ public class StateHandler {
SimpleResponse response = mono.block(Duration.ofSeconds(5));
if (response == null || response.getSuccess() == null) {
if (response == null || response.getCode() != 200 || response.getSuccess() == null) {
return false;
}