1. 添加策略模板API文档

This commit is contained in:
EnderByEndera
2024-01-12 19:24:19 +08:00
parent c1a5d2462f
commit 8a719709a3
33 changed files with 450 additions and 222 deletions

View File

@@ -1,18 +1,13 @@
package com.realtime.protection;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class ProtectionApplication {
public static void main(String[] args) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(ProtectionApplication.class);
// 在实际环境中应该修改为prod
builder.application().setAdditionalProfiles("test");
builder.run(args);
SpringApplication.run(ProtectionApplication.class, args);
}
}

View File

@@ -1,7 +1,6 @@
package com.realtime.protection.configuration.entity.task;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Future;
import jakarta.validation.constraints.NotNull;

View File

@@ -9,6 +9,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@@ -27,7 +28,7 @@ public class GlobalExceptionHandler {
}
@Order(3)
@ExceptionHandler(value = Exception.class)
@ExceptionHandler(value = {Exception.class})
public ResponseResult handleGlobalException(Exception e) {
log.error("meets global exception: " + e.getMessage());
return ResponseResult.error().setMessage(e.getMessage());
@@ -35,13 +36,20 @@ public class GlobalExceptionHandler {
@Order(2)
@ExceptionHandler(value = PersistenceException.class)
@ExceptionHandler(value = {PersistenceException.class})
public ResponseResult handleSQLException(PersistenceException e) {
log.error("meets database exception: " + e.getMessage());
return ResponseResult.invalid().setMessage(
"please check the integrity of the data. check if the json data exists in the database");
}
@Order(2)
@ExceptionHandler(value = DuplicateKeyException.class)
public ResponseResult handleDuplicateKeyException(DuplicateKeyException e) {
log.debug("meets duplicate key exception: " + e.getMessage());
return ResponseResult.invalid().setMessage("duplicate key in json data");
}
@Order(2)
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public ResponseResult handleBindException(MethodArgumentNotValidException e) {

View File

@@ -8,7 +8,7 @@ import java.util.LinkedHashMap;
import java.util.Map;
@Data
@Schema(name = "通用返回对象", description = "用于所有接口返回的通用返回对象")
@Schema(description = "用于所有接口返回的通用返回对象")
public class ResponseResult implements Serializable {
@Schema(description = "状态码")

View File

@@ -3,8 +3,6 @@ package com.realtime.protection.configuration.swagger;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Contact;
import io.swagger.v3.oas.annotations.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@OpenAPIDefinition(
info = @Info(

View File

@@ -1,6 +1,5 @@
package com.realtime.protection.server.command;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.realtime.protection.configuration.entity.task.Command;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

View File

@@ -7,7 +7,6 @@ import com.realtime.protection.configuration.entity.task.TaskCommandInfo;
import com.realtime.protection.configuration.exception.DorisStartException;
import com.realtime.protection.configuration.utils.SqlSessionWrapper;
import com.realtime.protection.configuration.utils.enums.StateEnum;
import com.realtime.protection.server.task.TaskMapper;
import com.realtime.protection.server.task.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;

View File

@@ -12,7 +12,6 @@ import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import org.apache.coyote.Response;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@@ -86,7 +85,10 @@ public interface ProtectObjectControllerApi {
)
},
parameters = {
@Parameter()
@Parameter(name = "proobj_name", description = "防护对象名称"),
@Parameter(name = "proobj_id", description = "防护对象ID"),
@Parameter(name = "page", description = "页码", example = "1"),
@Parameter(name = "page_size", description = "每页的对象个数", example = "5")
}
)
ResponseResult queryProtectObjects(@RequestParam(value = "proobj_name", required = false)
@@ -97,19 +99,102 @@ public interface ProtectObjectControllerApi {
@RequestParam("page_size") @Min(1) Integer pageSize);
@GetMapping("/{protectObjectId}/query")
@Operation(
summary = "查询单个防护对象",
description = "根据ID查询单个防护对象的信息",
responses = {
@ApiResponse(
description = "返回单个防护对象信息",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = ResponseResult.class)
)
)
},
parameters = {
@Parameter(name = "protectObjectId", description = "防护对象ID", example = "2")
}
)
ResponseResult queryProtectObject(@PathVariable Integer protectObjectId) throws IllegalAccessException;
@PostMapping("/{protectObjectId}/update")
@Operation(
summary = "更新防护对象",
description = "根据防护对象ID和信息更新防护对象",
responses = {
@ApiResponse(
description = "防护对象更新情况",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = ResponseResult.class)
)
)
},
parameters = {
@Parameter(name = "protectObjectId", description = "防护对象ID", example = "2")
},
requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "防护对象更新信息")
)
ResponseResult updateProtectObject(@PathVariable Integer protectObjectId,
@RequestBody @Valid ProtectObject protectObject);
@DeleteMapping("/{protectObjectId}/delete")
@Operation(
summary = "删除防护对象",
description = "根据防护对象ID删除对应防护对象",
responses = {
@ApiResponse(
description = "防护对象删除情况",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = ResponseResult.class)
)
)
},
parameters = {
@Parameter(name = "protectObjectId", description = "欲删除的防护对象ID", example = "2")
}
)
ResponseResult deleteProtectObject(@PathVariable Integer protectObjectId);
@DeleteMapping("/delete/{protectObjectIds}")
@Operation(
summary = "批量删除防护对象",
description = "根据多个对象ID删除多个防护对象",
responses = {
@ApiResponse(
description = "防护对象删除情况",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = ResponseResult.class)
)
)
},
parameters = {
@Parameter(name = "protectObjectIds", description = "欲删除的多个防护对象ID", example = "1,2,3,4")
}
)
ResponseResult deleteProtectObject(@PathVariable List<Integer> protectObjectIds);
@GetMapping("/{protectObjectId}/audit/{auditStatus}")
@Operation(
summary = "修改防护对象审核状态",
description = "修改指定防护对象ID对应的防护对象的审核状态",
responses = {
@ApiResponse(
description = "防护对象审核状态修改情况",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = ResponseResult.class)
)
)
},
parameters = {
@Parameter(name = "protectObjectId", description = "欲修改的防护对象ID", example = "2"),
@Parameter(name = "auditStatus", description = "欲修改的审核状态", example = "2")
}
)
ResponseResult changeProtectObjectAuditStatus(@PathVariable Integer protectObjectId,
@PathVariable Integer auditStatus);
}

