From b823bc37d2f391968b60cf214623e6c1ef7c0fd6 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Wed, 19 Feb 2014 19:10:58 -0500 Subject: [PATCH] Fixup unknown attribute handling. For one, prevent unknown attributes from getting into configmanager from now on. Additionally, have the attributes plugin manage to convey bad attributes when encountered. --- confluent/config/configmanager.py | 12 ++++++++---- confluent/messages.py | 9 ++++++--- plugins/configuration/attributes.py | 17 +++++++++++------ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/confluent/config/configmanager.py b/confluent/config/configmanager.py index e1b3ff6e..26c8e9af 100644 --- a/confluent/config/configmanager.py +++ b/confluent/config/configmanager.py @@ -54,7 +54,7 @@ from Crypto.Hash import SHA256 import array import ast import collections -import confluent.config.attributes as attributes +import confluent.config.attributes as allattributes import confluent.util import copy import cPickle @@ -545,6 +545,10 @@ class ConfigManager(object): self._cfgstore['groups'][group] = {'nodes': set([])} cfgobj = self._cfgstore['groups'][group] for attr in attribmap[group].iterkeys(): + if (attr not in allattributes.node or + ('type' in allattributes.node[attr] and + not isinstance(attribmap[node][attr],allattributes.node[attr]['type']))): + raise ValueError newdict = {} if attr == 'nodes': if not isinstance(attribmap[group][attr], list): @@ -610,9 +614,9 @@ class ConfigManager(object): cfgobj = self._cfgstore['nodes'][node] recalcexpressions = False for attrname in attribmap[node].iterkeys(): - if (attrname not in attributes.node or - ('type' in attributes.node[attrname] and - not isinstance(attribmap[node][attrname],attributes.node[attrname]['type']))): + if (attrname not in allattributes.node or + ('type' in allattributes.node[attrname] and + not isinstance(attribmap[node][attrname],allattributes.node[attrname]['type']))): raise ValueError newdict = {} if (isinstance(attribmap[node][attrname], str)): diff --git a/confluent/messages.py b/confluent/messages.py index 7d502786..957cb9b5 100644 --- a/confluent/messages.py +++ b/confluent/messages.py @@ -35,11 +35,11 @@ class ConfluentMessage(object): val = self.kvpairs[key] value = self.defaultvalue type = self.defaulttype - if 'value' in val: + if val is not None and 'value' in val: value = val['value'] if value is None: value = '' - if value == '' and 'isset' in val and val['isset'] is True: + if val is not None and value == '' and 'isset' in val and val['isset'] is True: # an encrypted value, put some *** to show it is set # in the explorer value = '********' @@ -271,7 +271,10 @@ class Attributes(ConfluentMessage): self.desc = desc nkv = {} for key in kv.iterkeys(): - nkv[key] = {'value': kv[key]} + if type(kv[key]) == str: + nkv[key] = {'value': kv[key]} + else: + nkv[key] = kv[key] if node is None: self.kvpairs = nkv else: diff --git a/plugins/configuration/attributes.py b/plugins/configuration/attributes.py index 50cde64e..cc5e37ae 100644 --- a/plugins/configuration/attributes.py +++ b/plugins/configuration/attributes.py @@ -41,7 +41,10 @@ def retrieve_nodegroup(nodegroup, element, configmanager, inputdata): if attribute == 'nodes': desc = 'The nodes belonging to this group' else: - desc = allattributes.node[attribute]['description'] + try: + desc = allattributes.node[attribute]['description'] + except KeyError: + desc = 'Unknown' if 'value' in currattr: yield msg.Attributes( kv={attribute: currattr['value']}, @@ -89,18 +92,20 @@ def retrieve_nodes(nodes, element, configmanager, inputdata): for node in attributes.iterkeys(): for attribute in sorted(attributes[node].iterkeys()): currattr = attributes[node][attribute] + try: + desc = allattributes.node[attribute]['description'] + except KeyError: + desc = 'Unknown' if 'value' in currattr: yield msg.Attributes(node, {attribute: currattr['value']}, - allattributes.node[attribute]['description']) + desc) elif 'cryptvalue' in currattr: yield msg.CryptedAttributes(node, - {attribute: currattr}, - allattributes.node[attribute]['description']) + {attribute: currattr}, desc) elif isinstance(currattr, list): yield msg.ListAttributes(node, - {attribute: currattr}, - allattributes.node[attribute]['description']) + {attribute: currattr}, desc) else: raise Exception("BUGGY ATTRIBUTE FOR NODE")