执行 Kubernetes etcd 安全评估
概述
etcd 是 Kubernetes 的分布式键值存储,作为所有集群数据的后端存储,包括 Secret、RBAC 策略、ConfigMap 和工作负载配置。若未经适当加固,etcd 会以明文暴露所有集群密钥,使其成为获取控制平面访问权限的攻击者的最高价值目标。全面的安全评估涵盖静态加密、传输层 TLS、访问控制、备份安全和网络隔离。
前置条件
- Kubernetes 控制平面节点的访问权限
- etcd 集群节点的 SSH 访问权限(或已配置 etcdctl)
- CIS Kubernetes Benchmark 参考文档
- 了解 TLS 证书管理和 EncryptionConfiguration
评估范围
1. 静态加密(Encryption at Rest)
验证 Kubernetes 是否加密了存储在 etcd 中的 Secret 数据:
# 检查 API server 是否配置了 EncryptionConfiguration
ps aux | grep kube-apiserver | grep encryption-provider-config
# 查看加密配置
cat /etc/kubernetes/enc/encryption-config.yaml
安全配置示例:
apiVersion: apiserver.config.k8s.io/v1
kind: EncryptionConfiguration
resources:
- resources:
- secrets
- configmaps
providers:
- aescbc:
keys:
- name: key1
secret: <base64 编码的 32 字节密钥>
- identity: {} # 回退:用于读取未加密数据
验证 etcd 中的密钥是否已加密:
# 直接从 etcd 读取密钥
ETCDCTL_API=3 etcdctl \
--endpoints=https://127.0.0.1:2379 \
--cacert=/etc/kubernetes/pki/etcd/ca.crt \
--cert=/etc/kubernetes/pki/etcd/server.crt \
--key=/etc/kubernetes/pki/etcd/server.key \
get /registry/secrets/default/my-secret | hexdump -C | head -20
# 若已加密,输出以 "k8s:enc:aescbc:v1:key1" 开头
# 若未加密,将看到明文键值对
2. TLS 传输安全
# 验证 etcd 使用 TLS 进行客户端连接
ETCDCTL_API=3 etcdctl endpoint health \
--endpoints=https://127.0.0.1:2379 \
--cacert=/etc/kubernetes/pki/etcd/ca.crt \
--cert=/etc/kubernetes/pki/etcd/server.crt \
--key=/etc/kubernetes/pki/etcd/server.key
# 检查对等节点 TLS 配置
ps aux | grep etcd | tr ' ' '\n' | grep -E "peer-cert|peer-key|peer-trusted-ca"
# 验证证书过期时间
openssl x509 -in /etc/kubernetes/pki/etcd/server.crt -noout -enddate
openssl x509 -in /etc/kubernetes/pki/etcd/peer.crt -noout -enddate
必需参数:
| 参数 | 必需值 | 用途 |
|---|---|---|
--cert-file |
服务器证书路径 | 客户端到服务器 TLS |
--key-file |
服务器密钥路径 | 客户端到服务器 TLS |
--trusted-ca-file |
CA 证书路径 | 客户端证书验证 |
--peer-cert-file |
对等节点证书路径 | 节点间 TLS |
--peer-key-file |
对等节点密钥路径 | 节点间 TLS |
--peer-trusted-ca-file |
对等节点 CA 路径 | 对等节点证书验证 |
--client-cert-auth |
true | 要求客户端证书 |
--peer-client-cert-auth |
true | 要求对等节点证书 |
3. 访问控制
# 验证 etcd 未监听所有网络接口
ps aux | grep etcd | tr ' ' '\n' | grep listen-client-urls
# 应为:https://127.0.0.1:2379(而非 0.0.0.0)
# 检查谁可以访问 etcd 证书
ls -la /etc/kubernetes/pki/etcd/
# 应仅对 root/etcd 用户可读
# 验证 API server 是唯一的 etcd 客户端
ss -tlnp | grep 2379
# 只有 kube-apiserver 应有连接
4. 备份安全
# 创建加密的 etcd 备份
ETCDCTL_API=3 etcdctl snapshot save /backup/etcd-snapshot.db \
--endpoints=https://127.0.0.1:2379 \
--cacert=/etc/kubernetes/pki/etcd/ca.crt \
--cert=/etc/kubernetes/pki/etcd/server.crt \
--key=/etc/kubernetes/pki/etcd/server.key
# 加密备份文件
gpg --symmetric --cipher-algo AES256 /backup/etcd-snapshot.db
# 验证备份完整性
ETCDCTL_API=3 etcdctl snapshot status /backup/etcd-snapshot.db --write-out=table
5. 网络隔离
# 验证 etcd 端口已防火墙保护
iptables -L -n | grep -E "2379|2380"
# 检查 etcd 是否可从工作节点访问(不应可访问)
# 在工作节点上运行:
curl -k https://<控制平面IP>:2379/health
# 应被拒绝/超时
CIS Benchmark 检查
| CIS 控制项 | 检查内容 | 预期结果 |
|---|---|---|
| 2.1 | etcd cert-file 已设置 | TLS 证书已配置 |
| 2.2 | etcd client-cert-auth | 已启用客户端证书认证 |
| 2.3 | etcd auto-tls 已禁用 | auto-tls=false |
| 2.4 | etcd peer cert-file 已设置 | 对等节点 TLS 已配置 |
| 2.5 | etcd peer client-cert-auth | 已启用对等节点认证 |
| 2.6 | etcd peer auto-tls 已禁用 | peer-auto-tls=false |
| 2.7 | etcd 独立 CA | etcd 使用独立 CA(不与集群共享) |
密钥轮换流程
# 1. 生成新加密密钥
NEW_KEY=$(head -c 32 /dev/urandom | base64)
# 2. 先用新密钥更新 EncryptionConfiguration
cat > /etc/kubernetes/enc/encryption-config.yaml <<EOF
apiVersion: apiserver.config.k8s.io/v1
kind: EncryptionConfiguration
resources:
- resources:
- secrets
providers:
- aescbc:
keys:
- name: key2
secret: ${NEW_KEY}
- name: key1
secret: <旧密钥>
- identity: {}
EOF
# 3. 重启 API server 加载新配置
# 4. 用新密钥重新加密所有密钥
kubectl get secrets --all-namespaces -o json | \
kubectl replace -f -
# 5. 从 EncryptionConfiguration 中移除旧密钥
# 6. 再次重启 API server