Skip to content

Commit

Permalink
Static access Token, closing #136
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristophSchranz committed Jan 22, 2024
1 parent 0fbee05 commit 7150b07
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .build/jupyter_server_config_token_addendum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

# Set static token
import os
if os.getenv("JUPYTER_TOKEN"):
c.ServerApp.token = os.getenv("JUPYTER_TOKEN")
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,4 @@ src/jupyter_notebook_config.json
/build_push_python-only.sh
/build_push_slim.sh
extra/Getting_Started/tensorboard/runs
*copy.sh
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,37 @@ Some useful packages are suggested in the [Extension docs](https://jupyterlab.re
If you notice that an important package is missing in the default stack, please let us know so we can update it.


### Change the Password
### Authentification

### Set a Static Token

Jupyter by default regenerates a new token on each new start.
GPU-Jupyter provides the environment variable `JUPYTER_TOKEN` to set a customized static token.
This option is practicable if the host machine is periodically restartet.
It is suggested to use a long token such as a UUID:

```bash
export JUPYTER_TOKEN=$(uuidgen)
```

- For Docker add the parameter `-e JUPYTER_TOKEN=${JUPYTER_TOKEN}`, e.g.:

```bash
docker run --gpus all -d -it -p 8848:8888 -v $(pwd)/data:/home/jovyan/work -e GRANT_SUDO=yes -e JUPYTER_ENABLE_LAB=yes -e NB_UID="$(id -u)" -e NB_GID="$(id -g)" -e JUPYTER_TOKEN=${JUPYTER_TOKEN} --user root --restart always --name gpu-jupyter_1 gpu-jupyter
```

- In `docker-compose.yml`, the environment variable can be set under
`ENVIRONMENT`:

`NB_GID: ${JUPYTER_GID:-1000}`

The static token can be requested using `docker exec`:
```bash
docker exec -it gpu-jupyter_1 jupyter server list
```


#### Set a custom Password

There are two ways to set a password for GPU-Jupyter:

Expand All @@ -243,7 +273,7 @@ There are two ways to set a password for GPU-Jupyter:
```bash
bash generate-Dockerfile.sh --password [your_password]
```
This will update automatically the salted hashed token in the `custom/jupyter_notebook_config.json` file. Note that the specified password may be visible in your account's bash history.
This will update automatically the salted hashed token in the `.build/jupyter_notebook_config.json` file. Note that the specified password may be visible in your account's bash history.
### Adaptions for using Tensorboard
Expand Down
5 changes: 5 additions & 0 deletions custom/jupyter_server_config_token_addendum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

# Set static token
import os
if os.getenv("JUPYTER_TOKEN"):
c.ServerApp.token = os.getenv("JUPYTER_TOKEN")
1 change: 1 addition & 0 deletions docker-compose-swarm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
JUPYTER_ENABLE_LAB: "yes"
NB_UID: ${JUPYTER_UID:-1000}
NB_GID: ${JUPYTER_GID:-1000}
JUPYTER_TOKEN: ${JUPYTER_TOKEN}
# enable sudo permissions
user:
"root"
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ services:
JUPYTER_ENABLE_LAB: "yes"
NB_UID: ${JUPYTER_UID:-1000}
NB_GID: ${JUPYTER_GID:-1000}
JUPYTER_TOKEN: ${JUPYTER_TOKEN}
# enable sudo permissions
user:
"root"
Expand Down
14 changes: 13 additions & 1 deletion generate-Dockerfile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if [[ "$HELP" == 1 ]]; then
echo "Help for ./generate-Dockerfile.sh:"
echo "Usage: $0 [parameters]"
echo " -h|--help: Show this help."
echo " -p|--pw|--password: Set the password (and update in custom/jupyter_notebook_config.json)"
echo " -p|--pw|--password: Set the password (creates .build/jupyter_notebook_config.json and overrides the token)"
echo " -c|--commit: Set the head commit of the jupyter/docker-stacks submodule (https://github.com/jupyter/docker-stacks/commits/main). default: $HEAD_COMMIT."
echo " --python-only|--no-datascience-notebook: Use not the datascience-notebook from jupyter/docker-stacks, don't install Julia and R."
echo " --no-useful-packages: Don't install the useful packages, specified in custom/usefulpackages.Dockerfile"
Expand Down Expand Up @@ -175,6 +175,18 @@ fi
cp -r extra/Getting_Started data
chmod -R 755 data/

# set static token
# copy jupyter server config token addendum to .build
cp custom/jupyter_server_config_token_addendum.py .build/
# append token config into the jupyter server config
echo >> $DOCKERFILE
echo "# Set env-var JUPYTER_TOKEN as static token" >> $DOCKERFILE
echo "ARG JUPYTER_TOKEN" >> $DOCKERFILE
echo "ENV JUPYTER_TOKEN=\$JUPYTER_TOKEN" >> $DOCKERFILE
echo "COPY jupyter_server_config_token_addendum.py /etc/jupyter/" >> $DOCKERFILE
echo "RUN cat /etc/jupyter/jupyter_server_config_token_addendum.py >> /etc/jupyter/jupyter_server_config.py" >> $DOCKERFILE


# set password
if [[ "$USE_PASSWORD" == 1 ]]; then
echo "Set password to given input"
Expand Down

0 comments on commit 7150b07

Please sign in to comment.