File 0001-Allow-enforcing-usage-of-raw-definitions.patch of Package python-mistralclient.19243

From ab54cb9ae576c2b29c7cd9a9628f3908aaa3e0ee Mon Sep 17 00:00:00 2001
From: Takashi Kajinami <tkajinam@redhat.com>
Date: Thu, 15 Jul 2021 23:06:40 +0900
Subject: [PATCH] Allow enforcing usage of "raw" definitions

Since the chage 72a6bc1ad1461e20b8ccc7ef41898dd13067c6dc was merged,
mistralclient tries to determine if the value passed in is a definition
file path name or file URI, or the actual definition.
However this behavior causes problem when the client is used in server
side and results in allowing access to server files or any contents
in internal servers.

This change introduces the new enforce_raw_definitions variable to
disable that fallback behavior and ensure that client treats input
as raw contents.

Related-Bug: #1931558
Change-Id: I47931bdf8bbccb940d4c98c47f16a6eef27c026a
---
 mistralclient/api/base.py                     | 11 +++++-
 mistralclient/api/v2/actions.py               |  7 ++--
 mistralclient/api/v2/client.py                | 37 ++++++++++---------
 mistralclient/api/v2/code_sources.py          |  5 +--
 mistralclient/api/v2/environments.py          |  4 +-
 mistralclient/api/v2/workbooks.py             |  7 ++--
 mistralclient/api/v2/workflows.py             |  7 ++--
 ...orce_raw_definitions-94433339a63d15f8.yaml |  7 ++++
 8 files changed, 49 insertions(+), 36 deletions(-)
 create mode 100644 releasenotes/notes/enforce_raw_definitions-94433339a63d15f8.yaml

Index: python-mistralclient-4.0.1/mistralclient/api/base.py
===================================================================
--- python-mistralclient-4.0.1.orig/mistralclient/api/base.py
+++ python-mistralclient-4.0.1/mistralclient/api/base.py
@@ -18,6 +18,7 @@ import six
 from oslo_serialization import jsonutils
 
 from keystoneauth1 import exceptions
+from mistralclient import utils
 
 urlparse = six.moves.urllib.parse
 
@@ -71,8 +72,15 @@ def extract_json(response, response_key)
 class ResourceManager(object):
     resource_class = None
 
-    def __init__(self, http_client):
+    def __init__(self, http_client, enforce_raw_definitions=False):
         self.http_client = http_client
+        self.enforce_raw_definitions = enforce_raw_definitions
+
+    def get_contents_if_file(self, contents_or_file_name):
+        if self.enforce_raw_definitions:
+            return contents_or_file_name
+        else:
+            return utils.get_contents_if_file(contents_or_file_name)
 
     def find(self, **kwargs):
         return [i for i in self.list() if _check_items(i, kwargs.items())]
Index: python-mistralclient-4.0.1/mistralclient/api/v2/actions.py
===================================================================
--- python-mistralclient-4.0.1.orig/mistralclient/api/v2/actions.py
+++ python-mistralclient-4.0.1/mistralclient/api/v2/actions.py
@@ -14,7 +14,6 @@
 #    limitations under the License.
 
 from mistralclient.api import base
-from mistralclient import utils
 
 
 class Action(base.Resource):
