注意
MongoDB Atlas 中的审核
MongoDB Atlas支持对所有M10和更大的集群进行审核。 Atlas支持指定如下所述的JSON格式的Atlas 审核过滤,并使用AtlasAtlas 审核过滤器构建器来简化审核配置。 要学习;了解更多信息,请参阅Atlas文档中的“设置数据库审核”和“配置自定义审核筛选器”。
MongoDB Enterprise 支持审核各种操作。启用后,默认情况下审核工具会记录所有可审核的操作,详见“审核事件操作、详情和结果”。您可以指定事件筛选器来限制记录的事件。
可以在启动时配置审核过滤器,也可以将 MongoDB 配置为允许在运行时配置过滤器。
审计过滤器语法
审计过滤器的形式与 find 命令指定的查询谓词文档相同。要查看审计过滤器示例,请参阅示例。
注意
MongoDB Ops Manager 和 Cloud Manager 部署的过滤器
如果您通过 MongoDB Ops Manager 或 Cloud Manager 配置 Atlas 审核过滤器,则该过滤器必须是有效的 JSON。在此页面上的示例中,在每个字段名称周围添加 double 引号,如 { "atype": "authenticate", "param.db": "test" } 所示。要了解更多信息,请参阅MongoDB Ops Manager 手册。
运行时的过滤器配置
从 MongoDB 5.0 开始,可在运行时配置 mongod 和 mongos 节点的审核配置。一组这样的节点可以参与分布式审核配置。
要将节点包含在分布式审核配置中,请按如下方式更新节点的配置文件并重新启动服务器。
Parameter | 值 |
|---|---|
| |
未设置 | |
未设置 |
如果出现以下情况,服务器将记录错误并无法启动:
runtimeConfiguration为true并且
要在运行时修改审核筛选器和 auditAuthorizationSuccess 参数,请参阅 setAuditConfig。
系统启动时的筛选器配置
审核过滤器可以在命令行上指定,也可以在用于启动 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/dbauditLog: 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/dbsecurity: authorization: enabledauditLog: 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/dbsecurity: authorization: enabledauditLog: 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/dbsecurity: authorization: enabledauditLog: 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/dbsecurity: authorization: enabledauditLog: 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/dbsecurity: authorization: enabledauditLog: 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 }指定顶级查询操作符 ($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/dbsecurity: authorization: enabledauditLog: 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. |