From 2dbae4d2a4c8ee2a81e72aa0739c98cf14d1b41f Mon Sep 17 00:00:00 2001 From: llaszuk-r7 <99184394+llaszuk-r7@users.noreply.github.com> Date: Wed, 5 Jul 2023 14:59:01 +0200 Subject: [PATCH] run black linter, fix unit tests and validators (#1857) --- plugins/armorblox/.CHECKSUM | 6 ++-- plugins/armorblox/help.md | 12 +++---- .../actions/get_remediation_action/action.py | 12 +++---- .../icon_armorblox/connection/connection.py | 8 ++--- .../triggers/get_incidents/schema.py | 32 +++++++++---------- .../triggers/get_incidents/trigger.py | 21 ++++++++---- plugins/armorblox/icon_armorblox/util/api.py | 25 ++++++--------- plugins/armorblox/requirements.txt | 3 +- .../armorblox/unit_test/test_connection.py | 7 ++-- .../unit_test/test_get_remediation_action.py | 20 ++++++------ plugins/armorblox/unit_test/util.py | 8 ++--- 11 files changed, 76 insertions(+), 78 deletions(-) diff --git a/plugins/armorblox/.CHECKSUM b/plugins/armorblox/.CHECKSUM index 3093cfc882..278416fcaa 100644 --- a/plugins/armorblox/.CHECKSUM +++ b/plugins/armorblox/.CHECKSUM @@ -1,5 +1,5 @@ { - "spec": "406fd799fb05d81464ed8cb863ea1f56", + "spec": "cdf4d5d0aa4fb40b0a8d9ee3761ad09b", "manifest": "433b5ba19020a0a518c333be03af4eab", "setup": "67d906e679bb88b8e56aa98bc008d58b", "schemas": [ @@ -13,7 +13,7 @@ }, { "identifier": "get_incidents/schema.py", - "hash": "cc8737d06f9995ac1c317d6e0093cef6" + "hash": "af802290919168df0cbffcb432629043" } ] -} +} \ No newline at end of file diff --git a/plugins/armorblox/help.md b/plugins/armorblox/help.md index 595516dbff..ee9134eaf3 100644 --- a/plugins/armorblox/help.md +++ b/plugins/armorblox/help.md @@ -58,9 +58,9 @@ Example input: ##### Output -|Name|Type|Required|Description| -|----|----|--------|-----------| -|remediation_details|string|True|Remediation action of the requested incident identified by Armorblox| +|Name|Type|Required|Description|Example| +|----|----|--------|-----------|-------| +|remediation_details|string|True|Remediation action of the requested incident identified by Armorblox|ALERT| Example output: ``` @@ -91,9 +91,9 @@ Example input: ##### Output -|Name|Type|Required|Description| -|----|----|--------|-----------| -|incidents|[]incident|True|A list of incidents identified by Armorblox| +|Name|Type|Required|Description|Example| +|----|----|--------|-----------|-------| +|incidents|[]incident|True|A list of incidents identified by Armorblox|{"incidents": "some incidents"}| Example output: diff --git a/plugins/armorblox/icon_armorblox/actions/get_remediation_action/action.py b/plugins/armorblox/icon_armorblox/actions/get_remediation_action/action.py index 7384886e8f..e3f53de97b 100644 --- a/plugins/armorblox/icon_armorblox/actions/get_remediation_action/action.py +++ b/plugins/armorblox/icon_armorblox/actions/get_remediation_action/action.py @@ -1,18 +1,18 @@ import insightconnect_plugin_runtime from .schema import GetRemediationActionInput, GetRemediationActionOutput, Input, Output, Component + # Custom imports below class GetRemediationAction(insightconnect_plugin_runtime.Action): - def __init__(self): super(self.__class__, self).__init__( - name='get_remediation_action', - description=Component.DESCRIPTION, - input=GetRemediationActionInput(), - output=GetRemediationActionOutput()) + name="get_remediation_action", + description=Component.DESCRIPTION, + input=GetRemediationActionInput(), + output=GetRemediationActionOutput(), + ) def run(self, params={}): remediation_details = self.connection.api.get_remediation_action(params.get(Input.INCIDENT_ID)) return {Output.REMEDIATION_DETAILS: remediation_details} - \ No newline at end of file diff --git a/plugins/armorblox/icon_armorblox/connection/connection.py b/plugins/armorblox/icon_armorblox/connection/connection.py index dc28208558..28b2fb1fcc 100644 --- a/plugins/armorblox/icon_armorblox/connection/connection.py +++ b/plugins/armorblox/icon_armorblox/connection/connection.py @@ -1,12 +1,12 @@ import insightconnect_plugin_runtime from .schema import ConnectionSchema, Input from insightconnect_plugin_runtime.exceptions import PluginException, ConnectionTestException + # Custom imports below from icon_armorblox.util.api import ArmorbloxAPI class Connection(insightconnect_plugin_runtime.Connection): - def __init__(self): super(self.__class__, self).__init__(input=ConnectionSchema()) self.api = None @@ -15,13 +15,11 @@ def connect(self, params): self.logger.info("Connect: Connecting...") api_key = params.get(Input.API_KEY, {}).get("secretKey") tenant_name = params.get(Input.TENANT_NAME) - self.api = ArmorbloxAPI(api_key = api_key, tenant_name = tenant_name, logger=self.logger) + self.api = ArmorbloxAPI(api_key=api_key, tenant_name=tenant_name, logger=self.logger) def test(self): try: self.api.test_api() return {"success": True} except PluginException as error: - raise ConnectionTestException( - cause=error.cause, assistance=error.assistance, data=error.data - ) + raise ConnectionTestException(cause=error.cause, assistance=error.assistance, data=error.data) diff --git a/plugins/armorblox/icon_armorblox/triggers/get_incidents/schema.py b/plugins/armorblox/icon_armorblox/triggers/get_incidents/schema.py index a359f7aafb..32008476cf 100644 --- a/plugins/armorblox/icon_armorblox/triggers/get_incidents/schema.py +++ b/plugins/armorblox/icon_armorblox/triggers/get_incidents/schema.py @@ -82,13 +82,13 @@ class GetIncidentsOutput(insightconnect_plugin_runtime.Output): "properties": { "detection_tag_id": { "type": "string", - "title": "Detection tag ID", + "title": "Detection Tag ID", "description": "Detection tag ID", "order": 1 }, "detection_tag_name": { "type": "string", - "title": "Detection tag name", + "title": "Detection Tag name", "description": "Detection tag name", "order": 2 } @@ -120,7 +120,7 @@ class GetIncidentsOutput(insightconnect_plugin_runtime.Output): }, "external_senders": { "type": "array", - "title": "External senders", + "title": "External Senders", "description": "List of external senders", "items": { "type": "string" @@ -129,7 +129,7 @@ class GetIncidentsOutput(insightconnect_plugin_runtime.Output): }, "external_users": { "type": "array", - "title": "External users", + "title": "External Users", "description": "List of external users", "items": { "$ref": "#/definitions/user" @@ -138,7 +138,7 @@ class GetIncidentsOutput(insightconnect_plugin_runtime.Output): }, "final_detection_tags": { "type": "array", - "title": "Detection tags", + "title": "Detection Tags", "description": "Detection tags", "items": { "$ref": "#/definitions/final_detection_tag" @@ -147,7 +147,7 @@ class GetIncidentsOutput(insightconnect_plugin_runtime.Output): }, "folder_categories": { "type": "array", - "title": "Folder categories", + "title": "Folder Categories", "description": "Folder categories", "items": { "type": "string" @@ -174,7 +174,7 @@ class GetIncidentsOutput(insightconnect_plugin_runtime.Output): }, "policy_names": { "type": "array", - "title": "policy_names", + "title": "policy Names", "description": "List of policies", "items": { "type": "string" @@ -198,7 +198,7 @@ class GetIncidentsOutput(insightconnect_plugin_runtime.Output): }, "resolution_state": { "type": "string", - "title": "resolution_state", + "title": "Resolution State", "description": "Incident resolution state", "order": 6 }, @@ -215,13 +215,13 @@ class GetIncidentsOutput(insightconnect_plugin_runtime.Output): }, "title": { "type": "string", - "title": "Subject", + "title": "Title", "description": "Mail subject", "order": 5 }, "users": { "type": "array", - "title": "users", + "title": "Users", "description": "List of users", "items": { "$ref": "#/definitions/user" @@ -254,13 +254,13 @@ class GetIncidentsOutput(insightconnect_plugin_runtime.Output): "properties": { "detection_tag_id": { "type": "string", - "title": "Detection tag ID", + "title": "Detection Tag ID", "description": "Detection tag ID", "order": 1 }, "detection_tag_name": { "type": "string", - "title": "Detection tag name", + "title": "Detection Tag name", "description": "Detection tag name", "order": 2 } @@ -272,7 +272,7 @@ class GetIncidentsOutput(insightconnect_plugin_runtime.Output): "properties": { "email": { "type": "string", - "title": "User email", + "title": "User Email", "description": "Email of the user", "order": 2 }, @@ -284,7 +284,7 @@ class GetIncidentsOutput(insightconnect_plugin_runtime.Output): }, "name": { "type": "string", - "title": "User name", + "title": "User Name", "description": "Name of the user", "order": 1 } @@ -298,7 +298,7 @@ class GetIncidentsOutput(insightconnect_plugin_runtime.Output): "properties": { "email": { "type": "string", - "title": "User email", + "title": "User Email", "description": "Email of the user", "order": 2 }, @@ -310,7 +310,7 @@ class GetIncidentsOutput(insightconnect_plugin_runtime.Output): }, "name": { "type": "string", - "title": "User name", + "title": "User Name", "description": "Name of the user", "order": 1 } diff --git a/plugins/armorblox/icon_armorblox/triggers/get_incidents/trigger.py b/plugins/armorblox/icon_armorblox/triggers/get_incidents/trigger.py index 5e8953cb50..c0005e8100 100644 --- a/plugins/armorblox/icon_armorblox/triggers/get_incidents/trigger.py +++ b/plugins/armorblox/icon_armorblox/triggers/get_incidents/trigger.py @@ -1,25 +1,32 @@ import insightconnect_plugin_runtime import time from datetime import datetime, timedelta -from icon_armorblox.util.constants import ARMORBLOX_INCIDENT_API_TIME_FORMAT, ARMORBLOX_INCIDENT_API_TIME_DELTA_IN_DAYS, DEFAULT_INTERVAL_VALUE +from icon_armorblox.util.constants import ( + ARMORBLOX_INCIDENT_API_TIME_FORMAT, + ARMORBLOX_INCIDENT_API_TIME_DELTA_IN_DAYS, + DEFAULT_INTERVAL_VALUE, +) from .schema import GetIncidentsInput, GetIncidentsOutput, Input, Output, Component + # Custom imports below -class GetIncidents(insightconnect_plugin_runtime.Trigger): +class GetIncidents(insightconnect_plugin_runtime.Trigger): def __init__(self): super(self.__class__, self).__init__( - name='get_incidents', - description=Component.DESCRIPTION, - input=GetIncidentsInput(), - output=GetIncidentsOutput()) + name="get_incidents", + description=Component.DESCRIPTION, + input=GetIncidentsInput(), + output=GetIncidentsOutput(), + ) def run(self, params={}): """Run the trigger""" fetch_interval = params.get(Input.INTERVAL, DEFAULT_INTERVAL_VALUE) # First fetch last_fetch_time = (datetime.utcnow() - timedelta(days={ARMORBLOX_INCIDENT_API_TIME_DELTA_IN_DAYS})).strftime( - {ARMORBLOX_INCIDENT_API_TIME_FORMAT}) + {ARMORBLOX_INCIDENT_API_TIME_FORMAT} + ) while True: current_time = datetime.utcnow().replace(second=0).strftime({ARMORBLOX_INCIDENT_API_TIME_FORMAT}) events = self.connection.api.get_incidents(from_date=last_fetch_time, to_date=current_time) diff --git a/plugins/armorblox/icon_armorblox/util/api.py b/plugins/armorblox/icon_armorblox/util/api.py index 7c3c4e7689..f25ebdffec 100644 --- a/plugins/armorblox/icon_armorblox/util/api.py +++ b/plugins/armorblox/icon_armorblox/util/api.py @@ -6,8 +6,7 @@ class ArmorbloxAPI(Client): - - def __init__(self, api_key: str, tenant_name: str, logger = Logger): + def __init__(self, api_key: str, tenant_name: str, logger=Logger): super().__init__(api_key=api_key, instance_name=tenant_name) self.logger = logger self.incidents_list = [] @@ -15,29 +14,25 @@ def __init__(self, api_key: str, tenant_name: str, logger = Logger): def process_incidents(self, params): self.incidents_list = [] try: - response_json, next_page_token, total_count = self.incidents.list(params=params) + response_json, next_page_token, _ = self.incidents.list(params=params) self.incidents_list.extend(response_json) while next_page_token: params["page_token"] = next_page_token - response_json, next_page_token, total_count = self.incidents.list(params=params) + response_json, next_page_token, _ = self.incidents.list(params=params) self.incidents_list.extend(response_json) except Exception as credentials_exp: - PluginException('Incorrect Credentials. ' + str(credentials_exp)) + PluginException("Incorrect Credentials. " + str(credentials_exp)) def get_incidents(self, from_date: str = None, to_date: str = None): """ Hits the Armorblox API and fetch incidents. - + :param from_date: Custom time filter parameter :param to_date: Custom time filter parameter - + :return: List of incidents """ - params = { - "from_date": from_date, - "to_date": to_date, - "orderBy": "ASC" - } + params = {"from_date": from_date, "to_date": to_date, "orderBy": "ASC"} self.process_incidents(params) return self.incidents_list @@ -46,10 +41,10 @@ def get_remediation_action(self, incident_id): Returns the remediation action(s) for the input incident. """ rm_action_response = self.incidents.get(incident_id) - if 'remediation_actions' in rm_action_response.keys(): - remediation_actions = rm_action_response['remediation_actions'][0] + if "remediation_actions" in rm_action_response.keys(): + remediation_actions = rm_action_response["remediation_actions"][0] else: - remediation_actions = '' + remediation_actions = "" return remediation_actions def test_api(self): diff --git a/plugins/armorblox/requirements.txt b/plugins/armorblox/requirements.txt index 0fba9e45c0..bf0d448e26 100644 --- a/plugins/armorblox/requirements.txt +++ b/plugins/armorblox/requirements.txt @@ -1,4 +1,5 @@ # List third-party dependencies here, separated by newlines. # All dependencies must be version-pinned, eg. requests==1.2.0 # See: https://pip.pypa.io/en/stable/user_guide/#requirements-files -armorblox-sdk==0.1.4 \ No newline at end of file +armorblox-sdk==0.1.4 +parameterized==0.8.1 \ No newline at end of file diff --git a/plugins/armorblox/unit_test/test_connection.py b/plugins/armorblox/unit_test/test_connection.py index 5457b1417f..d5841f5ae7 100644 --- a/plugins/armorblox/unit_test/test_connection.py +++ b/plugins/armorblox/unit_test/test_connection.py @@ -3,9 +3,8 @@ sys.path.append(os.path.abspath("../")) import logging -from unittest import TestCase, mock +from unittest import TestCase -from insightconnect_plugin_runtime.exceptions import ConnectionTestException from icon_armorblox.connection.connection import Connection from icon_armorblox.connection.schema import Input @@ -19,10 +18,10 @@ def setUp(self) -> None: def test_connection_ok(self): self.connection.connect( { - Input.API_KEY: "any-api-key", + Input.API_KEY: {"secretKey": "any-api-key"}, Input.TENANT_NAME: "my-tenant-name", } ) response = self.connection.test() - expected_response = [] + expected_response = {"success": True} self.assertEqual(response, expected_response) diff --git a/plugins/armorblox/unit_test/test_get_remediation_action.py b/plugins/armorblox/unit_test/test_get_remediation_action.py index 0cb324c128..03d4b5ecbe 100644 --- a/plugins/armorblox/unit_test/test_get_remediation_action.py +++ b/plugins/armorblox/unit_test/test_get_remediation_action.py @@ -1,15 +1,14 @@ -import sys import os -sys.path.append(os.path.abspath('../')) +import sys + +from util import Util + +sys.path.append(os.path.abspath("../")) from unittest import TestCase -from icon_armorblox.connection.connection import Connection from icon_armorblox.actions.get_remediation_action import GetRemediationAction -from icon_armorblox.actions.get_remediation_action.schema import Input, Output -import json -import logging -from unit_test.util import Util -from unittest.mock import patch +from icon_armorblox.actions.get_remediation_action.schema import Input +from unittest.mock import patch, Mock from parameterized import parameterized @@ -17,8 +16,9 @@ class TestGetIndicatorDetails(TestCase): def setUp(self) -> None: self.action = Util.default_connector(GetRemediationAction()) - @parameterized.expand([("10597"),("11081"),("11063")]) + + @parameterized.expand([("10597"), ("11081"), ("11063")]) def test_get_remediation_action(self, mock_post: Mock, incident_id: str) -> None: actual = self.action.run({Input.INCIDENT_ID: incident_id}) - expected = {'remediation_details': 'WILL_AUTO_REMEDIATE'} + expected = {"remediation_details": "WILL_AUTO_REMEDIATE"} self.assertEqual(actual, expected) diff --git a/plugins/armorblox/unit_test/util.py b/plugins/armorblox/unit_test/util.py index ea1d01264e..332da99122 100644 --- a/plugins/armorblox/unit_test/util.py +++ b/plugins/armorblox/unit_test/util.py @@ -12,14 +12,13 @@ def default_connector(action, params: object = None): default_connection.logger = logging.getLogger("connection logger") params = { Input.API_KEY: {"api_key": {"secretKey": ""}}, - Input.TENANT_NAME : "tenant_name", + Input.TENANT_NAME: "tenant_name", } default_connection.connect(params) action.connection = default_connection action.logger = logging.getLogger("action logger") return action - @staticmethod def mocked_requests(*args, **kwargs): class MockResponse: @@ -28,12 +27,11 @@ def __init__(self, filename, status_code): self.status_code = status_code def json(self): - f = open(os.path.join( - os.path.dirname(os.path.realpath(__file__)), f"payloads/{self.filename}.json" - )) + f = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), f"payloads/{self.filename}.json")) result = json.load(f) f.close() return result + if args[0] == "https://tenant_name.armorblox.io/api/v1beta1/organizations/tenant_name/incidents/10597": return MockResponse("get_remediation_action", 200) elif args[0] == "https://tenant_name.armorblox.io/api/v1beta1/organizations/tenant_name/incidents/11081":