From 9106e02f6c7477b09a80126ff4e0e607a866d544 Mon Sep 17 00:00:00 2001 From: Luis Toledo Date: Fri, 23 Jun 2017 14:15:53 -0400 Subject: [PATCH] Split webhook URL to hide token part --- build.gradle | 6 +-- .../slack/SlackNotificationPlugin.java | 52 ++++++------------- 2 files changed, 20 insertions(+), 38 deletions(-) diff --git a/build.gradle b/build.gradle index e93121e..b0983d3 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'java' sourceCompatibility = 1.6 -project.version = 0.6 +project.version = 1.0 defaultTasks 'clean', 'build' configurations{ @@ -19,7 +19,7 @@ repositories { dependencies { pluginLibs group: 'org.freemarker', name: 'freemarker', version: '2.3.20' - compile(group:'org.rundeck', name: 'rundeck-core', version: '2.1.1') + compile(group:'org.rundeck', name: 'rundeck-core', version: '2.8.2') } // task to copy plugin libs to output/lib dir @@ -32,7 +32,7 @@ jar { //include contents of output dir from "$buildDir/output" manifest { - attributes 'Rundeck-Plugin-Version': '1.1', 'Rundeck-Plugin-Archive': 'true','Rundeck-Plugin-File-Version': "${project.version}" + attributes 'Rundeck-Plugin-Version': '1.2', 'Rundeck-Plugin-Archive': 'true','Rundeck-Plugin-File-Version': "${project.version}" //create space-separated list of pluginLibs def libList = configurations.pluginLibs.collect{'lib/'+it.name}.join(' ') attributes 'Rundeck-Plugin-Classnames': 'com.bitplaces.rundeck.plugins.slack.SlackNotificationPlugin', 'Rundeck-Plugin-Libs': "${libList}" diff --git a/src/main/java/com/bitplaces/rundeck/plugins/slack/SlackNotificationPlugin.java b/src/main/java/com/bitplaces/rundeck/plugins/slack/SlackNotificationPlugin.java index 5e09a6c..e7a9a94 100644 --- a/src/main/java/com/bitplaces/rundeck/plugins/slack/SlackNotificationPlugin.java +++ b/src/main/java/com/bitplaces/rundeck/plugins/slack/SlackNotificationPlugin.java @@ -22,6 +22,7 @@ import com.dtolabs.rundeck.plugins.descriptions.PluginDescription; import com.dtolabs.rundeck.plugins.descriptions.PluginProperty; import com.dtolabs.rundeck.plugins.notification.NotificationPlugin; +import com.dtolabs.rundeck.plugins.descriptions.Password; import java.io.*; import java.net.HttpURLConnection; @@ -51,8 +52,6 @@ public class SlackNotificationPlugin implements NotificationPlugin { private static final String SLACK_MESSAGE_COLOR_YELLOW = "warning"; private static final String SLACK_MESSAGE_COLOR_RED = "danger"; - private static final String SLACK_MESSAGE_FROM_NAME = "Rundeck"; -// private static final String SLACK_EXT_MESSAGE_TEMPLATE_PATH = "/var/lib/rundeck/libext/templates"; private static final String SLACK_MESSAGE_TEMPLATE = "slack-incoming-message.ftl"; private static final String TRIGGER_START = "start"; @@ -63,8 +62,13 @@ public class SlackNotificationPlugin implements NotificationPlugin { private static final Configuration FREEMARKER_CFG = new Configuration(); - @PluginProperty(title = "WebHook URL", description = "Slack Incoming WebHook URL", required = true) - private String webhook_url; + @PluginProperty(title = "WebHook Base URL", description = "Slack Incoming WebHook Base URL", required = true, defaultValue = "https://hooks.slack.com/services") + private String webhook_base_url; + + @Password + @PluginProperty(title = "WebHook Token", description = "WebHook Token, like T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX", required = true) + private String webhook_token; + /** * Sends a message to a Slack room when a job notification event is raised by Rundeck. @@ -79,25 +83,11 @@ public boolean postNotification(String trigger, Map executionData, Map config) { String ACTUAL_SLACK_TEMPLATE; -// if(null != external_template && !external_template.isEmpty()) { -// try { -// FileTemplateLoader externalTemplate = new FileTemplateLoader(new File(SLACK_EXT_MESSAGE_TEMPLATE_PATH)); -// System.err.printf("Found external template directory. Using it.\n"); -// TemplateLoader[] loaders = new TemplateLoader[]{externalTemplate}; -// MultiTemplateLoader mtl = new MultiTemplateLoader(loaders); -// FREEMARKER_CFG.setTemplateLoader(mtl); -// ACTUAL_SLACK_TEMPLATE = external_template; -// } catch (Exception e) { -// System.err.printf("No such directory: %s\n", SLACK_EXT_MESSAGE_TEMPLATE_PATH); -// return false; -// } -// }else{ - ClassTemplateLoader builtInTemplate = new ClassTemplateLoader(SlackNotificationPlugin.class, "/templates"); - TemplateLoader[] loaders = new TemplateLoader[]{builtInTemplate}; - MultiTemplateLoader mtl = new MultiTemplateLoader(loaders); - FREEMARKER_CFG.setTemplateLoader(mtl); - ACTUAL_SLACK_TEMPLATE = SLACK_MESSAGE_TEMPLATE; -// } + ClassTemplateLoader builtInTemplate = new ClassTemplateLoader(SlackNotificationPlugin.class, "/templates"); + TemplateLoader[] loaders = new TemplateLoader[]{builtInTemplate}; + MultiTemplateLoader mtl = new MultiTemplateLoader(loaders); + FREEMARKER_CFG.setTemplateLoader(mtl); + ACTUAL_SLACK_TEMPLATE = SLACK_MESSAGE_TEMPLATE; TRIGGER_NOTIFICATION_DATA.put(TRIGGER_START, new SlackNotificationData(ACTUAL_SLACK_TEMPLATE, SLACK_MESSAGE_COLOR_YELLOW)); TRIGGER_NOTIFICATION_DATA.put(TRIGGER_SUCCESS, new SlackNotificationData(ACTUAL_SLACK_TEMPLATE, SLACK_MESSAGE_COLOR_GREEN)); @@ -113,6 +103,8 @@ public boolean postNotification(String trigger, Map executionData, Map config) { throw new IllegalArgumentException("Unknown trigger type: [" + trigger + "]."); } + String webhook_url=this.webhook_base_url+"/"+this.webhook_token; + String message = generateMessage(trigger, executionData, config); String slackResponse = invokeSlackAPIMethod(webhook_url, message); String ms = "payload=" + URLEncoder.encode(message); @@ -136,13 +128,7 @@ private String generateMessage(String trigger, Map executionData, Map config) { model.put("color", color); model.put("executionData", executionData); model.put("config", config); -// model.put("channel", channel); -// if(username != null && !username.isEmpty()) { -// model.put("username", username); -// } -// if(icon_url != null && !icon_url.isEmpty()) { -// model.put("icon_url", icon_url); -// } + StringWriter sw = new StringWriter(); try { Template template = FREEMARKER_CFG.getTemplate(templateName); @@ -155,8 +141,7 @@ private String generateMessage(String trigger, Map executionData, Map config) { } return sw.toString(); -// String mm = "{\"text\": \"This is posted from rundeck\"}"; -// return urlEncode(mm); + } private String urlEncode(String s) { @@ -167,9 +152,7 @@ private String urlEncode(String s) { } } - // private String invokeSlackAPIMethod(String teamDomain, String token, String message) { private String invokeSlackAPIMethod(String webhook_url, String message) { - // URL requestUrl = toURL(SLACK_API_URL_SCHEMA + teamDomain + SLACK_API_BASE + SLACK_API_WEHOOK_PATH + token); URL requestUrl = toURL(webhook_url); HttpURLConnection connection = null; @@ -208,7 +191,6 @@ private HttpURLConnection openConnection(URL requestUrl) { private void putRequestStream(HttpURLConnection connection, String message) { try { connection.setRequestMethod("POST"); -// connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("charset", "utf-8"); connection.setDoInput(true);