2 Ocak 2023 Pazartesi

Audit Trails

Giriş
Audit Trail için iki seçenek var
1. Ayarları elle yapmak
2. pg_audit extension kullanmak

1. Ayarları elle yapmak
Şöyle yaparız
> show log_statement;
> set log_statement = 'all';

// log destination is stderr
> show log_destination;

//Show logger pattern
> show log_line_prefix;
%m [%p]
Logger pattern'ı değiştirmek için postgresql.conf dosyasında şöyle yaparız
# Locate the line log_line_prefix and change to the following
log_line_prefix = '(%m,%p,%d,%u,%r)'
Ayrıca bu dosyada tanımlanabilecek diğer bazı değişkenlerin açıklaması şöyle
log_statement_sample_rate: value between 0 and 1. This is for sampling some statements to log. Useful if you have a lot of loads.

log_destination: we can specify the file path where store the log and the format as well. I.e: jsonlog /mnt/logs/postgres.json . plain, JSON and CSV are the format supported at this moment.

2. pg_audit Extension
Açıklaması şöyle
pgaudit (PostgreSQL Audit) provides detailed logging of database activities, including SELECT, INSERT, UPDATE, DELETE, and DDL commands.
Çıktılar log dosyalarına yazılır. Açıklaması şöyle
Captures detailed statement-level or row-level change logs, but outputs them to log files. These logs aren’t directly queryable without additional tooling, and enabling full data change capture requires extra configuration.
Bitnami ile bu extension kurulu geliyor. Şöyle yaparız
$ docker run -d \
  --name postgres-pg-audit \
  -e POSTGRESQL_PASSWORD=postgres \
  -e POSTGRESQL_PGAUDIT_LOG="read, ddl" \
  bitnami/postgresql:latest
Aslında pg_audit'in avantajı şöyle
define a logging policy per object
Örnek
Şöyle yaparız
>  create role auditor;

> set pgaudit.role = 'auditor';

> set pgaudit.log = '';

> grant select (password) on public.account to auditor;


Hiç yorum yok:

Yorum Gönder