MongoDB 在 CentOS 的安全审计方法
一 审计目标与能力
二 启用审计的两种方式
security:
authorization: enabled
auditLog:
destination: file
format: JSON
path: /var/log/mongodb/audit.json
sudo systemctl restart mongod
sudo systemctl status mongod
mongo --eval "db.runCommand({ getParameter: 1, auditLog: 1 })"
tail -f /var/log/mongodb/audit.json
security:
authorization: enabled
auditLog:
destination: file
format: BSON
path: /var/log/mongodb/audit.bson
# 可按需缩小范围,例如仅审计账号变更
# filter: '{ atype: { $in: ["createUser", "dropUser", "updateUser"] } }'
bsondump /var/log/mongodb/audit.bson > /var/log/mongodb/audit.json
grep -i "createUser" /var/log/mongodb/audit.json
以上步骤涵盖在 CentOS 上启用审计、重启验证、以及日志格式选择与转换的常用做法。
三 审计策略与最小开销配置
auditLog:
destination: file
format: BSON
path: /var/log/mongodb/audit.bson
filter: '{ atype: { $in: ["authenticate", "createUser", "dropUser", "grantRolesToUser", "revokeRolesFromUser", "dropDatabase", "dropCollection"] } }'
四 日志轮转与保护
/var/log/mongodb/audit.json
/var/log/mongodb/audit.bson {
daily
rotate 30
compress
missingok
notifempty
create 0600 mongod mongod
sharedscripts
postrotate
/bin/systemctl reload mongod > /dev/null 2>&1 || true
endscript
}
五 与整体安全的协同配置