Skip to content

Commit

Permalink
update dir ownership in the end
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianhao-Gu committed Sep 12, 2024
1 parent 9f729df commit d2165fd
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions src/jupyterhub_config/custom_spawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def start(self):
# Configure the notebook directory based on whether the user is an admin
self._configure_notebook_dir(username, user_dir)

# Change the ownership of the user's directory
self._change_ownership(user_dir, username)

# Set the command to start the notebook
env_vars = [f'{key}={value}' for key, value in self.environment.items()]

Expand Down Expand Up @@ -104,33 +107,8 @@ def _ensure_user_directory(self, user_dir: Path, username: str):
Ensure the user's home directory exists and is correctly owned and permissioned.
"""
if not user_dir.exists():

self.log.info(f'Getting user info for {username}')
try:
user_info = pwd.getpwnam(username)
except KeyError:
raise ValueError(f'System user {username} does not exist')
# Get the Jupyter user's UID and GID
uid = user_info.pw_uid
gid = user_info.pw_gid

self.log.info(f'Creating user directory for {username}')
user_dir.mkdir(parents=True, exist_ok=True) # guard against race conditions

# Change the directory's ownership to the user
os.chown(user_dir, uid, gid)

# Set directory permissions to 750: Owner (rwx), Group (r-x), Others (---)
os.chmod(user_dir, 0o750)

# Set user and group ID for subsequent operations
os.setgid(gid)
os.setuid(uid)

# Grand spark_user read/write access to the user's home directory
# self.log.info(f'Granting read/write access to spark_user for {username}')
# subprocess.run(['sudo', 'setfacl', '-m', f'u:spark_user:rwX', user_dir], check=True)
# subprocess.run(['sudo', 'setfacl', '-m', f'u:root:rwX', user_dir], check=True)
user_dir.mkdir(parents=True, exist_ok=True)
else:
self.log.info(f'Reusing user directory for {username}')

Expand Down Expand Up @@ -218,3 +196,26 @@ def _configure_notebook_dir(self, username: str, user_dir: Path):
else:
self.log.info(f'Non-admin user detected: {username}. Setting up user-specific workspace.')
self.notebook_dir = str(user_dir)

def _change_ownership(self, user_dir: Path, username: str):
"""
Change the ownership of the user's directory to the user.
"""
self.log.info(f'Getting user info for {username}')
try:
user_info = pwd.getpwnam(username)
except KeyError:
raise ValueError(f'System user {username} does not exist')
# Get the Jupyter user's UID and GID
uid = user_info.pw_uid
gid = user_info.pw_gid

for root, dirs, files in os.walk(user_dir):
for name in dirs + files:
filepath = os.path.join(root, name)

# Change the directory's ownership to the user
os.chown(filepath, uid, gid)

# Set directory permissions to 750: Owner (rwx), Group (r-x), Others (---)
os.chmod(filepath, 0o750)

0 comments on commit d2165fd

Please sign in to comment.