Write integration code
Cortex XSIAM steps for writing sample integration code.
Once we've finished adding our parameters, command, argument, and outputs, we can write the integration code.
Import Python libraries for the integration
To begin, we have the option to import Python libraries, so that their commands are available for our integration. Every integration runs inside a Docker image, and our standard Docker image includes most of the common packages, such as JSON and collections. In our Yoda Speak integration, we don’t need to import any libraries, as it only uses the BaseClient class, implicitly imported from CommonServerPython.
When working in Visual Studio Code, we recommend importing the following at the top of your code for debugging purposes.
# uncomment the import statements for debugging in PyCharm, VS Code or other IDEs.
# import demistomock as demisto
# from CommonServerPython import * # noqa # pylint: disable=unused-wildcard-import
# from CommonServerUserPython import * # noqaIf you want to use Python libraries that are not included in the standard Cortex XSIAM Docker image, you can create a customized Docker image.
Define the output context key prefix
Set the term Phrase as a prefix for the output context keys.
TRANSLATE_OUTPUT_PREFIX = 'Phrase'Disable insecure request warnings
Next we prevent Python from raising a warning when accessing resources insecurely.
Since we created the insecure parameter that allows the integration to ignore TLS/SSL certificate validation errors, we also need to disable the warning.
Create the integration API client
The Client is an object that communicates with the API. We create a class called Client. When a Client object is created, it instantiates a parent BaseClient using the parameters we have set up (whether to use proxy, whether to allow insecure connections, and the base URL). If the user provided values to the api_key parameter, the Client sets the relevant headers it will use.
In this example, when using the Yoda Speak API with an API key, the API key is passed as a header.
The number of methods our Client class has usually matches the number of commands in our integration. The Yoda Speak integration only has the translation command, so our Client object should have a matching method to the API request which returns its result.
Implement the test_module command
The test_module function is run whenever the Test integration button is clicked in the integration instance settings. The test_module function sends a hard coded preset string (here, it’s I have the high ground) to the Yoda-Speak translate API to test API connectivity and authentication. There are three possible results:
HTTP response code is 200, which means the request is successful. We return the string
okper the convention for a successful test.The request is not successful and the problem is related to authorization:
Authorization Error: make sure API Key is correctly set.The request is not successful for any other reason: The error text is displayed.
Implement the translation command
The translate_command function uses the client that is provided as an argument for the function and it calls translate using the text provided. The client is created outside of the function (in main()). The function performs several steps.
Confirms that there is a non-empty string to translate. If the string input is empty, it raises an exception.
Tells the Client to send the appropriate API call. If the translation fails (for example due to an API rate limit, authentication, or connection error), an exception is raised.
If the translation succeeds, we want to return it to Cortex XSIAM. To do that, we use a class called
CommandResult(which is declared in CSP). We supply it with the following arguments:outputs: We create a dictionary calledoutputswhere both the original text and the translation are stored.outputs_prefix: The first level of the output in the context data. It usually matches the name of the integration or service.raw_response: The argument used to attach the raw response received from the service, which can be useful when debugging unexpected behaviors.outputs_key_field: Since we can run the translation command multiple times, and possibly receive different results for the same string of text, the system needs to know where to update or append each result. In this example we tell the system thatPhrase.Originalis the key that represents the original text we translated, so that the next time the command is run on the same string of text, the translated values will update.readable_output: This is what users see in the War Room when calling the command, so it should be formatted. We can use thetableToMarkdownfunction (from CSP) to turn the JSON into a user-friendly table. We providetableToMarkdownwith both the JSON values and a title for the table.
Implement the integration main function
Everything actually runs within main. We pull in the integration parameters, arguments, and the translate command. The parameters are assigned to variables. Notice that the parameters are the same ones we set up in the integration settings earlier.
When the function runs, the command will be logged for debugging purposes.
We now create a Client using the given parameters. The Client is defined.
There are two possible commands that can be passed to the main function in our integration.
test-module: If the command name istest-module, it means the user has clicked the integration Test button while setting up or editing an integration instance.When returning
ok, the user is shown a greenSuccessmessage. If any value other thanokis returned, an error is displayed. Make sure you return errors that help the user understand what to change in the integration settings in order to fix connection issues.yoda-speak-translate: This is the primary command for our integration and lets us translate strings of text.
There is also an else option. This returns an error if someone tries to run a command that was created in the YAML file but does not exist in the Python (PY) file. For example, if you added a command yoda-interpret in the integration settings, but did not add it to this file, and then tried to run that command, you would see Yoda-interpret is not implemented.
Log and return integration errors
If any errors occur during the execution of our code, show those errors to the user and also return an error.
Run the integration main function
This line tells the system where to start running our code. By convention, we call the main function main.
Last updated
Was this helpful?
