diff --git a/build.gradle b/build.gradle index 9aa2eb1..dfe5756 100644 --- a/build.gradle +++ b/build.gradle @@ -50,6 +50,8 @@ dependencies { implementation 'com.baomidou:dynamic-datasource-spring-boot3-starter:4.3.0' implementation 'com.github.xiaoymin:knife4j-openapi3-jakarta-spring-boot-starter:4.4.0' implementation 'com.squareup.okhttp3:okhttp:4.12.0' + implementation 'com.alibaba:druid:1.2.23' + } tasks.named('test') { diff --git a/src/main/java/com/realtime/protection/configuration/entity/task/DynamicTaskInfo.java b/src/main/java/com/realtime/protection/configuration/entity/task/DynamicTaskInfo.java index 3d2e42a..14f442b 100644 --- a/src/main/java/com/realtime/protection/configuration/entity/task/DynamicTaskInfo.java +++ b/src/main/java/com/realtime/protection/configuration/entity/task/DynamicTaskInfo.java @@ -1,11 +1,21 @@ package com.realtime.protection.configuration.entity.task; +import com.alibaba.druid.sql.ast.SQLStatement; +import com.alibaba.druid.sql.ast.statement.SQLSelect; +import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock; +import com.alibaba.druid.sql.ast.statement.SQLSelectStatement; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor; +import com.alibaba.druid.sql.parser.SQLStatementParser; +import com.alibaba.druid.stat.TableStat; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; +import lombok.extern.slf4j.Slf4j; import java.time.LocalDateTime; -import java.util.List; +import java.util.*; +@Slf4j @Data public class DynamicTaskInfo { @@ -45,10 +55,76 @@ public class DynamicTaskInfo { @JsonProperty("event_type") private String eventType; - @JsonProperty("log_rule_id") - private Long logRuleId; +// @JsonProperty("event_ids") +// private List eventIds; +// +// @JsonProperty("c_inoutid") +// private List inoutId; +// +// @JsonProperty("c_netnum") +// private List netNum; + + @JsonProperty("conditions") + private HashMap conditions = new HashMap<>(); + +// @JsonProperty("eq_condition") +// private HashMap eqConditions = new HashMap<>();; + + @JsonProperty("table_name") + private List tableNames = new ArrayList<>(); + + @JsonProperty("bw_sql") + private String bwSql; + + @JsonProperty("select_columns") + private List selectColumns = new ArrayList<>(); // 从防护对象列表中获取 @JsonProperty("protect_objects") private List protectObjects; + + public void parseSql() { + String bwSql = this.bwSql; + //解析SQL语句 + SQLStatementParser parser = new MySqlStatementParser(bwSql); + // 使用Parser解析生成AST,这里SQLStatement就是AST + SQLStatement sqlStatement = parser.parseStatement(); + + // 检查是否是SELECT语句 + if (sqlStatement instanceof SQLSelectStatement) { + // 创建访问者 + MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor(); + sqlStatement.accept(visitor); + + // 进一步细化,确保只获取SELECT子句的字段 + SQLSelectStatement selectStatement = (SQLSelectStatement) sqlStatement; + SQLSelect select = selectStatement.getSelect(); + SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) select.getQuery(); + + queryBlock.getSelectList().forEach(item -> this.selectColumns.add(item.toString())); + + Map tables = visitor.getTables(); + for (TableStat.Name t : tables.keySet()) { + this.tableNames.add(t.getName()); + } + //获取where条件 + List conditions = visitor.getConditions(); + conditions.forEach(condition -> { +// log.info("解析sql后的查询条件:{}", condition.getColumn().getName()); + if (condition.getOperator().equals("IN")){ + this.conditions.put(condition.getColumn().getName(),condition.getValues().toString()); + } + if (condition.getOperator().equals("=")){ + this.conditions.put(condition.getColumn().getName(),condition.getValues().toString()); + } + }); +// log.info("解析sql后的查询条件:{}",conditions); + + } else { + throw new IllegalArgumentException("不是SELECT查询语句,不进行字段解析。"); + } + + + } + } diff --git a/src/main/java/com/realtime/protection/configuration/response/ResponseResult.java b/src/main/java/com/realtime/protection/configuration/response/ResponseResult.java index 9e7e508..e54c48e 100644 --- a/src/main/java/com/realtime/protection/configuration/response/ResponseResult.java +++ b/src/main/java/com/realtime/protection/configuration/response/ResponseResult.java @@ -41,7 +41,7 @@ public class ResponseResult implements Serializable { } public static ResponseResult ok() { - return new ResponseResult(200, "request succeed"); + return new ResponseResult(200, "请求成功"); } public static ResponseResult ok(String message) { @@ -49,11 +49,11 @@ public class ResponseResult implements Serializable { } public static ResponseResult error() { - return new ResponseResult(500, "request failed"); + return new ResponseResult(500, "请求失败"); } public static ResponseResult invalid() { - return new ResponseResult(400, "invalid request"); + return new ResponseResult(400, "请求无效"); } public static ResponseResult invalid(String message) { @@ -61,7 +61,7 @@ public class ResponseResult implements Serializable { } public static ResponseResult unAuthorized() { - return new ResponseResult(401, "UnAuthorized User"); + return new ResponseResult(401, "未授权用户"); } public static ResponseResult error(String message) { diff --git a/src/main/java/com/realtime/protection/server/task/TaskService.java b/src/main/java/com/realtime/protection/server/task/TaskService.java index 79e970b..df05451 100644 --- a/src/main/java/com/realtime/protection/server/task/TaskService.java +++ b/src/main/java/com/realtime/protection/server/task/TaskService.java @@ -92,7 +92,7 @@ public class TaskService { //校验防护对象是否存在 boolean ProtectObjIdValid = task.getProtectObjectIds().stream() .allMatch(dynamicRuleMapper::queryProtectObjectById); - if (!ProtectObjIdValid) { + if (!ProtectObjIdValid && !task.getProtectObjectIds().isEmpty()){ throw new IllegalArgumentException("部分防护对象不存在"); } //任务和防护对象多对多关联建立 diff --git a/src/main/java/com/realtime/protection/server/task/status/StateHandler.java b/src/main/java/com/realtime/protection/server/task/status/StateHandler.java index f2ee50b..48d8e41 100644 --- a/src/main/java/com/realtime/protection/server/task/status/StateHandler.java +++ b/src/main/java/com/realtime/protection/server/task/status/StateHandler.java @@ -8,11 +8,10 @@ import com.realtime.protection.configuration.utils.enums.TaskTypeEnum; import com.realtime.protection.configuration.utils.enums.audit.AuditStatusEnum; import com.realtime.protection.server.command.CommandService; import com.realtime.protection.server.task.TaskService; -import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; -import org.springframework.web.reactive.function.client.WebClient; -import org.springframework.web.reactive.function.client.WebClientResponseException; +import org.springframework.web.reactive.function.client.*; import reactor.core.publisher.Mono; +import org.springframework.web.reactive.function.client.WebClient; import java.time.Duration; import java.util.List; @@ -114,12 +113,18 @@ public class StateHandler { if (dynamicTaskInfos == null || dynamicTaskInfos.isEmpty()) { throw new IllegalArgumentException("动态规则列表为空,请至少选择一个动态规则以启动动态/研判后类型任务"); } + //解析sql + dynamicTaskInfos.forEach(dynamicTaskInfo -> { + dynamicTaskInfo.parseSql(); + }); // 将所有关联的动态规则审批状态修改为“已使用” taskService.updateDynamicRuleAuditStatusInTask(task.getTaskId(), AuditStatusEnum.USING); AtomicReference success = new AtomicReference<>(false); +// System.out.println(dynamicTaskInfos); + Mono mono = client.post() .uri("/api/v1/kafkasend") .bodyValue(dynamicTaskInfos) @@ -144,3 +149,4 @@ public class StateHandler { return success.get(); } } + diff --git a/src/main/resources/mappers/CommandMapper.xml b/src/main/resources/mappers/CommandMapper.xml index f0fb343..9cef982 100644 --- a/src/main/resources/mappers/CommandMapper.xml +++ b/src/main/resources/mappers/CommandMapper.xml @@ -9,7 +9,7 @@ MASK_SRC_IP, MASK_SRC_PORT, MASK_DST_IP, MASK_DST_PORT, MASK_PROTOCOL, VALID_TIME, INVALID_TIME, IS_VALID, IS_JUDGED, SEND_TIMES, SUCCESS_TIMES, CREATE_TIME, LAST_UPDATE, IS_DELETED, - TASK_TYPE, RULE_ID) + TASKTYPE, RULE_ID) values (#{info.UUID}, #{info.taskId}, #{info.taskAct}, #{info.taskName}, #{info.eventType}, #{info.taskCreateDepart}, #{info.distributePoint}, #{info.frequency}, DEFAULT, @@ -32,7 +32,7 @@ MASK_SRC_IP, MASK_SRC_PORT, MASK_DST_IP, MASK_DST_PORT, MASK_PROTOCOL, VALID_TIME, INVALID_TIME, IS_VALID, IS_JUDGED, SEND_TIMES, SUCCESS_TIMES, CREATE_TIME, LAST_UPDATE, IS_DELETED, - TASK_TYPE, RULE_ID) + TASKTYPE, RULE_ID) values (#{info.UUID}, #{info.taskId}, #{info.taskAct}, #{info.taskName}, #{info.eventType}, #{info.taskCreateDepart}, #{info.distributePoint}, diff --git a/src/main/resources/mappers/DynamicRuleMapper.xml b/src/main/resources/mappers/DynamicRuleMapper.xml index 9247db8..ae37f33 100644 --- a/src/main/resources/mappers/DynamicRuleMapper.xml +++ b/src/main/resources/mappers/DynamicRuleMapper.xml @@ -12,11 +12,12 @@ (dynamic_rule_name, create_time, modify_time, dynamic_rule_create_username, dynamic_rule_create_depart, - dynamic_rule_create_user_id, bw_sql, dynamic_rule_display_id) + dynamic_rule_create_user_id, bw_sql, dynamic_rule_display_id, source_system) values (#{object.dynamicRuleName}, NOW(), NOW(), #{object.dynamicRuleCreateUsername}, #{object.dynamicRuleCreateDepart}, - #{object.dynamicRuleCreateUserId}, #{object.bwSql}, #{object.dynamicRuleDisplayId}) + #{object.dynamicRuleCreateUserId}, #{object.bwSql}, #{object.dynamicRuleDisplayId} + ,#{object.dynamicRuleSourceSystem}) diff --git a/src/main/resources/mappers/TaskMapper.xml b/src/main/resources/mappers/TaskMapper.xml index 9f5250d..b145dd3 100644 --- a/src/main/resources/mappers/TaskMapper.xml +++ b/src/main/resources/mappers/TaskMapper.xml @@ -388,7 +388,7 @@ - + - SELECT task_id, + SELECT tt.task_id, task_start_time, task_end_time, tdr.dynamic_rule_id as rule_id, - tst.strategy_template_source_system as source_system, - tst.event_type as event_type, - tdr.log_rule_id, + tt.source_system as source_system, + tt.event_type as event_type, + tdr.bw_sql as bw_sql, INET_NTOA(protect_object_ip) as protect_object_ip, protect_object_port, protect_object_url, protect_object_protocol FROM t_task AS tt LEFT JOIN realtime_protection.t_dynamic_rule tdr on tt.task_id = tdr.dynamic_rule_used_task_id - LEFT JOIN realtime_protection.t_protect_object_dynamic_rule_conn tpodrc - on tdr.dynamic_rule_id = tpodrc.dynamic_rule_id - LEFT JOIN realtime_protection.t_protect_object tpo on tpo.protect_object_id = tpodrc.protect_object_id - LEFT JOIN realtime_protection.t_strategy_template_new tst on tdr.template_id = tst.strategy_template_id - WHERE task_id = #{task_id} + LEFT JOIN realtime_protection.t_task_protectobject_conn tconn + on tt.task_id = tconn.task_id + LEFT JOIN realtime_protection.t_protect_object tpo on tpo.protect_object_id = tconn.protect_object_id +-- LEFT JOIN realtime_protection.t_strategy_template_new tst on tt.template_id = tst.strategy_template_id + WHERE tt.task_id = #{task_id}