[monitor] 请求监控参数对照监控参数定义校验

This commit is contained in:
tomsun28
2021-11-18 23:07:57 +08:00
parent 119d05c8ab
commit 3ce1009b1f
7 changed files with 287 additions and 4 deletions

View File

@@ -15,6 +15,7 @@ import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_ONLY;
@@ -50,6 +51,7 @@ public class Param {
*/
@ApiModelProperty(value = "参数标识符字段", example = "port", accessMode = READ_WRITE, position = 2)
@Length(max = 100)
@NotNull
private String field;
/**
@@ -57,6 +59,7 @@ public class Param {
*/
@ApiModelProperty(value = "参数值", example = "8080", accessMode = READ_WRITE, position = 3)
@Length(max = 255)
@NotNull
private String value;
/**

View File

@@ -71,7 +71,7 @@ public class ParamDefine {
/**
* 当type为number时,用range表示范围 eg: 0-233
*/
@ApiModelProperty(value = "当type为number时,用range表示范围", example = "0-233", accessMode = READ_WRITE, position = 6)
@ApiModelProperty(value = "当type为number时,用range区间表示范围", example = "[0,233]", accessMode = READ_WRITE, position = 6)
@Column(name = "param_range")
private String range;
@@ -80,7 +80,7 @@ public class ParamDefine {
*/
@ApiModelProperty(value = "当type为text时,用limit表示字符串限制大小.最大255", example = "30", accessMode = READ_WRITE, position = 7)
@Column(name = "param_limit")
private short limit;
private Short limit;
/**
* 当type为radio单选框,checkbox复选框时,option表示可选项值列表

View File

@@ -3,13 +3,17 @@ package com.usthe.manager.service.impl;
import com.usthe.common.entity.job.Configmap;
import com.usthe.common.entity.job.Job;
import com.usthe.common.entity.message.CollectRep;
import com.usthe.common.util.AesUtil;
import com.usthe.common.util.CommonConstants;
import com.usthe.common.util.IntervalExpressionUtil;
import com.usthe.common.util.IpDomainUtil;
import com.usthe.common.util.SnowFlakeIdGenerator;
import com.usthe.manager.dao.MonitorDao;
import com.usthe.manager.dao.ParamDao;
import com.usthe.manager.pojo.dto.MonitorDto;
import com.usthe.manager.pojo.entity.Monitor;
import com.usthe.manager.pojo.entity.Param;
import com.usthe.manager.pojo.entity.ParamDefine;
import com.usthe.manager.service.AppService;
import com.usthe.manager.service.MonitorService;
import com.usthe.manager.support.exception.MonitorDatabaseException;
@@ -21,6 +25,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
@@ -111,7 +116,69 @@ public class MonitorServiceImpl implements MonitorService {
@Override
@Transactional(readOnly = true)
public void validate(MonitorDto monitorDto, boolean isModify) throws IllegalArgumentException {
// 请求监控参数与监控参数定义映射校验匹配
Monitor monitor = monitorDto.getMonitor();
Map<String, Param> paramMap = monitorDto.getParams()
.stream()
.peek(param -> param.setMonitorId(monitor.getId()))
.collect(Collectors.toMap(Param::getField, param -> param));
List<ParamDefine> paramDefines = appService.getAppParamDefines(monitorDto.getMonitor().getApp());
if (paramDefines != null) {
for (ParamDefine paramDefine : paramDefines) {
String field = paramDefine.getField();
Param param = paramMap.get(field);
if (paramDefine.isRequired() && param == null) {
throw new IllegalArgumentException("Params field " + field + " is required.");
}
if (param != null) {
switch (paramDefine.getType()) {
case "number":
double doubleValue;
try {
doubleValue = Double.parseDouble(param.getValue());
} catch (Exception e) {
throw new IllegalArgumentException("Params field " + field + " type "
+ paramDefine.getType() + " is invalid.");
}
if (paramDefine.getRange() != null) {
if (!IntervalExpressionUtil.validNumberIntervalExpress(doubleValue,
paramDefine.getRange())) {
throw new IllegalArgumentException("Params field " + field + " type "
+ paramDefine.getType() + " over range " + paramDefine.getRange());
}
}
break;
case "text":
Short limit = paramDefine.getLimit();
if (limit != null) {
if (param.getValue() != null && param.getValue().length() > limit) {
throw new IllegalArgumentException("Params field " + field + " type "
+ paramDefine.getType() + " over limit " + limit);
}
}
break;
case "host":
String hostValue = param.getValue();
if (!IpDomainUtil.validateIpDomain(hostValue)) {
throw new IllegalArgumentException("Params field " + field + " value "
+ hostValue + " is invalid host value.");
}
break;
case "password":
// 明文密码需加密传输存储
String value = param.getValue();
if (!AesUtil.isCiphertext(value)) {
value = AesUtil.aesEncode(value);
param.setValue(value);
}
break;
// todo 更多参数定义与实际值格式校验
default:
throw new IllegalArgumentException("ParamDefine type " + paramDefine.getType() + " is invalid.");
}
}
}
}
}
@Override

View File

@@ -14,7 +14,7 @@ param:
name: 端口
type: number
# 当type为number时,用range表示范围
range: 0-255
range: '[0,255]'
required: true
- field: username
name: 用户名