From 7e85b32fc7262ac1d785aa8c9e542c29a7b33330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Mon, 4 Jul 2016 12:36:49 +0200 Subject: [PATCH] Gracefully handle missing node types when validating custom fields. --- pillar/application/__init__.py | 16 +++++++++++++++- pillar/application/utils/__init__.py | 4 ++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pillar/application/__init__.py b/pillar/application/__init__.py index 24d8f4df..ce5cb390 100644 --- a/pillar/application/__init__.py +++ b/pillar/application/__init__.py @@ -53,8 +53,22 @@ class ValidateCustomFields(Validator): def _validate_valid_properties(self, valid_properties, field, value): projects_collection = app.data.driver.db['projects'] lookup = {'_id': ObjectId(self.document['project'])} + project = projects_collection.find_one(lookup) - node_type = project_get_node_type(project, self.document['node_type']) + if project is None: + log.warning('Unknown project %s, declared by node %s', + project, self.document.get('_id')) + self._error(field, 'Unknown project') + return False + + node_type_name = self.document['node_type'] + node_type = project_get_node_type(project, node_type_name) + if node_type is None: + log.warning('Project %s has no node type %s, declared by node %s', + project, node_type_name, self.document.get('_id')) + self._error(field, 'Unknown node type') + return False + try: value = self.convert_properties(value, node_type['dyn_schema']) except Exception as e: diff --git a/pillar/application/utils/__init__.py b/pillar/application/utils/__init__.py index ff8ae4cc..42ef8423 100644 --- a/pillar/application/utils/__init__.py +++ b/pillar/application/utils/__init__.py @@ -74,5 +74,9 @@ def project_get_node_type(project_document, node_type_node_name): """Return a node_type subdocument for a project. If none is found, return None. """ + + if project_document is None: + return None + return next((node_type for node_type in project_document['node_types'] if node_type['name'] == node_type_node_name), None)