Unable to change uid inside the notebook while setting the spawner.environment['NB_UID'] successfully

I want to set uid, gid, groups and some id things inside the spawned notebook pod.
that is to say, when user logins into the notebook, he/she can see his name and uid with id command in terminal.
I have followed the tutorial from @manics , however no one succeed in user switch.

My configuration is listed below, any one could help me? great thanks.

hub:
  config:
    Authenticator:
      enable_auth_state: true
    # JupyterHub:
    #  authenticator_class: ldapauthenticator.LDAPAuthenticator
    LDAPAuthenticator:
      server_address: ...
      bind_dn_template:
        - cn={username},...
      use_ssl: true
      auth_state_attributes: ["uid", "uidNumber", "gidNumber"]
  extraConfig:
    SpawnerCustomConfig: |
      c.Spawner.cmd = ['start.sh','jupyterhub-singleuser','--allow-root']
      c.KubeSpawner.args = ['--allow-root']
      from ldapauthenticator import LDAPAuthenticator
      class LDAPAuthenticatorExt(LDAPAuthenticator):
          async def pre_spawn_start(self, user, spawner):
              auth_state = await user.get_auth_state()
              self.log.debug(f"pre_spawn_start auth_state: {auth_state}")
              if not auth_state:
                  return
              # setup environment
              spawner.environment["NB_UID"] = str(auth_state["uidNumber"][0])
              spawner.environment["NB_GID"] = str(auth_state["gidNumber"][0])
              spawner.environment["NB_USER"] = auth_state["uid"][0]
              self.log.debug(f"pre_spawn_start spawner.env: {spawner.environment}")
      c.JupyterHub.authenticator_class = LDAPAuthenticatorExt
singleuser:
  uid: 0
  cmd: "start-notebook.sh" #jupyterhub-singleuser

I have checked spawner.environment is setting to the correct NB_USER and some other things.
However, inside the notebook, I still can only get uid=1000(jovyan) gid=100(users) groups=100(users)

1 Like

Hi
This can possibly done in a different way, but I managed to change uid/gid by changing the docker-entrypoint.sh file mounted in the container with code like

# prevent /usr/local/bin/start.sh deleting NB_USER
sed -i 's/userdel/# userdel/' /usr/local/bin/start.sh
sed -i 's/useradd/# useradd/' /usr/local/bin/start.sh
sed -i "s/jovyan/$NB_USER/g" /etc/passwd
sed -i "s/jovyan/$NB_USER/g" /etc/shadow
sed -i "s/1000:100/$NB_UID:$NB_GID/" /etc/passwd

This code is executed as root within the container at the beginning, at least with dockerspawner