chronicle / soar-sdk

4 stars 2 forks source link

use siemplify SDK locally #1

Open notdodo opened 3 months ago

notdodo commented 3 months ago

Hi!

how to use the soar-sdk for local development?

this will greatly increase the user experience when creating new integrations since the builtin editor lacks of proper syntax formatting and auto-completion.

Jonah-RF commented 1 week ago

Hey @notdodo, I had a similar issue. I managed to "install" it locally by creating a directory in my virtual environment for every python file, and creating an __init__.py for every directory with the contents from .<directory_name> import *. I also added config files from the few that needed them

Here's an example of what part of venv/lib/python3.8/site-packages/ looked like

├── SiemplifyConnectorsDataModel │   ├── init.py │   ├── pycache │   │   ├── init.cpython-38.pyc │   │   └── SiemplifyConnectorsDataModel.cpython-38.pyc │   └── SiemplifyConnectorsDataModel.py ├── SiemplifyDataModel │   ├── init.py │   ├── pycache │   │   ├── init.cpython-38.pyc │   │   └── SiemplifyDataModel.cpython-38.pyc │   └── SiemplifyDataModel.py ├── SiemplifyJob │   ├── init.py │   ├── pycache │   │   ├── init.cpython-38.pyc │   │   └── SiemplifyJob.cpython-38.pyc │   └── SiemplifyJob.py ├── SiemplifyLogger │   ├── init.py │   ├── pycache │   │   ├── init.cpython-38.pyc │   │   └── SiemplifyLogger.cpython-38.pyc │   ├── ScriptingLogging.config │   └── SiemplifyLogger.py

Additionally, below is a python script I wrote to automatically create and configure all of the directories in your python virtual environment. You may need to modify some filepaths depending on where you have the repo/venv installed

import os
import pathlib
import shutil

CONFIG_DICT = {
    'SiemplifyLogger.py': 'ScriptingLogging.config',
    'SiemplifySdkConfig.py': 'sdk_config.ini',
    'OverflowManager.py': 'ConnectorsOverflow.config',
    'Siemplify.py': 'external_providers.json',
}

def main():
    path = pathlib.Path(__file__).parent.resolve()
    sdk_path = os.path.join(path, 'soar-sdk-main')
    venv_path = os.path.join(path, 'venv/lib/python3.8/site-packages')
    for script_name in os.listdir(sdk_path):
        if not script_name.endswith('.py') or script_name == '__init__.py':
            continue
        module_name = script_name.split('.')[0]
        module_path = os.path.join(venv_path, module_name)
        try:
            shutil.rmtree(module_path)
        except FileNotFoundError:
            print('{} not found, so not deleting'.format(module_name))

        os.mkdir(module_path)
        shutil.copy(os.path.join(sdk_path, script_name), os.path.join(module_path, script_name))
        # add __init__.py
        init = 'from .{} import *\n'.format(module_name)
        with open(os.path.join(module_path, '__init__.py'), 'w') as outfile:
            outfile.write(init)

        # add config file if needed
        config_file = CONFIG_DICT.get(script_name)
        if config_file:
            shutil.copy(
                os.path.join(sdk_path, config_file),
                os.path.join(module_path, config_file),
            )

if __name__ == '__main__':
    main()