Since the introduction of the Python3 scripting engine in QRadar SOAR, the utility and practicality of the existing Python2 scripting engine has diminished. The reasons to develop and migrate your scripts to Python3 are numerous, including increased package support and a more robust Python environment for your script logic.
But now the imperative to migrate your Python2 scripts is upon us. IBM QRadar SOAR V49 (expected in Q2 2023) will no longer run Python2 scripts. The good news is: converting your existing scripts to Python3 is straightforward. This article will provide guidance on the conversation effort and offer a few hints about where Python2 logic needs to be modified.
Scripts
Thankfully, it's easy to identify which scripts are running Python2. Navigate to the Scripts tab under Customizations Settings. The list view shows which scripts are running Python2.
In most cases, changing the Language setting to Python3 is all that's needed. There are a few language differences to be aware of. But for the most part, your scripts will just run in Python3. In the Conversion Hints section below, I'll present a few language differences to be aware of.
Should become:
from time import time
now = int(time()*1000) # milliseconds
Note the multiplication by 1000 to produce the equivalent epoch value in milliseconds.
Producing a string representation of the current date and time is possible using datetime as follows:
from datetime import datetime
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# '2023-03-24 11:28:34'
Converting a string representation of a date and time to epoch value is possible as follows:
start_time = int(datetime.strptime("2023-03-25 14:00:20", "%Y-%m-%d %H:%M:%S").strftime('%s'))*1000 # epoch in milliseconds
iteritems
The iteritems() method provides the ability to iterate over a dictionary using a syntax such as:
for k, v in results['content'].iteritems():
This syntax should be changed to items() for Python3:
for k, v in results[‘content’].items():
print
The syntax for the print state changed in Python3 to conform to all other functions, using parenthesis to enclose arguments.
This syntax:
print 'something'
Should be changed to:
print('something') # new syntax with parenthesis
The print statement is largely unused in workflow/playbook scripts. In general, if you're using print for debugging, use the log.info() or log.debug() statements instead.
unicode
One of the biggest differences between Python2 and 3 is the way strings and unicode strings are handled. In Python2, one needed to specifically identify unicode strings using the u' ' prefix. And string and unicode variable concatenation was not possible.
In Python3, all strings are unicode. So there's no need to distinguish between unicode strings and normal strings. Luckily, Python3 accepts strings using the u' ' prefix and ignores its interpretation.
However, the unicode function:
unicode_str = unicode(’something’)
will need to be removed:
unicode_str = 'somthing' # removed unicode conversion
Syntax such as this:
if isinstance(value, unicode):
entries.append(json_entry.format(unicode(key), unicode(value)))
should change to:
if isinstance(value, str): # test for string
entries.append(json_entry.format(key,value)) #removed the unicode() conversion
Finally, similar to tests using isinstance(value, unicode)
, tests using isinstance(value, basestring)
will cause errors in Python3 and should also change to isinstance(value, str)
.
Support for Workflows
Although there are no plans to sunset the use of workflows at this time, the SOAR team is shifting focus towards enhancing the breadth and depth of our playbooks and investing more in feature enhancements/capabilities in future releases
Conclusion
Hopefully, this information will assist you in your conversion efforts and ensure your workflows and playbooks continue to run unaffected. Please use the blog's comments section if there are other conversion issues you face so the entire community will benefit.
For additional information regarding the differences between the scripting engines, refer this
documentation.
Regards,
Mark