From a5873de0baf5a197b724b22409b9bc61900a02e5 Mon Sep 17 00:00:00 2001 From: sslypushenko Date: Fri, 16 Sep 2016 19:06:51 +0300 Subject: [PATCH] Add validation to package import Patch added validation to both CLI and UI. Validation triggers only manifest checks in order to be executed quickly. To run full set of checks CLI tool murano-ppkg-check should used. Depends-On: Iae6b56b2f7d32c75515ee92f0e33f9e3af9924e9 Change-Id: I94e395c09483dc2b5061df75c952bc87f60327aa --- muranoclient/common/utils.py | 19 ++++++++++++++++++- muranoclient/tests/unit/test_utils.py | 7 +++++-- muranoclient/v1/packages.py | 6 ++++++ requirements.txt | 1 + 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/muranoclient/common/utils.py b/muranoclient/common/utils.py index 5034befa..08ce62de 100644 --- a/muranoclient/common/utils.py +++ b/muranoclient/common/utils.py @@ -17,6 +17,9 @@ from __future__ import print_function import collections import json +from muranopkgcheck import manager as check_manager +from muranopkgcheck import pkg_loader as check_pkg_loader +from muranopkgcheck import validators as check_validators import os import re import shutil @@ -307,7 +310,11 @@ class Package(FileWrapperMixin): def from_file(file_obj): if not isinstance(file_obj, File): file_obj = File(file_obj) - return Package(file_obj) + pkg = Package(file_obj) + errs = pkg.validate() + if errs: + raise exceptions.HTTPBadRequest(details=errs) + return pkg @staticmethod def fromFile(file_obj): @@ -342,6 +349,16 @@ class Package(FileWrapperMixin): extension='.zip') ) + def validate(self): + m = check_manager.Manager(self._file, + loader=check_pkg_loader.ZipLoader) + errors = m.validate( + validators=[check_validators.manifest.ManifestValidator], + only_errors=True) + if errors: + fmt = check_manager.PlainTextFormatter().format + return 'Invalid Murano package\n{}\n'.format(fmt(errors)) + @property def contents(self): """Contents of a package.""" diff --git a/muranoclient/tests/unit/test_utils.py b/muranoclient/tests/unit/test_utils.py index 9ccfc901..7b837239 100644 --- a/muranoclient/tests/unit/test_utils.py +++ b/muranoclient/tests/unit/test_utils.py @@ -70,13 +70,14 @@ def make_pkg(manifest_override, image_dicts=None): 'Classes': {'foo': 'foo.yaml'}, 'Description': '', 'Format': 1.0, - 'FullName': '', + 'FullName': 'org.foo', 'Name': 'Apache HTTP Server', 'Type': 'Application'} manifest.update(manifest_override) file_obj = six.BytesIO() zfile = zipfile.ZipFile(file_obj, "a") zfile.writestr('manifest.yaml', yaml.dump(manifest)) + zfile.writestr('Classes/foo.yaml', yaml.dump({})) if image_dicts: images_list = [] default_image_spec = { @@ -414,10 +415,12 @@ class PackageTest(testtools.TestCase): self.assertRaises(ValueError, utils.Package.from_location, name='foo.bar.baz', base_url='') - def test_file_object_repo(self): + @mock.patch.object(utils.Package, 'validate') + def test_file_object_repo(self, m_validate): resp = requests.Response() resp.raw = six.BytesIO(six.b("123")) resp.status_code = 200 + m_validate.return_value = None with mock.patch( 'requests.get', mock.Mock(side_effect=lambda k, *args, **kwargs: resp)): diff --git a/muranoclient/v1/packages.py b/muranoclient/v1/packages.py index d7e0891c..d3a53d8f 100644 --- a/muranoclient/v1/packages.py +++ b/muranoclient/v1/packages.py @@ -19,6 +19,8 @@ import yaml from muranoclient.common import base from muranoclient.common import exceptions +from muranoclient.common import utils + DEFAULT_PAGE_SIZE = 20 @@ -51,6 +53,10 @@ class PackageManager(base.Manager): response_key='categories', obj_class=Category) def create(self, data, files): + for pkg_file in files.values(): + utils.Package.from_file(pkg_file) + pkg_file.seek(0) + response = self.api.request( '/v1/catalog/packages', 'POST', diff --git a/requirements.txt b/requirements.txt index 809dc63e..5259809c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,6 +13,7 @@ requests>=2.10.0 # Apache-2.0 PyYAML>=3.10.0 # MIT yaql>=1.1.0 # Apache 2.0 License osc-lib>=1.2.0 # Apache-2.0 +murano-pkg-check>=0.2.0 # Apache-2.0 oslo.serialization>=1.10.0 # Apache-2.0 oslo.utils>=3.17.0 # Apache-2.0