已合并
[bugfix] 优化证书私钥模数校验性能并拆分 TLS 相关 UT #694
[bugfix] 优化证书私钥模数校验性能并拆分 TLS 相关 UT #694
已合并
yuzechen创建于 2 天前
yuzechen
yuzechen成员
2 天前

1. 合入背景

Fixes #459

证书私钥模数校验在 RSA-3072 场景下通过 to_cryptography_key() 比对模数,单次约 550ms,导致 TLS 相关 UT 耗时过高;本 PR 改用 OpenSSL 原生校验并拆分冗余 comprehensive 用例。

2. 修改内容

  1. motor/common/http/cert_util.py

    • validate_certs_and_keys_modulus 改为 SSL.Context.check_privatekey() 校验证书与私钥匹配,避免私钥 to_cryptography_key() 的慢路径。
  2. tests/coordinator/test_http_server_cert.py

    • test_validate_cert_and_key_comprehensive 拆为参数化用例(成功路径、非法路径、空文件、格式错误、密钥不匹配、CA 错误等),保持覆盖同时减少单函数内重复全量校验。

3. 资料变更

不涉及

4. 接口变更

不涉及跨代码仓或客户面可见的接口变更;TLS 证书校验行为与语义不变,仅优化内部实现性能。

5. 测试结果

  • bash tests/run_tests.sh tests/coordinator/test_http_server_cert.py:31 passed
  • bash tests/run_tests.sh --serial:2711 passed,1 skipped
  • 证书 UT 文件总耗时由约 6s+ 降至约 4s;原先最慢单用例 test_validate_cert_and_key_comprehensive(约 6.75s)已消除

6. CheckList

[x] 代码注释完备

[x] 正确记录维测日志

[x] 是否有UT用例

[x] 若涉及多线程场景,考虑了并发场景,不存在死锁问题

likedislike
Pull Request已成功合入, 合并人@wangyang
(感谢 yuzechen 的贡献)
yuzechenyuzechen成员
2 天前 关联了issue:[bug] 证书私钥模数校验过慢导致 TLS 相关 UT 耗时过高
ascend-robotascend-robot成员
2 天前 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
2 天前 评论:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here
You can get sig-info at here


PR Approval Progress

Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-Ascend/MindIE-Motor 吕有辉, 韩依伦 (2/2) 吕有辉 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

Jechin, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
ascend-robotascend-robot成员
2 天前 添加了label:ci-pipeline-running
ascend-robot
ascend-robot成员
2 天前 评论:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
2 天前 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
2 天前 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
2 天前 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
2 天前 添加了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
2 天前 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
2 天前 添加了label:ci-pipeline-failed
ascend-robot
ascend-robot成员
2 天前 评论:
流水线 PR-pipeline_MindIE-Motor_gitcode#2589 [ commitID:acda33f1 ] 运行失败
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 Build_linux_arm 🕚 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 pre-commit >>>
开源片段检查 SCA >>>
开发者测试 UT_linux_arm 🕚 >>>
流水线 PR-pipeline_MindIE-Motor_gitcode >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员评论有效
  • compile : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
jason lyu
jason lyu成员2 天前进行代码检视2
motor/common/http/cert_util.py
@@ -45,3 +45,3 @@
4545 
4646def validate_certs_and_keys_modulus(server_crt: CryptoX509, server_key: CryptoX509) -> bool:
47- """Validate certificate and private key modulus match"""
47+ """Validate certificate and private key match via OpenSSL native check."""
jason lyu
jason lyu2 天前评论:

use_privatekey可能因密钥格式/加密抛Error或密码缺失异常,当前只捕获SSL.Error,其他异常会中断调用方。

likedislike
yuzechen
yuzechen成员
2 天前 评论:
likedislike
jason lyu
jason lyu成员2 天前进行代码检视2
motor/common/http/cert_util.py
@@ -54,2 +49,2 @@
54- key_modulus = key_rsa_key.public_key().public_numbers().n
55- return cert_modulus == key_modulus
49+ ctx = SSL.Context(SSL.TLS_SERVER_METHOD)
50+ ctx.use_certificate(server_crt)
jason lyu
jason lyu2 天前评论:

