文档菜单
文档首页
/
MongoDB 手册
/ / /

在自托管部署上配置审计过滤器

本页内容

  • 审计过滤器语法
  • 运行时过滤器配置
  • 系统启动时过滤器配置
  • 示例
  • 了解更多

注意

MongoDB Atlas 中的审计

MongoDB Atlas 支持所有M10 和更大集群的审计。Atlas 支持指定如下文档中所述的 JSON 格式审计过滤器,并使用 Atlas 审计过滤器构建器简化审计配置。了解更多,请参阅 MongoDB Atlas 文档中的设置数据库审计配置自定义审计过滤器。

MongoDB 企业版 支持审计 各种操作。当 启用 时,默认情况下,审计功能记录所有可审计操作,详细信息请参阅 审计事件操作、详情和结果。您可以指定事件过滤器以限制记录哪些事件。

您可以在 启动 时配置审计过滤器,或者您可以配置 MongoDB 允许在 运行时 配置过滤器。

审计过滤器与指定给 find 命令的查询谓词文档格式相同。要查看示例审计过滤器,请参阅 示例。

从MongoDB 5.0开始,可以对mongodmongos节点的审计配置进行运行时配置。一组这些节点可以参与分布式审计配置。

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

参数
true
未设置
未设置

如果runtimeConfiguration设置为true

  • 或者设置了auditLog.filter

  • 或者auditAuthorizationSuccess

如果,服务器将记录错误并无法启动。

参数在运行时,请参阅auditConfig

可以在配置文件中的 auditLog 会话下指定过滤器。请参阅以下示例以获取配置示例。

注意

如果启用了 runtimeConfiguration,则无法使用配置文件指定审计过滤器。

要记录所有可审计事件,请不要指定审计过滤器。默认情况下,审计设施记录所有可审计操作。

以下示例审计仅使用过滤器createCollectiondropCollection操作。

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

要指定审计过滤器,请将过滤器文档用单引号括起来,将其作为字符串传递。

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

要指定审计过滤器,请将过滤器文档用单引号括起来,将其作为字符串传递。

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\\./ }

要指定审计过滤器,请将过滤器文档用单引号括起来,将其作为字符串传递。

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角色的test数据库用户的操作,包括继承自readWrite角色的用户。

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

要指定审计过滤器,请将过滤器文档用单引号括起来,将其作为字符串传递。

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" ] }
}

审计的操作包括

要指定审计过滤器,请将过滤器文档用单引号括起来,将其作为字符串传递。

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" ] }
}

审计的操作包括

要指定审计过滤器,请将过滤器文档用单引号括起来,将其作为字符串传递。

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 }

从 MongoDB 8.0 开始,MongoDB 可以以 OCSF 格式写入日志消息OCSF 模式包含与默认的 mongo 模式不同的字段。

以下审计过滤器捕获记录在 OCSF 模式中的网络活动操作

{ category_uid: 4 }

要指定审计过滤器,请将过滤器文档用单引号括起来,将其作为字符串传递。

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。例如,以下过滤器捕获了当atypeauthenticate或操作是由具有readWrite角色的用户执行的操作

{
$or: [
{ atype: "authenticate" },
{ "roles.role": "readWrite" }
]
}

要指定审计过滤器,请将过滤器文档用单引号括起来,将其作为字符串传递。

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) 您可以在不启用--auth的情况下启用auditAuthorizationSuccess参数;然而,所有操作都将返回授权检查的成功。

返回

配置