2017-01-17 22 views
2

私は最近flask + requestsからaiohttpとその非同期httpクライアントに移動しました。aiohttpとクライアント側のSSL証明書

私のシナリオでは、HTTPS(カスタム証明書付き)以上のAPIを呼び出し、クライアント側の証明書を一緒に送信する必要があります。

最初の部分(カスタム証明書の検証)では、サポートは明確でclearly documented int the docsであり、うまく機能します。

一方、2番目の部分では、カスタムSSLクライアント側の証明書を添付してクライアントを認証する簡単な方法を見つけることができないようです。

あなたはそれを行う方法を知っていますか?どうもありがとう !

答えて

6

EDIT:件名に関するaiohttpドキュメントのアップデートをPRに送信しました。マージされました。将来的にこの問題が発生する場合があります誰のため

..

TL:DR

import ssl 
import aiohttp  

ssl_ctx = ssl.create_default_context(cafile='/path_to_client_root_ca') 
ssl_ctx.load_cert_chain('/path_to_client_public_key.pem', '/path_to_client_private_key.pem') 

conn = aiohttp.TCPConnector(ssl_context=ssl_ctx) 
session = aiohttp.ClientSession(connector=conn) 

# session will now send client certificates.. 

長い物語 - 私はそれが要求に実装されますどのように見てきた(きちんと文書化API here)、それは明らかにのurllib3の内部に実装されています。

... 
self.sock = ssl_wrap_socket(
    sock=conn, 
    keyfile=self.key_file, 
    certfile=self.cert_file, 
    ssl_context=self.ssl_context, 
) 
... 

行います:ここ

... 
if ca_certs or ca_cert_dir: 
    try: 
     context.load_verify_locations(ca_certs, ca_cert_dir) 
    except IOError as e: # Platform-specific: Python 2.6, 2.7, 3.2 
     raise SSLError(e) 
    # Py33 raises FileNotFoundError which subclasses OSError 
    # These are not equivalent unless we check the errno attribute 
    except OSError as e: # Platform-specific: Python 3.3 and beyond 
     if e.errno == errno.ENOENT: 
      raise SSLError(e) 
     raise 
elif getattr(context, 'load_default_certs', None) is not None: 
    # try to load OS default certs; works well on Windows (require Python3.4+) 
    context.load_default_certs() 

if certfile: 
    context.load_cert_chain(certfile, keyfile) 
if HAS_SNI: # Platform-specific: OpenSSL with enabled SNI 
    return context.wrap_socket(sock, server_hostname=server_hostname) 
... 

興味深いコールがload_cert_chainにある

urllib3は、それが最終的にこの関数を呼び出しそのHTTPSConnectionオブジェクトにダウンcertパラメータのすべての方法を下にしたたり落ちます - つまり、ssl.SSLContext(標準ライブラリインターフェイス)オブジェクトを作成し、そのようなクライアント証明書をload_cert_chainと呼び出すと、aiohttpは動作します要求\ urllib3と同じようにしてください。

したがって、aiohttpのドキュメントにはあなたにそれがないことが記載されていますが、自分でssl.SSLContextをロードできることを指定しています。

関連する問題