Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
368 lines (298 sloc) 16.6 KB

Batch

Important

The Batch module requires ThreatConnect version 5.6 or higher.

The ThreatConnect |trade| TcEx Framework provides the :py:mod:`~tcex.tcex_batch_v2.TcExBatch` module to create, delete, and update both Groups and Indicators in the ThreatConnect Platform. The App developer can dynamically build a data objects and the Batch module will handle writing the data to the ThreatConnect API.

External ID (xid)

The batch JSON data requires a xid value for all Groups and Indicators. The XID is used internally in ThreatConnect for associations and for updating existing Groups and must be unique for the entire instance of ThreatConnect.

The :py:mod:`~tcex.tcex_batch_v2.TcExBatch` module provides the :py:meth:`~tcex.tcex_batch_v2.TcExBatch.generate_xid` method to assist in generating 2 types of xid. The first type of xid is a unique xid based on UUID4. No input is required to produce a unique xid. The second type is a unique and reproducible xid value. This is the preferred xid type as it allows for the same unique xid to be generated on subsequent runs of the App. To generate a unique and reproducible xid either a string or array of value can be passed to the method (e.g., myapp-adversary-5 or ['myapp', 'adversary', '222']).

For Interface 1 and 2 the xid is optional, if not provided or set to True a unique xid based off the group "type-name" or indicator "type-value" will be auto-generated. If xid is set to False a random xid will be generated. A string value can also be passed if the xid is a known value (e.g., the id field from an remote source). Passing in an xid when possible is best practice and allows Groups and Indicators to be easily updated.

Important

For Groups when using a unique xid value generated using "type-name" support of duplicate group name is not possible. If having duplicate group names is a requirement then a xid should be provided for each group.

Note

In all of the examples below, the code to create the content is removed. You can read more about how to actually create the content in the submit section.

Groups

There are three interfaces to add Group Threat Intelligence data to the Batch Module.

Group Interface 1

The first interface is for type specific access. This interface allows for passing all the data in the method call or only the required fields with optional fields being set via property setters. All metadata (e.g., Attributes, Security Labels, and Tags) can be added to the group instance directly.

The example below passes all supported fields to adversary().

The example below passes only the required fields to document(). The optional properties can then be set whenever required. The same interface is used for the attribute. The required attribute properties are set first and then optional values can be added.

Note

The optional batch.save() method will write the group or indicator to disk. When processing large amounts of data this is the preferred method in order to save on the memory usage of the App.

Important

The file_content parameter for documents and reports will accept multiple types of data as well as a callback method. The callback method will be passed the xid of the documents/report and expects a single response containing the contents of the file. If loading a large number of documents it is best practice to not load the contents in memory, but instead use the callback method so that the files can be processed one at a time.

Group Interface 2

The second more dynamic interface uses the more generic :py:meth:`~tcex.tcex_batch_v2.TcExBatch.group` method. In this interface the group type, group name and optional xid are the only allowed fields. For type specific field such as eventDate for an Event Group the :py:meth:`~tcex.tcex_batch_v2.Group.add_key_value` method is available. The field name must be exactly what the batch API expects (which are listed here). Adding metadata behaves the same as in Interface 1.

The code below demonstrates how to create a Document using this interface (and the same principle applies for Reports and any other groups to which file contents can be added):

Group Interface 3

The third interface accepts the raw data formatted as a dictionary. This method requires that an xid be provided. All metadata should be included with in the JSON object. You can view the required fields for each group type here.

Indicators

There are three interfaces to add Indicator Threat Intelligence data to the Batch Module.

Indicator Interface 1

The first interface is for type specific access. This interface allows for passing all the data in the method call or only the required fields with optional fields being set via property setters. All metadata (e.g., Attributes, Security Labels, and Tags) can be added to the indicator instance directly.

Indicator Interface 2

The second more dynamic interface uses the more generic :py:meth:`~tcex.tcex_batch_v2.TcExBatch.indicator` method. In this interface the indicator type, indicator value, optional rating, optional confidence, and optional xid are the only allowed fields. For type specific field such as size for a File indicator the :py:meth:`~tcex.tcex_batch_v2.Indicator.add_key_value` method is available. The field name must be exactly what the batch API expects (which are listed here). Adding metadata behaves the same as in Interface 1.

Note

The casing of the indicator type (the first argument provided to the batch.indicator() function) should be the same as the name key provided when retrieving the indicator types.

Indicator Interface 3

The third interface accepts the raw data formatted as a dictionary. This method requires that an xid be provided. All metadata should be included with in the data. You can view the required fields for each indicator type here.

File Occurrence

Associations

Associations are supported as Group -> Group or Indicator -> Group. Using Interface 1 or 2 the behavior is the same for Group and Indicators. However, for Interface 3 the structure is slightly different as displayed below.

Group

Example of Group -> Group association.

Indicator

Example of Indicator -> Group association.

Submit

There are few options for submitting the batch job, all with an option to halt_on_error. The most common option :py:meth:`~tcex.tcex_batch_v2.TcExBatch.submit_all` provides a simple interface that will perform all the individual step by default (e.g., request create and upload, poll for status, retrieve errors, and submit files). When using this method it is possible to control the halt_on_error behavior for each step using global overrides.

In some cases handling errors may require more control. For these cases the submit method can be called with some or all of the additional features (e.g., polling, retrieving errors, and uploading files) disabled. It is also possible to call each method individually. A possible workflow could be to use :py:meth:`~tcex.tcex_batch_v2.TcExBatch.submit_create_and_upload` and then go retrieve data from remote endpoint while ThreatConnect processes the batch job. Next poll using :py:meth:`~tcex.tcex_batch_v2.TcExBatch.poll` for status and when the job is Completed the next job request can be submitted. If batch errors are reported in the Batch status the :py:meth:`~tcex.tcex_batch_v2.TcExBatch.errors` method can be used to retrieve the errors. Submitting files for Documents and Reports can be done using the :py:meth:`~tcex.tcex_batch_v2.TcExBatch.submit_files` method.

Note

The setting synchronousBatchSaveLimit in the ThreatConnect UI -> System Settings controls the synchronous processing of batch jobs. If the batch job is smaller than the defined value the batch data will be processed synchronously and the batch status will be returned on completion without the need to poll. The :py:meth:`~tcex.tcex_batch_v2.TcExBatch.submit` method provides logic for handling this so the developer is not required to check if the job was queued.

Option 1

Submit the job and wait for completion. In the example any error messages are requested to be returned as well.

Option 2

Call each step in submitting the batch job manually. A check for batch_id will indicate whether the job was processed asynchronously.