Merge pull request #623 from jeanmarcalkazzi/fix/616-baseurl

Add base_url capability to tljh-config
This commit is contained in:
Georgiana Elena
2020-11-05 11:54:39 +02:00
committed by GitHub
5 changed files with 172 additions and 100 deletions

View File

@@ -63,6 +63,13 @@ file. If what you want is only to change the property's value, you should use
Some of the existing ``<property-path>`` are listed below by categories: Some of the existing ``<property-path>`` are listed below by categories:
.. _tljh-base_url:
Base URL
--------
Use ``base_url`` to determine the base URL used by JupyterHub. This parameter will
be passed straight to ``c.JupyterHub.base_url``.
.. _tljh-set-auth: .. _tljh-set-auth:

View File

@@ -18,6 +18,7 @@ from tljh.normalize import generate_system_username
# This catches issues with PATH # This catches issues with PATH
TLJH_CONFIG_PATH = ['sudo', 'tljh-config'] TLJH_CONFIG_PATH = ['sudo', 'tljh-config']
def test_hub_up(): def test_hub_up():
r = requests.get('http://127.0.0.1') r = requests.get('http://127.0.0.1')
r.raise_for_status() r.raise_for_status()
@@ -46,6 +47,30 @@ async def test_user_code_execute():
assert pwd.getpwnam(f'jupyter-{username}') is not None assert pwd.getpwnam(f'jupyter-{username}') is not None
@pytest.mark.asyncio
async def test_user_server_started_with_custom_base_url():
"""
User logs in, starts a server with a custom base_url & executes code
"""
# This *must* be localhost, not an IP
# aiohttp throws away cookies if we are connecting to an IP!
base_url = "/custom-base"
hub_url = f"http://localhost{base_url}"
username = secrets.token_hex(8)
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'set', 'auth.type', 'dummyauthenticator.DummyAuthenticator')).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'set', 'base_url', base_url)).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')).wait()
async with User(username, hub_url, partial(login_dummy, password='')) as u:
await u.login()
await u.ensure_server_simulate()
# unset base_url to avoid problems with other tests
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'unset', 'base_url')).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')).wait()
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_user_admin_add(): async def test_user_admin_add():
""" """
@@ -68,7 +93,8 @@ async def test_user_admin_add():
assert pwd.getpwnam(f'jupyter-{username}') is not None assert pwd.getpwnam(f'jupyter-{username}') is not None
# Assert that the user has admin rights # Assert that the user has admin rights
assert f'jupyter-{username}' in grp.getgrnam('jupyterhub-admins').gr_mem assert f'jupyter-{username}' in grp.getgrnam(
'jupyterhub-admins').gr_mem
# FIXME: Make this test pass # FIXME: Make this test pass
@@ -97,7 +123,8 @@ async def test_user_admin_remove():
assert pwd.getpwnam(f'jupyter-{username}') is not None assert pwd.getpwnam(f'jupyter-{username}') is not None
# Assert that the user has admin rights # Assert that the user has admin rights
assert f'jupyter-{username}' in grp.getgrnam('jupyterhub-admins').gr_mem assert f'jupyter-{username}' in grp.getgrnam(
'jupyterhub-admins').gr_mem
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'remove-item', 'users.admin', username)).wait() assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'remove-item', 'users.admin', username)).wait()
assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')).wait() assert 0 == await (await asyncio.create_subprocess_exec(*TLJH_CONFIG_PATH, 'reload')).wait()
@@ -106,7 +133,8 @@ async def test_user_admin_remove():
await u.ensure_server_simulate() await u.ensure_server_simulate()
# Assert that the user does *not* have admin rights # Assert that the user does *not* have admin rights
assert f'jupyter-{username}' not in grp.getgrnam('jupyterhub-admins').gr_mem assert f'jupyter-{username}' not in grp.getgrnam(
'jupyterhub-admins').gr_mem
@pytest.mark.asyncio @pytest.mark.asyncio
@@ -229,6 +257,7 @@ async def test_idle_server_culled():
timeout=100, timeout=100,
) )
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_active_server_not_culled(): async def test_active_server_not_culled():
""" """

View File

