监控安全事件是确保 MongoDB 数据库安全的重要步骤。通过配置审计日志和使用监控工具,可以有效地跟踪数据库中的安全事件和异常活动。以下是详细的步骤,展示如何监控 MongoDB 中的安全事件。
步骤 1:启用审计日志
MongoDB 企业版提供了内置的审计日志功能,可以记录安全相关的事件。以下是如何启用和配置审计日志的详细步骤。
1.1 配置审计日志
编辑 MongoDB 的配置文件 mongod.conf,添加审计日志配置项:
auditLog:
destination: file
format: JSON
path: /var/log/mongodb/auditLog.json
filter: '{ atype: { $in: ["authCheck", "createUser", "dropUser", "updateUser", "createRole", "dropRole", "updateRole", "grantRolesToUser", "revokeRolesFromUser", "grantRolesToRole", "revokeRolesFromRole"] } }'
将这些配置添加到 mongod.conf 文件中,然后重启 MongoDB 服务以应用配置更改:
sudo systemctl restart mongod
1.2 审计日志示例
启用审计日志后,所有指定类型的事件将被记录到 /var/log/mongodb/auditLog.json 文件中。以下是一个审计日志条目的示例:
{
"atype": "authCheck",
"ts": {"$date": "2023-07-21T12:34:56.789Z"},
"local": {"ip": "127.0.0.1", "port": 27017},
"remote": {"ip": "192.168.1.100", "port": 54022},
"users": [{"user": "admin", "db": "admin"}],
"roles": [{"role": "readWrite", "db": "testdb"}],
"param": {"command": "find", "ns": "testdb.collection1"},
"result": 0
}
步骤 2:使用监控工具
2.1 使用 MongoDB Ops Manager 或 MongoDB Cloud Manager
MongoDB 提供了 Ops Manager 和 Cloud Manager 作为其企业级监控解决方案。通过这些工具,可以实时监控 MongoDB 集群的性能和安全事件。这些工具提供了详细的仪表盘、警报和报告功能。
- Ops Manager 是一个本地部署的解决方案,适用于希望在公司内部管理和监控 MongoDB 集群的企业。
- Cloud Manager 是一个云端托管的解决方案,适用于希望在云环境中管理和监控 MongoDB 集群的企业。
2.2 使用开源监控工具
如果没有 MongoDB 企业版,也可以使用开源监控工具,例如 Prometheus 和 Grafana 来监控 MongoDB 集群的性能和安全事件。
安装 Prometheus 和 Grafana
-
安装 Prometheus:
下载并安装 Prometheus:
wget https://github.com/prometheus/prometheus/releases/download/v2.32.1/prometheus-2.32.1.linux-amd64.tar.gz tar -xvf prometheus-2.32.1.linux-amd64.tar.gz cd prometheus-2.32.1.linux-amd64 ./prometheus --config.file=prometheus.yml -
安装 Grafana:
下载并安装 Grafana:
sudo apt-get install -y software-properties-common sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main" sudo apt-get update sudo apt-get install grafana sudo systemctl start grafana-server sudo systemctl enable grafana-server -
配置 MongoDB Exporter:
使用 MongoDB Exporter 将 MongoDB 指标导出到 Prometheus:
wget https://github.com/percona/mongodb_exporter/releases/download/v0.20.7/mongodb_exporter-0.20.7.linux-amd64.tar.gz tar -xvf mongodb_exporter-0.20.7.linux-amd64.tar.gz ./mongodb_exporter --mongodb.uri="mongodb://user:password@localhost:27017" -
配置 Prometheus:
编辑
prometheus.yml文件,添加 MongoDB Exporter 配置:global: scrape_interval: 15s scrape_configs: - job_name: 'mongodb' static_configs: - targets: ['localhost:9216']启动 Prometheus:
./prometheus --config.file=prometheus.yml -
配置 Grafana:
打开 Grafana Web 界面,默认端口为
3000,默认用户名和密码为admin。添加 Prometheus 数据源:
URL: http://localhost:9090导入 MongoDB 仪表盘(可以在 Grafana Dashboards 市场中找到 MongoDB 相关的仪表盘)。
完整示例脚本
以下是一个完整的示例脚本,展示了如何配置 MongoDB 审计日志和使用 Prometheus 和 Grafana 进行监控:
#!/bin/bash
# 更新系统包
sudo apt-get update
# 安装 MongoDB 企业版(如果尚未安装)
# 请注意需要有效的 MongoDB 企业版许可证
# 配置审计日志
sudo tee -a /etc/mongod.conf > /dev/null <<EOT
auditLog:
destination: file
format: JSON
path: /var/log/mongodb/auditLog.json
filter: '{ atype: { \$in: ["authCheck", "createUser", "dropUser", "updateUser", "createRole", "dropRole", "updateRole", "grantRolesToUser", "revokeRolesFromUser", "grantRolesToRole", "revokeRolesFromRole"] } }'
EOT
# 重启 MongoDB 服务
sudo systemctl restart mongod
# 安装 Prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.32.1/prometheus-2.32.1.linux-amd64.tar.gz
tar -xvf prometheus-2.32.1.linux-amd64.tar.gz
cd prometheus-2.32.1.linux-amd64
# 配置 Prometheus
tee -a prometheus.yml > /dev/null <<EOT
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'mongodb'
static_configs:
- targets: ['localhost:9216']
EOT
# 启动 Prometheus
./prometheus --config.file=prometheus.yml &
# 安装 Grafana
sudo apt-get install -y software-properties-common
sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
sudo apt-get update
sudo apt-get install grafana
sudo systemctl start grafana-server
sudo systemctl enable grafana-server
# 配置 MongoDB Exporter
wget https://github.com/percona/mongodb_exporter/releases/download/v0.20.7/mongodb_exporter-0.20.7.linux-amd64.tar.gz
tar -xvf mongodb_exporter-0.20.7.linux-amd64.tar.gz
./mongodb_exporter --mongodb.uri="mongodb://user:password@localhost:27017" &
总结
通过启用审计日志和使用监控工具如 MongoDB Ops Manager、Cloud Manager 或开源工具 Prometheus 和 Grafana,可以有效地监控 MongoDB 的安全事件。审计日志可以记录重要的安全事件,而监控工具可以提供实时的性能和安全监控,帮助管理员及时发现和处理潜在的安全问题。