This commit is contained in:
Micke Nordin 2024-01-16 16:46:17 +01:00
parent c002125e12
commit 25d4b5f233
Signed by untrusted user: Micke
GPG key ID: F53C4CC83EDAB3BE

View file

@ -173,52 +173,53 @@ hub:
class RefreshHandler(HubAuthenticated, RequestHandler): class RefreshHandler(HubAuthenticated, RequestHandler):
def api_request(self, method, url, **kwargs): def api_request(self, method, url, **kwargs):
"""Make an API request""" """Make an API request"""
url = url_path_join(self.hub_auth.api_url, url) url = url_path_join(self.hub_auth.api_url, url)
allow_404 = kwargs.pop('allow_404', False) allow_404 = kwargs.pop('allow_404', False)
headers = kwargs.setdefault('headers', {}) headers = kwargs.setdefault('headers', {})
headers.setdefault('Authorization', headers.setdefault('Authorization',
'token %s' % self.hub_auth.api_token) 'token %s' % self.hub_auth.api_token)
try: try:
r = requests.request(method, url, **kwargs) r = requests.request(method, url, **kwargs)
except requests.ConnectionError as e: except requests.ConnectionError as e:
app_log.error("Error connecting to %s: %s", url, e) app_log.error("Error connecting to %s: %s", url, e)
msg = "Failed to connect to Hub API at %r." % url msg = "Failed to connect to Hub API at %r." % url
msg += " Is the Hub accessible at this URL (from host: %s)?" % socket.gethostname( msg += " Is the Hub accessible at this URL (from host: %s)?" % socket.gethostname(
) )
if '127.0.0.1' in url: if '127.0.0.1' in url:
msg += " Make sure to set c.JupyterHub.hub_ip to an IP accessible to" + \ msg += " Make sure to set c.JupyterHub.hub_ip to an IP accessible to" + \
" single-user servers if the servers are not on the same host as the Hub." " single-user servers if the servers are not on the same host as the Hub."
raise HTTPError(500, msg) raise HTTPError(500, msg)
data = None data = None
if r.status_code == 404 and allow_404: if r.status_code == 404 and allow_404:
pass pass
elif r.status_code == 403: elif r.status_code == 403:
app_log.error( app_log.error(
"I don't have permission to check authorization with JupyterHub, my auth token may have expired: [%i] %s", "I don't have permission to check authorization with JupyterHub, my auth token may have expired: [%i] %s",
r.status_code, r.reason) r.status_code, r.reason)
app_log.error(r.text) app_log.error(r.text)
raise HTTPError( raise HTTPError(
500, 500,
"Permission failure checking authorization, I may need a new token" "Permission failure checking authorization, I may need a new token"
) )
elif r.status_code >= 500: elif r.status_code >= 500:
app_log.error("Upstream failure verifying auth token: [%i] %s", app_log.error("Upstream failure verifying auth token: [%i] %s",
r.status_code, r.reason)
app_log.error(r.text)
raise HTTPError(
502, "Failed to check authorization (upstream problem)")
elif r.status_code >= 400:
app_log.warning("Failed to check authorization: [%i] %s",
r.status_code, r.reason) r.status_code, r.reason)
app_log.warning(r.text) app_log.error(r.text)
raise HTTPError(500, "Failed to check authorization") raise HTTPError(
else: 502, "Failed to check authorization (upstream problem)")
data = r.json() elif r.status_code >= 400:
app_log.warning("Failed to check authorization: [%i] %s",
r.status_code, r.reason)
app_log.warning(r.text)
raise HTTPError(500, "Failed to check authorization")
else:
data = r.json()
return data
return data
@authenticated @authenticated
def get(self): def get(self):
user_model = self.get_current_user() user_model = self.get_current_user()