PG审计插件之pgaudit

0    605    1

Tags:

👉 本文共约4349个字,系统预计阅读时间或需17分钟。

简介

https://www.pgaudit.org/

https://github.com/pgaudit/pgaudit

PostgreSQL可以通过log_statement=all 提供日志审计,但是没有提供审计要求的详细程度。PostgreSQL Audit Extension (pgAudit)能够提供详细的会话和对象审计日志,是PG的一个扩展插件。pgAudit通过标准PostgreSQL日志记录工具提供详细的会话和/或对象审核日志记录。

注意:pgAudit可能会生成大量日志。请谨慎确定要在您的环境中记录哪些审核内容,以避免过多记录,可以根据需要开启审计,关闭审计设置pgaudit.log=’none’,并重新加载即可。

pgAudit版本支持的PostgreSQL主要版本:

  • pgAudit v1.6.X is intended to support PostgreSQL 14.
  • pgAudit v1.5.X is intended to support PostgreSQL 13.
  • pgAudit v1.4.X is intended to support PostgreSQL 12.
  • pgAudit v1.3.X is intended to support PostgreSQL 11.
  • pgAudit v1.2.X is intended to support PostgreSQL 10.
  • pgAudit v1.1.X is intended to support PostgreSQL 9.6.
  • pgAudit v1.0.X is intended to support PostgreSQL 9.5.

注意版本和数据库的匹配,最新的v.1.6.X版本并不支持PG13版本,编译会报错:pgaudit.c:1556:38: error: incompatible type for argument 4 of ‘next_ProcessUtility_hook’。

pgaudit 安装

https://www.pgaudit.org/

https://github.com/pgaudit/pgaudit

过程:

配置开启审计

分为会话和对象审计。

会话审计日志记录

会话审计日志提供用户在后端执行的所有语句的详细日志。使用pgaudit.log设置启用会话日志记录。

日志输出:

对象审计日志记录

影响特定关系的对象审计日志记录语句。只支持 SELECT, INSERT, UPDATEDELETE 命令。对象审计日志中不包括 TRUNCATE

对象审计日志记录旨在成为pgaudit.log = 'read, write'的细粒度替代。因此,将它们结合使用可能没有任何意义,但是一种可能的场景是使用会话日志记录来捕获每个语句,然后用对象日志记录来补充这些语句,以获得关于特定关系的更多细节。

对象级审计日志是通过角色系统实现的。pgaudit.role 设置定义用于审计日志记录的角色。当审计角色对执行的命令具有权限或从另一个角色继承权限时,将记录一个关系(表、视图等)。这允许您有效地拥有多个审计角色,即使在任何上下文中只有一个主角色。

设置pgaudit.role为auditor,并授予account表的SELECT和DELETE权限。account表上的任何SELECT或DELETE语句都将被记录:

日志输出:

相关配置参数

Settings may be modified only by a superuser. Allowing normal users to change their settings would defeat the point of an audit log.

Settings can be specified globally (in postgresql.conf or using ALTER SYSTEM ... SET), at the database level (using ALTER DATABASE ... SET), or at the role level (using ALTER ROLE ... SET). Note that settings are not inherited through normal role inheritance and SET ROLE will not alter a user's pgAudit settings. This is a limitation of the roles system and not inherent to pgAudit.

The pgAudit extension must be loaded in shared_preload_libraries. Otherwise, an error will be raised at load time and no audit logging will occur. In addition, CREATE EXTENSION pgaudit must be called before pgaudit.log is set. If the pgaudit extension is dropped and needs to be recreated then pgaudit.log must be unset first otherwise an error will be raised.

pgaudit.log

Specifies which classes of statements will be logged by session audit logging. Possible values are:

  • READ: SELECT and COPY when the source is a relation or a query.
  • WRITE: INSERT, UPDATE, DELETE, TRUNCATE, and COPY when the destination is a relation.
  • FUNCTION: Function calls and DO blocks.
  • ROLE: Statements related to roles and privileges: GRANT, REVOKE, CREATE/ALTER/DROP ROLE.
  • DDL: All DDL that is not included in the ROLE class.
  • MISC: Miscellaneous commands, e.g. DISCARD, FETCH, CHECKPOINT, VACUUM, SET.
  • MISC_SET: Miscellaneous SET commands, e.g. SET ROLE.
  • ALL: Include all of the above.

Multiple classes can be provided using a comma-separated list and classes can be subtracted by prefacing the class with a - sign (see Session Audit Logging).

The default is none.

本人提供Oracle、MySQL、PG等数据库的培训和考证业务,私聊QQ646634621或微信db_bao,谢谢!

pgaudit.log_catalog

指定如果语句中的所有关系都在pg_catalog中,则应该启用会话日志记录。禁用此设置将减少psql和PgAdmin等工具在日志中大量查询catalog的噪音。

