From 6eb06fe32725b8985c9971c70f00c2c69f1e371b Mon Sep 17 00:00:00 2001 From: Brian Dolbec Date: Sat, 16 Nov 2013 12:07:11 -0800 Subject: [PATCH] Initial creation of the SeedHandler class. Move the relavent code from gkeys/actions.py to the the new SeedHandler class. --- gkeys/actions.py | 53 ++++++----------- gkeys/seedhandler.py | 137 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+), 36 deletions(-) create mode 100644 gkeys/seedhandler.py diff --git a/gkeys/actions.py b/gkeys/actions.py index 2c1f76d..13031e5 100644 --- a/gkeys/actions.py +++ b/gkeys/actions.py @@ -13,7 +13,7 @@ from __future__ import print_function -from gkeys.config import GKEY +from gkeys.seedhandler import SeedHandler from gkeys.lib import GkeysGPG from gkeys.seed import Seeds @@ -32,30 +32,6 @@ class Actions(object): self.seeds = None - @staticmethod - def build_gkeydict(args): - keyinfo = {} - for x in GKEY._fields: - try: - value = getattr(args, x) - if value: - keyinfo[x] = value - except AttributeError: - pass - return keyinfo - - - @staticmethod - def build_gkeylist(args): - keyinfo = [] - for x in GKEY._fields: - try: - keyinfo.append(getattr(args, x)) - except AttributeError: - keyinfo.append(None) - return keyinfo - - def load_seeds(self, filename): if not filename: self.logger.debug("ACTIONS: load_seeds; no filename to load: " @@ -71,7 +47,8 @@ class Actions(object): def listseed(self, args): '''Action listseed method''' - kwargs = self.build_gkeydict(args) + handler = SeedHandler(self.logger) + kwargs = handler.build_gkeydict(args) self.logger.debug("ACTIONS: listseed; kwargs: %s" % str(kwargs)) if not self.seeds: self.seeds = self.load_seeds(args.seeds) @@ -83,9 +60,8 @@ class Actions(object): def addseed(self, args): '''Action addseed method''' - parts = self.build_gkeylist(args) - gkey = GKEY._make(parts) - self.logger.debug("ACTIONS: addseed; new gkey: %s" % str(gkey)) + handler = SeedHandler(self.logger) + gkey = handler.new(args) gkeys = self.listseed(args) if len(gkeys) == 0: self.logger.debug("ACTIONS: addkey; now adding gkey: %s" % str(gkey)) @@ -102,10 +78,13 @@ class Actions(object): def removeseed(self, args): '''Action removeseed method''' - parts = self.build_gkeylist(args) - searchkey = GKEY._make(parts) + handler = SeedHandler(self.logger) + searchkey = handler.new(args, needkeyid=False, checkintegrity=False) self.logger.debug("ACTIONS: removeseed; gkey: %s" % str(searchkey)) gkeys = self.listseed(args) + if not gkeys: + return ["Failed to Removed 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]) @@ -123,12 +102,12 @@ class Actions(object): def moveseed(self, args): '''Action moveseed method''' - parts = self.build_gkeylist(args) - searchkey = GKEY._make(parts) + handler = SeedHandler(self.logger) + searchkey = handler.new(args, needkeyid=False, checkintegrity=False) self.logger.debug("ACTIONS: moveseed; gkey: %s" % str(searchkey)) if not self.seeds: self.seeds = self.load_seeds(args.seeds) - kwargs = self.build_gkeydict(args) + kwargs = handler.build_gkeydict(args) sourcekeys = self.seeds.list(**kwargs) dest = self.load_seeds(args.destination) destkeys = dest.list(**kwargs) @@ -165,7 +144,8 @@ class Actions(object): '''Action listskey method''' self.seeds = self.load_seeds(args.seeds) if self.seeds: - kwargs = self.build_gkeydict(args) + handler = SeedHandler(self.logger) + kwargs = handler.build_gkeydict(args) # get the desired seed keyresults = self.seeds.list(**kwargs) if keyresults and not args.nick == '*' and self.output: @@ -213,7 +193,8 @@ class Actions(object): def addkey(self, args): '''Action addkey method''' - kwargs = self.build_gkeydict(args) + handler = SeedHandler(self.logger) + kwargs = handler.build_gkeydict(args) self.logger.debug("ACTIONS: listseed; kwargs: %s" % str(kwargs)) self.seeds = self.load_seeds(args.seeds) if self.seeds: diff --git a/gkeys/seedhandler.py b/gkeys/seedhandler.py new file mode 100644 index 0000000..af1fbaa --- /dev/null +++ b/gkeys/seedhandler.py @@ -0,0 +1,137 @@ +# +#-*- coding:utf-8 -*- + +""" + Gentoo-keys - seedhandler.py + + Seed handling interface module + + @copyright: 2012 by Brian Dolbec + @license: GNU GPL2, see COPYING for details. +""" + +import re + +from gkeys.config import (GKEY, NICK, NAME, KEYID, LONGKEYID, FINGERPRINT, + KEY_LEN) + + +class SeedHandler(object): + + + def __init__(self,logger): + self.logger = logger + self.fingerprint_re = re.compile('[0-9A-Fa-f]{40}') + + + def new(self, args, needkeyid=True, checkintegrity=True): + parts = self.build_gkeylist(args, needkeyid, checkintegrity) + gkey = GKEY._make(parts) + self.logger.debug("SeedHandler: new() new gkey: %s" % str(gkey)) + return gkey + + + @staticmethod + def build_gkeydict(args): + keyinfo = {} + for x in GKEY._fields: + try: + value = getattr(args, x) + if value: + keyinfo[x] = value + except AttributeError: + 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 x in GKEY._fields: + if GKEY.field_types[x] is str: + try: + value = getattr(args, x) + except AttributeError: + value = None + elif GKEY.field_types[x] is list: + try: + value = [y for y in getattr(args, x).split()] + except AttributeError: + value = None + keyinfo.append(value) + if x 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[-KEY_LEN['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' + 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 + 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 ldap info 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 + return is_good + + + def _check_fingerprint_integrity(self, keyinfo): + # assume it's good until an error is found + is_good = True + for x in keyinfo[FINGERPRINT]: + # check fingerprint integrity + if len(x) != 40: + self.logger.error('ERROR in keyinfo for: %s, %s' + %(keyinfo[NICK], keyinfo[NAME])) + self.logger.error(' GPGKey incorrect fingerprint ' + + 'length (%s) for fingerprint: %s' %(len(x), x)) + is_good = False + continue + if not self.fingerprint_re.match(x): + 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: ' + x) + is_good = False + return is_good -- 2.26.2