Skip to content

Commit d52cb9e

Browse files
committed
PS-10161 [DOCS] - pre_authenticate and general log events are still generated even when user filter is set to {"filter": {"log": false}}
new file: docs/write-audit-log-filter-definitions.md modified: mkdocs-base.yml
1 parent 241e3bf commit d52cb9e

File tree

2 files changed

+237
-0
lines changed

2 files changed

+237
-0
lines changed
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
# Write Audit‑Log Filter definitions
2+
3+
Percona Server's audit‑log-filter plugin lets you control which events are recorded by supplying a JSON filter definition.
4+
5+
A filter is stored in the `mysql.audit_log_filter` table and then attached to one or more MySQL accounts through the mysql.`audit_log_user` table.
6+
7+
The following is a step‑by‑step guide that shows how to create a filter, attach it to a user, and verify that the desired events are captured or suppressed.
8+
9+
## Filter JSON structure
10+
11+
A filter consists of a top‑level object named `filter`.
12+
13+
Inside this object you can set a global `log` flag and optionally define an array called `class`.
14+
15+
Each element of the `class` array identifies a class name (for example, `connection`, `general`, `query`, `table_access`).
16+
17+
Each class element may contain an `event` object that specifies particular events and whether they should be logged.
18+
19+
```json
20+
{
21+
"filter": {
22+
"log": true,
23+
"class": [
24+
{
25+
"name": "connection",
26+
"event": {
27+
"name": "connect",
28+
"log": true
29+
}
30+
},
31+
{
32+
"name": "query",
33+
"event": {
34+
"name": "execute",
35+
"log": true
36+
}
37+
}
38+
]
39+
}
40+
}
41+
```
42+
If the `log` flag is omitted, the default value is `true`.
43+
When `log` is set to `false`, the filter disables logging for all statement‑type events unless a class entry overrides that setting.
44+
45+
## Practical example
46+
47+
Suppose you want a user named `excludeUserTest` to generate no audit records for statements, but the server should keep a generic record that a connection was established.
48+
49+
First create a filter that disables logging globally:
50+
51+
```{.bash data-prompt="mysql>"}
52+
mysql> INSERT INTO mysql.audit_log_filter (filter_id, name, filter) VALUES
53+
(1, 'log_none',
54+
'{"filter": {"log": false}}');
55+
```
56+
57+
Ensure `filter_id` is unique; attempting to reuse an existing ID raises a duplicate-key error.
58+
59+
Next map the filter to the user:
60+
61+
```{.bash data-prompt="mysql>"}
62+
mysql> INSERT INTO mysql.audit_log_user (username, userhost, filtername) VALUES
63+
('excludeUserTest', '%', 'log_none');
64+
```
65+
66+
After the insertion, reload the audit log filter plugin so the new configuration becomes active:
67+
68+
```{.bash data-prompt="mysql>"}
69+
mysql> CALL mysql.audit_log_reload();
70+
```
71+
72+
When `excludeUserTest` connects and runs queries, the audit log contains only the generic connection record that the plugin writes automatically.
73+
74+
### Limitation – connection‑related events logged
75+
76+
Even when a filter contains only `{"filter": {"log": false}}`, two kinds of audit records continue to appear:
77+
78+
| Event | Class | When generated |
79+
|-------------------------|------------|--------------------------------------------------------------------------------|
80+
| `pre_authenticate` | `connection` | Before the server identifies the user. The default mapping (`% → log_all`) applies. |
81+
| `general / log` (status 0) | `general` | At session start. The filter does not disable the `general` class, so a record is written. |
82+
83+
To prevent these records, create a filter that explicitly disables the `connection` and `general` classes and assign it to the user:
84+
85+
```{.bash data-prompt="mysql>"}
86+
mysql> INSERT INTO mysql.audit_log_filter (filter_id, name, filter) VALUES
87+
(2, 'log_none_strict',
88+
'{
89+
"filter": {
90+
"log": false,
91+
"class": [
92+
{"name": "connection", "event": {"name": "pre_authenticate", "log": false}},
93+
{"name": "general", "event": {"name": "log", "log": false}}
94+
]
95+
}
96+
}');
97+
98+
99+
mysql> UPDATE mysql.audit_log_user
100+
SET filtername = 'log_none_strict'
101+
WHERE username = 'excludeUserTest' AND userhost = '%';
102+
103+
mysql> CALL mysql.audit_log_reload();
104+
```
105+
106+
After the reload, connections made by `excludeUserTest` will no longer generate the `pre_authenticate` or `general / log entries`.
107+
108+
## View existing filters and mappings
109+
110+
You can list the filters that are currently defined.
111+
112+
```{.bash data-prompt="mysql>"}
113+
SELECT filter_id, name, filter FROM mysql.audit_log_filter;
114+
```
115+
116+
You can also list the user‑to‑filter mappings.
117+
118+
```{.bash data-prompt="mysql>"}
119+
SELECT username, userhost, filtername FROM mysql.audit_log_user;
120+
```
121+
122+
These queries help you verify that the intended filter is attached to the correct accounts.
123+
124+
## Verify the filter
125+
126+
1. Rotate or remove the current audit log filter file so you can see fresh output.
127+
128+
2. Connect to the server as `excludeUserTest` and run a simple query, for example, `SELECT 1;`.
129+
130+
3. Examine the audit log filter file (default location `/var/lib/mysql/audit.log`) with `tail -f /var/lib/mysql/audit.log`.
131+
132+
4. Confirm that only the events you intended to keep appear.
133+
134+
If unexpected events are still present, double‑check the JSON syntax, ensure the filter ID is unique, and verify that the reload statement succeeded without errors.
135+
136+
## Common errors and solutions
137+
138+
### JSON syntax errors
139+
140+
Error: `ERROR 3130 (22032): Invalid JSON text in argument 1 to function audit_log_filter_set_filter`
141+
142+
Solution: Validate your JSON syntax before inserting. Common issues include:
143+
- Missing commas between array elements
144+
- Unmatched quotes or braces
145+
- Trailing commas
146+
147+
Use a JSON validator or test with a simple filter first:
148+
```{.bash data-prompt="mysql>"}
149+
mysql> SELECT JSON_VALID('{"filter": {"log": false}}');
150+
```
151+
152+
### Duplicate key errors
153+
154+
Error: `ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'`
155+
156+
Solution: Use a unique `filter_id` or check existing filters:
157+
```{.bash data-prompt="mysql>"}
158+
mysql> SELECT MAX(filter_id) FROM mysql.audit_log_filter;
159+
```
160+
161+
### Plugin reload failures
162+
163+
Error: `ERROR 1305 (42000): FUNCTION mysql.audit_log_reload does not exist`
164+
165+
Solution: Ensure the audit log plugin is installed and active:
166+
```{.bash data-prompt="mysql>"}
167+
mysql> SHOW PLUGINS WHERE Name = 'audit_log';
168+
mysql> INSTALL PLUGIN audit_log SONAME 'audit_log.so';
169+
```
170+
171+
### Filter not taking effect
172+
173+
Symptoms: Expected events still appear in audit log
174+
175+
Solutions:
176+
177+
1. Verify the filter is properly assigned to the user:
178+
```{.bash data-prompt="mysql>"}
179+
mysql> SELECT * FROM mysql.audit_log_user WHERE username = 'your_user';
180+
```
181+
182+
2. Check for conflicting filters or default mappings:
183+
```{.bash data-prompt="mysql>"}
184+
mysql> SELECT * FROM mysql.audit_log_user WHERE userhost = '%';
185+
```
186+
187+
3. Ensure the plugin reload completed successfully:
188+
```{.bash data-prompt="mysql>"}
189+
mysql> CALL mysql.audit_log_reload();
190+
```
191+
192+
## Troubleshoot and checklist
193+
194+
* Define the JSON filter with the desired global `log` flag.
195+
196+
* Add class entries for any event type that must be treated differently from the global setting.
197+
198+
* Insert the filter into `mysql.audit_log_filter` with a unique `filter_id`.
199+
200+
* Map the filter to the appropriate MySQL accounts in `mysql.audit_log_user`.
201+
202+
* Reload the audit plugin to apply the changes.
203+
204+
* Test the configuration with a fresh connection and inspect the audit log.
205+
206+
This checklist ensures that the audit log filter records exactly the events you need while omitting unnecessary noise.
207+
208+
## Resources
209+
210+
### Related Audit Log Filter Topics
211+
212+
* [Audit Log Filter overview](audit-log-filter-overview.md) - Learn about the plugin's capabilities and architecture
213+
214+
* [Install the Audit Log Filter](install-audit-log-filter.md) - Installation instructions and prerequisites
215+
216+
* [Audit log filter functions, options and variables](audit-log-filter-variables.md) - Complete reference for configuration options
217+
218+
* [Filter the Audit Log Filter logs](filter-audit-log-filter-files.md) - Advanced filtering using function calls
219+
220+
* [Audit Log Filter file format overview](audit-log-filter-formats.md) - Understanding log file formats (JSON, XML)
221+
222+
* [Audit Log Filter security](audit-log-filter-security.md) - Security considerations and best practices
223+
224+
* [Audit Log Filter restrictions](audit-log-filter-restrictions.md) - Known limitations and constraints
225+
226+
* [Manage the Audit Log Filter files](manage-audit-log-filter.md) - File management and rotation
227+
228+
### Troubleshooting and Support
229+
230+
* [Frequently asked questions](faq.md) - Common questions and answers
231+
232+
* [Get help from Percona](get-help.md) - Community support and expert services
233+
234+
* [Glossary](glossary.md) - Technical terms and definitions
235+
236+
* [Percona Monitoring and Management](https://docs.percona.com/percona-monitoring-and-management) - Monitoring and performance analysis

mkdocs-base.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ nav:
224224
- XML (New style): audit-log-filter-new.md
225225
- XML (Old style): audit-log-filter-old.md
226226
- JSON: audit-log-filter-json.md
227+
- write-audit-log-filter-definitions.md
227228
- audit-log-filter-security.md
228229
- audit-log-filter-compression-encryption.md
229230
- reading-audit-log-filter-files.md

0 commit comments

Comments
 (0)