Purpose
This function (expression) is used to create a filter.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition does not support the audit feature.
Syntax
AUDIT_LOG_FILTER_SET_FILTER('filter_name', 'definition_of_filters');
Considerations
Parameters
filter_name: specifies the name of the filter.Note
AUDIT_LOG_FILTER_SET_FILTERis aCREATE OR REPLACEstatement. If an object exists, the DDL statement will overwrite it.definition_of_filters: specifies the configuration of the audit filter in JSON format. In addition to the event type (Class), you can configure the event type subdivision (Event Type), event field (Event Field), and theAND/ORcombination of field conditions:- Event type subdivision (Event Type): used to further subdivide and filter a specified event type. In the JSON string, specify the relevant fields through the
nameparameter undereventunderclass, for example,{"class": [{"name": "connection", "event": [{"name": "disconnect"}]}]}. - Event field (Event Field) filtering: used to further subdivide and filter a specified event type. You can set the
fieldparameter to filter specific fields (field) for the three types of events: connection, general, and table_access. In the JSON string, specify the relevant fields through thelogparameter underevent, for example,"event": [{"name": "read", "log": {"field": {"name": "table_name", "value": "t1"}}}]. - Logical combination: supports logical combination of multiple fields using
AND/OR.
- Event type subdivision (Event Type): used to further subdivide and filter a specified event type. In the JSON string, specify the relevant fields through the
Note
Event type subdivision, event field filtering, and logical combination operations are supported starting from V4.6.1. Earlier versions support only event type filtering.
Subtypes supported for event type subdivision:
Event type (Class) |
Description |
Subtypes that can be subdivided (Event Type) |
|---|---|---|
| connection | Includes login and logout statements |
|
| general | Includes all non-DML statements |
|
| table_access | Can only be used for DML |
|
Fields supported for event field filtering:
The connection, general, and table_access event types are included. Each event type supports multiple event fields. For details, see the following table:
Event type |
Event field (Event Field) |
Data type |
Description |
|---|---|---|---|
| connection | connection_id | Int | Unique identifier of the connection |
| connection | user | String | The username of the log-in account to be audited |
| connection | database | String | The name of the database specified during connection |
| connection | ip | String | The client IP address of the user who established the connection |
| connection | server_ip | String | The IP address of the OBServer node to which the connection is made |
| connection | server_host | String | The host name of the OBServer node to which the connection is made |
| general | general_user | String | The specified username to be audited |
| general | general_sql_command | String | The specified specific SQL type. Currently, this can cover all non-DML statement types, such as CREATE_TABLE, ALTER_USER, ALTER_SYSTEM_SET_PARAMETER, etc.; other SQL statements such as SELECT, INSERT, UPDATE, DELETE, etc. |
| table_access | table_database | String | The name of the database to which the event belongs |
| table_access | table_name | String | The table related to the event. Two usages are supported:
|
To view the complete SQL types supported by general_sql_command, refer to the related documentation at the end of this topic.
Hierarchies, differences, and default behaviors of the log field
Logging behavior depends on the value of log and whether the class or event item is specified.
Hierarchy |
Value |
Description |
Behavior if unspecified |
|---|---|---|---|
filter.log |
true/false | The global switch for the filter, controlling whether to record by default. | If class or event is not specified, it is treated as true by default. |
filter.class[].log |
true/false/JSON string | A local switch or conditional filtering for a single class entry. |
Can be explicitly configured; if not configured, it is processed according to the rule above it. |
filter.class[].event[].log |
true/false/JSON string | A local switch or conditional filtering for a single event entry. |
Can be explicitly configured; if not configured, it is processed according to the rule above it. |
Other notes:
- The Hierarchy column in the table is an abbreviation for path notation, where:
filter.logrefers to thelogattribute under thefilterobject, for example,{"filter": {"log": true}}.filter.class[].logrefers to thelogattribute of a single element in theclassarray under thefilterobject, for example,{"filter": {"class": [{"log": true}]}}.filter.class[].event[].logrefers to thelogattribute of a single element in theeventarray under a single element in theclassarray under thefilterobject, for example,{"filter":{"class":[{"name":"table_access","event":[{"name":"read","log":true}]}]}}.
- In the documentation, "outermost
login JSON" and "innerloginclass/eventhierarchy" are not duplicate configurations: the former is the global switch, and the latter is local override or conditional filtering.
Common filter JSON syntaxes
Based on class, you can further expand to subdivide event types, event fields, and logical combinations as shown in the table above. The following are some common syntaxes:
Record all events.
{ "filter": { "log": true } }Do not record all events.
{ "filter": { "log": false } }Record only login and logout events (only
connectionclass).{ "filter": { "log": true, "class": [ { "name": "connection" } ] } }Explicitly list all classes (an equivalent way to write recording all events).
{ "filter": { "log": true, "class": [ { "name": "connection" }, { "name": "general" }, { "name": "table_access" } ] } }You can also merge multiple items at the same level into an array (an equivalent writing method): At the same level in the filter definition, if an item type appears multiple times with only different values (for example, multiple
classelements each have differentnames), you can merge these values into an array of the same item, retaining only one object:{ "filter": { "log": true, "class": [ { "name": [ "connection", "general", "table_access" ] } ] } }
Event type subdivision (Event Type)
Under
connection, onlyconnectis retained (excludingdisconnect).{ "filter": { "log": true, "class": [ { "name": "connection", "event": [ { "name": "connect" } ] } ] } }A single event entry in the
eventarray can explicitly includelogto specify whether to record events that meet the conditions of that entry. The following example selects multiple event types under the sametable_accessand specifies whether to write each type to the audit:{ "filter": { "log": true, "class": [ { "name": "table_access", "event": [ { "name": "read", "log": false }, { "name": "insert", "log": true }, { "name": "delete", "log": true }, { "name": "update", "log": true } ] } ] } }
Event field (Event Field) and logical combination
Record
connectanddisconnectevents forconnection, andinsert,delete, andupdateevents fortable_access.{ "filter": { "log": true, "class": [ { "name": "connection", "event": [ { "name": "connect" }, { "name": "disconnect" } ] }, { "name": "general" }, { "name": "table_access", "event": [ { "name": "insert" }, { "name": "delete" }, { "name": "update" } ] } ] } }The logical operators
andandorcan be nested to construct complex conditions. The following filter, ongeneral/statusevents, records only those that satisfy one of twoandgroups within thelogcondition: the first group isgeneral_command.strequalsQueryandgeneral_command.lengthequals5; the second group isgeneral_command.strequalsExecuteandgeneral_command.lengthequals7.{ "filter": { "class": { "name": "general", "event": { "name": "status", "log": { "or": [ { "and": [ { "field": { "name": "general_command.str", "value": "Query" } }, { "field": { "name": "general_command.length", "value": 5 } } ] }, { "and": [ { "field": { "name": "general_command.str", "value": "Execute" } }, { "field": { "name": "general_command.length", "value": 7 } } ] } ] } } } } }
Return value
The input must be a string constant, and the output is a string.
- If the DDL statement is executed successfully, the expression returns
OK. - If the DDL statement fails, the
SELECTstatement still executes successfully, and the output of the expression is the error message.
Examples
Create a filter
log_allto record all events.SELECT AUDIT_LOG_FILTER_SET_FILTER('log_all', '{ "filter": { "log": true } }');The return result is as follows:
+-------------------------------------------------------------------------+ | AUDIT_LOG_FILTER_SET_FILTER('log_all', '{ "filter": { "log": true } }') | +-------------------------------------------------------------------------+ | OK | +-------------------------------------------------------------------------+ 1 row in setIf the DDL statement fails, the
SELECTstatement still executes successfully, and the output of the expression is the error message.SELECT AUDIT_LOG_FILTER_SET_FILTER('log_err', '1');The return result is as follows:
+---------------------------------------------+ | AUDIT_LOG_FILTER_SET_FILTER('log_err', '1') | +---------------------------------------------+ | ERROR: JSON parsing error. | +---------------------------------------------+ 1 row in set
References
- List of supported
general_sql_commandstatement types: Appendix: Complete list of SQL statements supported by general_sql_command - Enable security auditing
