Files
cozystack/packages/apps/postgres/tests/audit_test.yaml
T
IvanHunters 61e0839e0e fix(postgres): validate audit.log and match audit keys case-insensitively
Review on #5404 surfaced three issues with the audit toggle:

- audit.log was a free string, so a mistyped class (write, ddl, rol)
  rendered unchanged and pgaudit fell back to none at the next restart,
  silently disabling auditing. Add a schema pattern over the pgaudit
  class list so a bad class is rejected at admission.
- The explicit-parameter-wins guard matched keys case-sensitively, so a
  user key like pgaudit.LoG landed beside the lowercase seed and
  PostgreSQL, which is case-insensitive, picked the seed. Match the user
  keys lowercased.
- The connection-logging and empty-log guards were untested. Cover
  log_disconnections, an empty pgaudit.log, an empty audit.log and a
  case-variant key.

Also note in the field docs that write/all record statement text, so the
audit trail is readable by anyone with access to the log stack.

Assisted-by: LLM
Signed-off-by: IvanHunters <xorokhotnikov@gmail.com>
2026-11-23 18:34:42 +03:00

191 lines
4.8 KiB
YAML

suite: audit toggle
# audit.enabled is a one-switch bundle for PostgreSQL access auditing. It loads
# the pgaudit extension through the sharedPreloadLibraries allowlist and turns on
# connection logging, so "who connected" (log_connections/log_disconnections) and
# "what they ran" (pgaudit.log classes) land in the instance log, which the
# platform log stack already collects. The pgaudit classes default to
# "write, ddl, role" and are overridable through audit.log; a pgaudit.log set
# directly in postgresql.parameters still wins, since audit only seeds a default.
templates:
- templates/db.yaml
release:
name: pg-test
namespace: tenant-test
set:
_cluster:
scheduling: {}
cluster-domain: cozy.local
tests:
- it: "off by default: no pgaudit preload and no audit parameters"
documentSelector:
path: kind
value: Cluster
asserts:
- notExists:
path: spec.postgresql.shared_preload_libraries
- notExists:
path: spec.postgresql.parameters["pgaudit.log"]
- notExists:
path: spec.postgresql.parameters.log_connections
- it: "enabling it preloads pgaudit and seeds the audit parameters"
set:
audit:
enabled: true
documentSelector:
path: kind
value: Cluster
asserts:
- equal:
path: spec.postgresql.shared_preload_libraries
value:
- pgaudit
- equal:
path: spec.postgresql.parameters["pgaudit.log"]
value: "write, ddl, role"
- equal:
path: spec.postgresql.parameters.log_connections
value: "on"
- equal:
path: spec.postgresql.parameters.log_disconnections
value: "on"
- it: "audit.log overrides the default pgaudit class set"
set:
audit:
enabled: true
log: "all"
documentSelector:
path: kind
value: Cluster
asserts:
- equal:
path: spec.postgresql.parameters["pgaudit.log"]
value: "all"
- it: "pgaudit already in the preload list is not duplicated"
set:
audit:
enabled: true
postgresql:
sharedPreloadLibraries:
- pg_stat_statements
- pgaudit
documentSelector:
path: kind
value: Cluster
asserts:
- equal:
path: spec.postgresql.shared_preload_libraries
value:
- pg_stat_statements
- pgaudit
- it: "an explicit pgaudit.log in parameters wins over the audit default"
set:
audit:
enabled: true
log: "write, ddl, role"
postgresql:
parameters:
pgaudit.log: "ddl"
documentSelector:
path: kind
value: Cluster
asserts:
- equal:
path: spec.postgresql.parameters["pgaudit.log"]
value: "ddl"
- it: "an explicit falsy connection-logging value wins over the audit default"
set:
audit:
enabled: true
postgresql:
parameters:
log_connections: 0
documentSelector:
path: kind
value: Cluster
asserts:
- equal:
path: spec.postgresql.parameters.log_connections
value: "0"
- equal:
path: spec.postgresql.parameters.log_disconnections
value: "on"
- it: "an explicit falsy log_disconnections wins over the audit default"
set:
audit:
enabled: true
postgresql:
parameters:
log_disconnections: 0
documentSelector:
path: kind
value: Cluster
asserts:
- equal:
path: spec.postgresql.parameters.log_disconnections
value: "0"
- it: "an explicit empty pgaudit.log in parameters wins over the audit default"
set:
audit:
enabled: true
postgresql:
parameters:
pgaudit.log: ""
documentSelector:
path: kind
value: Cluster
asserts:
- equal:
path: spec.postgresql.parameters["pgaudit.log"]
value: ""
- it: "an empty audit.log falls back to the default classes"
set:
audit:
enabled: true
log: ""
documentSelector:
path: kind
value: Cluster
asserts:
- equal:
path: spec.postgresql.parameters["pgaudit.log"]
value: "write, ddl, role"
- it: "a case-variant parameter key suppresses the lowercase audit seed"
set:
audit:
enabled: true
postgresql:
parameters:
pgaudit.LoG: "all"
documentSelector:
path: kind
value: Cluster
asserts:
- equal:
path: spec.postgresql.parameters["pgaudit.LoG"]
value: "all"
- notExists:
path: spec.postgresql.parameters["pgaudit.log"]
- it: "a mistyped audit class is rejected by the schema"
set:
audit:
enabled: true
log: "write, ddl, rol"
asserts:
- failedTemplate:
errorPattern: "audit/log"