Configure Audit Filters配置审核筛选器

On this page本页内容

Note注意
Auditing in MongoDB AtlasMongoDB Atlas中的审计

MongoDB Atlas supports auditing for all M10 and larger clusters. MongoDB Atlas支持所有M10和更大集群的审计。Atlas supports specifying a JSON-formatted audit filter as documented below and using the Atlas audit filter builder for simplified auditing configuration. Atlas支持指定JSON格式的审核筛选器,如下所述,并使用Atlas审核筛选器生成器简化审核配置。To learn more, see the Atlas documentation for Set Up Database Auditing and Configure a Custom Auditing Filter.要了解更多信息,请参阅Atlas文档以设置数据库审核配置自定义审核筛选器

MongoDB Enterprise supports auditing of various operations. MongoDB企业版支持对各种操作的审计。When enabled, the audit facility, by default, records all auditable operations as detailed in Audit Event Actions, Details, and Results. 启用后,默认情况下,审核功能将记录所有可审核操作,如审核事件操作、详细信息和结果中所述。You can specify event filters to limit which events are recorded. 您可以指定事件筛选器以限制记录的事件。Filters can be configured at startup or MongoDB can be configured to allow runtime specification.筛选器可以在启动时配置,或者MongoDB可以配置为允许运行时规范

Filter Configuration at Runtime运行时的筛选器配置

Starting in MongoDB 5.0, audit configurations for mongod and mongos nodes can be configured at runtime. 从MongoDB 5.0开始,可以在运行时配置mongodmongos节点的审计配置。A group of these nodes can take part in a distributed audit configuration.这些节点的一组可以参与分布式审计配置。

To include a node in a distributed audit configuration, update the node's configuration file as follows and restart the server.要在分布式审核配置中包含节点,请按以下方式更新节点的配置文件,然后重新启动服务器。

Parameter参数Value
auditLog.runtimeConfigurationtrue
auditLog.filterUnset
auditAuthorizationSuccessUnset

The server logs an error and fails to start if:如果出现以下情况,服务器将记录错误并无法启动:

To modify audit filters and the auditAuthorizationSuccess parameter at runtime, see setAuditConfig.要在运行时修改审核筛选器和auditAuthorizationSuccess参数,请参阅setAuditConfig

Filter Configuration at System Startup系统启动时的筛选器配置

Audit filters can be specified on the command line or else in the configuration file used to start the mongod or mongos instance.审计筛选器可以在命令行或用于启动mongodmongos实例的配置文件中指定。

Configuration File Usage配置文件用法

Filters can be specified in YAML under the auditLog session of the configuration file. 筛选器可以在配置文件auditLog会话下的YAML中指定。See the examples below for sample configurations.有关示例配置,请参阅下面的示例。

Note注意

If runtimeConfiguration is enabled, then the configuration file cannot be used to specify audit filters.如果启用了runtimeConfiguration,则配置文件不能用于指定审核筛选器。

Examples示例

Filter for Multiple Operation Types筛选多个操作类型

The following example audits only the createCollection and dropCollection actions by using the filter:以下示例通过使用筛选器仅审核createCollectiondropCollection操作:

{ atype: { $in: [ "createCollection", "dropCollection" ] } }

To specify an audit filter, enclose the filter document in single quotes to pass the document as a string.要指定审核筛选器,请将筛选器文档用单引号括起来,以字符串形式传递文档。

mongod --dbpath data/db --auditDestination file --auditFilter '{ atype: { $in: [ "createCollection", "dropCollection" ] } }' --auditFormat BSON --auditPath data/db/auditLog.bson

Include additional options as required for your configuration. 包括配置所需的其他选项。For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the --bind_ip. 例如,如果希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定--bind_ipFor more information, see Localhost Binding Compatibility Changes.有关详细信息,请参阅本地主机绑定兼容性更改

To specify the audit filter in a configuration file, you must use the YAML format of the configuration file.要在配置文件中指定审核筛选器,必须使用配置文件的YAML格式。

storage:
   dbPath: data/db
auditLog:
   destination: file
   format: BSON
   path: data/db/auditLog.bson
   filter: '{ atype: { $in: [ "createCollection", "dropCollection" ] } }'

Filter on Authentication Operations on a Single Database筛选单个数据库上的身份验证操作

The <field> can include any field in the audit message. <field>可以包括审核消息中的任何字段For authentication operations (i.e. atype: "authenticate"), the audit messages include a db field in the param document.对于身份验证操作(即atype: "authenticate"),审核消息在param文档中包含一个db字段。

