Adding several admin users on-the-fly

Hi,

I am working with Jupyterhub, and have developed my own custom LocalAuthenticator to authenticate users based on our in-house authentication method. Depending on the user that logs in, we are spinning up a separate notebook server for them, the name of this user’s server is determined from a combination of their name and other details. So this means that at the time of setting up Jupyterhub, I do not have access to all the users that may or may not access Jupyterhub.

Given the above context, I would like to assign some users (let’s say all users beginning with the letter “n”) as admins, during the time of authentication. This means that at the time of setting up Jupyterhub, I don’t know who these users are - so I cannot populate the admin_users set within jupyterhub_config.py file. I was unable to find much online as to how I can achieve this.

I’ve tried this - I’ve hardcoded an admin user “jhubuser” within the config file, and during the time of authentication, I check for some condition, and if that matches, I redirect them to the “jhubuser” server, which is already designated as an admin user. However, this has other problems - if multiple users match the condition, then all those users will be directed to the “jhubuser” notebook server from their individual browsers. which means our in-house authentication logic fails due to multiple user-cookies pointing to the same user.

So… TL;DR - is there a way I can set a particular user to be an admin user while I am inside the custom authenticator class?

Thanks for your help in advance!

I answered my own question over the course of the day. Posting it here for anyone who might benefit.

Let’s say you have a boolean variable called is_admin_user which determines whether a user is supposed to have admin access. Using this, you can add the following code within your LoginHandler’s get function:

        from jupyterhub import orm
        if is_admin_user:
            self.admin_users.add(user_name)
            user = orm.User.find(self.db, user_name)
            if user is not None:
                user.admin = True
                self.log.info(f"Setting {user_name} as an admin user")
                self.db.commit()
            else:
                self.log.warning(f"Unable to find user {user_name} in database. "
                                 f"Cannot provide admin access.")
1 Like