Skip to content

Commit

Permalink
Update base_connector
Browse files Browse the repository at this point in the history
  • Loading branch information
zdc217 committed Sep 23, 2024
1 parent 3cdfd63 commit 86dcfc9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.7","3.8","3.9","3.10","3.11"]
python-version: ["3.9","3.10","3.11", "3.12"]


steps:
Expand Down
32 changes: 30 additions & 2 deletions docs/readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Phantom Toolbox is library intended to simplify the development of
Splunk SOAR Applications using modern software development techniques.
This is an alpha release.

This product is supported by the Cybersecurity Development team at the
This product is supported by the Cybersecurity Development team at the
University of Illinois, on a best-effort basis. The expected End-of-Life
and End-of-Support dates of this version are October of 2025, the same as
its primary dependencies: `Python V3.11 <https://peps.python.org/pep-0664/>`_.
Expand All @@ -16,8 +16,36 @@ Installation

The simplest way to install the phantom-toolbox is to use pip.

There can be issues installing with older versions of `setuptools`,
There can be issues installing with older versions of `setuptools`,
so we recommend ensuring setuptools is up to date before installing::

$ pip install --upgrade setuptools
$ pip install phantom-toolbox

Usage
=====

The following `__init__` and `handle_action` boilerplate are required:

```python
class ExampleConnector(BaseConnector, NiceBaseConnector):
def __init__(self):
BaseConnector.__init__(self)
NiceBaseConnector.__init__(
self, phantom.APP_SUCCESS, phantom.APP_ERROR)
def handle_action(self, param):
# handle_action is an abstract method; it MUST be implemented here.
self.nice_handle_action(param)
```

The decorator `@handle` is used to register a handler to process an action:

```python
@handle('add')
def _handle_add(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
return action_result.set_status(phantom.APP_SUCCESS,
f"Sum {self.x + self.y}")
```
5 changes: 4 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ classifiers=
Topic :: Utilities

[options]
python_requires = >=3.7
python_requires = >=3.9
packages = find:
package_dir=
= src
Expand All @@ -44,6 +44,9 @@ where = src

[options.extras_require]
test =
flake8
types-requests
mypy
pytest
coverage
pytest-splunk-soar-connectors @ git+https://github.com/splunk/pytest-splunk-soar-connectors.git
39 changes: 20 additions & 19 deletions src/phtoolbox/app/base_connector.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# -----------------------------------------
# Nice and dry Phantom App
# -----------------------------------------
import inspect

# Phantom App imports
import phantom.app as phantom
from phantom.base_connector import BaseConnector
import inspect


def handle(*action_ids):
Expand All @@ -16,32 +13,36 @@ def decorator(func):
return decorator


class NiceBaseConnector(BaseConnector):

def __init__(self):
super(NiceBaseConnector, self).__init__()
class NiceBaseConnector():

def __init__(self, app_success_code, app_error_code):
self.__version__ = 'UNSET_VERSION - set self.__version__ to emit here'
self.__git_hash__ = 'UNSET_GIT_HASH - set self.__git_hash__ to emit here'
self.__build_time__ = 'UNSET_BUILD_TIME - set self.__build_time__ to emit here'

self.__git_hash__ = \
'UNSET_GIT_HASH - set self.__git_hash__ to emit here'
self.__build_time__ = \
'UNSET_BUILD_TIME - set self.__build_time__ to emit here'

self.actions = {}
for _, method in inspect.getmembers(self):
if hasattr(method, '_handle'):
for action_id in getattr(method, '_handle'):
self.actions[action_id] = method

self._state = None
self._app_success = app_success_code
self._app_error = app_error_code

def handle_action(self, param):
def nice_handle_action(self, param):
"""A function to implement handle_action.
The handle_action method MUST be defined in the child class."""
action_id = self.get_action_identifier()
self.debug_version_info()
self.debug_print("action_id", self.get_action_identifier())

if action_id in self.actions.keys():
return self.actions[action_id](param)

return phantom.APP_ERROR
return self._app_error

def debug_version_info(self):
""" Developers are encouraged to set the following values:
Expand All @@ -55,13 +56,13 @@ def debug_version_info(self):
self.debug_print("Build Time:" + self.__build_time__)

def initialize(self):
# Load the state in initialize, use it to store data
# that needs to be accessed across actions
# Load the state in initialize, use it to store data
# that needs to be accessed across actions
self._state = self.load_state()
return phantom.APP_SUCCESS
return self._app_success

def finalize(self):
# Save the state, this data is saved across actions and
# app upgrades
# Save the state, this data is saved across actions and
# app upgrades
self.save_state(self._state)
return phantom.APP_SUCCESS
return self._app_success
11 changes: 10 additions & 1 deletion tests/calc/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@
# Phantom App imports
import phantom.app as phantom
from phantom.action_result import ActionResult
from phantom.base_connector import BaseConnector

from phtoolbox.app.base_connector import (
NiceBaseConnector,
handle,
)


class CalcConnector(NiceBaseConnector):
class CalcConnector(BaseConnector, NiceBaseConnector):
def __init__(self):
BaseConnector.__init__(self)
NiceBaseConnector.__init__(
self, phantom.APP_SUCCESS, phantom.APP_ERROR)

def handle_action(self, param):
# handle_action is an abstract method; it MUST be implemented here.
self.nice_handle_action(param)

@handle('add')
def _handle_add(self, param):
Expand Down

0 comments on commit 86dcfc9

Please sign in to comment.