Generic SQL
Use the Generic SQL integration to run SQL queries on the following databases: MySQL, PostgreSQL, Microsoft SQL Server, Oracle, Teradata and Trino.
Database · GenericSQL
Details
| ID | Generic SQL |
|---|---|
| Provider | Microsoft |
| Category | Database |
| From Version | 5.0.0 |
| Docker Image | demisto/genericsql:1.2.0.10221838 |
| Supported Modules | Agentix Cloud Runtime Security Cloud Posture Security XSIAM EDR Cortex Cloud |
README
Generic SQL integration for the Databases: MySQL, PostgreSQL, Microsoft SQL Server, Oracle, Teradata and Trino.
Default ports
If the port value is empty, a default port will be selected according to the database type.
- MySQL: 3306
- PostgreSQL: 5432
- Microsoft SQL Server: 1433
- Oracle: 1521
- Trino: 8080
- Teradata: 1025
Connection Arguments
Specify arguments for the configuration of an instance name-value pairs, for example:
charset=utf8
Separate pairs using & character, for example:
charset=utf8&read_timeout=10
Connection Pooling
By default, the integration does not pool database connections. Thus, a connection is created and closed for each command run by the integration. When connection pooling is enabled, each Docker container will maintain a single connection open for the time specified in the the Connection Pool Time to Live parameter (default: 600 seconds). After the time to live expires, and upon execution of a new command, the database connection will close and a new connection will be created.
Note: When pooling is enabled, the number of active open database connections will equal the number of active running demisto/genericsql Docker containers.
Bind Variables
There are two options to use to bind variables:
- Use both bind variable names and values, for example:
SELECT * from Table Where ID=:x” bind_variables_names=x bind_variables_values=123 - Use only bind variable values, for example:
INSERT into Table(ID, Name) VALUES (%s, %s)” bind_variables_values= “123, Ben”
Fetch Incidents
There are two options to fetch incidents, determined by ‘Fetch by’ configuration:
-
ID and timestamp - when ID is unique but not necessarily ascending and timestamp is not unique.
Fill in ‘Fetch Column’ with your exact timestamp column name, and fill in ‘ID column name’ with your exact ID column name.
-
Unique ascending ID or Unique timestamp - when fetching by either ID or timestamp.
Fill only the ‘Fetch Column’ with the exact column name to fetch (ID column or timestamp column).
Fetch events query
The Generic SQL query or procedure to fetch according to.
When using queries, there are two requirements, and the third one depends on the database.
- ‘fetch column’ > or >= :’fetch column’
- order by (asc) ‘fetch column’
- (Optional) limit :limit (It’s possible if the DB supports it)
Queries examples
- Supported:
- Select ID, header_name from table_name where id >:id order by id – ok when fetching by ID and ID is the exact fetch column.
- Select * from table_name where timestamp >=:timestamp order by timestamp limit :limit – ok when fetching by timestamp and timestamp is the exact fetch column and database supports for limit.
- Unsupported:
- Select header_name from table_name – no select ID or timestamp column, can’t execute the fetch.
- Select alert_id from table_name – missing condition ‘where alert_id >:alert_id order by alert_id’, can’t execute the fetch.
The following are procedure examples for different SQL databases:
MySQL
Example: “CREATE PROCEDURE PROCEDURE_NAME(IN ts DATETIME, IN l INT)
BEGIN
SELECT * FROM TABLE_NAME
WHERE timestamp >= ts order by timestamp asc limit l;
END”
- Make sure to add as parameters the fetch parameter and the limit.
- The procedure should contain conditions on the fetch parameter: (In the example provided, ‘ts’ is a fetch timestamp parameter)
- timestamp >= ts or timestamp > ts if timestamp is unique.
- order by timestamp (asc).
- Run sql-command with your new procedure provided in the query argument in order to create your procedure.
- After creating the procedure, fill in ‘Fetch events query’ the value: ‘call PROCEDURE_NAME’ with your procedure name.
- Fetch parameters, ts (timestamp) or ID and l (limit), will be added by the fetch mechanism.
MSSQL
Example: “CREATE PROCEDURE PROCEDURE_NAME @timestamp DATETIME
AS
SELECT * FROM TABLE_NAME WHERE timestamp >= @timestamp order by timestamp”
- Make sure to add as parameters the fetch parameter.
- The procedure should contain conditions on the fetch parameter: (In the example provided, ‘timestamp’ is a fetch parameter)
- timestamp >= @timestamp or timestamp > @timestamp if timestamp is unique.
- order by timestamp (asc).
- The fetch parameter should be the same as the column name, the limit is handled outside the query.
- Run sql-command with your new procedure provided in the query argument, in order to create your procedure.
- After creating the procedure, fill in ‘Fetch events query’ the value: ‘EXEC PROCEDURE_NAME’ with your procedure name.
- Fetch parameters, ts (timestamp) or id and l (limit), will be added by the fetch mechanism.
Note: Other SQL databases are currently not supported by the fetch incidents.
Fetch Incidents query Notes
- When ‘Fetch by’ is ‘Unique ascending ID’ or ‘Unique timestamp’, make sure to create the procedure with ‘>’ and not ‘>=’ in the condition on the timestamp/id field.
- When ‘Fetch by’ is ‘ID and timestamp’, handling the ID occurs internally and has no reference in the query.
Configure Generic SQL on Cortex XSOAR
- Navigate to Settings > Integrations > Servers & Services.
- Search for Generic SQL.
- Click Add instance to create and configure a new integration instance.
- Name: a textual name for the integration instance.
- SQL DB
- Database host
- Port
- Database Name
- Username
- Connection Arguments (ex: arg1=val1&arg2=val2)
- Click Test to validate the URLs, token, and connection.
Commands
You can execute these commands from the Cortex XSOAR CLI, as part of an automation, or in a playbook.
After you successfully execute a command, a DBot message appears in the War Room with the command details.
The two commands are the same, they can get the same arguments and will provide the same outputs.
- query
- sql-command
1. query
Running a sql query
Required Permissions
Permissions to the database are needed
Base Command
query
Input
| Argument Name | Description | Required |
|---|---|---|
| limit | Number of results you would like to get back | Optional |
| query | The SQL query | Required |
| skip | Number of results you would like to skip on | Optional |
| bind_variables_names | e.g.: “foo”,”bar”,”alpha” | Optional |
| bind_variables_values | e.g.: 7,”foo”,3 | Optional |
Context Output
There is no context output for this command.
Command Example
!query query="select * from TestTable" limit=10 skip=0
Context Example
{
"GenericSQL": {
"GenericSQL": {
"Query": "select * from TestTable",
"Headers": ["LastName", "ID", "FirstName"],
"InstanceName": "MySQL_new_schema",
"Result": [
{
"LastName": "Grace",
"ID": 22222,
"FirstName": "Bob"
},
{
"LastName": "Jacob",
"ID": 33333,
"FirstName": "Liya"
},
{
"LastName": "James",
"ID": 44444,
"FirstName": "Chris"
},
{
"LastName": "Zohar",
"ID": 55555,
"FirstName": "Tamar"
}
]
}
}
}
Human Readable Output
Query result
ID LastName FirstName 22222 Grace Bob 33333 Jacob Liya 44444 James Chris 55555 Zohar Tamar
Command Example
!query query="INSERT into TestTable(ID, LastName, FirstName) VALUES (11111, :x , :y)" bind_variables_names=x,y bind_variables_values="test,playbook"
Context Example
{}
Human Readable Output
Command executed
Command Example
!query query="delete from TestTable where ID=11111"
Context Example
{}
Human Readable Output
Command executed
2. sql-command
Running a sql query
Base Command
sql-command
Input
| Argument Name | Description | Required |
|---|---|---|
| limit | Number of results you would like to get back | Optional |
| query | The SQL query | Required |
| skip | Number of results you would like to skip on | Optional |
| bind_variables_names | e.g.: “foo”,”bar”,”alpha” | Optional |
| bind_variables_values | e.g.: 7,”foo”,3 | Optional |
Context Output
There is no context output for this command.
Command Example
!sql-command query="select * from TestTable" limit=10 skip=0
Context Example
{
"GenericSQL": {
"GenericSQL": {
"Query": "select * from TestTable",
"Headers": ["LastName", "ID", "FirstName"],
"InstanceName": "MySQL_new_schema",
"Result": [
{
"LastName": "Grace",
"ID": 22222,
"FirstName": "Bob"
},
{
"LastName": "Jacob",
"ID": 33333,
"FirstName": "Liya"
},
{
"LastName": "James",
"ID": 44444,
"FirstName": "Chris"
},
{
"LastName": "Zohar",
"ID": 55555,
"FirstName": "Tamar"
}
]
}
}
}
Human Readable Output
Query result
ID LastName FirstName 22222 Grace Bob 33333 Jacob Liya 44444 James Chris 55555 Zohar Tamar
Command Example
!sql-command query="INSERT into TestTable(ID, LastName, FirstName) VALUES (11111, :x , :y)" bind_variables_names=x,y bind_variables_values="test,playbook"
Context Example
{}
Human Readable Output
Command executed
Command Example
!sql-command query="delete from TestTable where ID=11111"
Context Example
{}
Human Readable Output
Command executed
Troubleshooting
General Test Connection Error
In cases where you receive an error that is not clear when you Test the integration instance you can get detailed logs.
- Save the configured instance even though the Test doesn’t work.
- In the playground, run the
!sql-commandwithdebug-mode=true. For example:
!sql-command query="some simple query" debug-mode=true
A log file will be generated in the Playground. Examine the log file for further details that explain why the integration is failing.
Microsoft SQL Server
We provide two options for connecting to Microsoft SQL Server:
- Microsoft SQL Server: Uses the open source FreeTDS driver to communicate with Microsoft SQL Server. This driver supports authentication via domain logins (
DOMAIN\username) with a password. If you do not require a domain login for authentication, we recommend using theMicrosoft SQL Server - MS ODBC Driver. - Microsoft SQL Server - MS ODBC Driver: Official driver from Microsoft for Linux.
Note: Kerberos authentication is not supported.
If you experience any issues communicating with your Microsoft SQL Sever, try using both options as we’ve seen cases where one option works while the other doesn’t.
When configuring SQL Server, if you receive an error of the form:
('08S01', '[08S01] [FreeTDS][SQL Server]Unable to connect: Adaptive Server is unavailable or does not exist (20009) (SQLDriverConnect)')
(Background on this error at: http://sqlalche.me/e/13/e3q8)
It means there is a communication problem from the Generic SQL Docker to the SQL Server. It usually means the dns hostname of the SQL Server is not resolving. You can try using an IP instead of the DNS. You can further test from Docker by running the following command on the Cortex XSOAR machine:
echo "select @@version" | sudo docker run --rm -i demisto/genericsql:1.1.0.9726 tsql -H <sql_server_host> -p <sql_port_number> -U <user> -P <password> -D <db_to_connect> -v -o v
Autocommit: If you are seeing that insert/update operations are NOT being performed and no error is received, it could be a case that autocommit is not enabled on the connection and the transaction is rolledback. To enable autocommit, add the following to the connection arguments instance configuration option:
autocommit=True
Oracle
If you require connecting to Oracle via a SERVICE_NAME, leave the Database Name parameter empty and add to the Connection Arguments the following:
service_name=<SERVICE_NAME>
For example:
service_name=XEXDB
Possible Errors
- The bind variables lists are not the same length.
- Command is not an existing Generic SQL command.
Configuration parameters
dialect— SQL DB (required)host— Database host (required)port— Portcredentials— Username (required)fetch_parameters— Fetch byquery— Fetch events querycolumn_name— Fetch Columnid_column— ID Column name - in case of fetching by 'ID and timestamp'isFetch— Fetch incidentsincident_name— Incident Namedbname— Database Nameconnect_parameters— Connection Arguments (ex: arg1=val1&arg2=val2)max_fetch— Fetch Limit (Default / Max - 50, Recommended less than 50)first_fetch— First fetch timestamp or First fetch IDssl_connect— Use an SSL connectionuse_pool— Use Connection Poolinginsecure— Trust any certificate (not secure)use_ldap— Use LDAP (Teradata only)pool_ttl— Connection Pool Time to Live (seconds)incidentType— Incident typeincidentFetchInterval— Incidents Fetch Interval
Commands (3)
-
pgsql-queryDeprecatedRuns a SQL query. Deprecated. Use the generic sql-command instead.
-
queryDeprecatedRuns a SQL query. Deprecated. Use the generic sql-command instead.
-
sql-commandRunning a sql query.
category: Database provider: Microsoft commonfields: id: Generic SQL version: -1 sectionorder: - Connect - Collect configuration: - defaultvalue: MySQL display: SQL DB name: dialect options: - MySQL - PostgreSQL - Microsoft SQL Server - Microsoft SQL Server - MS ODBC Driver - Oracle - Trino - Teradata required: true type: 15 section: Connect - display: Database host name: host required: true type: 0 section: Connect - display: Port name: port type: 0 required: false section: Connect - display: Username name: credentials required: true type: 9 section: Connect - display: Fetch by name: fetch_parameters type: 15 defaultvalue: ID and timestamp options: - Unique timestamp - Unique ascending ID - ID and timestamp required: false section: Collect - display: Fetch events query name: query type: 0 additionalinfo: The Generic SQL query/procedure. required: false section: Collect - display: Fetch Column name: column_name type: 0 additionalinfo: Enter the exact column's name to fetch (ID column or timestamp column). required: false section: Collect - display: ID Column name - in case of fetching by 'ID and timestamp' name: id_column type: 0 required: false section: Collect - display: Fetch incidents name: isFetch type: 8 required: false section: Collect supportedModules: - agentix - xsiam - display: Incident Name additionalinfo: Enter the exact column's incident name (if empty - it's the Fetch Column). name: incident_name type: 0 required: false section: Collect - display: Database Name name: dbname type: 0 required: false section: Connect - display: 'Connection Arguments (ex: arg1=val1&arg2=val2)' name: connect_parameters type: 0 required: false section: Connect - defaultvalue: '50' display: Fetch Limit (Default / Max - 50, Recommended less than 50) name: max_fetch type: 0 required: false section: Collect supportedModules: - agentix - xsiam - display: First fetch timestamp or First fetch ID name: first_fetch type: 0 section: Collect required: false - display: Use an SSL connection name: ssl_connect type: 8 required: false section: Connect - display: Use Connection Pooling name: use_pool type: 8 required: false section: Connect - display: Trust any certificate (not secure) name: insecure type: 8 required: false section: Connect - display: Use LDAP (Teradata only) name: use_ldap type: 8 required: false section: Connect - display: Connection Pool Time to Live (seconds) name: pool_ttl type: 0 required: false additionalinfo: After this time the connection pool will be refreshed defaultvalue: 600 section: Connect - display: Incident type name: incidentType type: 13 required: false section: Collect supportedModules: - agentix - xsiam - display: Incidents Fetch Interval name: incidentFetchInterval defaultvalue: '1' required: false type: 19 section: Collect advanced: true supportedModules: - agentix - xsiam description: 'Use the Generic SQL integration to run SQL queries on the following databases: MySQL, PostgreSQL, Microsoft SQL Server, Oracle, Teradata and Trino.' display: Generic SQL name: Generic SQL script: commands: - arguments: - description: The SQL query to run. name: query required: true - defaultValue: '50' description: The maximum number of results to return. name: limit - defaultValue: '0' description: The offset at which to start the results. The default is 0. name: skip - description: 'A comma-separated list of names, for example: "foo","bar","alpha".' isArray: true name: bind_variables_names - description: 'A comma-separated list of value, for example: 7,"foo",3.' isArray: true name: bind_variables_values deprecated: true description: Runs a SQL query. Deprecated. Use the generic sql-command instead. name: pgsql-query - arguments: - description: The SQL query to run. name: query required: true - defaultValue: '50' description: The maximum number of results to return. The default is 50. name: limit - defaultValue: '0' description: The offset at which to start the results. The default is 0. name: skip - description: 'A comma-separated list of names, for example: "foo","bar","alpha".' isArray: true name: bind_variables_names - description: 'A comma-separated list of value, for example: 7,"foo",3.' isArray: true name: bind_variables_values deprecated: true description: Runs a SQL query. Deprecated. Use the generic sql-command instead. name: query - arguments: - description: The SQL query to run. name: query required: true - defaultValue: '50' description: The maximum number of results to return. name: limit - defaultValue: '0' description: The offset at which to start the results. The default is 0. name: skip - description: 'A comma-separated list of names, for example: "foo","bar","alpha".' isArray: true name: bind_variables_names - description: 'A comma-separated list of value, for example: 7,"foo",3.' isArray: true name: bind_variables_values description: Running a sql query. name: sql-command dockerimage: demisto/genericsql:1.2.0.10221838 isfetch: true runonce: false script: '-' subtype: python3 type: python fromversion: 5.0.0 tests: - generic-sql - generic-sql-oracle defaultclassifier: GenericSQL Classifier