Skip to content

Commit 5cb44a8

Browse files
authored
Merge pull request #1524 from maresb/unset
start.sh: JUPYTER_ENV_VARS_TO_UNSET introduced
2 parents 42a5fe7 + 058229d commit 5cb44a8

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

base-notebook/start.sh

+14
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ run-hooks () {
3939
echo "${0}: done running hooks in ${1}"
4040
}
4141

42+
# A helper function to unset env vars listed in the value of the env var
43+
# JUPYTER_ENV_VARS_TO_UNSET.
44+
unset_explicit_env_vars () {
45+
if [ -n "${JUPYTER_ENV_VARS_TO_UNSET}" ]; then
46+
for env_var_to_unset in $(echo "${JUPYTER_ENV_VARS_TO_UNSET}" | tr ',' ' '); do
47+
echo "Unset ${env_var_to_unset} due to JUPYTER_ENV_VARS_TO_UNSET"
48+
unset "${env_var_to_unset}"
49+
done
50+
unset JUPYTER_ENV_VARS_TO_UNSET
51+
fi
52+
}
53+
4254

4355
# NOTE: This hook will run as the user the container was started with!
4456
run-hooks /usr/local/bin/start-notebook.d
@@ -143,6 +155,7 @@ if [ "$(id -u)" == 0 ] ; then
143155
# NOTE: This hook is run as the root user!
144156
run-hooks /usr/local/bin/before-notebook.d
145157

158+
unset_explicit_env_vars
146159
echo "Running as ${NB_USER}:" "${cmd[@]}"
147160
exec sudo --preserve-env --set-home --user "${NB_USER}" \
148161
PATH="${PATH}" \
@@ -199,6 +212,7 @@ else
199212

200213
# NOTE: This hook is run as the user we started the container as!
201214
run-hooks /usr/local/bin/before-notebook.d
215+
unset_explicit_env_vars
202216
echo "Executing the command:" "${cmd[@]}"
203217
exec "${cmd[@]}"
204218
fi

base-notebook/test/test_container_options.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -240,25 +240,26 @@ def test_container_not_delete_bind_mount(container, tmp_path):
240240
assert len(list(tmp_path.iterdir())) == 1
241241

242242

243-
@pytest.mark.skip(reason="not yet implemented; TODO: cherry-pick b44b7ab")
244-
def test_jupyter_env_vars_to_unset_as_root(container):
243+
@pytest.mark.parametrize("enable_root", [False, True])
244+
def test_jupyter_env_vars_to_unset_as_root(container, enable_root):
245245
"""Environment variables names listed in JUPYTER_ENV_VARS_TO_UNSET
246246
should be unset in the final environment."""
247+
root_args = {"user": "root"} if enable_root else {}
247248
c = container.run(
248249
tty=True,
249-
user="root",
250250
environment=[
251251
"JUPYTER_ENV_VARS_TO_UNSET=SECRET_ANIMAL,UNUSED_ENV,SECRET_FRUIT",
252252
"FRUIT=bananas",
253-
"SECRET_FRUIT=mango",
254253
"SECRET_ANIMAL=cats",
254+
"SECRET_FRUIT=mango",
255255
],
256256
command=[
257257
"start.sh",
258258
"bash",
259259
"-c",
260-
"echo I like $FRUIT and ${SECRET_FRUIT:-stuff}, and love ${SECRET_LOVE:-to keep secrets}!",
260+
"echo I like $FRUIT and ${SECRET_FRUIT:-stuff}, and love ${SECRET_ANIMAL:-to keep secrets}!",
261261
],
262+
**root_args,
262263
)
263264
rv = c.wait(timeout=10)
264265
assert rv == 0 or rv["StatusCode"] == 0

docs/using/common.md

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ You do so by passing arguments to the `docker run` command.
7878
**You must grant the within-container notebook user or group (`NB_UID` or `NB_GID`) write access to the host directory (e.g., `sudo chown 1000 /some/host/folder/for/work`).**
7979
- `--user 5000 --group-add users` - Launches the container with a specific user ID and adds that user to the `users` group so that it can modify files in the default home directory and `/opt/conda`.
8080
You can use these arguments as alternatives to setting `${NB_UID}` and `${NB_GID}`.
81+
- `-e JUPYTER_ENV_VARS_TO_UNSET=ADMIN_SECRET_1,ADMIN_SECRET_2` - Unsets specified environment variables in the default startup script.
82+
The variables are unset after the hooks have executed but before the command provided to the startup script runs.
8183

8284
## Startup Hooks
8385

0 commit comments

Comments
 (0)