View File

@@ -11,7 +11,7 @@ import java.util.List;
@RestController
@RequestMapping("/template")
public class TemplateController {
public class TemplateController implements TemplateControllerApi {
private final TemplateService templateService;
@@ -19,6 +19,7 @@ public class TemplateController {
this.templateService = templateService;
}
@Override
@PostMapping("/new")
public ResponseResult newTemplate(@RequestBody @Valid Template template) {
@@ -35,6 +36,7 @@ public class TemplateController {
.setData("success", false);
}
@Override
@GetMapping("/query")
public ResponseResult queryTemplates(@RequestParam(value = "template_name", required = false) String templateName,
@RequestParam("page") @Min(1) Integer page,
@@ -45,12 +47,19 @@ public class TemplateController {
.setData("templates", templates);
}
@Override
@GetMapping("/{templateId}/query")
public ResponseResult queryTemplate(@PathVariable Integer templateId) throws IllegalAccessException {
Template template = templateService.queryTemplate(templateId);
if (template == null) {
return ResponseResult.invalid()
.setMessage("invalid templateId, maybe this template doesn't exist?");
}
return ResponseResult.ok()
.setDataMap(EntityUtils.entityToMap(templateService.queryTemplate(templateId)));
.setDataMap(EntityUtils.entityToMap(template));
}
@Override
@PostMapping("/{templateId}/update")
public ResponseResult updateTemplate(@PathVariable @Min(1) Integer templateId,
@RequestBody @Valid Template template) {
@@ -60,6 +69,7 @@ public class TemplateController {
.setData("success", success);
}
@Override
@DeleteMapping("/{templateId}/delete")
public ResponseResult deleteTemplate(@PathVariable @Min(1) Integer templateId) {
return ResponseResult.ok()

View File

@@ -0,0 +1,114 @@
package com.realtime.protection.server.defense.template;
import com.realtime.protection.configuration.entity.defense.template.Template;
import com.realtime.protection.configuration.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import org.springframework.web.bind.annotation.*;
@Tag(name = "防御策略模板API", description = "防御策略模板模块所有接口")
public interface TemplateControllerApi {
@PostMapping("/new")
@Operation(
summary = "新建防御策略模板",
description = "根据信息新建一个防护策略模板",
responses = {
@ApiResponse(
description = "返回新建防御策略模板结果",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = ResponseResult.class)
)
)
},
requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "防御策略模板信息")
)
ResponseResult newTemplate(@RequestBody @Valid Template template);
@GetMapping("/query")
@Operation(
summary = "查询多个防御策略模板",
description = "根据查询条件查询多个防御策略模板",
responses = {
@ApiResponse(
description = "返回所有查询到的防御策略模板",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = ResponseResult.class)
)
)
},
parameters = {
@Parameter(name = "template_name", description = "防御策略模板名称", example = "DDOS"),
@Parameter(name = "page", description = "页码", example = "1"),
@Parameter(name = "page_size", description = "每页对象数量", example = "5")
}
)
ResponseResult queryTemplates(@RequestParam(value = "template_name", required = false) String templateName,
@RequestParam("page") @Min(1) Integer page,
@RequestParam("page_size") @Min(1) Integer pageSize);
@GetMapping("/{templateId}/query")
@Operation(
summary = "查询单个防御策略模板",
description = "根据模板ID查询单个防御策略模板信息",
responses = {
@ApiResponse(
description = "返回单个防御策略模板信息",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = ResponseResult.class)
)
)
},
parameters = {
@Parameter(name = "templateId", description = "防御策略模板ID", example = "5")
}
)
ResponseResult queryTemplate(@PathVariable Integer templateId) throws IllegalAccessException;
@PostMapping("/{templateId}/update")
@Operation(
summary = "更新防御策略模板信息",
description = "根据提供的防御策略模板信息更新指定ID对应的防御策略模板",
responses = {
@ApiResponse(
description = "返回防御策略模板信息更新结果",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = ResponseResult.class)
)
)
},
parameters = {
@Parameter(name = "templateId", description = "防御策略模板ID", example = "5")
}
)
ResponseResult updateTemplate(@PathVariable @Min(1) Integer templateId,
@RequestBody @Valid Template template);
@DeleteMapping("/{templateId}/delete")
@Operation(
summary = "删除防御策略模板信息",
description = "根据策略模板ID删除指定的防御策略模板信息",
responses = {
@ApiResponse(
description = "返回防御策略模板删除结果",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = ResponseResult.class)
)
)
},
parameters = {
@Parameter(name = "templateId", description = "防御策略模板ID", example = "5")
}
)
ResponseResult deleteTemplate(@PathVariable @Min(1) Integer templateId);
}

