IBM Security QRadar SOAR

 View Only
  • 1.  How to Access Workflow Vars From Within Functions

    Posted Tue March 05, 2019 10:07 AM
    Hello All,

    I know how to access workflow variables using workflow.properties notation within pre and post-processing scripts, but how do I access workflow variables within my functions?

    The only way I can see this happening at the moment is for me to set a function-input variable using a pre-processing script that sets the input's value to a workflow.properties.somevar value.  I don't want to have to set up input variables to functions every time I want access to workflow vars.  I just want to be able to access them within downstream functions once a previous function has set them, in a global-like fashion you could say.

    Is there a module I need to import in the code that allows me to do that?

    Thanks,

    -Tony

    --
    Anthony (Tony) Bufort
    Product Integrations Engineer
    DomainTools, LLC

    T: (425) 221-4447
    E: tbufort@domaintools.com
    www.domaintools.com


  • 2.  RE: How to Access Workflow Vars From Within Functions

    Posted Wed March 06, 2019 07:14 AM
    Hi Tony,
     
    Thanks for using our Community Forum!
     
    Today, the workflow.properties variables are not exposed to the Function Code. As you have mentioned, you will need to create a function input for each variable.
     
    You could also make use of this handy function in your pre-process script to allow for an extensible function input:

    # Inside Pre Processing Script
    def dict_to_json_str(d):
      """Function that converts a dictionary into a JSON string.
         Supports types: basestring, unicode, bool, int and nested dicts.
         Does not support lists.
         If the value is None, it sets it to False."""
    
      json_entry = u'"{0}":{1}'
      json_entry_str = u'"{0}":"{1}"'
      entries = []
    
      for entry in d:
        key = entry
        value = d[entry]
    
        if value is None:
          value = False
    
        if isinstance(value, list):
          helper.fail('dict_to_json_str does not support Python Lists')
    
        if isinstance(value, basestring):
          value = value.replace(u'"', u'\\"')
          entries.append(json_entry_str.format(unicode(key), unicode(value)))
    
        elif isinstance(value, unicode):
          entries.append(json_entry.format(unicode(key), unicode(value)))
        
        elif isinstance(value, bool):
          value = 'true' if value == True else 'false'
          entries.append(json_entry.format(key, value))
    
        elif isinstance(value, int):
          entries.append(json_entry.format(unicode(key), value))
    
        elif isinstance(value, dict):
          entries.append(json_entry.format(key, dict_to_json_str(value)))
    
        else:
          helper.fail('dict_to_json_str does not support this type: {0}'.format(type(value)))
    
      return u'{0} {1} {2}'.format(u'{', ','.join(entries), u'}')
     
    inputs.custom_json_str = dict_to_json_str({
      "wf_var_one": workflow.properties.wf_var_one,
      "wf_var_two": workflow.properties.wf_var_two,
      "person": {"id": 1001, "email": "tom@example.com", "active": True}
    })
     
    # Inside Function Component
    import json
    custom_input_dict = json.loads(kwargs.get("custom_json_str"))
    
    if custom_input_dict.get("wf_var_one"):
      print custom_input_dict.get("wf_var_one")
    
    print custom_input_dict["person"]["email"]
    #prints: tom@example.com


    ------------------------------
    Shane Curtin
    Integrations Engineer - IBM Resilient
    ------------------------------



  • 3.  RE: How to Access Workflow Vars From Within Functions

    Posted Wed March 06, 2019 05:30 PM
    Hi Shane,

    Thanks very much for the confirmation, and the script. :)

    I don't suppose there are any plans to expose those workflow.properties variables to function code down the road, eh?  Seems like a lot of weaving in order to establish complex chains of workflow functions.

    -Tony

    ------------------------------
    Anthony Bufort
    Integrations Engineer
    DomainTools
    Renton WA
    14252214447
    ------------------------------



  • 4.  RE: How to Access Workflow Vars From Within Functions

    Posted Fri March 08, 2019 05:52 AM
    Hi Anthony,

    You're very welcome!

    Currently there are no plans, however we do take all feedback into consideration for our future development and we really appreciate all the interaction and ideas we are getting from the Community!

    ------------------------------
    Shane Curtin
    Integrations Engineer - IBM Resilient
    ------------------------------