From 340abcccac525e7a44773c9ba9ba8fefa34b3c1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Collonval?= Date: Wed, 27 Jul 2022 16:49:31 +0200 Subject: [PATCH 1/6] Remove listings handler Remove deprecated block/white list --- docs/source/api/handlers.rst | 13 ---- jupyterlab_server/app.py | 27 ++------ jupyterlab_server/handlers.py | 44 ------------- jupyterlab_server/listings_handler.py | 91 --------------------------- jupyterlab_server/rest-api.yml | 33 ---------- 5 files changed, 5 insertions(+), 203 deletions(-) delete mode 100644 jupyterlab_server/listings_handler.py diff --git a/docs/source/api/handlers.rst b/docs/source/api/handlers.rst index f3e13d02..f002f76c 100644 --- a/docs/source/api/handlers.rst +++ b/docs/source/api/handlers.rst @@ -17,19 +17,6 @@ Module: :mod:`jupyterlab_server.handlers` .. autofunction:: is_url -Module: :mod:`jupyterlab_server.listings_handler` -================================================= - -.. automodule:: jupyterlab_server.listings_handler - -.. currentmodule:: jupyterlab_server.listings_handler - -.. autoclass:: ListingsHandler - :members: - -.. autofunction:: fetch_listings - - Module: :mod:`jupyterlab_server.settings_handler` ================================================= diff --git a/jupyterlab_server/app.py b/jupyterlab_server/app.py index 69f67c78..ac7ec427 100644 --- a/jupyterlab_server/app.py +++ b/jupyterlab_server/app.py @@ -27,10 +27,8 @@ def app_namespace(self): # Should your extension expose other server extensions when launched directly? load_other_extensions = True - app_version = Unicode("", help="The version of the application.").tag(default=__version__) - - blacklist_uris = Unicode( - "", config=True, help="Deprecated, use `LabServerApp.blocked_extensions_uris`" + app_version = Unicode("", help="The version of the application.").tag( + default=__version__ ) blocked_extensions_uris = Unicode( @@ -38,24 +36,14 @@ def app_namespace(self): config=True, help=""" A list of comma-separated URIs to get the blocked extensions list - - .. versionchanged:: 2.0.0 - `LabServerApp.blacklist_uris` renamed to `blocked_extensions_uris` """, ) - whitelist_uris = Unicode( - "", config=True, help="Deprecated, use `LabServerApp.allowed_extensions_uris`" - ) - allowed_extensions_uris = Unicode( "", config=True, help=""" - "A list of comma-separated URIs to get the allowed extensions list - - .. versionchanged:: 2.0.0 - `LabServerApp.whitetlist_uris` renamed to `allowed_extensions_uris` + A list of comma-separated URIs to get the allowed extensions list """, ) @@ -63,18 +51,13 @@ def app_namespace(self): 60 * 60, config=True, help="The interval delay in seconds to refresh the lists" ) - listings_request_options = Dict( + listings_tornado_options = Dict( {}, config=True, help="The optional kwargs to use for the listings HTTP requests \ - as described on https://2.python-requests.org/en/v2.7.0/api/#requests.request", + as described on https://www.tornadoweb.org/en/stable/httpclient.html#tornado.httpclient.HTTPRequest", ) - _deprecated_aliases = { - "blacklist_uris": ("blocked_extensions_uris", "1.2"), - "whitelist_uris": ("allowed_extensions_uris", "1.2"), - } - # Method copied from # https://github.com/jupyterhub/jupyterhub/blob/d1a85e53dccfc7b1dd81b0c1985d158cc6b61820/jupyterhub/auth.py#L143-L161 @observe(*list(_deprecated_aliases)) diff --git a/jupyterlab_server/handlers.py b/jupyterlab_server/handlers.py index f9eea346..35f03b4b 100644 --- a/jupyterlab_server/handlers.py +++ b/jupyterlab_server/handlers.py @@ -15,7 +15,6 @@ from .config import LabConfig, get_page_config, recursive_update from .licenses_handler import LicensesHandler, LicensesManager -from .listings_handler import ListingsHandler, fetch_listings from .server import FileFindHandler, JupyterHandler from .server import url_path_join as ujoin from .settings_handler import SettingsHandler @@ -249,49 +248,6 @@ def add_handlers(handlers, extension_app): workspace_api_path = ujoin(extension_app.workspaces_api_url, "(?P.+)") handlers.append((workspace_api_path, WorkspacesHandler, workspaces_config)) - # Handle local listings. - - settings_config = extension_app.settings.get("config", {}).get("LabServerApp", {}) - blocked_extensions_uris = settings_config.get("blocked_extensions_uris", "") - allowed_extensions_uris = settings_config.get("allowed_extensions_uris", "") - - if (blocked_extensions_uris) and (allowed_extensions_uris): - warnings.warn( - "Simultaneous blocked_extensions_uris and allowed_extensions_uris is not supported. Please define only one of those." - ) - import sys - - sys.exit(-1) - - ListingsHandler.listings_refresh_seconds = settings_config.get( - "listings_refresh_seconds", 60 * 60 - ) - ListingsHandler.listings_request_opts = settings_config.get("listings_request_options", {}) - listings_url = ujoin(extension_app.listings_url) - listings_path = ujoin(listings_url, "(.*)") - - if blocked_extensions_uris: - ListingsHandler.blocked_extensions_uris = set(blocked_extensions_uris.split(",")) - if allowed_extensions_uris: - ListingsHandler.allowed_extensions_uris = set(allowed_extensions_uris.split(",")) - - fetch_listings(None) - - if ( - len(ListingsHandler.blocked_extensions_uris) > 0 - or len(ListingsHandler.allowed_extensions_uris) > 0 - ): - from tornado import ioloop - - ListingsHandler.pc = ioloop.PeriodicCallback( - lambda: fetch_listings(None), - callback_time=ListingsHandler.listings_refresh_seconds * 1000, - jitter=0.1, - ) - ListingsHandler.pc.start() - - handlers.append((listings_path, ListingsHandler, {})) - # Handle local themes. if extension_app.themes_dir: themes_url = extension_app.themes_url diff --git a/jupyterlab_server/listings_handler.py b/jupyterlab_server/listings_handler.py deleted file mode 100644 index 9407192b..00000000 --- a/jupyterlab_server/listings_handler.py +++ /dev/null @@ -1,91 +0,0 @@ -"""Tornado handlers for listing extensions.""" - -# Copyright (c) Jupyter Development Team. -# Distributed under the terms of the Modified BSD License. - -import json - -import requests -import tornado - -from .server import APIHandler - -LISTINGS_URL_SUFFIX = "@jupyterlab/extensionmanager-extension/listings.json" - - -def fetch_listings(logger): - """Fetch the listings for the extension manager.""" - if not logger: - from traitlets import log - - logger = log.get_logger() - if len(ListingsHandler.blocked_extensions_uris) > 0: - blocked_extensions = [] - for blocked_extensions_uri in ListingsHandler.blocked_extensions_uris: - logger.info( - "Fetching blocked_extensions from {}".format( - ListingsHandler.blocked_extensions_uris - ) - ) - r = requests.request( - "GET", blocked_extensions_uri, **ListingsHandler.listings_request_opts - ) - j = json.loads(r.text) - for b in j["blocked_extensions"]: - blocked_extensions.append(b) - ListingsHandler.blocked_extensions = blocked_extensions - if len(ListingsHandler.allowed_extensions_uris) > 0: - allowed_extensions = [] - for allowed_extensions_uri in ListingsHandler.allowed_extensions_uris: - logger.info( - "Fetching allowed_extensions from {}".format( - ListingsHandler.allowed_extensions_uris - ) - ) - r = requests.request( - "GET", allowed_extensions_uri, **ListingsHandler.listings_request_opts - ) - j = json.loads(r.text) - for w in j["allowed_extensions"]: - allowed_extensions.append(w) - ListingsHandler.allowed_extensions = allowed_extensions - ListingsHandler.listings = json.dumps( - { - "blocked_extensions_uris": list(ListingsHandler.blocked_extensions_uris), - "allowed_extensions_uris": list(ListingsHandler.allowed_extensions_uris), - "blocked_extensions": ListingsHandler.blocked_extensions, - "allowed_extensions": ListingsHandler.allowed_extensions, - } - ) - - -class ListingsHandler(APIHandler): - """An handler that returns the listings specs.""" - - """Below fields are class level fields that are accessed and populated - by the initialization and the fetch_listings methods. - Some fields are initialized before the handler creation in the - handlers.py#add_handlers method. - Having those fields predefined reduces the guards in the methods using - them. - """ - # The list of blocked_extensions URIS. - blocked_extensions_uris = set() - # The list of allowed_extensions URIS. - allowed_extensions_uris = set() - # The blocked extensions extensions. - blocked_extensions = [] - # The allowed extensions extensions. - allowed_extensions = [] - # The provider request options to be used for the request library. - listings_request_opts = {} - # The PeriodicCallback that schedule the call to fetch_listings method. - pc = None - - def get(self, path): - """Get the listings for the extension manager.""" - self.set_header("Content-Type", "application/json") - if path == LISTINGS_URL_SUFFIX: - self.write(ListingsHandler.listings) - else: - raise tornado.web.HTTPError(400) diff --git a/jupyterlab_server/rest-api.yml b/jupyterlab_server/rest-api.yml index 4fc0ffe8..5b817736 100644 --- a/jupyterlab_server/rest-api.yml +++ b/jupyterlab_server/rest-api.yml @@ -8,39 +8,6 @@ info: name: BSD-3-Clause paths: - /lab/api/listings/%40jupyterlab/extensionmanager-extension/listings.json: - get: - summary: Get Extension Listings Specs - description: | - Gets the list of extension metadata for the application - responses: - "200": - description: The Extension Listing specs - content: - application/json: - schema: - properties: - blocked_extension_uris: - type: array - description: list of blocked extension uris - items: - type: string - allowed_extension_uris: - type: array - description: list of allowed extension uris - items: - type: string - blocked_extensions: - type: array - description: list of blocked extensions - items: - $ref: "#/components/schemas/ListEntry" - allowed_extensions: - type: array - description: list of blocked extensions - items: - $ref: "#/components/schemas/ListEntry" - /lab/api/settings/: get: summary: Get Settings List From 898c446d0f41bd4b84f9b082eb331eeef7ca3cde Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 27 Jul 2022 15:38:30 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- jupyterlab_server/app.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/jupyterlab_server/app.py b/jupyterlab_server/app.py index ac7ec427..00547981 100644 --- a/jupyterlab_server/app.py +++ b/jupyterlab_server/app.py @@ -27,9 +27,7 @@ def app_namespace(self): # Should your extension expose other server extensions when launched directly? load_other_extensions = True - app_version = Unicode("", help="The version of the application.").tag( - default=__version__ - ) + app_version = Unicode("", help="The version of the application.").tag(default=__version__) blocked_extensions_uris = Unicode( "", From 61e775b85be9e457ba7c785db1fd62cfb59e30f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Collonval?= Date: Thu, 28 Jul 2022 10:09:01 +0200 Subject: [PATCH 3/6] Restore _deprecated_aliases attribute --- jupyterlab_server/app.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/jupyterlab_server/app.py b/jupyterlab_server/app.py index 00547981..99f4be97 100644 --- a/jupyterlab_server/app.py +++ b/jupyterlab_server/app.py @@ -55,7 +55,9 @@ def app_namespace(self): help="The optional kwargs to use for the listings HTTP requests \ as described on https://www.tornadoweb.org/en/stable/httpclient.html#tornado.httpclient.HTTPRequest", ) - + + _deprecated_aliases = {} + # Method copied from # https://github.com/jupyterhub/jupyterhub/blob/d1a85e53dccfc7b1dd81b0c1985d158cc6b61820/jupyterhub/auth.py#L143-L161 @observe(*list(_deprecated_aliases)) From 369e2b32b2577be882e938d3dc1b80f16aa92b64 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 28 Jul 2022 08:12:51 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- jupyterlab_server/app.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jupyterlab_server/app.py b/jupyterlab_server/app.py index 99f4be97..fd344900 100644 --- a/jupyterlab_server/app.py +++ b/jupyterlab_server/app.py @@ -55,9 +55,9 @@ def app_namespace(self): help="The optional kwargs to use for the listings HTTP requests \ as described on https://www.tornadoweb.org/en/stable/httpclient.html#tornado.httpclient.HTTPRequest", ) - + _deprecated_aliases = {} - + # Method copied from # https://github.com/jupyterhub/jupyterhub/blob/d1a85e53dccfc7b1dd81b0c1985d158cc6b61820/jupyterhub/auth.py#L143-L161 @observe(*list(_deprecated_aliases)) From 944980252f1f918173c184ed6b07ac63c84c6dae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Collonval?= Date: Thu, 28 Jul 2022 10:54:17 +0200 Subject: [PATCH 5/6] Clear observer linked to deprecated aliases --- jupyterlab_server/app.py | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/jupyterlab_server/app.py b/jupyterlab_server/app.py index fd344900..bd4ce545 100644 --- a/jupyterlab_server/app.py +++ b/jupyterlab_server/app.py @@ -56,30 +56,6 @@ def app_namespace(self): as described on https://www.tornadoweb.org/en/stable/httpclient.html#tornado.httpclient.HTTPRequest", ) - _deprecated_aliases = {} - - # Method copied from - # https://github.com/jupyterhub/jupyterhub/blob/d1a85e53dccfc7b1dd81b0c1985d158cc6b61820/jupyterhub/auth.py#L143-L161 - @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 JupyterLab {version}, use {cls}.{new} instead".format( - cls=self.__class__.__name__, - old=old_attr, - new=new_attr, - version=version, - ) - ) - setattr(self, new_attr, change.new) - def initialize_templates(self): self.static_paths = [self.static_dir] self.template_paths = [self.templates_dir] From 6b5e050155b97940ea96de93309c261fe42ebfb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Collonval?= Date: Thu, 28 Jul 2022 14:58:52 +0200 Subject: [PATCH 6/6] Remove tests --- tests/test_listings_api.py | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 tests/test_listings_api.py diff --git a/tests/test_listings_api.py b/tests/test_listings_api.py deleted file mode 100644 index 8224759a..00000000 --- a/tests/test_listings_api.py +++ /dev/null @@ -1,7 +0,0 @@ -from jupyterlab_server.test_utils import validate_request - - -async def test_get_listing(jp_fetch, labserverapp): - url = r"lab/api/listings/@jupyterlab/extensionmanager-extension/listings.json" - r = await jp_fetch(*url.split("/")) - validate_request(r)