Config
Introduction¶
The config module is used to configure PSEngine and control integration behavior. It does not interact with any Recorded Future datasets, and its use is optional, primarily serving as a convenience when a configuration file is needed.
Configuration values can be provided in several ways:
- Supported file formats:
.toml,.json, or.env - Environment variables
- Directly via parameters to the
initmethod
When loading configuration, PSEngine follows a strict priority:
- Values passed directly to the
initmethod - Values from environment variables
- Values from configuration files
If an environment variable is set, it will override the corresponding value in the config file.
The Config class is a singleton, meaning its values are immutable once initialized and accessible from any module. You initialize the configuration using the init method, and retrieve its data with the get_config method.
By default, the Config class manages a ConfigModel, which is a pydantic.BaseSettings class containing common attributes such as proxy settings and HTTP timeout.
The variables pre-defined by the ConfigModel are:
platform_id-> strapp_id-> strrf_token->RFTokenhttp_proxy-> strhttps_proxy-> strclient_ssl_verify-> boolclient_basic_auth-> (str, str)client_cert-> str or (str, str)client_timeout-> intclient_retries-> intclient_backoff_factor-> intclient_status_forcelist-> list of intclient_pool_max_size-> int
Warning
Define the Config before initializing the manager in your integration entry point. Once that is done, you can reference the Config from anywhere. See the example below.
See the API Reference for internal details of the module.
Examples¶
Warning
The following examples demonstrate how to use this module. Be sure to add appropriate error handling as needed; all possible errors for each method or function are listed in the API Reference page.
Additionally, you must configure the RF_TOKEN environment variable before getting started. For instructions, see Learn.
1: Read a Config from config.toml¶
To run this example, create a config.toml file with the following content:
Initialize the Config object with init, passing the path (absolute or relative) of the config file you intend to use. This creates an object but does not return it.
Since you want to print the value of my_value, use the get_config method to return the ConfigModel instance.
This will print 5.
2: Configure a Config from environment variables¶
You can read only environment variables that are statically defined in the ConfigModel. They need to be prefixed with RF_ and must be of the type specified in the model as described in the Introduction section.
For example, to set the app_id and platform_id variables:
Then read the config:
The sample code will print the values defined above.
3: Configure a Config from Python¶
You can initialize your config from the init method directly:
This will print 5.
4: Define your own config¶
If you want to define your own config in an integration, you can. The steps are:
- Define your model (
IntegrationModelin the example). It has to inherit frompsengine.ConfigModel. - Change
Config.initto use theconfig_classand assign it to theIntegrationConfigmodel you just created. - Use the
config_classwithIntegrationConfigin theget_configfunction as well. - Keep doing everything else as usual.
To replicate this example, first create a custom_config.toml file with the following content:
Place this in the same directory as the example Python code. Once the file configuration is created, the sample code will create the custom config in the IntegrationConfig class. Then call the init method, passing the custom configuration model as config_class and the usual TOML path.
Each property can be accessed using dot notation, for example, config.complex_value.data.
5: Real example¶
Assume you are developing an integration that needs to fetch playbook alerts. The current requirements for the alerts to be ingested are:
- Domain Abuse
NewstatusHighpriority- No older than yesterday
Each domain that triggered this alert has to be enriched with the links field.
You can opt for a quick script using free variables around the code or use the config.
Code 1 without the config:
In this example, you hardcode the values that you need for fetching the alert and enriching the IOCs. This is perfectly fine; however, in larger applications, it might be challenging to maintain if the requirements change.
An alternative is to save the requirements to a config file and use them instead of the hardcoded values.
Code 2 with the config:
With the int_config.toml file:
The script can be rewritten as below.
The code itself is longer; however, you gain maintainability since a person without development experience or inner understanding of the application can change the config to meet new requirements.
6: Using a proxy¶
In this example, a proxy is configured for the LookupMgr to use when connecting to the Internet. While client_ssl_verify is optional, it is included here to allow the example to work with a proxy that does not have a certificate.
As with previous examples, you first set up the Config, then initialize the manager. The https_proxy argument specifies the proxy URL, and the manager automatically uses this configuration during initialization.