class Validator(object): def __init__(self, kerberosPath, confPath=None, rulesPath=None, hfilePath=None): self.parser = ConfParser(kerberosPath) if confPath is not None: content = self._readConfigFile(confPath) rulesPath = content['RulesPath'] hfilePath = content['HfilePath'] if rulesPath is not None and hfilePath is not None: self.rules = self._loadRules(rulesPath) self.validKeys = SupportedKeys(hfilePath).validKeys.union( self.rules['Attributes']) else: raise ValueError( 'Invalid arguments for validator: no path to rules and definition files' ) self._attribute_pattern = re.compile(r'^\w+$') self._lowercase_pattern = re.compile(r'[a-z]') def _readConfigFile(self, path): f = open(path) result = dict() for line in f: line = line.rstrip() fields = line.split('=') result[fields[0]] = fields[1] return result def _loadRules(self, path): f = open(path) rules = yaml.load(f) f.close() return rules def validate(self): typeInfo = self.rules['Types'] for node in self.parser.walk(): self._validateTypes(node, typeInfo) self._validateAttrubutes(node, self.validKeys) # self._validateRealm(node) def _validateTypes(self, node, typeInfo): (path, dirs, avs) = node for (key, value) in avs: valid_type_pattern = typeInfo.get(key) if valid_type_pattern is not None: for t in value: if re.match(valid_type_pattern, t) is None: print 'Wrong type %s for attribute %s.%s' % (t, path, key) def _validateAttrubutes(self, node, validKeys): (path, dirs, avs) = node attributes = list() for attr in dirs: if self._attribute_pattern.match(attr) is not None: attributes.append(attr) for (attr, value) in avs: if self._attribute_pattern.match(attr) is not None: attributes.append(attr) for attr in attributes: if attr not in validKeys: print 'Unrecognized attribute %s at %s' % (attr, path)
class Validator(object): def __init__(self, kerberosPath, confPath=None, rulesPath=None, hfilePath=None): self.parser = ConfParser(kerberosPath) if confPath is not None: content = self._readConfigFile(confPath) rulesPath = content['RulesPath'] hfilePath = content['HfilePath'] if rulesPath is not None and hfilePath is not None: self.rules = self._loadRules(rulesPath) self.validKeys = SupportedKeys(hfilePath).validKeys.union(self.rules['Attributes']) else: raise ValueError('Invalid arguments for validator: no path to rules and definition files') self._attribute_pattern = re.compile(r'^\w+$') self._lowercase_pattern = re.compile(r'[a-z]') def _readConfigFile(self,path): f = open(path) result = dict() for line in f: line = line.rstrip() fields = line.split('=') result[fields[0]] = fields[1] return result def _loadRules(self, path): f = open(path) rules = yaml.load(f) f.close() return rules def validate(self): typeInfo = self.rules['Types'] for node in self.parser.walk(): self._validateTypes(node, typeInfo) self._validateAttrubutes(node, self.validKeys) # self._validateRealm(node) def _validateTypes(self, node, typeInfo): (path, dirs, avs) = node for (key, value) in avs: valid_type_pattern = typeInfo.get(key) if valid_type_pattern is not None: for t in value: if re.match(valid_type_pattern, t) is None: print 'Wrong type %s for attribute %s.%s' % (t,path,key) def _validateAttrubutes(self, node, validKeys): (path, dirs, avs) = node attributes = list() for attr in dirs: if self._attribute_pattern.match(attr) is not None: attributes.append(attr) for (attr, value) in avs: if self._attribute_pattern.match(attr) is not None: attributes.append(attr) for attr in attributes: if attr not in validKeys: print 'Unrecognized attribute %s at %s' % (attr, path)