|
2 | 2 | import time |
3 | 3 | import collections |
4 | 4 |
|
5 | | - |
6 | 5 | from loguru import logger |
7 | 6 | import statsd |
8 | 7 | try: |
|
16 | 15 | import threatingestor.config |
17 | 16 | import threatingestor.state |
18 | 17 | import threatingestor.exceptions |
| 18 | +import threatingestor.whitelist |
19 | 19 |
|
20 | 20 |
|
21 | 21 | class Ingestor: |
@@ -81,11 +81,21 @@ def __init__(self, config_file): |
81 | 81 | self.operators = {name: operator(**kwargs) |
82 | 82 | for name, operator, kwargs in self.config.operators()} |
83 | 83 |
|
| 84 | + logger.debug("Initializing whitelists") |
| 85 | + self.whitelist = threatingestor.whitelist.Whitelist(self.config.whitelists()) |
| 86 | + |
84 | 87 | except (TypeError, ConnectionError, threatingestor.exceptions.PluginError): |
85 | 88 | logger.warning("Twitter config format has recently changed. See https://github.com/InQuest/ThreatIngestor/releases/tag/v1.0.0b5") |
86 | 89 | logger.exception("Error initializing plugins") |
87 | 90 | sys.exit(1) |
88 | 91 |
|
| 92 | + def _is_whitelisted(self, artifact) -> bool: |
| 93 | + if self.whitelist.contains(str(artifact)): |
| 94 | + logger.debug( |
| 95 | + f"Reject {str(artifact)} from further processing because it is whitelisted." |
| 96 | + ) |
| 97 | + return True |
| 98 | + return False |
89 | 99 |
|
90 | 100 | def run(self): |
91 | 101 | """Run once, or forever, depending on config.""" |
@@ -118,6 +128,13 @@ def run_once(self): |
118 | 128 | # Save the source state. |
119 | 129 | self.statedb.save_state(source, saved_state) |
120 | 130 |
|
| 131 | + # Reject whitelisted artifacts |
| 132 | + artifacts = [ |
| 133 | + artifact |
| 134 | + for artifact in artifacts |
| 135 | + if not self._is_whitelisted(artifact) |
| 136 | + ] |
| 137 | + |
121 | 138 | # Process artifacts with each operator. |
122 | 139 | for operator in self.operators: |
123 | 140 | logger.debug(f"Processing {len(artifacts)} artifacts from source '{source}' with operator '{operator}'") |
|
0 commit comments