-
-
Notifications
You must be signed in to change notification settings - Fork 529
Custom Analyzer from the GUI #2524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
f808811
basic_analyzer draft
carellamartina 3c8e2ae
draft
carellamartina 513e105
AnalyzerConfigForm draft
carellamartina 97789c8
Merge branch 'develop' into create_analyzers
carellamartina db29e0b
added PluginEditButton and formik in AnalyzerConfigForm
carellamartina e415e1e
Merge branch 'develop' into create_analyzers
carellamartina 785106b
changes + tests
carellamartina dac4d1d
refactor
carellamartina e7f4399
prettier
carellamartina 90719cb
deepsource
carellamartina ac05845
fix test
carellamartina 4a23d06
fixes
carellamartina 9d6b90e
fix
carellamartina 321882a
fix
carellamartina c7ba857
refactor
carellamartina d0b771f
refactor
carellamartina 84a9407
Merge branch 'develop' into create_analyzers
carellamartina 6f5bf47
updated node version
carellamartina 84242c9
fixes
carellamartina 4b2e8f7
fixes
carellamartina b22c2ae
linter
carellamartina dd8a7f4
fix
carellamartina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
api_app/analyzers_manager/migrations/0123_basic_observable_analyzer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| from django.db import migrations | ||
|
|
||
|
|
||
| def migrate_python_module_pivot(apps, schema_editor): | ||
| PythonModule = apps.get_model("api_app", "PythonModule") | ||
| pm, _ = PythonModule.objects.update_or_create( | ||
| module="basic_observable_analyzer.BasicObservableAnalyzer", | ||
| base_path="api_app.analyzers_manager.observable_analyzers", | ||
| ) | ||
| Parameter = apps.get_model("api_app", "Parameter") | ||
| Parameter.objects.get_or_create( | ||
| name="url", | ||
| type="str", | ||
| python_module=pm, | ||
| is_secret=False, | ||
| required=True, | ||
| defaults={ | ||
| "description": "URL of the instance you want to connect to", | ||
| }, | ||
| ) | ||
| Parameter.objects.get_or_create( | ||
| name="api_key_name", | ||
| type="str", | ||
| python_module=pm, | ||
| is_secret=True, | ||
| required=False, | ||
| defaults={ | ||
| "description": "API key required for authentication", | ||
| }, | ||
| ) | ||
| Parameter.objects.get_or_create( | ||
| name="headers", | ||
| type="dict", | ||
| python_module=pm, | ||
| is_secret=False, | ||
| required=False, | ||
| defaults={ | ||
| "description": "Headers used for the request", | ||
| }, | ||
| ) | ||
| Parameter.objects.get_or_create( | ||
| name="http_method", | ||
| type="str", | ||
| python_module=pm, | ||
| is_secret=False, | ||
| required=True, | ||
| defaults={ | ||
| "description": "HTTP method used for the request", | ||
| }, | ||
| ) | ||
| Parameter.objects.get_or_create( | ||
| name="params", | ||
| type="dict", | ||
| python_module=pm, | ||
| is_secret=False, | ||
| required=False, | ||
| defaults={ | ||
| "description": "Params used for the query string or request payload", | ||
| }, | ||
| ) | ||
| Parameter.objects.get_or_create( | ||
| name="certificate", | ||
| type="str", | ||
| python_module=pm, | ||
| is_secret=True, | ||
| required=False, | ||
| defaults={ | ||
| "description": "Instance SSL certificate (multiline string).", | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| def reverse_migrate_module_pivot(apps, schema_editor): | ||
| PythonModule = apps.get_model("api_app", "PythonModule") | ||
| PythonModule.objects.get( | ||
| module="basic_observable_analyzer.BasicObservableAnalyzer", | ||
| base_path="api_app.analyzers_manager.observable_analyzers", | ||
| ).delete() | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("analyzers_manager", "0122_alter_soft_time_limit"), | ||
| ] | ||
| operations = [ | ||
| migrations.RunPython(migrate_python_module_pivot, reverse_migrate_module_pivot) | ||
| ] |
105 changes: 105 additions & 0 deletions
105
api_app/analyzers_manager/observable_analyzers/basic_observable_analyzer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import base64 | ||
| import logging | ||
| from tempfile import NamedTemporaryFile | ||
|
|
||
| import requests | ||
|
|
||
| from api_app.analyzers_manager.classes import ObservableAnalyzer | ||
| from api_app.analyzers_manager.constants import HTTPMethods | ||
| from api_app.analyzers_manager.exceptions import ( | ||
| AnalyzerConfigurationException, | ||
| AnalyzerRunException, | ||
| ) | ||
| from tests.mock_utils import MockUpResponse, if_mock_connections, patch | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class BasicObservableAnalyzer(ObservableAnalyzer): | ||
| url: str | ||
| headers: dict | ||
| params: dict | ||
| _certificate: str | ||
| _api_key_name: str | ||
| http_method: str = "get" | ||
|
|
||
| @staticmethod | ||
| def _clean_certificate(cert): | ||
| return ( | ||
| cert.replace("-----BEGIN CERTIFICATE-----", "-----BEGIN_CERTIFICATE-----") | ||
| .replace("-----END CERTIFICATE-----", "-----END_CERTIFICATE-----") | ||
| .replace(" ", "\n") | ||
| .replace("-----BEGIN_CERTIFICATE-----", "-----BEGIN CERTIFICATE-----") | ||
| .replace("-----END_CERTIFICATE-----", "-----END CERTIFICATE-----") | ||
| ) | ||
|
|
||
| def update(self) -> bool: | ||
| pass | ||
|
|
||
| def run(self): | ||
| if not hasattr(self, "url"): | ||
| raise AnalyzerConfigurationException("Instance URL is required") | ||
| if self.http_method not in HTTPMethods.values: | ||
| raise AnalyzerConfigurationException("Http method is not valid") | ||
|
|
||
| # replace <observable> placheholder | ||
| for key in self.params.keys(): | ||
| if self.params[key] == "<observable>": | ||
| self.params[key] = self.observable_name | ||
|
|
||
| # optional authentication | ||
| if hasattr(self, "_api_key_name") and self._api_key_name: | ||
| api_key = self._api_key_name | ||
| if ( | ||
| "Authorization" in self.headers.keys() | ||
| and self.headers["Authorization"].split(" ")[0] == "Basic" | ||
| ): | ||
| # the API uses basic auth so we need to base64 encode the auth payload | ||
| api_key = base64.b64encode(self._api_key_name.encode()).decode() | ||
| # replace <api_key> placeholder | ||
| for key in self.headers.keys(): | ||
| self.headers[key] = self.headers[key].replace("<api_key>", api_key) | ||
|
|
||
| # optional certificate | ||
| verify = True # defualt | ||
| if hasattr(self, "_certificate") and self._certificate: | ||
| self.__cert_file = NamedTemporaryFile(mode="w") | ||
| self.__cert_file.write(self._clean_certificate(self._certificate)) | ||
| self.__cert_file.flush() | ||
| verify = self.__cert_file.name | ||
|
|
||
| try: | ||
| if self.http_method == HTTPMethods.GET: | ||
| url = self.url | ||
| if not self.params.keys(): | ||
| url = self.url + self.observable_name | ||
| response = requests.get( | ||
| url, | ||
| params=self.params, | ||
| headers=self.headers, | ||
| verify=verify, | ||
| ) | ||
| else: | ||
| request_method = getattr(requests, self.http_method) | ||
| response = request_method( | ||
| self.url, headers=self.headers, json=self.params, verify=verify | ||
| ) | ||
| response.raise_for_status() | ||
| except requests.RequestException as e: | ||
| raise AnalyzerRunException(e) | ||
|
|
||
| response_json = response.json() | ||
| logger.debug(f"response received: {response_json}") | ||
| return response_json | ||
|
|
||
| @classmethod | ||
| def _monkeypatch(cls): | ||
| patches = [ | ||
| if_mock_connections( | ||
| patch( | ||
| "requests.get", | ||
| return_value=MockUpResponse({}, 200), | ||
| ), | ||
| ) | ||
| ] | ||
| return super()._monkeypatch(patches=patches) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.