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');
Purpose
Parameters
filter_name: specifies the name of the filter.Note
AUDIT_LOG_FILTER_SET_FILTERis aCREATE OR REPLACEstatement. If an object exists, the DDL operation will overwrite it.definition_of_filters: specifies the configuration of the audit filter in JSON format. At the level of event type (Class), you can configure event type subtypes (Event Type), event fields (Event Field), and theAND/ORcombinations between fields:- Event type subtype (Event Type): specifies the subtypes of the specified event type for further filtering. In the JSON string, specify the related field by using the
nameparameter undereventinclass, for example,{"class": [{"name": "connection", "event": [{"name": "disconnect"}]}]}. - Event field (Event Field) filtering: specifies the fields of the specified event type for further filtering. You can set the
fieldparameter to filter specific fields (field) of the connection, general, and table_access event types. In the JSON string, specify the related field by using thelogparameter underevent, for example,"event": [{"name": "read", "log": {"field": {"name": "table_name", "value": "t1"}}}]. - Logical combination: supports the
AND/ORcombinations between multiple fields.
- Event type subtype (Event Type): specifies the subtypes of the specified event type for further filtering. In the JSON string, specify the related field by using the
Note
Event type subtypes, event field filtering, and logical combinations are supported from V4.4.2 BP1. Event type filtering is supported only in V4.4.2.
Supported event type subtypes:
Event type (Class) |
Description |
Supported event type subtypes (Event Type) |
|---|---|---|
| connection | contains login and logout statements |
|
| general | contains all non-DML statements |
|
| table_access | applies only to DML statements |
|
Supported event fields:
The connection, general, and table_access event types are supported. Each event type has multiple event fields. For more information, see the table below:
Event type |
Event field (Event Field) |
Data type |
Description |
|---|---|---|---|
| connection | connection_id | Int | The unique identifier of the connection. |
| connection | user | String | The login username to be audited. |
| connection | database | String | The name of the database specified when a connection is established. |
| connection | ip | String | The IP address of the client used by the user to connect. |
| connection | server_ip | String | The IP address of the OBServer to which the connection is established. |
| connection | server_host | String | The hostname of the OBServer to which the connection is established. |
| general | general_user | String | The username to be audited. |
| general | general_sql_command | String | The specific SQL type to be audited. Currently, all non-DML statements can be audited. DDL statements such as CREATE_TABLE, ALTER_USER, and ALTER_SYSTEM_SET_PARAMETER are supported. Other SQL statements such as SELECT, INSERT, UPDATE, and DELETE are also supported. |
| 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. The table_name field can be used in the following two ways:
|
For more information about the SQL types supported by general_sql_command, see the related documentation at the end of this topic.
log field hierarchy, differences, and default behavior
The logging behavior depends on the value of log and whether class or event is specified.
Level |
Value |
Description |
Behavior when not specified |
|---|---|---|---|
filter.log |
true/false | The global switch for the filter, controlling whether to log by default. | If neither class nor event is specified, it defaults to true. |
filter.class[].log |
true/false/JSON string | A local switch or conditional filter for a single class entry. |
Can be explicitly configured; if not, it follows the parent rules. |
filter.class[].event[].log |
true/false/JSON string | A local switch or conditional filter for a single event entry. |
Can be explicitly configured; if not, it follows the parent rules. |
Additional notes:
- The Level column in the table is a shorthand for the path notation, where:
filter.logrefers to thelogproperty under thefilterobject, for example,{"filter": {"log": true}}.filter.class[].logrefers to thelogproperty of a single element in theclassarray under thefilterobject, for example,{"filter": {"class": [{"log": true}]}}.filter.class[].event[].logrefers to thelogproperty of a single element in theeventarray of a single element in theclassarray under thefilterobject, for example,{"filter":{"class":[{"name":"table_access","event":[{"name":"read","log":true}]}]}}.
- The "outer
log" in the JSON and the "locallog" in theclass/eventlevels are not redundant configurations: the former is the global switch, while the latter is a local override or conditional filter.
Common filters in JSON format
The class attribute can be extended to define more event types, event fields, and logical combinations. The following examples show several common methods of extending the class attribute:
Track all events.
{ "filter": { "log": true } }Does not record all events.
{ "filter": { "log": false } }Log only login and logout events (connection class only).
{ "filter": { "log": true, "class": [ { "name": "connection" } ] } }Explicitly list all classes (equivalent to recording all events).
{ "filter": { "log": true, "class": [ { "name": "connection" }, { "name": "general" }, { "name": "table_access" } ] } }Alternatively, multiple items on the same level can be merged into an array (equivalent approach): If the same type of items appear multiple times with different values at the same level in the filter definition, these items can be merged into the array of a single object, preserving only one object:
{ "filter": { "log": true, "class": [ { "name": [ "connection", "general", "table_access" ] } ] } }
Event type
Retain only
connectunderconnection(excludingdisconnect).{ "filter": { "log": true, "class": [ { "name": "connection", "event": [ { "name": "connect" } ] } ] } }A single event entry in the
eventarray can explicitly includelogto indicate whether to record events that meet the conditions of that entry. For example, you can select multiple event types for the sametable_accessand specify whether to log events of each type.{ "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 fields (event field) and logical grouping
Records events of
connectanddisconnectfor theconnectionobjects and events ofinsert,delete, andupdatefor thetable_accessobjects.{ "filter": { "log": true, "class": [ { "name": "connection", "event": [ { "name": "connect" }, { "name": "disconnect" } ] }, { "name": "general" }, { "name": "table_access", "event": [ { "name": "insert" }, { "name": "delete" }, { "name": "update" } ] } ] } }Logical operators
andandorcan be nested to construct complex conditions. The following filter applies only to events in thegeneralandstatusevent categories and records events that satisfy either of the twoandgroups: First group:general_command.strequalsQueryandgeneral_command.lengthequals5. Second group:general_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 expression must be a string constant, and the output is of the string type.
- If the DDL operation is successful, the expression returns
OK. - If the DDL operation fails, the
SELECTstatement still executes successfully, and the output of the expression is the error message.
Examples
Create a filter named
log_allthat records 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 setWhen a DDL statement fails, the
SELECTstatement still executes successfully, and the output of the expression is an 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
general_sql_commandstatement types: Appendix: Supported SQL types of thegeneral_sql_commandstatement- Enable security audit
