CheckContextValue

This script checks that a context key exists (and contains data), and optionally checks the value of the context key for a match against an input value. If a regex is not supplied, the script checks that the key is not empty. This script can be used with the "GenericPolling" playbook to poll for field population or that a field contains a specific value. This scripts does not support a context key which holds a list of values.

python · Common Scripts

Source

from CommonServerPython import *


def check_key(field_value, regex=None):
    if regex:
        if re.search(regex, field_value):
            return True
    else:
        if field_value:
            return True
    return False


def poll_field(args: dict[str, Any]) -> CommandResults:
    keys_list = args.get("key", "").split(".")
    regex = args.get("regex")
    ignore_case = argToBoolean(args.get("ignore_case", "False"))

    regex_ignore_case_flag = re.IGNORECASE if ignore_case else 0
    regex = re.compile(regex, regex_ignore_case_flag) if regex else None

    context = dict_safe_get(demisto.context(), keys_list)

    data = {"key": ".".join(keys_list), "exists": False}

    if context:
        data["exists"] = check_key(context, regex)

    command_results = CommandResults(
        outputs_key_field="key",
        outputs_prefix="CheckContextKey",
        outputs=data,
        readable_output="The key exists." if data["exists"] else "The key does not exist.",
        raw_response=data,
    )
    return command_results


def main():
    try:
        return_results(poll_field(demisto.args()))
    except Exception as err:
        demisto.error(traceback.format_exc())  # print the traceback
        return_error(f"Failed to execute CheckFieldValue script. Error: {err!s}")


""" ENTRY POINT """

if __name__ in ("__main__", "__builtin__", "builtins"):
    main()

README

This script checks that a context value exists (and contains data), and optionally checks the value of the key for a match against an input value. This script can be used with the “GenericPolling” playbook to poll for field population or that a field contains a specific value.

Script Data


Name Description
Script Type python3
Tags evaluation, polling
Cortex XSOAR Version 5.0.0

Inputs


Argument Name Description
key The key to check (can contain. ex: key1.key2.key3)
regex The regex pattern to check the field for. (optional).
ignore_case Whether character matching will be case-insensitive. Default is “False”.

Outputs


Path Description Type
CheckContextKey.name Key Name string
CheckContextKey.exists.exists Whether the Key Exists. Unknown