219 lines
5.3 KiB
Markdown
219 lines
5.3 KiB
Markdown
# SSL证书处理指南
|
||
|
||
## 🔒 问题描述
|
||
|
||
在连接到DMS服务时,可能会遇到SSL证书验证失败的错误:
|
||
|
||
```
|
||
SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1010)'))
|
||
```
|
||
|
||
这通常发生在以下情况:
|
||
- 服务器使用自签名证书
|
||
- 证书链不完整
|
||
- 本地缺少根证书
|
||
- 开发/测试环境的证书配置问题
|
||
|
||
## 🛠️ 解决方案
|
||
|
||
### 方案1:忽略SSL证书验证(推荐用于开发/测试)
|
||
|
||
使用 `--ignore-ssl` 参数来忽略SSL证书验证:
|
||
|
||
```bash
|
||
python run_api_tests.py \
|
||
--dms ./assets/doc/dms/domain.json \
|
||
--base-url https://www.dev.ideas.cnpc \
|
||
--ignore-ssl \
|
||
--strictness-level CRITICAL
|
||
```
|
||
|
||
#### 特点:
|
||
- ✅ 快速解决SSL证书问题
|
||
- ✅ 适用于开发和测试环境
|
||
- ⚠️ 会显示安全警告
|
||
- ❌ 不推荐在生产环境使用
|
||
|
||
### 方案2:配置正确的SSL证书(推荐用于生产)
|
||
|
||
#### 2.1 安装根证书
|
||
|
||
```bash
|
||
# Ubuntu/Debian
|
||
sudo apt-get update
|
||
sudo apt-get install ca-certificates
|
||
|
||
# CentOS/RHEL
|
||
sudo yum update ca-certificates
|
||
|
||
# macOS
|
||
# 通过钥匙串访问添加证书
|
||
```
|
||
|
||
#### 2.2 添加自定义证书
|
||
|
||
如果服务器使用自签名证书,可以将证书添加到系统信任列表:
|
||
|
||
```bash
|
||
# 下载证书
|
||
openssl s_client -showcerts -connect www.dev.ideas.cnpc:443 </dev/null 2>/dev/null | openssl x509 -outform PEM > server.crt
|
||
|
||
# 添加到系统证书库(Ubuntu/Debian)
|
||
sudo cp server.crt /usr/local/share/ca-certificates/
|
||
sudo update-ca-certificates
|
||
|
||
# 添加到系统证书库(CentOS/RHEL)
|
||
sudo cp server.crt /etc/pki/ca-trust/source/anchors/
|
||
sudo update-ca-trust
|
||
```
|
||
|
||
#### 2.3 使用环境变量
|
||
|
||
```bash
|
||
# 设置证书文件路径
|
||
export REQUESTS_CA_BUNDLE=/path/to/certificate.pem
|
||
export SSL_CERT_FILE=/path/to/certificate.pem
|
||
|
||
# 运行测试
|
||
python run_api_tests.py --dms ./assets/doc/dms/domain.json
|
||
```
|
||
|
||
## 🧪 测试SSL配置
|
||
|
||
### 使用测试脚本
|
||
|
||
```bash
|
||
# 运行SSL测试脚本
|
||
python test_ssl_ignore.py
|
||
```
|
||
|
||
这个脚本会:
|
||
1. 测试使用 `--ignore-ssl` 参数的情况
|
||
2. 测试不使用 `--ignore-ssl` 参数的情况
|
||
3. 验证SSL忽略功能是否正常工作
|
||
|
||
### 手动测试
|
||
|
||
```bash
|
||
# 测试SSL连接
|
||
curl -v https://www.dev.ideas.cnpc/api/schema/manage/schema
|
||
|
||
# 忽略SSL测试
|
||
curl -k -v https://www.dev.ideas.cnpc/api/schema/manage/schema
|
||
```
|
||
|
||
## 📋 使用建议
|
||
|
||
### 开发环境
|
||
```bash
|
||
# 推荐使用SSL忽略
|
||
python run_api_tests.py \
|
||
--dms ./assets/doc/dms/domain.json \
|
||
--base-url https://www.dev.ideas.cnpc \
|
||
--ignore-ssl \
|
||
--strictness-level CRITICAL
|
||
```
|
||
|
||
### 测试环境
|
||
```bash
|
||
# 可以使用SSL忽略,但建议配置证书
|
||
python run_api_tests.py \
|
||
--dms ./assets/doc/dms/domain.json \
|
||
--base-url https://test.ideas.cnpc \
|
||
--ignore-ssl \
|
||
--strictness-level HIGH
|
||
```
|
||
|
||
### 生产环境
|
||
```bash
|
||
# 必须配置正确的SSL证书,不使用--ignore-ssl
|
||
python run_api_tests.py \
|
||
--dms ./assets/doc/dms/domain.json \
|
||
--base-url https://prod.ideas.cnpc \
|
||
--strictness-level CRITICAL
|
||
```
|
||
|
||
## 🔧 技术实现
|
||
|
||
### 代码修改
|
||
|
||
1. **添加命令行参数**:
|
||
```python
|
||
parser.add_argument('--ignore-ssl', action='store_true',
|
||
help='忽略SSL证书验证(不推荐在生产环境使用)')
|
||
```
|
||
|
||
2. **修改requests调用**:
|
||
```python
|
||
response = requests.get(url, verify=not ignore_ssl)
|
||
```
|
||
|
||
3. **禁用SSL警告**:
|
||
```python
|
||
if ignore_ssl:
|
||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||
```
|
||
|
||
### 影响的文件
|
||
|
||
- `run_api_tests.py` - 添加命令行参数
|
||
- `ddms_compliance_suite/input_parser/parser.py` - 修改SSL验证逻辑
|
||
- `ddms_compliance_suite/test_orchestrator.py` - 传递SSL参数
|
||
|
||
## ⚠️ 安全注意事项
|
||
|
||
### 风险
|
||
- **中间人攻击**:忽略SSL验证可能导致数据被截获
|
||
- **数据泄露**:敏感信息可能被恶意服务器获取
|
||
- **身份伪造**:无法验证服务器身份的真实性
|
||
|
||
### 最佳实践
|
||
1. **仅在开发/测试环境使用** `--ignore-ssl`
|
||
2. **生产环境必须配置正确的SSL证书**
|
||
3. **定期更新证书和根证书库**
|
||
4. **监控SSL证书过期时间**
|
||
5. **使用HTTPS代替HTTP**
|
||
|
||
## 🔍 故障排除
|
||
|
||
### 常见错误
|
||
|
||
1. **证书过期**
|
||
```
|
||
certificate verify failed: certificate has expired
|
||
```
|
||
解决:更新服务器证书
|
||
|
||
2. **主机名不匹配**
|
||
```
|
||
certificate verify failed: Hostname mismatch
|
||
```
|
||
解决:使用正确的主机名或配置SAN
|
||
|
||
3. **自签名证书**
|
||
```
|
||
certificate verify failed: self signed certificate
|
||
```
|
||
解决:添加证书到信任列表或使用 `--ignore-ssl`
|
||
|
||
### 调试命令
|
||
|
||
```bash
|
||
# 检查证书信息
|
||
openssl s_client -showcerts -connect www.dev.ideas.cnpc:443
|
||
|
||
# 验证证书链
|
||
openssl verify -CAfile /etc/ssl/certs/ca-certificates.crt server.crt
|
||
|
||
# 测试连接
|
||
curl -v --cacert server.crt https://www.dev.ideas.cnpc
|
||
```
|
||
|
||
## 📚 相关资源
|
||
|
||
- [Python requests SSL文档](https://requests.readthedocs.io/en/latest/user/advanced/#ssl-cert-verification)
|
||
- [OpenSSL证书管理](https://www.openssl.org/docs/man1.1.1/man1/openssl-x509.html)
|
||
- [urllib3安全警告](https://urllib3.readthedocs.io/en/stable/advanced-usage.html#ssl-warnings)
|
||
|
||
通过合理配置SSL证书处理,可以确保API测试工具在各种环境中正常工作,同时保持适当的安全性。
|