logger.error的f-string应改为%s占位符,项目硬约束禁止f-string日志。

likedislike
yuzechen
yuzechen成员
2 天前 评论:
likedislike
jason lyu
jason lyu成员2 天前进行代码检视2
motor/common/http/cert_util.py
@@ -15,7 +15,7 @@ import stat
1515from datetime import timezone
1616from ssl import Purpose, create_default_context
1717 
18-from OpenSSL import crypto
18+from OpenSSL import SSL, crypto
1919from cryptography import x509 as crypt_x509
2020from cryptography.x509.oid import ExtensionOID
2121 
@@ -44,15 +44,16 @@ UTF8_ENCODING = "utf-8"
4444 
jason lyu
jason lyu2 天前评论:

改用OpenSSL原生检查后,函数不再比较模数,建议改名如validate_cert_and_key_match。

likedislike
yuzechen
yuzechen成员
2 天前 评论:
likedislike
jason lyu
jason lyu成员2 天前进行代码检视2
motor/common/http/cert_util.py
@@ -45,3 +45,3 @@
4545 
4646def validate_certs_and_keys_modulus(server_crt: CryptoX509, server_key: CryptoX509) -> bool:
47- """Validate certificate and private key modulus match"""
47+ """Validate certificate and private key match via OpenSSL native check."""
jason lyu
jason lyu2 天前评论:

use_privatekey对加密密钥无口令时会抛异常,当前除SSL.Error外均未捕获,可能向上传播。

likedislike
yuzechen
yuzechen成员
2 天前 评论:
likedislike
jason lyu
jason lyu成员2 天前进行代码检视2
tests/coordinator/test_http_server_cert.py
@@ -84,0 +64,4 @@
64+ critical=True,
65+ )
66+ .add_extension(
67+ x509.KeyUsage(
jason lyu
jason lyu2 天前评论:

证书创建为跨测试复用,可移至模块级conftest以减重复。

likedislike
yuzechen
yuzechen成员
2 天前 评论:
likedislike
yuzechenyuzechen成员
2 天前 推送  1 个提交:3a228f01-[bugfix] Address cert UT review feedback and pre-commit findings
ascend-robotascend-robot成员
2 天前 删除了label:ci-pipeline-failed
ascend-robotascend-robot成员
2 天前 添加了label:stat/needs-squash
ascend-robotascend-robot成员
2 天前 删除了label:ascend-cla/yes
ascend-robotascend-robot成员
2 天前 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
2 天前 评论:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
2 天前 删除了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
2 天前 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
2 天前 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
2 天前 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
2 天前 添加了label:docs-ci-pipeline-success
yuzechenyuzechen成员
2 天前 解决了最后一个问题
yuzechen
yuzechen成员
2 天前 评论:

compile

likedislike
ascend-robotascend-robot成员
2 天前 添加了label:ci-pipeline-running
ascend-robot
ascend-robot成员
2 天前 评论:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
2 天前 删除了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
2 天前 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
2 天前 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
2 天前 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
2 天前 添加了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
2 天前 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
2 天前 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
2 天前 评论:
流水线 PR-pipeline_MindIE-Motor_gitcode#2597 [ commitID:3a228f01 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 Build_linux_arm >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 pre-commit >>>
开源片段检查 SCA >>>
开发者测试 UT_linux_arm >>>
流水线 PR-pipeline_MindIE-Motor_gitcode >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员评论有效
  • compile : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
jason lyu
jason lyu成员
1 天前 评论:

/approve

likedislike
ascend-robotascend-robot成员
1 天前 添加了label:approved
yilunh
1 天前 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
1 天前 添加了label:lgtm
wangyangwangyang成员
1 天前 关闭了关联的issue
wangyangwangyang成员
1 天前 合入了pull request