View File

@@ -93,6 +93,9 @@ public interface TaskControllerApi {
)
)
},
parameters = {
@Parameter(name = "taskId", description = "任务ID")
},
requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
description = "任务信息,必须包含任务原有的或者添加/删除部分后的static_rule_ids和dynamic_rule_ids"
)

View File

@@ -10,7 +10,7 @@ import com.realtime.protection.server.task.status.StateHandler;
public class GeneratingState extends StateHandler implements State {
@Override
public Boolean handle(State newState, CommandService commandService, TaskService taskService, Long taskId) throws DorisStartException {
return switch(StateEnum.getStateEnumByState(newState)) {
return switch (StateEnum.getStateEnumByState(newState)) {
case RUNNING, GENERATING -> true;
case FAILED -> handleFailed(commandService, taskId);
default -> throw new IllegalStateException("Unexpected value: " + StateEnum.getStateEnumByState(newState));

View File

@@ -9,7 +9,7 @@ import com.realtime.protection.server.task.status.StateHandler;
public class RunningState extends StateHandler implements State {
@Override
public Boolean handle(State newState, CommandService commandService, TaskService taskService, Long taskId) {
return switch(StateEnum.getStateEnumByState(newState)) {
return switch (StateEnum.getStateEnumByState(newState)) {
case RUNNING, GENERATING -> true;
case PAUSED -> handlePause(commandService, taskId);
case STOP -> handleStop(commandService, taskId);

View File

@@ -125,9 +125,9 @@ public class WhiteListController {
//查询ip是否存在于白名单
@RequestMapping ("/exist")
public ResponseResult existWhiteListObject(@RequestParam(value = "ruleId", required = true)Integer ruleId,
@RequestParam(value = "ruleType", required = true)Integer ruleType) {
@RequestMapping("/exist")
public ResponseResult existWhiteListObject(@RequestParam(value = "ruleId", required = true) Integer ruleId,
@RequestParam(value = "ruleType", required = true) Integer ruleType) {
//是请求规则的id然后判断这个id所属的ip是否在白名单中吗
//静态应该可以,但动态的,动态是实时过来告警信息,不存储规则? 存的话也行那这里要区分id是静态的还是动态的
//这里先走通静态的要获取规则的源IP和目的IP去白名单select看有没有(有的还有IP掩码,暂未实现)

View File

@@ -14,23 +14,26 @@ public interface WhiteListMapper {
//新建
void newWhiteListObject(@Param("object") WhiteListObject object);
//分页查询
List<WhiteListObject> 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);
List<String> existWhiteListObject(@Param("staticRuleObject")StaticRuleObject staticRuleObject);
List<String> existWhiteListObject(@Param("staticRuleObject") StaticRuleObject staticRuleObject);
Integer queryWhiteListObjectAuditStuatusById(Integer id);
Boolean updateWhiteListObjectAuditStatus(Integer id, Integer status);
void newWhiteListObjects(@Param("whiteListObjects")List<WhiteListObject> whiteListBatch);
void newWhiteListObjects(@Param("whiteListObjects") List<WhiteListObject> whiteListBatch);
void deleteWhiteListObjects(@Param("whiteListIds") List<Integer> whiteListBatch);

View File

@@ -128,6 +128,7 @@ public class WhiteListService {
return whiteListMapper.existWhiteListObject(staticRuleObject);
}
public List<WhiteListObject> whiteListCommandJudge(Command command) {
//参数应该是指令,不管动态静态
// 命中的whitelist列表每一列包含ip port url