Skip to content

Commit c5db2a8

Browse files
committed
Notebook Server Token takes higher precedence over JupyterHub Token.
1 parent 405e1d5 commit c5db2a8

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ classifiers = [
4040
dependencies = [
4141
"jupyter-server-proxy",
4242
"jupyter-contrib-nbextensions",
43-
"matlab-proxy>=0.2.9",
43+
"matlab-proxy>=0.5.11",
4444
"psutil",
4545
"requests",
4646
]

src/jupyter_matlab_kernel/kernel.py

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,33 +77,73 @@ def start_matlab_proxy():
7777
if server["pid"] == jupyter_server_pid:
7878
found_nb_server = True
7979
nb_server = server
80+
# Stop iterating over the server list
81+
break
82+
83+
# Verify that Password is disabled
84+
if nb_server["password"] is True:
85+
# TODO: To support passwords, we either need to acquire it from Jupyter or ask the user?
86+
raise MATLABConnectionError(
87+
"""
88+
Error: MATLAB Kernel could not communicate with MATLAB.\n
89+
Reason: There is a password set to access the Jupyter server.\n
90+
Resolution: Delete the cached Notebook password file, and restart the kernel.\n
91+
See https://jupyter-notebook.readthedocs.io/en/stable/public_server.html#securing-a-notebook-server for more information.
92+
"""
93+
)
8094

8195
# Error out if the server is not found!
8296
if found_nb_server == False:
8397
raise MATLABConnectionError(
84-
"""The MATLAB Kernel for Jupyter was unable to find the notebook server from which it was spawned!\n
85-
Please relaunch kernel from JupyterLab or Classic Jupyter Notebook."""
98+
"""
99+
Error: MATLAB Kernel for Jupyter was unable to find the notebook server from which it was spawned!\n
100+
Resolution: Please relaunch kernel from JupyterLab or Classic Jupyter Notebook.
101+
"""
86102
)
87103

88-
# Fetch JupyterHub API token for HTTP request authentication
89-
# incase the jupyter server is started by JupyterHub.
90-
jh_api_token = os.getenv("JUPYTERHUB_API_TOKEN")
91-
92104
url = "{protocol}://localhost:{port}{base_url}matlab".format(
93105
protocol="https" if nb_server["secure"] else "http",
94106
port=nb_server["port"],
95107
base_url=nb_server["base_url"],
96108
)
97109

98-
token = nb_server["token"] if jh_api_token is None else jh_api_token
99-
headers = {
100-
"Authorization": f"token {token}",
101-
}
110+
# Fetch JupyterHub API token for HTTP request authentication
111+
# incase the jupyter server is started by JupyterHub.
112+
jh_api_token = os.getenv("JUPYTERHUB_API_TOKEN")
113+
114+
# set the token to be used during communication with Jupyter
115+
# In environments where tokens are set for both nb_server & JupyterHub
116+
# precedence is given to the nb_server token
117+
if nb_server["token"]:
118+
token = nb_server["token"]
119+
elif jh_api_token:
120+
token = jh_api_token
121+
else:
122+
token = None
123+
124+
if token:
125+
headers = {
126+
"Authorization": f"token {token}",
127+
}
128+
else:
129+
headers = None
130+
131+
# This is content that is present in the matlab-proxy index.html page which
132+
# can be used to validate a proper response.
133+
matlab_proxy_index_page_identifier = "MWI_MATLAB_PROXY_IDENTIFIER"
102134

103135
# send request to the matlab-proxy endpoint to make sure it is available.
104136
# If matlab-proxy is not started, jupyter-server starts it at this point.
105137
resp = requests.get(url, headers=headers, verify=False)
106138
if resp.status_code == requests.codes.OK:
139+
# Verify that the returned value is correct
140+
if matlab_proxy_index_page_identifier not in resp.text:
141+
raise MATLABConnectionError(
142+
"""
143+
Error: MATLAB Kernel could not communicate with MATLAB.
144+
Reason: Possibly due to invalid jupyter security tokens.
145+
"""
146+
)
107147
return url, nb_server["base_url"], headers
108148
else:
109149
resp.raise_for_status()

0 commit comments

Comments
 (0)