Skip to content

Commit 32569df

Browse files
committed
Fix SSL Configuration, Add SlashHandler to Application
1 parent c0d457d commit 32569df

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

jupyter_server_proxy/standalone/proxy.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import re
3+
import ssl
34
from logging import Logger
45

56
from jupyterhub import __version__ as __jh_version__
@@ -10,7 +11,7 @@
1011
from tornado.web import Application
1112
from tornado.websocket import WebSocketHandler
1213

13-
from ..handlers import SuperviseAndProxyHandler
14+
from ..handlers import AddSlashHandler, SuperviseAndProxyHandler
1415

1516

1617
class StandaloneHubProxyHandler(HubOAuthenticated, SuperviseAndProxyHandler):
@@ -69,20 +70,34 @@ def get_timeout(self):
6970

7071

7172
def configure_ssl():
72-
keyfile = os.environ.get("JUPYTERHUB_SSL_KEYFILE")
73-
certfile = os.environ.get("JUPYTERHUB_SSL_CERTFILE")
74-
cafile = os.environ.get("JUPYTERHUB_SSL_CLIENT_CA")
73+
# See jupyter_server/serverapp:init_webapp
74+
keyfile = os.environ.get("JUPYTERHUB_SSL_KEYFILE", "")
75+
certfile = os.environ.get("JUPYTERHUB_SSL_CERTFILE", "")
76+
client_ca = os.environ.get("JUPYTERHUB_SSL_CLIENT_CA", "")
7577

76-
if not (keyfile and certfile and cafile):
78+
if not (keyfile or certfile or client_ca):
7779
app_log.warn("Could not configure SSL")
7880
return None
7981

80-
ssl_context = make_ssl_context(keyfile, certfile, cafile)
82+
ssl_options = {}
83+
if keyfile:
84+
ssl_options["keyfile"] = keyfile
85+
if certfile:
86+
ssl_options["certfile"] = certfile
87+
if client_ca:
88+
ssl_options["ca_certs"] = client_ca
89+
90+
# PROTOCOL_TLS selects the highest ssl/tls protocol version that both the client and
91+
# server support. When PROTOCOL_TLS is not available use PROTOCOL_SSLv23.
92+
ssl_options["ssl_version"] = getattr(ssl, "PROTOCOL_TLS", ssl.PROTOCOL_SSLv23)
93+
if ssl_options.get("ca_certs", False):
94+
ssl_options["cert_reqs"] = ssl.CERT_REQUIRED
8195

8296
# Configure HTTPClient to use SSL for Proxy Requests
97+
ssl_context = make_ssl_context(keyfile, certfile, client_ca)
8398
httpclient.AsyncHTTPClient.configure(None, defaults={"ssl_options": ssl_context})
8499

85-
return ssl_context
100+
return ssl_options
86101

87102

88103
def make_proxy_app(
@@ -130,6 +145,8 @@ def __init__(self, *args, **kwargs):
130145

131146
app = Application(
132147
[
148+
# Redirects from the JupyterHub might not contain a slash
149+
(r"^" + re.escape(prefix) + r"$", AddSlashHandler),
133150
(
134151
r"^" + re.escape(prefix) + r"/(.*)",
135152
Proxy,

0 commit comments

Comments
 (0)