python-tuf/tests/simple_https_server.py
Martin Vrachev 4c81340610 Replace depricated ssl function and fix CI errors
When I tried adding support for Python3.10 we had CI errors due to
test failures: https://github.com/theupdateframework/python-tuf/pull/1610/checks?check_run_id=3861875325
The problem comes from the fact that we start a subprocess
executing simple_https_server.py, but then we fail to communicate the
message we expect from the server process to the main process actually
running the test. We expect our custom message to be the first line
printed from the server process, but instead, a deprecation warning is
printed first about the usage of ssl.wrap_socket(). Our custom message
is printed second.
As of Python 3.7 this function has been deprecated:
https://docs.python.org/3/library/ssl.html#ssl.wrap_socket and for
whatever the reason we didn't get a warning when using it before.

My fix does what is suggested in the warning and replaces the usage of
ssl.wrap_socket() by instantiating a ssl.SSLContext object and then
calling SSLContext.wrap_socket().
This removes the warning.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-10-21 14:15:29 +03:00

59 lines
1.5 KiB
Python
Executable file

#!/usr/bin/env python
# Copyright 2014 - 2017, New York University and the TUF contributors
# SPDX-License-Identifier: MIT OR Apache-2.0
"""
<Program>
simple_https_server.py
<Author>
Vladimir Diaz.
<Started>
June 17, 2014
<Copyright>
See LICENSE-MIT OR LICENSE for licensing information.
<Purpose>
Provide a simple https server that can be used by the unit tests. For
example, 'download.py' can connect to the https server started by this module
to verify that https downloads are permitted.
<Reference>
ssl.SSLContext.wrap_socket:
https://docs.python.org/3/library/ssl.html#ssl.SSLContext.wrap_socket
SimpleHTTPServer:
http://docs.python.org/library/simplehttpserver.html#module-SimpleHTTPServer
"""
import sys
import ssl
import os
import http.server
keyfile = os.path.join('ssl_certs', 'ssl_cert.key')
certfile = os.path.join('ssl_certs', 'ssl_cert.crt')
if len(sys.argv) > 1 and os.path.exists(sys.argv[1]):
certfile = sys.argv[1]
httpd = http.server.HTTPServer(('localhost', 0),
http.server.SimpleHTTPRequestHandler)
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(certfile, keyfile)
httpd.socket = context.wrap_socket(httpd.socket, server_side=True)
port_message = 'bind succeeded, server port is: ' \
+ str(httpd.server_address[1])
print(port_message)
if len(sys.argv) > 1 and certfile != sys.argv[1]:
print('simple_https_server: cert file was not found: ' + sys.argv[1] +
'; using default: ' + certfile + " certfile")
httpd.serve_forever()