The following example audits only the authenticate operations that occur against the test database by using the filter:以下示例通过使用筛选器仅审核针对test数据库发生的authenticate(身份验证)操作:

{ atype: "authenticate", "param.db": "test" }

To specify an audit filter, enclose the filter document in single quotes to pass the document as a string.要指定审核筛选器,请将筛选器文档用单引号括起来,以字符串形式传递文档。

mongod --dbpath data/db --auth --auditDestination file --auditFilter '{ atype: "authenticate", "param.db": "test" }' --auditFormat BSON --auditPath data/db/auditLog.bson

Include additional options as required for your configuration. 包括配置所需的其他选项。For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the --bind_ip. 例如,如果希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定--bind_ipFor more information, see Localhost Binding Compatibility Changes.有关详细信息,请参阅本地主机绑定兼容性更改

To specify the audit filter in a configuration file, you must use the YAML format of the configuration file.要在配置文件中指定审核筛选器,必须使用配置文件的YAML格式。

storage:
   dbPath: data/db
security:
   authorization: enabled
auditLog:
   destination: file
   format: BSON
   path: data/db/auditLog.bson
   filter: '{ atype: "authenticate", "param.db": "test" }'

To filter on all authenticate operations across databases, omit "param.db": "test" and use the filter { atype: "authenticate" }.要筛选跨数据库的所有authenticate(身份验证)操作,请省略"param.db": "test",并使用筛选器{ atype: "authenticate" }

Filter on Collection Creation and Drop Operations for a Single Database筛选单个数据库的集合创建和删除操作

The <field> can include any field in the audit message. <field>可以包括审核消息中的任何字段For collection creation and drop operations (i.e. atype: "createCollection" and atype: "dropCollection"), the audit messages include a namespace ns field in the param document.对于集合创建和删除操作(即atype: "createCollection"atype: "dropCollection"),审核消息在param文档中包含一个名称空间ns字段。

The following example audits only the createCollection and dropCollection operations that occur against the test database by using the filter:以下示例通过使用筛选器仅审核针对test数据库发生的createCollectiondropCollection操作:

Note注意

The regular expression requires two backslashes (\\) to escape the dot (.).正则表达式需要两个反斜杠(\\)来转义点(.)。

{ atype: { $in: [ "createCollection", "dropCollection" ] }, "param.ns": /^test\\./ } }

To specify an audit filter, enclose the filter document in single quotes to pass the document as a string.要指定审核筛选器,请将筛选器文档用单引号括起来,以字符串形式传递文档。

mongod --dbpath data/db --auth --auditDestination file --auditFilter '{ atype: { $in: [ "createCollection", "dropCollection" ] }, "param.ns": /^test\\./ } }' --auditFormat BSON --auditPath data/db/auditLog.bson

Include additional options as required for your configuration. 包括配置所需的其他选项。For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the --bind_ip. 例如,如果希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定--bind_ipFor more information, see Localhost Binding Compatibility Changes.有关详细信息,请参阅本地主机绑定兼容性更改

To specify the audit filter in a configuration file, you must use the YAML format of the configuration file.要在配置文件中指定审核筛选器,必须使用配置文件的YAML格式。

storage:
   dbPath: data/db
security:
   authorization: enabled
auditLog:
   destination: file
   format: BSON
   path: data/db/auditLog.bson
   filter: '{ atype: { $in: [ "createCollection", "dropCollection" ] }, "param.ns": /^test\\./ } }'

Filter by Authorization Role按授权角色筛选

The following example audits operations by users with readWrite role on the test database, including users with roles that inherit from readWrite, by using the filter:以下示例使用筛选器审核test数据库中具有readWrite角色的用户的操作,包括具有从readWrite继承的角色的用户:

{ roles: { role: "readWrite", db: "test" } }

To specify an audit filter, enclose the filter document in single quotes to pass the document as a string.要指定审核筛选器,请将筛选器文档用单引号括起来,以字符串形式传递文档。

mongod --dbpath data/db --auth --auditDestination file --auditFilter '{ roles: { role: "readWrite", db: "test" } }' --auditFormat BSON --auditPath data/db/auditLog.bson

Include additional options as required for your configuration. 包括配置所需的其他选项。For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the --bind_ip. 例如,如果希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定--bind_ipFor more information, see Localhost Binding Compatibility Changes.有关详细信息,请参阅本地主机绑定兼容性更改

To specify the audit filter in a configuration file, you must use the YAML format of the configuration file.要在配置文件中指定审核筛选器,必须使用配置文件的YAML格式。

storage:
   dbPath: data/db
