Visual Studio Code Cortex XSOAR Extension
The Cortex XSOAR extension for Visual Studio Code enables you to design and author scripts and integrations for Cortex XSOAR directly from VSCode. The extension adds a set of commands, as a sidebar with Automation and Integration Settings, just like the Settings sidebar in the Cortex XSOAR script editor. When writing code, the plugin provides you with auto-completion of Cortex XSOAR and Python functions. The extension also provides an easy-to-use set of demisto-sdk commands to format your packs, lint, and validate. The extension provides an easy virtual environment setup for Cortex XSOAR integrations and scripts.
These instructions cover configuring Visual Studio Code on Mac OS Big Sur (11.6) and creating a test Cortex XSOAR automation with Python 3.9 with Demisto-SDK 1.5.2 to verify the configuration.
Prerequisites
Visual Studio Code installed on Mac
GitHub account
Cortex XSOAR server installed
Review the following Cortex XSOAR development articles:
Installation and Configuration
In GitHub, create your content repository and personal access token.
Fork the public demisto/content to your repository, <your git user root>/content.
In the Mac finder or Terminal shell, create a Python environment.
Install a Docker desktop on Mac using the downloaded .DMG and verify its installation by running the following command.
docker run --rm hello-world
Install Homebrew from a Terminal shell.
/bin/bash -c "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install Python 3.9.7 and Pyenv with Homebrew by running the following commands.
brew update
brew install pyenv
pyenv install 3.9.7
Create a Cortex XSOAR content development directory from a Terminal shell (follow Set up Your Dev Environment).
Install virtualenv.
pip3 install virtualenv
Create a folder on your Mac (in this example, xsoarpython) by running the following commands.
mkdir ../xsoarpython
cd xsoarpython
In the Mac Terminal shell, create a local Cortex XSOAR content repository.
Clone your private GitHub Cortex XSOAR content repository to your local system by running the following commands.
git clone https://github.com/<git user>/content.git
cd content
pyenv local 3.9.7
Install the Demisto SDK and update to the latest version by running the following commands.
.hooks/bootstrap
source ./venv/bin/activate
pip3 install --upgrade demisto-sdk
Edit
.zshrc
(or whatever shell RC file in use) and add the following lines.export DEMISTO_BASE_URL=https://<xsoar IP or name>:<port>
export DEMISTO_API_KEY<XSOAR API Key>
export DEMISTO_SDK_GITHUB_TOKEN<token>
eval "$(pyenv init -)"
On the Mac in Visual Studio Code install extensions and configure them.
Install the Cortex XSOAR and Python extensions.
In the Cortex XSOAR Visual Studio Code extension settings, add the configuration:
xsoar > Demisto-sdk: Python Path /<your path>/xsoarpython/content/venv/bin/python
Restart Visual Studio Code.
Use the Select Interpreter command from the Command Palette and select the pyenv version of python (3.9.7) being used with the content repository.
In the Terminal pane at the bottom, verify the configuration.
Run the command
env
.Verify the following values:
DEMISTO_BASE_URL
DEMISTO_API_KEY
DEMISTO_SDK_GITHUB_TOKEN
Execute/debug a simple Python file and verify the pyenv Python version is used in the Terminal pane.
Execute this Cortex XSOAR command from the Demisto SDK in the Terminal pane:
demisto-sdk run --insecure -q 'ip ip="8.8.8.8"'
Verify the Demisto SDK command succeeds and provides expected output.
Create a Test Automation
On the Mac, create a Test Automation pack and script from the terminal shell by running the following commands.
cd xsoarpython/content
source ./venv/bin/activate
git config --global user.email "<your email name>@<email host>"
git config --global user.name "<your user name>"
git checkout -b test_automation
demisto-sdk init -n TestAutomation
demisto-sdk init --script --id "My Test Automation" -n TestAutomation -o Packs/TestAutomation/Scripts
From the Cortex XSOAR console IDE and Mac Terminal shell:
Copy
CommonServerPython.py
from Cortex XSOAR Automations into the Virtual Studio Code workspace (content/Packs/TestAutomation/Scripts).Copy
CommonServerUserPython.py
from Cortex XSOAR Automations into the Virtual Studio Code workspace, or create an empty file if not used in Cortex XSOAR.Copy
demistomock.py
into Virtual Studio Code workspace (content/demistomock.py at master · demisto/content · GitHub).
In Visual Studio Code:
Use Open Folder..." using the
xsoarpython/content
folder created.Edit
TestAutomation.py
and create a simple automation.From the Command Palette, run the the
XSOAR: Lint
command.Once there are no lint errors, commit the automation to GitHub using the
Git:Commit
command from the Command Palette or via the Source Control icon from the sidebar.
In GitHub:
Verify the
test_automation
branch was created.Verify the
TestAutomation.py
file is in the branch.
Development Documentation
The following are links to useful documentation resources.
Cortex XSOAR Automation Virtual Tour
Cortex XSOAR Training - Write Your First Automation
Cortex XSOAR Common Server Python
Cortex XSOAR Common Scripts Content Pack
Cortex XSOAR Generic Webhook Integration
Cortex XSOAR Custom Filters and Tranformers Operators
Cortex XSOAR Post-Processing Scripts
Add a Script in the Incident Layout
Create a Custom Widget Using an Automation Script
Cortex XSOAR Extension for Visual Studio Code
Third-Party Tools
The following are links to useful tools for developing scripts and troubleshooting specific issues.
Chrome developer tools for capturing HTTPS request/response traffic