|
1 | 1 | import os
|
2 | 2 | import re
|
| 3 | +import ssl |
3 | 4 | from logging import Logger
|
4 | 5 |
|
5 | 6 | from jupyterhub import __version__ as __jh_version__
|
|
10 | 11 | from tornado.web import Application
|
11 | 12 | from tornado.websocket import WebSocketHandler
|
12 | 13 |
|
13 |
| -from ..handlers import SuperviseAndProxyHandler |
| 14 | +from ..handlers import AddSlashHandler, SuperviseAndProxyHandler |
14 | 15 |
|
15 | 16 |
|
16 | 17 | class StandaloneHubProxyHandler(HubOAuthenticated, SuperviseAndProxyHandler):
|
@@ -69,20 +70,34 @@ def get_timeout(self):
|
69 | 70 |
|
70 | 71 |
|
71 | 72 | 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", "") |
75 | 77 |
|
76 |
| - if not (keyfile and certfile and cafile): |
| 78 | + if not (keyfile or certfile or client_ca): |
77 | 79 | app_log.warn("Could not configure SSL")
|
78 | 80 | return None
|
79 | 81 |
|
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 |
81 | 95 |
|
82 | 96 | # Configure HTTPClient to use SSL for Proxy Requests
|
| 97 | + ssl_context = make_ssl_context(keyfile, certfile, client_ca) |
83 | 98 | httpclient.AsyncHTTPClient.configure(None, defaults={"ssl_options": ssl_context})
|
84 | 99 |
|
85 |
| - return ssl_context |
| 100 | + return ssl_options |
86 | 101 |
|
87 | 102 |
|
88 | 103 | def make_proxy_app(
|
@@ -130,6 +145,8 @@ def __init__(self, *args, **kwargs):
|
130 | 145 |
|
131 | 146 | app = Application(
|
132 | 147 | [
|
| 148 | + # Redirects from the JupyterHub might not contain a slash |
| 149 | + (r"^" + re.escape(prefix) + r"$", AddSlashHandler), |
133 | 150 | (
|
134 | 151 | r"^" + re.escape(prefix) + r"/(.*)",
|
135 | 152 | Proxy,
|
|
0 commit comments