From c002125e123451d0688a70be7f42fa7b1cbf9c8c Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Tue, 16 Jan 2024 16:43:22 +0100 Subject: [PATCH] Try and try again --- jupyter/base/values/values.yaml | 67 +++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 8 deletions(-) diff --git a/jupyter/base/values/values.yaml b/jupyter/base/values/values.yaml index b7623a2..e3e70c7 100644 --- a/jupyter/base/values/values.yaml +++ b/jupyter/base/values/values.yaml @@ -173,22 +173,72 @@ hub: class RefreshHandler(HubAuthenticated, RequestHandler): - # @authenticated + def api_request(self, method, url, **kwargs): + """Make an API request""" + url = url_path_join(self.hub_auth.api_url, url) + allow_404 = kwargs.pop('allow_404', False) + headers = kwargs.setdefault('headers', {}) + headers.setdefault('Authorization', + 'token %s' % self.hub_auth.api_token) + try: + r = requests.request(method, url, **kwargs) + except requests.ConnectionError as e: + app_log.error("Error connecting to %s: %s", url, e) + msg = "Failed to connect to Hub API at %r." % url + msg += " Is the Hub accessible at this URL (from host: %s)?" % socket.gethostname( + ) + if '127.0.0.1' in url: + 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." + raise HTTPError(500, msg) + + data = None + if r.status_code == 404 and allow_404: + pass + elif r.status_code == 403: + app_log.error( + "I don't have permission to check authorization with JupyterHub, my auth token may have expired: [%i] %s", + r.status_code, r.reason) + app_log.error(r.text) + raise HTTPError( + 500, + "Permission failure checking authorization, I may need a new token" + ) + elif r.status_code >= 500: + 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) + app_log.warning(r.text) + raise HTTPError(500, "Failed to check authorization") + else: + data = r.json() + + return data + @authenticated def get(self): user_model = self.get_current_user() user_model = {'helloooooo': 'hej'} self.set_header('content-type', 'application/json') self.write(json.dumps(user_model, indent=1, sort_keys=True)) + class PingHandler(RequestHandler): + + def get(self): + self.set_header('content-type', 'application/json') + self.write(json.dumps({'ping': 1})) + def main(): - prefix = '/services/refresh-token/' - app = Application( - [ - (prefix, RefreshHandler), - (r'.*', RefreshHandler), - ] - ) + tornado.options.parse_command_line() + app = Application([ + (os.environ['JUPYTERHUB_SERVICE_PREFIX'] + 'tokens', RefreshHandler), + (os.environ['JUPYTERHUB_SERVICE_PREFIX'] + '/?', PingHandler), + ]) http_server = HTTPServer(app) url = urlparse(os.environ['JUPYTERHUB_SERVICE_URL']) @@ -196,6 +246,7 @@ hub: http_server.listen(url.port) IOLoop.current().start() + if __name__ == '__main__': main() networkPolicy: