From 8e56c1e4fb4d7d2e6bae2f12f64aed86617aed68 Mon Sep 17 00:00:00 2001 From: Pavlos Ratis Date: Sat, 24 May 2014 20:56:39 +0300 Subject: [PATCH] refine seed handling --- gkeys/actions.py | 12 ++-- gkeys/seedhandler.py | 153 ++++++++++++------------------------------- 2 files changed, 48 insertions(+), 117 deletions(-) diff --git a/gkeys/actions.py b/gkeys/actions.py index 79690fc..79c42ee 100644 --- a/gkeys/actions.py +++ b/gkeys/actions.py @@ -69,11 +69,11 @@ class Actions(object): def addseed(self, args): '''Action addseed method''' handler = SeedHandler(self.logger) - gkey = handler.new(args) + gkey = handler.new(args, checkgkey=True) gkeys = self.listseed(args) if len(gkeys) == 0: self.logger.debug("ACTIONS: addkey; now adding gkey: %s" % str(gkey)) - success = self.seeds.add(gkey) + success = self.seeds.add(getattr(gkey, 'nick')[0], gkey) if success: success = self.seeds.save() return ["Successfully added new seed: %s" % str(success), gkey] @@ -87,19 +87,19 @@ class Actions(object): def removeseed(self, args): '''Action removeseed method''' handler = SeedHandler(self.logger) - searchkey = handler.new(args, needkeyid=False, checkintegrity=False) + searchkey = handler.new(args) self.logger.debug("ACTIONS: removeseed; gkey: %s" % str(searchkey)) gkeys = self.listseed(args) if not gkeys: return ["Failed to remove seed: No gkeys returned from listseed()", None] if len(gkeys) == 1: - self.logger.debug("ACTIONS: removeseed; now deleting gkey: %s" % str(gkeys[0])) - success = self.seeds.delete(gkeys[0]) + self.logger.debug("ACTIONS: removeseed; now deleting gkey: %s" % str(gkeys)) + success = self.seeds.delete(gkeys) if success: success = self.seeds.save() return ["Successfully removed seed: %s" % str(success), - gkeys[0]] + gkeys] elif len(gkeys): messages = ["Too many seeds found to remove"] messages.extend(gkeys) diff --git a/gkeys/seedhandler.py b/gkeys/seedhandler.py index be68990..8aac6a8 100644 --- a/gkeys/seedhandler.py +++ b/gkeys/seedhandler.py @@ -12,29 +12,29 @@ import re -from gkeys.config import (GKEY, NICK, NAME, KEYID, LONGKEYID, FINGERPRINT, - KEYLEN_MAP) +from gkeys.config import GKEY class SeedHandler(object): - def __init__(self,logger): self.logger = logger self.fingerprint_re = re.compile('[0-9A-Fa-f]{40}') self.finerprint_re2 = re.compile('[0-9A-Fa-f]{4}( [0-9A-Fa-f]{4}){9}') - def new(self, args, needkeyid=True, checkintegrity=True): - parts = self.build_gkeylist(args, needkeyid, checkintegrity) - if parts: - gkey = GKEY._make(parts) - self.logger.debug("SeedHandler: new() new gkey: %s" % str(gkey)) + def new(self, args, checkgkey=False): + newgkey = self.build_gkeydict(args) + if checkgkey: + newgkey = self.check_gkey(newgkey) + if newgkey: + newgkey = GKEY(**newgkey) + self.logger.debug("SeedHandler: new() new gkey: %s" % str(newgkey)) else: self.logger.debug("SeedHandler: new() FAILED to et parts from: %s" % str(args)) return None - return gkey + return newgkey @staticmethod @@ -49,109 +49,40 @@ class SeedHandler(object): pass return keyinfo - - def build_gkeylist(self, args, needkeyid=True, checkintegrity=True): - keyinfo = [] - keyid_found = False - # assume it's good until an error is found - is_good = True - #self.logger.debug("SeedHandler: build_gkeylist; args = %s" % str(args)) - for attr in GKEY._fields: - if GKEY.field_types[attr] is str: - try: - value = getattr(args, attr) - except AttributeError: - value = None - elif GKEY.field_types[attr] is list: - try: - values = [y for y in getattr(args, attr).split(':')] - value = [v.replace(' ', '') for v in values] - except AttributeError: - value = None - keyinfo.append(value) - if attr in ["keyid", "longkeyid"] and value: - keyid_found = True - if not keyid_found and needkeyid: - fingerprint = keyinfo[FINGERPRINT] - if fingerprint: - self.logger.debug(' Generate gpgkey longkeyid, Found ' - 'fingerprint in args') - # assign it to gpgkey to prevent a possible - # "gpgkey" undefined error - gpgkey = ['0x' + x[-KEYLEN_MAP['longkeyid']:] for x in fingerprint] - keyinfo[LONGKEYID] = gpgkey - self.logger.debug(' Generate gpgkey longkeyid, NEW ' - 'keyinfo[LONGKEYID] = %s' % str(keyinfo[LONGKEYID])) - else: - gpgkey = 'Missing or Bad fingerprint from command line args' + def check_gkey(self, args): + # assume it's good until an error is found + is_good = True + try: + if args['fingerprint']: + # create a longkeyid based on fingerprint + is_ok = self._check_fingerprint_integrity(args) + args['keydir'] = args.get('keydir', args['nick']) + if not is_ok: is_good = False - if not keyinfo[LONGKEYID]: - self.logger.error('ERROR in seed creation info for: %s, %s' - %(keyinfo[NICK], keyinfo[NAME])) - self.logger.error(' A valid keyid, longkeyid or fingerprint ' - 'was not found for %s : gpgkey = %s' - %(keyinfo[NAME], gpgkey)) - is_good = False - if is_good: - if keyinfo[FINGERPRINT]: # fingerprints exist check - is_ok = self._check_fingerprint_integrity(keyinfo) - is_match = self._check_id_fingerprint_match(keyinfo) - if not is_ok or not is_match: - is_good = False - if is_good: - return keyinfo - return None - - - def _check_id_fingerprint_match(self, keyinfo): - # assume it's good until found an error is found + self.logger.error('Bad fingerprint from command line args.') + except KeyError: + self.logger.error('GPG fingerprint not found.') + is_good = False + # need to add values to a list + for key,value in args.items(): + args[key] = value.split() + if is_good: + return args + else: + self.logger.error('A valid fingerprint ' + 'was not found for %s' % args['name']) + return args + + def _check_fingerprint_integrity(self, gkey): + # assume it's good unti an error is found is_good = True - for x in [KEYID, LONGKEYID]: - # skip blank id field - if not keyinfo[x]: - continue - for y in keyinfo[x]: - index = len(y.lstrip('0x')) - if y.lstrip('0x').upper() not in \ - [x[-index:].upper() for x in keyinfo[FINGERPRINT]]: - self.logger.error('ERROR in keyinfo for: %s, %s' - %(keyinfo[NICK], keyinfo[NAME])) - self.logger.error(' ' + str(keyinfo)) - self.logger.error(' GPGKey id %s not found in the ' - % y.lstrip('0x') + 'listed fingerprint(s)') - is_good = False - ids = 0 - for x in [KEYID, LONGKEYID]: - if keyinfo[x]: - ids = ids + len(keyinfo[x]) - if ids != len(keyinfo[FINGERPRINT]): - self.logger.error('ERROR in keyinfo for: %s, %s' - %(keyinfo[NICK], keyinfo[NAME])) - self.logger.error(' ' + str(keyinfo)) - self.logger.error(' GPGKey the number of ids %d DO NOT match ' - 'the number of listed fingerprint(s), {%s,%s}, %s' - % (ids, keyinfo[KEYID], keyinfo[LONGKEYID], keyinfo[FINGERPRINT])) - is_good = False - - return is_good - - - def _check_fingerprint_integrity(self, keyinfo): - # assume it's good until an error is found - is_good = True - for fingerprint in keyinfo[FINGERPRINT]: - # check fingerprint integrity - if len(fingerprint) != 40: - self.logger.error('ERROR in keyinfo for: %s, %s' - %(keyinfo[NICK], keyinfo[NAME])) - self.logger.error(' GPGKey incorrect fingerprint ' + + fingerprint = gkey['fingerprint'] + # check fingerprint integrity + if len(fingerprint) != 40: + self.logger.error(' GPGKey incorrect fingerprint ' + 'length (%s) for fingerprint: %s' %(len(fingerprint), fingerprint)) - is_good = False - continue - if not self.fingerprint_re.match(fingerprint): - self.logger.error('ERROR in keyinfo info for: %s, %s' - %(keyinfo[NICK], keyinfo[NAME])) - self.logger.error(' GPGKey: Non hexadecimal digits in ' + - 'fingerprint for fingerprint: ' + fingerprint) - is_good = False + is_good = False + if not self.fingerprint_re.match(fingerprint): + self.logger.error(' GPGKey: Non hexadecimal digits in ' + 'fingerprint for fingerprint: ' + fingerprint) + is_good = False return is_good -- 2.26.2