forked from etsy/411
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.php
More file actions
executable file
·444 lines (398 loc) · 15.1 KB
/
Copy pathSearch.php
File metadata and controls
executable file
·444 lines (398 loc) · 15.1 KB
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
<?php
namespace FOO;
/**
* Class Search
* An Search generates Alert objects which are then passed through the rest
* of the pipeline.
* @package FOO
*/
abstract class Search extends TypeModel {
public static $TYPES = ['Null_Search', 'ES_Search', 'ECL_Search', 'ThreatExchange_Search', 'Ping_Search', 'HTTP_Search', 'Alert_Search', 'Graphite_Search', 'Push_Search'];
public static $TABLE = 'searches';
public static $PKEY = 'search_id';
/** @var bool Whether this Search type has multiple sources. */
public static $SOURCES = false;
public static $CONFIG_KEY = '';
// Categories.
public static $CATEGORIES = [
'general' => 'General',
'server' => 'Server',
'client' => 'Client',
'security' => 'Security',
];
// Threshold for flapping detection.
const FLAP_THRES = 0.4;
// How much weight to give to a change in Search state.
const FLAP_WEIGHT = 0.2;
// Priorities.
/** Low priority. */
const P_LOW = 0;
/** Medium priority. */
const P_MED = 1;
/** High priority. */
const P_HIGH = 2;
/** @var string[] Mapping of priorities to a user-friendly string. */
public static $PRIORITIES = [
self::P_LOW => 'Low',
self::P_MED => 'Medium',
self::P_HIGH => 'High',
];
// Frequency types.
/** Interval based frequency. */
const SCT_FREQ = 0;
/** Cron based frequency. */
const SCT_CRON = 1;
/** @var string[] Mapping of frequency types to a user-friendly string. */
public static $SCHEDULE_TYPES = [
self::SCT_FREQ => 'Frequency',
self::SCT_CRON => 'Cron'
];
// Notification schedules types.
/** No notifications. */
const NT_NONE = 0;
/** As Alerts come in. */
const NT_ONDEMAND = 1;
/** Hourly. */
const NT_HOURLY = 2;
/** Daily. */
const NT_DAILY = 3;
/** @var string[] Mapping of notification schedules to a user-friendly string. */
public static $NOTIF_TYPES = [
self::NT_NONE => 'None',
self::NT_ONDEMAND => 'On demand',
self::NT_HOURLY => 'Hourly',
self::NT_DAILY => 'Daily',
];
// Notification formats.
/** Full format. */
const NF_FULL = 0;
/** Alert content only format. */
const NF_CONTENTONLY = 1;
/** @var string[] Mapping of formats to a user-friendly string. */
public static $NOTIF_FORMATS = [
self::NF_FULL => 'Full',
self::NF_CONTENTONLY => 'Content only',
];
protected static function generateSchema() {
return [
'name' => [self::T_STR, null, ''],
'source' => [self::T_STR, null, ''],
'query_data' => [self::T_OBJ, null, []],
'state_data' => [self::T_OBJ, null, []],
'renderer_data' => [self::T_OBJ, null, []],
'description' => [self::T_STR, null, ''],
'category' => [self::T_ENUM, static::$CATEGORIES, ''],
'tags' => [self::T_ARR, self::T_STR, []],
'priority' => [self::T_ENUM, static::$PRIORITIES, self::P_LOW],
'schedule_type' => [self::T_ENUM, static::$SCHEDULE_TYPES, self::SCT_FREQ],
'frequency' => [self::T_NUM, null, 1],
'cron_expression' => [self::T_STR, null, '* * * * *'],
'range' => [self::T_NUM, null, 1],
'enabled' => [self::T_BOOL, null, true],
'assignee_type' => [self::T_ENUM, Assignee::$TYPES, Assignee::T_USER],
'assignee' => [self::T_NUM, null, User::NONE],
'owner' => [self::T_NUM, null, User::NONE],
'flap_rate' => [self::T_NUM, null, 0],
'notif_type' => [self::T_ENUM, static::$NOTIF_TYPES, self::NT_ONDEMAND],
'notif_format' => [self::T_ENUM, static::$NOTIF_FORMATS, self::NF_FULL],
'notif_data' => [self::T_ARR, null, []],
'autoclose_threshold' => [self::T_NUM, null, 0],
'last_status' => [self::T_STR, null, ''],
'last_execution_date' => [self::T_NUM, null, 0],
'last_success_date' => [self::T_NUM, null, 0],
'last_failure_date' => [self::T_NUM, null, 0],
'last_error_email_date' => [self::T_NUM, null, 0]
];
}
/**
* Creates a new Search of the appropriate type.
* @param string $type The type of the Search.
* @param array $data The attributes for the Search.
* @return Search The new Search.
*/
public static function newSearch($type, $data=null) {
return self::newObject($type, $data);
}
/**
* Return a list of configured data sources for this Search type.
* If the Search type doesn't support multiple sources, this returns null.
* @return string[]|null A list of sources or null.
*/
public static function getSources() {
if(static::$SOURCES) {
return array_keys(Config::get(static::$CONFIG_KEY));
}
return null;
}
public function isAccessible() {
$cfg = Config::get(static::$CONFIG_KEY);
if(static::$SOURCES) {
return count($cfg) > 0;
}
return !static::$CONFIG_KEY || !is_null($cfg);
}
/**
* Returns the config for this Search type.
* @return array Config data.
*/
public function getConfig() {
$cfg = Config::get(static::$CONFIG_KEY, []);
if(static::$SOURCES) {
$cfg = Util::get($cfg, $this->obj['source'], null);
}
return $cfg;
}
protected function serialize(array $data) {
$data['query_data'] = json_encode((object)$data['query_data']);
$data['state_data'] = json_encode((object)$data['state_data']);
$data['renderer_data'] = json_encode((object)$data['renderer_data']);
$data['notif_data'] = json_encode((object)$data['notif_data']);
$data['tags'] = implode(',', array_filter(array_map('trim', $data['tags']), 'strlen'));
$data['enabled'] = (bool)$data['enabled'];
return parent::serialize($data);
}
protected function deserialize(array $data) {
$data['query_data'] = (array)json_decode($data['query_data'], true);
$data['state_data'] = (array)json_decode($data['state_data'], true);
$data['renderer_data'] = (array)json_decode($data['renderer_data'], true);
$data['notif_data'] = (array)json_decode($data['notif_data'], true);
$data['tags'] = array_filter(array_map('trim', explode(',', $data['tags'])), 'strlen');
$data['enabled'] = (bool)$data['enabled'];
return parent::deserialize($data);
}
public function validateData(array $data) {
parent::validateData($data);
if(strlen(trim($data['name'])) == 0) {
throw new ValidationException('Invalid name');
}
switch($data['schedule_type']) {
case self::SCT_FREQ:
if($data['frequency'] < 1) {
throw new ValidationException('Invalid frequency');
}
break;
case self::SCT_CRON:
try {
\Cron\CronExpression::factory($data['cron_expression']);
} catch(\InvalidArgumentException $e) {
throw new ValidationException('Invalid cron expression');
}
break;
}
if($data['range'] < 1) {
throw new ValidationException('Invalid range');
}
$source_expr = trim(Util::get($data['query_data'], 'source_expr', ''));
if(strlen($source_expr) > 0) {
try {
$el = $this->getSELInstance();
$el->parse($source_expr, ['search', 'content', 'start', 'end']);
} catch(\Symfony\Component\ExpressionLanguage\SyntaxError $e) {
throw new ValidationException($e->getMessage());
}
}
foreach($data['tags'] as $tag) {
if(preg_match('/^\w+$/', $tag) === false) {
throw new ValidationException(sprintf('Invalid tag: %s', $tag));
}
}
$sources = static::$SOURCES ? static::getSources():[''];
if(!in_array($data['source'], $sources)) {
throw new ValidationException(sprintf('Invalid source: %s', $data['source']));
}
}
/**
* Retrieves data from lists. If a given list doesn't exist, this method will return an empty list.
* @param string[] $names The array of list names.
* @return array A mapping of list names to its contents.
*/
protected function getListData($names) {
$names = array_unique($names);
$lists = SListFinder::getByQuery(['name' => $names]);
$ret = [];
foreach($lists as $list) {
$ret[$list['name']] = $list->getData();
}
foreach($names as $name) {
if(!Util::exists($ret, $name)) {
$ret[$name] = [];
}
}
return $ret;
}
/**
* Construct the query string with any data (Lists, etc) inserted.
* @return mixed Query data.
*/
abstract protected function constructQuery();
/**
* Wraps _execute to automatically populate several values in alerts. You probably shouldn't override this method.
* @param int $date The current date.
* @return Alert[] An array of Alert instances.
*/
public function execute($date) {
$ret = [];
$constructed_qdata = $this->constructQuery();
foreach($this->_execute($date, $constructed_qdata) as $alert) {
$alert['search_id'] = $this['id'];
$alert['assignee_type'] = $this->obj['assignee_type'];
$alert['assignee'] = $this->obj['assignee'];
$alert['content_hash'] = $this->getContentHash($alert);
$ret[] = $alert;
}
return $ret;
}
/**
* Executes the search.
* @param int $date The current date.
* @param array $constructed_qdata The input data for the Search.
* @return Alert[] An array of Alert instances.
*/
abstract protected function _execute($date, $constructed_qdata);
/**
* Determines if the Search is currently working. Queries the Search source to see if it's up.
* @param int $date The current date.
* @return bool Whether the Search is currently working.
*/
public function isWorking($date) {
return true;
}
/**
* Determines if the Search is time based. Time based Searches are automatically retried when they fail.
* @return bool Whether the Search is time based.
*/
public function isTimeBased() {
return false;
}
/**
* Determines if this search should run at a given timestamp.
* @param int $date The timestamp to test.
* @param bool $backfill Whether we're attempting to backfill this point in time.
* @return bool Whether to execute the search.
*/
public function shouldRun($date, $backfill=false) {
$due = false;
$job = JobFinder::getLastByQuery(['type' => Search_Job::$TYPE, 'target_id' => $this->obj[static::$PKEY]]);
$last_scheduled_date = is_null($job) ? 0:$job['target_date'];
$delta = $date - $last_scheduled_date + 5;
switch($this->obj['schedule_type']) {
case self::SCT_FREQ:
if($backfill) {
$due = ($date / 60) % $this->obj['frequency'] == 0;
} else {
$due = $delta >= $this->obj['frequency'] * 60;
}
break;
case self::SCT_CRON:
$schedule = \Cron\CronExpression::factory($this->obj['cron_expression']);
$due =
$schedule->isDue(new \DateTime("@$date")) &&
$delta >= 10;
break;
}
return $due;
}
/**
* Wraps _getLink to use a default value if nothing is returned.
* @param Alert $alert The Alert object.
* @return string|null An URL with additional data about this Alert.
*/
public function getLink(Alert $alert) {
$source_expr = trim(Util::get($this->obj['query_data'], 'source_expr'));
$ret = null;
if(strlen($source_expr) > 0) {
try {
$el = $this->getSELInstance();
$ret = (string) $el->evaluate($source_expr, [
'search' => $this->toArray(),
'content' => $alert['content'],
'start' => $alert['alert_date'] - $this->obj['range'] * 60,
'end' => $alert['alert_date']
]);
} catch(\Exception $e) {}
}
if(is_null($ret)) {
$ret = $this->_getLink($alert);
}
return $ret;
}
/**
* Return a link for a given search result.
* @param Alert $alert The Alert object.
* @return string|null An URL with additional data about this Alert.
*/
protected function _getLink(Alert $alert) {
return null;
}
/**
* Retrieve an instance of SEL.
* @return ExpressionLanguage The SEL instance.
*/
protected function getSELInstance() {
static $el = null;
if(is_null($el)) {
$stub = function() {};
$func = function() {
$args = func_get_args();
array_shift($args);
return call_user_func_array([$this, 'generateLink'], $args);
};
$el = new ExpressionLanguage();
$el->register('link', [ExpressionLanguage::class, 'compileStub'], $func);
}
return $el;
}
/**
* Hashes the contents of an Alert and returns the hash.
* @param Alert $alert The Alert object.
* @return string A hash of the contents.
*/
public function getContentHash(Alert $alert) {
return hash('sha256', json_encode((object)$alert['content']));
}
/**
* Return the set of Filters for this Search.
* @return Filter[] An array of Filter objects.
*/
public function getFilters() {
return Hook::call('search.filters', [FilterFinder::getBySearch($this->obj[static::$PKEY])])[0];
}
/**
* Return the set of Targets for this Search.
* @return Target[] An array of Target objects.
*/
public function getTargets() {
$targets = TargetFinder::getBySearch($this->obj[static::$PKEY]);
return Hook::call('search.targets', [$targets])[0];
}
/**
* Get emails associated with this Search.
* @return string[] A list of emails.
*/
public function getEmails() {
$emails = Assignee::getEmails($this->obj['assignee_type'], $this->obj['assignee']);
$owner = UserFinder::getById($this->obj['owner']);
if(!is_null($owner)) {
$emails[] = $owner['email'];
}
return array_unique($emails);
}
}
/**
* Class SearchFinder
* Finder for Searches.
* @package FOO
* @method static Search getById(int $id, bool $archived=false)
* @method static Search[] getAll()
* @method static Search[] getByQuery(array $query=[], $count=null, $offset=null, $sort=[], $reverse=null);
* @method static Search[] hydrateModels($objs)
*/
class SearchFinder extends TypeModelFinder {
public static $MODEL = 'Search';
}
/**
* Class SearchException
* Thrown where there is an exception running the Search.
* @package FOO
*/
class SearchException extends \Exception {}