Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imagename and tag not sanitized in name_template #480

Open
vncntprvst opened this issue Mar 8, 2023 · 2 comments
Open

imagename and tag not sanitized in name_template #480

vncntprvst opened this issue Mar 8, 2023 · 2 comments
Labels

Comments

@vncntprvst
Copy link

Bug description

Using {imagename} in name_template produces a Bad Request "Invalid container name" error when starting the server.

Expected behaviour

The container should spawn without error.
In addition, the container name should be based on the image, e.g., datascience-notebook for jupyter/datascience-notebook.

Actual behaviour

When launching the Jupyter notebook container, Jupyterhub sends this error:
Bad Request ("Invalid container name (jupyterhub-singleuser:3.1-vncntprvst–), only [a-zA-Z0-9][a-zA-Z0-9_.-] are allowed")

How to reproduce

Define c.DockerSpawner.name_template = '{imagename}-{username}–{servername}' in jupyterhub_config.py and

c.DockerSpawner.allowed_images = {
    'Data science':'jupyter/datascience-notebook:latest'
} 

then select 'Data science' from the drop-down menu

Your personal set up

The server code repository is found here: https://github.com/wanglab-neuro/jupyterlab_server

  • OS: jupyterhub container
  • Version(s): jupyterhub/jupyterhub:3.1.1 with dockerspawner 12.1.0
# JupyterHub configuration
import os, sys, pwd, subprocess, re
import docker

############################# Authentication ########################

### GitHub OAuth authentication
from oauthenticator.github import GitHubOAuthenticator
<...>

############################# Generic ########################
## Admin access: give admins permission to log in to the single user notebook servers owned by other users
c.JupyterHub.admin_access = True 

############################# Permissions ########################
# Updating permissions for volumes mounted from host
c.DockerSpawner.extra_create_kwargs = {'user': 'root'}
c.DockerSpawner.environment = {
<...>
}

############################# Pre-spawn Hook ########################
def pre_spawn_hook(spawner):    
<...>
c.Spawner.pre_spawn_hook = pre_spawn_hook
c.DockerSpawner.post_start_cmd = 'bash -c "/media/link_package_files.sh"'

############################# Docker Spawner Configuration ########################
c.JupyterHub.spawner_class = 'dockerspawner.DockerSpawner' 
c.DockerSpawner.network_name = os.environ['DOCKER_NETWORK_NAME']
c.JupyterHub.hub_ip = os.environ['HUB_IP']

# Pick a docker image. 

c.JupyterHub.allow_named_servers=True
c.DockerSpawner.name_template = '{imagename}-{username}–{servername}'
# c.DockerSpawner.name_template = '{imagename.split(":")[0]}-{username}--{servername}'

c.DockerSpawner.allowed_images = {
    'Data science':'jupyter/datascience-notebook:latest'
    }

## access GPU
c.DockerSpawner.extra_host_config = {
    "device_requests": [
        docker.types.DeviceRequest(
            count=-1,
            capabilities=[["gpu"]],
        ),
    ],
}

## Remove containers once they are stopped
c.DockerSpawner.remove = True

# User data persistence
home_dir = os.environ.get('DOCKER_NOTEBOOK_DIR') or '/home/jovyan' #os.path.join('/home', '{username}')
notebook_dir = home_dir # this is the root directory for the Jupyterlab sidebar
data_dir = home_dir + '/data'
c.DockerSpawner.notebook_dir = notebook_dir #home_dir
c.DockerSpawner.default_url = '/lab'

c.DockerSpawner.volumes = {
<...>
        }

############################# Services ########################
 
c.JupyterHub.load_roles = [
    {
        "name": "jupyterhub-idle-culler-role",
        "scopes": [
            "list:users",
            "read:users:activity",
            "delete:servers",
            # "admin:users", # if using --cull-users
        ],
        # assignment of role's permissions to:
        "services": ["jupyterhub-idle-culler-service"],
    }
]
c.JupyterHub.services = [
    {
        "name": "jupyterhub-idle-culler-service",
        "command": [
            sys.executable,
            "-m", "jupyterhub_idle_culler",
            "--timeout=3600",
        ],
         "admin": True, # Has to be disabled version>2.0
    }
]
@vncntprvst vncntprvst added the bug label Mar 8, 2023
@welcome
Copy link

welcome bot commented Mar 8, 2023

Thank you for opening your first issue in this project! Engagement like this is essential for open source projects! 🤗

If you haven't done so already, check out Jupyter's Code of Conduct. Also, please try to follow the issue template as it helps other other community members to contribute more effectively.
welcome
You can meet the other Jovyans by joining our Discourse forum. There is also an intro thread there where you can stop by and say Hi! 👋

Welcome to the Jupyter community! 🎉

@vncntprvst
Copy link
Author

A couple things I tried to solve this but didn't work.
Adding this bit of code to the pre_spawn_hook:

    # Remove repository and image tag from image name
    image_name = spawner.image.split(':')[0].split('/')[-1]
    # Sanitize image name
    sanitized_image_name = re.sub(r'[^a-zA-Z0-9_.-]', '', image_name)
    # Set name_template with sanitized imagename, username, and servername
    spawner.name_template = f"{sanitized_image_name}-{spawner.user.name}--{spawner.name}"

This had no impact.
Also, defining name_template as follow:
c.DockerSpawner.name_template = '{imagename.split(":")[0]}-{username}--{servername}'
produced an Error 500.

@minrk minrk changed the title Incorrect imagename and tag not sanitized Incorrect imagename and tag not sanitized in name_template May 31, 2023
@minrk minrk changed the title Incorrect imagename and tag not sanitized in name_template imagename and tag not sanitized in name_template May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant