A series of filters are used to audit specific events.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition does not support the auditing feature.
Auditing scope
Filters can be applied based on dimensions such as account, event type, event attributes, and more. For each filter, you can choose whether to audit the filtered events.
Limitations and considerations
- An expression must be placed directly and uniquely in an output column (select item) of a
SELECTstatement, and cannot have a parent expression. - Expressions cannot be used in subqueries.
- After defining a filter, you must assign it to a user for it to take effect.
- A filter can be assigned to multiple users, but each user can have only one filter.
- When a connection is established, the current session determines which auditing filter to use, and this filter remains unchanged throughout the session lifecycle.
Audit log encryption
Note
The audit log encryption feature is supported starting with V4.6.1.
Audit logs of a MySQL-compatible tenant can be stored on disks and exported externally in plaintext or encrypted format. For more information about how to configure this feature, see the relevant documentation at the end of this topic.
Create a filter
You can create a filter to enable the auditing mode for a MySQL-compatible tenant. The filter controls whether to record events based on the event class. In addition, it also supports filtering by event type and event field, and logical combinations of AND and OR conditions for event fields.
Note
Filtering by event type, field, and logical combination is supported starting with V4.6.1. In earlier versions, only filtering by event type is supported.
Syntax
You can execute the AUDIT_LOG_FILTER_SET_FILTER function to create a filter. The syntax is as follows:
AUDIT_LOG_FILTER_SET_FILTER('filter_name', 'definition_of_filters');
The parameters are described as follows:
Parameter |
Description |
|---|---|
| filter_name | The name of the filter.
NoteThe |
| definition_of_filters | The specific configurations of the auditing filter, which is in the JSON string format. In addition to specifying the event class, you can also specify the event type and AND/OR combinations of field names and conditions. |
The parameters are described as follows:
- Event type: You can use this parameter to further specify the subtypes of an event. In the JSON string, specify the related fields using the
nameparameter undereventunderclass. For example,{"class": [{"name": "connection", "event": [{"name": "disconnect"}]}]}. - Event field: You can use this parameter to further specify the subtypes of an event. You can set the
fieldparameter to filter specific fields for the three types of events: connection, general, and table_access. In the JSON string, specify the related fields using thelogparameter underevent. For example,"event": [{"name": "read", "log": {"field": {"name": "table_name", "value": "t1"}}}]. - Logical combination: You can use
ANDandORto combine multiple fields.
The following table describes the subtypes of each event type.
Event type (Class) |
Description |
Subtype (Event type) |
|---|---|---|
| connection | Includes login and logout statements. |
|
| general | Includes all non-DML statements. |
|
| table_access | Can be used only for DML operations. |
|
The following table describes the fields that can be filtered for each event type.
The connection, general, and table_access event types support multiple fields. The following table describes the fields in detail.
Event type |
Event field (Event field) |
Data type |
Description |
|---|---|---|---|
| connection | connection_id | Int | The unique identifier of the connection. |
| connection | user | String | The username of the user who needs to be audited. |
| connection | database | String | The name of the database specified during connection establishment. |
| 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 established. |
| connection | server_host | String | The host name of the OBServer node to which the connection is established. |
| general | general_user | String | The username to be audited. |
| general | general_sql_command | String | The type of the SQL command to be audited. Currently, all non-DML statement types can be audited. For details about the specific statements, see the description below the table. |
| 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 list of SQL statement types that the general_sql_command parameter supports, see the relevant documentation at the end of this topic.
Hierarchies, differences, and default behaviors of the log field
The logging behavior depends on the value of the log field and whether the class or event field is specified.
Hierarchical level |
Value |
Description |
Default behavior if not specified |
|---|---|---|---|
filter.log |
true/false | The global switch for the filter, controlling whether to record events by default. | If the class or event field is not specified, true is used by default. |
filter.class[].log |
true/false/JSON string | The local switch or conditional filter for a single class entry. |
Can be explicitly configured. If not configured, the rule from the upper hierarchical level is applied. |
filter.class[].event[].log |
true/false/JSON string | The local switch or conditional filter for a single event entry. |
Can be explicitly configured. If not configured, the rule from the upper hierarchical level is applied. |
Other notes:
- The hierarchical level column in the table is an abbreviation for path notation. Specifically:
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}]}]}}.
- The "outermost
logfield in a JSON object" and the "innerlogfield in aclass/eventobject" are not duplicate configurations. The former is the global switch, and the latter is for local override or conditional filtering.
The following is a minimal JSON example of a filter. For more examples, see the reference documentation for the AUDIT_LOG_FILTER_SET_FILTER function in the relevant documentation at the end of this topic.
Record all events: Set
logtotrue(which is equivalent to recording all classes).{ "filter": { "log": true } }Record events where the event type is
connect(only the connection establishment operation is recorded under theconnectiontype; thedisconnectoperation is not included):{ "filter": { "log": true, "class": [ { "name": "connection", "event": [ { "name": "connect" } ] } ] } }
Example
This section provides a complete example.
Create a filter named
log_allto record all events.obclient [test]>SELECT AUDIT_LOG_FILTER_SET_FILTER('log_all', '{ "filter": { "log": true } }');When the DDL statement is executed successfully, the expression returns
OK.+-------------------------------------------------------------------------+ | AUDIT_LOG_FILTER_SET_FILTER('log_all', '{ "filter": { "log": true } }') | +-------------------------------------------------------------------------+ | OK | +-------------------------------------------------------------------------+ 1 row in setWhen the DDL statement fails, the
SELECTstatement still succeeds, and the output result is an error message.obclient [test]>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
View the definition of the auditing filter in the
mysql.audit_log_filterview. For the complete syntax and examples of the view, see the relevant documentation at the end of this topic.obclient [test]> select * from mysql.audit_log_filter;The return result is as follows:
+---------+-------------------------------+ | NAME | FILTER | +---------+-------------------------------+ | log_all | { "filter": { "log": true } } | +---------+-------------------------------+ 1 row in set (0.003 sec)The fields are described as follows:
FieldDescriptionNAME The name of the filter. FILTER The definition of the filter.
Configure filters
After you configure filters for the corresponding users, the background thread can print auditing logs.
Syntax
You can call the AUDIT_LOG_FILTER_SET_USER function to set a filter on a user.
AUDIT_LOG_FILTER_SET_USER('user_name', 'filter_name');
The following table describes the parameters.
Field |
Description |
|---|---|
| user_name | Specifies the user name
NoteThe
|
| filter_name | Specifies the filter name
Note
|
Example
Assign the
log_allfilter to theuser001user.obclient [test]> SELECT AUDIT_LOG_FILTER_SET_USER('user001', 'log_all');When the DDL statement is executed successfully, the expression returns
OK.+-------------------------------------------------+ | AUDIT_LOG_FILTER_SET_USER('user001', 'log_all') | +-------------------------------------------------+ | OK | +-------------------------------------------------+ 1 row in setWhen the DDL statement fails, the
SELECTstatement still succeeds, and the output result of the expression is an error message.obclient [test]>SELECT AUDIT_LOG_FILTER_SET_USER('log_err', '1');The return result is as follows:
+--------------------------------------------+ | AUDIT_LOG_FILTER_SET_USER('log_err', '1') | +--------------------------------------------+ | ERROR: Invalid character in the user name. | +--------------------------------------------+ 1 row in set (0.001 sec)
View the mapping between auditing filters and users through the
mysql.audit_log_userview. For the complete syntax and examples of this view, see the related documentation at the end of this topic.obclient [test]> select * from mysql.audit_log_user;The return result is as follows:
+---------+------+------------+ | USER | HOST | FILTERNAME | +---------+------+------------+ | user001 | % | log_all | +---------+------+------------+ 1 row in set (0.003 sec)The following table describes the fields in the query result.
FieldDescriptionUSER The username. HOST The hostname. FILTERNAME The filter name.
Enable auditing
Enable the auditing feature for the MySQL-compatible tenant by setting the parameter audit_log_enable. For more information about this parameter, see the related documentation at the end of this topic.
obclient> ALTER SYSTEM SET audit_log_enable=TRUE;
References
- Set audit log policies, including write, compression, rotation, cleanup, and encryption: Set audit rules
- Filter creation function: AUDIT_LOG_FILTER_SET_FILTER
- Filter configuration function: AUDIT_LOG_FILTER_SET_USER
- View filters: mysql.audit_log_filter
- User-filter mapping view: mysql.audit_log_user
- Audit enablement parameter: audit_log_enable
- List of supported
general_sql_commandstatement types: Appendix: Complete list of SQL types supported by general_sql_command