默认值为 on.

pgaudit.log_client

指定日志消息是否对客户端进程(如 psql)可见。此设置通常应保持禁用状态,但可能用于调试或其他目的。请注意,pgaudit.log_level 仅在 pgaudit.log_client 打开时启用。

默认值为关闭。

pgaudit.log_level

指定将用于日志条目的日志级别 (详见有效级别的消息严重级别),但注意不允许出现 ERROR, FATAL, 和PANIC 。此设置用于回归测试,对于测试或其他目的的最终用户也可能有用。

默认值为 log.

pgaudit.log_level 取值:

SeverityUsagesyslogeventlog
DEBUG1..DEBUG5Provides successively-more-detailed information for use by developers.DEBUGINFORMATION
INFOProvides information implicitly requested by the user, e.g., output from VACUUM VERBOSE.INFOINFORMATION
NOTICEProvides information that might be helpful to users, e.g., notice of truncation of long identifiers.NOTICEINFORMATION
WARNINGProvides warnings of likely problems, e.g., COMMIT outside a transaction block.NOTICEWARNING
ERRORReports an error that caused the current command to abort.WARNINGERROR
LOGReports information of interest to administrators, e.g., checkpoint activity.INFOINFORMATION
FATALReports an error that caused the current session to abort.ERRERROR
PANICReports an error that caused all database sessions to abort.CRITERROR

pgaudit.log_parameter

指定审计日志记录应该包括与语句一起传递的参数。当参数出现时,它们将包含在语句文本之后的CSV格式中。

默认值为 off.

pgaudit.log_relation

指定会话审计日志记录是否应该为SELECT或DML语句中引用的每个关系(表、视图等)创建单独的日志条目。对于不使用对象审计日志记录的详尽日志记录,这是一个有用的快捷方式。

默认值为 off.

pgaudit.log_rows

指定审核日志记录应包括语句检索或影响的行。启用后,行字段将包含在参数字段之后。

默认值为关闭。

pgaudit.log_statement

指定日志记录是否将包括语句文本和参数(如果启用)。根据要求,审核日志可能不需要此项,这会使日志不那么详细。

默认值为打开。

pgaudit.log_statement_once

指定日志记录是包含带有语句/子语句组合的第一个日志条目的语句文本和参数,还是包含每个条目。禁用此设置将减少冗长的日志记录,但可能会使确定生成日志条目的语句变得更加困难,尽管语句/子语句对以及进程id应该足以识别与前一个条目一起记录的语句文本。

默认值 off.

pgaudit.role

指定用于对象审计日志记录的主角色。可以通过将多个审计角色授予主角色来定义它们。这允许多个组负责审计日志记录的不同方面。

该项没有默认值.

日志格式

审计条目被写入标准日志记录工具,并以逗号分隔的格式包含以下列。只有在删除每个日志条目的日志行前缀部分时,输出才符合CSV格式。

  • AUDIT_TYPE - 会话或对象.
  • STATEMENT_ID - 此会话的唯一语句ID。每个语句ID表示后端调用。即使没有记录某些语句,语句id也是连续的。当记录多个关系时,语句ID可能有多个条目。
  • SUBSTATEMENT_ID - 主语句中每个子语句的顺序ID。例如,从一个查询中调用函数。即使没有记录一些子语句,子语句id也是连续的。当记录多个关系时,子语句ID可能有多个条目。
  • CLASS - 例如 READ, ROLE (详见 pgaudit.log).
  • COMMAND - 例如 ALTER TABLE, SELECT
  • OBJECT_TYPE - TABLE, INDEX, VIEW等. 可用于SELECT、DML和大多数DDL语句。
  • OBJECT_NAME - 完全限定对象名(例如public.account)。可用于SELECT、DML和大多数DDL语句。
  • STATEMENT - 在后端执行的语句。
  • PARAMETER - 如果设置了pgaudit.log_parameter 后,该字段将包含引用CSV的语句参数。

使用log_line_prefix添加满足审计日志需求所需的任何其他字段。典型的日志行前缀可能是 '\%m \%u \%d: ' ,它将为每个审计日志提供日期/时间、用户名和数据库名。

参考

参考链接:

https://access.crunchydata.com/documentation/pgaudit/1.0.6/#object-audit-logging

https://severalnines.com/database-blog/postgresql-audit-logging-best-practices

标签:

头像

小麦苗

学习或考证,均可联系麦老师,请加微信db_bao或QQ646634621

您可能还喜欢...

发表回复

嘿,我是小麦,需要帮助随时找我哦
  • 18509239930
  • 个人微信

  • 麦老师QQ聊天
  • 个人邮箱
  • 点击加入QQ群
  • 个人微店

  • 回到顶部
返回顶部