Develop your first custom action

Supported in:

In your first custom integration, you defined its parameters and created a Ping action to test the connection. This document guides you through how to create two new actions for your custom integration:

  • Get Domain Details: Retrieves domain information and presents the result in JSON format.
  • Enrich Entities: Enriches entities with domain details.

Create a custom action

To create a custom action, follow these steps:

  1. Go to the Integrated Development Environment (IDE) and click Add to add a new IDE item.
  2. Select Action.
  3. Name the action Get Domain Details and select the integration.
  4. Click Create. The IDE generates a new template with built-in code comments and explanations.

Configure action parameters

Based on the WHOIS XML API documentation, the Get Domain Details action requires two parameters: Check Availability and Domain Name. To configure these parameters, follow these steps:

  1. In the IDE module, click Add.
  2. Fill in the fields for Check Availability and click Save. This parameter indicates whether the domain is available, and its result is used in the automation that you create later.
  3. Fill in the fields for Domain Name and click Save. Use this field to enter the domain that you want the action to check.

Edit the Get Domain Details action

To edit the Get Domain Details action, follow these steps:

  1. Copy the provided code for Get Domain Details, paste it into the IDE, and review the code. The object must use the class's end method to return an output message and a result value:

    siemplify.end(msg, None)
    
  2. Extract the integration and action parameters. Integration parameters, such as API Key, are extracted using the siemplify.extract_configuration_param function. Action parameters, such as Domain Name and Check availability, are extracted using the siemplify.extract_action_param function:

    api_key = siemplify.extract_configuration_param(
        provider_name=INTEGRATION_NAME,
        param_name="API Key",
    )
    url = f"https://www.whoisxmlapi.com/whoisserver/WhoisService?apiKey={api_key}&outputFormat=json"
    domain = siemplify.extract_action_param(
        param_name="Domain Name",
        print_value=True,
    )
    availability_check = siemplify.extract_action_param(
        param_name="Check availability",
        is_mandatory=False,
        print_value=True,
    )
    
  3. Build the request and process the result:

    • Build the request URL based on the boolean value of the availability_check parameter.
    • Send the request to the WHOIS API.
    • Parse the response and add the relevant data to the action's result.
    • Define the output message presented to the user and include the JSON result:
    # Add domain to scan
    url = f"{url}&domainName={domain}"
    
    # Determine availability check
    if availability_check.lower() == "true":
        availability_check_qs = 1
    else:
        availability_check_qs = 0
    
    url = f"{url}&da={availability_check_qs}"
    response = requests.get(url)
    response.raise_for_status()
    
    # Add a JSON result that can be used in the next steps of the playbook
    siemplify.result.add_result_json(response.json())
    
    # Add the JSON to the action result presented in the context details
    siemplify.result.add_json("WhoisDetails", response.json())
    
    msg = f"Fetched data for {domain}"
    siemplify.end(msg, None)
    
    if __name__ == "__main__":
        main()
    

Add a JSON result to the action

As part of the Get Domain Details action, add a JSON sample so that you can extract specific fields in the playbook designer during the Create your first automation procedure:

  1. Copy the JSON from the WHOIS site's JSON example.
  2. In the Details tab of the IDE, enable the Include JSON Result toggle to display the JSON icon at the top of the IDE.
  3. Click file_json Manage JSON Sample > login Import JSON Example.

Test the action

To test the action that you created, follow these steps:

  1. Go to the Testing tab.
  2. In the Scope field, select your Test Case and Integration Instance.
  3. Click slideshow Play in the IDE.
  4. View the action's result in the Testing tab. After the test finishes, you can also review the logs and output in the Debug Output tab.

Create a test case

If you don't have any test cases in your environment, follow these steps to create one:

  1. Go to the Cases page and select a case.
  2. Select Ingest alert as test case. This action creates a new case with a Test label in your case queue.
  3. Return to the IDE and select the test case from the list.

Create an enrichment action

