You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by ch...@apache.org on 2022/11/14 12:20:46 UTC

[dolphinscheduler] branch 3.0.2-prepare updated: [fix#12439] [Alert] fix send script alert NPE (#12495) (#12895)

This is an automated email from the ASF dual-hosted git repository.

chufenggao pushed a commit to branch 3.0.2-prepare
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git


The following commit(s) were added to refs/heads/3.0.2-prepare by this push:
     new 82b1fc9c13 [fix#12439] [Alert] fix send script alert NPE (#12495) (#12895)
82b1fc9c13 is described below

commit 82b1fc9c13362da7ffa0e9f1af12f84bc54425dc
Author: Eric Gao <er...@gmail.com>
AuthorDate: Mon Nov 14 20:20:35 2022 +0800

    [fix#12439] [Alert] fix send script alert NPE (#12495) (#12895)
    
    * [fix#12439] [Alert] fix send script alert NPE
    
    Co-authored-by: pandong <10...@users.noreply.github.com>
---
 .../plugin/alert/script/ScriptAlertChannel.java    |  4 ++--
 .../plugin/alert/script/ScriptSender.java          | 18 ++++++++++++---
 .../plugin/alert/script/ScriptSenderTest.java      | 27 ++++++++++++++++++++++
 .../dolphinscheduler/alert/AlertSenderService.java |  3 ++-
 4 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java
index bd52955b74..affc8f1be9 100644
--- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java
@@ -29,8 +29,8 @@ public final class ScriptAlertChannel implements AlertChannel {
     public AlertResult process(AlertInfo alertinfo) {
         AlertData alertData = alertinfo.getAlertData();
         Map<String, String> paramsMap = alertinfo.getAlertParams();
-        if (null == paramsMap) {
-            return new AlertResult("false", "script params is null");
+        if (paramsMap == null || paramsMap.isEmpty()) {
+            return new AlertResult("false", "script params is empty");
         }
         return new ScriptSender(paramsMap).sendScriptAlert(alertData.getTitle(), alertData.getContent());
     }
diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java
index 7f255803c4..426a6cba0f 100644
--- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java
@@ -20,6 +20,7 @@ package org.apache.dolphinscheduler.plugin.alert.script;
 import org.apache.dolphinscheduler.alert.api.AlertResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.apache.dolphinscheduler.spi.utils.StringUtils;
 
 import java.io.File;
 import java.util.Map;
@@ -34,9 +35,15 @@ public final class ScriptSender {
     private final String userParams;
 
     ScriptSender(Map<String, String> config) {
-        scriptPath = config.get(ScriptParamsConstants.NAME_SCRIPT_PATH);
-        scriptType = config.get(ScriptParamsConstants.NAME_SCRIPT_TYPE);
-        userParams = config.get(ScriptParamsConstants.NAME_SCRIPT_USER_PARAMS);
+        scriptPath = StringUtils.isNotBlank(config.get(ScriptParamsConstants.NAME_SCRIPT_PATH))
+                ? config.get(ScriptParamsConstants.NAME_SCRIPT_PATH)
+                : "";
+        scriptType = StringUtils.isNotBlank(config.get(ScriptParamsConstants.NAME_SCRIPT_TYPE))
+                ? config.get(ScriptParamsConstants.NAME_SCRIPT_TYPE)
+                : "";
+        userParams = StringUtils.isNotBlank(config.get(ScriptParamsConstants.NAME_SCRIPT_USER_PARAMS))
+                ? config.get(ScriptParamsConstants.NAME_SCRIPT_USER_PARAMS)
+                : "";
     }
 
     AlertResult sendScriptAlert(String title, String content) {
@@ -44,6 +51,11 @@ public final class ScriptSender {
         if (ScriptType.SHELL.getDescp().equals(scriptType)) {
             return executeShellScript(title, content);
         }
+        // If it is another type of alarm script can be added here, such as python
+
+        alertResult.setStatus("false");
+        logger.error("script type error: {}", scriptType);
+        alertResult.setMessage("script type error : " + scriptType);
         return alertResult;
     }
 
diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSenderTest.java b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSenderTest.java
index 445d0738b5..d05ffb0b0e 100644
--- a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSenderTest.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSenderTest.java
@@ -53,4 +53,31 @@ public class ScriptSenderTest {
         Assert.assertEquals("false", alertResult.getStatus());
     }
 
+    @Test
+    public void testUserParamsNPE() {
+        scriptConfig.put(ScriptParamsConstants.NAME_SCRIPT_USER_PARAMS, null);
+        ScriptSender scriptSender = new ScriptSender(scriptConfig);
+        AlertResult alertResult;
+        alertResult = scriptSender.sendScriptAlert("test user params NPE", "test content");
+        Assert.assertEquals("true", alertResult.getStatus());
+    }
+
+    @Test
+    public void testPathNPE() {
+        scriptConfig.put(ScriptParamsConstants.NAME_SCRIPT_PATH, null);
+        ScriptSender scriptSender = new ScriptSender(scriptConfig);
+        AlertResult alertResult;
+        alertResult = scriptSender.sendScriptAlert("test path NPE", "test content");
+        Assert.assertEquals("false", alertResult.getStatus());
+    }
+
+    @Test
+    public void testTypeIsError() {
+        scriptConfig.put(ScriptParamsConstants.NAME_SCRIPT_TYPE, null);
+        ScriptSender scriptSender = new ScriptSender(scriptConfig);
+        AlertResult alertResult;
+        alertResult = scriptSender.sendScriptAlert("test type is error", "test content");
+        Assert.assertEquals("false", alertResult.getStatus());
+    }
+
 }
diff --git a/dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/AlertSenderService.java b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/AlertSenderService.java
index ad92862ba9..daf7d42fe5 100644
--- a/dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/AlertSenderService.java
+++ b/dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/AlertSenderService.java
@@ -35,6 +35,7 @@ import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseCommand
 import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseResult;
 
 import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections.MapUtils;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -184,7 +185,7 @@ public final class AlertSenderService extends Thread {
         Map<String, String> paramsMap = JSONUtils.toMap(instance.getPluginInstanceParams());
         String instanceWarnType = WarningType.ALL.getDescp();
 
-        if (paramsMap != null) {
+        if (MapUtils.isNotEmpty(paramsMap)) {
             instanceWarnType = paramsMap.getOrDefault(AlertConstants.NAME_WARNING_TYPE, WarningType.ALL.getDescp());
         }