3030# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3131# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3232
33+ import hashlib
3334import logging
3435import os
3536import ssl
4041
4142from elasticapm .transport .base import TransportException
4243from elasticapm .transport .http_base import AsyncHTTPTransportBase , HTTPTransportBase
43- from elasticapm .utils import compat
44+ from elasticapm .utils import compat , read_pem_file
4445
4546logger = logging .getLogger ("elasticapm.transport.http" )
4647
@@ -49,7 +50,12 @@ class Transport(HTTPTransportBase):
4950 def __init__ (self , url , ** kwargs ):
5051 super (Transport , self ).__init__ (url , ** kwargs )
5152 pool_kwargs = {"cert_reqs" : "CERT_REQUIRED" , "ca_certs" : certifi .where (), "block" : True }
52- if not self ._verify_server_cert :
53+ if self ._server_cert :
54+ pool_kwargs .update (
55+ {"assert_fingerprint" : self .cert_fingerprint , "assert_hostname" : False , "cert_reqs" : ssl .CERT_NONE }
56+ )
57+ del pool_kwargs ["ca_certs" ]
58+ elif not self ._verify_server_cert :
5359 pool_kwargs ["cert_reqs" ] = ssl .CERT_NONE
5460 pool_kwargs ["assert_hostname" ] = False
5561 proxy_url = os .environ .get ("HTTPS_PROXY" , os .environ .get ("HTTP_PROXY" ))
@@ -97,6 +103,16 @@ def send(self, data):
97103 if response :
98104 response .close ()
99105
106+ @property
107+ def cert_fingerprint (self ):
108+ if self ._server_cert :
109+ with open (self ._server_cert , "rb" ) as f :
110+ cert_data = read_pem_file (f )
111+ digest = hashlib .sha256 ()
112+ digest .update (cert_data )
113+ return digest .hexdigest ()
114+ return None
115+
100116
101117class AsyncTransport (AsyncHTTPTransportBase , Transport ):
102118 async_mode = True
0 commit comments