Skip to content

Commit b6839c0

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 b6839c0

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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`) and may contain an `event` object that specifies particular events and whether they should be logged.
16+
17+
```json
18+
{
19+
"filter": {
20+
"log": true,
21+
"class": [
22+
{
23+
"name": "connection",
24+
"event": {
25+
"name": "connect",
26+
"log": true
27+
}
28+
},
29+
{
30+
"name": "query",
31+
"event": {
32+
"name": "execute",
33+
"log": true
34+
}
35+
}
36+
]
37+
}
38+
}
39+
```
40+
If the `log` flag is omitted, the default value is `true`.
41+
When `log` is set to `false`, the filter disables logging for all statement‑type events unless a class entry overrides that setting.
42+
43+
## Practical example
44+
45+
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.
46+
47+
First create a filter that disables logging globally:
48+
49+
```{.bash data-prompt="mysql>"}
50+
mysql> INSERT INTO mysql.audit_log_filter (filter_id, name, filter) VALUES
51+
(1, 'log_none',
52+
'{"filter": {"log": false}}');
53+
```
54+
55+
Ensure `filter_id` is unique; attempting to reuse an existing ID raises a duplicate-key error.
56+
57+
Next map the filter to the user:
58+
59+
```{.bash data-prompt="mysql>"}
60+
mysql> INSERT INTO mysql.audit_log_user (username, userhost, filtername) VALUES
61+
('excludeUserTest', '%', 'log_none');
62+
```
63+
64+
After the insertion, reload the audit log filter plugin so the new configuration becomes active:
65+
66+
```{.bash data-prompt="mysql>"}
67+
mysql> CALL mysql.audit_log_reload();
68+
```
69+
70+
When `excludeUserTest` connects and runs queries, the audit log contains only the generic connection record that the plugin writes automatically.
71+
72+
### Limitation – connection‑related events logged
73+
74+
Even when a filter contains only `{"filter": {"log": false}}`, two kinds of audit records continue to appear:
75+
76+
| Event | Class | When generated |
77+
|-------------------------|------------|--------------------------------------------------------------------------------|
78+
| `pre_authenticate` | `connection` | Before the server identifies the user. The default mapping (`% → log_all`) applies. |
79+
| `general / log` (status 0) | `general` | At session start. The filter does not disable the `general` class, so a record is written. |
80+
81+
To prevent these records, create a filter that explicitly disables the `connection` and `general` classes and assign it to the user:
82+
83+
```{.bash data-prompt="mysql>"}
84+
mysql> INSERT INTO mysql.audit_log_filter (filter_id, name, filter) VALUES
85+
(2, 'log_none_strict',
86+
'{
87+
"filter": {
88+
"log": false,
89+
"class": [
90+
{"name": "connection", "event": {"name": "pre_authenticate", "log": false}},
91+
{"name": "general", "event": {"name": "log", "log": false}}
92+
]
93+
}
94+
}');
95+
96+
97+
mysql> UPDATE mysql.audit_log_user
98+
SET filtername = 'log_none_strict'
99+
WHERE username = 'excludeUserTest' AND userhost = '%';
100+
101+
mysql> CALL mysql.audit_log_reload();
102+
```
103+
104+
After the reload, connections made by `excludeUserTest` will no longer generate the `pre_authenticate` or `general / log entries`.
105+
106+
## View existing filters and mappings
107+
108+
You can list the filters that are currently defined.
109+
110+
```{.bash data-prompt="mysql>"}
111+
SELECT filter_id, name, filter FROM mysql.audit_log_filter;
112+
```
113+
114+
You can also list the user‑to‑filter mappings.
115+
116+
```{.bash data-prompt="mysql>"}
117+
SELECT username, userhost, filtername FROM mysql.audit_log_user;
118+
```
119+
120+
These queries help you verify that the intended filter is attached to the correct accounts.
121+
122+
## Verify the filter
123+
124+
1. Rotate or remove the current audit log filter file so you can see fresh output.
125+
126+
2. Connect to the server as `excludeUserTest` and run a simple query, for example, `SELECT 1;`.
127+
128+
3. Examine the audit log filter file (default location `/var/lib/mysql/audit.log`) with `tail -f /var/lib/mysql/audit.log`.
129+
130+
4. Confirm that only the events you intended to keep appear.
131+
132+
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.
133+
134+
## Common errors and solutions
135+
136+
### JSON syntax errors
137+
138+
**Error**: `ERROR 3130 (22032): Invalid JSON text in argument 1 to function audit_log_filter_set_filter`
139+
140+
**Solution**: Validate your JSON syntax before inserting. Common issues include:
141+
- Missing commas between array elements
142+
- Unmatched quotes or braces
143+
- Trailing commas
144+
145+
Use a JSON validator or test with a simple filter first:
146+
```{.bash data-prompt="mysql>"}
147+
mysql> SELECT JSON_VALID('{"filter": {"log": false}}');
148+
```
149+
150+
### Duplicate key errors
151+
152+
**Error**: `ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'`
153+
154+
**Solution**: Use a unique `filter_id` or check existing filters:
155+
```{.bash data-prompt="mysql>"}
156+
mysql> SELECT MAX(filter_id) FROM mysql.audit_log_filter;
157+
```
158+
159+
### Plugin reload failures
160+
161+
**Error**: `ERROR 1305 (42000): FUNCTION mysql.audit_log_reload does not exist`
162+
163+
**Solution**: Ensure the audit log plugin is installed and active:
164+
```{.bash data-prompt="mysql>"}
165+
mysql> SHOW PLUGINS WHERE Name = 'audit_log';
166+
mysql> INSTALL PLUGIN audit_log SONAME 'audit_log.so';
167+
```
168+
169+
### Filter not taking effect
170+
171+
**Symptoms**: Expected events still appear in audit log
172+
173+
**Solutions**:
174+
1. Verify the filter is properly assigned to the user:
175+
```{.bash data-prompt="mysql>"}
176+
mysql> SELECT * FROM mysql.audit_log_user WHERE username = 'your_user';
177+
```
178+
179+
2. Check for conflicting filters or default mappings:
180+
```{.bash data-prompt="mysql>"}
181+
mysql> SELECT * FROM mysql.audit_log_user WHERE userhost = '%';
182+
```
183+
184+
3. Ensure the plugin reload completed successfully:
185+
```{.bash data-prompt="mysql>"}
186+
mysql> CALL mysql.audit_log_reload();
187+
```
188+
189+
## Troubleshoot and checklist
190+
191+
* Define the JSON filter with the desired global `log` flag.
192+
193+
* Add class entries for any event type that must be treated differently from the global setting.
194+
195+
* Insert the filter into `mysql.audit_log_filter` with a unique `filter_id`.
196+
197+
* Map the filter to the appropriate MySQL accounts in `mysql.audit_log_user`.
198+
199+
* Reload the audit plugin to apply the changes.
200+
201+
* Test the configuration with a fresh connection and inspect the audit log.
202+
203+
This checklist ensures that the audit log filter records exactly the events you need while omitting unnecessary noise.

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)