Fernando Batista

edit

About * Curriculum Vitae

* Conferences

Work

Personal

Resources

Site

Jupyterhub not Recognizing Users' Groups

Jupyterhub is not Recognizing Users' Groups. There is an issue that prevents the user to get all the corresponding groups. Check Jupyterhub Not Recognizing Users' Groups. I have solved the problem by creating the file my_spawner.MySpawner, and changing jupyterhub_config.py

my_spawner.MySpawner

import os
import sys
import pwd
from subprocess import check_output
from jupyterhub.spawner import LocalProcessSpawner, _try_setcwd

def set_user_setuid(username, chdir=True):
    """Return a preexec_fn for spawning a single-user server as a particular user.
    Returned preexec_fn will set uid/gid, and attempt to chdir to the target user's
    home directory.
    """
    import grp
    import pwd

    user = pwd.getpwnam(username)
    uid = user.pw_uid
    gid = user.pw_gid
    home = user.pw_dir
    gids = [g.gr_gid for g in grp.getgrall() if username in g.gr_mem]

    def preexec():
        """Set uid/gid of current process
        Executed after fork but before exec by python.
        Also try to chdir to the user's home directory.
        """
        os.setgid(gid)
        try:
            os.setgroups(gids)
        except Exception as e:
            print('Failed to set groups %s' % e, file=sys.stderr)
        os.setuid(uid)

        # start in the user's home dir
        if chdir:
            _try_setcwd(home)

    return preexec


class MySpawner(LocalProcessSpawner):
    def make_preexec_fn(self, name):
        return set_user_setuid(name)

Last modified on October 08, 2021, at 01:12 PM UTC