合并redis到haskafka branch
This commit is contained in:
@@ -17,6 +17,11 @@ public class ProtectObject {
|
||||
@Schema(description = "防护对象ID", accessMode = Schema.AccessMode.READ_ONLY)
|
||||
private Integer protectObjectId;
|
||||
|
||||
@JsonProperty("proobj_display_id")
|
||||
@ExcelProperty("ID")
|
||||
@Schema(description = "防护对象显示ID", accessMode = Schema.AccessMode.READ_ONLY)
|
||||
private String protectObjectDisplayId;
|
||||
|
||||
@JsonProperty("proobj_name")
|
||||
@NotNull(message = "proobj_name字段不能为空。")
|
||||
@ExcelProperty("名称")
|
||||
|
||||
@@ -22,6 +22,10 @@ public class DynamicRuleObject {
|
||||
@Schema(description = "动态规则ID", accessMode = Schema.AccessMode.READ_ONLY)
|
||||
private Integer dynamicRuleId;
|
||||
|
||||
@JsonProperty("dynamic_rule_display_id")
|
||||
@Schema(description = "动态规则显示ID", accessMode = Schema.AccessMode.READ_ONLY)
|
||||
private String dynamicRuleDisplayId;
|
||||
|
||||
@NotNull
|
||||
@JsonProperty("dynamic_rule_name")
|
||||
@Schema(description = "动态规则名称", example = "动态规则测试", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@@ -38,8 +42,6 @@ public class DynamicRuleObject {
|
||||
@Schema(description = "动态规则创建用户名称", accessMode = Schema.AccessMode.READ_ONLY)
|
||||
private String dynamicRuleCreateUsername;
|
||||
|
||||
// @JsonProperty("dynamic_rule_audit_status")
|
||||
// private Integer dynamicRuleAuditStatus;
|
||||
@JsonProperty("dynamic_rule_create_depart")
|
||||
@Schema(description = "动态规则创建用户所属部门", accessMode = Schema.AccessMode.READ_ONLY)
|
||||
private String dynamicRuleCreateDepart;
|
||||
@@ -99,7 +101,6 @@ public class DynamicRuleObject {
|
||||
@Schema(description = "筛选条件-日志规则id", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private Long logRuleId;
|
||||
|
||||
|
||||
@JsonProperty("dynamic_rule_audit_status")
|
||||
@ExcelIgnore
|
||||
@Schema(description = "动态规则审核状态(0为未审核,1为已退回,2为审核通过)", example = "2", accessMode = Schema.AccessMode.READ_ONLY)
|
||||
|
||||
@@ -28,6 +28,11 @@ public class StaticRuleObject {
|
||||
@Schema(description = "静态规则ID", accessMode = Schema.AccessMode.READ_ONLY)
|
||||
private Integer staticRuleId;
|
||||
|
||||
@JsonProperty("static_rule_display_id")
|
||||
@ExcelProperty("ID")
|
||||
@Schema(description = "静态规则显示ID", accessMode = Schema.AccessMode.READ_ONLY)
|
||||
private String staticRuleDisplayId;
|
||||
|
||||
@NotNull
|
||||
@JsonProperty("static_rule_name")
|
||||
@ExcelProperty("名称")
|
||||
|
||||
@@ -16,6 +16,10 @@ public class Task {
|
||||
@Schema(description = "任务ID", accessMode = Schema.AccessMode.READ_ONLY)
|
||||
private Long taskId;
|
||||
|
||||
@JsonProperty("task_display_id")
|
||||
@Schema(description = "任务显示ID", accessMode = Schema.AccessMode.READ_ONLY)
|
||||
private String taskDisplayId;
|
||||
|
||||
@JsonProperty("task_name")
|
||||
@NotNull(message = "task_name字段不能为空。")
|
||||
@Schema(description = "任务名称", example = "静态任务")
|
||||
|
||||
@@ -17,6 +17,10 @@ public class TaskCommandInfo {
|
||||
@JsonProperty("task_id")
|
||||
private Long taskId;
|
||||
|
||||
@Schema(description = "任务显示ID", accessMode = Schema.AccessMode.READ_ONLY)
|
||||
@JsonProperty("task_display_id")
|
||||
private String taskDisplayId;
|
||||
|
||||
@Schema(description = "规则ID", hidden = true)
|
||||
@JsonProperty("rule_id")
|
||||
private Long ruleId;
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.realtime.protection.configuration.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.BoundValueOperations;
|
||||
import org.springframework.data.redis.core.Cursor;
|
||||
import org.springframework.data.redis.core.ScanOptions;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class Counter {
|
||||
private final StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
public Counter(StringRedisTemplate stringRedisTemplate) {
|
||||
this.stringRedisTemplate = stringRedisTemplate;
|
||||
}
|
||||
|
||||
public Long generateId(String typeName) {
|
||||
String key = String.format("counter::%s", typeName.toLowerCase());
|
||||
BoundValueOperations<String, String> op = stringRedisTemplate.boundValueOps(key);
|
||||
return op.increment();
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0 0 0 * * ?")
|
||||
public void resetId() {
|
||||
Cursor<String> cursor = stringRedisTemplate.scan(ScanOptions.scanOptions().build());
|
||||
while (cursor.hasNext()) {
|
||||
String key = cursor.next();
|
||||
if (key.startsWith("counter::")) {
|
||||
stringRedisTemplate.delete(key);
|
||||
log.debug("删除计时器key: {}", key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user