Generic SQL
GenericSQL Pack.#
This Integration is part of theGeneric SQL integration for the Databases: MySQL, PostgreSQL, Microsoft SQL Server, Oracle and Teradata.
#
Default portsIf 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
- Teradata: 1025
#
Connection ArgumentsSpecify arguments for the configuration of an instance name-value pairs, for example:
Separate pairs using & character, for example:
#
Connection PoolingBy 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 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 VariablesThere 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”
Note: Bind variables aren't supported with stored procedures.
#
Default portsIf 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
- Teradata: 1025
#
Connection ArgumentsSpecify arguments for the configuration of an instance name-value pairs, for example:
Separate pairs using & character, for example:
#
Connection PoolingBy 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 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 VariablesThere 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 IncidentsThere 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 queryThe 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.
#
CommandsYou 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. queryRunning a sql query
#
Required PermissionsPermissions to the database are needed
#
Base Commandquery
#
InputArgument 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 OutputThere is no context output for this command.
#
Command Example!query query="select * from TestTable" limit=10 skip=0
#
Context Example#
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 OutputCommand executed
#
Command Example!query query="delete from TestTable where ID=11111"
#
Context Example#
Human Readable OutputCommand executed
#
2. sql-commandRunning a sql query
#
Base Commandsql-command
#
InputArgument 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 OutputThere is no context output for this command.
#
Command Example!sql-command query="select * from TestTable" limit=10 skip=0
#
Context Example#
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 OutputCommand executed
#
Command Example!sql-command query="delete from TestTable where ID=11111"
#
Context Example#
Human Readable OutputCommand executed
#
Troubleshooting#
General Test Connection ErrorIn 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-command
withdebug-mode=true
. For example: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 ServerWe 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:
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 the from docker by running the following command on the Cortex XSOAR machine:
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:
#
OracleIf you require connecting to Oracle via a SERVICE_NAME, leave the Database Name
parameter empty and add to the Connection Arguments
the following:
For example:
#
Possible Errors:- The bind variables lists are not is the same length
- Command is not an existing Generic SQL command