This is an automated email from the ASF dual-hosted git repository.
zhaoqingran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
The following commit(s) were added to refs/heads/master by this push:
new b2acd530a [bugfix] fix ssl-cert days_remaining and npe (#1934)
b2acd530a is described below
commit b2acd530ab5af6ad74ba94cbe5b97578f8982b19
Author: tomsun28 <[email protected]>
AuthorDate: Wed May 8 19:05:43 2024 +0800
[bugfix] fix ssl-cert days_remaining and npe (#1934)
Signed-off-by: tomsun28 <[email protected]>
Co-authored-by: Logic <[email protected]>
---
.../collect/http/SslCertificateCollectImpl.java | 4 +-
.../hertzbeat/common/util/JexlCommonFunction.java | 10 ++---
.../common/util/JexlExpressionRunner.java | 16 +++++++-
.../org/apache/hertzbeat/common/util/JexlTest.java | 44 ++++++++++++++++++++--
manager/src/main/resources/define/app-ssl_cert.yml | 2 +-
5 files changed, 63 insertions(+), 13 deletions(-)
diff --git
a/collector/src/main/java/org/apache/hertzbeat/collector/collect/http/SslCertificateCollectImpl.java
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/http/SslCertificateCollectImpl.java
index 561c76353..96a4df12b 100644
---
a/collector/src/main/java/org/apache/hertzbeat/collector/collect/http/SslCertificateCollectImpl.java
+++
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/http/SslCertificateCollectImpl.java
@@ -159,8 +159,8 @@ public class SslCertificateCollectImpl extends
AbstractCollect {
}
HttpProtocol httpProtocol = metrics.getHttp();
String url = httpProtocol.getUrl();
- if (StringUtils.hasText(url) || !url.startsWith(RIGHT_DASH)) {
- httpProtocol.setUrl(url == null ? RIGHT_DASH : RIGHT_DASH +
url.trim());
+ if (!StringUtils.hasText(url) || !url.startsWith(RIGHT_DASH)) {
+ httpProtocol.setUrl(StringUtils.hasText(url) ? RIGHT_DASH +
url.trim() : RIGHT_DASH);
}
}
}
diff --git
a/common/src/main/java/org/apache/hertzbeat/common/util/JexlCommonFunction.java
b/common/src/main/java/org/apache/hertzbeat/common/util/JexlCommonFunction.java
index 7ccdced22..bc4b1bcf2 100644
---
a/common/src/main/java/org/apache/hertzbeat/common/util/JexlCommonFunction.java
+++
b/common/src/main/java/org/apache/hertzbeat/common/util/JexlCommonFunction.java
@@ -20,14 +20,11 @@
package org.apache.hertzbeat.common.util;
import java.util.regex.Pattern;
-import lombok.Data;
-import org.apache.commons.lang3.StringUtils;
/**
* the common function for jexl str equals, match, contains, etc.
* sys:now()
*/
-@Data
public class JexlCommonFunction {
/**
@@ -62,10 +59,13 @@ public class JexlCommonFunction {
* @return true if contains
*/
public boolean contains(String left, String right) {
+ if (left == null && right == null) {
+ return true;
+ }
if (left == null || right == null) {
return false;
}
- return StringUtils.containsIgnoreCase(left, right);
+ return left.toLowerCase().contains(right.toLowerCase());
}
@@ -78,7 +78,7 @@ public class JexlCommonFunction {
if (arg == null) {
return false;
}
- return StringUtils.isNotEmpty(String.valueOf(arg));
+ return !String.valueOf(arg).isEmpty();
}
/**
diff --git
a/common/src/main/java/org/apache/hertzbeat/common/util/JexlExpressionRunner.java
b/common/src/main/java/org/apache/hertzbeat/common/util/JexlExpressionRunner.java
index 6d13886ea..273ff46f4 100644
---
a/common/src/main/java/org/apache/hertzbeat/common/util/JexlExpressionRunner.java
+++
b/common/src/main/java/org/apache/hertzbeat/common/util/JexlExpressionRunner.java
@@ -26,6 +26,7 @@ import org.apache.commons.jexl3.JexlBuilder;
import org.apache.commons.jexl3.JexlContext;
import org.apache.commons.jexl3.JexlEngine;
import org.apache.commons.jexl3.JexlExpression;
+import org.apache.commons.jexl3.JexlFeatures;
import org.apache.commons.jexl3.MapContext;
/**
@@ -33,14 +34,25 @@ import org.apache.commons.jexl3.MapContext;
*/
public class JexlExpressionRunner {
+ private static final String LOADER_NAME = "jexl-class-loader";
private static final JexlEngine jexlEngine;
static {
Map<String, Object> functions = Maps.newLinkedHashMap();
// set the root namespace function
functions.put(null, new JexlCommonFunction());
- jexlEngine = new
JexlBuilder().charset(StandardCharsets.UTF_8).cache(256)
- .strict(true).silent(false).namespaces(functions).create();
+ ClassLoader classLoader = new ClassLoader() {
+ @Override
+ public String getName() {
+ return LOADER_NAME;
+ }
+ };
+ JexlFeatures features = new JexlFeatures();
+ features.annotation(false).loops(false).pragma(false)
+
.methodCall(false).lambda(false).newInstance(false).register(false);
+ jexlEngine = new
JexlBuilder().charset(StandardCharsets.UTF_8).cache(256).loader(classLoader)
+
.features(features).strict(true).silent(false).stackOverflow(40).namespaces(functions)
+ .create();
}
public static Object evaluate(String expression, Map<String, Object>
context) {
diff --git
a/common/src/test/java/org/apache/hertzbeat/common/util/JexlTest.java
b/common/src/test/java/org/apache/hertzbeat/common/util/JexlTest.java
index 4cf0f4233..dd0640429 100644
--- a/common/src/test/java/org/apache/hertzbeat/common/util/JexlTest.java
+++ b/common/src/test/java/org/apache/hertzbeat/common/util/JexlTest.java
@@ -27,6 +27,7 @@ import org.apache.commons.jexl3.JexlContext;
import org.apache.commons.jexl3.JexlEngine;
import org.apache.commons.jexl3.JexlException;
import org.apache.commons.jexl3.JexlExpression;
+import org.apache.commons.jexl3.JexlFeatures;
import org.apache.commons.jexl3.MapContext;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@@ -41,11 +42,20 @@ public class JexlTest {
@BeforeEach
void setUp() {
- jexlBuilder = new
JexlBuilder().charset(StandardCharsets.UTF_8).cache(256)
- .strict(true).silent(false);
+ ClassLoader classLoader = new ClassLoader() {
+ @Override
+ public String getName() {
+ return "jexl-class-loader";
+ }
+ };
+ JexlFeatures features = new JexlFeatures();
+ features.annotation(false).loops(false).pragma(false)
+
.methodCall(false).lambda(false).newInstance(false).register(false);
+ jexlBuilder = new
JexlBuilder().charset(StandardCharsets.UTF_8).cache(256).loader(classLoader)
+
.features(features).strict(true).silent(false).stackOverflow(40);
+
}
-
@Test
void testMultiExpression() {
JexlEngine jexl = jexlBuilder.create();
@@ -533,6 +543,21 @@ public class JexlTest {
Assertions.assertTrue(result.endsWith("-0"));
}
+ @Test
+ void testAddString() {
+ JexlEngine jexl = jexlBuilder.create();
+ JexlContext context = new MapContext();
+ context.set("x", "Ubuntu");
+ JexlExpression e = jexl.createExpression("x + \"-00000\"");
+ Object o = e.evaluate(context);
+ String result = (String) o;
+ e = jexl.createExpression("x + '-00000'");
+ Assertions.assertEquals("Ubuntu-00000", result);
+ o = e.evaluate(context);
+ result = (String) o;
+ Assertions.assertEquals("Ubuntu-00000", result);
+ }
+
@Test
void testUnconventionalMapping() {
// database_pages=Database pages
@@ -553,6 +578,19 @@ public class JexlTest {
JexlEngine jexl = jexlBuilder.create();
Assertions.assertThrows(JexlException.class, () ->
jexl.createExpression("new java.util.Date()"));
}
+
+ @Test
+ void testNewObjectException() {
+ JexlEngine jexl = jexlBuilder.create();
+ Assertions.assertThrows(JexlException.class, () ->
jexl.createExpression("new java.lang.StringBuilder()"));
+ }
+
+ @Test
+ void testMethodCallException() {
+ JexlEngine jexl = jexlBuilder.create();
+ Assertions.assertThrows(JexlException.class, () ->
jexl.createExpression("'string'.length()"));
+ Assertions.assertThrows(JexlException.class, () ->
jexl.createExpression("System.currentTimeMillis()"));
+ }
/**
* custom function
diff --git a/manager/src/main/resources/define/app-ssl_cert.yml
b/manager/src/main/resources/define/app-ssl_cert.yml
index 55b47ac37..d48406a67 100644
--- a/manager/src/main/resources/define/app-ssl_cert.yml
+++ b/manager/src/main/resources/define/app-ssl_cert.yml
@@ -133,7 +133,7 @@ metrics:
- end_timestamp
- days_remaining
calculates:
- - days_remaining=math.floor((end_timestamp-now())/86400000) #下取整
+ - days_remaining=(end_timestamp-now())/86400000
# the protocol used for monitoring, eg: sql, ssh, http, telnet, wmi, snmp,
sdk
protocol: ssl_cert
http:
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]