|
11 | 11 | from timesketch.lib.analyzers import manager
|
12 | 12 |
|
13 | 13 |
|
| 14 | +RE_FLAGS = [ |
| 15 | + 're.ASCII', |
| 16 | + 're.IGNORECASE', |
| 17 | + 're.LOCALE', |
| 18 | + 're.MULTILINE', |
| 19 | + 're.DOTALL', |
| 20 | + 're.VERBOSE', |
| 21 | +] |
| 22 | + |
| 23 | + |
14 | 24 | class FeatureExtractionSketchPlugin(interface.BaseSketchAnalyzer):
|
15 | 25 | """Sketch analyzer for FeatureExtraction."""
|
16 | 26 |
|
17 | 27 | NAME = 'feature_extraction'
|
18 | 28 |
|
19 | 29 | CONFIG_FILE = 'features.yaml'
|
20 | 30 |
|
21 |
| - def __init__(self, index_name, sketch_id): |
| 31 | + FORM_FIELDS = [ |
| 32 | + { |
| 33 | + 'name': 'query_string', |
| 34 | + 'type': 'ts-dynamic-form-text-input', |
| 35 | + 'label': 'The filter query to narrow down the result set', |
| 36 | + 'placeholder': 'Query', |
| 37 | + 'default_value': '' |
| 38 | + }, |
| 39 | + { |
| 40 | + 'name': 'query_dsl', |
| 41 | + 'type': 'ts-dynamic-form-text-input', |
| 42 | + 'label': 'The filter query DSL to narrow down the result', |
| 43 | + 'placeholder': 'Query DSL', |
| 44 | + 'default_value': '' |
| 45 | + }, |
| 46 | + { |
| 47 | + 'name': 'attribute', |
| 48 | + 'type': 'ts-dynamic-form-text-input', |
| 49 | + 'label': 'Name of the field to apply regular expression against', |
| 50 | + 'placeholder': 'Field Name', |
| 51 | + 'default_value': '' |
| 52 | + }, |
| 53 | + { |
| 54 | + 'name': 'store_as', |
| 55 | + 'type': 'ts-dynamic-form-text-input', |
| 56 | + 'label': 'Name of the field to store the extracted results in', |
| 57 | + 'placeholder': 'Store results as field name', |
| 58 | + 'default_value': '' |
| 59 | + }, |
| 60 | + { |
| 61 | + 'name': 're', |
| 62 | + 'type': 'ts-dynamic-form-text-input', |
| 63 | + 'label': 'The regular expression to extract data from field', |
| 64 | + 'placeholder': 'Regular Expression', |
| 65 | + 'default_value': '' |
| 66 | + }, |
| 67 | + { |
| 68 | + 'name': 're_flags', |
| 69 | + 'type': 'ts-dynamic-form-multi-select-input', |
| 70 | + 'label': 'List of flags to pass to the regular expression', |
| 71 | + 'placeholder': 'Regular Expression flags', |
| 72 | + 'default_value': [], |
| 73 | + 'options': RE_FLAGS, |
| 74 | + 'optional': True, |
| 75 | + }, |
| 76 | + { |
| 77 | + 'name': 'emojis', |
| 78 | + 'type': 'ts-dynamic-form-multi-select-input', |
| 79 | + 'label': 'List of emojis to add to events with matches', |
| 80 | + 'placeholder': 'Emojis to add to events', |
| 81 | + 'default_value': [], |
| 82 | + 'options': [x.code for x in emojis.EMOJI_MAP.values()], |
| 83 | + 'options-label': [ |
| 84 | + '{0:s} - {1:s}'.format( |
| 85 | + x, y.help) for x, y in emojis.EMOJI_MAP.items()], |
| 86 | + 'optional': True, |
| 87 | + }, |
| 88 | + { |
| 89 | + 'name': 'tags', |
| 90 | + 'type': 'ts-dynamic-form-text-input', |
| 91 | + 'label': 'Tag to add to events with matches', |
| 92 | + 'placeholder': 'Tag to add to events', |
| 93 | + 'default_value': '', |
| 94 | + 'optional': True, |
| 95 | + }, |
| 96 | + { |
| 97 | + 'name': 'create_view', |
| 98 | + 'type': 'ts-dynamic-form-boolean', |
| 99 | + 'label': 'Should a view be created if there is a match', |
| 100 | + 'placeholder': 'Create a view', |
| 101 | + 'default_value': False, |
| 102 | + 'optional': True, |
| 103 | + }, |
| 104 | + { |
| 105 | + 'name': 'aggregate', |
| 106 | + 'type': 'ts-dynamic-form-boolean', |
| 107 | + 'label': 'Should results be aggregated if there is a match', |
| 108 | + 'placeholder': 'Aggregate results', |
| 109 | + 'default_value': False, |
| 110 | + 'optional': True, |
| 111 | + }, |
| 112 | + ] |
| 113 | + |
| 114 | + |
| 115 | + def __init__(self, index_name, sketch_id, config=None): |
22 | 116 | """Initialize The Sketch Analyzer.
|
23 | 117 |
|
24 | 118 | Args:
|
25 | 119 | index_name: Elasticsearch index name
|
26 | 120 | sketch_id: Sketch ID
|
| 121 | + config: Optional dict that contains the configuration for the |
| 122 | + analyzer. If not provided, the default YAML file will be |
| 123 | + loaded up. |
27 | 124 | """
|
28 | 125 | self.index_name = index_name
|
29 | 126 | super(FeatureExtractionSketchPlugin, self).__init__(
|
30 | 127 | index_name, sketch_id)
|
| 128 | + self._config = config |
31 | 129 |
|
32 | 130 | def run(self):
|
33 | 131 | """Entry point for the analyzer.
|
34 | 132 |
|
35 | 133 | Returns:
|
36 | 134 | String with summary of the analyzer result.
|
37 | 135 | """
|
38 |
| - config = interface.get_yaml_config(self.CONFIG_FILE) |
39 |
| - |
| 136 | + config = self._config or interface.get_yaml_config(self.CONFIG_FILE) |
40 | 137 | if not config:
|
41 | 138 | return 'Unable to parse the config file.'
|
42 | 139 |
|
|
0 commit comments