如何使用 openapi3 python 来使用相互身份验证 (mTLS) 连接到服务器

问题描述 投票:0回答:0

我有一个实现 https://osia.readthedocs.io/en/stable/abis.yaml < This server is configured to use mutual TLS (as a client, I need to have a truststore, and a keypair to connect to the server). The abis.yaml does not indicate this security method.

的服务器

我不明白如何向 openapi3 提供我的密钥对。我想我需要使用持久的 requests.session,但我不明白如何提供这个会话。

这是一个示例代码,它失败了,但可以解释我想要做什么。

from requests import Session
import yaml
from openapi3 import OpenAPI

# load the spec file and read the yaml
with open('abis.yaml') as f:
    spec = yaml.safe_load(f.read())

# parse the spec into python - this will raise if the spec is invalid
req: Session = Session()
req.cert = ( "otsbms.pem", "otsbms.key.pem" )
req.verify = 'ca.pem'
req.request(method= 'DELETE', url='https://192.168.101.41/brs/v1/persons/P1?transactionId=guid',)
# the line above returns '500' meaning the https connection was successful, and the abis server did not understand my request, which is another problem outside the scope of this question.

api = OpenAPI(raw_document=spec, ssl_verify='ca.pem', use_session=True, session_factory=Session)
api.servers[0].url = 'https://192.168.101.41/'

# api.authenticate( "mutualTLS", ( "otsbms.pem", "otsbms.key.pem" ) )

# call operations and receive result models
result = api.call_deleteAll(parameters={"personId": "a123", "transactionId": "a456", },session=req)

结果是:我几乎确信我的 ca.pem 是好的,但服务器在挑战密钥对时确实失败了。

requests.exceptions.SSLError: HTTPSConnectionPool(host='192.168.101.41', port=443): Max retries exceeded with url: //v1/persons/a123?transactionId=a456 (Caused by SSLError(SSLError(1, '[SSL: SSLV3_ALERT_BAD_CERTIFICATE] sslv3 alert bad certificate (_ssl.c:997)')))

python python-requests openapi client-certificates
© www.soinside.com 2019 - 2024. All rights reserved.