@@ -29,7 +28,7 @@ class ActionManager(base.ResourceManager
 
         # If the specified definition is actually a file, read in the
         # definition file
-        definition = utils.get_contents_if_file(definition)
+        definition = self.get_contents_if_file(definition)
         url = '/actions?scope=%s' % scope
         if namespace:
             url += '&namespace=%s' % namespace
@@ -52,7 +51,7 @@ class ActionManager(base.ResourceManager
         url = ('/actions/%s' % id if id else '/actions') + params
         # If the specified definition is actually a file, read in the
         # definition file
-        definition = utils.get_contents_if_file(definition)
+        definition = self.get_contents_if_file(definition)
         return self._update(
             url,
             definition,
@@ -96,7 +95,7 @@ class ActionManager(base.ResourceManager
 
         # If the specified definition is actually a file, read in the
         # definition file
-        definition = utils.get_contents_if_file(definition)
+        definition = self.get_contents_if_file(definition)
 
         return self._validate(
             '/actions/validate',
Index: python-mistralclient-4.0.1/mistralclient/api/v2/client.py
===================================================================
--- python-mistralclient-4.0.1.orig/mistralclient/api/v2/client.py
+++ python-mistralclient-4.0.1/mistralclient/api/v2/client.py
@@ -40,10 +40,25 @@ _DEFAULT_MISTRAL_URL = "http://localhost
 
 class Client(object):
 
+    MANAGERS = {
+        'workbooks': workbooks.WorkbookManager,
+        'executions': executions.ExecutionManager,
+        'tasks': tasks.TaskManager,
+        'actions': actions.ActionManager,
+        'workflows': workflows.WorkflowManager,
+        'cron_triggers': cron_triggers.CronTriggerManager,
+        'event_triggers': event_triggers.EventTriggerManager,
+        'environments': environments.EnvironmentManager,
+        'action_executions': action_executions.ActionExecutionManager,
+        'services': services.ServiceManager,
+        'members': members.MemberManager,
+        }
+
     def __init__(self, auth_type='keystone', **kwargs):
         # We get the session at this point, as some instances of session
         # objects might have mutexes that can't be deep-copied.
         session = kwargs.pop('session', None)
+        enforce_raw_definitions = kwargs.pop('enforce_raw_definitions', False)
         req = copy.deepcopy(kwargs)
         mistral_url = req.get('mistral_url')
         profile = req.get('profile')
@@ -76,17 +91,5 @@ class Client(object):
 
         http_client = httpclient.HTTPClient(mistral_url, session=session,
                                             **req)
-
-        # Create all resource managers.
-        self.workbooks = workbooks.WorkbookManager(http_client)
-        self.executions = executions.ExecutionManager(http_client)
-        self.tasks = tasks.TaskManager(http_client)
-        self.actions = actions.ActionManager(http_client)
-        self.workflows = workflows.WorkflowManager(http_client)
-        self.cron_triggers = cron_triggers.CronTriggerManager(http_client)
-        self.event_triggers = event_triggers.EventTriggerManager(http_client)
-        self.environments = environments.EnvironmentManager(http_client)
-        self.action_executions = action_executions.ActionExecutionManager(
-            http_client)
-        self.services = services.ServiceManager(http_client)
-        self.members = members.MemberManager(http_client)
+        for name, cls in self.MANAGERS.items():
+            setattr(self, name, cls(http_client, enforce_raw_definitions))
Index: python-mistralclient-4.0.1/mistralclient/api/v2/environments.py
===================================================================
--- python-mistralclient-4.0.1.orig/mistralclient/api/v2/environments.py
+++ python-mistralclient-4.0.1/mistralclient/api/v2/environments.py
@@ -44,7 +44,7 @@ class EnvironmentManager(base.ResourceMa
         # read it's contents first.
         if 'file' in kwargs:
             file = kwargs['file']
-            kwargs = utils.load_content(utils.get_contents_if_file(file))
+            kwargs = utils.load_content(self.get_contents_if_file(file))
 
         self._ensure_not_empty(name=kwargs.get('name', None),
                                variables=kwargs.get('variables', None))
@@ -60,7 +60,7 @@ class EnvironmentManager(base.ResourceMa
         # read it's contents first.
         if 'file' in kwargs:
             file = kwargs['file']
-            kwargs = utils.load_content(utils.get_contents_if_file(file))
+            kwargs = utils.load_content(self.get_contents_if_file(file))
 
         name = kwargs.get('name', None)
         self._ensure_not_empty(name=name)
Index: python-mistralclient-4.0.1/mistralclient/api/v2/workbooks.py
===================================================================
--- python-mistralclient-4.0.1.orig/mistralclient/api/v2/workbooks.py
+++ python-mistralclient-4.0.1/mistralclient/api/v2/workbooks.py
@@ -14,7 +14,6 @@
 #    limitations under the License.
 
 from mistralclient.api import base
-from mistralclient import utils
 
 
 class Workbook(base.Resource):
@@ -44,7 +43,7 @@ class WorkbookManager(base.ResourceManag
 
         # If the specified definition is actually a file, read in the
         # definition file
-        definition = utils.get_contents_if_file(definition)
+        definition = self.get_contents_if_file(definition)
 
         return self._create(
             self._get_workbooks_url(None, namespace, scope),
@@ -58,7 +57,7 @@ class WorkbookManager(base.ResourceManag
 
         # If the specified definition is actually a file, read in the
         # definition file
-        definition = utils.get_contents_if_file(definition)
+        definition = self.get_contents_if_file(definition)
 
         return self._update(
             self._get_workbooks_url(None, namespace, scope),
@@ -98,7 +97,7 @@ class WorkbookManager(base.ResourceManag
 
         # If the specified definition is actually a file, read in the
         # definition file
-        definition = utils.get_contents_if_file(definition)
+        definition = self.get_contents_if_file(definition)
 
         return self._validate(
             '/workbooks/validate',
Index: python-mistralclient-4.0.1/mistralclient/api/v2/workflows.py
===================================================================
--- python-mistralclient-4.0.1.orig/mistralclient/api/v2/workflows.py
+++ python-mistralclient-4.0.1/mistralclient/api/v2/workflows.py
@@ -14,7 +14,6 @@
 #    limitations under the License.
 
 from mistralclient.api import base
-from mistralclient import utils
 
 
 class Workflow(base.Resource):
@@ -29,7 +28,7 @@ class WorkflowManager(base.ResourceManag
 
         # If the specified definition is actually a file, read in the
         # definition file
-        definition = utils.get_contents_if_file(definition)
+        definition = self.get_contents_if_file(definition)
 
         return self._create(
             '/workflows?scope=%s&namespace=%s' % (scope, namespace),
@@ -47,7 +46,7 @@ class WorkflowManager(base.ResourceManag
 
         # If the specified definition is actually a file, read in the
         # definition file
-        definition = utils.get_contents_if_file(definition)
+        definition = self.get_contents_if_file(definition)
 
         is_iter_resp = True
         response_key = 'workflows'
@@ -106,7 +105,7 @@ class WorkflowManager(base.ResourceManag
 
         # If the specified definition is actually a file, read in the
         # definition file
-        definition = utils.get_contents_if_file(definition)
+        definition = self.get_contents_if_file(definition)
 
         return self._validate(
             '/workflows/validate',
Index: python-mistralclient-4.0.1/releasenotes/notes/enforce_raw_definitions-94433339a63d15f8.yaml
===================================================================
--- /dev/null
+++ python-mistralclient-4.0.1/releasenotes/notes/enforce_raw_definitions-94433339a63d15f8.yaml
@@ -0,0 +1,7 @@
+---
+features:
+  - |
+    The new ``enforce_raw_definitions`` variable has been added to
+    the ``Client`` class and each ``ResourceManager`` class. This variable is
+    set to False by default. Setting this parameter to True enforces the client
+    to use ``definition`` passed in as raw content.
openSUSE Build Service is sponsored by