diff --git a/.circleci/config.yml b/.circleci/config.yml index 03efe746..2f4a568a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -19,3 +19,15 @@ jobs: name: smoke test jupyterhub command: | docker run --rm -it jupyterhub/jupyterhub jupyterhub --help + - run: + name: verify static files + command: | + docker run --rm -it -v $PWD/dockerfiles:/io jupyterhub/jupyterhub python3 /io/test.py + + +# Tell CircleCI to use this workflow when it builds the site +workflows: + version: 2 + default: + jobs: + - build diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md deleted file mode 100644 index 476d8bb6..00000000 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ /dev/null @@ -1,37 +0,0 @@ ---- -name: Bug report -about: Create a report to help us improve - ---- - -Hi! Thanks for using JupyterHub. - -If you are reporting an issue with JupyterHub, please use the [GitHub issue](https://github.com/jupyterhub/jupyterhub/issues) search feature to check if your issue has been asked already. If it has, please add your comments to the existing issue. - -**Describe the bug** -A clear and concise description of what the bug is. - -**To Reproduce** -Steps to reproduce the behavior: -1. Go to '...' -2. Click on '....' -3. Scroll down to '....' -4. See error - -**Expected behavior** -A clear and concise description of what you expected to happen. - -**Screenshots** -If applicable, add screenshots to help explain your problem. - -**Desktop (please complete the following information):** - - OS: [e.g. iOS] - - Browser [e.g. chrome, safari] - - Version [e.g. 22] - -**Additional context** -Add any other context about the problem here. - -- Running `jupyter troubleshoot` from the command line, if possible, and posting -its output would also be helpful. -- Running in `--debug` mode can also be helpful for troubleshooting. diff --git a/.github/ISSUE_TEMPLATE/installation-and-configuration-issues.md b/.github/ISSUE_TEMPLATE/installation-and-configuration-issues.md deleted file mode 100644 index 9a6c731b..00000000 --- a/.github/ISSUE_TEMPLATE/installation-and-configuration-issues.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -name: Installation and configuration issues -about: Installation and configuration assistance - ---- - -If you are having issues with installation or configuration, you may ask for help on the JupyterHub gitter channel or file an issue here. diff --git a/.github/PULL_REQUEST_TEMPLATE/.keep b/.github/PULL_REQUEST_TEMPLATE/.keep deleted file mode 100644 index e69de29b..00000000 diff --git a/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE/PULL_REQUEST_TEMPLATE.md similarity index 100% rename from PULL_REQUEST_TEMPLATE.md rename to .github/PULL_REQUEST_TEMPLATE/PULL_REQUEST_TEMPLATE.md diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..671bf5d6 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,206 @@ +# This is a GitHub workflow defining a set of jobs with a set of steps. +# ref: https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions +# +name: Run tests + +# Trigger the workflow's on all PRs but only on pushed tags or commits to +# main/master branch to avoid PRs developed in a GitHub fork's dedicated branch +# to trigger. +on: + pull_request: + push: + +defaults: + run: + # Declare bash be used by default in this workflow's "run" steps. + # + # NOTE: bash will by default run with: + # --noprofile: Ignore ~/.profile etc. + # --norc: Ignore ~/.bashrc etc. + # -e: Exit directly on errors + # -o pipefail: Don't mask errors from a command piped into another command + shell: bash + +env: + # UTF-8 content may be interpreted as ascii and causes errors without this. + LANG: C.UTF-8 + +jobs: + # Run "pre-commit run --all-files" + pre-commit: + runs-on: ubuntu-20.04 + timeout-minutes: 2 + + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + with: + python-version: 3.8 + + # ref: https://github.com/pre-commit/action + - uses: pre-commit/action@v2.0.0 + - name: Help message if pre-commit fail + if: ${{ failure() }} + run: | + echo "You can install pre-commit hooks to automatically run formatting" + echo "on each commit with:" + echo " pre-commit install" + echo "or you can run by hand on staged files with" + echo " pre-commit run" + echo "or after-the-fact on already committed files with" + echo " pre-commit run --all-files" + + + # Run "pytest jupyterhub/tests" in various configurations + pytest: + runs-on: ubuntu-20.04 + timeout-minutes: 10 + + strategy: + # Keep running even if one variation of the job fail + fail-fast: false + matrix: + # We run this job multiple times with different parameterization + # specified below, these parameters have no meaning on their own and + # gain meaning on how job steps use them. + # + # subdomain: + # Tests everything when JupyterHub is configured to add routes for + # users with dedicated subdomains like user1.jupyter.example.com + # rather than jupyter.example.com/user/user1. + # + # db: [mysql/postgres] + # Tests everything when JupyterHub works against a dedicated mysql or + # postgresql server. + # + # jupyter_server: + # Tests everything when the user instances are started with + # jupyter_server instead of notebook. + # + # main_dependencies: + # Tests everything when the we use the latest available dependencies + # from: ipytraitlets. + # + # NOTE: Since only the value of these parameters are presented in the + # GitHub UI when the workflow run, we avoid using true/false as + # values by instead duplicating the name to signal true. + include: + - python: "3.6" + subdomain: subdomain + - python: "3.7" + db: mysql + - python: "3.8" + db: postgres + - python: "3.8" + jupyter_server: jupyter_server + - python: "3.9" + main_dependencies: main_dependencies + + steps: + # NOTE: In GitHub workflows, environment variables are set by writing + # assignment statements to a file. They will be set in the following + # steps as if would used `export MY_ENV=my-value`. + - name: Configure environment variables + run: | + if [ "${{ matrix.subdomain }}" != "" ]; then + echo "JUPYTERHUB_TEST_SUBDOMAIN_HOST=http://localhost.jovyan.org:8000" >> $GITHUB_ENV + fi + if [ "${{ matrix.db }}" == "mysql" ]; then + echo "MYSQL_HOST=127.0.0.1" >> $GITHUB_ENV + echo "JUPYTERHUB_TEST_DB_URL=mysql+mysqlconnector://root@127.0.0.1:3306/jupyterhub" >> $GITHUB_ENV + fi + if [ "${{ matrix.db }}" == "postgres" ]; then + echo "PGHOST=127.0.0.1" >> $GITHUB_ENV + echo "PGUSER=test_user" >> $GITHUB_ENV + echo "PGPASSWORD=hub[test/:?" >> $GITHUB_ENV + echo "JUPYTERHUB_TEST_DB_URL=postgresql://test_user:hub%5Btest%2F%3A%3F@127.0.0.1:5432/jupyterhub" >> $GITHUB_ENV + fi + if [ "${{ matrix.jupyter_server }}" != "" ]; then + echo "JUPYTERHUB_SINGLEUSER_APP=jupyterhub.tests.mockserverapp.MockServerApp" >> $GITHUB_ENV + fi + - uses: actions/checkout@v2 + + # NOTE: actions/setup-node@v1 make use of a cache within the GitHub base + # environment and setup in a fraction of a second. + - name: Install Node v14 + uses: actions/setup-node@v1 + with: + node-version: "14" + - name: Install Node dependencies + run: | + npm install + npm install -g configurable-http-proxy + npm list + + # NOTE: actions/setup-python@v2 make use of a cache within the GitHub base + # environment and setup in a fraction of a second. + - name: Install Python ${{ matrix.python }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python }} + - name: Install Python dependencies + run: | + pip install --upgrade pip + pip install --upgrade . -r dev-requirements.txt + + if [ "${{ matrix.main_dependencies }}" != "" ]; then + pip install git+https://github.com/ipython/traitlets#egg=traitlets --force + fi + if [ "${{ matrix.jupyter_server }}" != "" ]; then + pip uninstall notebook --yes + pip install jupyter_server + fi + if [ "${{ matrix.db }}" == "mysql" ]; then + pip install mysql-connector-python + fi + if [ "${{ matrix.db }}" == "postgres" ]; then + pip install psycopg2-binary + fi + + pip freeze + + # NOTE: If you need to debug this DB setup step, consider the following. + # + # 1. mysql/postgressql are database servers we start as docker containers, + # and we use clients named mysql/psql. + # + # 2. When we start a database server we need to pass environment variables + # explicitly as part of the `docker run` command. These environment + # variables are named differently from the similarly named environment + # variables used by the clients. + # + # - mysql server ref: https://hub.docker.com/_/mysql/ + # - mysql client ref: https://dev.mysql.com/doc/refman/5.7/en/environment-variables.html + # - postgres server ref: https://hub.docker.com/_/postgres/ + # - psql client ref: https://www.postgresql.org/docs/9.5/libpq-envars.html + # + # 3. When we connect, they should use 127.0.0.1 rather than the + # default way of connecting which leads to errors like below both for + # mysql and postgresql unless we set MYSQL_HOST/PGHOST to 127.0.0.1. + # + # - ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) + # + - name: Start a database server (${{ matrix.db }}) + if: ${{ matrix.db }} + run: | + if [ "${{ matrix.db }}" == "mysql" ]; then + sudo apt-get update + sudo apt-get install -y mysql-client + DB=mysql bash ci/docker-db.sh + DB=mysql bash ci/init-db.sh + fi + if [ "${{ matrix.db }}" == "postgres" ]; then + sudo apt-get update + sudo apt-get install -y postgresql-client + DB=postgres bash ci/docker-db.sh + DB=postgres bash ci/init-db.sh + fi + + - name: Run pytest + # FIXME: --color=yes explicitly set because: + # https://github.com/actions/runner/issues/241 + run: | + pytest -v --maxfail=2 --color=yes --cov=jupyterhub jupyterhub/tests + - name: Submit codecov report + run: | + codecov diff --git a/.gitignore b/.gitignore index 90608475..44a7b3d4 100644 --- a/.gitignore +++ b/.gitignore @@ -24,5 +24,7 @@ MANIFEST .coverage.* htmlcov .idea/ +.vscode/ .pytest_cache pip-wheel-metadata +docs/source/reference/metrics.rst diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ae6a999d..e8f6663e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,15 +1,14 @@ repos: - repo: https://github.com/asottile/reorder_python_imports - rev: v1.3.5 + rev: v1.9.0 hooks: - id: reorder-python-imports - language_version: python3.6 -- repo: https://github.com/ambv/black - rev: 18.9b0 +- repo: https://github.com/psf/black + rev: 19.10b0 hooks: - id: black - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v2.1.0 + rev: v2.4.0 hooks: - id: end-of-file-fixer - id: check-json diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 788bfa4f..00000000 --- a/.travis.yml +++ /dev/null @@ -1,100 +0,0 @@ -language: python -sudo: false -cache: - - pip -python: - - 3.6 - - 3.5 - - nightly -env: - global: - - ASYNC_TEST_TIMEOUT=15 - - MYSQL_HOST=127.0.0.1 - - MYSQL_TCP_PORT=13306 -services: - - postgres - - docker - -# installing dependencies -before_install: - - set -e - - nvm install 6; nvm use 6 - - npm install - - npm install -g configurable-http-proxy - - | - # setup database - if [[ $JUPYTERHUB_TEST_DB_URL == mysql* ]]; then - unset MYSQL_UNIX_PORT - DB=mysql bash ci/docker-db.sh - DB=mysql bash ci/init-db.sh - # FIXME: mysql-connector-python 8.0.16 incorrectly decodes bytes to str - # ref: https://bugs.mysql.com/bug.php?id=94944 - pip install 'mysql-connector-python==8.0.15' - elif [[ $JUPYTERHUB_TEST_DB_URL == postgresql* ]]; then - psql -c "CREATE USER $PGUSER WITH PASSWORD '$PGPASSWORD';" -U postgres - DB=postgres bash ci/init-db.sh - pip install psycopg2-binary - fi -install: - - pip install --upgrade pip - - pip install --upgrade --pre -r dev-requirements.txt . - - pip freeze - -# running tests -script: - - | - # run tests - if [[ -z "$TEST" ]]; then - pytest -v --maxfail=2 --cov=jupyterhub jupyterhub/tests - fi - - | - # run autoformat - if [[ "$TEST" == "lint" ]]; then - pre-commit run --all-files - fi - - | - # build docs - if [[ "$TEST" == "docs" ]]; then - pushd docs - pip install --upgrade -r requirements.txt - pip install --upgrade alabaster_jupyterhub - make html - popd - fi -after_success: - - codecov -after_failure: - - | - # point to auto-lint-fix - if [[ "$TEST" == "lint" ]]; then - echo "You can install pre-commit hooks to automatically run formatting" - echo "on each commit with:" - echo " pre-commit install" - echo "or you can run by hand on staged files with" - echo " pre-commit run" - echo "or after-the-fact on already committed files with" - echo " pre-commit run --all-files" - fi - -matrix: - fast_finish: true - include: - - python: 3.6 - env: TEST=lint - - python: 3.6 - env: TEST=docs - - python: 3.6 - env: JUPYTERHUB_TEST_SUBDOMAIN_HOST=http://localhost.jovyan.org:8000 - - python: 3.6 - env: - - JUPYTERHUB_TEST_DB_URL=mysql+mysqlconnector://root@127.0.0.1:$MYSQL_TCP_PORT/jupyterhub - - python: 3.6 - env: - - PGUSER=jupyterhub - - PGPASSWORD=hub[test/:? - # password in url is url-encoded (urllib.parse.quote($PGPASSWORD, safe='')) - - JUPYTERHUB_TEST_DB_URL=postgresql://jupyterhub:hub%5Btest%2F%3A%3F@127.0.0.1/jupyterhub - - python: 3.7 - dist: xenial - allow_failures: - - python: nightly diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index cfced3c8..0f5ec05a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,6 +8,12 @@ for a friendly and welcoming collaborative environment. ## Setting up a development environment + + JupyterHub requires Python >= 3.5 and nodejs. As a Python project, a development install of JupyterHub follows standard practices for the basics (steps 1-2). @@ -60,7 +66,7 @@ pre-commit run which should run any autoformatting on your code and tell you about any errors it couldn't fix automatically. -You may also install [black integration](https://github.com/ambv/black#editor-integration) +You may also install [black integration](https://github.com/psf/black#editor-integration) into your text editor to format code automatically. If you have already committed files before setting up the pre-commit @@ -97,6 +103,35 @@ and other collections of tests for different components. When writing a new test, there should usually be a test of similar functionality already written and related tests should be added nearby. -When in doubt, feel free to ask. -TODO: describe some details about fixtures, etc. +The fixtures live in `jupyterhub/tests/conftest.py`. There are +fixtures that can be used for JupyterHub components, such as: + +- `app`: an instance of JupyterHub with mocked parts +- `auth_state_enabled`: enables persisting auth_state (like authentication tokens) +- `db`: a sqlite in-memory DB session +- `io_loop`: a Tornado event loop +- `event_loop`: a new asyncio event loop +- `user`: creates a new temporary user +- `admin_user`: creates a new temporary admin user +- single user servers + - `cleanup_after`: allows cleanup of single user servers between tests +- mocked service + - `MockServiceSpawner`: a spawner that mocks services for testing with a short poll interval + - `mockservice`: mocked service with no external service url + - `mockservice_url`: mocked service with a url to test external services + +And fixtures to add functionality or spawning behavior: + +- `admin_access`: grants admin access +- `no_patience`: sets slow-spawning timeouts to zero +- `slow_spawn`: enables the SlowSpawner (a spawner that takes a few seconds to start) +- `never_spawn`: enables the NeverSpawner (a spawner that will never start) +- `bad_spawn`: enables the BadSpawner (a spawner that fails immediately) +- `slow_bad_spawn`: enables the SlowBadSpawner (a spawner that fails after a short delay) + +To read more about fixtures check out the +[pytest docs](https://docs.pytest.org/en/latest/fixture.html) +for how to use the existing fixtures, and how to create new ones. + +When in doubt, feel free to [ask](https://gitter.im/jupyterhub/jupyterhub). diff --git a/Dockerfile b/Dockerfile index 9277cdc6..ab7fca68 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,40 +21,81 @@ # your jupyterhub_config.py will be added automatically # from your docker directory. -FROM ubuntu:18.04 -LABEL maintainer="Jupyter Project " +ARG BASE_IMAGE=ubuntu:focal-20200729@sha256:6f2fb2f9fb5582f8b587837afd6ea8f37d8d1d9e41168c90f410a6ef15fa8ce5 +FROM $BASE_IMAGE AS builder + +USER root -# install nodejs, utf8 locale, set CDN because default httpredir is unreliable ENV DEBIAN_FRONTEND noninteractive -RUN apt-get -y update && \ - apt-get -y upgrade && \ - apt-get -y install wget git bzip2 && \ - apt-get purge && \ - apt-get clean && \ - rm -rf /var/lib/apt/lists/* -ENV LANG C.UTF-8 +RUN apt-get update \ + && apt-get install -yq --no-install-recommends \ + build-essential \ + ca-certificates \ + locales \ + python3-dev \ + python3-pip \ + python3-pycurl \ + nodejs \ + npm \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* -# install Python + NodeJS with conda -RUN wget -q https://repo.continuum.io/miniconda/Miniconda3-4.5.11-Linux-x86_64.sh -O /tmp/miniconda.sh && \ - echo 'e1045ee415162f944b6aebfe560b8fee */tmp/miniconda.sh' | md5sum -c - && \ - bash /tmp/miniconda.sh -f -b -p /opt/conda && \ - /opt/conda/bin/conda install --yes -c conda-forge \ - python=3.6 sqlalchemy tornado jinja2 traitlets requests pip pycurl \ - nodejs configurable-http-proxy && \ - /opt/conda/bin/pip install --upgrade pip && \ - rm /tmp/miniconda.sh -ENV PATH=/opt/conda/bin:$PATH +RUN python3 -m pip install --upgrade setuptools pip wheel -ADD . /src/jupyterhub +# copy everything except whats in .dockerignore, its a +# compromise between needing to rebuild and maintaining +# what needs to be part of the build +COPY . /src/jupyterhub/ WORKDIR /src/jupyterhub -RUN pip install . && \ - rm -rf $PWD ~/.cache ~/.npm +# Build client component packages (they will be copied into ./share and +# packaged with the built wheel.) +RUN python3 setup.py bdist_wheel +RUN python3 -m pip wheel --wheel-dir wheelhouse dist/*.whl + + +FROM $BASE_IMAGE + +USER root + +ENV DEBIAN_FRONTEND=noninteractive + +RUN apt-get update \ + && apt-get install -yq --no-install-recommends \ + ca-certificates \ + curl \ + gnupg \ + locales \ + python3-pip \ + python3-pycurl \ + nodejs \ + npm \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +ENV SHELL=/bin/bash \ + LC_ALL=en_US.UTF-8 \ + LANG=en_US.UTF-8 \ + LANGUAGE=en_US.UTF-8 + +RUN locale-gen $LC_ALL + +# always make sure pip is up to date! +RUN python3 -m pip install --no-cache --upgrade setuptools pip + +RUN npm install -g configurable-http-proxy@^4.2.0 \ + && rm -rf ~/.npm + +# install the wheels we built in the first stage +COPY --from=builder /src/jupyterhub/wheelhouse /tmp/wheelhouse +RUN python3 -m pip install --no-cache /tmp/wheelhouse/* RUN mkdir -p /srv/jupyterhub/ WORKDIR /srv/jupyterhub/ + EXPOSE 8000 +LABEL maintainer="Jupyter Project " LABEL org.jupyter.service="jupyterhub" CMD ["jupyterhub"] diff --git a/README.md b/README.md index 6c1fc6d0..28afd74e 100644 --- a/README.md +++ b/README.md @@ -10,14 +10,16 @@ # [JupyterHub](https://github.com/jupyterhub/jupyterhub) -[![PyPI](https://img.shields.io/pypi/v/jupyterhub.svg)](https://pypi.python.org/pypi/jupyterhub) -[![Documentation Status](https://readthedocs.org/projects/jupyterhub/badge/?version=latest)](https://jupyterhub.readthedocs.org/en/latest/?badge=latest) -[![Build Status](https://travis-ci.org/jupyterhub/jupyterhub.svg?branch=master)](https://travis-ci.org/jupyterhub/jupyterhub) -[![Circle CI](https://circleci.com/gh/jupyterhub/jupyterhub.svg?style=shield&circle-token=b5b65862eb2617b9a8d39e79340b0a6b816da8cc)](https://circleci.com/gh/jupyterhub/jupyterhub) -[![codecov.io](https://codecov.io/github/jupyterhub/jupyterhub/coverage.svg?branch=master)](https://codecov.io/github/jupyterhub/jupyterhub?branch=master) -[![GitHub](https://img.shields.io/badge/issue_tracking-github-blue.svg)](https://github.com/jupyterhub/jupyterhub/issues) -[![Discourse](https://img.shields.io/badge/help_forum-discourse-blue.svg)](https://discourse.jupyter.org/c/jupyterhub) -[![Gitter](https://img.shields.io/badge/social_chat-gitter-blue.svg)](https://gitter.im/jupyterhub/jupyterhub) +[![Latest PyPI version](https://img.shields.io/pypi/v/jupyterhub?logo=pypi)](https://pypi.python.org/pypi/jupyterhub) +[![Latest conda-forge version](https://img.shields.io/conda/vn/conda-forge/jupyterhub?logo=conda-forge)](https://www.npmjs.com/package/jupyterhub) +[![Documentation build status](https://img.shields.io/readthedocs/jupyterhub?logo=read-the-docs)](https://jupyterhub.readthedocs.org/en/latest/) +[![TravisCI build status](https://img.shields.io/travis/com/jupyterhub/jupyterhub?logo=travis)](https://travis-ci.com/jupyterhub/jupyterhub) +[![DockerHub build status](https://img.shields.io/docker/build/jupyterhub/jupyterhub?logo=docker&label=build)](https://hub.docker.com/r/jupyterhub/jupyterhub/tags) +[![CircleCI build status](https://img.shields.io/circleci/build/github/jupyterhub/jupyterhub?logo=circleci)](https://circleci.com/gh/jupyterhub/jupyterhub) +[![Test coverage of code](https://codecov.io/gh/jupyterhub/jupyterhub/branch/master/graph/badge.svg)](https://codecov.io/gh/jupyterhub/jupyterhub) +[![GitHub](https://img.shields.io/badge/issue_tracking-github-blue?logo=github)](https://github.com/jupyterhub/jupyterhub/issues) +[![Discourse](https://img.shields.io/badge/help_forum-discourse-blue?logo=discourse)](https://discourse.jupyter.org/c/jupyterhub) +[![Gitter](https://img.shields.io/badge/social_chat-gitter-blue?logo=gitter)](https://gitter.im/jupyterhub/jupyterhub) With [JupyterHub](https://jupyterhub.readthedocs.io) you can create a **multi-user Hub** which spawns, manages, and proxies multiple instances of the @@ -72,6 +74,7 @@ for administration of the Hub and its users. The `nodejs-legacy` package installs the `node` executable and is currently required for npm to work on Debian/Ubuntu. +- If using the default PAM Authenticator, a [pluggable authentication module (PAM)](https://en.wikipedia.org/wiki/Pluggable_authentication_module). - TLS certificate and key for HTTPS communication - Domain name @@ -145,12 +148,12 @@ To start the Hub on a specific url and port ``10.0.1.2:443`` with **https**: ### Authenticators -| Authenticator | Description | -| --------------------------------------------------------------------------- | ------------------------------------------------- | -| PAMAuthenticator | Default, built-in authenticator | -| [OAuthenticator](https://github.com/jupyterhub/oauthenticator) | OAuth + JupyterHub Authenticator = OAuthenticator | -| [ldapauthenticator](https://github.com/jupyterhub/ldapauthenticator) | Simple LDAP Authenticator Plugin for JupyterHub | -| [kerberosauthenticator](https://github.com/jcrist/kerberosauthenticator) | Kerberos Authenticator Plugin for JupyterHub | +| Authenticator | Description | +| ---------------------------------------------------------------------------- | ------------------------------------------------- | +| PAMAuthenticator | Default, built-in authenticator | +| [OAuthenticator](https://github.com/jupyterhub/oauthenticator) | OAuth + JupyterHub Authenticator = OAuthenticator | +| [ldapauthenticator](https://github.com/jupyterhub/ldapauthenticator) | Simple LDAP Authenticator Plugin for JupyterHub | +| [kerberosauthenticator](https://github.com/jupyterhub/kerberosauthenticator) | Kerberos Authenticator Plugin for JupyterHub | ### Spawners @@ -162,7 +165,7 @@ To start the Hub on a specific url and port ``10.0.1.2:443`` with **https**: | [sudospawner](https://github.com/jupyterhub/sudospawner) | Spawn single-user servers without being root | | [systemdspawner](https://github.com/jupyterhub/systemdspawner) | Spawn single-user notebook servers using systemd | | [batchspawner](https://github.com/jupyterhub/batchspawner) | Designed for clusters using batch scheduling software | -| [yarnspawner](https://github.com/jcrist/yarnspawner) | Spawn single-user notebook servers distributed on a Hadoop cluster | +| [yarnspawner](https://github.com/jupyterhub/yarnspawner) | Spawn single-user notebook servers distributed on a Hadoop cluster | | [wrapspawner](https://github.com/jupyterhub/wrapspawner) | WrapSpawner and ProfilesSpawner enabling runtime configuration of spawners | ## Docker @@ -242,6 +245,7 @@ our JupyterHub [Gitter](https://gitter.im/jupyterhub/jupyterhub) channel. - [Documentation for JupyterHub's REST API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/jupyterhub/master/docs/rest-api.yml#/default) - [Documentation for Project Jupyter](http://jupyter.readthedocs.io/en/latest/index.html) | [PDF](https://media.readthedocs.org/pdf/jupyter/latest/jupyter.pdf) - [Project Jupyter website](https://jupyter.org) +- [Project Jupyter community](https://jupyter.org/community) JupyterHub follows the Jupyter [Community Guides](https://jupyter.readthedocs.io/en/latest/community/content-community.html). diff --git a/ci/docker-db.sh b/ci/docker-db.sh index 14e06388..9bb69b73 100755 --- a/ci/docker-db.sh +++ b/ci/docker-db.sh @@ -1,59 +1,60 @@ #!/usr/bin/env bash -# source this file to setup postgres and mysql -# for local testing (as similar as possible to docker) +# The goal of this script is to start a database server as a docker container. +# +# Required environment variables: +# - DB: The database server to start, either "postgres" or "mysql". +# +# - PGUSER/PGPASSWORD: For the creation of a postgresql user with associated +# password. set -eu -export MYSQL_HOST=127.0.0.1 -export MYSQL_TCP_PORT=${MYSQL_TCP_PORT:-13306} -export PGHOST=127.0.0.1 -NAME="hub-test-$DB" -DOCKER_RUN="docker run -d --name $NAME" +# Stop and remove any existing database container +DOCKER_CONTAINER="hub-test-$DB" +docker rm -f "$DOCKER_CONTAINER" 2>/dev/null || true -docker rm -f "$NAME" 2>/dev/null || true +# Prepare environment variables to startup and await readiness of either a mysql +# or postgresql server. +if [[ "$DB" == "mysql" ]]; then + # Environment variables can influence both the mysql server in the docker + # container and the mysql client. + # + # ref server: https://hub.docker.com/_/mysql/ + # ref client: https://dev.mysql.com/doc/refman/5.7/en/setting-environment-variables.html + # + DOCKER_RUN_ARGS="-p 3306:3306 --env MYSQL_ALLOW_EMPTY_PASSWORD=1 mysql:5.7" + READINESS_CHECK="mysql --user root --execute \q" +elif [[ "$DB" == "postgres" ]]; then + # Environment variables can influence both the postgresql server in the + # docker container and the postgresql client (psql). + # + # ref server: https://hub.docker.com/_/postgres/ + # ref client: https://www.postgresql.org/docs/9.5/libpq-envars.html + # + # POSTGRES_USER / POSTGRES_PASSWORD will create a user on startup of the + # postgres server, but PGUSER and PGPASSWORD are the environment variables + # used by the postgresql client psql, so we configure the user based on how + # we want to connect. + # + DOCKER_RUN_ARGS="-p 5432:5432 --env "POSTGRES_USER=${PGUSER}" --env "POSTGRES_PASSWORD=${PGPASSWORD}" postgres:9.5" + READINESS_CHECK="psql --command \q" +else + echo '$DB must be mysql or postgres' + exit 1 +fi -case "$DB" in -"mysql") - RUN_ARGS="-e MYSQL_ALLOW_EMPTY_PASSWORD=1 -p $MYSQL_TCP_PORT:3306 mysql:5.7" - CHECK="mysql --host $MYSQL_HOST --port $MYSQL_TCP_PORT --user root -e \q" - ;; -"postgres") - RUN_ARGS="-p 5432:5432 postgres:9.5" - CHECK="psql --user postgres -c \q" - ;; -*) - echo '$DB must be mysql or postgres' - exit 1 -esac - -$DOCKER_RUN $RUN_ARGS +# Start the database server +docker run --detach --name "$DOCKER_CONTAINER" $DOCKER_RUN_ARGS +# Wait for the database server to start echo -n "waiting for $DB " for i in {1..60}; do - if $CHECK; then - echo 'done' - break - else - echo -n '.' - sleep 1 - fi + if $READINESS_CHECK; then + echo 'done' + break + else + echo -n '.' + sleep 1 + fi done -$CHECK - -case "$DB" in -"mysql") - ;; -"postgres") - # create the user - psql --user postgres -c "CREATE USER $PGUSER WITH PASSWORD '$PGPASSWORD';" - ;; -*) -esac - -echo -e " -Set these environment variables: - - export MYSQL_HOST=127.0.0.1 - export MYSQL_TCP_PORT=$MYSQL_TCP_PORT - export PGHOST=127.0.0.1 -" +$READINESS_CHECK diff --git a/ci/init-db.sh b/ci/init-db.sh index dfeb12b4..b510f549 100755 --- a/ci/init-db.sh +++ b/ci/init-db.sh @@ -1,27 +1,26 @@ #!/usr/bin/env bash -# initialize jupyterhub databases for testing +# The goal of this script is to initialize a running database server with clean +# databases for use during tests. +# +# Required environment variables: +# - DB: The database server to start, either "postgres" or "mysql". set -eu -MYSQL="mysql --user root --host $MYSQL_HOST --port $MYSQL_TCP_PORT -e " -PSQL="psql --user postgres -c " - -case "$DB" in -"mysql") - EXTRA_CREATE='CHARACTER SET utf8 COLLATE utf8_general_ci' - SQL="$MYSQL" - ;; -"postgres") - SQL="$PSQL" - ;; -*) - echo '$DB must be mysql or postgres' - exit 1 -esac +# Prepare env vars SQL_CLIENT and EXTRA_CREATE_DATABASE_ARGS +if [[ "$DB" == "mysql" ]]; then + SQL_CLIENT="mysql --user root --execute " + EXTRA_CREATE_DATABASE_ARGS='CHARACTER SET utf8 COLLATE utf8_general_ci' +elif [[ "$DB" == "postgres" ]]; then + SQL_CLIENT="psql --command " +else + echo '$DB must be mysql or postgres' + exit 1 +fi +# Configure a set of databases in the database server for upgrade tests set -x - for SUFFIX in '' _upgrade_072 _upgrade_081 _upgrade_094; do - $SQL "DROP DATABASE jupyterhub${SUFFIX};" 2>/dev/null || true - $SQL "CREATE DATABASE jupyterhub${SUFFIX} ${EXTRA_CREATE:-};" + $SQL_CLIENT "DROP DATABASE jupyterhub${SUFFIX};" 2>/dev/null || true + $SQL_CLIENT "CREATE DATABASE jupyterhub${SUFFIX} ${EXTRA_CREATE_DATABASE_ARGS:-};" done diff --git a/demo-image/Dockerfile b/demo-image/Dockerfile new file mode 100644 index 00000000..c5add75e --- /dev/null +++ b/demo-image/Dockerfile @@ -0,0 +1,16 @@ +# Demo JupyterHub Docker image +# +# This should only be used for demo or testing and not as a base image to build on. +# +# It includes the notebook package and it uses the DummyAuthenticator and the SimpleLocalProcessSpawner. +ARG BASE_IMAGE=jupyterhub/jupyterhub-onbuild +FROM ${BASE_IMAGE} + +# Install the notebook package +RUN python3 -m pip install notebook + +# Create a demo user +RUN useradd --create-home demo +RUN chown demo . + +USER demo diff --git a/demo-image/README.md b/demo-image/README.md new file mode 100644 index 00000000..7c9681b8 --- /dev/null +++ b/demo-image/README.md @@ -0,0 +1,25 @@ +## Demo Dockerfile + +This is a demo JupyterHub Docker image to help you get a quick overview of what +JupyterHub is and how it works. + +It uses the SimpleLocalProcessSpawner to spawn new user servers and +DummyAuthenticator for authentication. +The DummyAuthenticator allows you to log in with any username & password and the +SimpleLocalProcessSpawner allows starting servers without having to create a +local user for each JupyterHub user. + +### Important! + +This should only be used for demo or testing purposes! +It shouldn't be used as a base image to build on. + +### Try it +1. `cd` to the root of your jupyterhub repo. + +2. Build the demo image with `docker build -t jupyterhub-demo demo-image`. + +3. Run the demo image with `docker run -d -p 8000:8000 jupyterhub-demo`. + +4. Visit http://localhost:8000 and login with any username and password +5. Happy demo-ing :tada:! diff --git a/demo-image/jupyterhub_config.py b/demo-image/jupyterhub_config.py new file mode 100644 index 00000000..312df765 --- /dev/null +++ b/demo-image/jupyterhub_config.py @@ -0,0 +1,7 @@ +# Configuration file for jupyterhub-demo + +c = get_config() + +# Use DummyAuthenticator and SimpleSpawner +c.JupyterHub.spawner_class = "simple" +c.JupyterHub.authenticator_class = "dummy" diff --git a/dev-requirements.txt b/dev-requirements.txt index 54a64481..7b14b306 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -14,4 +14,7 @@ pytest-asyncio pytest-cov pytest>=3.3 requests-mock +# blacklist urllib3 releases affected by https://github.com/urllib3/urllib3/issues/1683 +# I *think* this should only affect testing, not production +urllib3!=1.25.4,!=1.25.5 virtualenv diff --git a/dockerfiles/README.md b/dockerfiles/README.md index 5799597a..b17546bd 100644 --- a/dockerfiles/README.md +++ b/dockerfiles/README.md @@ -12,7 +12,7 @@ Dockerfile.alpine contains base image for jupyterhub. It does not work independ * start configurable-http-proxy in another container * specify CONFIGPROXY_AUTH_TOKEN env in both containers -* put both containers on the same network (e.g. docker create network jupyterhub; docker run ... --net jupyterhub) +* put both containers on the same network (e.g. docker network create jupyterhub; docker run ... --net jupyterhub) * tell jupyterhub where CHP is (e.g. c.ConfigurableHTTPProxy.api_url = 'http://chp:8001') * tell jupyterhub not to start the proxy itself (c.ConfigurableHTTPProxy.should_start = False) * Use dummy authenticator for ease of testing. Update following in jupyterhub_config file diff --git a/dockerfiles/test.py b/dockerfiles/test.py new file mode 100644 index 00000000..32b6dd30 --- /dev/null +++ b/dockerfiles/test.py @@ -0,0 +1,9 @@ +import os + +from jupyterhub._data import DATA_FILES_PATH + +print(f"DATA_FILES_PATH={DATA_FILES_PATH}") + +for sub_path in ("templates", "static/components", "static/css/style.min.css"): + path = os.path.join(DATA_FILES_PATH, sub_path) + assert os.path.exists(path), path diff --git a/docs/Makefile b/docs/Makefile index b20dc238..5e61789e 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -48,6 +48,7 @@ help: @echo " doctest to run all doctests embedded in the documentation (if enabled)" @echo " coverage to run coverage check of the documentation (if enabled)" @echo " spelling to run spell check on documentation" + @echo " metrics to generate documentation for metrics by inspecting the source code" clean: rm -rf $(BUILDDIR)/* @@ -60,7 +61,12 @@ rest-api: source/_static/rest-api/index.html source/_static/rest-api/index.html: rest-api.yml node_modules npm run rest-api -html: rest-api +metrics: source/reference/metrics.rst + +source/reference/metrics.rst: generate-metrics.py + python3 generate-metrics.py + +html: rest-api metrics $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." diff --git a/docs/environment.yml b/docs/environment.yml deleted file mode 100644 index b23b5601..00000000 --- a/docs/environment.yml +++ /dev/null @@ -1,25 +0,0 @@ -# ReadTheDocs uses the `environment.yaml` so make sure to update that as well -# if you change the dependencies of JupyterHub in the various `requirements.txt` -name: jhub_docs -channels: - - conda-forge -dependencies: -- nodejs -- python=3.6 -- alembic -- jinja2 -- pamela -- requests -- sqlalchemy>=1 -- tornado>=5.0 -- traitlets>=4.1 -- sphinx>=1.7 -- pip: - - entrypoints - - oauthlib>=2.0 - - recommonmark==0.5.0 - - async_generator - - prometheus_client - - attrs>=17.4.0 - - sphinx-copybutton - - alabaster_jupyterhub diff --git a/docs/generate-metrics.py b/docs/generate-metrics.py new file mode 100644 index 00000000..81304bef --- /dev/null +++ b/docs/generate-metrics.py @@ -0,0 +1,57 @@ +import os +from os.path import join + +from pytablewriter import RstSimpleTableWriter +from pytablewriter.style import Style + +import jupyterhub.metrics + +HERE = os.path.abspath(os.path.dirname(__file__)) + + +class Generator: + @classmethod + def create_writer(cls, table_name, headers, values): + writer = RstSimpleTableWriter() + writer.table_name = table_name + writer.headers = headers + writer.value_matrix = values + writer.margin = 1 + [writer.set_style(header, Style(align="center")) for header in headers] + return writer + + def _parse_metrics(self): + table_rows = [] + for name in dir(jupyterhub.metrics): + obj = getattr(jupyterhub.metrics, name) + if obj.__class__.__module__.startswith('prometheus_client.'): + for metric in obj.describe(): + table_rows.append([metric.type, metric.name, metric.documentation]) + return table_rows + + def prometheus_metrics(self): + generated_directory = f"{HERE}/source/reference" + if not os.path.exists(generated_directory): + os.makedirs(generated_directory) + + filename = f"{generated_directory}/metrics.rst" + table_name = "" + headers = ["Type", "Name", "Description"] + values = self._parse_metrics() + writer = self.create_writer(table_name, headers, values) + + title = "List of Prometheus Metrics" + underline = "============================" + content = f"{title}\n{underline}\n{writer.dumps()}" + with open(filename, 'w') as f: + f.write(content) + print(f"Generated {filename}.") + + +def main(): + doc_generator = Generator() + doc_generator.prometheus_metrics() + + +if __name__ == "__main__": + main() diff --git a/docs/requirements.txt b/docs/requirements.txt index 5f8b447a..2d9da795 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,7 +1,12 @@ -# ReadTheDocs uses the `environment.yaml` so make sure to update that as well -# if you change this file -r ../requirements.txt + alabaster_jupyterhub -recommonmark==0.5.0 +# Temporary fix of #3021. Revert back to released autodoc-traits when +# 0.1.0 released. +https://github.com/jupyterhub/autodoc-traits/archive/75885ee24636efbfebfceed1043459715049cd84.zip +pydata-sphinx-theme +pytablewriter>=0.56 +recommonmark>=0.6 sphinx-copybutton +sphinx-jsonschema sphinx>=1.7 diff --git a/docs/rest-api.yml b/docs/rest-api.yml index 573e886b..09d0ed99 100644 --- a/docs/rest-api.yml +++ b/docs/rest-api.yml @@ -1,13 +1,13 @@ -# see me at: http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyter/jupyterhub/master/docs/rest-api.yml#/default +# see me at: http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyterhub/jupyterhub/master/docs/rest-api.yml#/default swagger: '2.0' info: title: JupyterHub description: The REST API for JupyterHub - version: 0.9.0dev + version: 1.2.0dev license: name: BSD-3-Clause schemes: - - [http, https] + [http, https] securityDefinitions: token: type: apiKey @@ -190,7 +190,7 @@ paths: in: path required: true type: string - - body: + - name: body in: body schema: type: object @@ -202,34 +202,37 @@ paths: Timestamp of last-seen activity for this user. Only needed if this is not activity associated with using a given server. - required: false servers: description: | Register activity for specific servers by name. The keys of this dict are the names of servers. The default server has an empty name (''). - required: false type: object properties: '': description: | Activity for a single server. type: object + required: + - last_activity properties: last_activity: - required: true type: string format: date-time description: | Timestamp of last-seen activity on this server. - example: - last_activity: '2019-02-06T12:54:14Z' - servers: - '': - last_activity: '2019-02-06T12:54:14Z' - gpu: - last_activity: '2019-02-06T12:54:14Z' - + example: + last_activity: '2019-02-06T12:54:14Z' + servers: + '': + last_activity: '2019-02-06T12:54:14Z' + gpu: + last_activity: '2019-02-06T12:54:14Z' + responses: + '401': + $ref: '#/responses/Unauthorized' + '404': + description: No such user /users/{name}/server: post: summary: Start a user's single-user notebook server @@ -239,15 +242,19 @@ paths: in: path required: true type: string - - options: + - name: options description: | Spawn options can be passed as a JSON body when spawning via the API instead of spawn form. The structure of the options will depend on the Spawner's configuration. + The body itself will be available as `user_options` for the + Spawner. in: body required: false - type: object + schema: + type: object + responses: '201': description: The user's notebook server has started @@ -276,11 +283,14 @@ paths: required: true type: string - name: server_name - description: name given to a named-server + description: | + name given to a named-server. + + Note that depending on your JupyterHub infrastructure there are chracterter size limitation to `server_name`. Default spawner with K8s pod will not allow Jupyter Notebooks to be spawned with a name that contains more than 253 characters (keep in mind that the pod will be spawned with extra characters to identify the user and hub). in: path required: true type: string - - options: + - name: options description: | Spawn options can be passed as a JSON body when spawning via the API instead of spawn form. @@ -288,7 +298,8 @@ paths: will depend on the Spawner's configuration. in: body required: false - type: object + schema: + type: object responses: '201': description: The user's notebook named-server has started @@ -307,19 +318,30 @@ paths: in: path required: true type: string - - name: remove - description: | - Whether to fully remove the server, rather than just stop it. - Removing a server deletes things like the state of the stopped server. + - name: body in: body required: false - type: boolean + schema: + type: object + properties: + remove: + type: boolean + description: | + Whether to fully remove the server, rather than just stop it. + Removing a server deletes things like the state of the stopped server. + Default: false. responses: '204': description: The user's notebook named-server has stopped '202': description: The user's notebook named-server has not yet stopped as it is taking a while to stop /users/{name}/tokens: + parameters: + - name: name + description: username + in: path + required: true + type: string get: summary: List tokens for the user responses: @@ -329,25 +351,43 @@ paths: type: array items: $ref: '#/definitions/Token' + '401': + $ref: '#/responses/Unauthorized' + '404': + description: No such user post: summary: Create a new token for the user parameters: - - name: expires_in - type: number - required: false + - name: token_params in: body - description: lifetime (in seconds) after which the requested token will expire. - - name: note - type: string required: false - in: body - description: A note attached to the token for future bookkeeping + schema: + type: object + properties: + expires_in: + type: number + description: lifetime (in seconds) after which the requested token will expire. + note: + type: string + description: A note attached to the token for future bookkeeping responses: '201': description: The newly created token schema: $ref: '#/definitions/Token' + '400': + description: Body must be a JSON dict or empty /users/{name}/tokens/{token_id}: + parameters: + - name: name + description: username + in: path + required: true + type: string + - name: token_id + in: path + required: true + type: string get: summary: Get the model for a token by id responses: @@ -361,12 +401,13 @@ paths: '204': description: The token has been deleted /user: - summary: Return authenticated user's model - description: - parameters: - responses: - '200': - description: The authenticated user's model is returned. + get: + summary: Return authenticated user's model + responses: + '200': + description: The authenticated user's model is returned. + schema: + $ref: '#/definitions/User' /groups: get: summary: List groups @@ -539,14 +580,15 @@ paths: Logging in via this method is only available when the active Authenticator accepts passwords (e.g. not OAuth). parameters: - - name: username + - name: credentials in: body - required: false - type: string - - name: password - in: body - required: false - type: string + schema: + type: object + properties: + username: + type: string + password: + type: string responses: '200': description: The new API token @@ -562,10 +604,10 @@ paths: get: summary: Identify a user or service from an API token parameters: - - name: token - in: path - required: true - type: string + - name: token + in: path + required: true + type: string responses: '200': description: The user or service identified by the API token @@ -576,14 +618,14 @@ paths: summary: Identify a user from a cookie description: Used by single-user notebook servers to hand off cookie authentication to the Hub parameters: - - name: cookie_name - in: path - required: true - type: string - - name: cookie_value - in: path - required: true - type: string + - name: cookie_name + in: path + required: true + type: string + - name: cookie_value + in: path + required: true + type: string responses: '200': description: The user identified by the cookie @@ -618,6 +660,11 @@ paths: in: query required: true type: string + responses: + '200': + description: Success + '400': + description: OAuth2Error /oauth2/token: post: summary: Request an OAuth2 token @@ -629,27 +676,27 @@ paths: parameters: - name: client_id description: The client id - in: form + in: formData required: true type: string - name: client_secret description: The client secret - in: form + in: formData required: true type: string - name: grant_type description: The grant type (always 'authorization_code') - in: form + in: formData required: true type: string - name: code description: The code provided by the authorization redirect - in: form + in: formData required: true type: string - name: redirect_uri description: The redirect url - in: form + in: formData required: true type: string responses: @@ -668,14 +715,28 @@ paths: post: summary: Shutdown the Hub parameters: - - name: proxy + - name: body in: body - type: boolean - description: Whether the proxy should be shutdown as well (default from Hub config) - - name: servers - in: body - type: boolean - description: Whether users' notebook servers should be shutdown as well (default from Hub config) + schema: + type: object + properties: + proxy: + type: boolean + description: Whether the proxy should be shutdown as well (default from Hub config) + servers: + type: boolean + description: Whether users' notebook servers should be shutdown as well (default from Hub config) + responses: + '202': + description: Shutdown successful + '400': + description: Unexpeced value for proxy or servers +# Descriptions of common responses +responses: + NotFound: + description: The specified resource was not found + Unauthorized: + description: Authentication/Authorization error definitions: User: type: object @@ -703,11 +764,10 @@ definitions: format: date-time description: Timestamp of last-seen activity from the user servers: - type: object + type: array description: The active servers for this user. items: - schema: - $ref: '#/definitions/Server' + $ref: '#/definitions/Server' Server: type: object properties: @@ -745,6 +805,9 @@ definitions: state: type: object description: Arbitrary internal state from this server's spawner. Only available on the hub's users list or get-user-by-name method, and only if a hub admin. None otherwise. + user_options: + type: object + description: User specified options for the user's spawned instance of a single-user server. Group: type: object properties: diff --git a/docs/source/_static/custom.css b/docs/source/_static/custom.css index 3c232768..e55a6a55 100644 --- a/docs/source/_static/custom.css +++ b/docs/source/_static/custom.css @@ -1,106 +1,4 @@ -div#helm-chart-schema h2, -div#helm-chart-schema h3, -div#helm-chart-schema h4, -div#helm-chart-schema h5, -div#helm-chart-schema h6 { - font-family: courier new; -} - -h3, h3 ~ * { - margin-left: 3% !important; -} - -h4, h4 ~ * { - margin-left: 6% !important; -} - -h5, h5 ~ * { - margin-left: 9% !important; -} - -h6, h6 ~ * { - margin-left: 12% !important; -} - -h7, h7 ~ * { - margin-left: 15% !important; -} - -img.logo { - width:100% -} - -.right-next { - float: right; - max-width: 45%; - overflow: auto; - text-overflow: ellipsis; - white-space: nowrap; -} - -.right-next::after{ - content: ' »'; -} - -.left-prev { - float: left; - max-width: 45%; - overflow: auto; - text-overflow: ellipsis; - white-space: nowrap; -} - -.left-prev::before{ - content: '« '; -} - -.prev-next-bottom { - margin-top: 3em; -} - -.prev-next-top { - margin-bottom: 1em; -} - -/* Sidebar TOC and headers */ - -div.sphinxsidebarwrapper div { - margin-bottom: .8em; -} -div.sphinxsidebar h3 { - font-size: 1.3em; - padding-top: 0px; - font-weight: 800; - margin-left: 0px !important; -} - -div.sphinxsidebar p.caption { - font-size: 1.2em; - margin-bottom: 0px; - margin-left: 0px !important; - font-weight: 900; - color: #767676; -} - -div.sphinxsidebar ul { - font-size: .8em; - margin-top: 0px; - padding-left: 3%; - margin-left: 0px !important; -} - -div.relations ul { - font-size: 1em; - margin-left: 0px !important; -} - -div#searchbox form { - margin-left: 0px !important; -} - -/* body elements */ -.toctree-wrapper span.caption-text { - color: #767676; - font-style: italic; - font-weight: 300; +/* Added to avoid logo being too squeezed */ +.navbar-brand { + height: 4rem !important; } diff --git a/docs/source/_static/images/logo/logo.png b/docs/source/_static/images/logo/logo.png index 7cdfc55b..a7a4e61b 100644 Binary files a/docs/source/_static/images/logo/logo.png and b/docs/source/_static/images/logo/logo.png differ diff --git a/docs/source/_templates/navigation.html b/docs/source/_templates/navigation.html deleted file mode 100644 index 9fa52ff2..00000000 --- a/docs/source/_templates/navigation.html +++ /dev/null @@ -1,16 +0,0 @@ -{# Custom template for navigation.html - - alabaster theme does not provide blocks for titles to - be overridden so this custom theme handles title and - toctree for sidebar -#} -

{{ _('Table of Contents') }}

-{{ toctree(includehidden=theme_sidebar_includehidden, collapse=theme_sidebar_collapse) }} -{% if theme_extra_nav_links %} -
-
    - {% for text, uri in theme_extra_nav_links.items() %} -
  • {{ text }}
  • - {% endfor %} -
-{% endif %} diff --git a/docs/source/_templates/page.html b/docs/source/_templates/page.html deleted file mode 100644 index 8df1d7ec..00000000 --- a/docs/source/_templates/page.html +++ /dev/null @@ -1,30 +0,0 @@ -{% extends '!page.html' %} - -{# Custom template for page.html - - Alabaster theme does not provide blocks for prev/next at bottom of each page. - This is _in addition_ to the prev/next in the sidebar. The "Prev/Next" text - or symbols are handled by CSS classes in _static/custom.css -#} - -{% macro prev_next(prev, next, prev_title='', next_title='') %} - {%- if prev %} - {{ prev_title or prev.title }} - {%- endif %} - {%- if next %} - {{ next_title or next.title }} - {%- endif %} -
-{% endmacro %} - - -{% block body %} -
- {{ prev_next(prev, next, 'Previous', 'Next') }} -
- - {{super()}} -
- {{ prev_next(prev, next) }} -
-{% endblock %} diff --git a/docs/source/_templates/relations.html b/docs/source/_templates/relations.html deleted file mode 100644 index d7ace383..00000000 --- a/docs/source/_templates/relations.html +++ /dev/null @@ -1,17 +0,0 @@ -{# Custom template for relations.html - - alabaster theme does not provide previous/next page by default -#} -
-

Navigation

- -
diff --git a/docs/source/admin/upgrading.rst b/docs/source/admin/upgrading.rst index 18404955..874aae97 100644 --- a/docs/source/admin/upgrading.rst +++ b/docs/source/admin/upgrading.rst @@ -18,7 +18,7 @@ JupyterHub is painless, quick and with minimal user interruption. Read the Changelog ================== -The `changelog `_ contains information on what has +The `changelog <../changelog.html>`_ contains information on what has changed with the new JupyterHub release, and any deprecation warnings. Read these notes to familiarize yourself with the coming changes. There might be new releases of authenticators & spawners you are using, so diff --git a/docs/source/api/index.rst b/docs/source/api/index.rst index 7ce11d2a..2650b5a8 100644 --- a/docs/source/api/index.rst +++ b/docs/source/api/index.rst @@ -1,8 +1,8 @@ .. _api-index: -################## -The JupyterHub API -################## +############## +JupyterHub API +############## :Release: |release| :Date: |today| diff --git a/docs/source/changelog.md b/docs/source/changelog.md index 4cd14dbd..637d2c83 100644 --- a/docs/source/changelog.md +++ b/docs/source/changelog.md @@ -7,6 +7,310 @@ command line for details. ## [Unreleased] +## 1.2 + +### [1.2.1] 2020-10-30 + +([full changelog](https://github.com/jupyterhub/jupyterhub/compare/1.2.0...1.2.1)) + +#### Bugs fixed + +- JupyterHub services' oauth_no_confirm configuration regression in 1.2.0 [#3234](https://github.com/jupyterhub/jupyterhub/pull/3234) ([@bitnik](https://github.com/bitnik)) + +#### Contributors to this release + +([GitHub contributors page for this release](https://github.com/jupyterhub/jupyterhub/graphs/contributors?from=2020-10-29&to=2020-10-30&type=c)) + +[@bitnik](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Abitnik+updated%3A2020-10-29..2020-10-30&type=Issues) + +### [1.2.0] 2020-10-29 + +JupyterHub 1.2 is an incremental release with lots of small improvements. +It is unlikely that users will have to change much to upgrade, +but lots of new things are possible and/or better! + +There are no database schema changes requiring migration from 1.1 to 1.2. + +Highlights: + +- Deprecate black/whitelist configuration fields in favor of more inclusive blocked/allowed language. For example: `c.Authenticator.allowed_users = {'user', ...}` +- More configuration of page templates and service display +- Pagination of the admin page improving performance with large numbers of users +- Improved control of user redirect +- Support for [jupyter-server](https://jupyter-server.readthedocs.io/en/latest/)-based single-user servers, such as [Voilà](https://voila-gallery.org) and latest JupyterLab. +- Lots more improvements to documentation, HTML pages, and customizations + +([full changelog](https://github.com/jupyterhub/jupyterhub/compare/1.1.0...1.2.0)) + + +#### Enhancements made +* make pagination configurable [#3229](https://github.com/jupyterhub/jupyterhub/pull/3229) ([@minrk](https://github.com/minrk)) +* Make api_request to CHP's REST API more reliable [#3223](https://github.com/jupyterhub/jupyterhub/pull/3223) ([@consideRatio](https://github.com/consideRatio)) +* Control service display [#3160](https://github.com/jupyterhub/jupyterhub/pull/3160) ([@rcthomas](https://github.com/rcthomas)) +* Add a footer block + wrap the admin footer in this block [#3136](https://github.com/jupyterhub/jupyterhub/pull/3136) ([@pabepadu](https://github.com/pabepadu)) +* Allow JupyterHub.default_url to be a callable [#3133](https://github.com/jupyterhub/jupyterhub/pull/3133) ([@danlester](https://github.com/danlester)) +* Allow head requests for the health endpoint [#3131](https://github.com/jupyterhub/jupyterhub/pull/3131) ([@rkevin-arch](https://github.com/rkevin-arch)) +* Hide hamburger button menu in mobile/responsive mode and fix other minor issues [#3103](https://github.com/jupyterhub/jupyterhub/pull/3103) ([@kinow](https://github.com/kinow)) +* build jupyterhub/jupyterhub-demo image on docker hub [#3083](https://github.com/jupyterhub/jupyterhub/pull/3083) ([@minrk](https://github.com/minrk)) +* Add JupyterHub Demo docker image [#3059](https://github.com/jupyterhub/jupyterhub/pull/3059) ([@GeorgianaElena](https://github.com/GeorgianaElena)) +* Warn if both bind_url and ip/port/base_url are set [#3057](https://github.com/jupyterhub/jupyterhub/pull/3057) ([@GeorgianaElena](https://github.com/GeorgianaElena)) +* UI Feedback on Submit [#3028](https://github.com/jupyterhub/jupyterhub/pull/3028) ([@possiblyMikeB](https://github.com/possiblyMikeB)) +* Support kubespawner running on a IPv6 only cluster [#3020](https://github.com/jupyterhub/jupyterhub/pull/3020) ([@stv0g](https://github.com/stv0g)) +* Spawn with options passed in query arguments to /spawn [#3013](https://github.com/jupyterhub/jupyterhub/pull/3013) ([@twalcari](https://github.com/twalcari)) +* SpawnHandler POST with user form options displays the spawn-pending page [#2978](https://github.com/jupyterhub/jupyterhub/pull/2978) ([@danlester](https://github.com/danlester)) +* Start named servers by pressing the Enter key [#2960](https://github.com/jupyterhub/jupyterhub/pull/2960) ([@jtpio](https://github.com/jtpio)) +* Keep the URL fragments after spawning an application [#2952](https://github.com/jupyterhub/jupyterhub/pull/2952) ([@kinow](https://github.com/kinow)) +* Allow implicit spawn via javascript redirect [#2941](https://github.com/jupyterhub/jupyterhub/pull/2941) ([@minrk](https://github.com/minrk)) +* make init_spawners check O(running servers) not O(total users) [#2936](https://github.com/jupyterhub/jupyterhub/pull/2936) ([@minrk](https://github.com/minrk)) +* Add favicon to the base page template [#2930](https://github.com/jupyterhub/jupyterhub/pull/2930) ([@JohnPaton](https://github.com/JohnPaton)) +* Adding pagination in the admin panel [#2929](https://github.com/jupyterhub/jupyterhub/pull/2929) ([@cbjuan](https://github.com/cbjuan)) +* Generate prometheus metrics docs [#2891](https://github.com/jupyterhub/jupyterhub/pull/2891) ([@rajat404](https://github.com/rajat404)) +* Add support for Jupyter Server [#2601](https://github.com/jupyterhub/jupyterhub/pull/2601) ([@yuvipanda](https://github.com/yuvipanda)) + +#### Bugs fixed +* Fix #2284 must be sent from authorization page [#3219](https://github.com/jupyterhub/jupyterhub/pull/3219) ([@elgalu](https://github.com/elgalu)) +* avoid specifying default_value=None in Command traits [#3208](https://github.com/jupyterhub/jupyterhub/pull/3208) ([@minrk](https://github.com/minrk)) +* Prevent OverflowErrors in exponential_backoff() [#3204](https://github.com/jupyterhub/jupyterhub/pull/3204) ([@kreuzert](https://github.com/kreuzert)) +* update prometheus metrics for server spawn when it fails with exception [#3150](https://github.com/jupyterhub/jupyterhub/pull/3150) ([@yhal-nesi](https://github.com/yhal-nesi)) +* jupyterhub/utils: Load system default CA certificates in make_ssl_context [#3140](https://github.com/jupyterhub/jupyterhub/pull/3140) ([@chancez](https://github.com/chancez)) +* admin page sorts on spawner last_activity instead of user last_activity [#3137](https://github.com/jupyterhub/jupyterhub/pull/3137) ([@lydian](https://github.com/lydian)) +* Fix the services dropdown on the admin page [#3132](https://github.com/jupyterhub/jupyterhub/pull/3132) ([@pabepadu](https://github.com/pabepadu)) +* Don't log a warning when slow_spawn_timeout is disabled [#3127](https://github.com/jupyterhub/jupyterhub/pull/3127) ([@mriedem](https://github.com/mriedem)) +* app.py: Work around incompatibility between Tornado 6 and asyncio proactor event loop in python 3.8 on Windows [#3123](https://github.com/jupyterhub/jupyterhub/pull/3123) ([@alexweav](https://github.com/alexweav)) +* jupyterhub/user: clear spawner state after post_stop_hook [#3121](https://github.com/jupyterhub/jupyterhub/pull/3121) ([@rkdarst](https://github.com/rkdarst)) +* fix for stopping named server deleting default server and tests [#3109](https://github.com/jupyterhub/jupyterhub/pull/3109) ([@kxiao-fn](https://github.com/kxiao-fn)) +* Hide hamburger button menu in mobile/responsive mode and fix other minor issues [#3103](https://github.com/jupyterhub/jupyterhub/pull/3103) ([@kinow](https://github.com/kinow)) +* Rename Authenticator.white/blacklist to allowed/blocked [#3090](https://github.com/jupyterhub/jupyterhub/pull/3090) ([@minrk](https://github.com/minrk)) +* Include the query string parameters when redirecting to a new URL [#3089](https://github.com/jupyterhub/jupyterhub/pull/3089) ([@kinow](https://github.com/kinow)) +* Make `delete_invalid_users` configurable [#3087](https://github.com/jupyterhub/jupyterhub/pull/3087) ([@fcollonval](https://github.com/fcollonval)) +* Ensure client dependencies build before wheel [#3082](https://github.com/jupyterhub/jupyterhub/pull/3082) ([@diurnalist](https://github.com/diurnalist)) +* make Spawner.environment config highest priority [#3081](https://github.com/jupyterhub/jupyterhub/pull/3081) ([@minrk](https://github.com/minrk)) +* Changing start my server button link to spawn url once server is stopped [#3042](https://github.com/jupyterhub/jupyterhub/pull/3042) ([@rabsr](https://github.com/rabsr)) +* Fix CSS on admin page version listing [#3035](https://github.com/jupyterhub/jupyterhub/pull/3035) ([@vilhelmen](https://github.com/vilhelmen)) +* Fix user_row endblock in admin template [#3015](https://github.com/jupyterhub/jupyterhub/pull/3015) ([@jtpio](https://github.com/jtpio)) +* Fix --generate-config bug when specifying a filename [#2907](https://github.com/jupyterhub/jupyterhub/pull/2907) ([@consideRatio](https://github.com/consideRatio)) +* Handle the protocol when ssl is enabled and log the right URL [#2773](https://github.com/jupyterhub/jupyterhub/pull/2773) ([@kinow](https://github.com/kinow)) + +#### Maintenance and upkeep improvements +* Update travis-ci badge in README.md [#3232](https://github.com/jupyterhub/jupyterhub/pull/3232) ([@consideRatio](https://github.com/consideRatio)) +* stop building docs on circleci [#3209](https://github.com/jupyterhub/jupyterhub/pull/3209) ([@minrk](https://github.com/minrk)) +* Upgraded Jquery dep [#3174](https://github.com/jupyterhub/jupyterhub/pull/3174) ([@AngelOnFira](https://github.com/AngelOnFira)) +* Don't allow 'python:3.8 + master dependencies' to fail [#3157](https://github.com/jupyterhub/jupyterhub/pull/3157) ([@manics](https://github.com/manics)) +* Update Dockerfile to ubuntu:focal (Python 3.8) [#3156](https://github.com/jupyterhub/jupyterhub/pull/3156) ([@manics](https://github.com/manics)) +* Simplify code of the health check handler [#3149](https://github.com/jupyterhub/jupyterhub/pull/3149) ([@betatim](https://github.com/betatim)) +* Get error description from error key vs error_description key [#3147](https://github.com/jupyterhub/jupyterhub/pull/3147) ([@jgwerner](https://github.com/jgwerner)) +* Implement singleuser with mixins [#3128](https://github.com/jupyterhub/jupyterhub/pull/3128) ([@minrk](https://github.com/minrk)) +* only build tagged versions on docker tags [#3118](https://github.com/jupyterhub/jupyterhub/pull/3118) ([@minrk](https://github.com/minrk)) +* Log slow_stop_timeout when hit like slow_spawn_timeout [#3111](https://github.com/jupyterhub/jupyterhub/pull/3111) ([@mriedem](https://github.com/mriedem)) +* loosen jupyter-telemetry pin [#3102](https://github.com/jupyterhub/jupyterhub/pull/3102) ([@minrk](https://github.com/minrk)) +* Remove old context-less print statement [#3100](https://github.com/jupyterhub/jupyterhub/pull/3100) ([@mriedem](https://github.com/mriedem)) +* Allow `python:3.8 + master dependencies` to fail [#3079](https://github.com/jupyterhub/jupyterhub/pull/3079) ([@manics](https://github.com/manics)) +* Test with some master dependencies. [#3076](https://github.com/jupyterhub/jupyterhub/pull/3076) ([@Carreau](https://github.com/Carreau)) +* synchronize implementation of expiring values [#3072](https://github.com/jupyterhub/jupyterhub/pull/3072) ([@minrk](https://github.com/minrk)) +* More consistent behavior for UserDict.get and `key in UserDict` [#3071](https://github.com/jupyterhub/jupyterhub/pull/3071) ([@minrk](https://github.com/minrk)) +* pin jupyter_telemetry dependency [#3067](https://github.com/jupyterhub/jupyterhub/pull/3067) ([@Zsailer](https://github.com/Zsailer)) +* Use the issue templates from the central repo [#3056](https://github.com/jupyterhub/jupyterhub/pull/3056) ([@GeorgianaElena](https://github.com/GeorgianaElena)) +* Update links to the black GitHub repository [#3054](https://github.com/jupyterhub/jupyterhub/pull/3054) ([@jtpio](https://github.com/jtpio)) +* Log successful /health requests as debug level [#3047](https://github.com/jupyterhub/jupyterhub/pull/3047) ([@consideRatio](https://github.com/consideRatio)) +* Fix broken test due to BeautifulSoup 4.9.0 behavior change [#3025](https://github.com/jupyterhub/jupyterhub/pull/3025) ([@twalcari](https://github.com/twalcari)) +* Remove unused imports [#3019](https://github.com/jupyterhub/jupyterhub/pull/3019) ([@stv0g](https://github.com/stv0g)) +* Use pip instead of conda for building the docs on RTD [#3010](https://github.com/jupyterhub/jupyterhub/pull/3010) ([@GeorgianaElena](https://github.com/GeorgianaElena)) +* Avoid redundant logging of jupyterhub version mismatches [#2971](https://github.com/jupyterhub/jupyterhub/pull/2971) ([@mriedem](https://github.com/mriedem)) +* Add .vscode to gitignore [#2959](https://github.com/jupyterhub/jupyterhub/pull/2959) ([@jtpio](https://github.com/jtpio)) +* preserve auth type when logging obfuscated auth header [#2953](https://github.com/jupyterhub/jupyterhub/pull/2953) ([@minrk](https://github.com/minrk)) +* make spawner:server relationship explicitly one to one [#2944](https://github.com/jupyterhub/jupyterhub/pull/2944) ([@minrk](https://github.com/minrk)) +* Add what we need with some margin to Dockerfile's build stage [#2905](https://github.com/jupyterhub/jupyterhub/pull/2905) ([@consideRatio](https://github.com/consideRatio)) +* bump reorder-imports hook [#2899](https://github.com/jupyterhub/jupyterhub/pull/2899) ([@minrk](https://github.com/minrk)) + +#### Documentation improvements +* Fix typo in documentation [#3226](https://github.com/jupyterhub/jupyterhub/pull/3226) ([@xlotlu](https://github.com/xlotlu)) +* [docs] Remove duplicate line in changelog for 1.1.0 [#3207](https://github.com/jupyterhub/jupyterhub/pull/3207) ([@kinow](https://github.com/kinow)) +* changelog for 1.2.0b1 [#3192](https://github.com/jupyterhub/jupyterhub/pull/3192) ([@consideRatio](https://github.com/consideRatio)) +* Add SELinux configuration for nginx [#3185](https://github.com/jupyterhub/jupyterhub/pull/3185) ([@rainwoodman](https://github.com/rainwoodman)) +* Mention the PAM pitfall on fedora. [#3184](https://github.com/jupyterhub/jupyterhub/pull/3184) ([@rainwoodman](https://github.com/rainwoodman)) +* Added extra documentation for endpoint /users/{name}/servers/{server_name}. [#3159](https://github.com/jupyterhub/jupyterhub/pull/3159) ([@synchronizing](https://github.com/synchronizing)) +* docs: please docs linter (move_cert docstring) [#3151](https://github.com/jupyterhub/jupyterhub/pull/3151) ([@consideRatio](https://github.com/consideRatio)) +* Needed NoEsacpe (NE) option for apache [#3143](https://github.com/jupyterhub/jupyterhub/pull/3143) ([@basvandervlies](https://github.com/basvandervlies)) +* Document external service api_tokens better [#3142](https://github.com/jupyterhub/jupyterhub/pull/3142) ([@snickell](https://github.com/snickell)) +* Remove idle culler example [#3114](https://github.com/jupyterhub/jupyterhub/pull/3114) ([@yuvipanda](https://github.com/yuvipanda)) +* docs: unsqueeze logo, remove unused CSS and templates [#3107](https://github.com/jupyterhub/jupyterhub/pull/3107) ([@consideRatio](https://github.com/consideRatio)) +* Update version in docs/rest-api.yaml [#3104](https://github.com/jupyterhub/jupyterhub/pull/3104) ([@cmd-ntrf](https://github.com/cmd-ntrf)) +* Replace zonca/remotespawner with NERSC/sshspawner [#3086](https://github.com/jupyterhub/jupyterhub/pull/3086) ([@manics](https://github.com/manics)) +* Remove already done named servers from roadmap [#3084](https://github.com/jupyterhub/jupyterhub/pull/3084) ([@elgalu](https://github.com/elgalu)) +* proxy settings might cause authentication errors [#3078](https://github.com/jupyterhub/jupyterhub/pull/3078) ([@gatoniel](https://github.com/gatoniel)) +* Add Configuration Reference section to docs [#3077](https://github.com/jupyterhub/jupyterhub/pull/3077) ([@kinow](https://github.com/kinow)) +* document upgrading from api_tokens to services config [#3055](https://github.com/jupyterhub/jupyterhub/pull/3055) ([@minrk](https://github.com/minrk)) +* [Docs] Disable proxy_buffering when using nginx reverse proxy [#3048](https://github.com/jupyterhub/jupyterhub/pull/3048) ([@mhwasil](https://github.com/mhwasil)) +* docs: add proxy_http_version 1.1 [#3046](https://github.com/jupyterhub/jupyterhub/pull/3046) ([@ceocoder](https://github.com/ceocoder)) +* #1018 PAM added in prerequisites [#3040](https://github.com/jupyterhub/jupyterhub/pull/3040) ([@romainx](https://github.com/romainx)) +* Fix use of auxiliary verb on index.rst [#3022](https://github.com/jupyterhub/jupyterhub/pull/3022) ([@joshmeek](https://github.com/joshmeek)) +* Fix docs CI test failure: duplicate object description [#3021](https://github.com/jupyterhub/jupyterhub/pull/3021) ([@rkdarst](https://github.com/rkdarst)) +* Update issue templates [#3001](https://github.com/jupyterhub/jupyterhub/pull/3001) ([@GeorgianaElena](https://github.com/GeorgianaElena)) +* fix wrong name on firewall [#2997](https://github.com/jupyterhub/jupyterhub/pull/2997) ([@thuvh](https://github.com/thuvh)) +* updating docs theme [#2995](https://github.com/jupyterhub/jupyterhub/pull/2995) ([@choldgraf](https://github.com/choldgraf)) +* Update contributor docs [#2972](https://github.com/jupyterhub/jupyterhub/pull/2972) ([@mriedem](https://github.com/mriedem)) +* Server.user_options rest-api documented [#2966](https://github.com/jupyterhub/jupyterhub/pull/2966) ([@mriedem](https://github.com/mriedem)) +* Pin sphinx theme [#2956](https://github.com/jupyterhub/jupyterhub/pull/2956) ([@manics](https://github.com/manics)) +* [doc] Fix couple typos in the documentation [#2951](https://github.com/jupyterhub/jupyterhub/pull/2951) ([@kinow](https://github.com/kinow)) +* Docs: Fixed grammar on landing page [#2950](https://github.com/jupyterhub/jupyterhub/pull/2950) ([@alexdriedger](https://github.com/alexdriedger)) +* add general faq [#2946](https://github.com/jupyterhub/jupyterhub/pull/2946) ([@minrk](https://github.com/minrk)) +* docs: use metachannel for faster environment solve [#2943](https://github.com/jupyterhub/jupyterhub/pull/2943) ([@minrk](https://github.com/minrk)) +* update docs environments [#2942](https://github.com/jupyterhub/jupyterhub/pull/2942) ([@minrk](https://github.com/minrk)) +* [doc] Add more docs about Cookies used for authentication in JupyterHub [#2940](https://github.com/jupyterhub/jupyterhub/pull/2940) ([@kinow](https://github.com/kinow)) +* [doc] Use fixed commit plus line number in github link [#2939](https://github.com/jupyterhub/jupyterhub/pull/2939) ([@kinow](https://github.com/kinow)) +* [doc] Fix link to SSL encryption from troubleshooting page [#2938](https://github.com/jupyterhub/jupyterhub/pull/2938) ([@kinow](https://github.com/kinow)) +* rest api: fix schema for remove parameter in rest api [#2917](https://github.com/jupyterhub/jupyterhub/pull/2917) ([@minrk](https://github.com/minrk)) +* Add troubleshooting topics [#2914](https://github.com/jupyterhub/jupyterhub/pull/2914) ([@jgwerner](https://github.com/jgwerner)) +* Several fixes to the doc [#2904](https://github.com/jupyterhub/jupyterhub/pull/2904) ([@reneluria](https://github.com/reneluria)) +* fix: 'Non-ASCII character '\xc3' [#2901](https://github.com/jupyterhub/jupyterhub/pull/2901) ([@jgwerner](https://github.com/jgwerner)) +* Generate prometheus metrics docs [#2891](https://github.com/jupyterhub/jupyterhub/pull/2891) ([@rajat404](https://github.com/rajat404)) + +#### Contributors to this release +([GitHub contributors page for this release](https://github.com/jupyterhub/jupyterhub/graphs/contributors?from=2020-01-17&to=2020-10-29&type=c)) + +[@0nebody](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3A0nebody+updated%3A2020-01-17..2020-10-29&type=Issues) | [@1kastner](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3A1kastner+updated%3A2020-01-17..2020-10-29&type=Issues) | [@ahkui](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Aahkui+updated%3A2020-01-17..2020-10-29&type=Issues) | [@alexdriedger](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Aalexdriedger+updated%3A2020-01-17..2020-10-29&type=Issues) | [@alexweav](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Aalexweav+updated%3A2020-01-17..2020-10-29&type=Issues) | [@AlJohri](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3AAlJohri+updated%3A2020-01-17..2020-10-29&type=Issues) | [@Analect](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3AAnalect+updated%3A2020-01-17..2020-10-29&type=Issues) | [@analytically](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Aanalytically+updated%3A2020-01-17..2020-10-29&type=Issues) | [@aneagoe](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Aaneagoe+updated%3A2020-01-17..2020-10-29&type=Issues) | [@AngelOnFira](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3AAngelOnFira+updated%3A2020-01-17..2020-10-29&type=Issues) | [@barrachri](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Abarrachri+updated%3A2020-01-17..2020-10-29&type=Issues) | [@basvandervlies](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Abasvandervlies+updated%3A2020-01-17..2020-10-29&type=Issues) | [@betatim](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Abetatim+updated%3A2020-01-17..2020-10-29&type=Issues) | [@bigbosst](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Abigbosst+updated%3A2020-01-17..2020-10-29&type=Issues) | [@blink1073](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Ablink1073+updated%3A2020-01-17..2020-10-29&type=Issues) | [@Cadair](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3ACadair+updated%3A2020-01-17..2020-10-29&type=Issues) | [@Carreau](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3ACarreau+updated%3A2020-01-17..2020-10-29&type=Issues) | [@cbjuan](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Acbjuan+updated%3A2020-01-17..2020-10-29&type=Issues) | [@ceocoder](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Aceocoder+updated%3A2020-01-17..2020-10-29&type=Issues) | [@chancez](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Achancez+updated%3A2020-01-17..2020-10-29&type=Issues) | [@choldgraf](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Acholdgraf+updated%3A2020-01-17..2020-10-29&type=Issues) | [@Chrisjw42](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3AChrisjw42+updated%3A2020-01-17..2020-10-29&type=Issues) | [@cmd-ntrf](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Acmd-ntrf+updated%3A2020-01-17..2020-10-29&type=Issues) | [@consideRatio](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3AconsideRatio+updated%3A2020-01-17..2020-10-29&type=Issues) | [@danlester](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Adanlester+updated%3A2020-01-17..2020-10-29&type=Issues) | [@diurnalist](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Adiurnalist+updated%3A2020-01-17..2020-10-29&type=Issues) | [@Dmitry1987](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3ADmitry1987+updated%3A2020-01-17..2020-10-29&type=Issues) | [@dsblank](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Adsblank+updated%3A2020-01-17..2020-10-29&type=Issues) | [@dylex](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Adylex+updated%3A2020-01-17..2020-10-29&type=Issues) | [@echarles](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Aecharles+updated%3A2020-01-17..2020-10-29&type=Issues) | [@elgalu](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Aelgalu+updated%3A2020-01-17..2020-10-29&type=Issues) | [@fcollonval](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Afcollonval+updated%3A2020-01-17..2020-10-29&type=Issues) | [@gatoniel](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Agatoniel+updated%3A2020-01-17..2020-10-29&type=Issues) | [@GeorgianaElena](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3AGeorgianaElena+updated%3A2020-01-17..2020-10-29&type=Issues) | [@hnykda](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Ahnykda+updated%3A2020-01-17..2020-10-29&type=Issues) | [@itssimon](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Aitssimon+updated%3A2020-01-17..2020-10-29&type=Issues) | [@jgwerner](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Ajgwerner+updated%3A2020-01-17..2020-10-29&type=Issues) | [@JohnPaton](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3AJohnPaton+updated%3A2020-01-17..2020-10-29&type=Issues) | [@joshmeek](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Ajoshmeek+updated%3A2020-01-17..2020-10-29&type=Issues) | [@jtpio](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Ajtpio+updated%3A2020-01-17..2020-10-29&type=Issues) | [@kinow](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Akinow+updated%3A2020-01-17..2020-10-29&type=Issues) | [@kreuzert](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Akreuzert+updated%3A2020-01-17..2020-10-29&type=Issues) | [@kxiao-fn](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Akxiao-fn+updated%3A2020-01-17..2020-10-29&type=Issues) | [@lesiano](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Alesiano+updated%3A2020-01-17..2020-10-29&type=Issues) | [@limimiking](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Alimimiking+updated%3A2020-01-17..2020-10-29&type=Issues) | [@lydian](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Alydian+updated%3A2020-01-17..2020-10-29&type=Issues) | [@mabbasi90](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Amabbasi90+updated%3A2020-01-17..2020-10-29&type=Issues) | [@maluhoss](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Amaluhoss+updated%3A2020-01-17..2020-10-29&type=Issues) | [@manics](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Amanics+updated%3A2020-01-17..2020-10-29&type=Issues) | [@matteoipri](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Amatteoipri+updated%3A2020-01-17..2020-10-29&type=Issues) | [@mbmilligan](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Ambmilligan+updated%3A2020-01-17..2020-10-29&type=Issues) | [@meeseeksmachine](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Ameeseeksmachine+updated%3A2020-01-17..2020-10-29&type=Issues) | [@mhwasil](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Amhwasil+updated%3A2020-01-17..2020-10-29&type=Issues) | [@minrk](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Aminrk+updated%3A2020-01-17..2020-10-29&type=Issues) | [@mriedem](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Amriedem+updated%3A2020-01-17..2020-10-29&type=Issues) | [@nscozzaro](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Anscozzaro+updated%3A2020-01-17..2020-10-29&type=Issues) | [@pabepadu](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Apabepadu+updated%3A2020-01-17..2020-10-29&type=Issues) | [@possiblyMikeB](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3ApossiblyMikeB+updated%3A2020-01-17..2020-10-29&type=Issues) | [@psyvision](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Apsyvision+updated%3A2020-01-17..2020-10-29&type=Issues) | [@rabsr](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Arabsr+updated%3A2020-01-17..2020-10-29&type=Issues) | [@rainwoodman](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Arainwoodman+updated%3A2020-01-17..2020-10-29&type=Issues) | [@rajat404](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Arajat404+updated%3A2020-01-17..2020-10-29&type=Issues) | [@rcthomas](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Arcthomas+updated%3A2020-01-17..2020-10-29&type=Issues) | [@reneluria](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Areneluria+updated%3A2020-01-17..2020-10-29&type=Issues) | [@rgbkrk](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Argbkrk+updated%3A2020-01-17..2020-10-29&type=Issues) | [@rkdarst](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Arkdarst+updated%3A2020-01-17..2020-10-29&type=Issues) | [@rkevin-arch](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Arkevin-arch+updated%3A2020-01-17..2020-10-29&type=Issues) | [@romainx](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Aromainx+updated%3A2020-01-17..2020-10-29&type=Issues) | [@ryanlovett](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Aryanlovett+updated%3A2020-01-17..2020-10-29&type=Issues) | [@ryogesh](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Aryogesh+updated%3A2020-01-17..2020-10-29&type=Issues) | [@sdague](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Asdague+updated%3A2020-01-17..2020-10-29&type=Issues) | [@snickell](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Asnickell+updated%3A2020-01-17..2020-10-29&type=Issues) | [@SonakshiGrover](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3ASonakshiGrover+updated%3A2020-01-17..2020-10-29&type=Issues) | [@ssanderson](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Assanderson+updated%3A2020-01-17..2020-10-29&type=Issues) | [@stefanvangastel](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Astefanvangastel+updated%3A2020-01-17..2020-10-29&type=Issues) | [@steinad](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Asteinad+updated%3A2020-01-17..2020-10-29&type=Issues) | [@stephen-a2z](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Astephen-a2z+updated%3A2020-01-17..2020-10-29&type=Issues) | [@stevegore](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Astevegore+updated%3A2020-01-17..2020-10-29&type=Issues) | [@stv0g](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Astv0g+updated%3A2020-01-17..2020-10-29&type=Issues) | [@subgero](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Asubgero+updated%3A2020-01-17..2020-10-29&type=Issues) | [@sudi007](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Asudi007+updated%3A2020-01-17..2020-10-29&type=Issues) | [@summerswallow](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Asummerswallow+updated%3A2020-01-17..2020-10-29&type=Issues) | [@support](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Asupport+updated%3A2020-01-17..2020-10-29&type=Issues) | [@synchronizing](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Asynchronizing+updated%3A2020-01-17..2020-10-29&type=Issues) | [@thuvh](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Athuvh+updated%3A2020-01-17..2020-10-29&type=Issues) | [@tritemio](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Atritemio+updated%3A2020-01-17..2020-10-29&type=Issues) | [@twalcari](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Atwalcari+updated%3A2020-01-17..2020-10-29&type=Issues) | [@vchandvankar](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Avchandvankar+updated%3A2020-01-17..2020-10-29&type=Issues) | [@vilhelmen](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Avilhelmen+updated%3A2020-01-17..2020-10-29&type=Issues) | [@vlizanae](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Avlizanae+updated%3A2020-01-17..2020-10-29&type=Issues) | [@weimin](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Aweimin+updated%3A2020-01-17..2020-10-29&type=Issues) | [@welcome](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Awelcome+updated%3A2020-01-17..2020-10-29&type=Issues) | [@willingc](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Awillingc+updated%3A2020-01-17..2020-10-29&type=Issues) | [@xlotlu](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Axlotlu+updated%3A2020-01-17..2020-10-29&type=Issues) | [@yhal-nesi](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Ayhal-nesi+updated%3A2020-01-17..2020-10-29&type=Issues) | [@ynnelson](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Aynnelson+updated%3A2020-01-17..2020-10-29&type=Issues) | [@yuvipanda](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Ayuvipanda+updated%3A2020-01-17..2020-10-29&type=Issues) | [@zonca](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3Azonca+updated%3A2020-01-17..2020-10-29&type=Issues) | [@Zsailer](https://github.com/search?q=repo%3Ajupyterhub%2Fjupyterhub+involves%3AZsailer+updated%3A2020-01-17..2020-10-29&type=Issues) + +## 1.1 + +### [1.1.0] 2020-01-17 + +1.1 is a release with lots of accumulated fixes and improvements, +especially in performance, metrics, and customization. +There are no database changes in 1.1, so no database upgrade is required +when upgrading from 1.0 to 1.1. + +Of particular interest to deployments with automatic health checking and/or large numbers of users is that the slow startup time +introduced in 1.0 by additional spawner validation can now be mitigated by `JupyterHub.init_spawners_timeout`, +allowing the Hub to become responsive before the spawners may have finished validating. + +Several new Prometheus metrics are added (and others fixed!) +to measure sources of common performance issues, +such as proxy interactions and startup. + +1.1 also begins adoption of the Jupyter telemetry project in JupyterHub, +See [The Jupyter Telemetry docs](https://jupyter-telemetry.readthedocs.io) +for more info. The only events so far are starting and stopping servers, +but more will be added in future releases. + +There are many more fixes and improvements listed below. +Thanks to everyone who has contributed to this release! + + +#### New + +- LocalProcessSpawner should work on windows by using psutil.pid_exists [#2882](https://github.com/jupyterhub/jupyterhub/pull/2882) ([@ociule](https://github.com/ociule)) +- trigger auth_state_hook prior to options form, add auth_state to template namespace [#2881](https://github.com/jupyterhub/jupyterhub/pull/2881) ([@minrk](https://github.com/minrk)) +- Added guide 'install jupyterlab the hard way' #2110 [#2842](https://github.com/jupyterhub/jupyterhub/pull/2842) ([@mangecoeur](https://github.com/mangecoeur)) +- Add prometheus metric to measure hub startup time [#2799](https://github.com/jupyterhub/jupyterhub/pull/2799) ([@rajat404](https://github.com/rajat404)) +- Add Spawner.auth_state_hook [#2555](https://github.com/jupyterhub/jupyterhub/pull/2555) ([@rcthomas](https://github.com/rcthomas)) +- Link services from jupyterhub pages [#2763](https://github.com/jupyterhub/jupyterhub/pull/2763) ([@rcthomas](https://github.com/rcthomas)) +- `JupyterHub.user_redirect_hook` is added to allow admins to customize /user-redirect/ behavior [#2790](https://github.com/jupyterhub/jupyterhub/pull/2790) ([@yuvipanda](https://github.com/yuvipanda)) +- Add prometheus metric to measure hub startup time [#2799](https://github.com/jupyterhub/jupyterhub/pull/2799) ([@rajat404](https://github.com/rajat404)) +- Add prometheus metric to measure proxy route poll times [#2798](https://github.com/jupyterhub/jupyterhub/pull/2798) ([@rajat404](https://github.com/rajat404)) +- `PROXY_DELETE_DURATION_SECONDS` prometheus metric is added, to measure proxy route deletion times [#2788](https://github.com/jupyterhub/jupyterhub/pull/2788) ([@rajat404](https://github.com/rajat404)) +- `Service.oauth_no_confirm` is added, it is useful for admin-managed services that are considered part of the Hub and shouldn't need to prompt the user for access [#2767](https://github.com/jupyterhub/jupyterhub/pull/2767) ([@minrk](https://github.com/minrk)) +- `JupyterHub.default_server_name` is added to make the default server be a named server with provided name [#2735](https://github.com/jupyterhub/jupyterhub/pull/2735) ([@krinsman](https://github.com/krinsman)) +- `JupyterHub.init_spawners_timeout` is introduced to combat slow startups on large JupyterHub deployments [#2721](https://github.com/jupyterhub/jupyterhub/pull/2721) ([@minrk](https://github.com/minrk)) +- The configuration `uids` for local authenticators is added to consistently assign users UNIX id's between installations [#2687](https://github.com/jupyterhub/jupyterhub/pull/2687) ([@rgerkin](https://github.com/rgerkin)) +- `JupyterHub.activity_resolution` is introduced with a default value of 30s improving performance by not updating the database with user activity too often [#2605](https://github.com/jupyterhub/jupyterhub/pull/2605) ([@minrk](https://github.com/minrk)) +- [HubAuth](https://jupyterhub.readthedocs.io/en/stable/api/services.auth.html#jupyterhub.services.auth.HubAuth)'s SSL configuration can now be set through environment variables [#2588](https://github.com/jupyterhub/jupyterhub/pull/2588) ([@cmd-ntrf](https://github.com/cmd-ntrf)) +- Expose spawner.user_options in REST API. [#2755](https://github.com/jupyterhub/jupyterhub/pull/2755) ([@danielballan](https://github.com/danielballan)) +- add block for scripts included in head [#2828](https://github.com/jupyterhub/jupyterhub/pull/2828) ([@bitnik](https://github.com/bitnik)) +- Instrument JupyterHub to record events with jupyter_telemetry [Part II] [#2698](https://github.com/jupyterhub/jupyterhub/pull/2698) ([@Zsailer](https://github.com/Zsailer)) +- Make announcements visible without custom HTML [#2570](https://github.com/jupyterhub/jupyterhub/pull/2570) ([@consideRatio](https://github.com/consideRatio)) +- Display server version on admin page [#2776](https://github.com/jupyterhub/jupyterhub/pull/2776) ([@vilhelmen](https://github.com/vilhelmen)) + +#### Fixes + +- Bugfix: pam_normalize_username didn't return username [#2876](https://github.com/jupyterhub/jupyterhub/pull/2876) ([@rkdarst](https://github.com/rkdarst)) +- Cleanup if spawner stop fails [#2849](https://github.com/jupyterhub/jupyterhub/pull/2849) ([@gabber12](https://github.com/gabber12)) +- Fix an issue occurring with the default spawner and `internal_ssl` enabled [#2785](https://github.com/jupyterhub/jupyterhub/pull/2785) ([@rpwagner](https://github.com/rpwagner)) +- Fix named servers to not be spawnable unless activated [#2772](https://github.com/jupyterhub/jupyterhub/pull/2772) ([@bitnik](https://github.com/bitnik)) +- JupyterHub now awaits proxy availability before accepting web requests [#2750](https://github.com/jupyterhub/jupyterhub/pull/2750) ([@minrk](https://github.com/minrk)) +- Fix a no longer valid assumption that MySQL and MariaDB need to have `innodb_file_format` and `innodb_large_prefix` configured [#2712](https://github.com/jupyterhub/jupyterhub/pull/2712) ([@chicocvenancio](https://github.com/chicocvenancio)) +- Login/Logout button now updates to Login on logout [#2705](https://github.com/jupyterhub/jupyterhub/pull/2705) ([@aar0nTw](https://github.com/aar0nTw)) +- Fix handling of exceptions within `pre_spawn_start` hooks [#2684](https://github.com/jupyterhub/jupyterhub/pull/2684) ([@GeorgianaElena](https://github.com/GeorgianaElena)) +- Fix an issue where a user could end up spawning a default server instead of a named server as intended [#2682](https://github.com/jupyterhub/jupyterhub/pull/2682) ([@rcthomas](https://github.com/rcthomas)) +- /hub/admin now redirects to login if unauthenticated [#2670](https://github.com/jupyterhub/jupyterhub/pull/2670) ([@GeorgianaElena](https://github.com/GeorgianaElena)) +- Fix spawning of users with names containing characters that needs to be escaped [#2648](https://github.com/jupyterhub/jupyterhub/pull/2648) ([@nicorikken](https://github.com/nicorikken)) +- Fix `TOTAL_USERS` prometheus metric [#2637](https://github.com/jupyterhub/jupyterhub/pull/2637) ([@GeorgianaElena](https://github.com/GeorgianaElena)) +- Fix `RUNNING_SERVERS` prometheus metric [#2629](https://github.com/jupyterhub/jupyterhub/pull/2629) ([@GeorgianaElena](https://github.com/GeorgianaElena)) +- Fix faulty redirects to 404 that could occur with the use of named servers [#2594](https://github.com/jupyterhub/jupyterhub/pull/2594) ([@vilhelmen](https://github.com/vilhelmen)) +- JupyterHub API spec is now a valid OpenAPI spec [#2590](https://github.com/jupyterhub/jupyterhub/pull/2590) ([@sbrunk](https://github.com/sbrunk)) +- Use of `--help` or `--version` previously could output unrelated errors [#2584](https://github.com/jupyterhub/jupyterhub/pull/2584) ([@minrk](https://github.com/minrk)) +- No longer crash on startup in Windows [#2560](https://github.com/jupyterhub/jupyterhub/pull/2560) ([@adelcast](https://github.com/adelcast)) +- Escape usernames in the frontend [#2640](https://github.com/jupyterhub/jupyterhub/pull/2640) ([@nicorikken](https://github.com/nicorikken)) + +#### Maintenance + + +- Optimize CI jobs and default to bionic [#2897](https://github.com/jupyterhub/jupyterhub/pull/2897) ([@consideRatio](https://github.com/consideRatio)) +- catch connection error for ssl failures [#2889](https://github.com/jupyterhub/jupyterhub/pull/2889) ([@minrk](https://github.com/minrk)) +- Fix implementation of default server name [#2887](https://github.com/jupyterhub/jupyterhub/pull/2887) ([@krinsman](https://github.com/krinsman)) +- fixup allow_failures [#2880](https://github.com/jupyterhub/jupyterhub/pull/2880) ([@minrk](https://github.com/minrk)) +- Pass tests on Python 3.8 [#2879](https://github.com/jupyterhub/jupyterhub/pull/2879) ([@minrk](https://github.com/minrk)) +- Fixup .travis.yml [#2868](https://github.com/jupyterhub/jupyterhub/pull/2868) ([@consideRatio](https://github.com/consideRatio)) +- Update README's badges [#2867](https://github.com/jupyterhub/jupyterhub/pull/2867) ([@consideRatio](https://github.com/consideRatio)) +- Dockerfile: add build-essential to builder image [#2866](https://github.com/jupyterhub/jupyterhub/pull/2866) ([@rkdarst](https://github.com/rkdarst)) +- Dockerfile: Copy share/ to the final image [#2864](https://github.com/jupyterhub/jupyterhub/pull/2864) ([@rkdarst](https://github.com/rkdarst)) +- chore: Dockerfile updates [#2853](https://github.com/jupyterhub/jupyterhub/pull/2853) ([@jgwerner](https://github.com/jgwerner)) +- simplify Dockerfile [#2840](https://github.com/jupyterhub/jupyterhub/pull/2840) ([@minrk](https://github.com/minrk)) +- docker: fix onbuild image arg [#2839](https://github.com/jupyterhub/jupyterhub/pull/2839) ([@minrk](https://github.com/minrk)) +- remove redundant pip package list in docs environment.yml [#2838](https://github.com/jupyterhub/jupyterhub/pull/2838) ([@minrk](https://github.com/minrk)) +- docs: Update docs to run tests [#2812](https://github.com/jupyterhub/jupyterhub/pull/2812) ([@jgwerner](https://github.com/jgwerner)) +- remove redundant pip package list in docs environment.yml [#2838](https://github.com/jupyterhub/jupyterhub/pull/2838) ([@minrk](https://github.com/minrk)) +- updating to pandas docs theme [#2820](https://github.com/jupyterhub/jupyterhub/pull/2820) ([@choldgraf](https://github.com/choldgraf)) +- Adding institutional faq [#2800](https://github.com/jupyterhub/jupyterhub/pull/2800) ([@choldgraf](https://github.com/choldgraf)) +- Add inline comment to test [#2826](https://github.com/jupyterhub/jupyterhub/pull/2826) ([@consideRatio](https://github.com/consideRatio)) +- Raise error on missing specified config [#2824](https://github.com/jupyterhub/jupyterhub/pull/2824) ([@consideRatio](https://github.com/consideRatio)) +- chore: Refactor Dockerfile [#2816](https://github.com/jupyterhub/jupyterhub/pull/2816) ([@jgwerner](https://github.com/jgwerner)) +- chore: Update python versions in travis matrix [#2811](https://github.com/jupyterhub/jupyterhub/pull/2811) ([@jgwerner](https://github.com/jgwerner)) +- chore: Bump package versions used in pre-commit config [#2810](https://github.com/jupyterhub/jupyterhub/pull/2810) ([@jgwerner](https://github.com/jgwerner)) +- adding docs preview to circleci [#2803](https://github.com/jupyterhub/jupyterhub/pull/2803) ([@choldgraf](https://github.com/choldgraf)) +- adding institutional faq [#2800](https://github.com/jupyterhub/jupyterhub/pull/2800) ([@choldgraf](https://github.com/choldgraf)) +- The proxy's REST API listens on port `8001` [#2795](https://github.com/jupyterhub/jupyterhub/pull/2795) ([@bnuhero](https://github.com/bnuhero)) +- cull_idle_servers.py: rebind max_age and inactive_limit locally [#2794](https://github.com/jupyterhub/jupyterhub/pull/2794) ([@rkdarst](https://github.com/rkdarst)) +- Fix deprecation warnings [#2789](https://github.com/jupyterhub/jupyterhub/pull/2789) ([@tirkarthi](https://github.com/tirkarthi)) +- Log proxy class [#2783](https://github.com/jupyterhub/jupyterhub/pull/2783) ([@GeorgianaElena](https://github.com/GeorgianaElena)) +- Add docs for fixtures in CONTRIBUTING.md [#2782](https://github.com/jupyterhub/jupyterhub/pull/2782) ([@kinow](https://github.com/kinow)) +- Fix header project name typo [#2775](https://github.com/jupyterhub/jupyterhub/pull/2775) ([@kinow](https://github.com/kinow)) +- Remove unused setupegg.py [#2774](https://github.com/jupyterhub/jupyterhub/pull/2774) ([@kinow](https://github.com/kinow)) +- Log JupyterHub version on startup [#2752](https://github.com/jupyterhub/jupyterhub/pull/2752) ([@consideRatio](https://github.com/consideRatio)) +- Reduce verbosity for "Failing suspected API request to not-running server" (new) [#2751](https://github.com/jupyterhub/jupyterhub/pull/2751) ([@rkdarst](https://github.com/rkdarst)) +- Add missing package for json schema doc build [#2744](https://github.com/jupyterhub/jupyterhub/pull/2744) ([@willingc](https://github.com/willingc)) +- block urllib3 versions with encoding bug [#2743](https://github.com/jupyterhub/jupyterhub/pull/2743) ([@minrk](https://github.com/minrk)) +- Remove tornado deprecated/unnecessary AsyncIOMainLoop().install() call [#2740](https://github.com/jupyterhub/jupyterhub/pull/2740) ([@kinow](https://github.com/kinow)) +- Fix deprecated call [#2739](https://github.com/jupyterhub/jupyterhub/pull/2739) ([@kinow](https://github.com/kinow)) +- Remove duplicate hub and authenticator traitlets from Spawner [#2736](https://github.com/jupyterhub/jupyterhub/pull/2736) ([@eslavich](https://github.com/eslavich)) +- Update issue template [#2725](https://github.com/jupyterhub/jupyterhub/pull/2725) ([@willingc](https://github.com/willingc)) +- Use autodoc-traits sphinx extension [#2723](https://github.com/jupyterhub/jupyterhub/pull/2723) ([@willingc](https://github.com/willingc)) +- Add New Server: change redirecting to relative to home page in js [#2714](https://github.com/jupyterhub/jupyterhub/pull/2714) ([@bitnik](https://github.com/bitnik)) +- Create a warning when creating a service implicitly from service_tokens [#2704](https://github.com/jupyterhub/jupyterhub/pull/2704) ([@katsar0v](https://github.com/katsar0v)) +- Fix mistypos [#2702](https://github.com/jupyterhub/jupyterhub/pull/2702) ([@rlukin](https://github.com/rlukin)) +- Add Jupyter community link [#2696](https://github.com/jupyterhub/jupyterhub/pull/2696) ([@mattjshannon](https://github.com/mattjshannon)) +- Fix failing travis tests [#2695](https://github.com/jupyterhub/jupyterhub/pull/2695) ([@GeorgianaElena](https://github.com/GeorgianaElena)) +- Documentation update: hint for using services instead of service tokens. [#2679](https://github.com/jupyterhub/jupyterhub/pull/2679) ([@katsar0v](https://github.com/katsar0v)) +- Replace header logo: jupyter -> jupyterhub [#2672](https://github.com/jupyterhub/jupyterhub/pull/2672) ([@consideRatio](https://github.com/consideRatio)) +- Update spawn-form example [#2662](https://github.com/jupyterhub/jupyterhub/pull/2662) ([@kinow](https://github.com/kinow)) +- Update flask hub authentication services example in doc [#2658](https://github.com/jupyterhub/jupyterhub/pull/2658) ([@cmd-ntrf](https://github.com/cmd-ntrf)) +- close `
` tag in home.html [#2649](https://github.com/jupyterhub/jupyterhub/pull/2649) ([@bitnik](https://github.com/bitnik)) +- Some theme updates; no double NEXT/PREV buttons. [#2647](https://github.com/jupyterhub/jupyterhub/pull/2647) ([@Carreau](https://github.com/Carreau)) +- fix typos on technical reference documentation [#2646](https://github.com/jupyterhub/jupyterhub/pull/2646) ([@ilee38](https://github.com/ilee38)) +- Update links for Hadoop-related subprojects [#2645](https://github.com/jupyterhub/jupyterhub/pull/2645) ([@jcrist](https://github.com/jcrist)) +- corrected docker network create instructions in dockerfiles README [#2632](https://github.com/jupyterhub/jupyterhub/pull/2632) ([@bartolone](https://github.com/bartolone)) +- Fixed docs and testing code to use refactored SimpleLocalProcessSpawner [#2631](https://github.com/jupyterhub/jupyterhub/pull/2631) ([@danlester](https://github.com/danlester)) +- Update the config used for testing [#2628](https://github.com/jupyterhub/jupyterhub/pull/2628) ([@jtpio](https://github.com/jtpio)) +- Update doc: do not suggest depricated config key [#2626](https://github.com/jupyterhub/jupyterhub/pull/2626) ([@lumbric](https://github.com/lumbric)) +- Add missing words [#2625](https://github.com/jupyterhub/jupyterhub/pull/2625) ([@remram44](https://github.com/remram44)) +- cull-idle: Include a hint on how to add custom culling logic [#2613](https://github.com/jupyterhub/jupyterhub/pull/2613) ([@rkdarst](https://github.com/rkdarst)) +- Replace existing redirect code by Tornado's addslash decorator [#2609](https://github.com/jupyterhub/jupyterhub/pull/2609) ([@kinow](https://github.com/kinow)) +- Hide Stop My Server red button after server stopped. [#2577](https://github.com/jupyterhub/jupyterhub/pull/2577) ([@aar0nTw](https://github.com/aar0nTw)) +- Update link of `changelog` [#2565](https://github.com/jupyterhub/jupyterhub/pull/2565) ([@iblis17](https://github.com/iblis17)) +- typo [#2564](https://github.com/jupyterhub/jupyterhub/pull/2564) ([@julienchastang](https://github.com/julienchastang)) +- Update to simplify the language related to spawner options [#2558](https://github.com/jupyterhub/jupyterhub/pull/2558) ([@NikeNano](https://github.com/NikeNano)) +- Adding the use case of the Elucidata: How Jupyter Notebook is used in… [#2548](https://github.com/jupyterhub/jupyterhub/pull/2548) ([@IamViditAgarwal](https://github.com/IamViditAgarwal)) +- Dict rewritten as literal [#2546](https://github.com/jupyterhub/jupyterhub/pull/2546) ([@remyleone](https://github.com/remyleone)) + ## 1.0 ### [1.0.0] 2019-05-03 @@ -89,8 +393,8 @@ whether it was through discussion, testing, documentation, or development. This hook may transform the return value of `Authenticator.authenticate()` and return a new authentication dictionary, e.g. specifying admin privileges, group membership, - or custom white/blacklisting logic. - This hook is called *after* existing normalization and whitelist checking. + or custom allowed/blocked logic. + This hook is called *after* existing normalization and allowed-username checking. - `Spawner.options_from_form` may now be async - Added `JupyterHub.shutdown_on_logout` option to trigger shutdown of a user's servers when they log out. @@ -276,14 +580,14 @@ and tornado < 5.0. launching an IPython session connected to your JupyterHub database. - Include `User.auth_state` in user model on single-user REST endpoints for admins only. - Include `Server.state` in server model on REST endpoints for admins only. -- Add `Authenticator.blacklist` for blacklisting users instead of whitelisting. +- Add `Authenticator.blacklist` for blocking users instead of allowing. - Pass `c.JupyterHub.tornado_settings['cookie_options']` down to Spawners so that cookie options (e.g. `expires_days`) can be set globally for the whole application. - SIGINFO (`ctrl-t`) handler showing the current status of all running threads, coroutines, and CPU/memory/FD consumption. - Add async `Spawner.get_options_form` alternative to `.options_form`, so it can be a coroutine. - Add `JupyterHub.redirect_to_server` config to govern whether - users should be sent to their server on login or the JuptyerHub home page. + users should be sent to their server on login or the JupyterHub home page. - html page templates can be more easily customized and extended. - Allow registering external OAuth clients for using the Hub as an OAuth provider. - Add basic prometheus metrics at `/hub/metrics` endpoint. @@ -576,7 +880,10 @@ Fix removal of `/login` page in 0.4.0, breaking some OAuth providers. First preview release -[Unreleased]: https://github.com/jupyterhub/jupyterhub/compare/1.0.0...HEAD +[Unreleased]: https://github.com/jupyterhub/jupyterhub/compare/1.2.1...HEAD +[1.2.1]: https://github.com/jupyterhub/jupyterhub/compare/1.2.0...1.2.1 +[1.2.0]: https://github.com/jupyterhub/jupyterhub/compare/1.1.0...1.2.0 +[1.1.0]: https://github.com/jupyterhub/jupyterhub/compare/1.0.0...1.1.0 [1.0.0]: https://github.com/jupyterhub/jupyterhub/compare/0.9.6...1.0.0 [0.9.6]: https://github.com/jupyterhub/jupyterhub/compare/0.9.4...0.9.6 [0.9.4]: https://github.com/jupyterhub/jupyterhub/compare/0.9.3...0.9.4 diff --git a/docs/source/conf.py b/docs/source/conf.py index 2e2e2087..317420d7 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- # import os -import shlex import sys # Set paths @@ -19,10 +18,10 @@ extensions = [ 'sphinx.ext.napoleon', 'autodoc_traits', 'sphinx_copybutton', + 'sphinx-jsonschema', + 'recommonmark', ] -templates_path = ['_templates'] - # The master toctree document. master_doc = 'index' @@ -37,7 +36,6 @@ from os.path import dirname docs = dirname(dirname(__file__)) root = dirname(docs) sys.path.insert(0, root) -sys.path.insert(0, os.path.join(docs, 'sphinxext')) import jupyterhub @@ -59,25 +57,74 @@ default_role = 'literal' import recommonmark from recommonmark.transform import AutoStructify +# -- Config ------------------------------------------------------------- +from jupyterhub.app import JupyterHub +from docutils import nodes +from sphinx.directives.other import SphinxDirective +from contextlib import redirect_stdout +from io import StringIO + +# create a temp instance of JupyterHub just to get the output of the generate-config +# and help --all commands. +jupyterhub_app = JupyterHub() + + +class ConfigDirective(SphinxDirective): + """Generate the configuration file output for use in the documentation.""" + + has_content = False + required_arguments = 0 + optional_arguments = 0 + final_argument_whitespace = False + option_spec = {} + + def run(self): + # The generated configuration file for this version + generated_config = jupyterhub_app.generate_config_file() + # post-process output + home_dir = os.environ['HOME'] + generated_config = generated_config.replace(home_dir, '$HOME', 1) + par = nodes.literal_block(text=generated_config) + return [par] + + +class HelpAllDirective(SphinxDirective): + """Print the output of jupyterhub help --all for use in the documentation.""" + + has_content = False + required_arguments = 0 + optional_arguments = 0 + final_argument_whitespace = False + option_spec = {} + + def run(self): + # The output of the help command for this version + buffer = StringIO() + with redirect_stdout(buffer): + jupyterhub_app.print_help('--help-all') + all_help = buffer.getvalue() + # post-process output + home_dir = os.environ['HOME'] + all_help = all_help.replace(home_dir, '$HOME', 1) + par = nodes.literal_block(text=all_help) + return [par] + def setup(app): app.add_config_value('recommonmark_config', {'enable_eval_rst': True}, True) - app.add_stylesheet('custom.css') + app.add_css_file('custom.css') app.add_transform(AutoStructify) + app.add_directive('jupyterhub-generate-config', ConfigDirective) + app.add_directive('jupyterhub-help-all', HelpAllDirective) -source_parsers = {'.md': 'recommonmark.parser.CommonMarkParser'} - source_suffix = ['.rst', '.md'] # source_encoding = 'utf-8-sig' # -- Options for HTML output ---------------------------------------------- # The theme to use for HTML and HTML Help pages. -import alabaster_jupyterhub - -html_theme = 'alabaster_jupyterhub' -html_theme_path = [alabaster_jupyterhub.get_html_theme_path()] +html_theme = 'pydata_sphinx_theme' html_logo = '_static/images/logo/logo.png' html_favicon = '_static/images/logo/favicon.ico' @@ -85,31 +132,6 @@ html_favicon = '_static/images/logo/favicon.ico' # Paths that contain custom static files (such as style sheets) html_static_path = ['_static'] -html_theme_options = { - 'show_related': True, - 'description': 'Documentation for JupyterHub', - 'github_user': 'jupyterhub', - 'github_repo': 'jupyterhub', - 'github_banner': False, - 'github_button': True, - 'github_type': 'star', - 'show_powered_by': False, - 'extra_nav_links': { - 'GitHub Repo': 'http://github.com/jupyterhub/jupyterhub', - 'Issue Tracker': 'http://github.com/jupyterhub/jupyterhub/issues', - }, -} - -html_sidebars = { - '**': [ - 'about.html', - 'searchbox.html', - 'navigation.html', - 'relations.html', - 'sourcelink.html', - ] -} - htmlhelp_basename = 'JupyterHubdoc' # -- Options for LaTeX output --------------------------------------------- @@ -192,14 +214,12 @@ intersphinx_mapping = {'https://docs.python.org/3/': None} # -- Read The Docs -------------------------------------------------------- on_rtd = os.environ.get('READTHEDOCS', None) == 'True' -if not on_rtd: - html_theme = 'alabaster' -else: +if on_rtd: # readthedocs.org uses their theme by default, so no need to specify it - # build rest-api, since RTD doesn't run make + # build both metrics and rest-api, since RTD doesn't run make from subprocess import check_call as sh - sh(['make', 'rest-api'], cwd=docs) + sh(['make', 'metrics', 'rest-api'], cwd=docs) # -- Spell checking ------------------------------------------------------- diff --git a/docs/source/contributing/index.rst b/docs/source/contributing/index.rst new file mode 100644 index 00000000..78c02037 --- /dev/null +++ b/docs/source/contributing/index.rst @@ -0,0 +1,21 @@ +============ +Contributing +============ + +We want you to contribute to JupyterHub in ways that are most exciting +& useful to you. We value documentation, testing, bug reporting & code equally, +and are glad to have your contributions in whatever form you wish :) + +Our `Code of Conduct `_ +(`reporting guidelines `_) +helps keep our community welcoming to as many people as possible. + +.. toctree:: + :maxdepth: 2 + + community + setup + docs + tests + roadmap + security diff --git a/docs/source/contributing/roadmap.md b/docs/source/contributing/roadmap.md index 8ad33fbf..c1a6194e 100644 --- a/docs/source/contributing/roadmap.md +++ b/docs/source/contributing/roadmap.md @@ -83,7 +83,6 @@ these will be moved at a future review of the roadmap. - (prometheus?) API for resource monitoring - tracking activity on single-user servers instead of the proxy - notes and activity tracking per API token - - UI for managing named servers ### Later diff --git a/docs/source/contributing/setup.rst b/docs/source/contributing/setup.rst index 73ffada6..aaad9e57 100644 --- a/docs/source/contributing/setup.rst +++ b/docs/source/contributing/setup.rst @@ -8,7 +8,7 @@ System requirements =================== JupyterHub can only run on MacOS or Linux operating systems. If you are -using Windows, we recommend using `VirtualBox `_ +using Windows, we recommend using `VirtualBox `_ or a similar system to run `Ubuntu Linux `_ for development. @@ -18,7 +18,7 @@ Install Python JupyterHub is written in the `Python `_ programming language, and requires you have at least version 3.5 installed locally. If you haven’t installed Python before, the recommended way to install it is to use -`miniconda `_. Remember to get the ‘Python 3’ version, +`miniconda `_. Remember to get the ‘Python 3’ version, and **not** the ‘Python 2’ version! Install nodejs @@ -45,7 +45,13 @@ When developing JupyterHub, you need to make changes to the code & see their effects quickly. You need to do a developer install to make that happen. -1. Clone the `JupyterHub git repository `_ +.. note:: This guide does not attempt to dictate *how* development + environements should be isolated since that is a personal preference and can + be achieved in many ways, for example `tox`, `conda`, `docker`, etc. See this + `forum thread `_ for + a more detailed discussion. + +1. Clone the `JupyterHub git repository `_ to your computer. .. code:: bash @@ -93,7 +99,14 @@ happen. python3 -m pip install -r dev-requirements.txt python3 -m pip install -r requirements.txt -5. Install the development version of JupyterHub. This lets you edit +5. Setup a database. + + The default database engine is ``sqlite`` so if you are just trying + to get up and running quickly for local development that should be + available via `python `__. + See :doc:`/reference/database` for details on other supported databases. + +6. Install the development version of JupyterHub. This lets you edit JupyterHub code in a text editor & restart the JupyterHub process to see your code changes immediately. @@ -101,24 +114,23 @@ happen. python3 -m pip install --editable . -6. You are now ready to start JupyterHub! +7. You are now ready to start JupyterHub! .. code:: bash jupyterhub -7. You can access JupyterHub from your browser at +8. You can access JupyterHub from your browser at ``http://localhost:8000`` now. Happy developing! -Using DummyAuthenticator & SimpleSpawner -======================================== +Using DummyAuthenticator & SimpleLocalProcessSpawner +==================================================== To simplify testing of JupyterHub, it’s helpful to use :class:`~jupyterhub.auth.DummyAuthenticator` instead of the default JupyterHub -authenticator and `SimpleSpawner `_ -instead of the default spawner. +authenticator and SimpleLocalProcessSpawner instead of the default spawner. There is a sample configuration file that does this in ``testing/jupyterhub_config.py``. To launch jupyterhub with this @@ -126,7 +138,6 @@ configuration: .. code:: bash - pip install jupyterhub-simplespawner jupyterhub -f testing/jupyterhub_config.py The default JupyterHub `authenticator @@ -137,15 +148,15 @@ require your system to have user accounts for each user you want to log in to JupyterHub as. DummyAuthenticator allows you to log in with any username & password, -while SimpleSpawner allows you to start servers without having to +while SimpleLocalProcessSpawner allows you to start servers without having to create a unix user for each JupyterHub user. Together, these make it much easier to test JupyterHub. Tip: If you are working on parts of JupyterHub that are common to all authenticators & spawners, we recommend using both DummyAuthenticator & -SimpleSpawner. If you are working on just authenticator related parts, -use only SimpleSpawner. Similarly, if you are working on just spawner -related parts, use only DummyAuthenticator. +SimpleLocalProcessSpawner. If you are working on just authenticator related +parts, use only SimpleLocalProcessSpawner. Similarly, if you are working on +just spawner related parts, use only DummyAuthenticator. Troubleshooting =============== diff --git a/docs/source/contributing/tests.rst b/docs/source/contributing/tests.rst index a59487c0..c607df92 100644 --- a/docs/source/contributing/tests.rst +++ b/docs/source/contributing/tests.rst @@ -23,27 +23,28 @@ Running the tests .. code-block:: bash - pytest --async-test-timeout 15 -v jupyterhub/tests + pytest -v jupyterhub/tests This should display progress as it runs all the tests, printing information about any test failures as they occur. + + If you wish to confirm test coverage the run tests with the `--cov` flag: - The ``--async-test-timeout`` parameter is used by `pytest-tornado - `_ to set the - asynchronous test timeout to 15 seconds rather than the default 5, - since some of our tests take longer than 5s to execute. + .. code-block:: bash + + pytest -v --cov=jupyterhub jupyterhub/tests #. You can also run tests in just a specific file: .. code-block:: bash - pytest --async-test-timeout 15 -v jupyterhub/tests/ + pytest -v jupyterhub/tests/ #. To run a specific test only, you can do: .. code-block:: bash - pytest --async-test-timeout 15 -v jupyterhub/tests/:: + pytest -v jupyterhub/tests/:: This runs the test with function name ```` defined in ````. This is very useful when you are iteratively @@ -63,16 +64,5 @@ Troubleshooting Test Failures All the tests are failing ------------------------- -Make sure you have completed all the steps in :ref:`contributing/setup` sucessfully, and +Make sure you have completed all the steps in :ref:`contributing/setup` successfully, and can launch ``jupyterhub`` from the terminal. - -Tests are timing out --------------------- - -The ``--async-test-timeout`` parameter to ``pytest`` is used by -`pytest-tornado `_ to set -the asynchronous test timeout to a higher value than the default of 5s, -since some of our tests take longer than 5s to execute. If the tests -are still timing out, try increasing that value even more. You can -also set an environment variable ``ASYNC_TEST_TIMEOUT`` instead of -passing ``--async-test-timeout`` to each invocation of pytest. diff --git a/docs/source/events/index.rst b/docs/source/events/index.rst new file mode 100644 index 00000000..90e30acb --- /dev/null +++ b/docs/source/events/index.rst @@ -0,0 +1,50 @@ +Eventlogging and Telemetry +========================== + +JupyterHub can be configured to record structured events from a running server using Jupyter's `Telemetry System`_. The types of events that JupyterHub emits are defined by `JSON schemas`_ listed below_ + + emitted as JSON data, defined and validated by the JSON schemas listed below. + + +.. _logging: https://docs.python.org/3/library/logging.html +.. _`Telemetry System`: https://github.com/jupyter/telemetry +.. _`JSON schemas`: https://json-schema.org/ + +How to emit events +------------------ + +Event logging is handled by its ``Eventlog`` object. This leverages Python's standing logging_ library to emit, filter, and collect event data. + + +To begin recording events, you'll need to set two configurations: + + 1. ``handlers``: tells the EventLog *where* to route your events. This trait is a list of Python logging handlers that route events to + 2. ``allows_schemas``: tells the EventLog *which* events should be recorded. No events are emitted by default; all recorded events must be listed here. + +Here's a basic example: + +.. code-block:: + + import logging + + c.EventLog.handlers = [ + logging.FileHandler('event.log'), + ] + + c.EventLog.allowed_schemas = [ + 'hub.jupyter.org/server-action' + ] + +The output is a file, ``"event.log"``, with events recorded as JSON data. + + + +.. _below: + +Event schemas +------------- + +.. toctree:: + :maxdepth: 2 + + server-actions.rst diff --git a/docs/source/events/server-actions.rst b/docs/source/events/server-actions.rst new file mode 100644 index 00000000..12018713 --- /dev/null +++ b/docs/source/events/server-actions.rst @@ -0,0 +1 @@ +.. jsonschema:: ../../../jupyterhub/event-schemas/server-actions/v1.yaml diff --git a/docs/source/gallery-jhub-deployments.md b/docs/source/gallery-jhub-deployments.md index ef4e7967..55a3dff6 100644 --- a/docs/source/gallery-jhub-deployments.md +++ b/docs/source/gallery-jhub-deployments.md @@ -142,7 +142,10 @@ easy to do with RStudio too. - Kristen Thyng - Oceanography - [Teaching with JupyterHub and nbgrader](http://kristenthyng.com/blog/2016/09/07/jupyterhub+nbgrader/) - +### Elucidata + - What's new in Jupyter Notebooks @[Elucidata](https://elucidata.io/): + - Using Jupyter Notebooks with Jupyterhub on GCP, managed by GKE + - https://medium.com/elucidata/why-you-should-be-using-a-jupyter-notebook-8385a4ccd93d ## Service Providers @@ -170,7 +173,7 @@ easy to do with RStudio too. ### Hadoop -- [Deploying JupyterHub on Hadoop](https://jcrist.github.io/jupyterhub-on-hadoop/) +- [Deploying JupyterHub on Hadoop](https://jupyterhub-on-hadoop.readthedocs.io) ## Miscellaneous diff --git a/docs/source/getting-started/authenticators-users-basics.md b/docs/source/getting-started/authenticators-users-basics.md index b954f88d..ec078fed 100644 --- a/docs/source/getting-started/authenticators-users-basics.md +++ b/docs/source/getting-started/authenticators-users-basics.md @@ -4,23 +4,23 @@ The default Authenticator uses [PAM][] to authenticate system users with their username and password. With the default Authenticator, any user with an account and password on the system will be allowed to login. -## Create a whitelist of users +## Create a set of allowed users -You can restrict which users are allowed to login with a whitelist, -`Authenticator.whitelist`: +You can restrict which users are allowed to login with a set, +`Authenticator.allowed_users`: ```python -c.Authenticator.whitelist = {'mal', 'zoe', 'inara', 'kaylee'} +c.Authenticator.allowed_users = {'mal', 'zoe', 'inara', 'kaylee'} ``` -Users in the whitelist are added to the Hub database when the Hub is +Users in the `allowed_users` set are added to the Hub database when the Hub is started. ## Configure admins (`admin_users`) Admin users of JupyterHub, `admin_users`, can add and remove users from -the user `whitelist`. `admin_users` can take actions on other users' +the user `allowed_users` set. `admin_users` can take actions on other users' behalf, such as stopping and restarting their servers. A set of initial admin users, `admin_users` can configured be as follows: @@ -28,7 +28,7 @@ A set of initial admin users, `admin_users` can configured be as follows: ```python c.Authenticator.admin_users = {'mal', 'zoe'} ``` -Users in the admin list are automatically added to the user `whitelist`, +Users in the admin set are automatically added to the user `allowed_users` set, if they are not already present. Each authenticator may have different ways of determining whether a user is an @@ -53,12 +53,12 @@ sure your users know if admin_access is enabled.** Users can be added to and removed from the Hub via either the admin panel or the REST API. When a user is **added**, the user will be -automatically added to the whitelist and database. Restarting the Hub -will not require manually updating the whitelist in your config file, +automatically added to the allowed users set and database. Restarting the Hub +will not require manually updating the allowed users set in your config file, as the users will be loaded from the database. After starting the Hub once, it is not sufficient to **remove** a user -from the whitelist in your config file. You must also remove the user +from the allowed users set in your config file. You must also remove the user from the Hub's database, either by deleting the user from JupyterHub's admin page, or you can clear the `jupyterhub.sqlite` database and start fresh. diff --git a/docs/source/getting-started/faq.md b/docs/source/getting-started/faq.md new file mode 100644 index 00000000..ae912847 --- /dev/null +++ b/docs/source/getting-started/faq.md @@ -0,0 +1,36 @@ +# Frequently asked questions + + +### How do I share links to notebooks? + +In short, where you see `/user/name/notebooks/foo.ipynb` use `/hub/user-redirect/notebooks/foo.ipynb` (replace `/user/name` with `/hub/user-redirect`). + +Sharing links to notebooks is a common activity, +and can look different based on what you mean. +Your first instinct might be to copy the URL you see in the browser, +e.g. `hub.jupyter.org/user/yourname/notebooks/coolthing.ipynb`. +However, let's break down what this URL means: + +`hub.jupyter.org/user/yourname/` is the URL prefix handled by *your server*, +which means that sharing this URL is asking the person you share the link with +to come to *your server* and look at the exact same file. +In most circumstances, this is forbidden by permissions because the person you share with does not have access to your server. +What actually happens when someone visits this URL will depend on whether your server is running and other factors. + +But what is our actual goal? +A typical situation is that you have some shared or common filesystem, +such that the same path corresponds to the same document +(either the exact same document or another copy of it). +Typically, what folks want when they do sharing like this +is for each visitor to open the same file *on their own server*, +so Breq would open `/user/breq/notebooks/foo.ipynb` and +Seivarden would open `/user/seivarden/notebooks/foo.ipynb`, etc. + +JupyterHub has a special URL that does exactly this! +It's called `/hub/user-redirect/...` and after the visitor logs in, +So if you replace `/user/yourname` in your URL bar +with `/hub/user-redirect` any visitor should get the same +URL on their own server, rather than visiting yours. + +In JupyterLab 2.0, this should also be the result of the "Copy Shareable Link" +action in the file browser. diff --git a/docs/source/getting-started/index.rst b/docs/source/getting-started/index.rst index b1e509d9..bae95f8f 100644 --- a/docs/source/getting-started/index.rst +++ b/docs/source/getting-started/index.rst @@ -1,5 +1,10 @@ -Getting Started -=============== +Get Started +=========== + +This section covers how to configure and customize JupyterHub for your +needs. It contains information about authentication, networking, security, and +other topics that are relevant to individuals or organizations deploying their +own JupyterHub. .. toctree:: :maxdepth: 2 @@ -10,3 +15,5 @@ Getting Started authenticators-users-basics spawners-basics services-basics + faq + institutional-faq diff --git a/docs/source/getting-started/institutional-faq.md b/docs/source/getting-started/institutional-faq.md new file mode 100644 index 00000000..0d38f7eb --- /dev/null +++ b/docs/source/getting-started/institutional-faq.md @@ -0,0 +1,266 @@ +# Institutional FAQ + +This page contains common questions from users of JupyterHub, +broken down by their roles within organizations. + +## For all + +### Is it appropriate for adoption within a larger institutional context? + +Yes! JupyterHub has been used at-scale for large pools of users, as well +as complex and high-performance computing. For example, UC Berkeley uses +JupyterHub for its Data Science Education Program courses (serving over +3,000 students). The Pangeo project uses JupyterHub to provide access +to scalable cloud computing with Dask. JupyterHub is stable customizable +to the use-cases of large organizations. + +### I keep hearing about Jupyter Notebook, JupyterLab, and now JupyterHub. What’s the difference? + +Here is a quick breakdown of these three tools: + +* **The Jupyter Notebook** is a document specification (the `.ipynb`) file that interweaves + narrative text with code cells and their outputs. It is also a graphical interface + that allows users to edit these documents. There are also several other graphical interfaces + that allow users to edit the `.ipynb` format (nteract, Jupyter Lab, Google Colab, Kaggle, etc). +* **JupyterLab** is a flexible and extendible user interface for interactive computing. It + has several extensions that are tailored for using Jupyter Notebooks, as well as extensions + for other parts of the data science stack. +* **JupyterHub** is an application that manages interactive computing sessions for **multiple users**. + It also connects them with infrastructure those users wish to access. It can provide + remote access to Jupyter Notebooks and Jupyter Lab for many people. + +## For management + +### Briefly, what problem does JupyterHub solve for us? + +JupyterHub provides a shared platform for data science and collaboration. +It allows users to utilize familiar data science workflows (such as the scientific python stack, +the R tidyverse, and Jupyter Notebooks) on institutional infrastructure. It also allows administrators +some control over access to resources, security, environments, and authentication. + +### Is JupyterHub mature? Why should we trust it? + +Yes - the core JupyterHub application recently +reached 1.0 status, and is considered stable and performant for most institutions. +JupyterHub has also been deployed (along with other tools) to work on +scalable infrastructure, large datasets, and high-performance computing. + +### Who else uses JupyterHub? + +JupyterHub is used at a variety of institutions in academia, +industry, and government research labs. It is most-commonly used by two kinds of groups: + +* Small teams (e.g., data science teams, research labs, or collaborative projects) to provide a + shared resource for interactive computing, collaboration, and analytics. +* Large teams (e.g., a department, a large class, or a large group of remote users) to provide + access to organizational hardware, data, and analytics environments at scale. + +Here are a sample of organizations that use JupyterHub: + +* **Universities and colleges**: UC Berkeley, UC San Diego, Cal Poly SLO, Harvard University, University of Chicago, + University of Oslo, University of Sheffield, Université Paris Sud, University of Versailles +* **Research laboratories**: NASA, NCAR, NOAA, the Large Synoptic Survey Telescope, Brookhaven National Lab, + Minnesota Supercomputing Institute, ALCF, CERN, Lawrence Livermore National Laboratory +* **Online communities**: Pangeo, Quantopian, mybinder.org, MathHub, Open Humans +* **Computing infrastructure providers**: NERSC, San Diego Supercomputing Center, Compute Canada +* **Companies**: Capital One, SANDVIK code, Globus + +See the [Gallery of JupyterHub deployments](../gallery-jhub-deployments.md) for +a more complete list of JupyterHub deployments at institutions. + +### How does JupyterHub compare with hosted products, like Google Colaboratory, RStudio.cloud, or Anaconda Enterprise? + +JupyterHub puts you in control of your data, infrastructure, and coding environment. +In addition, it is vendor neutral, which reduces lock-in to a particular vendor or service. +JupyterHub provides access to interactive computing environments in the cloud (similar to each of these services). +Compared with the tools above, it is more flexible, more customizable, free, and +gives administrators more control over their setup and hardware. + +Because JupyterHub is an open-source, community-driven tool, it can be extended and +modified to fit an institution's needs. It plays nicely with the open source data science +stack, and can serve a variety of computing enviroments, user interfaces, and +computational hardware. It can also be deployed anywhere - on enterprise cloud infrastructure, on +High-Performance-Computing machines, on local hardware, or even on a single laptop, which +is not possible with most other tools for shared interactive computing. + +## For IT + +### How would I set up JupyterHub on institutional hardware? + +That depends on what kind of hardware you've got. JupyterHub is flexible enough to be deployed +on a variety of hardware, including in-room hardware, on-prem clusters, cloud infrastructure, +etc. + +The most common way to set up a JupyterHub is to use a JupyterHub distribution, these are pre-configured +and opinionated ways to set up a JupyterHub on particular kinds of infrastructure. The two distributions +that we currently suggest are: + +* [Zero to JupyterHub for Kubernetes](https://z2jh.jupyter.org) is a scalable JupyterHub deployment and + guide that runs on Kubernetes. Better for larger or dynamic user groups (50-10,000) or more complex + compute/data needs. +* [The Littlest JupyterHub](https://tljh.jupyter.org) is a lightweight JupyterHub that runs on a single + single machine (in the cloud or under your desk). Better for smaller usergroups (4-80) or more + lightweight computational resources. + + +### Does JupyterHub run well in the cloud? + +Yes - most deployments of JupyterHub are run via cloud infrastructure and on a variety of cloud providers. +Depending on the distribution of JupyterHub that you'd like to use, you can also connect your JupyterHub +deployment with a number of other cloud-native services so that users have access to other resources from +their interactive computing sessions. + +For example, if you use the [Zero to JupyterHub for Kubernetes](https://z2jh.jupyter.org) distribution, +you'll be able to utilize container-based workflows of other technologies such as the [dask-kubernetes](https://kubernetes.dask.org/en/latest/) +project for distributed computing. + +The Z2JH Helm Chart also has some functionality built in for auto-scaling your cluster up and down +as more resources are needed - allowing you to utilize the benefits of a flexible cloud-based deployment. + +### Is JupyterHub secure? + +The short answer: yes. JupyterHub as a standalone application has been battle-tested at an institutional +level for several years, and makes a number of "default" security decisions that are reasonable for most +users. + +* For security considerations in the base JupyterHub application, + [see the JupyterHub security page](https://jupyterhub.readthedocs.io/en/stable/reference/websecurity.html) +* For security considerations when deploying JupyterHub on Kubernetes, see the + [JupyterHub on Kubernetes security page](https://zero-to-jupyterhub.readthedocs.io/en/latest/security.html). + +The longer answer: it depends on your deployment. Because JupyterHub is very flexible, it can be used +in a variety of deployment setups. This often entails connecting your JupyterHub to **other** infrastructure +(such as a [Dask Gateway service](https://gateway.dask.org/)). There are many security decisions to be made +in these cases, and the security of your JupyterHub deployment will often depend on these decisions. + +If you are worried about security, don't hesitate to reach out to the JupyterHub community in the +[Jupyter Community Forum](https://discourse.jupyter.org/c/jupyterhub). This community of practice has many +individuals with experience running secure JupyterHub deployments. + + +### Does JupyterHub provide computing or data infrastructure? + +No - JupyterHub manages user sessions and can *control* computing infrastructure, but it does not provide these +things itself. You are expected to run JupyterHub on your own infrastructure (local or in the cloud). Moreover, +JupyterHub has no internal concept of "data", but is designed to be able to communicate with data repositories +(again, either locally or remotely) for use within interactive computing sessions. + + +### How do I manage users? + +JupyterHub offers a few options for managing your users. Upon setting up a JupyterHub, you can choose what +kind of **authentication** you'd like to use. For example, you can have users sign up with an institutional +email address, or choose a username / password when they first log-in, or offload authentication onto +another service such as an organization's OAuth. + +The users of a JupyterHub are stored locally, and can be modified manually by an administrator of the JupyterHub. +Moreover, the *active* users on a JupyterHub can be found on the administrator's page. This page +gives you the abiltiy to stop or restart kernels, inspect user filesystems, and even take over user +sessions to assist them with debugging. + +### How do I manage software environments? + +A key benefit of JupyterHub is the ability for an administrator to define the environment(s) that users +have access to. There are many ways to do this, depending on what kind of infrastructure you're using for +your JupyterHub. + +For example, **The Littlest JupyterHub** runs on a single VM. In this case, the administrator defines +an environment by installing packages to a shared folder that exists on the path of all users. The +**JupyterHub for Kubernetes** deployment uses Docker images to define environments. You can create your +own list of Docker images that users can select from, and can also control things like the amount of +RAM available to users, or the types of machines that their sessions will use in the cloud. + +### How does JupyterHub manage computational resources? + +For interactive computing sessions, JupyterHub controls computational resources via a **spawner**. +Spawners define how a new user session is created, and are customized for particular kinds of +infrastructure. For example, the KubeSpawner knows how to control a Kubernetes deployment +to create new pods when users log in. + +For more sophisticated computational resources (like distributed computing), JupyterHub can +connect with other infrastructure tools (like Dask or Spark). This allows users to control +scalable or high-performance resources from within their JupyterHub sessions. The logic of +how those resources are controlled is taken care of by the non-JupyterHub application. + + +### Can JupyterHub be used with my high-performance computing resources? + +Yes - JupyterHub can provide access to many kinds of computing infrastructure. +Especially when combined with other open-source schedulers such as Dask, you can manage fairly +complex computing infrastructure from the interactive sessions of a JupyterHub. For example +[see the Dask HPC page](https://docs.dask.org/en/latest/setup/hpc.html). + +### How much resources do user sessions take? + +This is highly configurable by the administrator. If you wish for your users to have simple +data analytics environments for prototyping and light data exploring, you can restrict their +memory and CPU based on the resources that you have available. If you'd like your JupyterHub +to serve as a gateway to high-performance compute or data resources, you may increase the +resources available on user machines, or connect them with computing infrastructure elsewhere. + +### Can I customize the look and feel of a JupyterHub? + +JupyterHub provides some customization of the graphics displayed to users. The most common +modification is to add custom branding to the JupyterHub login page, loading pages, and +various elements that persist across all pages (such as headers). + +## For Technical Leads + +### Will JupyterHub “just work” with our team's interactive computing setup? + +Depending on the complexity of your setup, you'll have different experiences with "out of the box" +distributions of JupyterHub. If all of the resources you need will fit on a single VM, then +[The Littlest JupyterHub](https://tljh.jupyter.org) should get you up-and-running within +a half day or so. For more complex setups, such as scalable Kubernetes clusters or access +to high-performance computing and data, it will require more time and expertise with +the technologies your JupyterHub will use (e.g., dev-ops knowledge with cloud computing). + +In general, the base JupyterHub deployment is not the bottleneck for setup, it is connecting +your JupyterHub with the various services and tools that you wish to provide to your users. + + +### How well does JupyterHub scale? What are JupyterHub's limitations? + +JupyterHub works well at both a small scale (e.g., a single VM or machine) as well as a +high scale (e.g., a scalable Kubernetes cluster). It can be used for teams as small a 2, and +for user bases as large as 10,000. The scalability of JupyterHub largely depends on the +infrastructure on which it is deployed. JupyterHub has been designed to be lightweight and +flexible, so you can tailor your JupyterHub deployment to your needs. + + +### Is JupyterHub resilient? What happens when a machine goes down? + +For JupyterHubs that are deployed in a containerized environment (e.g., Kubernetes), it is +possible to configure the JupyterHub to be fairly resistant to failures in the system. +For example, if JupyterHub fails, then user sessions will not be affected (though new +users will not be able to log in). When a JupyterHub process is restarted, it should +seamlessly connect with the user database and the system will return to normal. +Again, the details of your JupyterHub deployment (e.g., whether it's deployed on a scalable cluster) +will affect the resiliency of the deployment. + +### What interfaces does JupyterHub support? + +Out of the box, JupyterHub supports a variety of popular data science interfaces for user sessions, +such as JupyterLab, Jupyter Notebooks, and RStudio. Any interface that can be served +via a web address can be served with a JupyterHub (with the right setup). + +### Does JupyterHub make it easier for our team to collaborate? + +JupyterHub provides a standardized environment and access to shared resources for your teams. +This greatly reduces the cost associated with sharing analyses and content with other team +members, and makes it easier to collaborate and build off of one another's ideas. Combined with +access to high-performance computing and data, JupyterHub provides a common resource to +amplify your team's ability to prototype their analyses, scale them to larger data, and then +share their results with one another. + +JupyterHub also provides a computational framework to share computational narratives between +different levels of an organization. For example, data scientists can share Jupyter Notebooks +rendered as [voila dashboards](https://voila.readthedocs.io/en/stable/) with those who are not +familiar with programming, or create publicly-available interactive analyses to allow others to +interact with your work. + +### Can I use JupyterHub with R/RStudio or other languages and environments? + +Yes, Jupyter is a polyglot project, and there are over 40 community-provided kernels for a variety +of languages (the most common being Python, Julia, and R). You can also use a JupyterHub to provide +access to other interfaces, such as RStudio, that provide their own access to a language kernel. diff --git a/docs/source/getting-started/networking-basics.md b/docs/source/getting-started/networking-basics.md index 86c80dcc..5fc44238 100644 --- a/docs/source/getting-started/networking-basics.md +++ b/docs/source/getting-started/networking-basics.md @@ -41,7 +41,7 @@ port. ## Set the Proxy's REST API communication URL (optional) -By default, this REST API listens on port 8081 of `localhost` only. +By default, this REST API listens on port 8001 of `localhost` only. The Hub service talks to the proxy via a REST API on a secondary port. The API URL can be configured separately and override the default settings. diff --git a/docs/source/getting-started/security-basics.rst b/docs/source/getting-started/security-basics.rst index 80996555..87007311 100644 --- a/docs/source/getting-started/security-basics.rst +++ b/docs/source/getting-started/security-basics.rst @@ -80,6 +80,49 @@ To achieve this, simply omit the configuration settings ``c.JupyterHub.ssl_key`` and ``c.JupyterHub.ssl_cert`` (setting them to ``None`` does not have the same effect, and is an error). +.. _authentication-token: + +Proxy authentication token +-------------------------- + +The Hub authenticates its requests to the Proxy using a secret token that +the Hub and Proxy agree upon. Note that this applies to the default +``ConfigurableHTTPProxy`` implementation. Not all proxy implementations +use an auth token. + +The value of this token should be a random string (for example, generated by +``openssl rand -hex 32``). You can store it in the configuration file or an +environment variable + +Generating and storing token in the configuration file +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +You can set the value in the configuration file, ``jupyterhub_config.py``: + +.. code-block:: python + + c.ConfigurableHTTPProxy.api_token = 'abc123...' # any random string + +Generating and storing as an environment variable +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +You can pass this value of the proxy authentication token to the Hub and Proxy +using the ``CONFIGPROXY_AUTH_TOKEN`` environment variable: + +.. code-block:: bash + + export CONFIGPROXY_AUTH_TOKEN=$(openssl rand -hex 32) + +This environment variable needs to be visible to the Hub and Proxy. + +Default if token is not set +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If you don't set the Proxy authentication token, the Hub will generate a random +key itself, which means that any time you restart the Hub you **must also +restart the Proxy**. If the proxy is a subprocess of the Hub, this should happen +automatically (this is the default configuration). + .. _cookie-secret: Cookie secret @@ -146,41 +189,73 @@ itself, ``jupyterhub_config.py``, as a binary string: If the cookie secret value changes for the Hub, all single-user notebook servers must also be restarted. +.. _cookies: -.. _authentication-token: +Cookies used by JupyterHub authentication +----------------------------------------- -Proxy authentication token --------------------------- +The following cookies are used by the Hub for handling user authentication. -The Hub authenticates its requests to the Proxy using a secret token that -the Hub and Proxy agree upon. The value of this string should be a random -string (for example, generated by ``openssl rand -hex 32``). +This section was created based on this post_ from Discourse. -Generating and storing token in the configuration file -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +.. _post: https://discourse.jupyter.org/t/how-to-force-re-login-for-users/1998/6 -Or you can set the value in the configuration file, ``jupyterhub_config.py``: +jupyterhub-hub-login +~~~~~~~~~~~~~~~~~~~~ -.. code-block:: python +This is the login token used when visiting Hub-served pages that are +protected by authentication such as the main home, the spawn form, etc. +If this cookie is set, then the user is logged in. - c.JupyterHub.proxy_auth_token = '0bc02bede919e99a26de1e2a7a5aadfaf6228de836ec39a05a6c6942831d8fe5' +Resetting the Hub cookie secret effectively revokes this cookie. -Generating and storing as an environment variable -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +This cookie is restricted to the path ``/hub/``. -You can pass this value of the proxy authentication token to the Hub and Proxy -using the ``CONFIGPROXY_AUTH_TOKEN`` environment variable: +jupyterhub-user- +~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. code-block:: bash +This is the cookie used for authenticating with a single-user server. +It is set by the single-user server after OAuth with the Hub. - export CONFIGPROXY_AUTH_TOKEN=$(openssl rand -hex 32) +Effectively the same as ``jupyterhub-hub-login``, but for the +single-user server instead of the Hub. It contains an OAuth access token, +which is checked with the Hub to authenticate the browser. -This environment variable needs to be visible to the Hub and Proxy. +Each OAuth access token is associated with a session id (see ``jupyterhub-session-id`` section +below). -Default if token is not set -~~~~~~~~~~~~~~~~~~~~~~~~~~~ +To avoid hitting the Hub on every request, the authentication response +is cached. And to avoid a stale cache the cache key is comprised of both +the token and session id. -If you don't set the Proxy authentication token, the Hub will generate a random -key itself, which means that any time you restart the Hub you **must also -restart the Proxy**. If the proxy is a subprocess of the Hub, this should happen -automatically (this is the default configuration). +Resetting the Hub cookie secret effectively revokes this cookie. + +This cookie is restricted to the path ``/user/``, so that +only the user’s server receives it. + +jupyterhub-session-id +~~~~~~~~~~~~~~~~~~~~~ + +This is a random string, meaningless in itself, and the only cookie +shared by the Hub and single-user servers. + +Its sole purpose is to coordinate logout of the multiple OAuth cookies. + +This cookie is set to ``/`` so all endpoints can receive it, or clear it, etc. + +jupyterhub-user--oauth-state +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +A short-lived cookie, used solely to store and validate OAuth state. +It is only set while OAuth between the single-user server and the Hub +is processing. + +If you use your browser development tools, you should see this cookie +for a very brief moment before your are logged in, +with an expiration date shorter than ``jupyterhub-hub-login`` or +``jupyterhub-user-``. + +This cookie should not exist after you have successfully logged in. + +This cookie is restricted to the path ``/user/``, so that only +the user’s server receives it. diff --git a/docs/source/getting-started/services-basics.md b/docs/source/getting-started/services-basics.md index 7e716a63..a5246350 100644 --- a/docs/source/getting-started/services-basics.md +++ b/docs/source/getting-started/services-basics.md @@ -3,9 +3,9 @@ When working with JupyterHub, a **Service** is defined as a process that interacts with the Hub's REST API. A Service may perform a specific or action or task. For example, shutting down individuals' single user -notebook servers that have been is a good example of a task that could -be automated by a Service. Let's look at how the [cull_idle_servers][] -script can be used as a Service. +notebook servers that have been idle for some time is a good example of +a task that could be automated by a Service. Let's look at how the +[jupyterhub_idle_culler][] script can be used as a Service. ## Real-world example to cull idle servers @@ -15,11 +15,11 @@ document will: - explain some basic information about API tokens - clarify that API tokens can be used to authenticate to single-user servers as of [version 0.8.0](../changelog) -- show how the [cull_idle_servers][] script can be: +- show how the [jupyterhub_idle_culler][] script can be: - used in a Hub-managed service - run as a standalone script -Both examples for `cull_idle_servers` will communicate tasks to the +Both examples for `jupyterhub_idle_culler` will communicate tasks to the Hub via the REST API. ## API Token basics @@ -78,17 +78,23 @@ single-user servers, and only cookies can be used for authentication. 0.8 supports using JupyterHub API tokens to authenticate to single-user servers. -## Configure `cull-idle` to run as a Hub-Managed Service +## Configure the idle culler to run as a Hub-Managed Service + +Install the idle culler: + +``` +pip install jupyterhub-idle-culler +``` In `jupyterhub_config.py`, add the following dictionary for the -`cull-idle` Service to the `c.JupyterHub.services` list: +`idle-culler` Service to the `c.JupyterHub.services` list: ```python c.JupyterHub.services = [ { - 'name': 'cull-idle', + 'name': 'idle-culler', 'admin': True, - 'command': [sys.executable, 'cull_idle_servers.py', '--timeout=3600'], + 'command': [sys.executable, '-m', 'jupyterhub_idle_culler', '--timeout=3600'], } ] ``` @@ -101,21 +107,21 @@ where: ## Run `cull-idle` manually as a standalone script -Now you can run your script, i.e. `cull_idle_servers`, by providing it +Now you can run your script by providing it the API token and it will authenticate through the REST API to interact with it. -This will run `cull-idle` manually. `cull-idle` can be run as a standalone +This will run the idle culler service manually. It can be run as a standalone script anywhere with access to the Hub, and will periodically check for idle servers and shut them down via the Hub's REST API. In order to shutdown the servers, the token given to cull-idle must have admin privileges. Generate an API token and store it in the `JUPYTERHUB_API_TOKEN` environment -variable. Run `cull_idle_servers.py` manually. +variable. Run `jupyterhub_idle_culler` manually. ```bash export JUPYTERHUB_API_TOKEN='token' - python3 cull_idle_servers.py [--timeout=900] [--url=http://127.0.0.1:8081/hub/api] + python -m jupyterhub_idle_culler [--timeout=900] [--url=http://127.0.0.1:8081/hub/api] ``` -[cull_idle_servers]: https://github.com/jupyterhub/jupyterhub/blob/master/examples/cull-idle/cull_idle_servers.py +[jupyterhub_idle_culler]: https://github.com/jupyterhub/jupyterhub-idle-culler diff --git a/docs/source/index-about.rst b/docs/source/index-about.rst new file mode 100644 index 00000000..f0771c60 --- /dev/null +++ b/docs/source/index-about.rst @@ -0,0 +1,15 @@ +===== +About +===== + +JupyterHub is an open source project and community. It is a part of the +`Jupyter Project `_. JupyterHub is an open and inclusive +community, and invites contributions from anyone. This section covers information +about our community, as well as ways that you can connect and get involved. + +.. toctree:: + :maxdepth: 1 + + contributor-list + changelog + gallery-jhub-deployments diff --git a/docs/source/index-admin.rst b/docs/source/index-admin.rst new file mode 100644 index 00000000..f780d0ab --- /dev/null +++ b/docs/source/index-admin.rst @@ -0,0 +1,13 @@ +===================== +Administrator's Guide +===================== + +This guide covers best-practices, tips, common questions and operations, as +well as other information relevant to running your own JupyterHub over time. + +.. toctree:: + :maxdepth: 2 + + troubleshooting + admin/upgrading + changelog diff --git a/docs/source/index.rst b/docs/source/index.rst index db23641a..8d51a6bc 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -2,18 +2,18 @@ JupyterHub ========== -`JupyterHub`_ is the best way to serve `Jupyter notebook`_ for multiple users. -It can be used in a classes of students, a corporate data science group or scientific +`JupyterHub`_ is the best way to serve `Jupyter notebook`_ for multiple users. +It can be used in a class of students, a corporate data science group or scientific research group. It is a multi-user **Hub** that spawns, manages, and proxies multiple instances of the single-user `Jupyter notebook`_ server. -To make life easier, JupyterHub have distributions. Be sure to -take a look at them before continuing with the configuration of the broad -original system of `JupyterHub`_. Today, you can find two main cases: +To make life easier, JupyterHub has distributions. Be sure to +take a look at them before continuing with the configuration of the broad +original system of `JupyterHub`_. Today, you can find two main cases: -1. If you need a simple case for a small amount of users (0-100) and single server - take a look at - `The Littlest JupyterHub `__ distribution. +1. If you need a simple case for a small amount of users (0-100) and single server + take a look at + `The Littlest JupyterHub `__ distribution. 2. If you need to allow for even more users, a dynamic amount of servers can be used on a cloud, take a look at the `Zero to JupyterHub with Kubernetes `__ . @@ -72,46 +72,41 @@ Installation Guide ------------------ .. toctree:: - :maxdepth: 1 + :maxdepth: 2 installation-guide - quickstart - quickstart-docker - installation-basics Getting Started --------------- .. toctree:: - :maxdepth: 1 + :maxdepth: 2 getting-started/index - getting-started/config-basics - getting-started/networking-basics - getting-started/security-basics - getting-started/authenticators-users-basics - getting-started/spawners-basics - getting-started/services-basics Technical Reference ------------------- .. toctree:: - :maxdepth: 1 + :maxdepth: 2 reference/index - reference/technical-overview - reference/websecurity - reference/authenticators - reference/spawners - reference/services - reference/rest - reference/templates - reference/config-user-env - reference/config-examples - reference/config-ghoauth - reference/config-proxy - reference/config-sudo + +Administrators guide +-------------------- + +.. toctree:: + :maxdepth: 2 + + index-admin + +API Reference +------------- + +.. toctree:: + :maxdepth: 2 + + api/index Contributing ------------ @@ -125,51 +120,17 @@ Our `Code of Conduct `_ - `Jupyter website `_ -.. _contents: - -Full Table of Contents -====================== - -.. toctree:: - :maxdepth: 2 - - installation-guide - getting-started/index - reference/index - api/index - troubleshooting - contributor-list - gallery-jhub-deployments - changelog - - .. _JupyterHub: https://github.com/jupyterhub/jupyterhub .. _Jupyter notebook: https://jupyter-notebook.readthedocs.io/en/latest/ .. _REST API: http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyterhub/jupyterhub/master/docs/rest-api.yml#!/default diff --git a/docs/source/installation-guide-hard.md b/docs/source/installation-guide-hard.md new file mode 100644 index 00000000..f53e528a --- /dev/null +++ b/docs/source/installation-guide-hard.md @@ -0,0 +1,347 @@ +# Install JupyterHub and JupyterLab from the ground up + +The combination of [JupyterHub](https://jupyterhub.readthedocs.io) and [JupyterLab](https://jupyterlab.readthedocs.io) +is a great way to make shared computing resources available to a group. + +These instructions are a guide for a manual, 'bare metal' install of [JupyterHub](https://jupyterhub.readthedocs.io) +and [JupyterLab](https://jupyterlab.readthedocs.io). This is ideal for running on a single server: build a beast +of a machine and share it within your lab, or use a virtual machine from any VPS or cloud provider. + +This guide has similar goals to [The Littlest JupyterHub](https://the-littlest-jupyterhub.readthedocs.io) setup +script. However, instead of bundling all these step for you into one installer, we will perform every step manually. +This makes it easy to customize any part (e.g. if you want to run other services on the same system and need to make them +work together), as well as giving you full control and understanding of your setup. + + +## Prerequisites + +Your own server with administrator (root) access. This could be a local machine, a remotely hosted one, or a cloud instance +or VPS. Each user who will access JupyterHub should have a standard user account on the machine. The install will be done +through the command line - useful if you log into your machine remotely using SSH. + +This tutorial was tested on **Ubuntu 18.04**. No other Linux distributions have been tested, but the instructions +should be reasonably straightforward to adapt. + + +## Goals + +JupyterLab enables access to a multiple 'kernels', each one being a given environment for a given language. The most +common is a Python environment, for scientific computing usually one managed by the `conda` package manager. + +This guide will set up JupyterHub and JupyterLab seperately from the Python environment. In other words, we treat +JupyterHub+JupyterLab as a 'app' or webservice, which will connect to the kernels available on the system. Specifically: + +- We will create an installation of JupyterHub and JupyterLab using a virtualenv under `/opt` using the system Python. + +- We will install conda globally. + +- We will create a shared conda environment which can be used (but not modified) by all users. + +- We will show how users can create their own private conda environments, where they can install whatever they like. + + +The default JupyterHub Authenticator uses PAM to authenticate system users with their username and password. One can +[choose the authenticator](https://jupyterhub.readthedocs.io/en/stable/reference/authenticators.html#authenticators) +that best suits their needs. In this guide we will use the default Authenticator because it makes it easy for everyone to manage data +in their home folder and to mix and match different services and access methods (e.g. SSH) which all work using the +Linux system user accounts. Therefore, each user of JupyterHub will need a standard system user account. + +Another goal of this guide is to use system provided packages wherever possible. This has the advantage that these packages +get automatic patches and security updates (be sure to turn on automatic updates in Ubuntu). This means less maintenance +work and a more reliable system. + +## Part 1: JupyterHub and JupyterLab + +### Setup the JupyterHub and JupyterLab in a virtual environment + +First we create a virtual environment under '/opt/jupyterhub'. The '/opt' folder is where apps not belonging to the operating +system are [commonly installed](https://unix.stackexchange.com/questions/11544/what-is-the-difference-between-opt-and-usr-local). +Both jupyterlab and jupyterhub will be installed into this virtualenv. Create it with the command: + +```sh +sudo python3 -m venv /opt/jupyterhub/ +``` + +Now we use pip to install the required Python packages into the new virtual environment. Be sure to install +`wheel` first. Since we are separating the user interface from the computing kernels, we don't install +any Python scientific packages here. The only exception is `ipywidgets` because this is needed to allow connection +between interactive tools running in the kernel and the user interface. + +Note that we use `/opt/jupyterhub/bin/python3 -m pip install` each time - this [makes sure](https://snarky.ca/why-you-should-use-python-m-pip/) +that the packages are installed to the correct virtual environment. + +Perform the install using the following commands: + +```sh +sudo /opt/jupyterhub/bin/python3 -m pip install wheel +sudo /opt/jupyterhub/bin/python3 -m pip install jupyterhub jupyterlab +sudo /opt/jupyterhub/bin/python3 -m pip install ipywidgets +``` + +JupyterHub also currently defaults to requiring `configurable-http-proxy`, which needs `nodejs` and `npm`. The versions +of these available in Ubuntu therefore need to be installed first (they are a bit old but this is ok for our needs): + +```sh +sudo apt install nodejs npm +``` + +Then install `configurable-http-proxy`: + +```sh +sudo npm install -g configurable-http-proxy +``` + +### Create the configuration for JupyterHub + +Now we start creating configuration files. To keep everything together, we put all the configuration into the folder +created for the virtualenv, under `/opt/jupyterhub/etc/`. For each thing needing configuration, we will create a further +subfolder and necessary files. + +First create the folder for the JupyterHub configuration and navigate to it: + +```sh +sudo mkdir -p /opt/jupyterhub/etc/jupyterhub/ +cd /opt/jupyterhub/etc/jupyterhub/ +``` +Then generate the default configuration file + +```sh +sudo /opt/jupyterhub/bin/jupyterhub --generate-config +``` +This will produce the default configuration file `/opt/jupyterhub/etc/jupyterhub/jupyterhub_config.py` + +You will need to edit the configuration file to make the JupyterLab interface by the default. +Set the following configuration option in your `jupyterhub_config.py` file: + +```python +c.Spawner.default_url = '/lab' +``` + +Further configuration options may be found in the documentation. + +### Setup Systemd service + +We will setup JupyterHub to run as a system service using Systemd (which is responsible for managing all services and +servers that run on startup in Ubuntu). We will create a service file in a suitable location in the virtualenv folder +and then link it to the system services. First create the folder for the service file: + +```sh +sudo mkdir -p /opt/jupyterhub/etc/systemd +``` + +Then create the following text file using your [favourite editor](https://micro-editor.github.io/) at +```sh +/opt/jupyterhub/etc/systemd/jupyterhub.service +``` + +Paste the following service unit definition into the file: + +``` +[Unit] +Description=JupyterHub +After=syslog.target network.target + +[Service] +User=root +Environment="PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/jupyterhub/bin" +ExecStart=/opt/jupyterhub/bin/jupyterhub -f /opt/jupyterhub/etc/jupyterhub/jupyterhub_config.py + +[Install] +WantedBy=multi-user.target +``` + +This sets up the environment to use the virtual environment we created, tells Systemd how to start jupyterhub using +the configuration file we created, specifies that jupyterhub will be started as the `root` user (needed so that it can +start jupyter on behalf of other logged in users), and specifies that jupyterhub should start on boot after the network +is enabled. + +Finally, we need to make systemd aware of our service file. First we symlink our file into systemd's directory: + +```sh +sudo ln -s /opt/jupyterhub/etc/systemd/jupyterhub.service /etc/systemd/system/jupyterhub.service +``` + +Then tell systemd to reload its configuration files + +```sh +sudo systemctl daemon-reload +``` + +And finally enable the service + +```sh +sudo systemctl enable jupyterhub.service +``` + +The service will start on reboot, but we can start it straight away using: + +```sh +sudo systemctl start jupyterhub.service +``` + +...and check that it's running using: + +```sh +sudo systemctl status jupyterhub.service +``` + +You should now be already be able to access jupyterhub using `:8000` (assuming you haven't already set +up a firewall or something). However, when you log in the jupyter notebooks will be trying to use the Python virtualenv +that was created to install JupyterHub, this is not what we want. So on to part 2 + +## Part 2: Conda environments + +### Install conda for the whole system + +We will use `conda` to manage Python environments. We will install the officially maintained `conda` packages for Ubuntu, +this means they will get automatic updates with the rest of the system. Setup repo for the official Conda debian packages, +instructions are copied from [here](https://docs.conda.io/projects/conda/en/latest/user-guide/install/rpm-debian.html): + +Install Anacononda public gpg key to trusted store +```sh +curl https://repo.anaconda.com/pkgs/misc/gpgkeys/anaconda.asc | gpg --dearmor > conda.gpg +sudo install -o root -g root -m 644 conda.gpg /etc/apt/trusted.gpg.d/ +``` + +Add Debian repo + +```sh +echo "deb [arch=amd64] https://repo.anaconda.com/pkgs/misc/debrepo/conda stable main" | sudo tee /etc/apt/sources.list.d/conda.list +``` + +Install conda + +```sh +sudo apt update +sudo apt install conda +``` + +This will install conda into the folder `/opt/conda/`, with the conda command available at `/opt/conda/bin/conda`. + +Finally, we can make conda more easily available to users by symlinking the conda shell setup script to the profile +'drop in' folder so that it gets run on login + +```sh +sudo ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh +``` + +### Install a default conda environment for all users + +First create a folder for conda envs (might exist already): +```sh +sudo mkdir /opt/conda/envs/ +``` + +Then create a conda environment to your liking within that folder. Here we have called it 'python' because it will +be the obvious default - call it whatever you like. You can install whatever you like into this environment, but you MUST at least install `ipykernel`. + +```sh +sudo /opt/conda/bin/conda create --prefix /opt/conda/envs/python python=3.7 ipykernel +``` + +Once your env is set up as desired, make it visible to Jupyter by installing the kernel spec. There are two options here: + +1 ) Install into the JupyterHub virtualenv - this ensures it overrides the default python version. It will only be visible +to the JupyterHub installation we have just created. This is useful to avoid conda environments appearing where they are not expected. + +```sh +sudo /opt/conda/envs/python/bin/python -m ipykernel install --prefix=/opt/jupyterhub/ --name 'python' --display-name "Python (default)" +``` + +2 ) Install it system-wide by putting it into `/usr/local`. It will be visible to any parallel install of JupyterHub or +JupyterLab, and will persist even if you later delete or modify the JupyterHub installation. This is useful if the kernels +might be used by other services, or if you want to modify the JupyterHub installation independently from the conda environments. + +```sh +sudo /opt/conda/envs/python/bin/python -m ipykernel install --prefix /usr/local/ --name 'python' --display-name "Python (default)" +```` + +### Setting up users' own conda environments + +There is relatively little for the administrator to do here, as users will have to set up their own environments using the shell. +On login they should run `conda init` or `/opt/conda/bin/conda`. The can then use conda to set up their environment, +although they must also install `ipykernel`. Once done, they can enable their kernel using: + +```sh +/path/to/kernel/env/bin/python -m ipykernel install --name 'python-my-env' --display-name "Python My Env" +``` + +This will place the kernel spec into their home folder, where Jupyter will look for it on startup. + + +## Setting up a reverse proxy + +The guide so far results in JupyterHub running on port 8000. It is not generally advisable to run open web services in +this way - instead, use a reverse proxy running on standard HTTP/HTTPS ports. + +> **Important**: Be aware of the security implications especially if you are running a server that is accessible from the open internet +> i.e. not protected within an institutional intranet or private home/office network. You should set up a firewall and +> HTTPS encryption, which is outside of the scope of this guide. For HTTPS consider using [LetsEncrypt](https://letsencrypt.org/) +> or setting up a [self-signed certificate](https://www.digitalocean.com/community/tutorials/how-to-create-a-self-signed-ssl-certificate-for-nginx-in-ubuntu-18-04). +> Firewalls may be set up using `ufw` or `firewalld` and combined with `fail2ban`. + +### Using Nginx +Nginx is a mature and established web server and reverse proxy and is easy to install using `sudo apt install nginx`. +Details on using Nginx as a reverse proxy can be found elsewhere. Here, we will only outline the additional steps needed +to setup JupyterHub with Nginx and host it at a given URL e.g. `/jupyter`. +This could be useful for example if you are running several services or web pages on the same server. + +To achieve this needs a few tweaks to both the JupyterHub configuration and the Nginx config. First, edit the +configuration file `/opt/jupyterhub/etc/jupyterhub/jupyterhub_config.py` and add the line: + +```python +c.JupyterHub.bind_url = 'http://:8000/jupyter' +``` + +where `/jupyter` will be the relative URL of the JupyterHub. + +Now Nginx must be configured with a to pass all traffic from `/jupyter` to the the local address `127.0.0.1:8000`. +Add the following snippet to your nginx configuration file (e.g. `/etc/nginx/sites-available/default`). + +``` + location /jupyter/ { + # NOTE important to also set base url of jupyterhub to /jupyter in its config + proxy_pass http://127.0.0.1:8000; + + proxy_redirect off; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + + # websocket headers + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $connection_upgrade; + + } +``` + +Also add this snippet before the *server* block: + +``` +map $http_upgrade $connection_upgrade { + default upgrade; + '' close; + } +``` + +Nginx will not run if there are errors in the configuration, check your configuration using: + +```sh +nginx -t +``` + +If there are no errors, you can restart the Nginx service for the new configuration to take effect. + +```sh +sudo systemctl restart nginx.service +``` + + +## Getting started using your new JupyterHub + +Once you have setup JupyterHub and Nginx proxy as described, you can browse to your JupyterHub IP or URL +(e.g. if your server IP address is `123.456.789.1` and you decided to host JupyterHub at the `/jupyter` URL, browse +to `123.456.789.1/jupyter`). You will find a login page where you enter your Linux username and password. On login +you will be presented with the JupyterLab interface, with the file browser pane showing the contents of your users' +home directory on the server. diff --git a/docs/source/installation-guide.rst b/docs/source/installation-guide.rst index ccda8667..ad58488e 100644 --- a/docs/source/installation-guide.rst +++ b/docs/source/installation-guide.rst @@ -1,5 +1,9 @@ -Installation Guide -================== +Installation +============ + +These sections cover how to get up-and-running with JupyterHub. They cover +some basics of the tools needed to deploy JupyterHub as well as how to get it +running on your own infrastructure. .. toctree:: :maxdepth: 3 @@ -7,3 +11,4 @@ Installation Guide quickstart quickstart-docker installation-basics + installation-guide-hard diff --git a/docs/source/quickstart.md b/docs/source/quickstart.md index f9c4beaa..b621aab2 100644 --- a/docs/source/quickstart.md +++ b/docs/source/quickstart.md @@ -26,6 +26,10 @@ Before installing JupyterHub, you will need: The `nodejs-legacy` package installs the `node` executable and is currently required for npm to work on Debian/Ubuntu. +- A [pluggable authentication module (PAM)](https://en.wikipedia.org/wiki/Pluggable_authentication_module) + to use the [default Authenticator](./getting-started/authenticators-users-basics.md). + PAM is often available by default on most distributions, if this is not the case it can be installed by + using the operating system's package manager. - TLS certificate and key for HTTPS communication - Domain name diff --git a/docs/source/reference/config-ghoauth.md b/docs/source/reference/config-ghoauth.md index b120b3b2..6ec46e1c 100644 --- a/docs/source/reference/config-ghoauth.md +++ b/docs/source/reference/config-ghoauth.md @@ -52,7 +52,7 @@ c.GitHubOAuthenticator.oauth_callback_url = os.environ['OAUTH_CALLBACK_URL'] c.LocalAuthenticator.create_system_users = True # specify users and admin -c.Authenticator.whitelist = {'rgbkrk', 'minrk', 'jhamrick'} +c.Authenticator.allowed_users = {'rgbkrk', 'minrk', 'jhamrick'} c.Authenticator.admin_users = {'jhamrick', 'rgbkrk'} # uses the default spawner diff --git a/docs/source/reference/config-proxy.md b/docs/source/reference/config-proxy.md index 5decd697..cd56025c 100644 --- a/docs/source/reference/config-proxy.md +++ b/docs/source/reference/config-proxy.md @@ -17,10 +17,12 @@ satisfy the following: Let's start out with needed JupyterHub configuration in `jupyterhub_config.py`: ```python -# Force the proxy to only listen to connections to 127.0.0.1 -c.JupyterHub.ip = '127.0.0.1' +# Force the proxy to only listen to connections to 127.0.0.1 (on port 8000) +c.JupyterHub.bind_url = 'http://127.0.0.1:8000' ``` +(For Jupyterhub < 0.9 use `c.JupyterHub.ip = '127.0.0.1'`.) + For high-quality SSL configuration, we also generate Diffie-Helman parameters. This can take a few minutes: @@ -81,8 +83,11 @@ server { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # websocket headers + proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; + + proxy_buffering off; } # Managing requests to verify letsencrypt host @@ -137,6 +142,20 @@ Now restart `nginx`, restart the JupyterHub, and enjoy accessing `https://HUB.DOMAIN.TLD` while serving other content securely on `https://NO_HUB.DOMAIN.TLD`. +### SELinux permissions for nginx +On distributions with SELinux enabled (e.g. Fedora), one may encounter permission errors +when the nginx service is started. + +We need to allow nginx to perform network relay and connect to the jupyterhub port. The +following commands do that: + +```bash +semanage port -a -t http_port_t -p tcp 8000 +setsebool -P httpd_can_network_relay 1 +setsebool -P httpd_can_network_connect 1 +``` +Replace 8000 with the port the jupyterhub server is running from. + ## Apache @@ -197,8 +216,8 @@ In case of the need to run the jupyterhub under /jhub/ or other location please httpd.conf amendments: ```bash - RewriteRule /jhub/(.*) ws://127.0.0.1:8000/jhub/$1 [P,L] - RewriteRule /jhub/(.*) http://127.0.0.1:8000/jhub/$1 [P,L] + RewriteRule /jhub/(.*) ws://127.0.0.1:8000/jhub/$1 [NE.P,L] + RewriteRule /jhub/(.*) http://127.0.0.1:8000/jhub/$1 [NE,P,L] ProxyPass /jhub/ http://127.0.0.1:8000/jhub/ ProxyPassReverse /jhub/ http://127.0.0.1:8000/jhub/ diff --git a/docs/source/reference/config-reference.rst b/docs/source/reference/config-reference.rst new file mode 100644 index 00000000..22ee25cb --- /dev/null +++ b/docs/source/reference/config-reference.rst @@ -0,0 +1,30 @@ +============================== +Configuration Reference +============================== + +.. important:: + + Make sure the version of JupyterHub for this documentation matches your + installation version, as the output of this command may change between versions. + +JupyterHub configuration +------------------------ + +As explained in the `Configuration Basics <../getting-started/config-basics.html#generate-a-default-config-file>`_ +section, the ``jupyterhub_config.py`` can be automatically generated via + + .. code-block:: bash + + jupyterhub --generate-config + + +The following contains the output of that command for reference. + +.. jupyterhub-generate-config:: + +JupyterHub help command output +------------------------------ + +This section contains the output of the command ``jupyterhub --help-all``. + +.. jupyterhub-help-all:: diff --git a/docs/source/reference/config-sudo.md b/docs/source/reference/config-sudo.md index fc0d4865..cc0fdf9c 100644 --- a/docs/source/reference/config-sudo.md +++ b/docs/source/reference/config-sudo.md @@ -57,7 +57,7 @@ To do this we add to `/etc/sudoers` (use `visudo` for safe editing of sudoers): For example: ```bash -# comma-separated whitelist of users that can spawn single-user servers +# comma-separated list of users that can spawn single-user servers # this should include all of your Hub users Runas_Alias JUPYTER_USERS = rhea, zoe, wash @@ -120,6 +120,11 @@ the shadow password database. ### Shadow group (Linux) +**Note:** On Fedora based distributions there is no clear way to configure +the PAM database to allow sufficient access for authenticating with the target user's password +from JupyterHub. As a workaround we recommend use an +[alternative authentication method](https://github.com/jupyterhub/jupyterhub/wiki/Authenticators). + ```bash $ ls -l /etc/shadow -rw-r----- 1 root shadow 2197 Jul 21 13:41 shadow diff --git a/docs/source/reference/index.rst b/docs/source/reference/index.rst index 088d0ae8..ed478fa1 100644 --- a/docs/source/reference/index.rst +++ b/docs/source/reference/index.rst @@ -1,6 +1,9 @@ Technical Reference =================== +This section covers more of the details of the JupyterHub architecture, as well as +what happens under-the-hood when you deploy and configure your JupyterHub. + .. toctree:: :maxdepth: 2 @@ -13,10 +16,13 @@ Technical Reference proxy separate-proxy rest + monitoring database templates + ../events/index config-user-env config-examples config-ghoauth config-proxy config-sudo + config-reference diff --git a/docs/source/reference/monitoring.rst b/docs/source/reference/monitoring.rst new file mode 100644 index 00000000..774656ec --- /dev/null +++ b/docs/source/reference/monitoring.rst @@ -0,0 +1,20 @@ +Monitoring +========== + +This section covers details on monitoring the state of your JupyterHub installation. + +JupyterHub expose the ``/metrics`` endpoint that returns text describing its current +operational state formatted in a way `Prometheus `_ understands. + +Prometheus is a separate open source tool that can be configured to repeatedly poll +JupyterHub's ``/metrics`` endpoint to parse and save its current state. + +By doing so, Prometheus can describe JupyterHub's evolving state over time. +This evolving state can then be accessed through Prometheus that expose its underlying +storage to those allowed to access it, and be presented with dashboards by a +tool like `Grafana `_. + +.. toctree:: + :maxdepth: 2 + + metrics diff --git a/docs/source/reference/proxy.md b/docs/source/reference/proxy.md index cdc083cf..fd58816a 100644 --- a/docs/source/reference/proxy.md +++ b/docs/source/reference/proxy.md @@ -19,7 +19,7 @@ In general, for a proxy to be usable by JupyterHub, it must: 1. support websockets without prior knowledge of the URL where websockets may occur -2. support trie-based routing (i.e. allow different routes on `/foo` and +2. support trie-based routing (i.e. allow different routes on `/foo` and `/foo/bar` and route based on specificity) 3. adding or removing a route should not cause existing connections to drop @@ -62,7 +62,7 @@ Hub should call these methods when the Hub itself starts and stops. ## Encryption When using `internal_ssl` to encrypt traffic behind the proxy, at minimum, -your `Proxy` will need client ssl certificates which the `Hub` must be made +your `Proxy` will need client ssl certificates which the `Hub` must be made aware of. These can be generated with the command `jupyterhub --generate-certs` which will write them to the `internal_certs_location` in folders named `proxy_api` and `proxy_client`. Alternatively, these can be provided to the @@ -102,7 +102,7 @@ route to be proxied, such as `/user/name/`. A routespec will: ### Adding a route When adding a route, JupyterHub may pass a JSON-serializable dict as a `data` -argument that should be attacked to the proxy route. When that route is +argument that should be attached to the proxy route. When that route is retrieved, the `data` argument should be returned as well. If your proxy implementation doesn't support storing data attached to routes, then your Python wrapper may have to handle storing the `data` piece itself, e.g in a @@ -204,7 +204,7 @@ setup( ``` If you have added this metadata to your package, -users can select your authenticator with the configuration: +users can select your proxy with the configuration: ```python c.JupyterHub.proxy_class = 'mything' diff --git a/docs/source/reference/rest.md b/docs/source/reference/rest.md index 95e9ea8b..0432f677 100644 --- a/docs/source/reference/rest.md +++ b/docs/source/reference/rest.md @@ -57,6 +57,9 @@ generating an API token is available from the JupyterHub user interface: ## Add API tokens to the config file +**This is deprecated. We are in no rush to remove this feature, +but please consider if service tokens are right for you.** + You may also add a dictionary of API tokens and usernames to the hub's configuration file, `jupyterhub_config.py` (note that the **key** is the 'secret-token' while the **value** is the 'username'): @@ -67,6 +70,41 @@ c.JupyterHub.api_tokens = { } ``` +### Updating to admin services + +The `api_tokens` configuration has been softly deprecated since the introduction of services. +We have no plans to remove it, +but users are encouraged to use service configuration instead. + +If you have been using `api_tokens` to create an admin user +and a token for that user to perform some automations, +the services mechanism may be a better fit. +If you have the following configuration: + +```python +c.JupyterHub.admin_users = {"service-admin",} +c.JupyterHub.api_tokens = { + "secret-token": "service-admin", +} +``` + +This can be updated to create an admin service, with the following configuration: + +```python +c.JupyterHub.services = [ + { + "name": "service-token", + "admin": True, + "api_token": "secret-token", + }, +] +``` + +The token will have the same admin permissions, +but there will no longer be a user account created to house it. +The main noticeable difference is that there will be no notebook server associated with the account +and the service will not show up in the various user list pages and APIs. + ## Make an API request To authenticate your requests, pass the API token in the request's diff --git a/docs/source/reference/services.md b/docs/source/reference/services.md index 24de3bdd..77d6cb25 100644 --- a/docs/source/reference/services.md +++ b/docs/source/reference/services.md @@ -151,6 +151,8 @@ c.JupyterHub.services = [ { 'name': 'my-web-service', 'url': 'https://10.0.1.1:1984', + # any secret >8 characters, you'll use api_token to + # authenticate api requests to the hub from your service 'api_token': 'super-secret', } ] @@ -249,7 +251,7 @@ prefix = os.environ.get('JUPYTERHUB_SERVICE_PREFIX', '/') auth = HubAuth( api_token=os.environ['JUPYTERHUB_API_TOKEN'], - cookie_cache_max_age=60, + cache_max_age=60, ) app = Flask(__name__) @@ -313,7 +315,7 @@ class MyHandler(HubAuthenticated, web.RequestHandler): The HubAuth will automatically load the desired configuration from the Service environment variables. -If you want to limit user access, you can whitelist users through either the +If you want to limit user access, you can specify allowed users through either the `.hub_users` attribute or `.hub_groups`. These are sets that check against the username and user group list, respectively. If a user matches neither the user list nor the group list, they will not be allowed access. If both are left @@ -331,7 +333,9 @@ and taking note of the following process: 1. retrieve the cookie `jupyterhub-services` from the request. 2. Make an API request `GET /hub/api/authorizations/cookie/jupyterhub-services/cookie-value`, where cookie-value is the url-encoded value of the `jupyterhub-services` cookie. - This request must be authenticated with a Hub API token in the `Authorization` header. + This request must be authenticated with a Hub API token in the `Authorization` header, + for example using the `api_token` from your [external service's configuration](#externally-managed-services). + For example, with [requests][]: ```python @@ -360,7 +364,7 @@ and taking note of the following process: An example of using an Externally-Managed Service and authentication is in [nbviewer README][nbviewer example] section on securing the notebook viewer, -and an example of its configuration is found [here](https://github.com/jupyter/nbviewer/blob/master/nbviewer/providers/base.py#L94). +and an example of its configuration is found [here](https://github.com/jupyter/nbviewer/blob/ed942b10a52b6259099e2dd687930871dc8aac22/nbviewer/providers/base.py#L95). nbviewer can also be run as a Hub-Managed Service as described [nbviewer README][nbviewer example] section on securing the notebook viewer. diff --git a/docs/source/reference/spawners.md b/docs/source/reference/spawners.md index 7ecc28c8..78a78d3f 100644 --- a/docs/source/reference/spawners.md +++ b/docs/source/reference/spawners.md @@ -25,10 +25,10 @@ Some examples include: run without being root, by spawning an intermediate process via `sudo` - [BatchSpawner](https://github.com/jupyterhub/batchspawner) for spawning remote servers using batch systems -- [YarnSpawner](https://github.com/jcrist/yarnspawner) for spawning notebook +- [YarnSpawner](https://github.com/jupyterhub/yarnspawner) for spawning notebook servers in YARN containers on a Hadoop cluster -- [RemoteSpawner](https://github.com/zonca/remotespawner) to spawn notebooks - and a remote server and tunnel the port via SSH +- [SSHSpawner](https://github.com/NERSC/sshspawner) to spawn notebooks + on a remote server using SSH ## Spawner control methods @@ -74,7 +74,7 @@ It should return `None` if it is still running, and an integer exit status, otherwise. For the local process case, `Spawner.poll` uses `os.kill(PID, 0)` -to check if the local process is still running. +to check if the local process is still running. On Windows, it uses `psutil.pid_exists`. ### Spawner.stop @@ -195,7 +195,7 @@ setup( ``` If you have added this metadata to your package, -users can select your authenticator with the configuration: +users can select your spawner with the configuration: ```python c.JupyterHub.spawner_class = 'myservice' diff --git a/docs/source/reference/templates.md b/docs/source/reference/templates.md index 61784403..d820c099 100644 --- a/docs/source/reference/templates.md +++ b/docs/source/reference/templates.md @@ -70,7 +70,7 @@ To add announcements to be displayed on a page, you have two options: ### Announcement Configuration Variables If you set the configuration variable `JupyterHub.template_vars = -{'announcement': 'some_text}`, the given `some_text` will be placed on +{'announcement': 'some_text'}`, the given `some_text` will be placed on the top of all pages. The more specific variables `announcement_login`, `announcement_spawn`, `announcement_home`, and `announcement_logout` are more specific and only show on their diff --git a/docs/source/reference/websecurity.md b/docs/source/reference/websecurity.md index ccdc616b..b9b1df68 100644 --- a/docs/source/reference/websecurity.md +++ b/docs/source/reference/websecurity.md @@ -76,7 +76,7 @@ resolves the cross-site issues. ### Disable user config -If subdomains are not available or not desirable, JupyterHub provides a a +If subdomains are not available or not desirable, JupyterHub provides a configuration option `Spawner.disable_user_config`, which can be set to prevent the user-owned configuration files from being loaded. After implementing this option, PATHs and package installation and PATHs are the other things that the diff --git a/docs/source/troubleshooting.md b/docs/source/troubleshooting.md index 270a26de..3958e590 100644 --- a/docs/source/troubleshooting.md +++ b/docs/source/troubleshooting.md @@ -7,8 +7,8 @@ problem and how to resolve it. [*Behavior*](#behavior) - JupyterHub proxy fails to start - sudospawner fails to run -- What is the default behavior when none of the lists (admin, whitelist, - group whitelist) are set? +- What is the default behavior when none of the lists (admin, allowed, + allowed groups) are set? - JupyterHub Docker container not accessible at localhost [*Errors*](#errors) @@ -55,14 +55,14 @@ or add: to the config file, `jupyterhub_config.py`. -### What is the default behavior when none of the lists (admin, whitelist, group whitelist) are set? +### What is the default behavior when none of the lists (admin, allowed, allowed groups) are set? When nothing is given for these lists, there will be no admins, and all users who can authenticate on the system (i.e. all the unix users on the server with -a password) will be allowed to start a server. The whitelist lets you limit -this to a particular set of users, and the admin_users lets you specify who +a password) will be allowed to start a server. The allowed username set lets you limit +this to a particular set of users, and admin_users lets you specify who among them may use the admin interface (not necessary, unless you need to do -things like inspect other users' servers, or modify the userlist at runtime). +things like inspect other users' servers, or modify the user list at runtime). ### JupyterHub Docker container not accessible at localhost @@ -75,6 +75,50 @@ tell Jupyterhub to start at `0.0.0.0` which is visible to everyone. Try this command: `docker run -p 8000:8000 -d --name jupyterhub jupyterhub/jupyterhub jupyterhub --ip 0.0.0.0 --port 8000` +### How can I kill ports from JupyterHub managed services that have been orphaned? + +I started JupyterHub + nbgrader on the same host without containers. When I try to restart JupyterHub + nbgrader with this configuration, errors appear that the service accounts cannot start because the ports are being used. + +How can I kill the processes that are using these ports? + +Run the following command: + + sudo kill -9 $(sudo lsof -t -i:) + +Where `` is the port used by the nbgrader course service. This configuration is specified in `jupyterhub_config.py`. + +### Why am I getting a Spawn failed error message? + +After successfully logging in to JupyterHub with a compatible authenticators, I get a 'Spawn failed' error message in the browser. The JupyterHub logs have `jupyterhub KeyError: "getpwnam(): name not found: `. + +This issue occurs when the authenticator requires a local system user to exist. In these cases, you need to use a spawner +that does not require an existing system user account, such as `DockerSpawner` or `KubeSpawner`. + +### How can I run JupyterHub with sudo but use my current env vars and virtualenv location? + +When launching JupyterHub with `sudo jupyterhub` I get import errors and my environment variables don't work. + +When launching services with `sudo ...` the shell won't have the same environment variables or `PATH`s in place. The most direct way to solve this issue is to use the full path to your python environment and add environment variables. For example: + +```bash +sudo MY_ENV=abc123 \ + /home/foo/venv/bin/python3 \ + /srv/jupyterhub/jupyterhub +``` + +### How can I view the logs for JupyterHub or the user's Notebook servers when using the DockerSpawner? + +Use `docker logs ` where `` is the container name defined within `docker-compose.yml`. For example, to view the logs of the JupyterHub container use: + + docker logs hub + +By default, the user's notebook server is named `jupyter-` where `username` is the user's username within JupyterHub's db. So if you wanted to see the logs for user `foo` you would use: + + docker logs jupyter-foo + +You can also tail logs to view them in real time using the `-f` option: + + docker logs -f hub ## Errors @@ -108,7 +152,7 @@ You should see a similar 200 message, as above, in the Hub log when you first visit your single-user notebook server. If you don't see this message in the log, it may mean that your single-user notebook server isn't connecting to your Hub. -If you see 403 (forbidden) like this, it's a token problem: +If you see 403 (forbidden) like this, it's likely a token problem: ``` 403 GET /hub/api/authorizations/cookie/jupyterhub-token-name/[secret] (@10.0.1.4) 4.14ms @@ -152,6 +196,42 @@ After this, when you start your server via JupyterHub, it will build a new container. If this was the underlying cause of the issue, you should see your server again. +##### Proxy settings (403 GET) + +When your whole JupyterHub sits behind a organization proxy (*not* a reverse proxy like NGINX as part of your setup and *not* the configurable-http-proxy) the environment variables `HTTP_PROXY`, `HTTPS_PROXY`, `http_proxy` and `https_proxy` might be set. This confuses the jupyterhub-singleuser servers: When connecting to the Hub for authorization they connect via the proxy instead of directly connecting to the Hub on localhost. The proxy might deny the request (403 GET). This results in the singleuser server thinking it has a wrong auth token. To circumvent this you should add `,,localhost,127.0.0.1` to the environment variables `NO_PROXY` and `no_proxy`. + +### Launching Jupyter Notebooks to run as an externally managed JupyterHub service with the `jupyterhub-singleuser` command returns a `JUPYTERHUB_API_TOKEN` error + +[JupyterHub services](https://jupyterhub.readthedocs.io/en/stable/reference/services.html) allow processes to interact with JupyterHub's REST API. Example use-cases include: + +* **Secure Testing**: provide a canonical Jupyter Notebook for testing production data to reduce the number of entry points into production systems. +* **Grading Assignments**: provide access to shared Jupyter Notebooks that may be used for management tasks such grading assignments. +* **Private Dashboards**: share dashboards with certain group members. + +If possible, try to run the Jupyter Notebook as an externally managed service with one of the provided [jupyter/docker-stacks](https://github.com/jupyter/docker-stacks). + +Standard JupyterHub installations include a [jupyterhub-singleuser](https://github.com/jupyterhub/jupyterhub/blob/9fdab027daa32c9017845572ad9d5ba1722dbc53/setup.py#L116) command which is built from the `jupyterhub.singleuser:main` method. The `jupyterhub-singleuser` command is the default command when JupyterHub launches single-user Jupyter Notebooks. One of the goals of this command is to make sure the version of JupyterHub installed within the Jupyter Notebook coincides with the version of the JupyterHub server itself. + +If you launch a Jupyter Notebook with the `jupyterhub-singleuser` command directly from the command line the Jupyter Notebook won't have access to the `JUPYTERHUB_API_TOKEN` and will return: + +``` + JUPYTERHUB_API_TOKEN env is required to run jupyterhub-singleuser. + Did you launch it manually? +``` + +If you plan on testing `jupyterhub-singleuser` independently from JupyterHub, then you can set the api token environment variable. For example, if were to run the single-user Jupyter Notebook on the host, then: + + export JUPYTERHUB_API_TOKEN=my_secret_token + jupyterhub-singleuser + +With a docker container, pass in the environment variable with the run command: + + docker run -d \ + -p 8888:8888 \ + -e JUPYTERHUB_API_TOKEN=my_secret_token \ + jupyter/datascience-notebook:latest + +[This example](https://github.com/jupyterhub/jupyterhub/tree/master/examples/service-notebook/external) demonstrates how to combine the use of the `jupyterhub-singleuser` environment variables when launching a Notebook as an externally managed service. ## How do I...? @@ -193,7 +273,7 @@ where `ssl_cert` is example-chained.crt and ssl_key to your private key. Then restart JupyterHub. -See also [JupyterHub SSL encryption](getting-started.md#ssl-encryption). +See also [JupyterHub SSL encryption](./getting-started/security-basics.html#ssl-encryption). ### Install JupyterHub without a network connection @@ -252,8 +332,7 @@ notebook servers to default to JupyterLab: ### How do I set up JupyterHub for a workshop (when users are not known ahead of time)? 1. Set up JupyterHub using OAuthenticator for GitHub authentication -2. Configure whitelist to be an empty list in` jupyterhub_config.py` -3. Configure admin list to have workshop leaders be listed with administrator privileges. +2. Configure admin list to have workshop leaders be listed with administrator privileges. Users will need a GitHub account to login and be authenticated by the Hub. @@ -281,7 +360,6 @@ Or use syslog: jupyterhub | logger -t jupyterhub - ## Troubleshooting commands The following commands provide additional detail about installed packages, diff --git a/docs/sphinxext/autodoc_traits.py b/docs/sphinxext/autodoc_traits.py deleted file mode 100644 index 3d54f8bb..00000000 --- a/docs/sphinxext/autodoc_traits.py +++ /dev/null @@ -1,57 +0,0 @@ -"""autodoc extension for configurable traits""" -from sphinx.domains.python import PyClassmember -from sphinx.ext.autodoc import AttributeDocumenter -from sphinx.ext.autodoc import ClassDocumenter -from traitlets import TraitType -from traitlets import Undefined - - -class ConfigurableDocumenter(ClassDocumenter): - """Specialized Documenter subclass for traits with config=True""" - - objtype = 'configurable' - directivetype = 'class' - - def get_object_members(self, want_all): - """Add traits with .tag(config=True) to members list""" - check, members = super().get_object_members(want_all) - get_traits = ( - self.object.class_own_traits - if self.options.inherited_members - else self.object.class_traits - ) - trait_members = [] - for name, trait in sorted(get_traits(config=True).items()): - # put help in __doc__ where autodoc will look for it - trait.__doc__ = trait.help - trait_members.append((name, trait)) - return check, trait_members + members - - -class TraitDocumenter(AttributeDocumenter): - objtype = 'trait' - directivetype = 'attribute' - member_order = 1 - priority = 100 - - @classmethod - def can_document_member(cls, member, membername, isattr, parent): - return isinstance(member, TraitType) - - def add_directive_header(self, sig): - default = self.object.get_default_value() - if default is Undefined: - default_s = '' - else: - default_s = repr(default) - self.options.annotation = 'c.{name} = {trait}({default})'.format( - name=self.format_name(), - trait=self.object.__class__.__name__, - default=default_s, - ) - super().add_directive_header(sig) - - -def setup(app): - app.add_autodocumenter(ConfigurableDocumenter) - app.add_autodocumenter(TraitDocumenter) diff --git a/examples/cull-idle/README.md b/examples/cull-idle/README.md index 9f043e05..005f15a8 100644 --- a/examples/cull-idle/README.md +++ b/examples/cull-idle/README.md @@ -1,41 +1,4 @@ -# `cull-idle` Example +# idle-culler example -The `cull_idle_servers.py` file provides a script to cull and shut down idle -single-user notebook servers. This script is used when `cull-idle` is run as -a Service or when it is run manually as a standalone script. - - -## Configure `cull-idle` to run as a Hub-Managed Service - -In `jupyterhub_config.py`, add the following dictionary for the `cull-idle` -Service to the `c.JupyterHub.services` list: - -```python -c.JupyterHub.services = [ - { - 'name': 'cull-idle', - 'admin': True, - 'command': [sys.executable, 'cull_idle_servers.py', '--timeout=3600'], - } -] -``` - -where: - -- `'admin': True` indicates that the Service has 'admin' permissions, and -- `'command'` indicates that the Service will be managed by the Hub. - -## Run `cull-idle` manually as a standalone script - -This will run `cull-idle` manually. `cull-idle` can be run as a standalone -script anywhere with access to the Hub, and will periodically check for idle -servers and shut them down via the Hub's REST API. In order to shutdown the -servers, the token given to cull-idle must have admin privileges. - -Generate an API token and store it in the `JUPYTERHUB_API_TOKEN` environment -variable. Run `cull_idle_servers.py` manually. - -```bash - export JUPYTERHUB_API_TOKEN=$(jupyterhub token) - python3 cull_idle_servers.py [--timeout=900] [--url=http://127.0.0.1:8081/hub/api] -``` +The idle culler has been moved to its own repository at +[jupyterhub/jupyterhub-idle-culler](https://github.com/jupyterhub/jupyterhub-idle-culler). diff --git a/examples/cull-idle/cull_idle_servers.py b/examples/cull-idle/cull_idle_servers.py deleted file mode 100755 index 5740063c..00000000 --- a/examples/cull-idle/cull_idle_servers.py +++ /dev/null @@ -1,385 +0,0 @@ -#!/usr/bin/env python3 -"""script to monitor and cull idle single-user servers - -Caveats: - -last_activity is not updated with high frequency, -so cull timeout should be greater than the sum of: - -- single-user websocket ping interval (default: 30s) -- JupyterHub.last_activity_interval (default: 5 minutes) - -You can run this as a service managed by JupyterHub with this in your config:: - - - c.JupyterHub.services = [ - { - 'name': 'cull-idle', - 'admin': True, - 'command': [sys.executable, 'cull_idle_servers.py', '--timeout=3600'], - } - ] - -Or run it manually by generating an API token and storing it in `JUPYTERHUB_API_TOKEN`: - - export JUPYTERHUB_API_TOKEN=$(jupyterhub token) - python3 cull_idle_servers.py [--timeout=900] [--url=http://127.0.0.1:8081/hub/api] - -This script uses the same ``--timeout`` and ``--max-age`` values for -culling users and users' servers. If you want a different value for -users and servers, you should add this script to the services list -twice, just with different ``name``s, different values, and one with -the ``--cull-users`` option. -""" -import json -import os -from datetime import datetime -from datetime import timezone -from functools import partial - -try: - from urllib.parse import quote -except ImportError: - from urllib import quote - -import dateutil.parser - -from tornado.gen import coroutine, multi -from tornado.locks import Semaphore -from tornado.log import app_log -from tornado.httpclient import AsyncHTTPClient, HTTPRequest -from tornado.ioloop import IOLoop, PeriodicCallback -from tornado.options import define, options, parse_command_line - - -def parse_date(date_string): - """Parse a timestamp - - If it doesn't have a timezone, assume utc - - Returned datetime object will always be timezone-aware - """ - dt = dateutil.parser.parse(date_string) - if not dt.tzinfo: - # assume naïve timestamps are UTC - dt = dt.replace(tzinfo=timezone.utc) - return dt - - -def format_td(td): - """ - Nicely format a timedelta object - - as HH:MM:SS - """ - if td is None: - return "unknown" - if isinstance(td, str): - return td - seconds = int(td.total_seconds()) - h = seconds // 3600 - seconds = seconds % 3600 - m = seconds // 60 - seconds = seconds % 60 - return "{h:02}:{m:02}:{seconds:02}".format(h=h, m=m, seconds=seconds) - - -@coroutine -def cull_idle( - url, api_token, inactive_limit, cull_users=False, max_age=0, concurrency=10 -): - """Shutdown idle single-user servers - - If cull_users, inactive *users* will be deleted as well. - """ - auth_header = {'Authorization': 'token %s' % api_token} - req = HTTPRequest(url=url + '/users', headers=auth_header) - now = datetime.now(timezone.utc) - client = AsyncHTTPClient() - - if concurrency: - semaphore = Semaphore(concurrency) - - @coroutine - def fetch(req): - """client.fetch wrapped in a semaphore to limit concurrency""" - yield semaphore.acquire() - try: - return (yield client.fetch(req)) - finally: - yield semaphore.release() - - else: - fetch = client.fetch - - resp = yield fetch(req) - users = json.loads(resp.body.decode('utf8', 'replace')) - futures = [] - - @coroutine - def handle_server(user, server_name, server): - """Handle (maybe) culling a single server - - Returns True if server is now stopped (user removable), - False otherwise. - """ - log_name = user['name'] - if server_name: - log_name = '%s/%s' % (user['name'], server_name) - if server.get('pending'): - app_log.warning( - "Not culling server %s with pending %s", log_name, server['pending'] - ) - return False - - # jupyterhub < 0.9 defined 'server.url' once the server was ready - # as an *implicit* signal that the server was ready. - # 0.9 adds a dedicated, explicit 'ready' field. - # By current (0.9) definitions, servers that have no pending - # events and are not ready shouldn't be in the model, - # but let's check just to be safe. - - if not server.get('ready', bool(server['url'])): - app_log.warning( - "Not culling not-ready not-pending server %s: %s", log_name, server - ) - return False - - if server.get('started'): - age = now - parse_date(server['started']) - else: - # started may be undefined on jupyterhub < 0.9 - age = None - - # check last activity - # last_activity can be None in 0.9 - if server['last_activity']: - inactive = now - parse_date(server['last_activity']) - else: - # no activity yet, use start date - # last_activity may be None with jupyterhub 0.9, - # which introduces the 'started' field which is never None - # for running servers - inactive = age - - should_cull = ( - inactive is not None and inactive.total_seconds() >= inactive_limit - ) - if should_cull: - app_log.info( - "Culling server %s (inactive for %s)", log_name, format_td(inactive) - ) - - if max_age and not should_cull: - # only check started if max_age is specified - # so that we can still be compatible with jupyterhub 0.8 - # which doesn't define the 'started' field - if age is not None and age.total_seconds() >= max_age: - app_log.info( - "Culling server %s (age: %s, inactive for %s)", - log_name, - format_td(age), - format_td(inactive), - ) - should_cull = True - - if not should_cull: - app_log.debug( - "Not culling server %s (age: %s, inactive for %s)", - log_name, - format_td(age), - format_td(inactive), - ) - return False - - if server_name: - # culling a named server - delete_url = url + "/users/%s/servers/%s" % ( - quote(user['name']), - quote(server['name']), - ) - else: - delete_url = url + '/users/%s/server' % quote(user['name']) - - req = HTTPRequest(url=delete_url, method='DELETE', headers=auth_header) - resp = yield fetch(req) - if resp.code == 202: - app_log.warning("Server %s is slow to stop", log_name) - # return False to prevent culling user with pending shutdowns - return False - return True - - @coroutine - def handle_user(user): - """Handle one user. - - Create a list of their servers, and async exec them. Wait for - that to be done, and if all servers are stopped, possibly cull - the user. - """ - # shutdown servers first. - # Hub doesn't allow deleting users with running servers. - # jupyterhub 0.9 always provides a 'servers' model. - # 0.8 only does this when named servers are enabled. - if 'servers' in user: - servers = user['servers'] - else: - # jupyterhub < 0.9 without named servers enabled. - # create servers dict with one entry for the default server - # from the user model. - # only if the server is running. - servers = {} - if user['server']: - servers[''] = { - 'last_activity': user['last_activity'], - 'pending': user['pending'], - 'url': user['server'], - } - server_futures = [ - handle_server(user, server_name, server) - for server_name, server in servers.items() - ] - results = yield multi(server_futures) - if not cull_users: - return - # some servers are still running, cannot cull users - still_alive = len(results) - sum(results) - if still_alive: - app_log.debug( - "Not culling user %s with %i servers still alive", - user['name'], - still_alive, - ) - return False - - should_cull = False - if user.get('created'): - age = now - parse_date(user['created']) - else: - # created may be undefined on jupyterhub < 0.9 - age = None - - # check last activity - # last_activity can be None in 0.9 - if user['last_activity']: - inactive = now - parse_date(user['last_activity']) - else: - # no activity yet, use start date - # last_activity may be None with jupyterhub 0.9, - # which introduces the 'created' field which is never None - inactive = age - - should_cull = ( - inactive is not None and inactive.total_seconds() >= inactive_limit - ) - if should_cull: - app_log.info("Culling user %s (inactive for %s)", user['name'], inactive) - - if max_age and not should_cull: - # only check created if max_age is specified - # so that we can still be compatible with jupyterhub 0.8 - # which doesn't define the 'started' field - if age is not None and age.total_seconds() >= max_age: - app_log.info( - "Culling user %s (age: %s, inactive for %s)", - user['name'], - format_td(age), - format_td(inactive), - ) - should_cull = True - - if not should_cull: - app_log.debug( - "Not culling user %s (created: %s, last active: %s)", - user['name'], - format_td(age), - format_td(inactive), - ) - return False - - req = HTTPRequest( - url=url + '/users/%s' % user['name'], method='DELETE', headers=auth_header - ) - yield fetch(req) - return True - - for user in users: - futures.append((user['name'], handle_user(user))) - - for (name, f) in futures: - try: - result = yield f - except Exception: - app_log.exception("Error processing %s", name) - else: - if result: - app_log.debug("Finished culling %s", name) - - -if __name__ == '__main__': - define( - 'url', - default=os.environ.get('JUPYTERHUB_API_URL'), - help="The JupyterHub API URL", - ) - define('timeout', default=600, help="The idle timeout (in seconds)") - define( - 'cull_every', - default=0, - help="The interval (in seconds) for checking for idle servers to cull", - ) - define( - 'max_age', - default=0, - help="The maximum age (in seconds) of servers that should be culled even if they are active", - ) - define( - 'cull_users', - default=False, - help="""Cull users in addition to servers. - This is for use in temporary-user cases such as tmpnb.""", - ) - define( - 'concurrency', - default=10, - help="""Limit the number of concurrent requests made to the Hub. - - Deleting a lot of users at the same time can slow down the Hub, - so limit the number of API requests we have outstanding at any given time. - """, - ) - - parse_command_line() - if not options.cull_every: - options.cull_every = options.timeout // 2 - api_token = os.environ['JUPYTERHUB_API_TOKEN'] - - try: - AsyncHTTPClient.configure("tornado.curl_httpclient.CurlAsyncHTTPClient") - except ImportError as e: - app_log.warning( - "Could not load pycurl: %s\n" - "pycurl is recommended if you have a large number of users.", - e, - ) - - loop = IOLoop.current() - cull = partial( - cull_idle, - url=options.url, - api_token=api_token, - inactive_limit=options.timeout, - cull_users=options.cull_users, - max_age=options.max_age, - concurrency=options.concurrency, - ) - # schedule first cull immediately - # because PeriodicCallback doesn't start until the end of the first interval - loop.add_callback(cull) - # schedule periodic cull - pc = PeriodicCallback(cull, 1e3 * options.cull_every) - pc.start() - try: - loop.start() - except KeyboardInterrupt: - pass diff --git a/examples/cull-idle/jupyterhub_config.py b/examples/cull-idle/jupyterhub_config.py deleted file mode 100644 index 29fdc2d6..00000000 --- a/examples/cull-idle/jupyterhub_config.py +++ /dev/null @@ -1,11 +0,0 @@ -import sys - -# run cull-idle as a service - -c.JupyterHub.services = [ - { - 'name': 'cull-idle', - 'admin': True, - 'command': [sys.executable, 'cull_idle_servers.py', '--timeout=3600'], - } -] diff --git a/examples/external-oauth/whoami-oauth-basic.py b/examples/external-oauth/whoami-oauth-basic.py index ad98115c..2aca9f55 100644 --- a/examples/external-oauth/whoami-oauth-basic.py +++ b/examples/external-oauth/whoami-oauth-basic.py @@ -5,13 +5,11 @@ so all URLs and requests necessary for OAuth with JupyterHub should be in one pl """ import json import os -import sys from urllib.parse import urlencode from urllib.parse import urlparse from tornado import log from tornado import web -from tornado.auth import OAuth2Mixin from tornado.httpclient import AsyncHTTPClient from tornado.httpclient import HTTPRequest from tornado.httputil import url_concat diff --git a/examples/service-announcement/announcement.py b/examples/service-announcement/announcement.py index 2b140fdb..a0cf5964 100644 --- a/examples/service-announcement/announcement.py +++ b/examples/service-announcement/announcement.py @@ -4,7 +4,6 @@ import json import os from tornado import escape -from tornado import gen from tornado import ioloop from tornado import web diff --git a/examples/service-whoami-flask/jupyterhub_config.py b/examples/service-whoami-flask/jupyterhub_config.py index 54d3c736..63ddaa38 100644 --- a/examples/service-whoami-flask/jupyterhub_config.py +++ b/examples/service-whoami-flask/jupyterhub_config.py @@ -1,6 +1,3 @@ -import os -import sys - c.JupyterHub.services = [ { 'name': 'whoami', diff --git a/examples/service-whoami/jupyterhub_config.py b/examples/service-whoami/jupyterhub_config.py index d9ccd889..1d1f9435 100644 --- a/examples/service-whoami/jupyterhub_config.py +++ b/examples/service-whoami/jupyterhub_config.py @@ -1,4 +1,3 @@ -import os import sys c.JupyterHub.services = [ diff --git a/examples/service-whoami/whoami-oauth.py b/examples/service-whoami/whoami-oauth.py index c1a576c9..72c97dda 100644 --- a/examples/service-whoami/whoami-oauth.py +++ b/examples/service-whoami/whoami-oauth.py @@ -6,7 +6,6 @@ showing the user their own info. """ import json import os -from getpass import getuser from urllib.parse import urlparse from tornado.httpserver import HTTPServer @@ -25,6 +24,7 @@ class WhoAmIHandler(HubOAuthenticated, RequestHandler): # `getuser()` here would mean only the user who started the service # can access the service: + # from getpass import getuser # hub_users = {getuser()} @authenticated diff --git a/examples/service-whoami/whoami.py b/examples/service-whoami/whoami.py index 6dc56c9e..2a5a3373 100644 --- a/examples/service-whoami/whoami.py +++ b/examples/service-whoami/whoami.py @@ -4,7 +4,6 @@ This serves `/services/whoami/`, authenticated with the Hub, showing the user th """ import json import os -from getpass import getuser from urllib.parse import urlparse from tornado.httpserver import HTTPServer @@ -21,6 +20,7 @@ class WhoAmIHandler(HubAuthenticated, RequestHandler): # `getuser()` here would mean only the user who started the service # can access the service: + # from getpass import getuser # hub_users = {getuser()} @authenticated diff --git a/examples/spawn-form/jupyterhub_config.py b/examples/spawn-form/jupyterhub_config.py index ff7c2526..58fe59dc 100644 --- a/examples/spawn-form/jupyterhub_config.py +++ b/examples/spawn-form/jupyterhub_config.py @@ -10,10 +10,15 @@ class DemoFormSpawner(LocalProcessSpawner): def _options_form_default(self): default_env = "YOURNAME=%s\n" % self.user.name return """ - - - - +
+ + +
+
+ + +
""".format( env=default_env ) diff --git a/hooks/post_build b/hooks/post_build index 874fd6d5..70d712e2 100755 --- a/hooks/post_build +++ b/hooks/post_build @@ -1,4 +1,7 @@ #!/bin/bash set -exuo pipefail +# build jupyterhub-onbuild image docker build --build-arg BASE_IMAGE=$DOCKER_REPO:$DOCKER_TAG -t ${DOCKER_REPO}-onbuild:$DOCKER_TAG onbuild +# build jupyterhub-demo image +docker build --build-arg BASE_IMAGE=${DOCKER_REPO}-onbuild:$DOCKER_TAG -t ${DOCKER_REPO}-demo:$DOCKER_TAG demo-image diff --git a/hooks/post_push b/hooks/post_push index 1b19f796..4db84638 100755 --- a/hooks/post_push +++ b/hooks/post_push @@ -2,8 +2,11 @@ set -exuo pipefail export ONBUILD=${DOCKER_REPO}-onbuild +export DEMO=${DOCKER_REPO}-demo +export REPOS="${DOCKER_REPO} ${ONBUILD} ${DEMO}" # push ONBUILD image docker push $ONBUILD:$DOCKER_TAG +docker push $DEMO:$DOCKER_TAG function get_hub_version() { rm -f hub_version @@ -20,25 +23,20 @@ function get_hub_version() { fi } - get_hub_version -# when building master, push 0.9.0.dev as well -docker tag $DOCKER_REPO:$DOCKER_TAG $DOCKER_REPO:$hub_xyz -docker push $DOCKER_REPO:$hub_xyz -docker tag $ONBUILD:$DOCKER_TAG $ONBUILD:$hub_xyz -docker push $ONBUILD:$hub_xyz +for repo in ${REPOS}; do + # when building master, push 0.9.0.dev as well + docker tag $repo:$DOCKER_TAG $repo:$hub_xyz + docker push $repo:$hub_xyz -# when building 0.9.x, push 0.9 as well -docker tag $DOCKER_REPO:$DOCKER_TAG $DOCKER_REPO:$hub_xy -docker push $DOCKER_REPO:$hub_xy -docker tag $ONBUILD:$DOCKER_TAG $ONBUILD:$hub_xy -docker push $ONBUILD:$hub_xyz + # when building 0.9.x, push 0.9 as well + docker tag $repo:$DOCKER_TAG $repo:$hub_xy + docker push $repo:$hub_xy -# if building a stable release, tag latest as well -if [[ "$latest" == "1" ]]; then - docker tag $DOCKER_REPO:$DOCKER_TAG $DOCKER_REPO:latest - docker push $DOCKER_REPO:latest - docker tag $ONBUILD:$DOCKER_TAG $ONBUILD:latest - docker push $ONBUILD:latest -fi + # if building a stable release, tag latest as well + if [[ "$latest" == "1" ]]; then + docker tag $repo:$DOCKER_TAG $repo:latest + docker push $repo:latest + fi +done diff --git a/jupyterhub/_version.py b/jupyterhub/_version.py index 240bab39..58b0bff5 100644 --- a/jupyterhub/_version.py +++ b/jupyterhub/_version.py @@ -4,10 +4,10 @@ version_info = ( 1, + 3, 0, - 0, - # "b2", # release (b1, rc1, or "" for final or dev) - # "dev", # dev or nothing + "", # release (b1, rc1, or "" for final or dev) + "dev", # dev or nothing for beta/rc/stable releases ) # pep 440 version: no dot before beta/rc, but before .dev @@ -18,6 +18,15 @@ version_info = ( __version__ = ".".join(map(str, version_info[:3])) + ".".join(version_info[3:]) +# Singleton flag to only log the major/minor mismatch warning once per mismatch combo. +_version_mismatch_warning_logged = {} + + +def reset_globals(): + """Used to reset globals between test cases.""" + global _version_mismatch_warning_logged + _version_mismatch_warning_logged = {} + def _check_version(hub_version, singleuser_version, log): """Compare Hub and single-user server versions""" @@ -42,19 +51,27 @@ def _check_version(hub_version, singleuser_version, log): hub_major_minor = V(hub_version).version[:2] singleuser_major_minor = V(singleuser_version).version[:2] extra = "" + do_log = True if singleuser_major_minor == hub_major_minor: # patch-level mismatch or lower, log difference at debug-level # because this should be fine log_method = log.debug else: # log warning-level for more significant mismatch, such as 0.8 vs 0.9, etc. - log_method = log.warning - extra = " This could cause failure to authenticate and result in redirect loops!" - log_method( - "jupyterhub version %s != jupyterhub-singleuser version %s." + extra, - hub_version, - singleuser_version, - ) + key = '%s-%s' % (hub_version, singleuser_version) + global _version_mismatch_warning_logged + if _version_mismatch_warning_logged.get(key): + do_log = False # We already logged this warning so don't log it again. + else: + log_method = log.warning + extra = " This could cause failure to authenticate and result in redirect loops!" + _version_mismatch_warning_logged[key] = True + if do_log: + log_method( + "jupyterhub version %s != jupyterhub-singleuser version %s." + extra, + hub_version, + singleuser_version, + ) else: log.debug( "jupyterhub and jupyterhub-singleuser both on version %s" % hub_version diff --git a/jupyterhub/alembic/env.py b/jupyterhub/alembic/env.py index 4846f4c1..584f82c1 100644 --- a/jupyterhub/alembic/env.py +++ b/jupyterhub/alembic/env.py @@ -28,7 +28,7 @@ if 'jupyterhub' in sys.modules: alembic_logger.propagate = True alembic_logger.parent = app.log else: - fileConfig(config.config_file_name) + fileConfig(config.config_file_name, disable_existing_loggers=False) else: fileConfig(config.config_file_name) diff --git a/jupyterhub/apihandlers/auth.py b/jupyterhub/apihandlers/auth.py index 07e4d18b..76fcd8b8 100644 --- a/jupyterhub/apihandlers/auth.py +++ b/jupyterhub/apihandlers/auth.py @@ -198,14 +198,39 @@ class OAuthAuthorizeHandler(OAuthHandler, BaseHandler): raise self.send_oauth_response(headers, body, status) + def needs_oauth_confirm(self, user, oauth_client): + """Return whether the given oauth client needs to prompt for access for the given user + + Checks list for oauth clients that don't need confirmation + + (i.e. the user's own server) + + .. versionadded: 1.1 + """ + # get the oauth client ids for the user's own server(s) + own_oauth_client_ids = set( + spawner.oauth_client_id for spawner in user.spawners.values() + ) + if ( + # it's the user's own server + oauth_client.identifier in own_oauth_client_ids + # or it's in the global no-confirm list + or oauth_client.identifier + in self.settings.get('oauth_no_confirm_list', set()) + ): + return False + # default: require confirmation + return True + @web.authenticated - def get(self): + async def get(self): """GET /oauth/authorization Render oauth confirmation page: "Server at ... would like permission to ...". - Users accessing their own server will skip confirmation. + Users accessing their own server or a blessed service + will skip confirmation. """ uri, http_method, body, headers = self.extract_oauth_params() @@ -215,20 +240,25 @@ class OAuthAuthorizeHandler(OAuthHandler, BaseHandler): ) credentials = self.add_credentials(credentials) client = self.oauth_provider.fetch_by_client_id(credentials['client_id']) - if client.redirect_uri.startswith(self.current_user.url): + if not self.needs_oauth_confirm(self.current_user, client): self.log.debug( "Skipping oauth confirmation for %s accessing %s", self.current_user, client.description, ) - # access to my own server doesn't require oauth confirmation # this is the pre-1.0 behavior for all oauth self._complete_login(uri, headers, scopes, credentials) return # Render oauth 'Authorize application...' page + auth_state = await self.current_user.get_auth_state() self.write( - self.render_template("oauth.html", scopes=scopes, oauth_client=client) + await self.render_template( + "oauth.html", + auth_state=auth_state, + scopes=scopes, + oauth_client=client, + ) ) # Errors that should be shown to the user on the provider website @@ -245,9 +275,26 @@ class OAuthAuthorizeHandler(OAuthHandler, BaseHandler): uri, http_method, body, headers = self.extract_oauth_params() referer = self.request.headers.get('Referer', 'no referer') full_url = self.request.full_url() - if referer != full_url: + # trim protocol, which cannot be trusted with multiple layers of proxies anyway + # Referer is set by browser, but full_url can be modified by proxy layers to appear as http + # when it is actually https + referer_proto, _, stripped_referer = referer.partition("://") + referer_proto = referer_proto.lower() + req_proto, _, stripped_full_url = full_url.partition("://") + req_proto = req_proto.lower() + if referer_proto != req_proto: + self.log.warning("Protocol mismatch: %s != %s", referer, full_url) + if req_proto == "https": + # insecure origin to secure target is not allowed + raise web.HTTPError( + 403, "Not allowing authorization form submitted from insecure page" + ) + if stripped_referer != stripped_full_url: # OAuth post must be made to the URL it came from - self.log.error("OAuth POST from %s != %s", referer, full_url) + self.log.error("Original OAuth POST from %s != %s", referer, full_url) + self.log.error( + "Stripped OAuth POST from %s != %s", stripped_referer, stripped_full_url + ) raise web.HTTPError( 403, "Authorization form must be sent from authorization page" ) diff --git a/jupyterhub/apihandlers/base.py b/jupyterhub/apihandlers/base.py index 600876cd..d59cfb12 100644 --- a/jupyterhub/apihandlers/base.py +++ b/jupyterhub/apihandlers/base.py @@ -141,6 +141,7 @@ class APIHandler(BaseHandler): 'ready': spawner.ready, 'state': spawner.get_state() if include_state else None, 'url': url_path_join(spawner.user.url, spawner.name, '/'), + 'user_options': spawner.user_options, 'progress_url': spawner._progress_url, } diff --git a/jupyterhub/apihandlers/groups.py b/jupyterhub/apihandlers/groups.py index 78e833f7..e6b439ba 100644 --- a/jupyterhub/apihandlers/groups.py +++ b/jupyterhub/apihandlers/groups.py @@ -3,7 +3,6 @@ # Distributed under the terms of the Modified BSD License. import json -from tornado import gen from tornado import web from .. import orm diff --git a/jupyterhub/apihandlers/proxy.py b/jupyterhub/apihandlers/proxy.py index 83901832..0a43583b 100644 --- a/jupyterhub/apihandlers/proxy.py +++ b/jupyterhub/apihandlers/proxy.py @@ -2,12 +2,9 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. import json -from urllib.parse import urlparse -from tornado import gen from tornado import web -from .. import orm from ..utils import admin_only from .base import APIHandler diff --git a/jupyterhub/apihandlers/services.py b/jupyterhub/apihandlers/services.py index b8a98d51..3d1f7bcc 100644 --- a/jupyterhub/apihandlers/services.py +++ b/jupyterhub/apihandlers/services.py @@ -23,6 +23,7 @@ def service_model(service): 'command': service.command, 'pid': service.proc.pid if service.proc else 0, 'info': service.info, + 'display': service.display, } diff --git a/jupyterhub/apihandlers/users.py b/jupyterhub/apihandlers/users.py index 1c633723..44c829ff 100644 --- a/jupyterhub/apihandlers/users.py +++ b/jupyterhub/apihandlers/users.py @@ -589,11 +589,14 @@ class SpawnProgressAPIHandler(APIHandler): async with aclosing( iterate_until(spawn_future, spawner._generate_progress()) ) as events: - async for event in events: - # don't allow events to sneakily set the 'ready' flag - if 'ready' in event: - event.pop('ready', None) - await self.send_event(event) + try: + async for event in events: + # don't allow events to sneakily set the 'ready' flag + if 'ready' in event: + event.pop('ready', None) + await self.send_event(event) + except asyncio.CancelledError: + pass # progress finished, wait for spawn to actually resolve, # in case progress finished early @@ -622,14 +625,14 @@ def _parse_timestamp(timestamp): - raise HTTPError(400) on parse error - handle and strip tz info for internal consistency - (we use naïve utc timestamps everywhere) + (we use naive utc timestamps everywhere) """ try: dt = parse_date(timestamp) except Exception: raise web.HTTPError(400, "Not a valid timestamp: %r", timestamp) if dt.tzinfo: - # strip timezone info to naïve UTC datetime + # strip timezone info to naive UTC datetime dt = dt.astimezone(timezone.utc).replace(tzinfo=None) now = datetime.utcnow() diff --git a/jupyterhub/app.py b/jupyterhub/app.py index 815097a8..d616f223 100644 --- a/jupyterhub/app.py +++ b/jupyterhub/app.py @@ -5,17 +5,21 @@ import asyncio import atexit import binascii +import json import logging import os import re import signal import socket import sys +import time from concurrent.futures import ThreadPoolExecutor from datetime import datetime +from datetime import timedelta from datetime import timezone from functools import partial from getpass import getuser +from glob import glob from operator import itemgetter from textwrap import dedent from urllib.parse import unquote @@ -36,7 +40,6 @@ from tornado.ioloop import IOLoop, PeriodicCallback from tornado.log import app_log, access_log, gen_log import tornado.options from tornado import gen, web -from tornado.platform.asyncio import AsyncIOMainLoop from traitlets import ( Unicode, @@ -52,11 +55,15 @@ from traitlets import ( Instance, Bytes, Float, + Union, observe, default, + validate, ) from traitlets.config import Application, Configurable, catch_config_error +from jupyter_telemetry.eventlog import EventLog + here = os.path.dirname(__file__) import jupyterhub @@ -70,8 +77,9 @@ from .user import UserDict from .oauth.provider import make_provider from ._data import DATA_FILES_PATH from .log import CoroutineLogFormatter, log_request +from .pagination import Pagination from .proxy import Proxy, ConfigurableHTTPProxy -from .traitlets import URLPrefix, Command, EntryPointType +from .traitlets import URLPrefix, Command, EntryPointType, Callable from .utils import ( maybe_future, url_path_join, @@ -79,6 +87,10 @@ from .utils import ( print_ps_info, make_ssl_context, ) +from .metrics import HUB_STARTUP_DURATION_SECONDS +from .metrics import INIT_SPAWNERS_DURATION_SECONDS +from .metrics import RUNNING_SERVERS +from .metrics import TOTAL_USERS # classes for config from .auth import Authenticator, PAMAuthenticator @@ -145,8 +157,8 @@ flags = { } COOKIE_SECRET_BYTES = ( - 32 -) # the number of bytes to use when generating new cookie secrets + 32 # the number of bytes to use when generating new cookie secrets +) HEX_RE = re.compile('^([a-f0-9]{2})+$', re.IGNORECASE) @@ -268,7 +280,7 @@ class JupyterHub(Application): @default('classes') def _load_classes(self): - classes = [Spawner, Authenticator, CryptKeeper] + classes = [Spawner, Authenticator, CryptKeeper, Pagination] for name, trait in self.traits(config=True).items(): # load entry point groups into configurable class list # so that they show up in config files, etc. @@ -303,6 +315,19 @@ class JupyterHub(Application): config_file = Unicode('jupyterhub_config.py', help="The config file to load").tag( config=True ) + + @validate("config_file") + def _validate_config_file(self, proposal): + if not self.generate_config and not os.path.isfile(proposal.value): + print( + "ERROR: Failed to find specified config file: {}".format( + proposal.value + ), + file=sys.stderr, + ) + sys.exit(1) + return proposal.value + generate_config = Bool(False, help="Generate default config file").tag(config=True) generate_certs = Bool(False, help="Generate certs used for internal ssl").tag( config=True @@ -325,6 +350,18 @@ class JupyterHub(Application): redirect_to_server = Bool( True, help="Redirect user to server (if running), instead of control panel." ).tag(config=True) + activity_resolution = Integer( + 30, + help=""" + Resolution (in seconds) for updating activity + + If activity is registered that is less than activity_resolution seconds + more recent than the current value, + the new value will be ignored. + + This avoids too many writes to the Hub database. + """, + ).tag(config=True) last_activity_interval = Integer( 300, help="Interval (in seconds) at which to update last-activity timestamps." ).tag(config=True) @@ -526,10 +563,23 @@ class JupyterHub(Application): def _url_part_changed(self, change): """propagate deprecated ip/port/base_url config to the bind_url""" urlinfo = urlparse(self.bind_url) - urlinfo = urlinfo._replace(netloc='%s:%i' % (self.ip, self.port)) + if ':' in self.ip: + fmt = '[%s]:%i' + else: + fmt = '%s:%i' + urlinfo = urlinfo._replace(netloc=fmt % (self.ip, self.port)) urlinfo = urlinfo._replace(path=self.base_url) bind_url = urlunparse(urlinfo) + + # Warn if both bind_url and ip/port/base_url are set if bind_url != self.bind_url: + if self.bind_url != self._bind_url_default(): + self.log.warning( + "Both bind_url and ip/port/base_url have been configured. " + "JupyterHub.ip, JupyterHub.port, JupyterHub.base_url are" + " deprecated in JupyterHub 0.9," + " please use JupyterHub.bind_url instead." + ) self.bind_url = bind_url bind_url = Unicode( @@ -541,6 +591,22 @@ class JupyterHub(Application): """, ).tag(config=True) + @validate('bind_url') + def _validate_bind_url(self, proposal): + """ensure protocol field of bind_url matches ssl""" + v = proposal['value'] + proto, sep, rest = v.partition('://') + if self.ssl_cert and proto != 'https': + return 'https' + sep + rest + elif proto != 'http' and not self.ssl_cert: + return 'http' + sep + rest + return v + + @default('bind_url') + def _bind_url_default(self): + proto = 'https' if self.ssl_cert else 'http' + return proto + '://:8000' + subdomain_host = Unicode( '', help="""Run single-user servers on subdomains of this host. @@ -578,7 +644,9 @@ class JupyterHub(Application): @default('logo_file') def _logo_file_default(self): - return os.path.join(self.data_files_path, 'static', 'images', 'jupyter.png') + return os.path.join( + self.data_files_path, 'static', 'images', 'jupyterhub-80.png' + ) jinja_environment_options = Dict( help="Supply extra arguments that will be passed to Jinja environment." @@ -674,10 +742,10 @@ class JupyterHub(Application): help="""The ip or hostname for proxies and spawners to use for connecting to the Hub. - Use when the bind address (`hub_ip`) is 0.0.0.0 or otherwise different + Use when the bind address (`hub_ip`) is 0.0.0.0, :: or otherwise different from the connect address. - Default: when `hub_ip` is 0.0.0.0, use `socket.gethostname()`, otherwise use `hub_ip`. + Default: when `hub_ip` is 0.0.0.0 or ::, use `socket.gethostname()`, otherwise use `hub_ip`. Note: Some spawners or proxy implementations might not support hostnames. Check your spawner or proxy documentation to see if they have extra requirements. @@ -795,14 +863,14 @@ class JupyterHub(Application): api_tokens = Dict( Unicode(), - help="""PENDING DEPRECATION: consider using service_tokens + help="""PENDING DEPRECATION: consider using services Dict of token:username to be loaded into the database. Allows ahead-of-time generation of API tokens for use by externally managed services, which authenticate as JupyterHub users. - Consider using service_tokens for general services that talk to the JupyterHub API. + Consider using services for general services that talk to the JupyterHub API. """, ).tag(config=True) @@ -880,6 +948,25 @@ class JupyterHub(Application): def _authenticator_default(self): return self.authenticator_class(parent=self, db=self.db) + implicit_spawn_seconds = Float( + 0, + help="""Trigger implicit spawns after this many seconds. + + When a user visits a URL for a server that's not running, + they are shown a page indicating that the requested server + is not running with a button to spawn the server. + + Setting this to a positive value will redirect the user + after this many seconds, effectively clicking this button + automatically for the users, + automatically beginning the spawn process. + + Warning: this can result in errors and surprising behavior + when sharing access URLs to actual servers, + since the wrong server is likely to be started. + """, + ).tag(config=True) + allow_named_servers = Bool( False, help="Allow named single-user servers per user" ).tag(config=True) @@ -895,6 +982,22 @@ class JupyterHub(Application): """, ).tag(config=True) + default_server_name = Unicode( + "", + help="If named servers are enabled, default name of server to spawn or open, e.g. by user-redirect.", + ).tag(config=True) + # Ensure that default_server_name doesn't do anything if named servers aren't allowed + _default_server_name = Unicode( + help="Non-configurable version exposed to JupyterHub." + ) + + @default('_default_server_name') + def _set_default_server_name(self): + if self.allow_named_servers: + return self.default_server_name + else: + return "" + # class for spawning single-user servers spawner_class = EntryPointType( default_value=LocalProcessSpawner, @@ -968,6 +1071,28 @@ class JupyterHub(Application): """, ).tag(config=True) + init_spawners_timeout = Integer( + 10, + help=""" + Timeout (in seconds) to wait for spawners to initialize + + Checking if spawners are healthy can take a long time + if many spawners are active at hub start time. + + If it takes longer than this timeout to check, + init_spawner will be left to complete in the background + and the http server is allowed to start. + + A timeout of -1 means wait forever, + which can mean a slow startup of the Hub + but ensures that the Hub is fully consistent by the time it starts responding to requests. + This matches the behavior of jupyterhub 1.0. + + .. versionadded: 1.1.0 + + """, + ).tag(config=True) + db_url = Unicode( 'sqlite:///jupyterhub.sqlite', help="url for the database. e.g. `sqlite:///jupyterhub.sqlite`", @@ -1191,12 +1316,42 @@ class JupyterHub(Application): """ ).tag(config=True) - default_url = Unicode( + default_url = Union( + [Unicode(), Callable()], help=""" The default URL for users when they arrive (e.g. when user directs to "/") By default, redirects users to their own server. - """ + + Can be a Unicode string (e.g. '/hub/home') or a callable based on the handler object: + + :: + + def default_url_fn(handler): + user = handler.current_user + if user and user.admin: + return '/hub/admin' + return '/hub/home' + + c.JupyterHub.default_url = default_url_fn + """, + ).tag(config=True) + + user_redirect_hook = Callable( + None, + allow_none=True, + help=""" + Callable to affect behavior of /user-redirect/ + + Receives 4 parameters: + 1. path - URL path that was provided after /user-redirect/ + 2. request - A Tornado HTTPServerRequest representing the current request. + 3. user - The currently authenticated user. + 4. base_url - The base_url of the current hub, for relative redirects + + It should return the new URL to redirect to, or None to preserve + current behavior. + """, ).tag(config=True) def init_handlers(self): @@ -1549,22 +1704,22 @@ class JupyterHub(Application): # the admin_users config variable will never be used after this point. # only the database values will be referenced. - whitelist = [ + allowed_users = [ self.authenticator.normalize_username(name) - for name in self.authenticator.whitelist + for name in self.authenticator.allowed_users ] - self.authenticator.whitelist = set(whitelist) # force normalization - for username in whitelist: + self.authenticator.allowed_users = set(allowed_users) # force normalization + for username in allowed_users: if not self.authenticator.validate_username(username): raise ValueError("username %r is not valid" % username) - if not whitelist: + if not allowed_users: self.log.info( - "Not using whitelist. Any authenticated user will be allowed." + "Not using allowed_users. Any authenticated user will be allowed." ) - # add whitelisted users to the db - for name in whitelist: + # add allowed users to the db + for name in allowed_users: user = orm.User.find(db, name) if user is None: user = orm.User(name=name) @@ -1574,13 +1729,16 @@ class JupyterHub(Application): db.commit() # Notify authenticator of all users. - # This ensures Auth whitelist is up-to-date with the database. - # This lets whitelist be used to set up initial list, - # but changes to the whitelist can occur in the database, + # This ensures Authenticator.allowed_users is up-to-date with the database. + # This lets .allowed_users be used to set up initial list, + # but changes to the allowed_users set can occur in the database, # and persist across sessions. + total_users = 0 for user in db.query(orm.User): try: - await maybe_future(self.authenticator.add_user(user)) + f = self.authenticator.add_user(user) + if f: + await maybe_future(f) except Exception: self.log.exception("Error adding user %s already in db", user.name) if self.authenticator.delete_invalid_users: @@ -1602,6 +1760,7 @@ class JupyterHub(Application): ) ) else: + total_users += 1 # handle database upgrades where user.created is undefined. # we don't want to allow user.created to be undefined, # so initialize it to last_activity (if defined) or now. @@ -1609,9 +1768,11 @@ class JupyterHub(Application): user.created = user.last_activity or datetime.utcnow() db.commit() - # The whitelist set and the users in the db are now the same. + # The allowed_users set and the users in the db are now the same. # From this point on, any user changes should be done simultaneously - # to the whitelist set and user db, unless the whitelist is empty (all users allowed). + # to the allowed_users set and user db, unless the allowed set is empty (all users allowed). + + TOTAL_USERS.set(total_users) async def init_groups(self): """Load predefined groups into the database""" @@ -1624,11 +1785,11 @@ class JupyterHub(Application): for username in usernames: username = self.authenticator.normalize_username(username) if not ( - await maybe_future( - self.authenticator.check_whitelist(username, None) - ) + await maybe_future(self.authenticator.check_allowed(username, None)) ): - raise ValueError("Username %r is not in whitelist" % username) + raise ValueError( + "Username %r is not in Authenticator.allowed_users" % username + ) user = orm.User.find(db, name=username) if user is None: if not self.authenticator.validate_username(username): @@ -1652,11 +1813,20 @@ class JupyterHub(Application): if kind == 'user': name = self.authenticator.normalize_username(name) if not ( - await maybe_future(self.authenticator.check_whitelist(name, None)) + await maybe_future(self.authenticator.check_allowed(name, None)) ): - raise ValueError("Token name %r is not in whitelist" % name) + raise ValueError( + "Token user name %r is not in Authenticator.allowed_users" + % name + ) if not self.authenticator.validate_username(name): - raise ValueError("Token name %r is not valid" % name) + raise ValueError("Token user name %r is not valid" % name) + if kind == 'service': + if not any(service["name"] == name for service in self.services): + self.log.warning( + "Warning: service '%s' not in services, creating implicitly. It is recommended to register services using services list." + % name + ) orm_token = orm.APIToken.find(db, token) if orm_token is None: obj = Class.find(db, name) @@ -1689,17 +1859,27 @@ class JupyterHub(Application): # purge expired tokens hourly purge_expired_tokens_interval = 3600 + def purge_expired_tokens(self): + """purge all expiring token objects from the database + + run periodically + """ + # this should be all the subclasses of Expiring + for cls in (orm.APIToken, orm.OAuthAccessToken, orm.OAuthCode): + self.log.debug("Purging expired {name}s".format(name=cls.__name__)) + cls.purge_expired(self.db) + async def init_api_tokens(self): """Load predefined API tokens (for services) into database""" await self._add_tokens(self.service_tokens, kind='service') await self._add_tokens(self.api_tokens, kind='user') - purge_expired_tokens = partial(orm.APIToken.purge_expired, self.db) - purge_expired_tokens() + + self.purge_expired_tokens() # purge expired tokens hourly # we don't need to be prompt about this # because expired tokens cannot be used anyway pc = PeriodicCallback( - purge_expired_tokens, 1e3 * self.purge_expired_tokens_interval + self.purge_expired_tokens, 1e3 * self.purge_expired_tokens_interval ) pc.start() @@ -1813,6 +1993,7 @@ class JupyterHub(Application): ) async def init_spawners(self): + self.log.debug("Initializing spawners") db = self.db def _user_summary(user): @@ -1903,21 +2084,43 @@ class JupyterHub(Application): else: self.log.debug("%s not running", spawner._log_name) + spawner._check_pending = False + # parallelize checks for running Spawners + # run query on extant Server objects + # so this is O(running servers) not O(total users) + # Server objects can be associated with either a Spawner or a Service, + # we are only interested in the ones associated with a Spawner check_futures = [] - for orm_user in db.query(orm.User): - user = self.users[orm_user] - self.log.debug("Loading state for %s from db", user.name) - for name, orm_spawner in user.orm_spawners.items(): - if orm_spawner.server is not None: - # spawner should be running - # instantiate Spawner wrapper and check if it's still alive - spawner = user.spawners[name] - f = asyncio.ensure_future(check_spawner(user, name, spawner)) - check_futures.append(f) + for orm_server in db.query(orm.Server): + orm_spawner = orm_server.spawner + if not orm_spawner: + # check for orphaned Server rows + # this shouldn't happen if we've got our sqlachemy right + if not orm_server.service: + self.log.warning("deleting orphaned server %s", orm_server) + self.db.delete(orm_server) + self.db.commit() + continue + # instantiate Spawner wrapper and check if it's still alive + # spawner should be running + user = self.users[orm_spawner.user] + spawner = user.spawners[orm_spawner.name] + self.log.debug("Loading state for %s from db", spawner._log_name) + # signal that check is pending to avoid race conditions + spawner._check_pending = True + f = asyncio.ensure_future(check_spawner(user, spawner.name, spawner)) + check_futures.append(f) + + # it's important that we get here before the first await + # so that we know all spawners are instantiated and in the check-pending state # await checks after submitting them all - await gen.multi(check_futures) + if check_futures: + self.log.debug( + "Awaiting checks for %i possibly-running spawners", len(check_futures) + ) + await gen.multi(check_futures) db.commit() # only perform this query if we are going to log it @@ -1925,6 +2128,10 @@ class JupyterHub(Application): user_summaries = map(_user_summary, self.users.values()) self.log.debug("Loaded users:\n%s", '\n'.join(user_summaries)) + active_counts = self.users.count_active_users() + RUNNING_SERVERS.set(active_counts['active']) + return len(check_futures) + def init_oauth(self): base_url = self.hub.base_url self.oauth_provider = make_provider( @@ -1980,7 +2187,7 @@ class JupyterHub(Application): def init_tornado_settings(self): """Set up the tornado settings dict.""" base_url = self.hub.base_url - jinja_options = dict(autoescape=True) + jinja_options = dict(autoescape=True, enable_async=True) jinja_options.update(self.jinja_environment_options) base_path = self._template_paths_default()[0] if base_path not in self.template_paths: @@ -1992,6 +2199,14 @@ class JupyterHub(Application): ] ) jinja_env = Environment(loader=loader, **jinja_options) + # We need a sync jinja environment too, for the times we *must* use sync + # code - particularly in RequestHandler.write_error. Since *that* + # is called from inside the asyncio event loop, we can't actulaly just + # schedule it on the loop - without starting another thread with its + # own loop, which seems not worth the trouble. Instead, we create another + # environment, exactly like this one, but sync + del jinja_options['enable_async'] + jinja_env_sync = Environment(loader=loader, **jinja_options) login_url = url_path_join(base_url, 'login') logout_url = self.authenticator.logout_url(base_url) @@ -2004,6 +2219,15 @@ class JupyterHub(Application): else: version_hash = datetime.now().strftime("%Y%m%d%H%M%S") + oauth_no_confirm_list = set() + for service in self._service_map.values(): + if service.oauth_no_confirm: + self.log.warning( + "Allowing service %s to complete OAuth without confirmation on an authorization web page", + service.name, + ) + oauth_no_confirm_list.add(service.oauth_client_id) + settings = dict( log_function=log_request, config=self.config, @@ -2011,6 +2235,7 @@ class JupyterHub(Application): db=self.db, proxy=self.proxy, hub=self.hub, + activity_resolution=self.activity_resolution, admin_users=self.authenticator.admin_users, admin_access=self.admin_access, authenticator=self.authenticator, @@ -2028,13 +2253,17 @@ class JupyterHub(Application): template_path=self.template_paths, template_vars=self.template_vars, jinja2_env=jinja_env, + jinja2_env_sync=jinja_env_sync, version_hash=version_hash, subdomain_host=self.subdomain_host, domain=self.domain, statsd=self.statsd, + implicit_spawn_seconds=self.implicit_spawn_seconds, allow_named_servers=self.allow_named_servers, + default_server_name=self._default_server_name, named_server_limit_per_user=self.named_server_limit_per_user, oauth_provider=self.oauth_provider, + oauth_no_confirm_list=oauth_no_confirm_list, concurrent_spawn_limit=self.concurrent_spawn_limit, spawn_throttle_retry_range=self.spawn_throttle_retry_range, active_server_limit=self.active_server_limit, @@ -2048,6 +2277,8 @@ class JupyterHub(Application): internal_ssl_ca=self.internal_ssl_ca, trusted_alt_names=self.trusted_alt_names, shutdown_on_logout=self.shutdown_on_logout, + eventlog=self.eventlog, + app=self, ) # allow configured settings to have priority settings.update(self.tornado_settings) @@ -2073,6 +2304,16 @@ class JupyterHub(Application): e, ) + def init_eventlog(self): + """Set up the event logging system.""" + self.eventlog = EventLog(parent=self) + + for dirname, _, files in os.walk(os.path.join(here, 'event-schemas')): + for file in files: + if not file.endswith('.yaml'): + continue + self.eventlog.register_schema_file(os.path.join(dirname, file)) + def write_pid_file(self): pid = os.getpid() if self.pid_file: @@ -2082,11 +2323,22 @@ class JupyterHub(Application): @catch_config_error async def initialize(self, *args, **kwargs): + hub_startup_start_time = time.perf_counter() super().initialize(*args, **kwargs) if self.generate_config or self.generate_certs or self.subapp: return + self._start_future = asyncio.Future() + + def record_start(f): + startup_time = time.perf_counter() - hub_startup_start_time + self.log.debug("It took %.3f seconds for the Hub to start", startup_time) + HUB_STARTUP_DURATION_SECONDS.observe(startup_time) + + self._start_future.add_done_callback(record_start) + self.load_config_file(self.config_file) self.init_logging() + self.log.info("Running JupyterHub version %s", jupyterhub.__version__) if 'JupyterHubApp' in self.config: self.log.warning( "Use JupyterHub in config, not JupyterHubApp. Outdated config:\n%s", @@ -2122,7 +2374,9 @@ class JupyterHub(Application): _log_cls("Authenticator", self.authenticator_class) _log_cls("Spawner", self.spawner_class) + _log_cls("Proxy", self.proxy_class) + self.init_eventlog() self.init_pycurl() self.init_secrets() self.init_internal_ssl() @@ -2135,11 +2389,62 @@ class JupyterHub(Application): self.init_services() await self.init_api_tokens() self.init_tornado_settings() - await self.init_spawners() - self.cleanup_oauth_clients() self.init_handlers() self.init_tornado_application() + # init_spawners can take a while + init_spawners_timeout = self.init_spawners_timeout + if init_spawners_timeout < 0: + # negative timeout means forever (previous, most stable behavior) + init_spawners_timeout = 86400 + + init_start_time = time.perf_counter() + init_spawners_future = asyncio.ensure_future(self.init_spawners()) + + def log_init_time(f): + n_spawners = f.result() + spawner_initialization_time = time.perf_counter() - init_start_time + INIT_SPAWNERS_DURATION_SECONDS.observe(spawner_initialization_time) + self.log.info( + "Initialized %i spawners in %.3f seconds", + n_spawners, + spawner_initialization_time, + ) + + init_spawners_future.add_done_callback(log_init_time) + + try: + + # don't allow a zero timeout because we still need to be sure + # that the Spawner objects are defined and pending + await gen.with_timeout( + timedelta(seconds=max(init_spawners_timeout, 1)), init_spawners_future + ) + except gen.TimeoutError: + self.log.warning( + "init_spawners did not complete within %i seconds. " + "Allowing to complete in the background.", + self.init_spawners_timeout, + ) + + if init_spawners_future.done(): + self.cleanup_oauth_clients() + else: + # schedule async operations after init_spawners finishes + async def finish_init_spawners(): + await init_spawners_future + # schedule cleanup after spawners are all set up + # because it relies on the state resolved by init_spawners + self.cleanup_oauth_clients() + # trigger a proxy check as soon as all spawners are ready + # because this may be *after* the check made as part of normal startup. + # To avoid races with partially-complete start, + # ensure that start is complete before running this check. + await self._start_future + await self.proxy.check_routes(self.users, self._service_map) + + asyncio.ensure_future(finish_init_spawners()) + async def cleanup(self): """Shutdown managed services and various subprocesses. Cleanup runtime files.""" @@ -2247,7 +2552,7 @@ class JupyterHub(Application): continue dt = parse_date(route_data['last_activity']) if dt.tzinfo: - # strip timezone info to naïve UTC datetime + # strip timezone info to naive UTC datetime dt = dt.astimezone(timezone.utc).replace(tzinfo=None) if user.last_activity: @@ -2289,7 +2594,7 @@ class JupyterHub(Application): if self.generate_certs: self.load_config_file(self.config_file) if not self.internal_ssl: - self.log.warn( + self.log.warning( "You'll need to enable `internal_ssl` " "in the `jupyterhub_config` file to use " "these certs." @@ -2304,6 +2609,20 @@ class JupyterHub(Application): loop.stop() return + # start the proxy + if self.proxy.should_start: + try: + await self.proxy.start() + except Exception as e: + self.log.critical("Failed to start proxy", exc_info=True) + self.exit(1) + else: + self.log.info("Not starting proxy") + + # verify that we can talk to the proxy before listening. + # avoids delayed failure if we can't talk to the proxy + await self.proxy.get_all_routes() + ssl_context = make_ssl_context( self.internal_ssl_key, self.internal_ssl_cert, @@ -2341,16 +2660,6 @@ class JupyterHub(Application): self.log.error("Failed to bind hub to %s", self.hub.bind_url) raise - # start the proxy - if self.proxy.should_start: - try: - await self.proxy.start() - except Exception as e: - self.log.critical("Failed to start proxy", exc_info=True) - self.exit(1) - else: - self.log.info("Not starting proxy") - # start the service(s) for service_name, service in self._service_map.items(): msg = ( @@ -2420,22 +2729,31 @@ class JupyterHub(Application): pc.start() self.log.info("JupyterHub is now running at %s", self.proxy.public_url) + # Use atexit for Windows, it doesn't have signal handling support + if _mswindows: + atexit.register(self.atexit) # register cleanup on both TERM and INT self.init_signal() + self._start_future.set_result(None) def init_signal(self): loop = asyncio.get_event_loop() for s in (signal.SIGTERM, signal.SIGINT): - loop.add_signal_handler( - s, lambda s=s: asyncio.ensure_future(self.shutdown_cancel_tasks(s)) - ) - infosignals = [signal.SIGUSR1] - if hasattr(signal, 'SIGINFO'): - infosignals.append(signal.SIGINFO) - for s in infosignals: - loop.add_signal_handler( - s, lambda s=s: asyncio.ensure_future(self.log_status(s)) - ) + if not _mswindows: + loop.add_signal_handler( + s, lambda s=s: asyncio.ensure_future(self.shutdown_cancel_tasks(s)) + ) + else: + signal.signal(s, self.win_shutdown_cancel_tasks) + + if not _mswindows: + infosignals = [signal.SIGUSR1] + if hasattr(signal, 'SIGINFO'): + infosignals.append(signal.SIGINFO) + for s in infosignals: + loop.add_signal_handler( + s, lambda s=s: asyncio.ensure_future(self.log_status(s)) + ) async def log_status(self, sig): """Log current status, triggered by SIGINFO (^T in many terminals)""" @@ -2443,6 +2761,59 @@ class JupyterHub(Application): print_ps_info() print_stacks() + def win_shutdown_cancel_tasks(self, signum, frame): + self.log.critical("Received signalnum %s, , initiating shutdown...", signum) + raise SystemExit(128 + signum) + + def _init_asyncio_patch(self): + """Set default asyncio policy to be compatible with Tornado. + + Tornado 6 (at least) is not compatible with the default + asyncio implementation on Windows. + + Pick the older SelectorEventLoopPolicy on Windows + if the known-incompatible default policy is in use. + + Do this as early as possible to make it a low priority and overrideable. + + ref: https://github.com/tornadoweb/tornado/issues/2608 + + FIXME: If/when tornado supports the defaults in asyncio, + remove and bump tornado requirement for py38. + """ + if sys.platform.startswith("win") and sys.version_info >= (3, 8): + try: + from asyncio import ( + WindowsProactorEventLoopPolicy, + WindowsSelectorEventLoopPolicy, + ) + except ImportError: + pass + # not affected + else: + if ( + type(asyncio.get_event_loop_policy()) + is WindowsProactorEventLoopPolicy + ): + # WindowsProactorEventLoopPolicy is not compatible with Tornado 6. + # Fallback to the pre-3.8 default of WindowsSelectorEventLoopPolicy. + asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy()) + + _atexit_ran = False + + def atexit(self): + """atexit callback""" + if self._atexit_ran: + return + self._atexit_ran = True + self._init_asyncio_patch() + # run the cleanup step (in a new loop, because the interrupted one is unclean) + asyncio.set_event_loop(asyncio.new_event_loop()) + IOLoop.clear_current() + loop = IOLoop() + loop.make_current() + loop.run_sync(self.cleanup) + async def shutdown_cancel_tasks(self, sig): """Cancel all other tasks of the event loop and initiate cleanup""" self.log.critical("Received signal %s, initiating shutdown...", sig.name) @@ -2485,14 +2856,17 @@ class JupyterHub(Application): @classmethod def launch_instance(cls, argv=None): self = cls.instance() - AsyncIOMainLoop().install() + self._init_asyncio_patch() loop = IOLoop.current() - loop.add_callback(self.launch_instance_async, argv) + task = asyncio.ensure_future(self.launch_instance_async(argv)) try: loop.start() except KeyboardInterrupt: print("\nInterrupted") finally: + if task.done(): + # re-raise exceptions in launch_instance_async + task.result() loop.stop() loop.close() diff --git a/jupyterhub/auth.py b/jupyterhub/auth.py index bc906a70..bc9c1ef8 100644 --- a/jupyterhub/auth.py +++ b/jupyterhub/auth.py @@ -7,6 +7,7 @@ import re import sys import warnings from concurrent.futures import ThreadPoolExecutor +from functools import partial from shutil import which from subprocess import PIPE from subprocess import Popen @@ -100,41 +101,74 @@ class Authenticator(LoggingConfigurable): """ ).tag(config=True) - whitelist = Set( + whitelist = Set(help="Deprecated, use `Authenticator.allowed_users`", config=True,) + + allowed_users = Set( help=""" - Whitelist of usernames that are allowed to log in. + Set of usernames that are allowed to log in. Use this with supported authenticators to restrict which users can log in. This is an - additional whitelist that further restricts users, beyond whatever restrictions the + additional list that further restricts users, beyond whatever restrictions the authenticator has in place. If empty, does not perform any additional restriction. + + .. versionchanged:: 1.2 + `Authenticator.whitelist` renamed to `allowed_users` """ ).tag(config=True) - blacklist = Set( + blocked_users = Set( help=""" - Blacklist of usernames that are not allowed to log in. + Set of usernames that are not allowed to log in. Use this with supported authenticators to restrict which users can not log in. This is an - additional blacklist that further restricts users, beyond whatever restrictions the + additional block list that further restricts users, beyond whatever restrictions the authenticator has in place. If empty, does not perform any additional restriction. .. versionadded: 0.9 + + .. versionchanged:: 1.2 + `Authenticator.blacklist` renamed to `blocked_users` """ ).tag(config=True) - @observe('whitelist') - def _check_whitelist(self, change): + _deprecated_aliases = { + "whitelist": ("allowed_users", "1.2"), + "blacklist": ("blocked_users", "1.2"), + } + + @observe(*list(_deprecated_aliases)) + def _deprecated_trait(self, change): + """observer for deprecated traits""" + old_attr = change.name + new_attr, version = self._deprecated_aliases.get(old_attr) + new_value = getattr(self, new_attr) + if new_value != change.new: + # only warn if different + # protects backward-compatible config from warnings + # if they set the same value under both names + self.log.warning( + "{cls}.{old} is deprecated in JupyterHub {version}, use {cls}.{new} instead".format( + cls=self.__class__.__name__, + old=old_attr, + new=new_attr, + version=version, + ) + ) + setattr(self, new_attr, change.new) + + @observe('allowed_users') + def _check_allowed_users(self, change): short_names = [name for name in change['new'] if len(name) <= 1] if short_names: sorted_names = sorted(short_names) single = ''.join(sorted_names) string_set_typo = "set('%s')" % single self.log.warning( - "whitelist contains single-character names: %s; did you mean set([%r]) instead of %s?", + "Allowed set contains single-character names: %s; did you mean set([%r]) instead of %s?", sorted_names[:8], single, string_set_typo, @@ -206,6 +240,7 @@ class Authenticator(LoggingConfigurable): delete_invalid_users = Bool( False, + config=True, help="""Delete any users from the database that do not pass validation When JupyterHub starts, `.add_user` will be called @@ -260,39 +295,74 @@ class Authenticator(LoggingConfigurable): def __init__(self, **kwargs): super().__init__(**kwargs) - for method_name in ( - 'check_whitelist', - 'check_blacklist', - 'check_group_whitelist', + self._init_deprecated_methods() + + def _init_deprecated_methods(self): + # handles deprecated signature *and* name + # with correct subclass override priority! + for old_name, new_name in ( + ('check_whitelist', 'check_allowed'), + ('check_blacklist', 'check_blocked_users'), + ('check_group_whitelist', 'check_allowed_groups'), ): - original_method = getattr(self, method_name, None) - if original_method is None: + old_method = getattr(self, old_name, None) + if old_method is None: # no such method (check_group_whitelist is optional) continue - signature = inspect.signature(original_method) - if 'authentication' not in signature.parameters: + + # allow old name to have higher priority + # if and only if it's defined in a later subclass + # than the new name + for cls in self.__class__.mro(): + has_old_name = old_name in cls.__dict__ + has_new_name = new_name in cls.__dict__ + if has_new_name: + break + if has_old_name and not has_new_name: + warnings.warn( + "{0}.{1} should be renamed to {0}.{2} for JupyterHub >= 1.2".format( + cls.__name__, old_name, new_name + ), + DeprecationWarning, + ) + # use old name instead of new + # if old name is overridden in subclass + def _new_calls_old(old_name, *args, **kwargs): + return getattr(self, old_name)(*args, **kwargs) + + setattr(self, new_name, partial(_new_calls_old, old_name)) + break + + # deprecate pre-1.0 method signatures + signature = inspect.signature(old_method) + if 'authentication' not in signature.parameters and not any( + param.kind == inspect.Parameter.VAR_KEYWORD + for param in signature.parameters.values() + ): # adapt to pre-1.0 signature for compatibility warnings.warn( """ {0}.{1} does not support the authentication argument, - added in JupyterHub 1.0. + added in JupyterHub 1.0. and is renamed to {2} in JupyterHub 1.2. It should have the signature: - def {1}(self, username, authentication=None): + def {2}(self, username, authentication=None): ... Adapting for compatibility. """.format( - self.__class__.__name__, method_name + self.__class__.__name__, old_name, new_name ), DeprecationWarning, ) - def wrapped_method(username, authentication=None, **kwargs): + def wrapped_method( + original_method, username, authentication=None, **kwargs + ): return original_method(username, **kwargs) - setattr(self, method_name, wrapped_method) + setattr(self, old_name, partial(wrapped_method, old_method)) async def run_post_auth_hook(self, handler, authentication): """ @@ -326,39 +396,45 @@ class Authenticator(LoggingConfigurable): username = self.username_map.get(username, username) return username - def check_whitelist(self, username, authentication=None): - """Check if a username is allowed to authenticate based on whitelist configuration + def check_allowed(self, username, authentication=None): + """Check if a username is allowed to authenticate based on configuration Return True if username is allowed, False otherwise. - No whitelist means any username is allowed. + No allowed_users set means any username is allowed. - Names are normalized *before* being checked against the whitelist. + Names are normalized *before* being checked against the allowed set. .. versionchanged:: 1.0 Signature updated to accept authentication data and any future changes - """ - if not self.whitelist: - # No whitelist means any name is allowed - return True - return username in self.whitelist - def check_blacklist(self, username, authentication=None): - """Check if a username is blocked to authenticate based on blacklist configuration + .. versionchanged:: 1.2 + Renamed check_whitelist to check_allowed + """ + if not self.allowed_users: + # No allowed set means any name is allowed + return True + return username in self.allowed_users + + def check_blocked_users(self, username, authentication=None): + """Check if a username is blocked to authenticate based on Authenticator.blocked configuration Return True if username is allowed, False otherwise. - No blacklist means any username is allowed. + No block list means any username is allowed. - Names are normalized *before* being checked against the blacklist. + Names are normalized *before* being checked against the block list. .. versionadded: 0.9 .. versionchanged:: 1.0 Signature updated to accept authentication data as second argument + + .. versionchanged:: 1.2 + Renamed check_blacklist to check_blocked_users """ - if not self.blacklist: - # No blacklist means any name is allowed + if not self.blocked_users: + # No block list means any name is allowed return True - return username not in self.blacklist + return username not in self.blocked_users async def get_authenticated_user(self, handler, data): """Authenticate the user who is attempting to log in @@ -367,7 +443,7 @@ class Authenticator(LoggingConfigurable): This calls `authenticate`, which should be overridden in subclasses, normalizes the username if any normalization should be done, - and then validates the name in the whitelist. + and then validates the name in the allowed set. This is the outer API for authenticating a user. Subclasses should not override this method. @@ -375,7 +451,7 @@ class Authenticator(LoggingConfigurable): The various stages can be overridden separately: - `authenticate` turns formdata into a username - `normalize_username` normalizes the username - - `check_whitelist` checks against the user whitelist + - `check_allowed` checks against the allowed usernames .. versionchanged:: 0.8 return dict instead of username @@ -389,7 +465,7 @@ class Authenticator(LoggingConfigurable): else: authenticated = {'name': authenticated} authenticated.setdefault('auth_state', None) - # Leave the default as None, but reevaluate later post-whitelist + # Leave the default as None, but reevaluate later post-allowed-check authenticated.setdefault('admin', None) # normalize the username @@ -400,20 +476,18 @@ class Authenticator(LoggingConfigurable): self.log.warning("Disallowing invalid username %r.", username) return - blacklist_pass = await maybe_future( - self.check_blacklist(username, authenticated) - ) - whitelist_pass = await maybe_future( - self.check_whitelist(username, authenticated) + blocked_pass = await maybe_future( + self.check_blocked_users(username, authenticated) ) + allowed_pass = await maybe_future(self.check_allowed(username, authenticated)) - if blacklist_pass: + if blocked_pass: pass else: - self.log.warning("User %r in blacklist. Stop authentication", username) + self.log.warning("User %r blocked. Stop authentication", username) return - if whitelist_pass: + if allowed_pass: if authenticated['admin'] is None: authenticated['admin'] = await maybe_future( self.is_admin(handler, authenticated) @@ -423,7 +497,7 @@ class Authenticator(LoggingConfigurable): return authenticated else: - self.log.warning("User %r not in whitelist.", username) + self.log.warning("User %r not allowed.", username) return async def refresh_user(self, user, handler=None): @@ -479,7 +553,7 @@ class Authenticator(LoggingConfigurable): It must return the username on successful authentication, and return None on failed authentication. - Checking the whitelist is handled separately by the caller. + Checking allowed_users/blocked_users is handled separately by the caller. .. versionchanged:: 0.8 Allow `authenticate` to return a dict containing auth_state. @@ -520,10 +594,10 @@ class Authenticator(LoggingConfigurable): This method may be a coroutine. - By default, this just adds the user to the whitelist. + By default, this just adds the user to the allowed_users set. Subclasses may do more extensive things, such as adding actual unix users, - but they should call super to ensure the whitelist is updated. + but they should call super to ensure the allowed_users set is updated. Note that this should be idempotent, since it is called whenever the hub restarts for all users. @@ -533,19 +607,19 @@ class Authenticator(LoggingConfigurable): """ if not self.validate_username(user.name): raise ValueError("Invalid username: %s" % user.name) - if self.whitelist: - self.whitelist.add(user.name) + if self.allowed_users: + self.allowed_users.add(user.name) def delete_user(self, user): """Hook called when a user is deleted - Removes the user from the whitelist. - Subclasses should call super to ensure the whitelist is updated. + Removes the user from the allowed_users set. + Subclasses should call super to ensure the allowed_users set is updated. Args: user (User): The User wrapper object """ - self.whitelist.discard(user.name) + self.allowed_users.discard(user.name) auto_login = Bool( False, @@ -610,6 +684,41 @@ class Authenticator(LoggingConfigurable): return [('/login', LoginHandler)] +def _deprecated_method(old_name, new_name, version): + """Create a deprecated method wrapper for a deprecated method name""" + + def deprecated(self, *args, **kwargs): + warnings.warn( + ( + "{cls}.{old_name} is deprecated in JupyterHub {version}." + " Please use {cls}.{new_name} instead." + ).format( + cls=self.__class__.__name__, + old_name=old_name, + new_name=new_name, + version=version, + ), + DeprecationWarning, + stacklevel=2, + ) + old_method = getattr(self, new_name) + return old_method(*args, **kwargs) + + return deprecated + + +import types + +# deprecate white/blacklist method names +for _old_name, _new_name, _version in [ + ("check_whitelist", "check_allowed", "1.2"), + ("check_blacklist", "check_blocked_users", "1.2"), +]: + setattr( + Authenticator, _old_name, _deprecated_method(_old_name, _new_name, _version), + ) + + class LocalAuthenticator(Authenticator): """Base class for Authenticators that work with local Linux/UNIX users @@ -660,37 +769,46 @@ class LocalAuthenticator(Authenticator): # This appears to be the Linux non-interactive adduser command: return ['adduser', '-q', '--gecos', '""', '--disabled-password'] - group_whitelist = Set( + uids = Dict( help=""" - Whitelist all users from this UNIX group. - - This makes the username whitelist ineffective. + Dictionary of uids to use at user creation time. + This helps ensure that users created from the database + get the same uid each time they are created + in temporary deployments or containers. """ ).tag(config=True) - @observe('group_whitelist') - def _group_whitelist_changed(self, change): + group_whitelist = Set(help="""DEPRECATED: use allowed_groups""",).tag(config=True) + + allowed_groups = Set( + help=""" + Allow login from all users in these UNIX groups. + + If set, allowed username set is ignored. """ - Log a warning if both group_whitelist and user whitelist are set. - """ - if self.whitelist: + ).tag(config=True) + + @observe('allowed_groups') + def _allowed_groups_changed(self, change): + """Log a warning if mutually exclusive user and group allowed sets are specified.""" + if self.allowed_users: self.log.warning( - "Ignoring username whitelist because group whitelist supplied!" + "Ignoring Authenticator.allowed_users set because Authenticator.allowed_groups supplied!" ) - def check_whitelist(self, username, authentication=None): - if self.group_whitelist: - return self.check_group_whitelist(username, authentication) + def check_allowed(self, username, authentication=None): + if self.allowed_groups: + return self.check_allowed_groups(username, authentication) else: - return super().check_whitelist(username, authentication) + return super().check_allowed(username, authentication) - def check_group_whitelist(self, username, authentication=None): + def check_allowed_groups(self, username, authentication=None): """ - If group_whitelist is configured, check if authenticating user is part of group. + If allowed_groups is configured, check if authenticating user is part of group. """ - if not self.group_whitelist: + if not self.allowed_groups: return False - for grnam in self.group_whitelist: + for grnam in self.allowed_groups: try: group = self._getgrnam(grnam) except KeyError: @@ -762,7 +880,13 @@ class LocalAuthenticator(Authenticator): Tested to work on FreeBSD and Linux, at least. """ name = user.name - cmd = [arg.replace('USERNAME', name) for arg in self.add_user_cmd] + [name] + cmd = [arg.replace('USERNAME', name) for arg in self.add_user_cmd] + try: + uid = self.uids[name] + cmd += ['--uid', '%d' % uid] + except KeyError: + self.log.debug("No UID for user %s" % name) + cmd += [name] self.log.info("Creating user: %s", ' '.join(map(pipes.quote, cmd))) p = Popen(cmd, stdout=PIPE, stderr=STDOUT) p.wait() @@ -828,7 +952,7 @@ class PAMAuthenticator(LocalAuthenticator): Authoritative list of user groups that determine admin access. Users not in these groups can still be granted admin status through admin_users. - White/blacklisting rules still apply. + allowed/blocked rules still apply. """ ).tag(config=True) @@ -966,10 +1090,21 @@ class PAMAuthenticator(LocalAuthenticator): uid = pwd.getpwnam(username).pw_uid username = pwd.getpwuid(uid).pw_name username = self.username_map.get(username, username) + return username else: return super().normalize_username(username) +for _old_name, _new_name, _version in [ + ("check_group_whitelist", "check_group_allowed", "1.2"), +]: + setattr( + LocalAuthenticator, + _old_name, + _deprecated_method(_old_name, _new_name, _version), + ) + + class DummyAuthenticator(Authenticator): """Dummy Authenticator for testing diff --git a/jupyterhub/crypto.py b/jupyterhub/crypto.py index 57bd00d3..039201b1 100644 --- a/jupyterhub/crypto.py +++ b/jupyterhub/crypto.py @@ -6,7 +6,6 @@ from concurrent.futures import ThreadPoolExecutor from traitlets import Any from traitlets import default -from traitlets import Dict from traitlets import Integer from traitlets import List from traitlets import observe diff --git a/jupyterhub/event-schemas/server-actions/v1.yaml b/jupyterhub/event-schemas/server-actions/v1.yaml new file mode 100644 index 00000000..35ffad67 --- /dev/null +++ b/jupyterhub/event-schemas/server-actions/v1.yaml @@ -0,0 +1,59 @@ +"$id": hub.jupyter.org/server-action +version: 1 +title: JupyterHub server events +description: | + Record actions on user servers made via JupyterHub. + + JupyterHub can perform various actions on user servers via + direct interaction from users, or via the API. This event is + recorded whenever either of those happen. + + Limitations: + + 1. This does not record all server starts / stops, only those + explicitly performed by JupyterHub. For example, a user's server + can go down because the node it was running on dies. That will + not cause an event to be recorded, since it was not initiated + by JupyterHub. In practice this happens often, so this is not + a complete record. + 2. Events are only recorded when an action succeeds. +type: object +required: +- action +- username +- servername +properties: + action: + enum: + - start + - stop + description: | + Action performed by JupyterHub. + + This is a required field. + + Possibl Values: + + 1. start + A user's server was successfully started + + 2. stop + A user's server was successfully stopped + username: + type: string + description: | + Name of the user whose server this action was performed on. + + This is the normalized name used by JupyterHub itself, + which is derived from the authentication provider used but + might not be the same as used in the authentication provider. + servername: + type: string + description: | + Name of the server this action was performed on. + + JupyterHub supports each user having multiple servers with + arbitrary names, and this field specifies the name of the + server. + + The 'default' server is denoted by the empty string diff --git a/jupyterhub/handlers/base.py b/jupyterhub/handlers/base.py index 83449750..7fc28715 100644 --- a/jupyterhub/handlers/base.py +++ b/jupyterhub/handlers/base.py @@ -2,7 +2,6 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. import asyncio -import copy import json import math import random @@ -26,13 +25,14 @@ from tornado.httputil import HTTPHeaders from tornado.httputil import url_concat from tornado.ioloop import IOLoop from tornado.log import app_log -from tornado.web import MissingArgumentError +from tornado.web import addslash from tornado.web import RequestHandler from .. import __version__ from .. import orm from ..metrics import PROXY_ADD_DURATION_SECONDS -from ..metrics import ProxyAddStatus +from ..metrics import PROXY_DELETE_DURATION_SECONDS +from ..metrics import ProxyDeleteStatus from ..metrics import RUNNING_SERVERS from ..metrics import SERVER_POLL_DURATION_SECONDS from ..metrics import SERVER_SPAWN_DURATION_SECONDS @@ -139,6 +139,10 @@ class BaseHandler(RequestHandler): def hub(self): return self.settings['hub'] + @property + def app(self): + return self.settings['app'] + @property def proxy(self): return self.settings['proxy'] @@ -155,6 +159,10 @@ class BaseHandler(RequestHandler): def oauth_provider(self): return self.settings['oauth_provider'] + @property + def eventlog(self): + return self.settings['eventlog'] + def finish(self, *args, **kwargs): """Roll back any uncommitted transactions from the handler.""" if self.db.dirty: @@ -248,10 +256,40 @@ class BaseHandler(RequestHandler): orm_token = orm.OAuthAccessToken.find(self.db, token) if orm_token is None: return None - orm_token.last_activity = orm_token.user.last_activity = datetime.utcnow() - self.db.commit() + + now = datetime.utcnow() + recorded = self._record_activity(orm_token, now) + if self._record_activity(orm_token.user, now) or recorded: + self.db.commit() return self._user_from_orm(orm_token.user) + def _record_activity(self, obj, timestamp=None): + """record activity on an ORM object + + If last_activity was more recent than self.activity_resolution seconds ago, + do nothing to avoid unnecessarily frequent database commits. + + Args: + obj: an ORM object with a last_activity attribute + timestamp (datetime, optional): the timestamp of activity to register. + Returns: + recorded (bool): True if activity was recorded, False if not. + """ + if timestamp is None: + timestamp = datetime.utcnow() + resolution = self.settings.get("activity_resolution", 0) + if not obj.last_activity or resolution == 0: + self.log.debug("Recording first activity for %s", obj) + obj.last_activity = timestamp + return True + if (timestamp - obj.last_activity).total_seconds() > resolution: + # this debug line will happen just too often + # uncomment to debug last_activity updates + # self.log.debug("Recording activity for %s", obj) + obj.last_activity = timestamp + return True + return False + async def refresh_auth(self, user, force=False): """Refresh user authentication info @@ -322,14 +360,15 @@ class BaseHandler(RequestHandler): # record token activity now = datetime.utcnow() - orm_token.last_activity = now + recorded = self._record_activity(orm_token, now) if orm_token.user: # FIXME: scopes should give us better control than this # don't consider API requests originating from a server # to be activity from the user if not orm_token.note.startswith("Server at "): - orm_token.user.last_activity = now - self.db.commit() + recorded = self._record_activity(orm_token.user, now) or recorded + if recorded: + self.db.commit() if orm_token.service: return orm_token.service @@ -359,8 +398,8 @@ class BaseHandler(RequestHandler): clear() return # update user activity - user.last_activity = datetime.utcnow() - self.db.commit() + if self._record_activity(user): + self.db.commit() return user def _user_from_orm(self, orm_user): @@ -452,6 +491,8 @@ class BaseHandler(RequestHandler): path=url_path_join(self.base_url, 'services'), **kwargs ) + # Reset _jupyterhub_user + self._jupyterhub_user = None def _set_cookie(self, key, value, encrypted=True, **overrides): """Setting any cookie should go through here @@ -594,8 +635,15 @@ class BaseHandler(RequestHandler): ) if not next_url: - # custom default URL - next_url = default or self.default_url + # custom default URL, usually passed because user landed on that page but was not logged in + if default: + next_url = default + else: + # As set in jupyterhub_config.py + if callable(self.default_url): + next_url = self.default_url(self) + else: + next_url = self.default_url if not next_url: # default URL after login @@ -610,8 +658,42 @@ class BaseHandler(RequestHandler): next_url = url_path_join(self.hub.base_url, 'spawn') else: next_url = url_path_join(self.hub.base_url, 'home') + + next_url = self.append_query_parameters(next_url, exclude=['next']) return next_url + def append_query_parameters(self, url, exclude=None): + """Append the current request's query parameters to the given URL. + + Supports an extra optional parameter ``exclude`` that when provided must + contain a list of parameters to be ignored, i.e. these parameters will + not be added to the URL. + + This is important to avoid infinite loops with the next parameter being + added over and over, for instance. + + The default value for ``exclude`` is an array with "next". This is useful + as most use cases in JupyterHub (all?) won't want to include the next + parameter twice (the next parameter is added elsewhere to the query + parameters). + + :param str url: a URL + :param list exclude: optional list of parameters to be ignored, defaults to + a list with "next" (to avoid redirect-loops) + :rtype (str) + """ + if exclude is None: + exclude = ['next'] + if self.request.query: + query_string = [ + param + for param in parse_qsl(self.request.query) + if param[0] not in exclude + ] + if query_string: + url = url_concat(url, query_string) + return url + async def auth_to_user(self, authenticated, user=None): """Persist data from .authenticate() or .refresh_user() to the User database @@ -632,9 +714,10 @@ class BaseHandler(RequestHandler): raise ValueError("Username doesn't match! %s != %s" % (username, user.name)) if user is None: - new_user = username not in self.users - user = self.user_from_username(username) + user = self.find_user(username) + new_user = user is None if new_user: + user = self.user_from_username(username) await maybe_future(self.authenticator.add_user(user)) # Only set `admin` if the authenticator returned an explicit value. if admin is not None and admin != user.admin: @@ -727,6 +810,7 @@ class BaseHandler(RequestHandler): active_counts['spawn_pending'] + active_counts['proxy_pending'] ) active_count = active_counts['active'] + RUNNING_SERVERS.set(active_count) concurrent_spawn_limit = self.concurrent_spawn_limit active_server_limit = self.active_server_limit @@ -810,10 +894,14 @@ class BaseHandler(RequestHandler): "User %s took %.3f seconds to start", user_server_name, toc - tic ) self.statsd.timing('spawner.success', (toc - tic) * 1000) - RUNNING_SERVERS.inc() SERVER_SPAWN_DURATION_SECONDS.labels( status=ServerSpawnStatus.success ).observe(time.perf_counter() - spawn_start_time) + self.eventlog.record_event( + 'hub.jupyter.org/server-action', + 1, + {'action': 'start', 'username': user.name, 'servername': server_name}, + ) proxy_add_start_time = time.perf_counter() spawner._proxy_pending = True try: @@ -822,12 +910,13 @@ class BaseHandler(RequestHandler): PROXY_ADD_DURATION_SECONDS.labels(status='success').observe( time.perf_counter() - proxy_add_start_time ) + RUNNING_SERVERS.inc() except Exception: self.log.exception("Failed to add %s to proxy!", user_server_name) self.log.error( "Stopping %s to avoid inconsistent state", user_server_name ) - await user.stop() + await user.stop(server_name) PROXY_ADD_DURATION_SECONDS.labels(status='failure').observe( time.perf_counter() - proxy_add_start_time ) @@ -844,7 +933,7 @@ class BaseHandler(RequestHandler): # clear spawner._spawn_future when it's done # keep an exception around, though, to prevent repeated implicit spawns # if spawn is failing - if f.exception() is None: + if f.cancelled() or f.exception() is None: spawner._spawn_future = None # Now we're all done. clear _spawn_pending flag spawner._spawn_pending = False @@ -855,11 +944,14 @@ class BaseHandler(RequestHandler): # update failure count and abort if consecutive failure limit # is reached def _track_failure_count(f): - if f.exception() is None: + if f.cancelled() or f.exception() is None: # spawn succeeded, reset failure count self.settings['failure_count'] = 0 return # spawn failed, increment count and abort if limit reached + SERVER_SPAWN_DURATION_SECONDS.labels( + status=ServerSpawnStatus.failure + ).observe(time.perf_counter() - spawn_start_time) self.settings.setdefault('failure_count', 0) self.settings['failure_count'] += 1 failure_count = self.settings['failure_count'] @@ -892,13 +984,16 @@ class BaseHandler(RequestHandler): # waiting_for_response indicates server process has started, # but is yet to become responsive. if spawner._spawn_pending and not spawner._waiting_for_response: - # still in Spawner.start, which is taking a long time - # we shouldn't poll while spawn is incomplete. - self.log.warning( - "User %s is slow to start (timeout=%s)", - user_server_name, - self.slow_spawn_timeout, - ) + # If slow_spawn_timeout is intentionally disabled then we + # don't need to log a warning, just return. + if self.slow_spawn_timeout > 0: + # still in Spawner.start, which is taking a long time + # we shouldn't poll while spawn is incomplete. + self.log.warning( + "User %s is slow to start (timeout=%s)", + user_server_name, + self.slow_spawn_timeout, + ) return # start has finished, but the server hasn't come up @@ -961,7 +1056,18 @@ class BaseHandler(RequestHandler): self.log.warning( "User %s server stopped, with exit code: %s", user.name, status ) - await self.proxy.delete_user(user, server_name) + proxy_deletion_start_time = time.perf_counter() + try: + await self.proxy.delete_user(user, server_name) + PROXY_DELETE_DURATION_SECONDS.labels( + status=ProxyDeleteStatus.success + ).observe(time.perf_counter() - proxy_deletion_start_time) + except Exception: + PROXY_DELETE_DURATION_SECONDS.labels( + status=ProxyDeleteStatus.failure + ).observe(time.perf_counter() - proxy_deletion_start_time) + raise + await user.stop(server_name) async def stop_single_user(self, user, server_name=''): @@ -984,17 +1090,32 @@ class BaseHandler(RequestHandler): tic = time.perf_counter() try: await self.proxy.delete_user(user, server_name) + PROXY_DELETE_DURATION_SECONDS.labels( + status=ProxyDeleteStatus.success + ).observe(time.perf_counter() - tic) + await user.stop(server_name) toc = time.perf_counter() self.log.info( "User %s server took %.3f seconds to stop", user.name, toc - tic ) self.statsd.timing('spawner.stop', (toc - tic) * 1000) - RUNNING_SERVERS.dec() SERVER_STOP_DURATION_SECONDS.labels( status=ServerStopStatus.success ).observe(toc - tic) + self.eventlog.record_event( + 'hub.jupyter.org/server-action', + 1, + { + 'action': 'stop', + 'username': user.name, + 'servername': server_name, + }, + ) except: + PROXY_DELETE_DURATION_SECONDS.labels( + status=ProxyDeleteStatus.failure + ).observe(time.perf_counter() - tic) SERVER_STOP_DURATION_SECONDS.labels( status=ServerStopStatus.failure ).observe(time.perf_counter() - tic) @@ -1009,7 +1130,10 @@ class BaseHandler(RequestHandler): except gen.TimeoutError: # hit timeout, but stop is still pending self.log.warning( - "User %s:%s server is slow to stop", user.name, server_name + "User %s:%s server is slow to stop (timeout=%s)", + user.name, + server_name, + self.slow_stop_timeout, ) # return handle on the future for hooking up callbacks @@ -1032,16 +1156,36 @@ class BaseHandler(RequestHandler): "home page.".format(home=home) ) - def get_template(self, name): - """Return the jinja template object for a given name""" - return self.settings['jinja2_env'].get_template(name) + def get_template(self, name, sync=False): + """ + Return the jinja template object for a given name - def render_template(self, name, **ns): + If sync is True, we return a Template that is compiled without async support. + Only those can be used in synchronous code. + + If sync is False, we return a Template that is compiled with async support + """ + if sync: + key = 'jinja2_env_sync' + else: + key = 'jinja2_env' + return self.settings[key].get_template(name) + + def render_template(self, name, sync=False, **ns): + """ + Render jinja2 template + + If sync is set to True, we return an awaitable + If sync is set to False, we render the template & return a string + """ template_ns = {} template_ns.update(self.template_namespace) template_ns.update(ns) - template = self.get_template(name) - return template.render(**template_ns) + template = self.get_template(name, sync) + if sync: + return template.render(**template_ns) + else: + return template.render_async(**template_ns) @property def template_namespace(self): @@ -1055,11 +1199,24 @@ class BaseHandler(RequestHandler): logout_url=self.settings['logout_url'], static_url=self.static_url, version_hash=self.version_hash, + services=self.get_accessible_services(user), ) if self.settings['template_vars']: ns.update(self.settings['template_vars']) return ns + def get_accessible_services(self, user): + accessible_services = [] + if user is None: + return accessible_services + for service in self.services.values(): + if not service.url: + continue + if not service.display: + continue + accessible_services.append(service) + return accessible_services + def write_error(self, status_code, **kwargs): """render custom error pages""" exc_info = kwargs.get('exc_info') @@ -1103,17 +1260,19 @@ class BaseHandler(RequestHandler): # Content-Length must be recalculated. self.clear_header('Content-Length') - # render the template + # render_template is async, but write_error can't be! + # so we run it sync here, instead of making a sync version of render_template + try: - html = self.render_template('%s.html' % status_code, **ns) + html = self.render_template('%s.html' % status_code, sync=True, **ns) except TemplateNotFound: self.log.debug("No template for %d", status_code) try: - html = self.render_template('error.html', **ns) + html = self.render_template('error.html', sync=True, **ns) except: # In this case, any side effect must be avoided. ns['no_spawner_check'] = True - html = self.render_template('error.html', **ns) + html = self.render_template('error.html', sync=True, **ns) self.write(html) @@ -1325,7 +1484,9 @@ class UserUrlHandler(BaseHandler): return pending_url = url_concat( - url_path_join(self.hub.base_url, 'spawn-pending', user.name, server_name), + url_path_join( + self.hub.base_url, 'spawn-pending', user.escaped_name, server_name + ), {'next': self.request.uri}, ) if spawner.pending or spawner._failed: @@ -1337,13 +1498,20 @@ class UserUrlHandler(BaseHandler): # serve a page prompting for spawn and 503 error # visiting /user/:name no longer triggers implicit spawn # without explicit user action - self.set_status(503) spawn_url = url_concat( - url_path_join(self.hub.base_url, "spawn", user.name, server_name), + url_path_join(self.hub.base_url, "spawn", user.escaped_name, server_name), {"next": self.request.uri}, ) - html = self.render_template( - "not_running.html", user=user, server_name=server_name, spawn_url=spawn_url + self.set_status(503) + + auth_state = await user.get_auth_state() + html = await self.render_template( + "not_running.html", + user=user, + server_name=server_name, + spawn_url=spawn_url, + auth_state=auth_state, + implicit_spawn_seconds=self.settings.get("implicit_spawn_seconds", 0), ) self.finish(html) @@ -1416,19 +1584,51 @@ class UserRedirectHandler(BaseHandler): If the user is not logged in, send to login URL, redirecting back here. + If c.JupyterHub.user_redirect_hook is set, the return value of that + callable is used to generate the redirect URL. + .. versionadded:: 0.7 """ @web.authenticated - def get(self, path): - user = self.current_user - user_url = url_path_join(user.url, path) - if self.request.query: - user_url = url_concat(user_url, parse_qsl(self.request.query)) + async def get(self, path): + # If hook is present to generate URL to redirect to, use that instead + # of the default. The configurer is responsible for making sure this + # URL is right. If None is returned by the hook, we do our normal + # processing + url = None + if self.app.user_redirect_hook: + url = await maybe_future( + self.app.user_redirect_hook( + path, self.request, self.current_user, self.base_url + ) + ) + if url is None: + user = self.current_user + user_url = user.url - url = url_concat( - url_path_join(self.hub.base_url, "spawn", user.name), {"next": user_url} - ) + if self.app.default_server_name: + user_url = url_path_join(user_url, self.app.default_server_name) + + user_url = url_path_join(user_url, path) + if self.request.query: + user_url = url_concat(user_url, parse_qsl(self.request.query)) + + if self.app.default_server_name: + url = url_concat( + url_path_join( + self.hub.base_url, + "spawn", + user.escaped_name, + self.app.default_server_name, + ), + {"next": user_url}, + ) + else: + url = url_concat( + url_path_join(self.hub.base_url, "spawn", user.escaped_name), + {"next": user_url}, + ) self.redirect(url) @@ -1450,10 +1650,9 @@ class CSPReportHandler(BaseHandler): class AddSlashHandler(BaseHandler): """Handler for adding trailing slash to URLs that need them""" - def get(self, *args): - src = urlparse(self.request.uri) - dest = src._replace(path=src.path + '/') - self.redirect(urlunparse(dest)) + @addslash + def get(self): + pass default_handlers = [ diff --git a/jupyterhub/handlers/login.py b/jupyterhub/handlers/login.py index f1bd7c45..605cd580 100644 --- a/jupyterhub/handlers/login.py +++ b/jupyterhub/handlers/login.py @@ -72,14 +72,14 @@ class LogoutHandler(BaseHandler): Override this function to set a custom logout page. """ if self.authenticator.auto_login: - html = self.render_template('logout.html') + html = await self.render_template('logout.html') self.finish(html) else: self.redirect(self.settings['login_url'], permanent=False) async def get(self): """Log the user out, call the custom action, forward the user - to the logout page + to the logout page """ await self.default_handle_logout() await self.handle_logout() @@ -132,7 +132,7 @@ class LoginHandler(BaseHandler): self.redirect(auto_login_url) return username = self.get_argument('username', default='') - self.finish(self._render(username=username)) + self.finish(await self._render(username=username)) async def post(self): # parse the arguments dict @@ -149,7 +149,7 @@ class LoginHandler(BaseHandler): self._jupyterhub_user = user self.redirect(self.get_next_url(user)) else: - html = self._render( + html = await self._render( login_error='Invalid username or password', username=data['username'] ) self.finish(html) diff --git a/jupyterhub/handlers/metrics.py b/jupyterhub/handlers/metrics.py index f7a95b62..0f63d9c3 100644 --- a/jupyterhub/handlers/metrics.py +++ b/jupyterhub/handlers/metrics.py @@ -1,7 +1,6 @@ from prometheus_client import CONTENT_TYPE_LATEST from prometheus_client import generate_latest from prometheus_client import REGISTRY -from tornado import gen from ..utils import metrics_authentication from .base import BaseHandler diff --git a/jupyterhub/handlers/pages.py b/jupyterhub/handlers/pages.py index d93f2638..43f975a2 100644 --- a/jupyterhub/handlers/pages.py +++ b/jupyterhub/handlers/pages.py @@ -2,20 +2,21 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. import asyncio -import copy import time from collections import defaultdict from datetime import datetime from http.client import responses from jinja2 import TemplateNotFound -from tornado import gen from tornado import web from tornado.httputil import url_concat +from tornado.httputil import urlparse +from .. import __version__ from .. import orm from ..metrics import SERVER_POLL_DURATION_SECONDS from ..metrics import ServerPollStatus +from ..pagination import Pagination from ..utils import admin_only from ..utils import maybe_future from ..utils import url_path_join @@ -61,12 +62,14 @@ class HomeHandler(BaseHandler): # to establish that this is an explicit spawn request rather # than an implicit one, which can be caused by any link to `/user/:name(/:server_name)` if user.active: - url = url_path_join(self.base_url, 'user', user.name) + url = url_path_join(self.base_url, 'user', user.escaped_name) else: - url = url_path_join(self.hub.base_url, 'spawn', user.name) + url = url_path_join(self.hub.base_url, 'spawn', user.escaped_name) - html = self.render_template( + auth_state = await user.get_auth_state() + html = await self.render_template( 'home.html', + auth_state=auth_state, user=user, url=url, allow_named_servers=self.allow_named_servers, @@ -89,10 +92,12 @@ class SpawnHandler(BaseHandler): default_url = None - def _render_form(self, for_user, spawner_options_form, message=''): - return self.render_template( + async def _render_form(self, for_user, spawner_options_form, message=''): + auth_state = await for_user.get_auth_state() + return await self.render_template( 'spawn.html', for_user=for_user, + auth_state=auth_state, spawner_options_form=spawner_options_form, error_message=message, url=self.request.uri, @@ -117,6 +122,23 @@ class SpawnHandler(BaseHandler): if user is None: raise web.HTTPError(404, "No such user: %s" % for_user) + if server_name: + if not self.allow_named_servers: + raise web.HTTPError(400, "Named servers are not enabled.") + if ( + self.named_server_limit_per_user > 0 + and server_name not in user.orm_spawners + ): + named_spawners = list(user.all_spawners(include_default=False)) + if self.named_server_limit_per_user <= len(named_spawners): + raise web.HTTPError( + 400, + "User {} already has the maximum of {} named servers." + " One must be deleted before a new server can be created".format( + user.name, self.named_server_limit_per_user + ), + ) + if not self.allow_named_servers and user.running: url = self.get_next_url(user, default=user.server_url(server_name)) self.log.info("User is running: %s", user.name) @@ -127,17 +149,8 @@ class SpawnHandler(BaseHandler): server_name = '' spawner = user.spawners[server_name] - # resolve `?next=...`, falling back on the spawn-pending url - # must not be /user/server for named servers, - # which may get handled by the default server if they aren't ready yet - pending_url = url_path_join( - self.hub.base_url, "spawn-pending", user.name, server_name - ) - - if self.get_argument('next', None): - # preserve `?next=...` through spawn-pending - pending_url = url_concat(pending_url, {'next': self.get_argument('next')}) + pending_url = self._get_pending_url(user, server_name) # spawner is active, redirect back to get progress, etc. if spawner.ready: @@ -153,26 +166,56 @@ class SpawnHandler(BaseHandler): # Add handler to spawner here so you can access query params in form rendering. spawner.handler = self + + # auth_state may be an input to options form, + # so resolve the auth state hook here + auth_state = await user.get_auth_state() + await spawner.run_auth_state_hook(auth_state) + + # Try to start server directly when query arguments are passed. + error_message = '' + query_options = {} + for key, byte_list in self.request.query_arguments.items(): + query_options[key] = [bs.decode('utf8') for bs in byte_list] + + # 'next' is reserved argument for redirect after spawn + query_options.pop('next', None) + + if len(query_options) > 0: + try: + self.log.debug( + "Triggering spawn with supplied query arguments for %s", + spawner._log_name, + ) + options = await maybe_future(spawner.options_from_query(query_options)) + pending_url = self._get_pending_url(user, server_name) + return await self._wrap_spawn_single_user( + user, server_name, spawner, pending_url, options + ) + except Exception as e: + self.log.error( + "Failed to spawn single-user server with query arguments", + exc_info=True, + ) + error_message = str(e) + # fallback to behavior without failing query arguments + spawner_options_form = await spawner.get_options_form() if spawner_options_form: self.log.debug("Serving options form for %s", spawner._log_name) - form = self._render_form( - for_user=user, spawner_options_form=spawner_options_form + form = await self._render_form( + for_user=user, + spawner_options_form=spawner_options_form, + message=error_message, ) self.finish(form) else: self.log.debug( "Triggering spawn with default options for %s", spawner._log_name ) - # Explicit spawn request: clear _spawn_future - # which may have been saved to prevent implicit spawns - # after a failure. - if spawner._spawn_future and spawner._spawn_future.done(): - spawner._spawn_future = None - # not running, no form. Trigger spawn and redirect back to /user/:name - f = asyncio.ensure_future(self.spawn_single_user(user, server_name)) - await asyncio.wait([f], timeout=1) - self.redirect(pending_url) + return await self._wrap_spawn_single_user( + user, server_name, spawner, pending_url + ) @web.authenticated async def post(self, for_user=None, server_name=''): @@ -202,14 +245,20 @@ class SpawnHandler(BaseHandler): for key, byte_list in self.request.files.items(): form_options["%s_file" % key] = byte_list try: + self.log.debug( + "Triggering spawn with supplied form options for %s", spawner._log_name + ) options = await maybe_future(spawner.options_from_form(form_options)) - await self.spawn_single_user(user, server_name=server_name, options=options) + pending_url = self._get_pending_url(user, server_name) + return await self._wrap_spawn_single_user( + user, server_name, spawner, pending_url, options + ) except Exception as e: self.log.error( "Failed to spawn single-user server with form", exc_info=True ) spawner_options_form = await user.spawner.get_options_form() - form = self._render_form( + form = await self._render_form( for_user=user, spawner_options_form=spawner_options_form, message=str(e) ) self.finish(form) @@ -219,11 +268,52 @@ class SpawnHandler(BaseHandler): next_url = self.get_next_url( user, default=url_path_join( - self.hub.base_url, "spawn-pending", user.name, server_name + self.hub.base_url, "spawn-pending", user.escaped_name, server_name ), ) self.redirect(next_url) + def _get_pending_url(self, user, server_name): + # resolve `?next=...`, falling back on the spawn-pending url + # must not be /user/server for named servers, + # which may get handled by the default server if they aren't ready yet + + pending_url = url_path_join( + self.hub.base_url, "spawn-pending", user.escaped_name, server_name + ) + + pending_url = self.append_query_parameters(pending_url, exclude=['next']) + + if self.get_argument('next', None): + # preserve `?next=...` through spawn-pending + pending_url = url_concat(pending_url, {'next': self.get_argument('next')}) + + return pending_url + + async def _wrap_spawn_single_user( + self, user, server_name, spawner, pending_url, options=None + ): + # Explicit spawn request: clear _spawn_future + # which may have been saved to prevent implicit spawns + # after a failure. + if spawner._spawn_future and spawner._spawn_future.done(): + spawner._spawn_future = None + # not running, no form. Trigger spawn and redirect back to /user/:name + f = asyncio.ensure_future( + self.spawn_single_user(user, server_name, options=options) + ) + done, pending = await asyncio.wait([f], timeout=1) + # If spawn_single_user throws an exception, raise a 500 error + # otherwise it may cause a redirect loop + if f.done() and f.exception(): + exc = f.exception() + raise web.HTTPError( + 500, + "Error in Authenticator.pre_spawn_start: %s %s" + % (type(exc).__name__, str(exc)), + ) + return self.redirect(pending_url) + class SpawnPendingHandler(BaseHandler): """Handle /hub/spawn-pending/:user/:server @@ -270,6 +360,8 @@ class SpawnPendingHandler(BaseHandler): # if spawning fails for any reason, point users to /hub/home to retry self.extra_error_html = self.spawn_home_error + auth_state = await user.get_auth_state() + # First, check for previous failure. if ( not spawner.active @@ -282,11 +374,14 @@ class SpawnPendingHandler(BaseHandler): # We should point the user to Home if the most recent spawn failed. exc = spawner._spawn_future.exception() self.log.error("Previous spawn for %s failed: %s", spawner._log_name, exc) - spawn_url = url_path_join(self.hub.base_url, "spawn", user.escaped_name) + spawn_url = url_path_join( + self.hub.base_url, "spawn", user.escaped_name, server_name + ) self.set_status(500) - html = self.render_template( + html = await self.render_template( "not_running.html", user=user, + auth_state=auth_state, server_name=server_name, spawn_url=spawn_url, failed=True, @@ -307,8 +402,12 @@ class SpawnPendingHandler(BaseHandler): page = "stop_pending.html" else: page = "spawn_pending.html" - html = self.render_template( - page, user=user, spawner=spawner, progress_url=spawner._progress_url + html = await self.render_template( + page, + user=user, + spawner=spawner, + progress_url=spawner._progress_url, + auth_state=auth_state, ) self.finish(html) return @@ -327,10 +426,13 @@ class SpawnPendingHandler(BaseHandler): # further, set status to 404 because this is not # serving the expected page if status is not None: - spawn_url = url_path_join(self.hub.base_url, "spawn", user.escaped_name) - html = self.render_template( + spawn_url = url_path_join( + self.hub.base_url, "spawn", user.escaped_name, server_name + ) + html = await self.render_template( "not_running.html", user=user, + auth_state=auth_state, server_name=server_name, spawn_url=spawn_url, ) @@ -348,14 +450,18 @@ class SpawnPendingHandler(BaseHandler): class AdminHandler(BaseHandler): """Render the admin page.""" + @web.authenticated @admin_only - def get(self): + async def get(self): + page, per_page, offset = Pagination(config=self.config).get_page_args(self) + available = {'name', 'admin', 'running', 'last_activity'} default_sort = ['admin', 'name'] mapping = {'running': orm.Spawner.server_id} for name in available: if name not in mapping: - mapping[name] = getattr(orm.User, name) + table = orm.User if name != "last_activity" else orm.Spawner + mapping[name] = getattr(table, name) default_order = { 'name': 'asc', @@ -390,23 +496,41 @@ class AdminHandler(BaseHandler): # get User.col.desc() order objects ordered = [getattr(c, o)() for c, o in zip(cols, orders)] - users = self.db.query(orm.User).outerjoin(orm.Spawner).order_by(*ordered) + users = ( + self.db.query(orm.User) + .outerjoin(orm.Spawner) + .order_by(*ordered) + .limit(per_page) + .offset(offset) + ) users = [self._user_from_orm(u) for u in users] - from itertools import chain running = [] for u in users: running.extend(s for s in u.spawners.values() if s.active) - html = self.render_template( + total = self.db.query(orm.User.id).count() + pagination = Pagination( + url=self.request.uri, + total=total, + page=page, + per_page=per_page, + config=self.config, + ) + + auth_state = await self.current_user.get_auth_state() + html = await self.render_template( 'admin.html', current_user=self.current_user, + auth_state=auth_state, admin_access=self.settings.get('admin_access', False), users=users, running=running, sort={s: o for s, o in zip(sorts, orders)}, allow_named_servers=self.allow_named_servers, named_server_limit_per_user=self.named_server_limit_per_user, + server_version='{} {}'.format(__version__, self.version_hash), + pagination=pagination, ) self.finish(html) @@ -415,7 +539,7 @@ class TokenPageHandler(BaseHandler): """Handler for page requesting new API tokens""" @web.authenticated - def get(self): + async def get(self): never = datetime(1900, 1, 1) user = self.current_user @@ -484,8 +608,12 @@ class TokenPageHandler(BaseHandler): oauth_clients = sorted(oauth_clients, key=sort_key, reverse=True) - html = self.render_template( - 'token.html', api_tokens=api_tokens, oauth_clients=oauth_clients + auth_state = await self.current_user.get_auth_state() + html = await self.render_template( + 'token.html', + api_tokens=api_tokens, + oauth_clients=oauth_clients, + auth_state=auth_state, ) self.finish(html) @@ -493,7 +621,7 @@ class TokenPageHandler(BaseHandler): class ProxyErrorHandler(BaseHandler): """Handler for rendering proxy error pages""" - def get(self, status_code_s): + async def get(self, status_code_s): status_code = int(status_code_s) status_message = responses.get(status_code, 'Unknown HTTP Error') # build template namespace @@ -517,19 +645,23 @@ class ProxyErrorHandler(BaseHandler): self.set_header('Content-Type', 'text/html') # render the template try: - html = self.render_template('%s.html' % status_code, **ns) + html = await self.render_template('%s.html' % status_code, **ns) except TemplateNotFound: self.log.debug("No template for %d", status_code) - html = self.render_template('error.html', **ns) + html = await self.render_template('error.html', **ns) self.write(html) class HealthCheckHandler(BaseHandler): - """Answer to health check""" + """Serve health check probes as quickly as possible""" - def get(self, *args): - self.finish() + # There is nothing for us to do other than return a positive + # HTTP status code as quickly as possible for GET or HEAD requests + def get(self): + pass + + head = get default_handlers = [ diff --git a/jupyterhub/log.py b/jupyterhub/log.py index a9992acf..f9fbffe8 100644 --- a/jupyterhub/log.py +++ b/jupyterhub/log.py @@ -12,6 +12,7 @@ from tornado.log import LogFormatter from tornado.web import HTTPError from tornado.web import StaticFileHandler +from .handlers.pages import HealthCheckHandler from .metrics import prometheus_log_method @@ -98,8 +99,12 @@ def _scrub_headers(headers): headers = dict(headers) if 'Authorization' in headers: auth = headers['Authorization'] - if auth.startswith('token '): - headers['Authorization'] = 'token [secret]' + if ' ' in auth: + auth_type = auth.split(' ', 1)[0] + else: + # no space, hide the whole thing in case there was a mistake + auth_type = '' + headers['Authorization'] = '{} [secret]'.format(auth_type) if 'Cookie' in headers: c = SimpleCookie(headers['Cookie']) redacted = [] @@ -123,7 +128,9 @@ def log_request(handler): """ status = handler.get_status() request = handler.request - if status == 304 or (status < 300 and isinstance(handler, StaticFileHandler)): + if status == 304 or ( + status < 300 and isinstance(handler, (StaticFileHandler, HealthCheckHandler)) + ): # static-file success and 304 Found are debug-level log_method = access_log.debug elif status < 400: @@ -162,7 +169,7 @@ def log_request(handler): location='', ) msg = "{status} {method} {uri}{location} ({user}@{ip}) {request_time:.2f}ms" - if status >= 500 and status != 502: + if status >= 500 and status not in {502, 503}: log_method(json.dumps(headers, indent=2)) elif status in {301, 302}: # log redirect targets diff --git a/jupyterhub/metrics.py b/jupyterhub/metrics.py index 558167ff..2bb9013b 100644 --- a/jupyterhub/metrics.py +++ b/jupyterhub/metrics.py @@ -39,16 +39,24 @@ RUNNING_SERVERS = Gauge( 'running_servers', 'the number of user servers currently running' ) -RUNNING_SERVERS.set(0) - -TOTAL_USERS = Gauge('total_users', 'toal number of users') - -TOTAL_USERS.set(0) +TOTAL_USERS = Gauge('total_users', 'total number of users') CHECK_ROUTES_DURATION_SECONDS = Histogram( 'check_routes_duration_seconds', 'Time taken to validate all routes in proxy' ) +HUB_STARTUP_DURATION_SECONDS = Histogram( + 'hub_startup_duration_seconds', 'Time taken for Hub to start' +) + +INIT_SPAWNERS_DURATION_SECONDS = Histogram( + 'init_spawners_duration_seconds', 'Time taken for spawners to initialize' +) + +PROXY_POLL_DURATION_SECONDS = Histogram( + 'proxy_poll_duration_seconds', 'duration for polling all routes from proxy' +) + class ServerSpawnStatus(Enum): """ @@ -139,14 +147,37 @@ for s in ServerStopStatus: SERVER_STOP_DURATION_SECONDS.labels(status=s) +PROXY_DELETE_DURATION_SECONDS = Histogram( + 'proxy_delete_duration_seconds', + 'duration for deleting user routes from proxy', + ['status'], +) + + +class ProxyDeleteStatus(Enum): + """ + Possible values for 'status' label of PROXY_DELETE_DURATION_SECONDS + """ + + success = 'success' + failure = 'failure' + + def __str__(self): + return self.value + + +for s in ProxyDeleteStatus: + PROXY_DELETE_DURATION_SECONDS.labels(status=s) + + def prometheus_log_method(handler): """ Tornado log handler for recording RED metrics. We record the following metrics: - Rate – the number of requests, per second, your services are serving. - Errors – the number of failed requests per second. - Duration – The amount of time each request takes expressed as a time interval. + Rate: the number of requests, per second, your services are serving. + Errors: the number of failed requests per second. + Duration: the amount of time each request takes expressed as a time interval. We use a fully qualified name of the handler as a label, rather than every url path to reduce cardinality. diff --git a/jupyterhub/oauth/provider.py b/jupyterhub/oauth/provider.py index 6157223f..7649f1ad 100644 --- a/jupyterhub/oauth/provider.py +++ b/jupyterhub/oauth/provider.py @@ -2,16 +2,11 @@ implements https://oauthlib.readthedocs.io/en/latest/oauth2/server.html """ -from datetime import datetime -from urllib.parse import urlparse - from oauthlib import uri_validate from oauthlib.oauth2 import RequestValidator from oauthlib.oauth2 import WebApplicationServer from oauthlib.oauth2.rfc6749.grant_types import authorization_code from oauthlib.oauth2.rfc6749.grant_types import base -from sqlalchemy.orm import scoped_session -from tornado import web from tornado.escape import url_escape from tornado.log import app_log @@ -250,7 +245,7 @@ class JupyterHubRequestValidator(RequestValidator): client=orm_client, code=code['code'], # oauth has 5 minutes to complete - expires_at=int(datetime.utcnow().timestamp() + 300), + expires_at=int(orm.OAuthCode.now() + 300), # TODO: persist oauth scopes # scopes=request.scopes, user=request.user.orm_user, @@ -347,7 +342,7 @@ class JupyterHubRequestValidator(RequestValidator): orm_access_token = orm.OAuthAccessToken( client=client, grant_type=orm.GrantType.authorization_code, - expires_at=datetime.utcnow().timestamp() + token['expires_in'], + expires_at=orm.OAuthAccessToken.now() + token['expires_in'], refresh_token=token['refresh_token'], # TODO: save scopes, # scopes=scopes, @@ -441,7 +436,7 @@ class JupyterHubRequestValidator(RequestValidator): Method is used by: - Authorization Code Grant """ - orm_code = self.db.query(orm.OAuthCode).filter_by(code=code).first() + orm_code = orm.OAuthCode.find(self.db, code=code) if orm_code is None: app_log.debug("No such code: %s", code) return False diff --git a/jupyterhub/objects.py b/jupyterhub/objects.py index d2e32639..e5aa9c1c 100644 --- a/jupyterhub/objects.py +++ b/jupyterhub/objects.py @@ -53,7 +53,7 @@ class Server(HasTraits): Never used in APIs, only logging, since it can be non-connectable value, such as '', meaning all interfaces. """ - if self.ip in {'', '0.0.0.0'}: + if self.ip in {'', '0.0.0.0', '::'}: return self.url.replace(self._connect_ip, self.ip or '*', 1) return self.url @@ -87,13 +87,13 @@ class Server(HasTraits): """The address to use when connecting to this server When `ip` is set to a real ip address, the same value is used. - When `ip` refers to 'all interfaces' (e.g. '0.0.0.0'), + When `ip` refers to 'all interfaces' (e.g. '0.0.0.0' or '::'), clients connect via hostname by default. Setting `connect_ip` explicitly overrides any default behavior. """ if self.connect_ip: return self.connect_ip - elif self.ip in {'', '0.0.0.0'}: + elif self.ip in {'', '0.0.0.0', '::'}: # if listening on all interfaces, default to hostname for connect return socket.gethostname() else: @@ -149,7 +149,12 @@ class Server(HasTraits): if self.connect_url: parsed = urlparse(self.connect_url) return "{proto}://{host}".format(proto=parsed.scheme, host=parsed.netloc) - return "{proto}://{ip}:{port}".format( + + if ':' in self._connect_ip: + fmt = "{proto}://[{ip}]:{port}" + else: + fmt = "{proto}://{ip}:{port}" + return fmt.format( proto=self.proto, ip=self._connect_ip, port=self._connect_port ) @@ -213,8 +218,4 @@ class Hub(Server): return url_path_join(self.url, 'api') def __repr__(self): - return "<%s %s:%s>" % ( - self.__class__.__name__, - self.server.ip, - self.server.port, - ) + return "<%s %s:%s>" % (self.__class__.__name__, self.ip, self.port) diff --git a/jupyterhub/orm.py b/jupyterhub/orm.py index 471dd4e2..cc96ca88 100644 --- a/jupyterhub/orm.py +++ b/jupyterhub/orm.py @@ -26,6 +26,7 @@ from sqlalchemy import select from sqlalchemy import Table from sqlalchemy import Unicode from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import backref from sqlalchemy.orm import interfaces from sqlalchemy.orm import object_session from sqlalchemy.orm import relationship @@ -230,7 +231,12 @@ class Spawner(Base): user_id = Column(Integer, ForeignKey('users.id', ondelete='CASCADE')) server_id = Column(Integer, ForeignKey('servers.id', ondelete='SET NULL')) - server = relationship(Server, cascade="all") + server = relationship( + Server, + backref=backref('spawner', uselist=False), + single_parent=True, + cascade="all, delete-orphan", + ) state = Column(JSONDict) name = Column(Unicode(255)) @@ -282,7 +288,12 @@ class Service(Base): # service-specific interface _server_id = Column(Integer, ForeignKey('servers.id', ondelete='SET NULL')) - server = relationship(Server, cascade='all') + server = relationship( + Server, + backref=backref('service', uselist=False), + single_parent=True, + cascade="all, delete-orphan", + ) pid = Column(Integer) def new_api_token(self, token=None, **kwargs): @@ -300,7 +311,46 @@ class Service(Base): return db.query(cls).filter(cls.name == name).first() -class Hashed(object): +class Expiring: + """Mixin for expiring entries + + Subclass must define at least expires_at property, + which should be unix timestamp or datetime object + """ + + now = utcnow # funciton, must return float timestamp or datetime + expires_at = None # must be defined + + @property + def expires_in(self): + """Property returning expiration in seconds from now + + or None + """ + if self.expires_at: + delta = self.expires_at - self.now() + if isinstance(delta, timedelta): + delta = delta.total_seconds() + return delta + else: + return None + + @classmethod + def purge_expired(cls, db): + """Purge expired API Tokens from the database""" + now = cls.now() + deleted = False + for obj in ( + db.query(cls).filter(cls.expires_at != None).filter(cls.expires_at < now) + ): + app_log.debug("Purging expired %s", obj) + deleted = True + db.delete(obj) + if deleted: + db.commit() + + +class Hashed(Expiring): """Mixin for tables with hashed tokens""" prefix_length = 4 @@ -357,11 +407,21 @@ class Hashed(object): """Start the query for matching token. Returns an SQLAlchemy query already filtered by prefix-matches. + + .. versionchanged:: 1.2 + + Excludes expired matches. """ prefix = token[: cls.prefix_length] # since we can't filter on hashed values, filter on prefix # so we aren't comparing with all tokens - return db.query(cls).filter(bindparam('prefix', prefix).startswith(cls.prefix)) + prefix_match = db.query(cls).filter( + bindparam('prefix', prefix).startswith(cls.prefix) + ) + prefix_match = prefix_match.filter( + or_(cls.expires_at == None, cls.expires_at >= cls.now()) + ) + return prefix_match @classmethod def find(cls, db, token): @@ -397,6 +457,7 @@ class APIToken(Hashed, Base): return 'a%i' % self.id # token metadata for bookkeeping + now = datetime.utcnow # for expiry created = Column(DateTime, default=datetime.utcnow) expires_at = Column(DateTime, default=None, nullable=True) last_activity = Column(DateTime) @@ -417,20 +478,6 @@ class APIToken(Hashed, Base): cls=self.__class__.__name__, pre=self.prefix, kind=kind, name=name ) - @classmethod - def purge_expired(cls, db): - """Purge expired API Tokens from the database""" - now = utcnow() - deleted = False - for token in ( - db.query(cls).filter(cls.expires_at != None).filter(cls.expires_at < now) - ): - app_log.debug("Purging expired %s", token) - deleted = True - db.delete(token) - if deleted: - db.commit() - @classmethod def find(cls, db, token, *, kind=None): """Find a token object by value. @@ -441,9 +488,6 @@ class APIToken(Hashed, Base): `kind='service'` only returns API tokens for services """ prefix_match = cls.find_prefix(db, token) - prefix_match = prefix_match.filter( - or_(cls.expires_at == None, cls.expires_at >= utcnow()) - ) if kind == 'user': prefix_match = prefix_match.filter(cls.user_id != None) elif kind == 'service': @@ -486,7 +530,7 @@ class APIToken(Hashed, Base): assert service.id is not None orm_token.service = service if expires_in is not None: - orm_token.expires_at = utcnow() + timedelta(seconds=expires_in) + orm_token.expires_at = cls.now() + timedelta(seconds=expires_in) db.add(orm_token) db.commit() return token @@ -510,6 +554,10 @@ class OAuthAccessToken(Hashed, Base): __tablename__ = 'oauth_access_tokens' id = Column(Integer, primary_key=True, autoincrement=True) + @staticmethod + def now(): + return datetime.utcnow().timestamp() + @property def api_id(self): return 'o%i' % self.id @@ -536,11 +584,12 @@ class OAuthAccessToken(Hashed, Base): last_activity = Column(DateTime, nullable=True) def __repr__(self): - return "<{cls}('{prefix}...', client_id={client_id!r}, user={user!r}>".format( + return "<{cls}('{prefix}...', client_id={client_id!r}, user={user!r}, expires_in={expires_in}>".format( cls=self.__class__.__name__, client_id=self.client_id, user=self.user and self.user.name, prefix=self.prefix, + expires_in=self.expires_in, ) @classmethod @@ -557,8 +606,9 @@ class OAuthAccessToken(Hashed, Base): return orm_token -class OAuthCode(Base): +class OAuthCode(Expiring, Base): __tablename__ = 'oauth_codes' + id = Column(Integer, primary_key=True, autoincrement=True) client_id = Column( Unicode(255), ForeignKey('oauth_clients.identifier', ondelete='CASCADE') @@ -570,6 +620,19 @@ class OAuthCode(Base): # state = Column(Unicode(1023)) user_id = Column(Integer, ForeignKey('users.id', ondelete='CASCADE')) + @staticmethod + def now(): + return datetime.utcnow().timestamp() + + @classmethod + def find(cls, db, code): + return ( + db.query(cls) + .filter(cls.code == code) + .filter(or_(cls.expires_at == None, cls.expires_at >= cls.now())) + .first() + ) + class OAuthClient(Base): __tablename__ = 'oauth_clients' @@ -623,7 +686,10 @@ def _expire_relationship(target, relationship_prop): return # many-to-many and one-to-many have a list of peers # many-to-one has only one - if relationship_prop.direction is interfaces.MANYTOONE: + if ( + relationship_prop.direction is interfaces.MANYTOONE + or not relationship_prop.uselist + ): peers = [peers] for obj in peers: if inspect(obj).persistent: @@ -770,8 +836,8 @@ def mysql_large_prefix_check(engine): ).fetchall() ) if ( - variables['innodb_file_format'] == 'Barracuda' - and variables['innodb_large_prefix'] == 'ON' + variables.get('innodb_file_format', 'Barracuda') == 'Barracuda' + and variables.get('innodb_large_prefix', 'ON') == 'ON' ): return True else: diff --git a/jupyterhub/pagination.py b/jupyterhub/pagination.py new file mode 100644 index 00000000..8d4b912d --- /dev/null +++ b/jupyterhub/pagination.py @@ -0,0 +1,213 @@ +"""Basic class to manage pagination utils.""" +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. +from traitlets import Bool +from traitlets import default +from traitlets import Integer +from traitlets import observe +from traitlets import Unicode +from traitlets import validate +from traitlets.config import Configurable + + +class Pagination(Configurable): + + # configurable options + default_per_page = Integer( + 100, + config=True, + help="Default number of entries per page for paginated results.", + ) + + max_per_page = Integer( + 250, + config=True, + help="Maximum number of entries per page for paginated results.", + ) + + # state variables + url = Unicode("") + page = Integer(1) + per_page = Integer(1, min=1) + + @default("per_page") + def _default_per_page(self): + return self.default_per_page + + @validate("per_page") + def _limit_per_page(self, proposal): + if self.max_per_page and proposal.value > self.max_per_page: + return self.max_per_page + if proposal.value <= 1: + return 1 + return proposal.value + + @observe("max_per_page") + def _apply_max(self, change): + if change.new: + self.per_page = min(change.new, self.per_page) + + total = Integer(0) + + total_pages = Integer(0) + + @default("total_pages") + def _calculate_total_pages(self): + total_pages = self.total // self.per_page + if self.total % self.per_page: + # there's a remainder, add 1 + total_pages += 1 + return total_pages + + @observe("per_page", "total") + def _update_total_pages(self, change): + """Update total_pages when per_page or total is changed""" + self.total_pages = self._calculate_total_pages() + + separator = Unicode("...") + + def get_page_args(self, handler): + """ + This method gets the arguments used in the webpage to configurate the pagination + In case of no arguments, it uses the default values from this class + + Returns: + - page: The page requested for paginating or the default value (1) + - per_page: The number of items to return in this page. No more than max_per_page + - offset: The offset to consider when managing pagination via the ORM + """ + page = handler.get_argument("page", 1) + per_page = handler.get_argument("per_page", self.default_per_page) + try: + self.per_page = int(per_page) + except Exception: + self.per_page = self._default_per_page + + try: + self.page = int(page) + if self.page < 1: + self.page = 1 + except: + self.page = 1 + + return self.page, self.per_page, self.per_page * (self.page - 1) + + @property + def info(self): + """Get the pagination information.""" + start = 1 + (self.page - 1) * self.per_page + end = start + self.per_page - 1 + if end > self.total: + end = self.total + + if start > self.total: + start = self.total + + return {'total': self.total, 'start': start, 'end': end} + + def calculate_pages_window(self): + """Calculates the set of pages to render later in links() method. + It returns the list of pages to render via links for the pagination + By default, as we've observed in other applications, we're going to render + only a finite and predefined number of pages, avoiding visual fatigue related + to a long list of pages. By default, we render 7 pages plus some inactive links with the characters '...' + to point out that there are other pages that aren't explicitly rendered. + The primary way of work is to provide current webpage and 5 next pages, the last 2 ones + (in case the current page + 5 does not overflow the total lenght of pages) and the first one for reference. + """ + + before_page = 2 + after_page = 2 + window_size = before_page + after_page + 1 + + # Add 1 to total_pages since our starting page is 1 and not 0 + last_page = self.total_pages + + pages = [] + + # will default window + start, end fit without truncation? + if self.total_pages > window_size + 2: + if self.page - before_page > 1: + # before_page will not reach page 1 + pages.append(1) + if self.page - before_page > 2: + # before_page will not reach page 2, need separator + pages.append(self.separator) + + pages.extend(range(max(1, self.page - before_page), self.page)) + # we now have up to but not including self.page + + if self.page + after_page + 1 >= last_page: + # after_page gets us to the end + pages.extend(range(self.page, last_page + 1)) + else: + # add full after_page entries + pages.extend(range(self.page, self.page + after_page + 1)) + # add separator *if* this doesn't get to last page - 1 + if self.page + after_page < last_page - 1: + pages.append(self.separator) + pages.append(last_page) + + return pages + + else: + # everything will fit, nothing to think about + # always return at least one page + return list(range(1, last_page + 1)) or [1] + + @property + def links(self): + """Get the links for the pagination. + Getting the input from calculate_pages_window(), generates the HTML code + for the pages to render, plus the arrows to go onwards and backwards (if needed). + """ + if self.total_pages == 1: + return [] + + pages_to_render = self.calculate_pages_window() + + links = ['') + + return ''.join(links) diff --git a/jupyterhub/proxy.py b/jupyterhub/proxy.py index 1b1a9f43..5b8d386a 100644 --- a/jupyterhub/proxy.py +++ b/jupyterhub/proxy.py @@ -24,7 +24,6 @@ import time from functools import wraps from subprocess import Popen from urllib.parse import quote -from urllib.parse import urlparse from tornado import gen from tornado.httpclient import AsyncHTTPClient @@ -42,7 +41,9 @@ from traitlets.config import LoggingConfigurable from . import utils from .metrics import CHECK_ROUTES_DURATION_SECONDS +from .metrics import PROXY_POLL_DURATION_SECONDS from .objects import Server +from .utils import exponential_backoff from .utils import make_ssl_context from .utils import url_path_join from jupyterhub.traitlets import Command @@ -768,10 +769,36 @@ class ConfigurableHTTPProxy(Proxy): method=method, headers={'Authorization': 'token {}'.format(self.auth_token)}, body=body, + connect_timeout=3, # default: 20s + request_timeout=10, # default: 20s ) - async with self.semaphore: - result = await client.fetch(req) - return result + + async def _wait_for_api_request(): + try: + async with self.semaphore: + return await client.fetch(req) + except HTTPError as e: + # Retry on potentially transient errors in CHP, typically + # numbered 500 and up. Note that CHP isn't able to emit 429 + # errors. + if e.code >= 500: + self.log.warning( + "api_request to the proxy failed with status code {}, retrying...".format( + e.code + ) + ) + return False # a falsy return value make exponential_backoff retry + else: + self.log.error("api_request to proxy failed: {0}".format(e)) + # An unhandled error here will help the hub invoke cleanup logic + raise + + result = await exponential_backoff( + _wait_for_api_request, + 'Repeated api_request to proxy path "{}" failed.'.format(path), + timeout=30, + ) + return result async def add_route(self, routespec, target, data): body = data or {} @@ -801,6 +828,7 @@ class ConfigurableHTTPProxy(Proxy): async def get_all_routes(self, client=None): """Fetch the proxy's routes.""" + proxy_poll_start_time = time.perf_counter() resp = await self.api_request('', client=client) chp_routes = json.loads(resp.body.decode('utf8', 'replace')) all_routes = {} @@ -811,4 +839,5 @@ class ConfigurableHTTPProxy(Proxy): self.log.debug("Omitting non-jupyterhub route %r", routespec) continue all_routes[routespec] = self._reformat_routespec(routespec, chp_data) + PROXY_POLL_DURATION_SECONDS.observe(time.perf_counter() - proxy_poll_start_time) return all_routes diff --git a/jupyterhub/services/auth.py b/jupyterhub/services/auth.py index c6d8b83c..c9e6974f 100644 --- a/jupyterhub/services/auth.py +++ b/jupyterhub/services/auth.py @@ -216,7 +216,7 @@ class HubAuth(SingletonConfigurable): return self.hub_host + url_path_join(self.hub_prefix, 'login') keyfile = Unicode( - '', + os.getenv('JUPYTERHUB_SSL_KEYFILE', ''), help="""The ssl key to use for requests Use with certfile @@ -224,7 +224,7 @@ class HubAuth(SingletonConfigurable): ).tag(config=True) certfile = Unicode( - '', + os.getenv('JUPYTERHUB_SSL_CERTFILE', ''), help="""The ssl cert to use for requests Use with keyfile @@ -232,7 +232,7 @@ class HubAuth(SingletonConfigurable): ).tag(config=True) client_ca = Unicode( - '', + os.getenv('JUPYTERHUB_SSL_CLIENT_CA', ''), help="""The ssl certificate authority to use to verify requests Use with keyfile and certfile @@ -371,9 +371,13 @@ class HubAuth(SingletonConfigurable): ) app_log.warning(r.text) msg = "Failed to check authorization" - # pass on error_description from oauth failure + # pass on error from oauth failure try: - description = r.json().get("error_description") + response = r.json() + # prefer more specific 'error_description', fallback to 'error' + description = response.get( + "error_description", response.get("error", "Unknown error") + ) except Exception: pass else: @@ -428,7 +432,7 @@ class HubAuth(SingletonConfigurable): ) auth_header_name = 'Authorization' - auth_header_pat = re.compile('token\s+(.+)', re.IGNORECASE) + auth_header_pat = re.compile(r'token\s+(.+)', re.IGNORECASE) def get_token(self, handler): """Get the user token from a request @@ -860,15 +864,15 @@ class HubAuthenticated(object): if kind == 'service': # it's a service, check hub_services if self.hub_services and name in self.hub_services: - app_log.debug("Allowing whitelisted Hub service %s", name) + app_log.debug("Allowing Hub service %s", name) return model else: app_log.warning("Not allowing Hub service %s", name) raise UserNotAllowed(model) if self.hub_users and name in self.hub_users: - # user in whitelist - app_log.debug("Allowing whitelisted Hub user %s", name) + # user in allowed list + app_log.debug("Allowing Hub user %s", name) return model elif self.hub_groups and set(model['groups']).intersection(self.hub_groups): allowed_groups = set(model['groups']).intersection(self.hub_groups) @@ -877,7 +881,7 @@ class HubAuthenticated(object): name, ','.join(sorted(allowed_groups)), ) - # group in whitelist + # group in allowed list return model else: app_log.warning("Not allowing Hub user %s", name) diff --git a/jupyterhub/services/service.py b/jupyterhub/services/service.py index 9da030b7..10883cb1 100644 --- a/jupyterhub/services/service.py +++ b/jupyterhub/services/service.py @@ -147,11 +147,14 @@ class Service(LoggingConfigurable): - name: str the name of the service - - admin: bool(false) + - admin: bool(False) whether the service should have administrative privileges - url: str (None) The URL where the service is/should be. If specified, the service will be added to the proxy at /services/:name + - oauth_no_confirm: bool(False) + Whether this service should be allowed to complete oauth + with logged-in users without prompting for confirmation. If a service is to be managed by the Hub, it has a few extra options: @@ -184,6 +187,7 @@ class Service(LoggingConfigurable): If managed, will be passed as JUPYTERHUB_SERVICE_URL env. """ ).tag(input=True) + api_token = Unicode( help="""The API token to use for the service. @@ -197,6 +201,25 @@ class Service(LoggingConfigurable): """ ).tag(input=True) + display = Bool( + True, help="""Whether to list the service on the JupyterHub UI""" + ).tag(input=True) + + oauth_no_confirm = Bool( + False, + help="""Skip OAuth confirmation when users access this service. + + By default, when users authenticate with a service using JupyterHub, + they are prompted to confirm that they want to grant that service + access to their credentials. + Setting oauth_no_confirm=True skips the confirmation web page for this service. + Skipping the confirmation page is useful for admin-managed services that are considered part of the Hub + and shouldn't need extra prompts for login. + + .. versionadded: 1.1 + """, + ).tag(input=True) + # Managed service API: spawner = Any() @@ -323,7 +346,7 @@ class Service(LoggingConfigurable): env['JUPYTERHUB_SERVICE_PREFIX'] = self.server.base_url hub = self.hub - if self.hub.ip in ('0.0.0.0', ''): + if self.hub.ip in ('', '0.0.0.0', '::'): # if the Hub is listening on all interfaces, # tell services to connect via localhost # since they are always local subprocesses diff --git a/jupyterhub/singleuser/__init__.py b/jupyterhub/singleuser/__init__.py new file mode 100644 index 00000000..0b06e2f6 --- /dev/null +++ b/jupyterhub/singleuser/__init__.py @@ -0,0 +1,13 @@ +"""JupyterHub single-user server entrypoints + +Contains default notebook-app subclass and mixins +""" +from .app import main +from .app import SingleUserNotebookApp +from .mixins import HubAuthenticatedHandler +from .mixins import make_singleuser_app + +# backward-compatibility +JupyterHubLoginHandler = SingleUserNotebookApp.login_handler_class +JupyterHubLogoutHandler = SingleUserNotebookApp.logout_handler_class +OAuthCallbackHandler = SingleUserNotebookApp.oauth_callback_handler_class diff --git a/jupyterhub/singleuser/__main__.py b/jupyterhub/singleuser/__main__.py new file mode 100644 index 00000000..18d6d1b4 --- /dev/null +++ b/jupyterhub/singleuser/__main__.py @@ -0,0 +1,4 @@ +from .app import main + +if __name__ == '__main__': + main() diff --git a/jupyterhub/singleuser/app.py b/jupyterhub/singleuser/app.py new file mode 100644 index 00000000..3afe6fe1 --- /dev/null +++ b/jupyterhub/singleuser/app.py @@ -0,0 +1,20 @@ +"""Make a single-user app based on the environment: + +- $JUPYTERHUB_SINGLEUSER_APP, the base Application class, to be wrapped in JupyterHub authentication. + default: notebook.notebookapp.NotebookApp +""" +import os + +from traitlets import import_item + +from .mixins import make_singleuser_app + +JUPYTERHUB_SINGLEUSER_APP = ( + os.environ.get("JUPYTERHUB_SINGLEUSER_APP") or "notebook.notebookapp.NotebookApp" +) + +App = import_item(JUPYTERHUB_SINGLEUSER_APP) + +SingleUserNotebookApp = make_singleuser_app(App) + +main = SingleUserNotebookApp.launch_instance diff --git a/jupyterhub/singleuser.py b/jupyterhub/singleuser/mixins.py similarity index 77% rename from jupyterhub/singleuser.py rename to jupyterhub/singleuser/mixins.py index 6fa4f350..1703e9a0 100755 --- a/jupyterhub/singleuser.py +++ b/jupyterhub/singleuser/mixins.py @@ -1,8 +1,15 @@ #!/usr/bin/env python -"""Extend regular notebook server to be aware of multiuser things.""" +"""Mixins to regular notebook server to add JupyterHub auth. + +Meant to be compatible with jupyter_server and classic notebook + +Use make_singleuser_app to create a compatible Application class +with JupyterHub authentication mixins enabled. +""" # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. import asyncio +import importlib import json import os import random @@ -19,38 +26,29 @@ from tornado.httpclient import AsyncHTTPClient from tornado.httpclient import HTTPRequest from tornado.web import HTTPError from tornado.web import RequestHandler +from traitlets import Any +from traitlets import Bool +from traitlets import Bytes +from traitlets import CUnicode +from traitlets import default +from traitlets import import_item +from traitlets import Integer +from traitlets import observe +from traitlets import TraitError +from traitlets import Unicode +from traitlets import validate +from traitlets.config import Configurable -try: - import notebook -except ImportError: - raise ImportError("JupyterHub single-user server requires notebook >= 4.0") - -from traitlets import ( - Any, - Bool, - Bytes, - Integer, - Unicode, - CUnicode, - default, - observe, - validate, - TraitError, -) - -from notebook.notebookapp import ( - NotebookApp, - aliases as notebook_aliases, - flags as notebook_flags, -) -from notebook.auth.login import LoginHandler -from notebook.auth.logout import LogoutHandler -from notebook.base.handlers import IPythonHandler - -from ._version import __version__, _check_version -from .log import log_request -from .services.auth import HubOAuth, HubOAuthenticated, HubOAuthCallbackHandler -from .utils import isoformat, url_path_join, make_ssl_context, exponential_backoff +from .._version import __version__ +from .._version import _check_version +from ..log import log_request +from ..services.auth import HubOAuth +from ..services.auth import HubOAuthCallbackHandler +from ..services.auth import HubOAuthenticated +from ..utils import exponential_backoff +from ..utils import isoformat +from ..utils import make_ssl_context +from ..utils import url_path_join # Authenticate requests with the Hub @@ -80,7 +78,7 @@ class HubAuthenticatedHandler(HubOAuthenticated): return set() -class JupyterHubLoginHandler(LoginHandler): +class JupyterHubLoginHandlerMixin: """LoginHandler that hooks up Hub authentication""" @staticmethod @@ -113,7 +111,7 @@ class JupyterHubLoginHandler(LoginHandler): return -class JupyterHubLogoutHandler(LogoutHandler): +class JupyterHubLogoutHandlerMixin: def get(self): self.settings['hub_auth'].clear_cookie(self) self.redirect( @@ -122,7 +120,7 @@ class JupyterHubLogoutHandler(LogoutHandler): ) -class OAuthCallbackHandler(HubOAuthCallbackHandler, IPythonHandler): +class OAuthCallbackHandlerMixin(HubOAuthCallbackHandler): """Mixin IPythonHandler to get the right error pages, etc.""" @property @@ -131,27 +129,22 @@ class OAuthCallbackHandler(HubOAuthCallbackHandler, IPythonHandler): # register new hub related command-line aliases -aliases = dict(notebook_aliases) -aliases.update( - { - 'user': 'SingleUserNotebookApp.user', - 'group': 'SingleUserNotebookApp.group', - 'cookie-name': 'HubAuth.cookie_name', - 'hub-prefix': 'SingleUserNotebookApp.hub_prefix', - 'hub-host': 'SingleUserNotebookApp.hub_host', - 'hub-api-url': 'SingleUserNotebookApp.hub_api_url', - 'base-url': 'SingleUserNotebookApp.base_url', - } -) -flags = dict(notebook_flags) -flags.update( - { - 'disable-user-config': ( - {'SingleUserNotebookApp': {'disable_user_config': True}}, - "Disable user-controlled configuration of the notebook server.", - ) - } -) +aliases = { + 'user': 'SingleUserNotebookApp.user', + 'group': 'SingleUserNotebookApp.group', + 'cookie-name': 'HubAuth.cookie_name', + 'hub-prefix': 'SingleUserNotebookApp.hub_prefix', + 'hub-host': 'SingleUserNotebookApp.hub_host', + 'hub-api-url': 'SingleUserNotebookApp.hub_api_url', + 'base-url': 'SingleUserNotebookApp.base_url', +} +flags = { + 'disable-user-config': ( + {'SingleUserNotebookApp': {'disable_user_config': True}}, + "Disable user-controlled configuration of the notebook server.", + ) +} + page_template = """ {% extends "templates/page.html" %} @@ -216,21 +209,29 @@ def _exclude_home(path_list): yield p -class SingleUserNotebookApp(NotebookApp): +class SingleUserNotebookAppMixin(Configurable): """A Subclass of the regular NotebookApp that is aware of the parent multiuser context.""" description = dedent( """ Single-user server for JupyterHub. Extends the Jupyter Notebook server. - Meant to be invoked by JupyterHub Spawners, and not directly. + Meant to be invoked by JupyterHub Spawners, not directly. """ ) examples = "" subcommands = {} version = __version__ - classes = NotebookApp.classes + [HubOAuth] + + # must be set in mixin subclass + # make_singleuser_app sets these + # aliases = aliases + # flags = flags + # login_handler_class = JupyterHubLoginHandler + # logout_handler_class = JupyterHubLogoutHandler + # oauth_callback_handler_class = OAuthCallbackHandler + # classes = NotebookApp.classes + [HubOAuth] # disable single-user app's localhost checking allow_remote_access = True @@ -323,19 +324,15 @@ class SingleUserNotebookApp(NotebookApp): return url.hostname return '127.0.0.1' - aliases = aliases - flags = flags - - # disble some single-user configurables + # disable some single-user configurables token = '' open_browser = False quit_button = False trust_xheaders = True - login_handler_class = JupyterHubLoginHandler - logout_handler_class = JupyterHubLogoutHandler + port_retries = ( - 0 - ) # disable port-retries, since the Spawner will tell us what port to use + 0 # disable port-retries, since the Spawner will tell us what port to use + ) disable_user_config = Bool( False, @@ -381,11 +378,11 @@ class SingleUserNotebookApp(NotebookApp): # disable config-migration when user config is disabled return else: - super(SingleUserNotebookApp, self).migrate_config() + super().migrate_config() @property def config_file_paths(self): - path = super(SingleUserNotebookApp, self).config_file_paths + path = super().config_file_paths if self.disable_user_config: # filter out user-writable config dirs if user config is disabled @@ -394,7 +391,7 @@ class SingleUserNotebookApp(NotebookApp): @property def nbextensions_path(self): - path = super(SingleUserNotebookApp, self).nbextensions_path + path = super().nbextensions_path if self.disable_user_config: path = list(_exclude_home(path)) @@ -490,7 +487,7 @@ class SingleUserNotebookApp(NotebookApp): # protect against mixed timezone comparisons if not last_activity.tzinfo: # assume naive timestamps are utc - self.log.warning("last activity is using naïve timestamps") + self.log.warning("last activity is using naive timestamps") last_activity = last_activity.replace(tzinfo=timezone.utc) if self._last_activity_sent and last_activity < self._last_activity_sent: @@ -562,7 +559,7 @@ class SingleUserNotebookApp(NotebookApp): # start by hitting Hub to check version ioloop.IOLoop.current().run_sync(self.check_hub_version) ioloop.IOLoop.current().add_callback(self.keep_activity_updated) - super(SingleUserNotebookApp, self).start() + super().start() def init_hub_auth(self): api_token = None @@ -610,12 +607,17 @@ class SingleUserNotebookApp(NotebookApp): 'Content-Security-Policy', ';'.join(["frame-ancestors 'self'", "report-uri " + csp_report_uri]), ) - super(SingleUserNotebookApp, self).init_webapp() + super().init_webapp() # add OAuth callback self.web_app.add_handlers( r".*$", - [(urlparse(self.hub_auth.oauth_redirect_uri).path, OAuthCallbackHandler)], + [ + ( + urlparse(self.hub_auth.oauth_redirect_uri).path, + self.oauth_callback_handler_class, + ) + ], ) # apply X-JupyterHub-Version to *all* request handlers (even redirects) @@ -656,9 +658,82 @@ class SingleUserNotebookApp(NotebookApp): env.loader = ChoiceLoader([FunctionLoader(get_page), orig_loader]) -def main(argv=None): - return SingleUserNotebookApp.launch_instance(argv) +def detect_base_package(App): + """Detect the base package for an App class + + Will return 'notebook' or 'jupyter_server' + based on which package App subclasses from. + + Will return None if neither is identified (e.g. fork package, or duck-typing). + """ + # guess notebook or jupyter_server based on App class inheritance + for cls in App.mro(): + pkg = cls.__module__.split(".", 1)[0] + if pkg in {"notebook", "jupyter_server"}: + return pkg + return None -if __name__ == "__main__": - main() +def make_singleuser_app(App): + """Make and return a singleuser notebook app + + given existing notebook or jupyter_server Application classes, + mix-in jupyterhub auth. + + Instances of App must have the following attributes defining classes: + + - .login_handler_class + - .logout_handler_class + - .base_handler_class (only required if not a subclass of the default app + in jupyter_server or notebook) + + App should be a subclass of `notebook.notebookapp.NotebookApp` + or `jupyter_server.serverapp.ServerApp`. + """ + + empty_parent_app = App() + + # detect base classes + LoginHandler = empty_parent_app.login_handler_class + LogoutHandler = empty_parent_app.logout_handler_class + BaseHandler = getattr(empty_parent_app, "base_handler_class", None) + if BaseHandler is None: + pkg = detect_base_package(App) + if pkg == "jupyter_server": + BaseHandler = import_item("jupyter_server.base.handlers.JupyterHandler") + elif pkg == "notebook": + BaseHandler = import_item("notebook.base.handlers.IPythonHandler") + else: + raise ValueError( + "{}.base_handler_class must be defined".format(App.__name__) + ) + + # create Handler classes from mixins + bases + class JupyterHubLoginHandler(JupyterHubLoginHandlerMixin, LoginHandler): + pass + + class JupyterHubLogoutHandler(JupyterHubLogoutHandlerMixin, LogoutHandler): + pass + + class OAuthCallbackHandler(OAuthCallbackHandlerMixin, BaseHandler): + pass + + # create merged aliases & flags + merged_aliases = {} + merged_aliases.update(empty_parent_app.aliases or {}) + merged_aliases.update(aliases) + + merged_flags = {} + merged_flags.update(empty_parent_app.flags or {}) + merged_flags.update(flags) + # create mixed-in App class, bringing it all together + class SingleUserNotebookApp(SingleUserNotebookAppMixin, App): + aliases = merged_aliases + flags = merged_flags + classes = empty_parent_app.classes + [HubOAuth] + + login_handler_class = JupyterHubLoginHandler + logout_handler_class = JupyterHubLogoutHandler + oauth_callback_handler_class = OAuthCallbackHandler + + return SingleUserNotebookApp diff --git a/jupyterhub/spawner.py b/jupyterhub/spawner.py index af84c122..ad07fa44 100644 --- a/jupyterhub/spawner.py +++ b/jupyterhub/spawner.py @@ -4,8 +4,6 @@ Contains base Spawner class & default implementation # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. import ast -import asyncio -import errno import json import os import pipes @@ -16,6 +14,8 @@ import warnings from subprocess import Popen from tempfile import mkdtemp +if os.name == 'nt': + import psutil from async_generator import async_generator from async_generator import yield_ from sqlalchemy import inspect @@ -86,6 +86,7 @@ class Spawner(LoggingConfigurable): _start_pending = False _stop_pending = False _proxy_pending = False + _check_pending = False _waiting_for_response = False _jupyterhub_version = None _spawn_future = None @@ -121,6 +122,8 @@ class Spawner(LoggingConfigurable): return 'spawn' elif self._stop_pending: return 'stop' + elif self._check_pending: + return 'check' return None @property @@ -207,8 +210,6 @@ class Spawner(LoggingConfigurable): return self.orm_spawner.name return '' - hub = Any() - authenticator = Any() internal_ssl = Bool(False) internal_trust_bundles = Dict() internal_certs_location = Unicode('') @@ -381,6 +382,37 @@ class Spawner(LoggingConfigurable): """ return form_data + def options_from_query(self, query_data): + """Interpret query arguments passed to /spawn + + Query arguments will always arrive as a dict of unicode strings. + Override this function to understand single-values, numbers, etc. + + By default, options_from_form is called from this function. You can however override + this function if you need to process the query arguments differently. + + This should coerce form data into the structure expected by self.user_options, + which must be a dict, and should be JSON-serializeable, + though it can contain bytes in addition to standard JSON data types. + + This method should not have any side effects. + Any handling of `user_options` should be done in `.start()` + to ensure consistent behavior across servers + spawned via the API and form submission page. + + Instances will receive this data on self.user_options, after passing through this function, + prior to `Spawner.start`. + + .. versionadded:: 1.2 + user_options are persisted in the JupyterHub database to be reused + on subsequent spawns if no options are given. + user_options is serialized to JSON as part of this persistence + (with additional support for bytes in case of uploaded file data), + and any non-bytes non-jsonable values will be replaced with None + if the user_options are re-used. + """ + return self.options_from_form(query_data) + user_options = Dict( help=""" Dict of user specified options for the user's spawned instance of a single-user server. @@ -399,11 +431,12 @@ class Spawner(LoggingConfigurable): 'VIRTUAL_ENV', 'LANG', 'LC_ALL', + 'JUPYTERHUB_SINGLEUSER_APP', ], help=""" - Whitelist of environment variables for the single-user server to inherit from the JupyterHub process. + List of environment variables for the single-user server to inherit from the JupyterHub process. - This whitelist is used to ensure that sensitive information in the JupyterHub process's environment + This list is used to ensure that sensitive information in the JupyterHub process's environment (such as `CONFIGPROXY_AUTH_TOKEN`) is not passed to the single-user server's process. """, ).tag(config=True) @@ -422,7 +455,7 @@ class Spawner(LoggingConfigurable): Environment variables that end up in the single-user server's process come from 3 sources: - This `environment` configurable - - The JupyterHub process' environment variables that are whitelisted in `env_keep` + - The JupyterHub process' environment variables that are listed in `env_keep` - Variables to establish contact between the single-user notebook and the hub (such as JUPYTERHUB_API_TOKEN) The `environment` configurable should be set by JupyterHub administrators to add @@ -433,6 +466,11 @@ class Spawner(LoggingConfigurable): Note that the spawner class' interface is not guaranteed to be exactly same across upgrades, so if you are using the callable take care to verify it continues to work after upgrades! + + .. versionchanged:: 1.2 + environment from this configuration has highest priority, + allowing override of 'default' env variables, + such as JUPYTERHUB_API_URL. """ ).tag(config=True) @@ -627,6 +665,24 @@ class Spawner(LoggingConfigurable): """ ).tag(config=True) + auth_state_hook = Any( + help=""" + An optional hook function that you can implement to pass `auth_state` + to the spawner after it has been initialized but before it starts. + The `auth_state` dictionary may be set by the `.authenticate()` + method of the authenticator. This hook enables you to pass some + or all of that information to your spawner. + + Example:: + + def userdata_hook(spawner, auth_state): + spawner.userdata = auth_state["userdata"] + + c.Spawner.auth_state_hook = userdata_hook + + """ + ).tag(config=True) + def load_state(self, state): """Restore state of spawner from database. @@ -688,16 +744,6 @@ class Spawner(LoggingConfigurable): if key in os.environ: env[key] = os.environ[key] - # config overrides. If the value is a callable, it will be called with - # one parameter - the current spawner instance - and the return value - # will be assigned to the environment variable. This will be called at - # spawn time. - for key, value in self.environment.items(): - if callable(value): - env[key] = value(self) - else: - env[key] = value - env['JUPYTERHUB_API_TOKEN'] = self.api_token # deprecated (as of 0.7.2), for old versions of singleuser env['JPY_API_TOKEN'] = self.api_token @@ -745,6 +791,18 @@ class Spawner(LoggingConfigurable): env['JUPYTERHUB_SSL_CERTFILE'] = self.cert_paths['certfile'] env['JUPYTERHUB_SSL_CLIENT_CA'] = self.cert_paths['cafile'] + # env overrides from config. If the value is a callable, it will be called with + # one parameter - the current spawner instance - and the return value + # will be assigned to the environment variable. This will be called at + # spawn time. + # Called last to ensure highest priority, in case of overriding other + # 'default' variables like the API url + for key, value in self.environment.items(): + if callable(value): + env[key] = value(self) + else: + env[key] = value + return env async def get_url(self): @@ -885,14 +943,13 @@ class Spawner(LoggingConfigurable): Arguments: paths (dict): a list of paths for key, cert, and CA. - These paths will be resolvable and readable by the Hub process, - but not necessarily by the notebook server. + These paths will be resolvable and readable by the Hub process, + but not necessarily by the notebook server. Returns: - dict: a list (potentially altered) of paths for key, cert, - and CA. - These paths should be resolvable and readable - by the notebook server to be launched. + dict: a list (potentially altered) of paths for key, cert, and CA. + These paths should be resolvable and readable by the notebook + server to be launched. `.move_certs` is called after certs for the singleuser notebook have @@ -931,7 +988,9 @@ class Spawner(LoggingConfigurable): args.append('--notebook-dir=%s' % _quote_safe(notebook_dir)) if self.default_url: default_url = self.format_string(self.default_url) - args.append('--NotebookApp.default_url=%s' % _quote_safe(default_url)) + args.append( + '--SingleUserNotebookApp.default_url=%s' % _quote_safe(default_url) + ) if self.debug: args.append('--debug') @@ -953,6 +1012,14 @@ class Spawner(LoggingConfigurable): except Exception: self.log.exception("post_stop_hook failed with exception: %s", self) + async def run_auth_state_hook(self, auth_state): + """Run the auth_state_hook if defined""" + if self.auth_state_hook is not None: + try: + await maybe_future(self.auth_state_hook(self, auth_state)) + except Exception: + self.log.exception("auth_stop_hook failed with exception: %s", self) + @property def _progress_url(self): return self.user.progress_url(self.name) @@ -1351,7 +1418,8 @@ class LocalProcessSpawner(Spawner): home = user.pw_dir # Create dir for user's certs wherever we're starting - out_dir = "{home}/.jupyterhub/jupyterhub-certs".format(home=home) + hub_dir = "{home}/.jupyterhub".format(home=home) + out_dir = "{hub_dir}/jupyterhub-certs".format(hub_dir=hub_dir) shutil.rmtree(out_dir, ignore_errors=True) os.makedirs(out_dir, 0o700, exist_ok=True) @@ -1365,7 +1433,7 @@ class LocalProcessSpawner(Spawner): ca = os.path.join(out_dir, os.path.basename(paths['cafile'])) # Set cert ownership to user - for f in [out_dir, key, cert, ca]: + for f in [hub_dir, out_dir, key, cert, ca]: shutil.chown(f, user=uid, group=gid) return {"keyfile": key, "certfile": cert, "cafile": ca} @@ -1440,9 +1508,11 @@ class LocalProcessSpawner(Spawner): self.clear_state() return 0 - # send signal 0 to check if PID exists - # this doesn't work on Windows, but that's okay because we don't support Windows. - alive = await self._signal(0) + # We use pustil.pid_exists on windows + if os.name == 'nt': + alive = psutil.pid_exists(self.pid) + else: + alive = await self._signal(0) if not alive: self.clear_state() return 0 @@ -1458,11 +1528,10 @@ class LocalProcessSpawner(Spawner): """ try: os.kill(self.pid, sig) + except ProcessLookupError: + return False # process is gone except OSError as e: - if e.errno == errno.ESRCH: - return False # process is gone - else: - raise + raise # Can be EPERM or EINVAL return True # process exists async def stop(self, now=False): @@ -1549,5 +1618,5 @@ class SimpleLocalProcessSpawner(LocalProcessSpawner): return env def move_certs(self, paths): - """No-op for installing certs""" + """No-op for installing certs.""" return paths diff --git a/jupyterhub/tests/mocking.py b/jupyterhub/tests/mocking.py index 9e2a928d..5084bab3 100644 --- a/jupyterhub/tests/mocking.py +++ b/jupyterhub/tests/mocking.py @@ -166,14 +166,16 @@ class FormSpawner(MockSpawner): options_form = "IMAFORM" def options_from_form(self, form_data): - options = {} - options['notspecified'] = 5 + options = {'notspecified': 5} if 'bounds' in form_data: options['bounds'] = [int(i) for i in form_data['bounds']] if 'energy' in form_data: options['energy'] = form_data['energy'][0] if 'hello_file' in form_data: options['hello'] = form_data['hello_file'][0] + + if 'illegal_argument' in form_data: + raise ValueError("You are not allowed to specify 'illegal_argument'") return options @@ -379,9 +381,9 @@ class MockHub(JupyterHub): class MockSingleUserServer(SingleUserNotebookApp): """Mock-out problematic parts of single-user server when run in a thread - + Currently: - + - disable signal handler """ @@ -392,6 +394,17 @@ class MockSingleUserServer(SingleUserNotebookApp): class StubSingleUserSpawner(MockSpawner): """Spawner that starts a MockSingleUserServer in a thread.""" + @default("default_url") + def _default_url(self): + """Use a default_url that any jupyter server will provide + + Should be: + + - authenticated, so we are testing auth + - always available (i.e. in base ServerApp and NotebookApp + """ + return "/api/status" + _thread = None @gen.coroutine diff --git a/jupyterhub/tests/mockserverapp.py b/jupyterhub/tests/mockserverapp.py new file mode 100644 index 00000000..84479e4b --- /dev/null +++ b/jupyterhub/tests/mockserverapp.py @@ -0,0 +1,17 @@ +"""Example JupyterServer app subclass""" +from jupyter_server.base.handlers import JupyterHandler +from jupyter_server.serverapp import ServerApp +from tornado import web + + +class TreeHandler(JupyterHandler): + @web.authenticated + def get(self): + self.write("OK!") + + +class MockServerApp(ServerApp): + def initialize(self, argv=None): + self.default_url = "/tree" + super().initialize(argv) + self.web_app.add_handlers(".*$", [(self.base_url + "tree/?", TreeHandler)]) diff --git a/jupyterhub/tests/test_api.py b/jupyterhub/tests/test_api.py index aedb4426..fa6a20d1 100644 --- a/jupyterhub/tests/test_api.py +++ b/jupyterhub/tests/test_api.py @@ -1,5 +1,4 @@ """Tests for the REST API.""" -import asyncio import json import re import sys @@ -1514,6 +1513,7 @@ async def test_get_services(app, mockservice_url): 'prefix': mockservice.server.base_url, 'url': mockservice.url, 'info': {}, + 'display': True, } } @@ -1538,6 +1538,7 @@ async def test_get_service(app, mockservice_url): 'prefix': mockservice.server.base_url, 'url': mockservice.url, 'info': {}, + 'display': True, } r = await api_request( diff --git a/jupyterhub/tests/test_app.py b/jupyterhub/tests/test_app.py index 0fb3abb6..accdd430 100644 --- a/jupyterhub/tests/test_app.py +++ b/jupyterhub/tests/test_app.py @@ -3,6 +3,7 @@ import binascii import os import re import sys +import time from subprocess import check_output from subprocess import PIPE from subprocess import Popen @@ -11,7 +12,6 @@ from tempfile import TemporaryDirectory from unittest.mock import patch import pytest -from tornado import gen from traitlets.config import Config from .. import orm @@ -39,6 +39,28 @@ def test_token_app(): assert re.match(r'^[a-z0-9]+$', out) +def test_raise_error_on_missing_specified_config(): + """ + Using the -f or --config flag when starting JupyterHub should require the + file to be found and exit if it isn't. + """ + # subprocess.run doesn't have a timeout flag, so if this test would fail by + # not letting jupyterhub error out, we would wait forever. subprocess.Popen + # allow us to manually timeout. + process = Popen( + [sys.executable, '-m', 'jupyterhub', '--config', 'not-available.py'] + ) + # wait inpatiently for the process to exit like we want it to + for i in range(100): + time.sleep(0.1) + returncode = process.poll() + if returncode is not None: + break + else: + process.kill() + assert returncode == 1 + + def test_generate_config(): with NamedTemporaryFile(prefix='jupyterhub_config', suffix='.py') as tf: cfg_file = tf.name @@ -69,7 +91,7 @@ def test_generate_config(): os.remove(cfg_file) assert cfg_file in out assert 'Spawner.cmd' in cfg_text - assert 'Authenticator.whitelist' in cfg_text + assert 'Authenticator.allowed_users' in cfg_text async def test_init_tokens(request): diff --git a/jupyterhub/tests/test_auth.py b/jupyterhub/tests/test_auth.py index 10ae0b1a..58d48456 100644 --- a/jupyterhub/tests/test_auth.py +++ b/jupyterhub/tests/test_auth.py @@ -1,11 +1,12 @@ """Tests for PAM authentication""" # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. -import os +import logging from unittest import mock import pytest from requests import HTTPError +from traitlets.config import Config from .mocking import MockPAMAuthenticator from .mocking import MockStructGroup @@ -137,8 +138,8 @@ async def test_pam_auth_admin_groups(): assert authorized['admin'] is False -async def test_pam_auth_whitelist(): - authenticator = MockPAMAuthenticator(whitelist={'wash', 'kaylee'}) +async def test_pam_auth_allowed(): + authenticator = MockPAMAuthenticator(allowed_users={'wash', 'kaylee'}) authorized = await authenticator.get_authenticated_user( None, {'username': 'kaylee', 'password': 'kaylee'} ) @@ -155,11 +156,11 @@ async def test_pam_auth_whitelist(): assert authorized is None -async def test_pam_auth_group_whitelist(): +async def test_pam_auth_allowed_groups(): def getgrnam(name): return MockStructGroup('grp', ['kaylee']) - authenticator = MockPAMAuthenticator(group_whitelist={'group'}) + authenticator = MockPAMAuthenticator(allowed_groups={'group'}) with mock.patch.object(authenticator, '_getgrnam', getgrnam): authorized = await authenticator.get_authenticated_user( @@ -174,7 +175,7 @@ async def test_pam_auth_group_whitelist(): assert authorized is None -async def test_pam_auth_blacklist(): +async def test_pam_auth_blocked(): # Null case compared to next case authenticator = MockPAMAuthenticator() authorized = await authenticator.get_authenticated_user( @@ -183,33 +184,33 @@ async def test_pam_auth_blacklist(): assert authorized['name'] == 'wash' # Blacklist basics - authenticator = MockPAMAuthenticator(blacklist={'wash'}) + authenticator = MockPAMAuthenticator(blocked_users={'wash'}) authorized = await authenticator.get_authenticated_user( None, {'username': 'wash', 'password': 'wash'} ) assert authorized is None - # User in both white and blacklists: default deny. Make error someday? + # User in both allowed and blocked: default deny. Make error someday? authenticator = MockPAMAuthenticator( - blacklist={'wash'}, whitelist={'wash', 'kaylee'} + blocked_users={'wash'}, allowed_users={'wash', 'kaylee'} ) authorized = await authenticator.get_authenticated_user( None, {'username': 'wash', 'password': 'wash'} ) assert authorized is None - # User not in blacklist can log in + # User not in blocked set can log in authenticator = MockPAMAuthenticator( - blacklist={'wash'}, whitelist={'wash', 'kaylee'} + blocked_users={'wash'}, allowed_users={'wash', 'kaylee'} ) authorized = await authenticator.get_authenticated_user( None, {'username': 'kaylee', 'password': 'kaylee'} ) assert authorized['name'] == 'kaylee' - # User in whitelist, blacklist irrelevent + # User in allowed, blocked irrelevent authenticator = MockPAMAuthenticator( - blacklist={'mal'}, whitelist={'wash', 'kaylee'} + blocked_users={'mal'}, allowed_users={'wash', 'kaylee'} ) authorized = await authenticator.get_authenticated_user( None, {'username': 'wash', 'password': 'wash'} @@ -218,15 +219,16 @@ async def test_pam_auth_blacklist(): # User in neither list authenticator = MockPAMAuthenticator( - blacklist={'mal'}, whitelist={'wash', 'kaylee'} + blocked_users={'mal'}, allowed_users={'wash', 'kaylee'} ) authorized = await authenticator.get_authenticated_user( None, {'username': 'simon', 'password': 'simon'} ) assert authorized is None - # blacklist == {} - authenticator = MockPAMAuthenticator(blacklist=set(), whitelist={'wash', 'kaylee'}) + authenticator = MockPAMAuthenticator( + blocked_users=set(), allowed_users={'wash', 'kaylee'} + ) authorized = await authenticator.get_authenticated_user( None, {'username': 'kaylee', 'password': 'kaylee'} ) @@ -253,7 +255,7 @@ async def test_deprecated_signatures(): async def test_pam_auth_no_such_group(): - authenticator = MockPAMAuthenticator(group_whitelist={'nosuchcrazygroup'}) + authenticator = MockPAMAuthenticator(allowed_groups={'nosuchcrazygroup'}) authorized = await authenticator.get_authenticated_user( None, {'username': 'kaylee', 'password': 'kaylee'} ) @@ -262,7 +264,7 @@ async def test_pam_auth_no_such_group(): async def test_wont_add_system_user(): user = orm.User(name='lioness4321') - authenticator = auth.PAMAuthenticator(whitelist={'mal'}) + authenticator = auth.PAMAuthenticator(allowed_users={'mal'}) authenticator.create_system_users = False with pytest.raises(KeyError): await authenticator.add_user(user) @@ -270,7 +272,7 @@ async def test_wont_add_system_user(): async def test_cant_add_system_user(): user = orm.User(name='lioness4321') - authenticator = auth.PAMAuthenticator(whitelist={'mal'}) + authenticator = auth.PAMAuthenticator(allowed_users={'mal'}) authenticator.add_user_cmd = ['jupyterhub-fake-command'] authenticator.create_system_users = True @@ -296,7 +298,7 @@ async def test_cant_add_system_user(): async def test_add_system_user(): user = orm.User(name='lioness4321') - authenticator = auth.PAMAuthenticator(whitelist={'mal'}) + authenticator = auth.PAMAuthenticator(allowed_users={'mal'}) authenticator.create_system_users = True authenticator.add_user_cmd = ['echo', '/home/USERNAME'] @@ -317,13 +319,13 @@ async def test_add_system_user(): async def test_delete_user(): user = orm.User(name='zoe') - a = MockPAMAuthenticator(whitelist={'mal'}) + a = MockPAMAuthenticator(allowed_users={'mal'}) - assert 'zoe' not in a.whitelist + assert 'zoe' not in a.allowed_users await a.add_user(user) - assert 'zoe' in a.whitelist + assert 'zoe' in a.allowed_users a.delete_user(user) - assert 'zoe' not in a.whitelist + assert 'zoe' not in a.allowed_users def test_urls(): @@ -461,3 +463,55 @@ async def test_post_auth_hook(): ) assert authorized['testkey'] == 'testvalue' + + +class MyAuthenticator(auth.Authenticator): + def check_whitelist(self, username, authentication=None): + return username == "subclass-allowed" + + +def test_deprecated_config(caplog): + cfg = Config() + cfg.Authenticator.whitelist = {'user'} + log = logging.getLogger("testlog") + authenticator = auth.Authenticator(config=cfg, log=log) + assert caplog.record_tuples == [ + ( + log.name, + logging.WARNING, + 'Authenticator.whitelist is deprecated in JupyterHub 1.2, use ' + 'Authenticator.allowed_users instead', + ) + ] + assert authenticator.allowed_users == {'user'} + + +def test_deprecated_methods(): + cfg = Config() + cfg.Authenticator.whitelist = {'user'} + authenticator = auth.Authenticator(config=cfg) + + assert authenticator.check_allowed("user") + with pytest.deprecated_call(): + assert authenticator.check_whitelist("user") + assert not authenticator.check_allowed("otheruser") + with pytest.deprecated_call(): + assert not authenticator.check_whitelist("otheruser") + + +def test_deprecated_config_subclass(): + cfg = Config() + cfg.MyAuthenticator.whitelist = {'user'} + with pytest.deprecated_call(): + authenticator = MyAuthenticator(config=cfg) + assert authenticator.allowed_users == {'user'} + + +def test_deprecated_methods_subclass(): + with pytest.deprecated_call(): + authenticator = MyAuthenticator() + + assert authenticator.check_allowed("subclass-allowed") + assert authenticator.check_whitelist("subclass-allowed") + assert not authenticator.check_allowed("otheruser") + assert not authenticator.check_whitelist("otheruser") diff --git a/jupyterhub/tests/test_auth_expiry.py b/jupyterhub/tests/test_auth_expiry.py index 781c751b..48f85eb4 100644 --- a/jupyterhub/tests/test_auth_expiry.py +++ b/jupyterhub/tests/test_auth_expiry.py @@ -7,7 +7,6 @@ authentication can expire in a number of ways: - doesn't need refresh - needs refresh and cannot be refreshed without new login """ -import asyncio from contextlib import contextmanager from unittest import mock from urllib.parse import parse_qs diff --git a/jupyterhub/tests/test_dummyauth.py b/jupyterhub/tests/test_dummyauth.py index 3f34c343..dbeaf583 100644 --- a/jupyterhub/tests/test_dummyauth.py +++ b/jupyterhub/tests/test_dummyauth.py @@ -1,8 +1,6 @@ """Tests for dummy authentication""" # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. -import pytest - from jupyterhub.auth import DummyAuthenticator diff --git a/jupyterhub/tests/test_eventlog.py b/jupyterhub/tests/test_eventlog.py new file mode 100644 index 00000000..3cfbaec1 --- /dev/null +++ b/jupyterhub/tests/test_eventlog.py @@ -0,0 +1,80 @@ +"""Tests for Eventlogging in JupyterHub. + +To test a new schema or event, simply add it to the +`valid_events` and `invalid_events` variables below. + +You *shouldn't* need to write new tests. +""" +import io +import json +import logging +from unittest import mock + +import jsonschema +import pytest +from traitlets.config import Config + +from .mocking import MockHub + + +# To test new schemas, add them to the `valid_events` +# and `invalid_events` dictionary below. + +# To test valid events, add event item with the form: +# { ( '', ) : { } } +valid_events = [ + ( + 'hub.jupyter.org/server-action', + 1, + dict(action='start', username='test-username', servername='test-servername'), + ) +] + +# To test invalid events, add event item with the form: +# { ( '', ) : { } } +invalid_events = [ + # Missing required keys + ('hub.jupyter.org/server-action', 1, dict(action='start')) +] + + +@pytest.fixture +def eventlog_sink(app): + """Return eventlog and sink objects""" + sink = io.StringIO() + handler = logging.StreamHandler(sink) + # Update the EventLog config with handler + cfg = Config() + cfg.EventLog.handlers = [handler] + + with mock.patch.object(app.config, 'EventLog', cfg.EventLog): + # recreate the eventlog object with our config + app.init_eventlog() + # return the sink from the fixture + yield app.eventlog, sink + # reset eventlog with original config + app.init_eventlog() + + +@pytest.mark.parametrize('schema, version, event', valid_events) +def test_valid_events(eventlog_sink, schema, version, event): + eventlog, sink = eventlog_sink + eventlog.allowed_schemas = [schema] + # Record event + eventlog.record_event(schema, version, event) + # Inspect consumed event + output = sink.getvalue() + assert output + data = json.loads(output) + # Verify event data was recorded + assert data is not None + + +@pytest.mark.parametrize('schema, version, event', invalid_events) +def test_invalid_events(eventlog_sink, schema, version, event): + eventlog, sink = eventlog_sink + eventlog.allowed_schemas = [schema] + + # Make sure an error is thrown when bad events are recorded + with pytest.raises(jsonschema.ValidationError): + recorded_event = eventlog.record_event(schema, version, event) diff --git a/jupyterhub/tests/test_internal_ssl_app.py b/jupyterhub/tests/test_internal_ssl_app.py index 95b382c4..cae0519b 100644 --- a/jupyterhub/tests/test_internal_ssl_app.py +++ b/jupyterhub/tests/test_internal_ssl_app.py @@ -1,8 +1,6 @@ """Test the JupyterHub entry point with internal ssl""" # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. -import sys - import jupyterhub.tests.mocking from jupyterhub.tests.test_app import * diff --git a/jupyterhub/tests/test_internal_ssl_connections.py b/jupyterhub/tests/test_internal_ssl_connections.py index a253367e..a650806c 100644 --- a/jupyterhub/tests/test_internal_ssl_connections.py +++ b/jupyterhub/tests/test_internal_ssl_connections.py @@ -6,6 +6,7 @@ from unittest import mock from urllib.parse import urlparse import pytest +from requests.exceptions import ConnectionError from requests.exceptions import SSLError from tornado import gen @@ -15,6 +16,9 @@ from .utils import async_requests ssl_enabled = True +# possible errors raised by ssl failures +SSL_ERROR = (SSLError, ConnectionError) + @gen.coroutine def wait_for_spawner(spawner, timeout=10): @@ -41,7 +45,7 @@ def wait_for_spawner(spawner, timeout=10): async def test_connection_hub_wrong_certs(app): """Connecting to the internal hub url fails without correct certs""" - with pytest.raises(SSLError): + with pytest.raises(SSL_ERROR): kwargs = {'verify': False} r = await async_requests.get(app.hub.url, **kwargs) r.raise_for_status() @@ -49,7 +53,7 @@ async def test_connection_hub_wrong_certs(app): async def test_connection_proxy_api_wrong_certs(app): """Connecting to the proxy api fails without correct certs""" - with pytest.raises(SSLError): + with pytest.raises(SSL_ERROR): kwargs = {'verify': False} r = await async_requests.get(app.proxy.api_url, **kwargs) r.raise_for_status() @@ -68,7 +72,7 @@ async def test_connection_notebook_wrong_certs(app): status = await spawner.poll() assert status is None - with pytest.raises(SSLError): + with pytest.raises(SSL_ERROR): kwargs = {'verify': False} r = await async_requests.get(spawner.server.url, **kwargs) r.raise_for_status() diff --git a/jupyterhub/tests/test_named_servers.py b/jupyterhub/tests/test_named_servers.py index 0f6809c1..20e515a2 100644 --- a/jupyterhub/tests/test_named_servers.py +++ b/jupyterhub/tests/test_named_servers.py @@ -1,6 +1,8 @@ """Tests for named servers""" +import asyncio import json from unittest import mock +from urllib.parse import urlencode from urllib.parse import urlparse import pytest @@ -27,6 +29,17 @@ def named_servers(app): yield +@pytest.fixture +def default_server_name(app, named_servers): + """configure app to use a default server name""" + server_name = 'myserver' + try: + app.default_server_name = server_name + yield server_name + finally: + app.default_server_name = '' + + async def test_default_server(app, named_servers): """Test the default /users/:user/server handler when named servers are enabled""" username = 'rosie' @@ -57,6 +70,7 @@ async def test_default_server(app, named_servers): username ), 'state': {'pid': 0}, + 'user_options': {}, } }, } @@ -116,6 +130,7 @@ async def test_create_named_server(app, named_servers): username, servername ), 'state': {'pid': 0}, + 'user_options': {}, } for name in [servername] }, @@ -232,7 +247,7 @@ async def test_named_server_limit(app, named_servers): assert r.text == '' -async def test_named_server_spawn_form(app, username): +async def test_named_server_spawn_form(app, username, named_servers): server_name = "myserver" base_url = public_url(app) cookies = await app.login_user(username) @@ -265,3 +280,116 @@ async def test_named_server_spawn_form(app, username): assert server_name in user.spawners spawner = user.spawners[server_name] spawner.user_options == {'energy': '938MeV', 'bounds': [-10, 10], 'notspecified': 5} + + +async def test_user_redirect_default_server_name( + app, username, named_servers, default_server_name +): + name = username + server_name = default_server_name + cookies = await app.login_user(name) + + r = await api_request(app, 'users', username, 'servers', server_name, method='post') + r.raise_for_status() + assert r.status_code == 201 + assert r.text == '' + + r = await get_page('/user-redirect/tree/top/', app) + r.raise_for_status() + print(urlparse(r.url)) + path = urlparse(r.url).path + assert path == url_path_join(app.base_url, '/hub/login') + query = urlparse(r.url).query + assert query == urlencode( + {'next': url_path_join(app.hub.base_url, '/user-redirect/tree/top/')} + ) + + r = await get_page('/user-redirect/notebooks/test.ipynb', app, cookies=cookies) + r.raise_for_status() + print(urlparse(r.url)) + path = urlparse(r.url).path + while '/spawn-pending/' in path: + await asyncio.sleep(0.1) + r = await async_requests.get(r.url, cookies=cookies) + path = urlparse(r.url).path + assert path == url_path_join( + app.base_url, '/user/{}/{}/notebooks/test.ipynb'.format(name, server_name) + ) + + +async def test_user_redirect_hook_default_server_name( + app, username, named_servers, default_server_name +): + """ + Test proper behavior of user_redirect_hook when c.JupyterHub.default_server_name is set + """ + name = username + server_name = default_server_name + cookies = await app.login_user(name) + + r = await api_request(app, 'users', username, 'servers', server_name, method='post') + r.raise_for_status() + assert r.status_code == 201 + assert r.text == '' + + async def dummy_redirect(path, request, user, base_url): + assert base_url == app.base_url + assert path == 'redirect-to-terminal' + assert request.uri == url_path_join( + base_url, 'hub', 'user-redirect', 'redirect-to-terminal' + ) + # exclude custom server_name + # custom hook is respected exactly + url = url_path_join(user.url, '/terminals/1') + return url + + app.user_redirect_hook = dummy_redirect + + r = await get_page('/user-redirect/redirect-to-terminal', app) + r.raise_for_status() + print(urlparse(r.url)) + path = urlparse(r.url).path + assert path == url_path_join(app.base_url, '/hub/login') + query = urlparse(r.url).query + assert query == urlencode( + {'next': url_path_join(app.hub.base_url, '/user-redirect/redirect-to-terminal')} + ) + + # We don't actually want to start the server by going through spawn - just want to make sure + # the redirect is to the right place + r = await get_page( + '/user-redirect/redirect-to-terminal', + app, + cookies=cookies, + allow_redirects=False, + ) + r.raise_for_status() + redirected_url = urlparse(r.headers['Location']) + assert redirected_url.path == url_path_join( + app.base_url, 'user', username, 'terminals/1' + ) + + +async def test_named_server_stop_server(app, username, named_servers): + server_name = "myserver" + await app.login_user(username) + user = app.users[username] + + r = await api_request(app, 'users', username, 'server', method='post') + assert r.status_code == 201 + assert r.text == '' + assert user.spawners[''].server + + with mock.patch.object( + app.proxy, 'add_user', side_effect=Exception('mock exception') + ): + r = await api_request( + app, 'users', username, 'servers', server_name, method='post' + ) + r.raise_for_status() + assert r.status_code == 201 + assert r.text == '' + + assert user.spawners[server_name].server is None + assert user.spawners[''].server + assert user.running diff --git a/jupyterhub/tests/test_orm.py b/jupyterhub/tests/test_orm.py index d609b27b..0c125c5a 100644 --- a/jupyterhub/tests/test_orm.py +++ b/jupyterhub/tests/test_orm.py @@ -74,6 +74,16 @@ def test_user(db): assert found is None +def test_user_escaping(db): + orm_user = orm.User(name='company\\user@company.com,\"quoted\"') + db.add(orm_user) + db.commit() + user = User(orm_user) + assert user.name == 'company\\user@company.com,\"quoted\"' + assert user.escaped_name == 'company%5Cuser@company.com%2C%22quoted%22' + assert user.json_escaped_name == 'company\\\\user@company.com,\\\"quoted\\\"' + + def test_tokens(db): user = orm.User(name='inara') db.add(user) @@ -124,7 +134,7 @@ def test_token_expiry(db): assert orm_token.expires_at > now + timedelta(seconds=50) assert orm_token.expires_at < now + timedelta(seconds=70) the_future = mock.patch( - 'jupyterhub.orm.utcnow', lambda: now + timedelta(seconds=70) + 'jupyterhub.orm.APIToken.now', lambda: now + timedelta(seconds=70) ) with the_future: found = orm.APIToken.find(db, token=token) @@ -472,3 +482,78 @@ def test_group_delete_cascade(db): db.delete(user1) db.commit() assert user1 not in group1.users + + +def test_expiring_api_token(app, user): + db = app.db + token = orm.APIToken.new(expires_in=30, user=user) + orm_token = orm.APIToken.find(db, token, kind='user') + assert orm_token + + # purge_expired doesn't delete non-expired + orm.APIToken.purge_expired(db) + found = orm.APIToken.find(db, token) + assert found is orm_token + + with mock.patch.object( + orm.APIToken, 'now', lambda: datetime.utcnow() + timedelta(seconds=60) + ): + found = orm.APIToken.find(db, token) + assert found is None + assert orm_token in db.query(orm.APIToken) + orm.APIToken.purge_expired(db) + assert orm_token not in db.query(orm.APIToken) + + +def test_expiring_oauth_token(app, user): + db = app.db + token = "abc123" + now = orm.OAuthAccessToken.now + client = orm.OAuthClient(identifier="xxx", secret="yyy") + db.add(client) + orm_token = orm.OAuthAccessToken( + token=token, + grant_type=orm.GrantType.authorization_code, + client=client, + user=user, + expires_at=now() + 30, + ) + db.add(orm_token) + db.commit() + + found = orm.OAuthAccessToken.find(db, token) + assert found is orm_token + # purge_expired doesn't delete non-expired + orm.OAuthAccessToken.purge_expired(db) + found = orm.OAuthAccessToken.find(db, token) + assert found is orm_token + + with mock.patch.object(orm.OAuthAccessToken, 'now', lambda: now() + 60): + found = orm.OAuthAccessToken.find(db, token) + assert found is None + assert orm_token in db.query(orm.OAuthAccessToken) + orm.OAuthAccessToken.purge_expired(db) + assert orm_token not in db.query(orm.OAuthAccessToken) + + +def test_expiring_oauth_code(app, user): + db = app.db + code = "abc123" + now = orm.OAuthCode.now + orm_code = orm.OAuthCode(code=code, expires_at=now() + 30) + db.add(orm_code) + db.commit() + + found = orm.OAuthCode.find(db, code) + assert found is orm_code + # purge_expired doesn't delete non-expired + orm.OAuthCode.purge_expired(db) + found = orm.OAuthCode.find(db, code) + assert found is orm_code + + with mock.patch.object(orm.OAuthCode, 'now', lambda: now() + 60): + found = orm.OAuthCode.find(db, code) + assert found is None + assert orm_code in db.query(orm.OAuthCode) + orm.OAuthCode.purge_expired(db) + assert orm_code not in db.query(orm.OAuthCode) diff --git a/jupyterhub/tests/test_pages.py b/jupyterhub/tests/test_pages.py index 61a98065..51b5b258 100644 --- a/jupyterhub/tests/test_pages.py +++ b/jupyterhub/tests/test_pages.py @@ -9,6 +9,7 @@ from urllib.parse import urlparse import pytest from bs4 import BeautifulSoup from tornado import gen +from tornado.escape import url_escape from tornado.httputil import url_concat from .. import orm @@ -92,8 +93,9 @@ async def test_home_auth(app): async def test_admin_no_auth(app): - r = await get_page('admin', app) - assert r.status_code == 403 + r = await get_page('admin', app, allow_redirects=False) + assert r.status_code == 302 + assert '/hub/login' in r.headers['Location'] async def test_admin_not_admin(app): @@ -109,6 +111,13 @@ async def test_admin(app): assert r.url.endswith('/admin') +async def test_admin_version(app): + cookies = await app.login_user('admin') + r = await get_page('admin', app, cookies=cookies, allow_redirects=False) + r.raise_for_status() + assert "version_footer" in r.text + + @pytest.mark.parametrize('sort', ['running', 'last_activity', 'admin', 'name']) async def test_admin_sort(app, sort): cookies = await app.login_user('admin') @@ -247,6 +256,47 @@ async def test_spawn_page_admin(app, admin_access): assert "Spawning server for {}".format(u.name) in r.text +async def test_spawn_with_query_arguments(app): + with mock.patch.dict(app.users.settings, {'spawner_class': FormSpawner}): + base_url = ujoin(public_host(app), app.hub.base_url) + cookies = await app.login_user('jones') + orm_u = orm.User.find(app.db, 'jones') + u = app.users[orm_u] + await u.stop() + next_url = ujoin(app.base_url, 'user/jones/tree') + r = await async_requests.get( + url_concat( + ujoin(base_url, 'spawn'), {'next': next_url, 'energy': '510keV'}, + ), + cookies=cookies, + ) + r.raise_for_status() + assert r.history + assert u.spawner.user_options == { + 'energy': '510keV', + 'notspecified': 5, + } + + +async def test_spawn_with_query_arguments_error(app): + with mock.patch.dict(app.users.settings, {'spawner_class': FormSpawner}): + base_url = ujoin(public_host(app), app.hub.base_url) + cookies = await app.login_user('jones') + orm_u = orm.User.find(app.db, 'jones') + u = app.users[orm_u] + await u.stop() + next_url = ujoin(app.base_url, 'user/jones/tree') + r = await async_requests.get( + url_concat( + ujoin(base_url, 'spawn'), + {'next': next_url, 'energy': '510keV', 'illegal_argument': '42'}, + ), + cookies=cookies, + ) + r.raise_for_status() + assert "You are not allowed to specify " in r.text + + async def test_spawn_form(app): with mock.patch.dict(app.users.settings, {'spawner_class': FormSpawner}): base_url = ujoin(public_host(app), app.hub.base_url) @@ -282,6 +332,12 @@ async def test_spawn_form_admin_access(app, admin_access): data={'bounds': ['-3', '3'], 'energy': '938MeV'}, ) r.raise_for_status() + + while '/spawn-pending/' in r.url: + await asyncio.sleep(0.1) + r = await async_requests.get(r.url, cookies=cookies) + r.raise_for_status() + assert r.history assert r.url.startswith(public_url(app, u)) assert u.spawner.user_options == { @@ -346,7 +402,7 @@ async def test_spawn_pending(app, username, slow_spawn): assert page.find('div', {'class': 'progress'}) # validate event source url by consuming it - script = page.body.find('script').text + script = page.body.find('script').string assert 'EventSource' in script # find EventSource url in javascript # maybe not the most robust way to check this? @@ -398,6 +454,47 @@ async def test_user_redirect(app, username): assert path == ujoin(app.base_url, '/user/%s/notebooks/test.ipynb' % name) +async def test_user_redirect_hook(app, username): + """ + Test proper behavior of user_redirect_hook + """ + name = username + cookies = await app.login_user(name) + + async def dummy_redirect(path, request, user, base_url): + assert base_url == app.base_url + assert path == 'redirect-to-terminal' + assert request.uri == ujoin( + base_url, 'hub', 'user-redirect', 'redirect-to-terminal' + ) + url = ujoin(user.url, '/terminals/1') + return url + + app.user_redirect_hook = dummy_redirect + + r = await get_page('/user-redirect/redirect-to-terminal', app) + r.raise_for_status() + print(urlparse(r.url)) + path = urlparse(r.url).path + assert path == ujoin(app.base_url, '/hub/login') + query = urlparse(r.url).query + assert query == urlencode( + {'next': ujoin(app.hub.base_url, '/user-redirect/redirect-to-terminal')} + ) + + # We don't actually want to start the server by going through spawn - just want to make sure + # the redirect is to the right place + r = await get_page( + '/user-redirect/redirect-to-terminal', + app, + cookies=cookies, + allow_redirects=False, + ) + r.raise_for_status() + redirected_url = urlparse(r.headers['Location']) + assert redirected_url.path == ujoin(app.base_url, 'user', username, 'terminals/1') + + async def test_user_redirect_deprecated(app, username): """redirecting from /user/someonelse/ URLs (deprecated)""" name = username @@ -426,6 +523,58 @@ async def test_user_redirect_deprecated(app, username): ) +@pytest.mark.parametrize( + 'url, params, redirected_url, form_action', + [ + ( + # spawn?param=value + # will encode given parameters for an unauthenticated URL in the next url + # the next parameter will contain the app base URL (replaces BASE_URL in tests) + 'spawn', + [('param', 'value')], + '/hub/login?next={{BASE_URL}}hub%2Fspawn%3Fparam%3Dvalue', + '/hub/login?next={{BASE_URL}}hub%2Fspawn%3Fparam%3Dvalue', + ), + ( + # login?param=fromlogin&next=encoded(/hub/spawn?param=value) + # will drop parameters given to the login page, passing only the next url + 'login', + [('param', 'fromlogin'), ('next', '/hub/spawn?param=value')], + '/hub/login?param=fromlogin&next=%2Fhub%2Fspawn%3Fparam%3Dvalue', + '/hub/login?next=%2Fhub%2Fspawn%3Fparam%3Dvalue', + ), + ( + # login?param=value&anotherparam=anothervalue + # will drop parameters given to the login page, and use an empty next url + 'login', + [('param', 'value'), ('anotherparam', 'anothervalue')], + '/hub/login?param=value&anotherparam=anothervalue', + '/hub/login?next=', + ), + ( + # login + # simplest case, accessing the login URL, gives an empty next url + 'login', + [], + '/hub/login', + '/hub/login?next=', + ), + ], +) +async def test_login_page(app, url, params, redirected_url, form_action): + url = url_concat(url, params) + r = await get_page(url, app) + redirected_url = redirected_url.replace('{{BASE_URL}}', url_escape(app.base_url)) + assert r.url.endswith(redirected_url) + # now the login.html rendered template must include the given parameters in the form + # action URL, including the next URL + page = BeautifulSoup(r.text, "html.parser") + form = page.find("form", method="post") + action = form.attrs['action'] + form_action = form_action.replace('{{BASE_URL}}', url_escape(app.base_url)) + assert action.endswith(form_action) + + async def test_login_fail(app): name = 'wash' base_url = public_url(app) @@ -456,26 +605,29 @@ async def test_login_strip(app): @pytest.mark.parametrize( - 'running, next_url, location', + 'running, next_url, location, params', [ # default URL if next not specified, for both running and not - (True, '', ''), - (False, '', ''), + (True, '', '', None), + (False, '', '', None), # next_url is respected - (False, '/hub/admin', '/hub/admin'), - (False, '/user/other', '/hub/user/other'), - (False, '/absolute', '/absolute'), - (False, '/has?query#andhash', '/has?query#andhash'), + (False, '/hub/admin', '/hub/admin', None), + (False, '/user/other', '/hub/user/other', None), + (False, '/absolute', '/absolute', None), + (False, '/has?query#andhash', '/has?query#andhash', None), # next_url outside is not allowed - (False, 'relative/path', ''), - (False, 'https://other.domain', ''), - (False, 'ftp://other.domain', ''), - (False, '//other.domain', ''), - (False, '///other.domain/triple', ''), - (False, '\\\\other.domain/backslashes', ''), + (False, 'relative/path', '', None), + (False, 'https://other.domain', '', None), + (False, 'ftp://other.domain', '', None), + (False, '//other.domain', '', None), + (False, '///other.domain/triple', '', None), + (False, '\\\\other.domain/backslashes', '', None), + # params are handled correctly + (True, '/hub/admin', 'hub/admin?left=1&right=2', [('left', 1), ('right', 2)]), + (False, '/hub/admin', 'hub/admin?left=1&right=2', [('left', 1), ('right', 2)]), ], ) -async def test_login_redirect(app, running, next_url, location): +async def test_login_redirect(app, running, next_url, location, params): cookies = await app.login_user('river') user = app.users['river'] if location: @@ -487,6 +639,8 @@ async def test_login_redirect(app, running, next_url, location): location = ujoin(app.base_url, 'hub/spawn') url = 'login' + if params: + url = url_concat(url, params) if next_url: if '//' not in next_url and next_url.startswith('/'): next_url = ujoin(app.base_url, next_url, '') @@ -596,7 +750,7 @@ async def test_shutdown_on_logout(app, shutdown_on_logout): assert spawner.ready == (not shutdown_on_logout) -async def test_login_no_whitelist_adds_user(app): +async def test_login_no_allowed_adds_user(app): auth = app.authenticator mock_add_user = mock.Mock() with mock.patch.object(auth, 'add_user', mock_add_user): @@ -611,7 +765,9 @@ async def test_static_files(app): r = await async_requests.get(ujoin(base_url, 'logo')) r.raise_for_status() assert r.headers['content-type'] == 'image/png' - r = await async_requests.get(ujoin(base_url, 'static', 'images', 'jupyter.png')) + r = await async_requests.get( + ujoin(base_url, 'static', 'images', 'jupyterhub-80.png') + ) r.raise_for_status() assert r.headers['content-type'] == 'image/png' r = await async_requests.get(ujoin(base_url, 'static', 'css', 'style.min.css')) @@ -787,3 +943,42 @@ async def test_metrics_auth(app): async def test_health_check_request(app): r = await get_page('health', app) assert r.status_code == 200 + + +async def test_pre_spawn_start_exc_no_form(app): + exc = "pre_spawn_start error" + + # throw exception from pre_spawn_start + @gen.coroutine + def mock_pre_spawn_start(user, spawner): + raise Exception(exc) + + with mock.patch.object(app.authenticator, 'pre_spawn_start', mock_pre_spawn_start): + cookies = await app.login_user('summer') + # spawn page should thow a 500 error and show the pre_spawn_start error message + r = await get_page('spawn', app, cookies=cookies) + assert r.status_code == 500 + assert exc in r.text + + +async def test_pre_spawn_start_exc_options_form(app): + exc = "pre_spawn_start error" + + # throw exception from pre_spawn_start + @gen.coroutine + def mock_pre_spawn_start(user, spawner): + raise Exception(exc) + + with mock.patch.dict( + app.users.settings, {'spawner_class': FormSpawner} + ), mock.patch.object(app.authenticator, 'pre_spawn_start', mock_pre_spawn_start): + cookies = await app.login_user('spring') + user = app.users['spring'] + # spawn page shouldn't throw any error until the spawn is started + r = await get_page('spawn', app, cookies=cookies) + assert r.url.endswith('/spawn') + r.raise_for_status() + assert FormSpawner.options_form in r.text + # spawning the user server should throw the pre_spawn_start error + with pytest.raises(Exception, match="%s" % exc): + await user.spawn() diff --git a/jupyterhub/tests/test_pagination.py b/jupyterhub/tests/test_pagination.py new file mode 100644 index 00000000..b9833eae --- /dev/null +++ b/jupyterhub/tests/test_pagination.py @@ -0,0 +1,45 @@ +"""tests for pagination""" +from pytest import mark +from pytest import raises +from traitlets.config import Config + +from jupyterhub.pagination import Pagination + + +def test_per_page_bounds(): + cfg = Config() + cfg.Pagination.max_per_page = 10 + p = Pagination(config=cfg, per_page=20, total=100) + assert p.per_page == 10 + with raises(Exception): + p.per_page = 0 + + +@mark.parametrize( + "page, per_page, total, expected", + [ + (1, 10, 99, [1, 2, 3, "...", 10]), + (2, 10, 99, [1, 2, 3, 4, "...", 10]), + (3, 10, 99, [1, 2, 3, 4, 5, "...", 10]), + (4, 10, 99, [1, 2, 3, 4, 5, 6, "...", 10]), + (5, 10, 99, [1, "...", 3, 4, 5, 6, 7, "...", 10]), + (6, 10, 99, [1, "...", 4, 5, 6, 7, 8, "...", 10]), + (7, 10, 99, [1, "...", 5, 6, 7, 8, 9, 10]), + (8, 10, 99, [1, "...", 6, 7, 8, 9, 10]), + (9, 10, 99, [1, "...", 7, 8, 9, 10]), + (1, 20, 99, [1, 2, 3, 4, 5]), + (1, 10, 0, [1]), + (1, 10, 1, [1]), + (1, 10, 10, [1]), + (1, 10, 11, [1, 2]), + (1, 10, 50, [1, 2, 3, 4, 5]), + (1, 10, 60, [1, 2, 3, 4, 5, 6]), + (1, 10, 70, [1, 2, 3, 4, 5, 6, 7]), + (1, 10, 80, [1, 2, 3, "...", 8]), + ], +) +def test_window(page, per_page, total, expected): + cfg = Config() + cfg.Pagination + pagination = Pagination(page=page, per_page=per_page, total=total) + assert pagination.calculate_pages_window() == expected diff --git a/jupyterhub/tests/test_proxy.py b/jupyterhub/tests/test_proxy.py index 0de5748b..e912bc62 100644 --- a/jupyterhub/tests/test_proxy.py +++ b/jupyterhub/tests/test_proxy.py @@ -2,7 +2,6 @@ import json import os from contextlib import contextmanager -from queue import Queue from subprocess import Popen from urllib.parse import quote from urllib.parse import urlparse diff --git a/jupyterhub/tests/test_services.py b/jupyterhub/tests/test_services.py index 248de1b1..127a9f45 100644 --- a/jupyterhub/tests/test_services.py +++ b/jupyterhub/tests/test_services.py @@ -2,18 +2,13 @@ import asyncio import os import sys -import time from binascii import hexlify from contextlib import contextmanager from subprocess import Popen -from threading import Event -import pytest -import requests from async_generator import async_generator from async_generator import asynccontextmanager from async_generator import yield_ -from tornado import gen from tornado.ioloop import IOLoop from ..utils import maybe_future diff --git a/jupyterhub/tests/test_services_auth.py b/jupyterhub/tests/test_services_auth.py index de4d73e9..867d1d97 100644 --- a/jupyterhub/tests/test_services_auth.py +++ b/jupyterhub/tests/test_services_auth.py @@ -11,7 +11,6 @@ from threading import Thread from unittest import mock from urllib.parse import urlparse -import pytest import requests import requests_mock from pytest import raises @@ -185,7 +184,7 @@ def test_hub_authenticated(request): m.get(good_url, text=json.dumps(mock_model)) - # no whitelist + # no specific allowed user r = requests.get( 'http://127.0.0.1:%i' % port, cookies={'jubal': 'early'}, @@ -194,7 +193,7 @@ def test_hub_authenticated(request): r.raise_for_status() assert r.status_code == 200 - # pass whitelist + # pass allowed user TestHandler.hub_users = {'jubalearly'} r = requests.get( 'http://127.0.0.1:%i' % port, @@ -204,7 +203,7 @@ def test_hub_authenticated(request): r.raise_for_status() assert r.status_code == 200 - # no pass whitelist + # no pass allowed ser TestHandler.hub_users = {'kaylee'} r = requests.get( 'http://127.0.0.1:%i' % port, @@ -213,7 +212,7 @@ def test_hub_authenticated(request): ) assert r.status_code == 403 - # pass group whitelist + # pass allowed group TestHandler.hub_groups = {'lions'} r = requests.get( 'http://127.0.0.1:%i' % port, @@ -223,7 +222,7 @@ def test_hub_authenticated(request): r.raise_for_status() assert r.status_code == 200 - # no pass group whitelist + # no pass allowed group TestHandler.hub_groups = {'tigers'} r = requests.get( 'http://127.0.0.1:%i' % port, diff --git a/jupyterhub/tests/test_singleuser.py b/jupyterhub/tests/test_singleuser.py index 1bd08696..49b366c9 100644 --- a/jupyterhub/tests/test_singleuser.py +++ b/jupyterhub/tests/test_singleuser.py @@ -3,8 +3,6 @@ import sys from subprocess import check_output from urllib.parse import urlparse -import pytest - import jupyterhub from ..utils import url_path_join from .mocking import public_url @@ -33,7 +31,11 @@ async def test_singleuser_auth(app): # with cookies, login successful r = await async_requests.get(url, cookies=cookies) r.raise_for_status() - assert urlparse(r.url).path.rstrip('/').endswith('/user/nandy/tree') + assert ( + urlparse(r.url) + .path.rstrip('/') + .endswith(url_path_join('/user/nandy', user.spawner.default_url or "/tree")) + ) assert r.status_code == 200 # logout @@ -48,7 +50,11 @@ async def test_singleuser_auth(app): assert urlparse(r.url).path.endswith('/oauth2/authorize') # submit the oauth form to complete authorization r = await s.post(r.url, data={'scopes': ['identify']}, headers={'Referer': r.url}) - assert urlparse(r.url).path.rstrip('/').endswith('/user/nandy/tree') + assert ( + urlparse(r.url) + .path.rstrip('/') + .endswith(url_path_join('/user/nandy', user.spawner.default_url or "/tree")) + ) # user isn't authorized, should raise 403 assert r.status_code == 403 assert 'burgess' in r.text @@ -76,7 +82,9 @@ async def test_disable_user_config(app): # with cookies, login successful r = await async_requests.get(url, cookies=cookies) r.raise_for_status() - assert r.url.rstrip('/').endswith('/user/nandy/tree') + assert r.url.rstrip('/').endswith( + url_path_join('/user/nandy', user.spawner.default_url or "/tree") + ) assert r.status_code == 200 diff --git a/jupyterhub/tests/test_spawner.py b/jupyterhub/tests/test_spawner.py index 30d35b8f..99b84393 100644 --- a/jupyterhub/tests/test_spawner.py +++ b/jupyterhub/tests/test_spawner.py @@ -76,7 +76,8 @@ async def test_spawner(db, request): assert status is None await spawner.stop() status = await spawner.poll() - assert status == 1 + assert status is not None + assert isinstance(status, int) async def wait_for_spawner(spawner, timeout=10): @@ -102,7 +103,8 @@ async def wait_for_spawner(spawner, timeout=10): async def test_single_user_spawner(app, request): - user = next(iter(app.users.values()), None) + orm_user = app.db.query(orm.User).first() + user = app.users[orm_user] spawner = user.spawner spawner.cmd = ['jupyterhub-singleuser'] await user.spawn() @@ -402,3 +404,15 @@ async def test_spawner_routing(app, name): assert r.url == url assert r.text == urlparse(url).path await user.stop() + + +async def test_spawner_env(db): + env_overrides = { + "JUPYTERHUB_API_URL": "https://test.horse/hub/api", + "TEST_KEY": "value", + } + spawner = new_spawner(db, environment=env_overrides) + env = spawner.get_env() + for key, value in env_overrides.items(): + assert key in env + assert env[key] == value diff --git a/jupyterhub/tests/test_user.py b/jupyterhub/tests/test_user.py new file mode 100644 index 00000000..df7bc8a2 --- /dev/null +++ b/jupyterhub/tests/test_user.py @@ -0,0 +1,22 @@ +import pytest + +from ..user import UserDict +from .utils import add_user + + +@pytest.mark.parametrize("attr", ["self", "id", "name"]) +async def test_userdict_get(db, attr): + u = add_user(db, name="rey", app=False) + userdict = UserDict(db_factory=lambda: db, settings={}) + + if attr == "self": + key = u + else: + key = getattr(u, attr) + + # `in` checks cache only + assert key not in userdict + assert userdict.get(key) + assert userdict.get(key).id == u.id + # `in` should find it now + assert key in userdict diff --git a/jupyterhub/tests/test_version.py b/jupyterhub/tests/test_version.py index 974c01e5..db9fdd82 100644 --- a/jupyterhub/tests/test_version.py +++ b/jupyterhub/tests/test_version.py @@ -4,6 +4,11 @@ import logging import pytest from .._version import _check_version +from .._version import reset_globals + + +def setup_function(function): + reset_globals() @pytest.mark.parametrize( @@ -25,3 +30,27 @@ def test_check_version(hub_version, singleuser_version, log_level, msg, caplog): record = caplog.records[0] assert record.levelno == log_level assert msg in record.getMessage() + + +def test_check_version_singleton(caplog): + """Tests that minor version difference logging is only logged once.""" + # Run test_check_version twice which will assert that the warning is only logged + # once. + for x in range(2): + test_check_version( + '1.2.0', + '1.1.0', + logging.WARNING, + 'This could cause failure to authenticate', + caplog, + ) + # Run it again with a different singleuser_version to make sure that is logged as + # a warning. + caplog.clear() + test_check_version( + '1.2.0', + '1.1.1', + logging.WARNING, + 'This could cause failure to authenticate', + caplog, + ) diff --git a/jupyterhub/traitlets.py b/jupyterhub/traitlets.py index af3cd7fa..0cc616a9 100644 --- a/jupyterhub/traitlets.py +++ b/jupyterhub/traitlets.py @@ -9,6 +9,7 @@ from traitlets import List from traitlets import TraitError from traitlets import TraitType from traitlets import Type +from traitlets import Undefined from traitlets import Unicode @@ -27,11 +28,15 @@ class Command(List): but allows it to be specified as a single string. """ - def __init__(self, default_value=None, **kwargs): + def __init__(self, default_value=Undefined, **kwargs): kwargs.setdefault('minlen', 1) if isinstance(default_value, str): default_value = [default_value] - super().__init__(Unicode(), default_value, **kwargs) + if default_value is not Undefined and ( + not (default_value is None and not kwargs.get("allow_none", False)) + ): + kwargs["default_value"] = default_value + super().__init__(Unicode(), **kwargs) def validate(self, obj, value): if isinstance(value, str): diff --git a/jupyterhub/user.py b/jupyterhub/user.py index 42dbcdf5..a970c120 100644 --- a/jupyterhub/user.py +++ b/jupyterhub/user.py @@ -1,5 +1,6 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. +import json import warnings from collections import defaultdict from datetime import datetime @@ -21,6 +22,7 @@ from .crypto import decrypt from .crypto import encrypt from .crypto import EncryptionUnavailable from .crypto import InvalidToken +from .metrics import RUNNING_SERVERS from .metrics import TOTAL_USERS from .objects import Server from .spawner import LocalProcessSpawner @@ -32,7 +34,23 @@ from .utils import url_path_join class UserDict(dict): """Like defaultdict, but for users - Getting by a user id OR an orm.User instance returns a User wrapper around the orm user. + Users can be retrieved by: + + - integer database id + - orm.User object + - username str + + A User wrapper object is always returned. + + This dict contains at least all active users, + but not necessarily all users in the database. + + Checking `key in userdict` returns whether + an item is already in the cache, + *not* whether it is in the database. + + .. versionchanged:: 1.2 + ``'username' in userdict`` pattern is now supported """ def __init__(self, db_factory, settings): @@ -55,11 +73,28 @@ class UserDict(dict): return self[orm_user.id] def __contains__(self, key): + """key in userdict checks presence in the cache + + it does not check if the user is in the database + """ if isinstance(key, (User, orm.User)): key = key.id + elif isinstance(key, str): + # username lookup, O(N) + for user in self.values(): + if user.name == key: + key = user.id + break return dict.__contains__(self, key) def __getitem__(self, key): + """UserDict allows retrieval of user by any of: + + - User object + - orm.User object + - username (str) + - orm.User.id int (actual key used in underlying dict) + """ if isinstance(key, User): key = key.id elif isinstance(key, str): @@ -67,7 +102,7 @@ class UserDict(dict): if orm_user is None: raise KeyError("No such user: %s" % key) else: - key = orm_user + key = orm_user.id if isinstance(key, orm.User): # users[orm_user] returns User(orm_user) orm_user = key @@ -90,6 +125,20 @@ class UserDict(dict): else: raise KeyError(repr(key)) + def get(self, key, default=None): + """Retrieve a User object if it can be found, else default + + Lookup can be by User object, id, or name + + .. versionchanged:: 1.2 + ``get()`` accesses the database instead of just the cache by integer id, + so is equivalent to catching KeyErrors on attempted lookup. + """ + try: + return self[key] + except KeyError: + return default + def __delitem__(self, key): user = self[key] for orm_spawner in user.orm_user._orm_spawners: @@ -349,6 +398,11 @@ class User: """My name, escaped for use in URLs, cookies, etc.""" return quote(self.name, safe='@~') + @property + def json_escaped_name(self): + """The user name, escaped for use in javascript inserts, etc.""" + return json.dumps(self.name)[1:-1] + @property def proxy_spec(self): """The proxy routespec for my default server""" @@ -522,17 +576,24 @@ class User: # trigger pre-spawn hook on authenticator authenticator = self.authenticator - if authenticator: - await maybe_future(authenticator.pre_spawn_start(self, spawner)) - - spawner._start_pending = True - # update spawner start time, and activity for both spawner and user - self.last_activity = ( - spawner.orm_spawner.started - ) = spawner.orm_spawner.last_activity = datetime.utcnow() - db.commit() - # wait for spawner.start to return try: + spawner._start_pending = True + + if authenticator: + # pre_spawn_start can thow errors that can lead to a redirect loop + # if left uncaught (see https://github.com/jupyterhub/jupyterhub/issues/2683) + await maybe_future(authenticator.pre_spawn_start(self, spawner)) + + # trigger auth_state hook + auth_state = await self.get_auth_state() + await spawner.run_auth_state_hook(auth_state) + + # update spawner start time, and activity for both spawner and user + self.last_activity = ( + spawner.orm_spawner.started + ) = spawner.orm_spawner.last_activity = datetime.utcnow() + db.commit() + # wait for spawner.start to return # run optional preparation work to bootstrap the notebook await maybe_future(spawner.run_pre_spawn_hook()) if self.settings.get('internal_ssl'): @@ -552,7 +613,12 @@ class User: else: # >= 0.7 returns (ip, port) proto = 'https' if self.settings['internal_ssl'] else 'http' - url = '%s://%s:%i' % ((proto,) + url) + + # check if spawner returned an IPv6 address + if ':' in url[0]: + url = '%s://[%s]:%i' % ((proto,) + url) + else: + url = '%s://%s:%i' % ((proto,) + url) urlinfo = urlparse(url) server.proto = urlinfo.scheme server.ip = urlinfo.hostname @@ -718,6 +784,7 @@ class User: spawner = self.spawners[server_name] spawner._spawn_pending = False spawner._start_pending = False + spawner._check_pending = False spawner.stop_polling() spawner._stop_pending = True @@ -728,8 +795,6 @@ class User: status = await spawner.poll() if status is None: await spawner.stop() - spawner.clear_state() - spawner.orm_spawner.state = spawner.get_state() self.last_activity = spawner.orm_spawner.last_activity = datetime.utcnow() # remove server entry from db spawner.server = None @@ -753,11 +818,23 @@ class User: self.db.delete(oauth_client) self.db.commit() self.log.debug("Finished stopping %s", spawner._log_name) + RUNNING_SERVERS.dec() finally: + spawner.server = None spawner.orm_spawner.started = None self.db.commit() # trigger post-stop hook - await maybe_future(spawner.run_post_stop_hook()) + try: + await maybe_future(spawner.run_post_stop_hook()) + except: + spawner.clear_state() + spawner.orm_spawner.state = spawner.get_state() + self.db.commit() + raise + spawner.clear_state() + spawner.orm_spawner.state = spawner.get_state() + self.db.commit() + # trigger post-spawner hook on authenticator auth = spawner.authenticator try: diff --git a/jupyterhub/utils.py b/jupyterhub/utils.py index 681e8f9a..73235914 100644 --- a/jupyterhub/utils.py +++ b/jupyterhub/utils.py @@ -50,7 +50,7 @@ ISO8601_s = '%Y-%m-%dT%H:%M:%SZ' def isoformat(dt): """Render a datetime object as an ISO 8601 UTC timestamp - Naïve datetime objects are assumed to be UTC + Naive datetime objects are assumed to be UTC """ # allow null timestamps to remain None without # having to check if isoformat should be called @@ -66,7 +66,7 @@ def can_connect(ip, port): Return True if we can connect, False otherwise. """ - if ip in {'', '0.0.0.0'}: + if ip in {'', '0.0.0.0', '::'}: ip = '127.0.0.1' try: socket.create_connection((ip, port)).close() @@ -85,6 +85,7 @@ def make_ssl_context(keyfile, certfile, cafile=None, verify=True, check_hostname return None purpose = ssl.Purpose.SERVER_AUTH if verify else ssl.Purpose.CLIENT_AUTH ssl_context = ssl.create_default_context(purpose, cafile=cafile) + ssl_context.load_default_certs() ssl_context.load_cert_chain(certfile, keyfile) ssl_context.check_hostname = check_hostname return ssl_context @@ -172,14 +173,15 @@ async def exponential_backoff( # this prevents overloading any single tornado loop iteration with # too many things dt = min(max_wait, remaining, random.uniform(0, start_wait * scale)) - scale *= scale_factor + if dt < max_wait: + scale *= scale_factor await gen.sleep(dt) raise TimeoutError(fail_message) async def wait_for_server(ip, port, timeout=10): """Wait for any server to show up at ip:port.""" - if ip in {'', '0.0.0.0'}: + if ip in {'', '0.0.0.0', '::'}: ip = '127.0.0.1' await exponential_backoff( lambda: can_connect(ip, port), @@ -444,7 +446,6 @@ def print_stacks(file=sys.stderr): # local imports because these will not be used often, # no need to add them to startup import asyncio - import resource import traceback from .log import coroutine_frames @@ -580,7 +581,7 @@ def utcnow(): def _parse_accept_header(accept): """ Parse the Accept header *accept* - + Return a list with 3-tuples of [(str(media_type), dict(params), float(q_value)),] ordered by q values. If the accept header includes vendor-specific types like:: diff --git a/onbuild/Dockerfile b/onbuild/Dockerfile index ad941aac..93655f4f 100644 --- a/onbuild/Dockerfile +++ b/onbuild/Dockerfile @@ -1,12 +1,12 @@ # JupyterHub Dockerfile that loads your jupyterhub_config.py # -# Adds ONBUILD step to jupyter/jupyterhub to load your juptyerhub_config.py into the image +# Adds ONBUILD step to jupyter/jupyterhub to load your jupyterhub_config.py into the image # # Derivative images must have jupyterhub_config.py next to the Dockerfile. -ARG BASE_IMAGE=jupyterhub/jupyterhub -FROM ${BASE_IMAGE} +ARG BASE_IMAGE=jupyterhub/jupyterhub:latest +FROM $BASE_IMAGE -ONBUILD ADD jupyterhub_config.py /srv/jupyterhub/jupyterhub_config.py +ONBUILD COPY jupyterhub_config.py /srv/jupyterhub/jupyterhub_config.py CMD ["jupyterhub", "-f", "/srv/jupyterhub/jupyterhub_config.py"] diff --git a/onbuild/README.md b/onbuild/README.md index 8964110d..fae5e4fb 100644 --- a/onbuild/README.md +++ b/onbuild/README.md @@ -2,7 +2,7 @@ If you base a Dockerfile on this image: - FROM juptyerhub/jupyterhub-onbuild:0.6 + FROM jupyterhub/jupyterhub-onbuild:0.6 ... then your `jupyterhub_config.py` adjacent to your Dockerfile will be loaded into the image and used by JupyterHub. diff --git a/package.json b/package.json index ff6c5dab..fba009a5 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "url": "https://github.com/jupyter/jupyterhub.git" }, "scripts": { - "postinstall": "python ./bower-lite", + "postinstall": "python3 ./bower-lite", "fmt": "prettier --write --trailing-comma es5 share/jupyterhub/static/js/*", "lessc": "lessc" }, @@ -21,7 +21,7 @@ "dependencies": { "bootstrap": "^3.4.1", "font-awesome": "^4.7.0", - "jquery": "^3.3.1", + "jquery": "^3.5.1", "moment": "^2.24.0", "requirejs": "^2.3.6" } diff --git a/pytest.ini b/pytest.ini index e3af6b27..dff95321 100644 --- a/pytest.ini +++ b/pytest.ini @@ -3,6 +3,9 @@ # so we have to disable this until pytest 3.11 # minversion = 3.3 +# jupyter_server plugin is incompatible with notebook imports +addopts = -p no:jupyter_server + python_files = test_*.py markers = gen_test: marks an async tornado test diff --git a/readthedocs.yml b/readthedocs.yml index 3a1802a6..6349c32d 100644 --- a/readthedocs.yml +++ b/readthedocs.yml @@ -1,11 +1,19 @@ -name: jupyterhub -type: sphinx -conda: - file: docs/environment.yml +version: 2 + +sphinx: + configuration: docs/source/conf.py + +build: + image: latest + python: - version: 3 + version: 3.7 + install: + - method: pip + path: . + - requirements: docs/requirements.txt + + formats: - htmlzip - epub - # pdf disabled due to bug in sphinx 1.8 + recommonmark - # - pdf diff --git a/requirements.txt b/requirements.txt index ad40788f..28e6987d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,9 +3,11 @@ async_generator>=1.8 certipy>=0.1.2 entrypoints jinja2 +jupyter_telemetry>=0.1.0 oauthlib>=3.0 -pamela +pamela; sys_platform != 'win32' prometheus_client>=0.0.21 +psutil>=5.6.5; sys_platform == 'win32' python-dateutil requests SQLAlchemy>=1.1 diff --git a/setup.py b/setup.py index 21b5ab4a..f6d545c6 100755 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # coding: utf-8 -# Copyright (c) Juptyer Development Team. +# Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. # ----------------------------------------------------------------------------- # Minimal Python version sanity check (from IPython) @@ -10,7 +10,6 @@ from __future__ import print_function import os import shutil import sys -from glob import glob from subprocess import check_call from setuptools import setup @@ -18,8 +17,8 @@ from setuptools.command.bdist_egg import bdist_egg v = sys.version_info -if v[:2] < (3, 5): - error = "ERROR: JupyterHub requires Python version 3.5 or above." +if v[:2] < (3, 6): + error = "ERROR: JupyterHub requires Python version 3.6 or above." print(error, file=sys.stderr) sys.exit(1) @@ -95,7 +94,7 @@ setup_args = dict( license="BSD", platforms="Linux, Mac OS X", keywords=['Interactive', 'Interpreter', 'Shell', 'Web'], - python_requires=">=3.5", + python_requires=">=3.6", entry_points={ 'jupyterhub.authenticators': [ 'default = jupyterhub.auth:PAMAuthenticator', diff --git a/setupegg.py b/setupegg.py deleted file mode 100755 index fa537b36..00000000 --- a/setupegg.py +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env python -"""Wrapper to run setup.py using setuptools.""" -# Import setuptools and call the actual setup -import setuptools - -with open('setup.py', 'rb') as f: - exec(compile(f.read(), 'setup.py', 'exec')) diff --git a/share/jupyterhub/static/js/admin.js b/share/jupyterhub/static/js/admin.js index 63680b30..584786d7 100644 --- a/share/jupyterhub/static/js/admin.js +++ b/share/jupyterhub/static/js/admin.js @@ -1,9 +1,8 @@ // Copyright (c) Jupyter Development Team. // Distributed under the terms of the Modified BSD License. -require(["jquery", "bootstrap", "moment", "jhapi", "utils"], function( +require(["jquery", "moment", "jhapi", "utils"], function( $, - bs, moment, JHAPI, utils diff --git a/share/jupyterhub/static/js/home.js b/share/jupyterhub/static/js/home.js index 616170a9..909c2b73 100644 --- a/share/jupyterhub/static/js/home.js +++ b/share/jupyterhub/static/js/home.js @@ -1,16 +1,14 @@ // Copyright (c) Jupyter Development Team. // Distributed under the terms of the Modified BSD License. -require(["jquery", "moment", "jhapi", "utils"], function( +require(["jquery", "moment", "jhapi"], function( $, moment, - JHAPI, - utils + JHAPI ) { "use strict"; var base_url = window.jhdata.base_url; - var prefix = window.jhdata.prefix; var user = window.jhdata.user; var api = new JHAPI(base_url); @@ -50,6 +48,17 @@ require(["jquery", "moment", "jhapi", "utils"], function( } } + function startServer() { + var row = getRow($(this)); + var serverName = row.find(".new-server-name").val(); + if (serverName === "") { + // ../spawn/user/ causes a 404, ../spawn/user redirects correctly to the default server + window.location.href = "./spawn/" + user; + } else { + window.location.href = "./spawn/" + user + "/" + serverName; + } + } + function stopServer() { var row = getRow($(this)); var serverName = row.data("server-name"); @@ -90,19 +99,22 @@ require(["jquery", "moment", "jhapi", "utils"], function( }); api.stop_server(user, { success: function() { + $("#stop").hide(); $("#start") .text("Start My Server") .attr("title", "Start your default server") .attr("disabled", false) + .attr("href", base_url + "spawn/" + user) .off("click"); }, }); }); - $(".new-server-btn").click(function() { - var row = getRow($(this)); - var serverName = row.find(".new-server-name").val(); - window.location.href = "../spawn/" + user + "/" + serverName; + $(".new-server-btn").click(startServer); + $(".new-server-name").on('keypress', function(e) { + if (e.which === 13) { + startServer.call(this); + } }); $(".stop-server").click(stopServer); diff --git a/share/jupyterhub/static/js/not_running.js b/share/jupyterhub/static/js/not_running.js new file mode 100644 index 00000000..12ad601a --- /dev/null +++ b/share/jupyterhub/static/js/not_running.js @@ -0,0 +1,13 @@ +// Copyright (c) Jupyter Development Team. +// Distributed under the terms of the Modified BSD License. + +require(["jquery", "utils"], function($, utils) { + "use strict"; + + var hash = utils.parse_url(window.location.href).hash; + if (hash !== undefined && hash !== '') { + var el = $("#start"); + var current_spawn_url = el.attr("href"); + el.attr("href", current_spawn_url + hash); + } +}); diff --git a/share/jupyterhub/static/js/utils.js b/share/jupyterhub/static/js/utils.js index 63ed0bd0..c09f48cf 100644 --- a/share/jupyterhub/static/js/utils.js +++ b/share/jupyterhub/static/js/utils.js @@ -2,7 +2,7 @@ // Original Copyright (c) IPython Development Team. // Distributed under the terms of the Modified BSD License. -// Modifications Copyright (c) Juptyer Development Team. +// Modifications Copyright (c) Jupyter Development Team. // Distributed under the terms of the Modified BSD License. define(["jquery"], function($) { diff --git a/share/jupyterhub/static/less/admin.less b/share/jupyterhub/static/less/admin.less index 70a262d9..0995b019 100644 --- a/share/jupyterhub/static/less/admin.less +++ b/share/jupyterhub/static/less/admin.less @@ -1,3 +1,12 @@ i.sort-icon { margin-left: 4px; } + +tr.pagination-row > td.pagination-page-info { + vertical-align: middle; +} + +.version_footer { + bottom: 0; + width: 100%; +} diff --git a/share/jupyterhub/static/less/login.less b/share/jupyterhub/static/less/login.less index d0040908..f5ca78e5 100644 --- a/share/jupyterhub/static/less/login.less +++ b/share/jupyterhub/static/less/login.less @@ -6,7 +6,7 @@ .bg-warning(); padding:10px; } - + .service-login { text-align: center; display: table-cell; @@ -27,9 +27,9 @@ } input[type=submit] { - margin-top: 16px; + margin-top: 0px; } - + .form-control:focus, input[type=submit]:focus { box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px @jupyter-orange; border-color: @jupyter-orange; diff --git a/share/jupyterhub/static/less/page.less b/share/jupyterhub/static/less/page.less index 6193b357..0d471a66 100644 --- a/share/jupyterhub/static/less/page.less +++ b/share/jupyterhub/static/less/page.less @@ -1,8 +1,30 @@ +@import "../components/bootstrap/less/variables.less"; + @logo-height: 28px; -.jpy-logo { - height: @logo-height; - margin-top: (@navbar-height - @logo-height) / 2; +#jupyterhub-logo { + @media (max-width: @grid-float-breakpoint) { + // same length as the navbar-toggle element, displayed on responsive mode + margin-left: 15px; + } + .jpy-logo { + height: @logo-height; + margin-top: (@navbar-height - @logo-height) / 2; + } +} + +.navbar-right { + li { + span { + // same as .nav > li > a from bootstrap, but applied to the span[id="login_widget"] + // or any other span that matches .nav > li > span, but only in responsive mode + @media (max-width: @grid-float-breakpoint) { + position: relative; + display: block; + padding: 10px 15px; + } + } + } } #header { @@ -26,3 +48,19 @@ // .progress-log-event:hover { // background: rgba(66, 165, 245, 0.2); // } + + +.feedback { + &-container { + margin-top: 16px; + } + + &-widget { + padding: 5px 0px 0px 6px; + i { + font-size: 2em; + color: lightgrey; + } + } + +} diff --git a/share/jupyterhub/templates/admin.html b/share/jupyterhub/templates/admin.html index 12edbdcd..41df5c01 100644 --- a/share/jupyterhub/templates/admin.html +++ b/share/jupyterhub/templates/admin.html @@ -22,7 +22,7 @@ {% block thead %} - {{ th("User (%i)" % users|length, 'name') }} + {{ th("User", 'name') }} {{ th("Admin", 'admin') }} {{ th("Last Activity", 'last_activity') }} {{ th("Running (%i)" % running|length, 'running', colspan=2) }} @@ -96,11 +96,23 @@ delete server {%- endif -%} - {% endblock user_row %} + {% endfor %} {% endfor %} + + + + {% if pagination.links %} + + {% endif %} + + + Displaying users {{ pagination.info.start|safe }} - {{ pagination.info.end|safe }} of {{ pagination.info.total|safe }} + + +
@@ -158,6 +170,14 @@ {% endblock %} +{% block footer %} + +{% endblock %} + {% block script %} {{ super() }} {% if login_term_url %} diff --git a/share/jupyterhub/templates/not_running.html b/share/jupyterhub/templates/not_running.html index 182e7ba0..0ef3ae7d 100644 --- a/share/jupyterhub/templates/not_running.html +++ b/share/jupyterhub/templates/not_running.html @@ -23,7 +23,14 @@ {% endif %} Would you like to retry starting it? {% else %} - Your server {{ server_name }} is not running. Would you like to start it? + Your server {{ server_name }} is not running. + {% if implicit_spawn_seconds %} + It will be restarted automatically. + If you are not redirected in a few seconds, + click below to launch your server. + {% else %} + Would you like to start it? + {% endif %} {% endif %}

{% endblock %} @@ -42,3 +49,21 @@ {% endblock %} +{% block script %} +{{ super () }} +{% if implicit_spawn_seconds %} + +{% endif %} + +{% endblock script %} diff --git a/share/jupyterhub/templates/page.html b/share/jupyterhub/templates/page.html index 5677eb93..f2564b66 100644 --- a/share/jupyterhub/templates/page.html +++ b/share/jupyterhub/templates/page.html @@ -34,9 +34,14 @@ {% block stylesheet %} {% endblock %} + {% block favicon %} + + {% endblock %} + {% block scripts %} + {% endblock %} +{% endblock %} diff --git a/singleuser/hooks/build b/singleuser/hooks/build index a0a77621..7f8bb861 100644 --- a/singleuser/hooks/build +++ b/singleuser/hooks/build @@ -1,11 +1,4 @@ #!/bin/bash set -ex -stable=0.9 - -for V in master $stable; do - docker build --build-arg JUPYTERHUB_VERSION=$V -t $DOCKER_REPO:$V . -done - -echo "tagging $IMAGE_NAME" -docker tag $DOCKER_REPO:$stable $IMAGE_NAME +docker build --build-arg JUPYTERHUB_VERSION=$DOCKER_TAG -t $DOCKER_REPO:$DOCKER_TAG . diff --git a/singleuser/hooks/post_push b/singleuser/hooks/post_push index 95c3cb01..616d2525 100644 --- a/singleuser/hooks/post_push +++ b/singleuser/hooks/post_push @@ -1,15 +1,10 @@ #!/bin/bash set -ex -stable=0.9 -for V in master $stable; do - docker push $DOCKER_REPO:$V -done - function get_hub_version() { rm -f hub_version V=$1 - docker run --rm -v $PWD:/version -u $(id -u) -i $DOCKER_REPO:$V sh -c 'jupyterhub --version > /version/hub_version' + docker run --rm -v $PWD:/version -u $(id -u) -i $DOCKER_REPO:$DOCKER_TAG sh -c 'jupyterhub --version > /version/hub_version' hub_xyz=$(cat hub_version) split=( ${hub_xyz//./ } ) hub_xy="${split[0]}.${split[1]}" @@ -18,14 +13,9 @@ function get_hub_version() { hub_xy="${hub_xy}.${split[3]}" fi } -# tag e.g. 0.8.1 with 0.8 -get_hub_version $stable -docker tag $DOCKER_REPO:$stable $DOCKER_REPO:$hub_xyz -docker push $DOCKER_REPO:$hub_xyz - # tag e.g. 0.9 with master -get_hub_version master -docker tag $DOCKER_REPO:master $DOCKER_REPO:$hub_xy +get_hub_version +docker tag $DOCKER_REPO:$DOCKER_TAG $DOCKER_REPO:$hub_xy docker push $DOCKER_REPO:$hub_xy -docker tag $DOCKER_REPO:master $DOCKER_REPO:$hub_xyz +docker tag $DOCKER_REPO:$DOCKER_TAG $DOCKER_REPO:$hub_xyz docker push $DOCKER_REPO:$hub_xyz diff --git a/testing/jupyterhub_config.py b/testing/jupyterhub_config.py index 56aea917..bd70ed97 100644 --- a/testing/jupyterhub_config.py +++ b/testing/jupyterhub_config.py @@ -13,6 +13,6 @@ c.JupyterHub.authenticator_class = DummyAuthenticator # Optionally set a global password that all users must use # c.DummyAuthenticator.password = "your_password" -from jupyterhub.spawners import SimpleSpawner +from jupyterhub.spawner import SimpleLocalProcessSpawner -c.JupyterHub.spawner_class = SimpleSpawner +c.JupyterHub.spawner_class = SimpleLocalProcessSpawner