Amazon DocumentDB 安全配置 #
一、安全概述 #
1.1 安全层次 #
text
DocumentDB安全层次:
├── 网络安全
│ ├── VPC隔离
│ ├── 安全组
│ └── 网络ACL
│
├── 访问控制
│ ├── IAM认证
│ ├── 用户名密码
│ └── 角色权限
│
├── 数据安全
│ ├── 存储加密
│ ├── 传输加密
│ └── 数据脱敏
│
└── 审计合规
├── 审计日志
├── 事件追踪
└── 合规认证
1.2 安全最佳实践 #
text
安全原则:
├── 最小权限原则
├── 纵深防御
├── 数据加密
├── 定期审计
└── 持续监控
二、VPC配置 #
2.1 VPC设计 #
text
VPC设计建议:
├── 使用私有子网
├── 跨多个可用区
├── 合理规划IP段
├── 配置路由表
└── 设置网络ACL
2.2 子网组配置 #
bash
# 创建子网组
aws docdb create-db-subnet-group \
--db-subnet-group-name my-subnet-group \
--db-subnet-group-description "DocumentDB Subnet Group" \
--subnet-ids subnet-12345 subnet-67890
# 查看子网组
aws docdb describe-db-subnet-groups \
--db-subnet-group-name my-subnet-group
2.3 安全组配置 #
bash
# 创建安全组
aws ec2 create-security-group \
--group-name docdb-sg \
--description "DocumentDB Security Group" \
--vpc-id vpc-12345
# 添加入站规则
aws ec2 authorize-security-group-ingress \
--group-id sg-12345 \
--protocol tcp \
--port 27017 \
--source-group sg-app
# 添加出站规则
aws ec2 authorize-security-group-egress \
--group-id sg-12345 \
--protocol -1 \
--port -1 \
--cidr 0.0.0.0/0
2.4 安全组最佳实践 #
text
安全组规则建议:
├── 只开放必要端口(27017)
├── 限制源IP范围
├── 使用安全组引用
├── 定期审查规则
└── 避免使用0.0.0.0/0
三、存储加密 #
3.1 启用加密 #
bash
# 创建加密集群
aws docdb create-db-cluster \
--db-cluster-identifier my-cluster \
--engine docdb \
--storage-encrypted \
--kms-key-id alias/my-key
# 加密是创建时设置,无法修改
3.2 KMS密钥管理 #
bash
# 创建KMS密钥
aws kms create-key \
--description "DocumentDB Encryption Key" \
--policy '{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {"AWS": "arn:aws:iam::123:root"},
"Action": "kms:*",
"Resource": "*"
}
]
}'
# 创建密钥别名
aws kms create-alias \
--alias-name alias/docdb-key \
--target-key-id key-id
3.3 加密特性 #
text
加密特性:
├── 使用AWS KMS管理密钥
├── AES-256加密算法
├── 自动密钥轮换
├── 加密备份数据
├── 加密快照
└── 加密复制数据
四、传输加密 #
4.1 TLS配置 #
bash
# DocumentDB默认启用TLS
# 下载证书
wget https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem
# 连接时使用TLS
mongo --ssl \
--host my-cluster.cluster-xxx.docdb.amazonaws.com \
--sslCAFile rds-combined-ca-bundle.pem \
--username admin \
--password
4.2 客户端配置 #
javascript
// Node.js TLS配置
const client = new MongoClient(uri, {
tls: true,
tlsCAFile: './rds-combined-ca-bundle.pem',
tlsAllowInvalidCertificates: false
});
// Python TLS配置
client = MongoClient(
uri,
tls=True,
tlsCAFile='./rds-combined-ca-bundle.pem'
)
4.3 禁用TLS(不推荐) #
bash
# 查看TLS状态
db.adminCommand({ getParameter: 1, tls: 1 })
# 注意:不建议禁用TLS
五、认证配置 #
5.1 密码认证 #
javascript
// 创建用户
db.createUser({
user: "appuser",
pwd: "SecurePassword123!",
roles: [
{ role: "readWrite", db: "mydb" }
]
})
// 修改密码
db.changeUserPassword("appuser", "NewPassword123!")
// 删除用户
db.dropUser("appuser")
5.2 IAM认证 #
bash
# 启用IAM认证
aws docdb modify-db-cluster \
--db-cluster-identifier my-cluster \
--enable-iam-database-authentication \
--apply-immediately
javascript
// 使用IAM认证连接
const { MongoClient } = require('mongodb');
const { RDS } = require('aws-sdk');
async function connectWithIAM() {
const rds = new RDS();
// 获取认证令牌
const token = rds.generateDBAuthToken({
DBHostname: 'my-cluster.cluster-xxx.docdb.amazonaws.com',
Port: 27017,
DBUsername: 'iam-user'
});
const uri = `mongodb://iam-user:${token}@my-cluster.cluster-xxx.docdb.amazonaws.com:27017/?ssl=true&authSource=$external&authMechanism=MONGODB-AWS`;
const client = new MongoClient(uri);
await client.connect();
return client;
}
5.3 角色权限 #
text
内置角色:
├── read - 只读权限
├── readWrite - 读写权限
├── dbAdmin - 数据库管理
├── userAdmin - 用户管理
├── dbOwner - 数据库所有者
├── clusterAdmin - 集群管理
└── root - 超级管理员
javascript
// 创建自定义角色
db.createRole({
role: "customReadWrite",
privileges: [
{
resource: { db: "mydb", collection: "" },
actions: ["find", "insert", "update", "remove"]
}
],
roles: []
})
// 授予角色
db.grantRolesToUser("appuser", [
{ role: "customReadWrite", db: "admin" }
])
六、网络安全 #
6.1 私有网络访问 #
text
私有访问配置:
├── 部署在私有子网
├── 使用VPC端点
├── 配置VPN
├── 使用Direct Connect
└── 禁止公网访问
6.2 VPC端点 #
bash
# 创建VPC端点(用于管理操作)
aws ec2 create-vpc-endpoint \
--vpc-id vpc-12345 \
--service-name com.amazonaws.us-east-1.docdb \
--vpc-endpoint-type Interface \
--subnet-ids subnet-12345 subnet-67890 \
--security-group-ids sg-12345
6.3 网络隔离 #
text
网络隔离建议:
├── 不同环境使用不同VPC
├── 生产环境禁止公网访问
├── 使用堡垒机访问
├── 配置网络ACL
└── 监控网络流量
七、审计日志 #
7.1 启用审计日志 #
bash
# 启用审计日志
aws docdb modify-db-cluster \
--db-cluster-identifier my-cluster \
--enable-cloudwatch-logs-exports '["audit"]' \
--apply-immediately
7.2 审计日志内容 #
text
审计日志记录:
├── 认证事件
├── 授权事件
├── 数据操作
├── DDL操作
├── 管理操作
└── 连接事件
7.3 日志分析 #
javascript
// CloudWatch Logs Insights查询
// 查找失败登录
fields @timestamp, @message
| filter @message like /authentication failed/
| sort @timestamp desc
// 统计操作类型
fields @message
| parse @message "command: *," as command
| stats count() by command
| sort count desc
八、数据保护 #
8.1 数据脱敏 #
javascript
// 数据脱敏函数
function maskEmail(email) {
const [local, domain] = email.split('@');
const maskedLocal = local.substring(0, 2) + '***';
return `${maskedLocal}@${domain}`;
}
function maskPhone(phone) {
return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
}
// 应用脱敏
db.users.find({}).forEach(user => {
user.email = maskEmail(user.email);
user.phone = maskPhone(user.phone);
printjson(user);
});
8.2 敏感数据保护 #
text
敏感数据处理:
├── 识别敏感数据
├── 加密存储
├── 访问控制
├── 审计日志
├── 数据脱敏
└── 定期审查
8.3 数据备份安全 #
text
备份安全:
├── 加密备份
├── 访问控制
├── 跨区域复制
├── 保留策略
└── 删除保护
九、合规认证 #
9.1 合规标准 #
text
DocumentDB合规认证:
├── SOC 1/2/3
├── PCI DSS
├── HIPAA
├── ISO 27001
├── FedRAMP
└── GDPR
9.2 合规检查 #
bash
# 使用AWS Config检查合规
aws configservice describe-compliance-by-config-rule \
--config-rule-names docdb-encryption-enabled
# 使用Security Hub
aws securityhub get-findings \
--filters '{"ProductName":[{"Value":"DocumentDB","Comparison":"EQUALS"}]}'
十、安全最佳实践 #
10.1 访问控制 #
text
访问控制最佳实践:
├── 使用最小权限原则
├── 定期轮换密码
├── 使用IAM认证
├── 审计用户权限
└── 删除不需要的用户
10.2 网络安全 #
text
网络安全最佳实践:
├── 使用私有子网
├── 配置安全组规则
├── 启用TLS加密
├── 监控网络流量
└── 定期审查配置
10.3 数据安全 #
text
数据安全最佳实践:
├── 启用存储加密
├── 使用KMS管理密钥
├── 加密敏感数据
├── 启用审计日志
└── 定期备份数据
十一、安全检查清单 #
11.1 日常检查 #
text
日常安全检查:
├── 检查安全组规则
├── 审查用户权限
├── 检查审计日志
├── 监控异常访问
└── 检查告警配置
11.2 定期审计 #
text
定期安全审计:
├── 审查IAM策略
├── 检查加密配置
├── 审计用户活动
├── 测试恢复流程
└── 更新安全文档
十二、总结 #
12.1 安全配置要点 #
| 层次 | 配置 |
|---|---|
| 网络 | VPC、安全组、TLS |
| 认证 | 密码、IAM、角色 |
| 数据 | 存储加密、传输加密 |
| 审计 | 日志、监控、告警 |
12.2 最佳实践总结 #
text
安全最佳实践:
├── 实施纵深防御
├── 最小权限原则
├── 加密所有数据
├── 启用审计日志
├── 定期安全审计
└── 持续监控改进
下一步,让我们学习性能优化!
最后更新:2026-03-27