Skip to content

Commit

Permalink
Split webhook URL to hide token part
Browse files Browse the repository at this point in the history
  • Loading branch information
ltamaster committed Jun 23, 2017
1 parent 243af84 commit 9106e02
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 38 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply plugin: 'java'
sourceCompatibility = 1.6
project.version = 0.6
project.version = 1.0
defaultTasks 'clean', 'build'

configurations{
Expand All @@ -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
Expand All @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand All @@ -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.
Expand All @@ -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));
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 9106e02

Please sign in to comment.