1. 添加CorsFilter以支持localhost:8000的跨域请求

2. 添加DynamicTaskInfo实体类以用于处理BW系统的body
3. 新添加动态规则生成方法
This commit is contained in:
EnderByEndera
2024-01-17 19:07:04 +08:00
parent 7112e85a26
commit 0ab46a51f1
9 changed files with 159 additions and 12 deletions

View File

@@ -0,0 +1,18 @@
package com.realtime.protection.configuration.cors;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsFilter implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry corsRegistry) {
corsRegistry.addMapping("/**")
.allowedOrigins("http://localhost:8000")
.allowCredentials(true)
.allowedMethods("GET", "POST", "DELETE", "PUT")
.allowedHeaders("*")
.exposedHeaders("*");
}
}

View File

@@ -0,0 +1,31 @@
package com.realtime.protection.configuration.entity.task;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.List;
@Data
public class DynamicTaskInfo {
@Data
private static class SimpleProtectObject {
private String IP;
private Integer port;
private String URL;
private String protocol;
}
// 从任务中获取
private Long taskId;
private LocalDateTime startTime;
private LocalDateTime endTime;
// 从规则中获取
private Integer ruleId;
private String sourceSystem;
private String eventType;
// 从防护对象列表中获取
private List<SimpleProtectObject> protectObjects;
}

View File

@@ -0,0 +1,14 @@
package com.realtime.protection.configuration.response;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
@Data
public class SimpleResponse {
@JsonProperty("code")
private Integer code;
@JsonProperty("success")
private Boolean success;
}

View File

