From 646355245aeaaf259f91d1ee3ffedeccba7851b3 Mon Sep 17 00:00:00 2001 From: Camila Diaz Date: Wed, 25 Aug 2021 22:36:42 -0500 Subject: [PATCH] workspaces: add validation function for the workflows Goes on top of reanahub/reana-client#545 PR set, and reanahub/reana#532 PR set Closes reanahub/reana-client#546 --- reana_commons/config.py | 2 ++ reana_commons/workspaces.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 reana_commons/workspaces.py diff --git a/reana_commons/config.py b/reana_commons/config.py index da1a30e2..ab3f21e9 100644 --- a/reana_commons/config.py +++ b/reana_commons/config.py @@ -339,6 +339,8 @@ def kubernetes_node_label_to_dict(node_label): DEFAULT_WORKSPACE_PATH = os.getenv("DEFAULT_WORKSPACE_PATH", "/var/reana") """Default workspace path defined by the admin.""" +ADMIN_ALLOWED_WORKSPACES = os.getenv("ADMIN_ALLOWED_WORKSPACES", None) + K8S_CERN_EOS_MOUNT_CONFIGURATION = { "volume": {"name": "eos", "hostPath": {"path": "/var/eos"}}, "volumeMounts": { diff --git a/reana_commons/workspaces.py b/reana_commons/workspaces.py new file mode 100644 index 00000000..a03bf1e7 --- /dev/null +++ b/reana_commons/workspaces.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# +# This file is part of REANA. +# Copyright (C) 2020 CERN. +# +# REANA is free software; you can redistribute it and/or modify it +# under the terms of the MIT License; see LICENSE file for more details. +"""REANA-Commons workspaces util.""" + +from reana_commons.errors import REANAValidationError +from reana_commons.config import ADMIN_ALLOWED_WORKSPACES, SHARED_VOLUME_PATH + + +def validate_workspace(workspace_option): + """Validate and return workspace. + + :param workspace_option: A string of the workspace. + :returns: A string of the validated workspace. + """ + if ADMIN_ALLOWED_WORKSPACES: + allowed_workspaces = ADMIN_ALLOWED_WORKSPACES.split(",") + allowed_workspaces.append(SHARED_VOLUME_PATH) + else: + allowed_workspaces = [SHARED_VOLUME_PATH] + + if workspace_option: + if workspace_option not in allowed_workspaces: + raise REANAValidationError( + '==> ERROR: Workspace "{0}" not valid.'.format(workspace_option) + ) + return workspace_option