注意
MongoDB Atlas 中的审核
MongoDB Atlas supports auditing for all M10 and larger clusters. Atlas supports specifying a JSON-formatted audit filter as documented below and using the Atlas audit filter builder for simplified auditing configuration. To learn more, see the Atlas documentation for Set Up Database Auditing and Configure a Custom Auditing Filter.
MongoDB Enterprise 支持审核各种操作。启用后,默认情况下审核工具会记录所有可审核的操作,详见“审核事件操作、详情和结果”。您可以指定事件过滤器来限制记录的事件。
You can configure audit filters at startup or you can configure MongoDB to allow filter configuration at runtime.
审计过滤器语法
Audit filters have the same form as query predicate documents specified to find commands. To see example audit filters, see Examples.
注意
MongoDB Ops Manager 和 Cloud Manager 部署的过滤器
If you configure an audit filter through Ops Manager or Cloud Manager, the filter must be valid JSON. Add double quotes around each field name in the examples on this page, as in { "atype": "authenticate", "param.db": "test" }. To learn more, see the Ops Manager manual.
运行时的过滤器配置
从 MongoDB 5.0 开始,可在运行时配置 mongod 和 mongos 节点的审核配置。一组这样的节点可以参与分布式审核配置。
要将节点包含在分布式审核配置中,请按如下方式更新节点的配置文件并重新启动服务器。
Parameter | 值 |
|---|---|
| |
未设置 | |
未设置 |
如果出现以下情况,服务器将记录错误并无法启动:
runtimeConfiguration为true并且
要在运行时修改审核筛选器和 auditAuthorizationSuccess 参数,请参阅 auditConfig。
系统启动时的筛选器配置
审核过滤器可以在命令行上指定,也可以在用于启动 mongod 或 mongos 实例的配置文件中指定。
配置文件使用
筛选器可在配置文件的 auditLog 会话下以 YAML 格式指定。有关配置示例,请参阅以下示例。
注意
如果启用 runtimeConfiguration,则配置文件不能用于指定审核筛选器。
示例
记录所有可审计事件
要记录所有可审核的事件,请勿指定审核过滤器。默认情况下,审核工具会记录所有可审核的操作。
筛选多种操作类型
以下示例通过筛选器仅审核 createCollection 和 dropCollection 操作:
{ atype: { $in: [ "createCollection", "dropCollection" ] } }
要指定 Atlas 审核过滤器,请将过滤器文档括在单引号 中,以将文档作为字符串传递。
mongod --dbpath data/db --auditDestination file --auditFilter '{ atype: { $in: [ "createCollection", "dropCollection" ] } }' --auditFormat BSON --auditPath data/db/auditLog.bson
根据配置要求包括其他选项。例如,如果希望远程客户端连接到您的部署,或是您的部署成员运行在不同主机上,请指定 --bind_ip。
要在配置文件中指定审核筛选器,必须使用配置文件的 YAML 格式。
storage: dbPath: data/db auditLog: destination: file format: BSON path: data/db/auditLog.bson filter: '{ atype: { $in: [ "createCollection", "dropCollection" ] } }'
过滤单个数据库上的身份验证操作
<field> 可以包括审核信息中的任何字段。对于身份验证操作(即 atype: "authenticate"),审核信息包括 param 文档中的 db 字段。
以下示例通过使用筛选器,只审核针对 test 数据库进行的 authenticate 操作:
{ atype: "authenticate", "param.db": "test" }
要指定 Atlas 审核过滤器,请将过滤器文档括在单引号 中,以将文档作为字符串传递。
mongod --dbpath data/db --auth --auditDestination file --auditFilter '{ atype: "authenticate", "param.db": "test" }' --auditFormat BSON --auditPath data/db/auditLog.bson
根据配置要求包括其他选项。例如,如果希望远程客户端连接到您的部署,或是您的部署成员运行在不同主机上,请指定 --bind_ip。
要在配置文件中指定审核筛选器,必须使用配置文件的 YAML 格式。
storage: dbPath: data/db security: authorization: enabled auditLog: destination: file format: BSON path: data/db/auditLog.bson filter: '{ atype: "authenticate", "param.db": "test" }'
要过滤数据库中的所有 authenticate 操作,请省略 "param.db": "test" 并使用过滤器 { atype: "authenticate" }。
过滤单个数据库的集合创建和删除操作
<field> 可以包括审核信息中的任何字段。对于集合创建和删除操作(即 atype: "createCollection" 和 atype: "dropCollection"),审核信息包括 param 文档中的命名空间 ns 字段。
以下示例通过使用过滤器,只审核针对 test 数据库进行的 createCollection 和 dropCollection 操作:
注意
正则表达式需要两个反斜杠 (\\) 来转义点 (.)。
{ atype: { $in: [ "createCollection", "dropCollection" ] }, "param.ns": /^test\\./ }
要指定 Atlas 审核过滤器,请将过滤器文档括在单引号 中,以将文档作为字符串传递。
mongod --dbpath data/db --auth --auditDestination file --auditFilter '{ atype: { $in: [ "createCollection", "dropCollection" ] }, "param.ns": /^test\\./ }' --auditFormat BSON --auditPath data/db/auditLog.bson
根据配置要求包括其他选项。例如,如果希望远程客户端连接到您的部署,或是您的部署成员运行在不同主机上,请指定 --bind_ip。
要在配置文件中指定审核筛选器,必须使用配置文件的 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\\./ }'
按授权角色筛选
以下示例使用过滤器审核具有 readWrite 角色的用户(包括具有从 readWrite 继承的角色的用户)对 test 数据库的操作:
{ roles: { role: "readWrite", db: "test" } }
要指定 Atlas 审核过滤器,请将过滤器文档括在单引号 中,以将文档作为字符串传递。
mongod --dbpath data/db --auth --auditDestination file --auditFilter '{ roles: { role: "readWrite", db: "test" } }' --auditFormat BSON --auditPath data/db/auditLog.bson
根据配置要求包括其他选项。例如,如果希望远程客户端连接到您的部署,或是您的部署成员运行在不同主机上,请指定 --bind_ip。
要在配置文件中指定审核筛选器,必须使用配置文件的 YAML 格式。
storage: dbPath: data/db security: authorization: enabled auditLog: destination: file format: BSON path: data/db/auditLog.bson filter: '{ roles: { role: "readWrite", db: "test" } }'
筛选读写操作
To capture read and write operations in the audit, you must also enable the audit system to log authorization successes using the auditAuthorizationSuccess parameter. [1]
注意
启用 auditAuthorizationSuccess 比仅记录授权失败更能降低性能。
该筛选器审核多个读写操作:
{ atype: "authCheck", "param.command": { $in: [ "find", "insert", "delete", "update", "findAndModify" ] } }
经审核的操作包括:
要指定 Atlas 审核过滤器,请将过滤器文档括在单引号 中,以将文档作为字符串传递。
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
根据配置要求包括其他选项。例如,如果希望远程客户端连接到您的部署,或是您的部署成员运行在不同主机上,请指定 --bind_ip。
要在配置文件中指定审核筛选器,必须使用配置文件的 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 }
对集合的读写操作进行筛选
To capture read and write operations in the audit, you must also enable the audit system to log authorization successes using the auditAuthorizationSuccess parameter. [1]
注意
启用 auditAuthorizationSuccess 比仅记录授权失败更能降低性能。
此过滤器对 test 数据库中的 orders 集合执行的多个读取和写入操作进行审核:
{ atype: "authCheck", "param.ns": "test.orders", "param.command": { $in: [ "find", "insert", "delete", "update", "findAndModify" ] } }
经审核的操作包括:
要指定 Atlas 审核过滤器,请将过滤器文档括在单引号 中,以将文档作为字符串传递。
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
根据配置要求包括其他选项。例如,如果希望远程客户端连接到您的部署,或是您的部署成员运行在不同主机上,请指定 --bind_ip。
要在配置文件中指定审核筛选器,必须使用配置文件的 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 }
过滤 OCSF 架构日志消息
从 MongoDB 8.0 开始,MongoDB 可以通过 OCSF 格式写入日志消息。OCSF 模式包含与默认 mongo 模式不同的字段。
以下审核过滤器捕获 OCSF模式中记录的网络活动操作:
{ category_uid: 4 }
要指定 Atlas 审核过滤器,请将过滤器文档括在单引号 中,以将文档作为字符串传递。
mongod --dbpath data/db --auth --setParameter auditAuthorizationSuccess=true --auditDestination file --auditFilter '{ category_uid: 4 }' --auditFormat JSON --auditSchema OCSF --auditPath data/db/auditLog.json
要在配置文件中指定审核筛选器,必须使用配置文件的 YAML 格式。
storage: dbPath: data/db security: authorization: enabled auditLog: destination: file format: JSON path: data/db/auditLog.json filter: '{ category_uid: 4 }' schema: OCSF setParameter: { auditAuthorizationSuccess: true }
有关 OCSF 日志消息的更多信息,请参阅 OCSF 架构审核消息。
指定顶级查询操作符 ($or)
要过滤多个审核消息字段,您可以指定顶级查询操作符,例如 $or 。例如,以下过滤器捕获 atype 为 authenticate 或该操作由具有 readWrite 角色的用户执行的操作:
{ $or: [ { atype: "authenticate" }, { "roles.role": "readWrite" } ] }
要指定 Atlas 审核过滤器,请将过滤器文档括在单引号 中,以将文档作为字符串传递。
mongod --dbpath data/db --auth --setParameter auditAuthorizationSuccess=true --auditDestination file --auditFilter '{ $or: [ { atype: "authenticate" }, { "roles.role": "readWrite" } ] }' --auditFormat BSON --auditPath data/db/auditLog.bson
根据配置要求包括其他选项。例如,如果希望远程客户端连接到您的部署,或是您的部署成员运行在不同主机上,请指定 --bind_ip。
要在配置文件中指定审核筛选器,必须使用配置文件的 YAML 格式。
storage: dbPath: data/db security: authorization: enabled auditLog: destination: file format: BSON path: data/db/auditLog.bson filter: '{ $or: [ { atype: "authenticate" }, { "roles.role": "readWrite" } ] }'
了解详情
| [1] | (1, 2) You can enable auditAuthorizationSuccess parameter without enabling --auth; however, all operations will return success for authorization checks. |