Closed dtrifiro closed 11 months ago
@bdattona this should solve all the issues we had :partying_face: :mountain:
Attention: 1 lines
in your changes are missing coverage. Please review.
Comparison is base (
58ff319
) 93.08% compared to head (6f53766
) 93.61%.
Files | Patch % | Lines |
---|---|---|
tests/test_utils.py | 96.87% | 1 Missing :warning: |
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
Here's the a script that highlights the differences between the two approaches
import ssl
import socket
import subprocess
host, port = ( # on rosa
"caikit-tgis-example-isvc-predictor-mytest.apps.rosa.vajain-test.cusw.p3.openshiftapps.com",
443,
)
# host, port = ( # on internal network
# "flan-t5-small-caikit-predictor-kserve-grpc.apps.ods-qe-02.rhods.ccitredhat.com",
# 443,
# )
def use_raw_socket():
print("=== Getting cert using raw socket")
context = ssl.SSLContext(ssl.PROTOCOL_TLS)
# context = ssl._create_unverified_context(cert_reqs=ssl.CERT_NONE)
with socket.create_connection((host, port)) as sock:
with context.wrap_socket(sock, server_hostname=host) as ssock:
der_cert = ssock.getpeercert(True)
pem_cert = ssl.DER_cert_to_PEM_cert(der_cert)
name = "cert.pem"
with open(name, "wb") as fh:
fh.write(pem_cert.encode())
print(f"=== saved {name}")
res = subprocess.check_output(f"openssl x509 -text -in {name}".split(" "))
name_txt = name.replace(".pem", ".txt")
print(res.decode())
with open(name_txt, "wb") as fh:
fh.write(res)
print(f"=== saved {name_txt}")
def use_ssl_get_server_cert():
print("=== Getting cert using ssl.get_server_cert")
cert = ssl.get_server_certificate((host, port))
name = "cert-ssl_get_server_cert.pem"
with open(name, "wb") as fh:
fh.write(cert.encode())
print(f"=== saved {name}")
res = subprocess.check_output(f"openssl x509 -text -in {name}".split(" "))
print(res.decode())
name_txt = name.replace(".pem", ".txt")
with open(name_txt, "wb") as fh:
fh.write(res)
print(f"=== saved {name_txt}")
if __name__ == "__main__":
use_raw_socket()
use_ssl_get_server_cert()
To view differences: vimdiff cert.txt cert-ssl_get_server_cert.txt
A small note: this fix is only required for python <= 3.9
, as more recent versions support TLS SNI with no extra configuration required.
verify=False
usedssl.get_server_certificate
to get the server certificates. Unfortunately this does not provide a server hostname (TLS SNI), causing the wrong certificates to be fetched when connecting to an host using name-based virtual hosting. This can be solved by providingserver_hostname
when wrapping the socket for TLS.fixes #80