Docs 菜单

Docs 主页开发应用程序MongoDB Manual

配置审核筛选器

在此页面上

  • 运行时的过滤器配置
  • 系统启动时的筛选器配置
  • 举例

注意

MongoDB Atlas 中的审核

MongoDB Atlas 支持对所有 M10和更大的集群进行审核。 Atlas 支持指定如下所述的 JSON 格式的审核筛选器,并使用 Atlas 审核筛选器构建器来简化审核配置。要了解更多信息,请参阅 Atlas 文档中的 设置数据库审核配置自定义审核筛选器。

MongoDB Enterprise支持对各种操作进行审核启用后,默认情况下,审核工具会记录所有可审核的操作,详见审核事件操作、详细信息和结果。您可以指定事件筛选器来限制记录的事件。可以在启动时配置筛选器,也可以将 MongoDB 配置为允许运行时规范。

从 MongoDB 5.0 开始,可在运行时配置 mongodmongos 节点的审核配置。一组这样的节点可以参与分布式审核配置。

要将节点包含在分布式审核配置中,请按如下方式更新节点的配置文件并重新启动服务器。

范围
auditLog.runtimeConfiguration
true
未设置
未设置

如果出现以下情况,服务器将记录错误并无法启动:

要在运行时修改审核筛选器和 auditAuthorizationSuccess 参数,请参阅 setAuditConfig

审核过滤器可以在命令行上指定,也可以在用于启动 mongodmongos 实例的配置文件中指定。

筛选器可在配置文件auditLog 会话下以 YAML 格式指定。有关配置示例,请参阅以下示例。

注意

如果启用 runtimeConfiguration,则配置文件不能用于指定审核筛选器。

以下示例通过筛选器仅审核 createCollectiondropCollection 操作:

{ 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 数据库进行的 createCollectiondropCollection 操作:

注意

正则表达式需要两个反斜杠 (\\) 来转义点 (.)。

{ 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" } }'

如需在审核中捕获读取和写入操作,还必须使用 auditAuthorizationSuccess 参数启用审核系统记录授权成功日志。[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 }

如需在审核中捕获读取和写入操作,还必须使用 auditAuthorizationSuccess 参数启用审核系统记录授权成功日志。[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 }

提示

[1](1, 2) 您可以启用 auditAuthorizationSuccess 参数,而不启用 --auth;但是,所有操作都将返回授权检查成功。
← 配置审查