This section explains how to create an enrichment action to add new data to entities. You can view the enriched data in the Entity Explorer. To create an enrichment action, follow these steps:

  1. In the IDE, create a new action and name it Enrich Entities.
  2. Copy and paste the following code into the action:

    from SiemplifyAction import SiemplifyAction
    from SiemplifyDataModel import EntityTypes
    from SiemplifyUtils import output_handler
    import requests
    
    # Example constants
    INTEGRATION_NAME = "My first Integration - Whois XML API"
    SCRIPT_NAME = "WHOIS XML API EnrichEntities"
    
    @output_handler
    def main():
        siemplify = SiemplifyAction()
        siemplify.script_name = SCRIPT_NAME
        siemplify.LOGGER.info("================= Main - Param Init =================")
    
        api_key = siemplify.extract_configuration_param(
            provider_name=INTEGRATION_NAME,
            param_name="API Key",
        )
        url = f"https://www.whoisxmlapi.com/whoisserver/WhoisService?apiKey={api_key}&outputFormat=json"
    
        siemplify.LOGGER.info("----------------- Main - Started -----------------")
    
        # Human-readable message shown in the UI as the action result
        output_message = "output message :"
        # Collect successful entity identifiers
        successful_entities = []
    
        for entity in siemplify.target_entities:
            siemplify.LOGGER.info(f"processing entity {entity.identifier}")
            if (
                entity.entity_type == EntityTypes.HOSTNAME and not entity.is_internal
            ) or entity.entity_type == EntityTypes.URL:
                entity_to_scan = entity.identifier
                scan_url = f"{url}&domainName={entity_to_scan}"
                response = requests.get(scan_url)
                response.raise_for_status()
                register_details = (
                    response.json().get("WhoisRecord", {}).get("registrant", {})
                )
                if register_details:
                    entity.additional_properties.update(register_details)
                    successful_entities.append(entity)
    
        if successful_entities:
            output_message += "\n Successfully processed entities:\n {}".format(
                "\n ".join([x.identifier for x in successful_entities])
            )
            # Send the enriched entity data back to the server
            siemplify.update_entities(successful_entities)
        else:
            output_message += "\n No entities were processed."
    
        result_value = len(successful_entities)
    
        siemplify.LOGGER.info("----------------- Main - Finished -----------------")
        siemplify.end(output_message, result_value)
    
    if __name__ == "__main__":
        main()
    
  3. Extract the configuration parameters. The script extracts the API Key parameter from the integration's configuration to authenticate requests to the WHOIS XML API.

  4. Identify the target entities to process. The script iterates through siemplify.target_entities and processes only the following entity types:

    • Non-internal hostnames
    • URLs
    for entity in siemplify.target_entities:
        siemplify.LOGGER.info(f"processing entity {entity.identifier}")
        if (
            entity.entity_type == EntityTypes.HOSTNAME and not entity.is_internal
        ) or entity.entity_type == EntityTypes.URL:
            entity_to_scan = entity.identifier
    
  5. Scan the domain and define the enrichment step and output message. Because this action runs on an Entity scope, you don't need to configure separate action parameters:

    scan_url = f"{url}&domainName={entity_to_scan}"
    response = requests.get(scan_url)
    response.raise_for_status()
    register_details = (
        response.json().get("WhoisRecord", {}).get("registrant", {})
    )
    if register_details:
        entity.additional_properties.update(register_details)
        successful_entities.append(entity)
    
    if successful_entities:
        output_message += "\n Successfully processed entities:\n {}".format(
            "\n ".join([x.identifier for x in successful_entities])
        )
        # Send the enriched entity data back to the server
        siemplify.update_entities(successful_entities)
    else:
        output_message += "\n No entities were processed."
    
    result_value = len(successful_entities)
    
  6. Enable the action and click Save. You now have a custom integration with the following custom actions:

    • Ping: Tests the connection to the WHOIS XML API product.
    • Get Domain Details: Extracts data about a domain and presents it as a JSON result.
    • Enrich Entities: Adds registrant data to target entities, which you can view in Entity Explorer.

You're now ready to create your first automation using the actions that you customized.

Need more help? Get answers from Community members and Google SecOps professionals.