@@ -77,33 +77,73 @@ def start_matlab_proxy():
77
77
if server ["pid" ] == jupyter_server_pid :
78
78
found_nb_server = True
79
79
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
+ )
80
94
81
95
# Error out if the server is not found!
82
96
if found_nb_server == False :
83
97
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
+ """
86
102
)
87
103
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
-
92
104
url = "{protocol}://localhost:{port}{base_url}matlab" .format (
93
105
protocol = "https" if nb_server ["secure" ] else "http" ,
94
106
port = nb_server ["port" ],
95
107
base_url = nb_server ["base_url" ],
96
108
)
97
109
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"
102
134
103
135
# send request to the matlab-proxy endpoint to make sure it is available.
104
136
# If matlab-proxy is not started, jupyter-server starts it at this point.
105
137
resp = requests .get (url , headers = headers , verify = False )
106
138
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
+ )
107
147
return url , nb_server ["base_url" ], headers
108
148
else :
109
149
resp .raise_for_status ()
0 commit comments