security:
   authorization: enabled
auditLog:
   destination: file
   format: BSON
   path: data/db/auditLog.bson
   filter: '{ roles: { role: "readWrite", db: "test" } }'

Filter on Read and Write Operations筛选读写操作

To capture read and write operations in the audit, you must also enable the audit system to log authorization successes using the auditAuthorizationSuccess parameter. 要捕获审核中的读和写操作,还必须启用审核系统使用auditAuthorizationSuccess参数记录授权成功。[1]

Note注意

Enabling auditAuthorizationSuccess degrades performance more than logging only the authorization failures.与仅记录授权失败相比,启用auditAuthorizationSuccess会降低性能。

This filter audits multiple read and write operations:此筛选器审核多个读写操作:

{
   atype: "authCheck",
   "param.command": { $in: [ "find", "insert", "delete", "update", "findandmodify" ] }
}

The audited operations include:经审计的业务包括:

To specify an audit filter, enclose the filter document in single quotes to pass the document as a string.要指定审核筛选器,请将筛选器文档用单引号括起来,以字符串形式传递文档。

mongod --dbpath data/db --auth --setParameter auditAuthorizationSuccess=true --auditDestination file --auditFilter '{ atype: "authCheck", "param.command": { $in: [ "find", "insert", "delete", "update", "findandmodify" ] } }' --auditFormat BSON --auditPath data/db/auditLog.bson

Include additional options as required for your configuration. 包括配置所需的其他选项。For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the --bind_ip. 例如,如果希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定--bind_ipFor more information, see Localhost Binding Compatibility Changes.有关详细信息,请参阅本地主机绑定兼容性更改

To specify the audit filter in a configuration file, you must use the YAML format of the configuration file.要在配置文件中指定审核筛选器,必须使用配置文件的YAML格式。

storage:
   dbPath: data/db
security:
   authorization: enabled
auditLog:
   destination: file
   format: BSON
   path: data/db/auditLog.bson
   filter: '{ atype: "authCheck", "param.command": { $in: [ "find", "insert", "delete", "update", "findandmodify" ] } }'
setParameter: { auditAuthorizationSuccess: true }

Filter on Read and Write Operations for a Collection筛选集合的读写操作

To capture read and write operations in the audit, you must also enable the audit system to log authorization successes using the auditAuthorizationSuccess parameter. 要捕获审核中的读和写操作,还必须启用审核系统使用auditAuthorizationSuccess参数记录授权成功。[1]

Note注意

Enabling auditAuthorizationSuccess degrades performance more than logging only the authorization failures.与仅记录授权失败相比,启用auditAuthorizationSuccess会降低性能。

This filter audits multiple read and write operations on the orders collection in the test database:此筛选器审核test数据库中orders集合的多个读写操作:

{
    atype: "authCheck",
    "param.ns": "test.orders",
    "param.command": { $in: [ "find", "insert", "delete", "update", "findandmodify" ] }
}

The audited operations include:经审计的业务包括:

To specify an audit filter, enclose the filter document in single quotes to pass the document as a string.要指定审核筛选器,请将筛选器文档用单引号括起来,以字符串形式传递文档。

mongod --dbpath data/db --auth --setParameter auditAuthorizationSuccess=true --auditDestination file --auditFilter '{ atype: "authCheck", "param.ns": "test.orders", "param.command": { $in: [ "find", "insert", "delete", "update", "findandmodify" ] } }' --auditFormat BSON --auditPath data/db/auditLog.bson

Include additional options as required for your configuration. 包括配置所需的其他选项。For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the --bind_ip. 例如,如果希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定--bind_ipFor more information, see Localhost Binding Compatibility Changes.有关详细信息,请参阅本地主机绑定兼容性更改

To specify the audit filter in a configuration file, you must use the YAML format of the configuration file.要在配置文件中指定审核筛选器,必须使用配置文件的YAML格式。

storage:
   dbPath: data/db
security:
   authorization: enabled
auditLog:
   destination: file
   format: BSON
   path: data/db/auditLog.bson
   filter: '{ atype: "authCheck", "param.ns": "test.orders", "param.command": { $in: [ "find", "insert", "delete", "update", "findandmodify" ] } }'
setParameter: { auditAuthorizationSuccess: true }
[1](1, 2) You can enable auditAuthorizationSuccess parameter without enabling --auth; however, all operations will return success for authorization checks.您可以在不启用--auth的情况下启用auditAuthorizationSuccess参数;但是,对于授权检查,所有操作都将返回成功。
←  Configure AuditingSystem Event Audit Messages →