Crates.io | sql-audit-cli |
lib.rs | sql-audit-cli |
version | 0.1.0 |
source | src |
created_at | 2020-12-17 17:24:35.516086 |
updated_at | 2020-12-17 17:24:35.516086 |
description | Run one command to start tracking all changes in your Postgres database! |
homepage | |
repository | https://github.com/triaxtec/sql-audit |
max_upload_size | |
id | 323970 |
size | 51,318 |
Tracking changes is the whole goal here. When someone inserts, updates, or deletes a record, those changes should be traced and attributable for the user. This serves the dual purposes of auditing who makes changes and providing a way to revert them.
pk
which is type integer
. There are plans to make this more flexible.public
schema of the connected database will be audited.cargo install sql-audit-cli
Create an audit.toml
with content like this:
database = "postgres://username:password@host/database"
exclude_tables = ["test_2"] # Tables to not audit
DO NOT CHECK THIS FILE IN as it obviously contains database credentials of the secure audit account. Future versions should make this config easier (e.g. prompting for credentials).
Run sql-audit
from the command line in the directory containing this file.
sql_audit
will be created in the database with the table audit
.sql_audit_trigger
.public
schema of the connected database will be audited except those listed in the exclude_tables
list in audit.toml
.TRIGGER
on INSERT
, UPDATE
, and DELETE
. Those triggers insert data into the audit
table created for each row with the following columns:
id: a surrogate key
ts: the time of the change
table_name: the table that the change took place on
pk: the pk of the row which the trigger ran on
operation: "INSERT", "UPDATE", "DELETE" depending on the action that triggered the trigger.
db_user: The database user which made the change
app_user: An optional string for storing data about a logged in user (to the app) that made the change. More details in features section.
request_id: An optional arbitrary string representing a specific web request. More details in features.
new_val: A JSON representation of the entire new row (only populated on insert or update).
new_val: A JSON representation of the entire old row (only populated on update or delete).
This setting only lasts for the duration of a transaction.
The point of app_user is to be able to track which users make changes at the application level instead of the database level. Often, an application will use a single set of database credentials regardless of which user is making a web request. Recording the application-level user is therefore often more useful than the database user alone. However, this can be set by whoever is making the query, so only trust this value as much as you trust the db_user
.
Using this is purely at the discretion of the consuming application. Set it by calling sql_audit::set_local_app_user
from Rust or using SELECT set_config('sql_audit.app_user', $1, true)
where $1 is whatever you want to store here. Using a param and binding is strongly recommended since the user name / email is usually some sort of user-provided input. You don't want to run arbitrary SQL with the permissions of the audit user!
This setting only lasts for the duration of a transaction.
This field is here for tracing purposes. When a user makes a web request, you will often log some unique request ID for tracking bugs/changes/whatever. You can also store that unique ID here to track database changes back to a specific web request (or the other way around).
Using this is purely at the discretion of the consuming application. Set it by calling sql_audit::set_local_app_user
from Rust or using SELECT set_config('sql_audit.request_id', $1, true)
where $1 is whatever you want to store here.