-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathstackstorm.js
447 lines (367 loc) · 14.1 KB
/
stackstorm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// Licensed to the StackStorm, Inc ('StackStorm') under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Description:
// StackStorm hubot integration
//
// Dependencies:
//
//
// Configuration:
// ST2_API - FQDN + port to StackStorm endpoint
// ST2_ROUTE - StackStorm notification route name
// ST2_COMMANDS_RELOAD_INTERVAL - Reload interval for commands
//
// Notes:
// Command list is automatically generated from StackStorm ChatOps metadata
//
"use strict";
var _ = require('lodash'),
util = require('util'),
env = _.clone(process.env),
Promise = require('rsvp').Promise,
utils = require('../lib/utils.js'),
slack_monkey_patch = require('../lib/slack_monkey_patch.js'),
formatCommand = require('../lib/format_command.js'),
formatData = require('../lib/format_data.js'),
postData = require('../lib/post_data.js'),
CommandFactory = require('../lib/command_factory.js'),
st2client = require('st2client'),
uuid = require('uuid')
;
// Setup the Environment
env.ST2_API = env.ST2_API || 'http://localhost:9101';
env.ST2_ROUTE = env.ST2_ROUTE || null;
env.ST2_WEBUI_URL = env.ST2_WEBUI_URL || null;
// Optional authentication info
env.ST2_AUTH_USERNAME = env.ST2_AUTH_USERNAME || null;
env.ST2_AUTH_PASSWORD = env.ST2_AUTH_PASSWORD || null;
// Optional authentication token
env.ST2_AUTH_TOKEN = env.ST2_AUTH_TOKEN || null;
// Optional API key
env.ST2_API_KEY = env.ST2_API_KEY || null;
// slack attachment colors
env.ST2_SLACK_SUCCESS_COLOR = env.ST2_SLACK_SUCCESS_COLOR || 'dfdfdf';
env.ST2_SLACK_FAIL_COLOR = env.ST2_SLACK_FAIL_COLOR || 'danger';
// Optional, if not provided, we infer it from the API URL
env.ST2_AUTH_URL = env.ST2_AUTH_URL || null;
// Command reload interval in seconds
env.ST2_COMMANDS_RELOAD_INTERVAL = parseInt(env.ST2_COMMANDS_RELOAD_INTERVAL || 120, 10);
// Cap message length to a certain number of characters.
env.ST2_MAX_MESSAGE_LENGTH = parseInt(env.ST2_MAX_MESSAGE_LENGTH || 500, 10);
// Constants
// Fun human-friendly commands. Use %s for payload output.
var START_MESSAGES = [
"I'll take it from here! Your execution ID for reference is %s",
"Got it! Remember %s as your execution ID",
"I'm on it! Your execution ID is %s",
"Let me get right on that. Remember %s as your execution ID",
"Always something with you. :) I'll take care of that. Your ID is %s",
"I have it covered. Your execution ID is %s",
"Let me start up the machine! Your execution ID is %s",
"I'll throw that task in the oven and get cookin'! Your execution ID is %s",
"Want me to take that off your hand? You got it! Don't forget your execution ID: %s",
"River Tam will get it done with her psychic powers. Your execution ID is %s"
];
var ERROR_MESSAGES = [
"I'm sorry, Dave. I'm afraid I can't do that. {~} %s"
];
var TWOFACTOR_MESSAGE = "This action requires two-factor auth! Waiting for your confirmation.";
module.exports = function(robot) {
slack_monkey_patch.patchSendMessage(robot);
var self = this;
var promise = Promise.resolve();
var url = utils.parseUrl(env.ST2_API);
var opts = {
protocol: url.protocol,
host: url.hostname,
port: url.port,
prefix: url.path,
rejectUnauthorized: false
};
var api = st2client(opts);
if (env.ST2_API_KEY) {
api.setKey({ key: env.ST2_API_KEY });
} else if (env.ST2_AUTH_TOKEN) {
api.setToken({ token: env.ST2_AUTH_TOKEN });
}
function authenticate() {
api.removeListener('expiry', authenticate);
// API key gets precedence 1
if (env.ST2_API_KEY) {
robot.logger.info('Using ST2_API_KEY as authentication. Expiry will lead to bot exit.');
return Promise.resolve();
}
// Auth token gets precedence 2
if (env.ST2_AUTH_TOKEN) {
robot.logger.info('Using ST2_AUTH_TOKEN as authentication. Expiry will lead to bot exit.');
return Promise.resolve();
}
robot.logger.info('Requesting a token...');
var url = utils.parseUrl(env.ST2_AUTH_URL);
var client = st2client({
auth: {
protocol: url.protocol,
host: url.hostname,
port: url.port,
prefix: url.path
}
});
return client.authenticate(env.ST2_AUTH_USERNAME, env.ST2_AUTH_PASSWORD)
.then(function (token) {
robot.logger.info('Token received. Expiring ' + token.expiry);
api.setToken(token);
client.on('expiry', authenticate);
})
.catch(function (err) {
robot.logger.error('Failed to authenticate: ' + err.message);
throw err;
});
}
if (env.ST2_API_KEY || env.ST2_AUTH_TOKEN || env.ST2_AUTH_USERNAME || env.ST2_AUTH_PASSWORD) {
// If using username and password then all are required.
if ((env.ST2_AUTH_USERNAME || env.ST2_AUTH_PASSWORD) &&
!(env.ST2_AUTH_USERNAME && env.ST2_AUTH_PASSWORD && env.ST2_AUTH_URL)) {
throw new Error('Env variables ST2_AUTH_USERNAME, ST2_AUTH_PASSWORD and ST2_AUTH_URL should only be used together.');
}
promise = authenticate();
}
// Pending 2-factor auth commands
if (env.HUBOT_2FA) {
var twofactor = {};
robot.logger.info('Two-factor auth is enabled');
}
// factory to manage commands
var command_factory = new CommandFactory(robot);
// formatter to manage per adapter message formatting.
var formatter = formatData.getFormatter(robot.adapterName, robot);
// handler to manage per adapter message post-ing.
var postDataHandler = postData.getDataPostHandler(robot.adapterName, robot, formatter);
var loadCommands = function() {
robot.logger.info('Loading commands....');
api.actionAlias.list()
.then(function (aliases) {
// Remove all the existing commands
command_factory.removeCommands();
_.each(aliases, function (alias) {
var name = alias.name;
var formats = alias.formats;
var description = alias.description;
if (alias.enabled === false) {
return;
}
if (!formats || formats.length === 0) {
robot.logger.error('No formats specified for command: ' + name);
return;
}
_.each(formats, function (format) {
var command = formatCommand(robot.logger, name, format.display || format, description);
command_factory.addCommand(command, name, format.display || format, alias,
format.display ? utils.DISPLAY : false);
_.each(format.representation, function (representation) {
command = formatCommand(robot.logger, name, representation, description);
command_factory.addCommand(command, name, representation, alias, utils.REPRESENTATION);
});
});
});
robot.logger.info(command_factory.st2_hubot_commands.length + ' commands are loaded');
})
.catch(function (err) {
var error_msg = 'Failed to retrieve commands from "%s": %s';
robot.logger.error(util.format(error_msg, env.ST2_API, err.message));
});
};
var sendAck = function (msg, res) {
var history_url = utils.getExecutionHistoryUrl(res.execution);
var history = history_url ? util.format(' (details available at %s)', history_url) : '';
if (res.actionalias && res.actionalias.ack) {
if (res.actionalias.ack.enabled === false) {
return;
} else if (res.actionalias.ack.append_url === false) {
history = '';
}
}
if (res.message) {
return msg.send(res.message + history);
}
var message = util.format(_.sample(START_MESSAGES), res.execution.id);
return msg.send(message + history);
};
var createExecution = function (msg, payload) {
robot.logger.debug('Sending command payload:', JSON.stringify(payload));
api.aliasExecution.create(payload)
.then(function (res) { sendAck(msg, res); })
.catch(function (err) {
// Compatibility with older StackStorm versions
if (err.status === 200) {
return sendAck(msg, { execution: { id: err.message } });
}
robot.logger.error('Failed to create an alias execution:', err);
var addressee = utils.normalizeAddressee(msg, robot.adapterName);
var message = util.format(_.sample(ERROR_MESSAGES), err.message);
if (err.requestId) {
message = util.format(
message,
util.format('; Use request ID %s to grep st2 api logs.', err.requestId));
}
postDataHandler.postData({
whisper: false,
user: addressee.name,
channel: addressee.room,
message: message,
extra: {
color: '#F35A00'
}
});
throw err;
});
};
var executeCommand = function(msg, command_name, format_string, command, action_alias) {
var addressee = utils.normalizeAddressee(msg, robot.adapterName);
var payload = {
'name': command_name,
'format': format_string,
'command': command,
'user': addressee.name,
'source_channel': addressee.room,
'notification_route': env.ST2_ROUTE || 'hubot'
};
if (utils.enable2FA(action_alias)) {
var twofactor_id = uuid.v4();
robot.logger.debug('Requested an action that requires 2FA. Guid: ' + twofactor_id);
msg.send(TWOFACTOR_MESSAGE);
api.executions.create({
'action': env.HUBOT_2FA,
'parameters': {
'uuid': twofactor_id,
'user': addressee.name,
'channel': addressee.room,
'hint': action_alias.description
}
});
twofactor[twofactor_id] = {
'msg': msg,
'payload': payload
};
} else {
createExecution(msg, payload);
}
};
robot.respond(/([\s\S]+?)$/i, function(msg) {
var command, result, command_name, format_string, action_alias;
// Normalize the command and remove special handling provided by the chat service.
// e.g. slack replace quote marks with left double quote which would break behavior.
command = formatter.normalizeCommand(msg.match[1]);
result = command_factory.getMatchingCommand(command);
if (!result) {
if (command.startsWith("help")) {
return;
}
var message = util.format(_.sample(ERROR_MESSAGES), "Command not found: " + command);
return msg.send(message);
}
command_name = result[0];
format_string = result[1];
action_alias = result[2];
executeCommand(msg, command_name, format_string, command, action_alias);
});
robot.router.post('/hubot/st2', function(req, res) {
var data;
try {
if (req.body.payload) {
data = JSON.parse(req.body.payload);
} else {
data = req.body;
}
// Special handler to try and figure out when a hipchat message
// is a whisper:
if (robot.adapterName === 'hipchat' && !data.whisper && data.channel.indexOf('@') > -1 ) {
data.whisper = true;
robot.logger.debug('Set whisper to true for hipchat message');
}
postDataHandler.postData(data);
res.send('{"status": "completed", "msg": "Message posted successfully"}');
} catch (e) {
robot.logger.error("Unable to decode JSON: " + e);
robot.logger.error(e.stack);
res.send('{"status": "failed", "msg": "An error occurred trying to post the message: ' + e + '"}');
}
});
var commands_load_interval;
function start() {
api.stream.listen().catch(function (err) {
robot.logger.error('Unable to connect to stream:', err);
}).then(function (source) {
source.onerror = function (err) {
// TODO: squeeze a little bit more info out of evensource.js
robot.logger.error('Stream error:', err);
};
source.addEventListener('st2.announcement__chatops', function (e) {
var data;
robot.logger.debug('Chatops message received:', e.data);
if (e.data) {
data = JSON.parse(e.data).payload;
} else {
data = e.data;
}
// Special handler to try and figure out when a hipchat message
// is a whisper:
if (robot.adapterName === 'hipchat' && !data.whisper && data.channel.indexOf('@') > -1 ) {
data.whisper = true;
robot.logger.debug('Set whisper to true for hipchat message');
}
postDataHandler.postData(data);
});
if (env.HUBOT_2FA) {
source.addEventListener('st2.announcement__2fa', function (e) {
var data;
robot.logger.debug('Successfull two-factor auth:', e.data);
if (e.data) {
data = JSON.parse(e.data).payload;
} else {
data = e.data;
}
var executionData = twofactor[data.uuid];
createExecution(executionData.msg, executionData.payload);
delete twofactor[data.uuid];
});
}
});
// Add an interval which tries to re-load the commands
commands_load_interval = setInterval(loadCommands.bind(self), (env.ST2_COMMANDS_RELOAD_INTERVAL * 1000));
// Initial command loading
loadCommands();
// Install SIGUSR2 handler which reloads the command
install_sigusr2_handler();
}
function stop() {
clearInterval(commands_load_interval);
api.stream.listen().then(function (source) {
source.removeAllListeners();
source.close();
});
}
function install_sigusr2_handler() {
process.on('SIGUSR2', function() {
loadCommands();
});
}
// Authenticate with StackStorm backend and then call start.
// On a failure to authenticate log the error but do not quit.
return promise.then(function () {
start();
return stop;
});
};