Add default db field to audit log records

Registered by Sergei Glushchenko

Description
~~~~~~~~~~~

  Add field `DB' for `MYSQL_AUDIT_GENERAL_STATUS' records.

  Example old format XML:

  ,----
  | <AUDIT_RECORD
  | NAME="Query"
  | RECORD="9_2016-06-29T17:37:10"
  | TIMESTAMP="2016-06-29T17:37:57 UTC"
  | COMMAND_CLASS="select"
  | CONNECTION_ID="2"
  | STATUS="0"
  | SQLTEXT="select 1"
  | USER="root[root] @ localhost []"
  | HOST="localhost"
  | OS_USER=""
  | IP=""
  | DB="test"
  | />
  `----

  Example JSON:

  ,----
  | {
  | "audit_record": {
  | "name": "Query",
  | "record": "11_2016-06-29T17:47:15",
  | "timestamp": "2016-06-29T17:47:54 UTC",
  | "command_class": "select",
  | "connection_id": "1",
  | "status": 0,
  | "sqltext": "select 1",
  | "user": "root[root] @ localhost []",
  | "host": "localhost",
  | "os_user": "",
  | "ip": "",
  | "db": "db"
  | }
  | }
  `----

  `DB' will be empty for connections which were live upon plugin
  installation.

Implementation
~~~~~~~~~~~~~~

  Since there is no `db' field for `MYSQL_AUDIT_GENERAL_CLASS' events,
  we need to keep track of default database changes inside the plugin.
  Current database can be set at connection time, with `change user'
  command and with `mysql_select_db' command (`COM_INIT_DB').

code
----

  - for successful `AUDIT_LOG_CONNECTTION_CLASS' events capture
    `database' value
  - for `MYSQL_AUDIT_GENERAL_CLASS' events having command `Init DB'
    capture `general_query' field. Note that `general_query' field is
    set only for `MYSQL_AUDIT_GENERAL_LOG' subclass, but not for
    `MYSQL_AUDIT_GENERAL_STATUS', so we need to capture this value from
    `MYSQL_AUDIT_GENERAL_LOG' event. `MYSQL_AUDIT_GENERAL_LOG' is not
    emitted if `Init DB' failed with error
  - query `use db1' changes default database as well. It generates log
    event `Query' with `sql_command' `change_db' and query `use db1'
    (could be `USE db1' or even `uSe `db`').
  - add `DB' to every supported log format as a last field

testing
-------

  - create test case executing `create database', `change user',
    `connect' with different default database. Record and compare logged
    output. Issue is that `mysql_select_db' is not available from test
    scenario, but we can probably run mysql cli in batch mode.
  - test case should also execute `INSTALL PLUGIN' and `UNINSTALL
           PLUGIN' to make sure there is no garbage and no crash when
           logging connections which were live upon plugin installation.
  - correctness of formatting will be verified by existing `_old',
    `_new', `_json' and `_csv' tests.

Blueprint information

Status:
Complete
Approver:
Laurynas Biveinis
Priority:
Medium
Drafter:
Sergei Glushchenko
Direction:
Approved
Assignee:
Sergei Glushchenko
Definition:
Approved
Series goal:
Accepted for 5.6
Implementation:
Implemented
Milestone target:
None
Started by
Sergei Glushchenko
Completed by
Sergei Glushchenko

Related branches

Sprints

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.

Subscribers

No subscribers.