Skip to content

Commit

Permalink
Merge branch 'master' into dependabot-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ableuler committed Feb 11, 2021
2 parents d159e67 + a90ff26 commit 5f783c2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions jupyterhub/spawners.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ def get_pod_manifest(self):
RENKU_ANNOTATION_PREFIX + "username": safe_username,
RENKU_ANNOTATION_PREFIX + "commit-sha": options.get("commit_sha"),
RENKU_ANNOTATION_PREFIX + "projectName": options.get("project"),
"hub.jupyter.org/network-access-hub": "true"
}

self.delete_grace_period = 30
Expand Down
3 changes: 3 additions & 0 deletions renku_notebooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ def create_app():
def handle_error(err):
headers = err.data.get("headers", None)
messages = err.data.get("messages", {"error": "Invalid request."})
app.logger.warning(
f"Validation of request parameters failed with the messages: {messages}"
)
if headers:
return jsonify({"messages": messages}), err.code, headers
else:
Expand Down
1 change: 1 addition & 0 deletions renku_notebooks/api/custom_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ def _deserialize(
is not None,
required=True,
)
serverOptionUrlValue = fields.Str(required=True)
20 changes: 18 additions & 2 deletions renku_notebooks/api/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@
validates_schema,
ValidationError,
pre_load,
INCLUDE,
)
import collections

from .. import config
from .custom_fields import (
serverOptionCpuValue,
serverOptionMemoryValue,
serverOptionUrlValue,
)
from ..util.misc import read_server_options_file


class LaunchNotebookRequestServerOptions(Schema):
defaultUrl = fields.String(required=True)
defaultUrl = serverOptionUrlValue
cpu_request = serverOptionCpuValue
mem_request = serverOptionMemoryValue
lfs_auto_fetch = fields.Bool(required=True)
Expand All @@ -30,6 +32,8 @@ def validate_server_options(self, data, **kwargs):
for option in data.keys():
if option not in server_options.keys():
continue # presence of option keys are already handled by marshmallow
if option == "defaultUrl":
continue # the defaultUrl field should not be limited to only server options
if server_options[option]["type"] == "boolean":
continue # boolean options are already validated by marshmallow
if data[option] not in server_options[option]["options"]:
Expand Down Expand Up @@ -100,6 +104,9 @@ class UserPodAnnotations(
that are returned to the UI as part of any endpoint that list servers.
"""

class Meta:
unknown = INCLUDE

def get_attribute(self, obj, key, *args, **kwargs):
# in marshmallow, any schema key with a dot in it is converted to nested dictionaries
# in marshmallow, this overrides that behaviour for dumping (serializing)
Expand Down Expand Up @@ -214,6 +221,15 @@ class ServerOptionString(ServerOptionBase):
)


class ServerOptionUrl(ServerOptionBase):
"""The schema used to describe a single option for the server_options endpoint."""

default = serverOptionUrlValue
options = fields.List(
serverOptionUrlValue, validate=lambda x: len(x) >= 1, required=True
)


class ServerOptionBool(ServerOptionBase):
"""The schema used to describe a single option for the server_options endpoint."""

Expand All @@ -227,7 +243,7 @@ class ServerOptions(Schema):
"""

cpu_request = fields.Nested(ServerOptionCpu(), required=True)
defaultUrl = fields.Nested(ServerOptionString(), required=True)
defaultUrl = fields.Nested(ServerOptionUrl(), required=True)
gpu_request = fields.Nested(ServerOptionGpu())
lfs_auto_fetch = fields.Nested(ServerOptionBool(), required=True)
mem_request = fields.Nested(ServerOptionMemory(), required=True)
Expand Down
39 changes: 39 additions & 0 deletions tests/test_schema_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from marshmallow import ValidationError
import pytest

from renku_notebooks.api.schemas import UserPodAnnotations


RENKU_ANNOTATION_PREFIX = "renku.io/"
JUPYTERHUB_ANNOTATION_PREFIX = "hub.jupyter.org/"

passing_annotation_response = {
f"{RENKU_ANNOTATION_PREFIX}namespace": "smth",
f"{RENKU_ANNOTATION_PREFIX}projectId": "smth",
f"{RENKU_ANNOTATION_PREFIX}projectName": "smth",
f"{RENKU_ANNOTATION_PREFIX}branch": "smth",
f"{RENKU_ANNOTATION_PREFIX}commit-sha": "smth",
f"{RENKU_ANNOTATION_PREFIX}username": "smth",
f"{RENKU_ANNOTATION_PREFIX}default_image_used": "smth",
f"{RENKU_ANNOTATION_PREFIX}repository": "smth",
f"{RENKU_ANNOTATION_PREFIX}git-host": "smth",
f"{JUPYTERHUB_ANNOTATION_PREFIX}servername": "smth",
f"{JUPYTERHUB_ANNOTATION_PREFIX}username": "smth",
}


def test_unknown_annotations_allowed():
schema = UserPodAnnotations()
response = {
**passing_annotation_response,
"extra_annotation": "smth",
}
assert schema.load(response) == response


def test_missing_required_annotation_fails():
schema = UserPodAnnotations()
response = passing_annotation_response.copy()
response.pop(f"{RENKU_ANNOTATION_PREFIX}projectName")
with pytest.raises(ValidationError):
schema.load(response)

0 comments on commit 5f783c2

Please sign in to comment.