From 492894e365b42357590a7cb4fff62a65c80549c2 Mon Sep 17 00:00:00 2001 From: mishaschwartz <4380924+mishaschwartz@users.noreply.github.com> Date: Thu, 30 Nov 2023 10:56:44 -0500 Subject: [PATCH] Add magpie cookie as environment variable in jupyterlab server --- CHANGES.md | 41 ++++++++++++++++++++++++- birdhouse/config/jupyterhub/default.env | 2 +- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 088fb34d8..454b58e1f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -15,7 +15,46 @@ [Unreleased](https://github.com/bird-house/birdhouse-deploy/tree/master) (latest) ------------------------------------------------------------------------------------------------------------------ -[//]: # (list changes here, using '-' for each new entry, remove this when items are added) +## Changes + +- Add magpie cookie as environment variable in jupyterlab server + + When the jupyterlab server starts, add the current magpie cookie values as an environment variable in the jupyterlab + environment. The environment variable is name `MAGPIE_COOKIES` and the value of this variable is a JSON object where + keys are the name of a cookie and values are a cookie's content. + + This will allow users to programatically access resources protected by magpie without having to + copy/paste these cookies from their browser session or add a username and password in plaintext to the file. + + For example, to access a dataset behind a secured URL with `xarray.open_dataset` using a username and password: + + ```python + import requests + from request_magpie import MagpieAuth + import xarray + + with requests.session() as session: + session.auth = MagpieAuth("https://mynode/magpie", "myusername", "myverysecretpassword") + store = xarray.backends.PydapDataStore.open("https://mynode/thredds/some/secure/dataset.nc", session=session) + dataset = xarray.open_dataset(store) + ``` + + And to do the same thing using the current magpie cookie already used to log in the current user (no need to include username and password) + + ```python + import os + import requests + import xarray + + with requests.session() as session: + for name, value in os.getenv("MAGPIE_COOKIES", {}).items(): + session.cookies.set(name, value) + store = xarray.backends.PydapDataStore.open("https://mynode/thredds/some/secure/dataset.nc", session=session) + dataset = xarray.open_dataset(store) + ``` + + Note that users who are already logged in to jupyterhub will need to log out and log in for these changes to take + effect. [1.39.2](https://github.com/bird-house/birdhouse-deploy/tree/1.39.2) (2023-11-30) ------------------------------------------------------------------------------------------------------------------ diff --git a/birdhouse/config/jupyterhub/default.env b/birdhouse/config/jupyterhub/default.env index e6d7ee139..ff532edfd 100644 --- a/birdhouse/config/jupyterhub/default.env +++ b/birdhouse/config/jupyterhub/default.env @@ -5,7 +5,7 @@ # are applied and must be added to the list of DELAYED_EVAL. export JUPYTERHUB_DOCKER=pavics/jupyterhub -export JUPYTERHUB_VERSION=4.0.2-20231127 +export JUPYTERHUB_VERSION=4.0.2-20231130 # Jupyter single-user server images, can be overriden in env.local to have a space separated list of multiple images export DOCKER_NOTEBOOK_IMAGES="pavics/workflow-tests:230601"