From c8821b7700758a467d88fd83075e079a74f15b82 Mon Sep 17 00:00:00 2001 From: Min RK Date: Tue, 13 Apr 2021 13:32:16 +0200 Subject: [PATCH] init default oauth client in init_db ensures jupyterhub client is present, which is required for creation of tokens, etc. --- jupyterhub/app.py | 39 ++++++++++++++++++------------------ jupyterhub/tests/conftest.py | 12 +++++------ 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/jupyterhub/app.py b/jupyterhub/app.py index f9d1b9a9..cdd130b6 100644 --- a/jupyterhub/app.py +++ b/jupyterhub/app.py @@ -214,8 +214,6 @@ class NewToken(Application): hub = JupyterHub(parent=self) hub.load_config_file(hub.config_file) hub.init_db() - hub.init_hub() - hub.init_oauth() def init_users(): loop = asyncio.new_event_loop() @@ -1694,6 +1692,26 @@ class JupyterHub(Application): except orm.DatabaseSchemaMismatch as e: self.exit(e) + # ensure the default oauth client exists + if ( + not self.db.query(orm.OAuthClient) + .filter_by(identifier="jupyterhub") + .one_or_none() + ): + # create the oauth client for jupyterhub itself + # this allows us to distinguish between orphaned tokens + # (failed cascade deletion) and tokens issued by the hub + # it has no client_secret, which means it cannot be used + # to make requests + client = orm.OAuthClient( + identifier="jupyterhub", + secret="", + redirect_uri="", + description="JupyterHub", + ) + self.db.add(client) + self.db.commit() + def init_hub(self): """Load the Hub URL config""" hub_args = dict( @@ -2295,23 +2313,6 @@ class JupyterHub(Application): login_url=url_path_join(base_url, 'login'), token_expires_in=self.oauth_token_expires_in, ) - # ensure the default oauth client exists - if ( - not self.db.query(orm.OAuthClient) - .filter_by(identifier="jupyterhub") - .first() - ): - # create the oauth client for jupyterhub itself - # this allows us to distinguish between orphaned tokens - # (failed cascade deletion) and tokens issued by the hub - # it has no client_secret, which means it cannot be used - # to make requests - self.oauth_provider.add_client( - client_id="jupyterhub", - client_secret="", - redirect_uri="", - description="JupyterHub", - ) def cleanup_oauth_clients(self): """Cleanup any OAuth clients that shouldn't be in the database. diff --git a/jupyterhub/tests/conftest.py b/jupyterhub/tests/conftest.py index 7efee9c1..f439bfb0 100644 --- a/jupyterhub/tests/conftest.py +++ b/jupyterhub/tests/conftest.py @@ -125,16 +125,14 @@ def db(): """Get a db session""" global _db if _db is None: - _db = orm.new_session_factory('sqlite:///:memory:')() + # make sure some initial db contents are filled out + # specifically, the 'default' jupyterhub oauth client + app = MockHub(db_url='sqlite:///:memory:') + app.init_db() + _db = app.db user = orm.User(name=getuser()) _db.add(user) _db.commit() - # make sure some initial db contents are filled out - # specifically, the 'default' jupyterhub oauth client - app = MockHub() - app.db = _db - app.init_hub() - app.init_oauth() return _db