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

Add support for JUPYTER_TOKEN_FILE #462

Merged
merged 1 commit into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions jupyter_server/serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,9 @@ def _write_cookie_secret_file(self, secret):
token = Unicode('<generated>',
help=_i18n("""Token used for authenticating first-time connections to the server.

The token can be read from the file referenced by JUPYTER_TOKEN_FILE or set directly
with the JUPYTER_TOKEN environment variable.

When no password is enabled,
the default is to generate a new, random token.

Expand All @@ -800,6 +803,10 @@ def _token_default(self):
if os.getenv('JUPYTER_TOKEN'):
self._token_generated = False
return os.getenv('JUPYTER_TOKEN')
if os.getenv('JUPYTER_TOKEN_FILE'):
self._token_generated = False
with io.open(os.getenv('JUPYTER_TOKEN_FILE'), "r") as token_file:
return token_file.read()
if self.password:
# no token if password is enabled
self._token_generated = False
Expand Down
19 changes: 17 additions & 2 deletions jupyter_server/tests/extension/test_launch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test launching Jupyter Server Applications
through as ExtensionApp launch_instance.
"""
from pathlib import Path
import os
import sys
import time
Expand Down Expand Up @@ -45,7 +46,7 @@ def wait_up(url, interval=0.1, check=None):

@pytest.fixture
def launch_instance(request, port, token):
def _run_in_subprocess(argv=[]):
def _run_in_subprocess(argv=[], add_token=True):

def _kill_extension_app():
try:
Expand All @@ -55,13 +56,15 @@ def _kill_extension_app():
pass
process.wait(10)

if add_token:
f'--ServerApp.token="{token}"',

process = subprocess.Popen([
sys.executable, '-m',
'mockextensions.app',
f'--port={port}',
'--ip=127.0.0.1',
'--no-browser',
f'--ServerApp.token="{token}"',
*argv,
], cwd=HERE)

Expand Down Expand Up @@ -93,3 +96,15 @@ def test_base_url(launch_instance, fetch):
assert r.status_code == 200


def test_token_file(launch_instance, fetch, token):
token_file = HERE / Path('token_file.txt')
os.environ['JUPYTER_TOKEN_FILE'] = str(token_file)
token_file.write_text(token, encoding='utf-8')

launch_instance(add_token=False)
r = fetch("/mock")
del os.environ['JUPYTER_TOKEN_FILE']
token_file.unlink()
assert r.status_code == 200