@@ -122,7 +122,7 @@ public class TaskController implements TaskControllerApi {
@Override @Override
@GetMapping("/{taskId}/running/{stateNum}") @GetMapping("/{taskId}/running/{stateNum}")
public ResponseResult changeTaskStatus(@PathVariable @NotNull @Min(0) @Max(6) Integer stateNum, public ResponseResult changeTaskStatus(@PathVariable @NotNull @Min(0) @Max(6) Integer stateNum,
@PathVariable @NotNull Long taskId) throws DorisStartException { @PathVariable @NotNull @Min(1) Long taskId) throws DorisStartException {
return ResponseResult.ok() return ResponseResult.ok()
.setData("task_id", taskId) .setData("task_id", taskId)
// 外部修改状态,需要进行状态检查 // 外部修改状态,需要进行状态检查

View File

@@ -180,7 +180,7 @@ public interface TaskControllerApi {
} }
) )
ResponseResult changeTaskStatus(@PathVariable @NotNull @Min(0) @Max(6) Integer stateNum, ResponseResult changeTaskStatus(@PathVariable @NotNull @Min(0) @Max(6) Integer stateNum,
@PathVariable @NotNull Long taskId) throws DorisStartException; @PathVariable @NotNull @Min(1) Long taskId) throws DorisStartException;
@GetMapping("/{taskId}/commands") @GetMapping("/{taskId}/commands")
@Operation( @Operation(

View File

@@ -1,5 +1,6 @@
package com.realtime.protection.server.task; package com.realtime.protection.server.task;
import com.realtime.protection.configuration.entity.task.DynamicTaskInfo;
import com.realtime.protection.configuration.entity.task.Task; import com.realtime.protection.configuration.entity.task.Task;
import com.realtime.protection.configuration.entity.task.TaskCommandInfo; import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
@@ -40,6 +41,8 @@ public interface TaskMapper {
List<TaskCommandInfo> getStaticCommandInfos(@Param("task_id") Long taskId); List<TaskCommandInfo> getStaticCommandInfos(@Param("task_id") Long taskId);
List<DynamicTaskInfo> getDynamicTaskInfos(@Param("task_id") Long taskId);
Integer queryTaskAuditStatus(@Param("task_id") Long taskId); Integer queryTaskAuditStatus(@Param("task_id") Long taskId);
Integer queryTaskStatus(@Param("task_id") Long taskId); Integer queryTaskStatus(@Param("task_id") Long taskId);

View File

@@ -1,6 +1,7 @@
package com.realtime.protection.server.task; package com.realtime.protection.server.task;
import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.dynamic.datasource.annotation.DS;
import com.realtime.protection.configuration.entity.task.DynamicTaskInfo;
import com.realtime.protection.configuration.entity.task.Task; import com.realtime.protection.configuration.entity.task.Task;
import com.realtime.protection.configuration.entity.task.TaskCommandInfo; import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
import com.realtime.protection.configuration.utils.enums.StateEnum; import com.realtime.protection.configuration.utils.enums.StateEnum;
@@ -110,6 +111,10 @@ public class TaskService {
return taskMapper.getStaticCommandInfos(taskId); return taskMapper.getStaticCommandInfos(taskId);
} }
public List<DynamicTaskInfo> getDynamicTaskInfos(Long taskId) {
return taskMapper.getDynamicTaskInfos(taskId);
}
public Integer queryTaskAuditStatus(Long taskId) { public Integer queryTaskAuditStatus(Long taskId) {
return taskMapper.queryTaskAuditStatus(taskId); return taskMapper.queryTaskAuditStatus(taskId);
} }

View File

@@ -1,18 +1,28 @@
package com.realtime.protection.server.task.status; package com.realtime.protection.server.task.status;
import com.realtime.protection.configuration.entity.task.DynamicTaskInfo;
import com.realtime.protection.configuration.entity.task.Task; import com.realtime.protection.configuration.entity.task.Task;
import com.realtime.protection.configuration.entity.task.TaskCommandInfo; import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
import com.realtime.protection.configuration.exception.DorisStartException; import com.realtime.protection.configuration.response.SimpleResponse;
import com.realtime.protection.configuration.utils.enums.TaskTypeEnum; import com.realtime.protection.configuration.utils.enums.TaskTypeEnum;
import com.realtime.protection.configuration.utils.status.AuditStatus; import com.realtime.protection.configuration.utils.status.AuditStatus;
import com.realtime.protection.server.command.CommandService; import com.realtime.protection.server.command.CommandService;
import com.realtime.protection.server.task.TaskService; import com.realtime.protection.server.task.TaskService;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Mono;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
public class StateHandler { public class StateHandler {
protected Boolean handleStart(TaskService taskService, CommandService commandService, Long taskId) throws DorisStartException { private final WebClient client = WebClient.builder()
.baseUrl("") // todo: unfinished
.build();
protected Boolean handleStart(TaskService taskService, CommandService commandService, Long taskId) {
Task task = taskService.queryTask(taskId); Task task = taskService.queryTask(taskId);
if (task == null) { if (task == null) {
@@ -32,8 +42,8 @@ public class StateHandler {
return switch (TaskTypeEnum.getTaskTypeByNum(task.getTaskType())) { return switch (TaskTypeEnum.getTaskTypeByNum(task.getTaskType())) {
case STATIC -> handleStaticTaskStart(commandService, taskService, taskId); case STATIC -> handleStaticTaskStart(commandService, taskService, taskId);
case DYNAMIC -> handleDynamicTaskStart(commandService, taskService, taskId); case DYNAMIC -> handleDynamicTaskStart(taskService, taskId);
case JUDGED -> handleJudgedTaskStart(commandService, taskService, taskId); case JUDGED -> handleJudgedTaskStart(taskService, taskId);
}; };
} }
@@ -64,14 +74,12 @@ public class StateHandler {
// todo: 如果是实时任务或者研判后处置任务,那么就需要在任务启动之后,立刻向动态规则中指定的系统发送日志筛选请求。 // todo: 如果是实时任务或者研判后处置任务,那么就需要在任务启动之后,立刻向动态规则中指定的系统发送日志筛选请求。
// 筛选完成后,系统返回日志,需要由接收端点提取字段,并且合成一条静态规则,再按照任务开始时间、结束时间和任务类型进行指令创建 // 筛选完成后,系统返回日志,需要由接收端点提取字段,并且合成一条静态规则,再按照任务开始时间、结束时间和任务类型进行指令创建
private Boolean handleJudgedTaskStart(CommandService commandService, TaskService taskService, Long taskId) { private Boolean handleJudgedTaskStart(TaskService taskService, Long taskId) {
// todo: 研判后处置任务的指令的is_valid字段一开始需要设置为false return sendFilters(taskService, taskId);
return true;
} }
private Boolean handleDynamicTaskStart(CommandService commandService, TaskService taskService, Long taskId) { private Boolean handleDynamicTaskStart(TaskService taskService, Long taskId) {
// todo: 实时任务的指令的is_valid字段一开始需要设置为true return sendFilters(taskService, taskId);
return true;
} }
private Boolean handleStaticTaskStart(CommandService commandService, TaskService taskService, Long taskId) { private Boolean handleStaticTaskStart(CommandService commandService, TaskService taskService, Long taskId) {
@@ -84,4 +92,36 @@ public class StateHandler {
commandService.createCommands(staticTaskCommandInfos); commandService.createCommands(staticTaskCommandInfos);
return true; return true;
} }
private Boolean sendFilters(TaskService taskService, Long taskId) {
List<DynamicTaskInfo> dynamicTaskInfos = taskService.getDynamicTaskInfos(taskId);
if (dynamicTaskInfos == null || dynamicTaskInfos.isEmpty()) {
throw new IllegalArgumentException("动态规则列表为空,请至少选择一个动态规则以启动动态/研判后类型任务");
}
AtomicReference<Boolean> success = new AtomicReference<>(false);
Mono<SimpleResponse> mono = client.post()
.uri("") // todo: unfinished
.bodyValue(dynamicTaskInfos)
.exchangeToMono(res -> {
if (res.statusCode().equals(HttpStatus.OK)) {
return res.bodyToMono(SimpleResponse.class);
}
return res.createError();
})
.doOnError(WebClientResponseException.class, res -> success.set(false));
SimpleResponse response = mono.block();
if (response == null) {
return false;
}
success.set(response.getSuccess());
return success.get();
}
} }

View File

@@ -223,4 +223,40 @@
WHERE task_id = #{task_id} WHERE task_id = #{task_id}
AND tsr.static_rule_audit_status = 2 AND tsr.static_rule_audit_status = 2
</select> </select>
<resultMap id="dynamicTaskInfoMap" type="com.realtime.protection.configuration.entity.task.DynamicTaskInfo">
<result column="task_id" property="taskId"/>
<result column="task_start_time" property="startTime"/>
<result column="task_end_time" property="endTime"/>
<result column="rule_id" property="ruleId"/>
<result column="source_system" property="sourceSystem"/>
<result column="event_type" property="eventType"/>
<collection property="protectObjects">
<result column="protect_object_ip" property="IP"/>
<result column="protect_object_port" property="port"/>
<result column="protect_object_url" property="URL"/>
<result column="protect_object_protocol" property="protocol"/>
</collection>
</resultMap>
<select id="getDynamicTaskInfos"
resultType="com.realtime.protection.configuration.entity.task.DynamicTaskInfo">
SELECT task_id,
task_start_time,
task_end_time,
tdr.dynamic_rule_id as rule_id,
strategy_template_source_system as source_system,
strategy_template_name as event_type,
INET_NTOA(protect_object_ip),
protect_object_port,
protect_object_url,
protect_object_protocol
FROM t_task AS tt
INNER JOIN realtime_protection.t_dynamic_rule tdr on tt.task_id = tdr.dynamic_rule_used_task_id
INNER JOIN realtime_protection.t_protect_object_dynamic_rule_conn tpodrc
on tdr.dynamic_rule_id = tpodrc.dynamic_rule_id
INNER JOIN realtime_protection.t_protect_object tpo on tpo.protect_object_id = tpodrc.protect_object_id
INNER JOIN realtime_protection.t_strategy_template tst on tdr.template_id = tst.strategy_template_id
WHERE task_id = #{task_id}
</select>
</mapper> </mapper>