@@ -2,6 +2,7 @@
Test configurer Test configurer
""" """
from traitlets import Dict
import os import os
import sys import sys
@@ -62,6 +63,22 @@ def apply_mock_config(overrides):
return c return c
def test_default_base_url():
"""
Test default JupyterHub base_url
"""
c = apply_mock_config({})
assert c.JupyterHub.base_url == '/'
def test_set_base_url():
"""
Test set JupyterHub base_url
"""
c = apply_mock_config({'base_url': '/custom-base'})
assert c.JupyterHub.base_url == '/custom-base'
def test_default_memory_limit(): def test_default_memory_limit():
""" """
Test default per user memory limit Test default per user memory limit
@@ -129,7 +146,7 @@ def test_auth_dummy():
assert c.JupyterHub.authenticator_class == 'dummyauthenticator.DummyAuthenticator' assert c.JupyterHub.authenticator_class == 'dummyauthenticator.DummyAuthenticator'
assert c.DummyAuthenticator.password == 'test' assert c.DummyAuthenticator.password == 'test'
from traitlets import Dict
def test_user_groups(): def test_user_groups():
""" """
Test setting user groups Test setting user groups
@@ -276,4 +293,3 @@ def test_auth_native():
}) })
assert c.JupyterHub.authenticator_class == 'nativeauthenticator.NativeAuthenticator' assert c.JupyterHub.authenticator_class == 'nativeauthenticator.NativeAuthenticator'
assert c.NativeAuthenticator.open_signup == True assert c.NativeAuthenticator.open_signup == True

View File

@@ -61,6 +61,7 @@ def set_item_in_config(config, property_path, value):
return config_copy return config_copy
def unset_item_from_config(config, property_path): def unset_item_from_config(config, property_path):
""" """
Unset key at property_path in config & return new config. Unset key at property_path in config & return new config.
@@ -105,6 +106,7 @@ def unset_item_from_config(config, property_path):
return config_copy return config_copy
def add_item_to_config(config, property_path, value): def add_item_to_config(config, property_path, value):
""" """
Add an item to a list in config. Add an item to a list in config.
@@ -238,15 +240,20 @@ def remove_config_value(config_path, key_path, value):
with open(config_path, 'w') as f: with open(config_path, 'w') as f:
yaml.dump(config, f) yaml.dump(config, f)
def check_hub_ready(): def check_hub_ready():
from .configurer import load_config from .configurer import load_config
base_url = load_config()['base_url']
base_url = base_url[:-1] if base_url[-1] == '/' else base_url
http_port = load_config()['http']['port'] http_port = load_config()['http']['port']
try: try:
r = requests.get('http://127.0.0.1:%d/hub/api' % http_port, verify=False) r = requests.get('http://127.0.0.1:%d%s/hub/api' % (http_port, base_url), verify=False)
return r.status_code == 200 return r.status_code == 200
except: except:
return False return False
def reload_component(component): def reload_component(component):
""" """
Reload a TLJH component. Reload a TLJH component.
@@ -389,13 +396,16 @@ def main(argv=None):
if args.action == 'show': if args.action == 'show':
show_config(args.config_path) show_config(args.config_path)
elif args.action == 'set': elif args.action == 'set':
set_config_value(args.config_path, args.key_path, parse_value(args.value)) set_config_value(args.config_path, args.key_path,
parse_value(args.value))
elif args.action == 'unset': elif args.action == 'unset':
unset_config_value(args.config_path, args.key_path) unset_config_value(args.config_path, args.key_path)
elif args.action == 'add-item': elif args.action == 'add-item':
add_config_value(args.config_path, args.key_path, parse_value(args.value)) add_config_value(args.config_path, args.key_path,
parse_value(args.value))
elif args.action == 'remove-item': elif args.action == 'remove-item':
remove_config_value(args.config_path, args.key_path, parse_value(args.value)) remove_config_value(args.config_path, args.key_path,
parse_value(args.value))
elif args.action == 'reload': elif args.action == 'reload':
reload_component(args.component) reload_component(args.component)
else: else:

View File

@@ -17,6 +17,7 @@ from .yaml import yaml
# Default configuration for tljh # Default configuration for tljh
# User provided config is merged into this # User provided config is merged into this
default = { default = {
'base_url': '/',
'auth': { 'auth': {
'type': 'firstuseauthenticator.FirstUseAuthenticator', 'type': 'firstuseauthenticator.FirstUseAuthenticator',
'FirstUseAuthenticator': { 'FirstUseAuthenticator': {
@@ -69,6 +70,7 @@ default = {
} }
} }
def load_config(config_file=CONFIG_FILE): def load_config(config_file=CONFIG_FILE):
"""Load the current config as a dictionary """Load the current config as a dictionary
@@ -92,6 +94,7 @@ def apply_config(config_overrides, c):
""" """
tljh_config = _merge_dictionaries(dict(default), config_overrides) tljh_config = _merge_dictionaries(dict(default), config_overrides)
update_base_url(c, tljh_config)
update_auth(c, tljh_config) update_auth(c, tljh_config)
update_userlists(c, tljh_config) update_userlists(c, tljh_config)
update_usergroups(c, tljh_config) update_usergroups(c, tljh_config)
@@ -134,6 +137,13 @@ def load_secrets():
return config return config
def update_base_url(c, config):
"""
Update base_url of JupyterHub through tljh config
"""
c.JupyterHub.base_url = config['base_url']
def update_auth(c, config): def update_auth(c, config):
""" """
Set auth related configuration from YAML config file Set auth related configuration from YAML config file