From 0757e5af4f25d9b7bc59120acdfa2e26a42a6cdf Mon Sep 17 00:00:00 2001 From: Carpe-Wang <78642589+Carpe-Wang@users.noreply.github.com> Date: Sat, 6 May 2023 09:46:49 +0800 Subject: [PATCH] fix the textarea value judgment error (#931) * fix issue 930 * update value format verification --------- Co-authored-by: Carpe-Wang --- .../alert/calculate/CalculateAlarm.java | 5 ++++- .../hertzbeat/common/util/JsonUtil.java | 8 ++++++- .../service/impl/MonitorServiceImpl.java | 21 +++++++------------ script/sql/schema.sql | 2 +- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/alerter/src/main/java/org/dromara/hertzbeat/alert/calculate/CalculateAlarm.java b/alerter/src/main/java/org/dromara/hertzbeat/alert/calculate/CalculateAlarm.java index e7be25b901f..140f73e1426 100644 --- a/alerter/src/main/java/org/dromara/hertzbeat/alert/calculate/CalculateAlarm.java +++ b/alerter/src/main/java/org/dromara/hertzbeat/alert/calculate/CalculateAlarm.java @@ -21,6 +21,7 @@ import com.googlecode.aviator.Expression; import com.googlecode.aviator.exception.CompileExpressionErrorException; import com.googlecode.aviator.exception.ExpressionRuntimeException; +import com.googlecode.aviator.exception.ExpressionSyntaxErrorException; import org.dromara.hertzbeat.alert.AlerterProperties; import org.dromara.hertzbeat.alert.AlerterWorkerPool; import org.dromara.hertzbeat.common.queue.CommonDataQueue; @@ -163,10 +164,12 @@ private void calculate(CollectRep.MetricsData metricsData) { try { Expression expression = AviatorEvaluator.compile(expr, true); match = (Boolean) expression.execute(fieldValueMap); - } catch (CompileExpressionErrorException compileException) { + } catch (CompileExpressionErrorException | ExpressionSyntaxErrorException compileException) { log.error("Alert Define Rule: {} Compile Error: {}.", expr, compileException.getMessage()); } catch (ExpressionRuntimeException expressionRuntimeException) { log.error("Alert Define Rule: {} Run Error: {}.", expr, expressionRuntimeException.getMessage()); + } catch (Exception e) { + log.error("Alert Define Rule: {} Run Error: {}.", e, e.getMessage()); } if (match != null && match) { diff --git a/common/src/main/java/org/dromara/hertzbeat/common/util/JsonUtil.java b/common/src/main/java/org/dromara/hertzbeat/common/util/JsonUtil.java index cdfb7363e15..d6c02ae17ea 100644 --- a/common/src/main/java/org/dromara/hertzbeat/common/util/JsonUtil.java +++ b/common/src/main/java/org/dromara/hertzbeat/common/util/JsonUtil.java @@ -21,6 +21,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import lombok.extern.slf4j.Slf4j; import org.springframework.util.StringUtils; @@ -39,7 +40,8 @@ public class JsonUtil { static { OBJECT_MAPPER - .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); } public static String toJson(Object source) { @@ -77,6 +79,10 @@ public static T fromJson(String jsonStr, TypeReference type) { return null; } } + + public static T fromJson(String jsonStr) { + return fromJson(jsonStr, new TypeReference<>() {}); + } } diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java index f397bfdb0dd..2afc90499d3 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java @@ -330,18 +330,17 @@ public void validate(MonitorDto monitorDto, Boolean isModify) throws IllegalArgu param.setType(CommonConstants.PARAM_TYPE_NUMBER); break; case "textarea": - if (StringUtils.hasText(param.getValue())) { + Short textareaLimit = paramDefine.getLimit(); + if (textareaLimit != null && param.getValue().length() > textareaLimit) { throw new IllegalArgumentException("Params field " + field + " type " + paramDefine.getType() + " over limit " + param.getValue()); } break; case "text": - Short limit = paramDefine.getLimit(); - if (limit != null) { - if (param.getValue().length() > limit) { - throw new IllegalArgumentException("Params field " + field + " type " - + paramDefine.getType() + " over limit " + limit); - } + Short textLimit = paramDefine.getLimit(); + if (textLimit != null && param.getValue().length() > textLimit) { + throw new IllegalArgumentException("Params field " + field + " type " + + paramDefine.getType() + " over limit " + textLimit); } break; case "host": @@ -364,9 +363,7 @@ public void validate(MonitorDto monitorDto, Boolean isModify) throws IllegalArgu case "boolean": // boolean check String booleanValue = param.getValue(); - try { - Boolean.parseBoolean(booleanValue); - } catch (Exception e) { + if (!"true".equalsIgnoreCase(booleanValue) && !"false".equalsIgnoreCase(booleanValue)) { throw new IllegalArgumentException("Params field " + field + " value " + booleanValue + " is invalid boolean value."); } @@ -405,9 +402,7 @@ public void validate(MonitorDto monitorDto, Boolean isModify) throws IllegalArgu } break; case "key-value": - try { - JsonUtil.toJson(param.getValue()); - } catch (Exception e) { + if (JsonUtil.fromJson(param.getValue()) == null) { throw new IllegalArgumentException("Params field " + field + " value " + param.getValue() + " is invalid key-value value"); } diff --git a/script/sql/schema.sql b/script/sql/schema.sql index e0d58e6ff62..f0e6457ecba 100644 --- a/script/sql/schema.sql +++ b/script/sql/schema.sql @@ -169,7 +169,7 @@ CREATE TABLE hzb_alert_silence match_all boolean not null default true comment '是否应用匹配所有', priorities varchar(100) comment '匹配告警级别,空为全部告警级别', tags varchar(4000) comment '匹配告警信息标签(monitorId:xxx,monitorName:xxx)', - times int not null default 0 comment '已静默告警次数', + times int default 0 comment '已静默告警次数', type tinyint not null default 0 comment '静默类型 0:一次性静默 1:周期性静默', days varchar(100) comment '周期性静默时有效 星期几,多选,全选或空则为每天 7:周日 1:周一 2:周二 3:周三 4:周四 5:周五 6:周六', period_start timestamp comment '静默时间段起始:00:00:00',