From f5c814f23b93b69aafb4e5a4302b813c9454c5e8 Mon Sep 17 00:00:00 2001 From: pandong <100667564+pandong2011@users.noreply.github.com> Date: Tue, 25 Oct 2022 15:11:54 +0800 Subject: [PATCH] [fix#12439] [Alert] fix send script alert NPE (#12495) * [fix#12439] [Alert] fix send script alert NPE --- .../alert/script/ScriptAlertChannel.java | 6 +++-- .../plugin/alert/script/ScriptSender.java | 18 ++++++++++--- .../plugin/alert/script/ScriptSenderTest.java | 27 +++++++++++++++++++ .../alert/AlertSenderService.java | 3 ++- 4 files changed, 48 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 3d865c6dc0..29fb34aa35 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 @@ -22,6 +22,8 @@ import org.apache.dolphinscheduler.alert.api.AlertData; import org.apache.dolphinscheduler.alert.api.AlertInfo; import org.apache.dolphinscheduler.alert.api.AlertResult; +import org.apache.commons.collections.MapUtils; + import java.util.Map; public final class ScriptAlertChannel implements AlertChannel { @@ -30,8 +32,8 @@ public final class ScriptAlertChannel implements AlertChannel { public AlertResult process(AlertInfo alertinfo) { AlertData alertData = alertinfo.getAlertData(); Map paramsMap = alertinfo.getAlertParams(); - if (null == paramsMap) { - return new AlertResult("false", "script params is null"); + if (MapUtils.isEmpty(paramsMap)) { + 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 3f6e690f03..ef7221ea82 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 @@ -18,6 +18,7 @@ package org.apache.dolphinscheduler.plugin.alert.script; import org.apache.dolphinscheduler.alert.api.AlertResult; +import org.apache.dolphinscheduler.spi.utils.StringUtils; import java.io.File; import java.util.Map; @@ -36,9 +37,15 @@ public final class ScriptSender { private final String userParams; ScriptSender(Map 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) { @@ -46,6 +53,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 ad33bbe6a3..33a32919f7 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 @@ -61,4 +61,31 @@ public class ScriptSenderTest { Assertions.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"); + Assertions.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"); + Assertions.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"); + Assertions.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 891a40b437..d0c4b033da 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 @@ -37,6 +37,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.Date; @@ -223,7 +224,7 @@ public final class AlertSenderService extends Thread { Map 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()); }