From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id 854B11386F1 for ; Sun, 9 Aug 2015 01:09:48 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 3B53B141C1; Sun, 9 Aug 2015 01:09:41 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id A11481419F for ; Sun, 9 Aug 2015 01:09:39 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id A0D21340ABD for ; Sun, 9 Aug 2015 01:09:38 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id E7CDE13A for ; Sun, 9 Aug 2015 01:09:33 +0000 (UTC) From: "Brian Dolbec" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Brian Dolbec" Message-ID: <1439049100.9e7539d68b75a37626cc4a2b6c59a8d21208b57c.dolsen@gentoo> Subject: [gentoo-commits] proj/gentoo-keys:master commit in: gkeys/gkeys/ X-VCS-Repository: proj/gentoo-keys X-VCS-Files: gkeys/gkeys/actions.py gkeys/gkeys/keyhandler.py X-VCS-Directories: gkeys/gkeys/ X-VCS-Committer: dolsen X-VCS-Committer-Name: Brian Dolbec X-VCS-Revision: 9e7539d68b75a37626cc4a2b6c59a8d21208b57c X-VCS-Branch: master Date: Sun, 9 Aug 2015 01:09:33 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: bb2561de-f1f3-4103-a6e3-3accf73d1aa6 X-Archives-Hash: e7c53e2aed37fd834a856a333d18fdd5 commit: 9e7539d68b75a37626cc4a2b6c59a8d21208b57c Author: Brian Dolbec gentoo org> AuthorDate: Wed Jul 29 06:35:53 2015 +0000 Commit: Brian Dolbec gentoo org> CommitDate: Sat Aug 8 15:51:40 2015 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-keys.git/commit/?id=9e7539d6 gkeys: [2 of 2] Move common key handling to it's own class Creation of a new KeyHandler class. Move primary seed handling intialization and control to the new KeyHandler class. This unifies key handling and removes suplicated code throughout Actions class. Break out a KEY_OPTIONS list. Cleans up a long line and makes it available for import use to ensure the correct options are added to the cli (gkeys-gpg) for correct key_search() operation. gkeys/gkeys/actions.py | 156 +++++++++++----------------------------------- gkeys/gkeys/keyhandler.py | 110 ++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 118 deletions(-) diff --git a/gkeys/gkeys/actions.py b/gkeys/gkeys/actions.py index 11f1ca8..69d05b6 100644 --- a/gkeys/gkeys/actions.py +++ b/gkeys/gkeys/actions.py @@ -26,18 +26,17 @@ else: from collections import defaultdict -from snakeoil.demandload import demandload from gkeys.actionbase import ActionBase from gkeys.gkey import GKEY from gkeys.checks import SPECCHECK_SUMMARY, convert_pf, convert_yn +from snakeoil.demandload import demandload + demandload( + "gkeys.base:Args", "json:load", - "gkeys.lib:GkeysGPG", - "gkeys.seedhandler:SeedHandler", ) - EXTENSIONS = ['.sig', '.asc', '.gpg','.gpgsig'] @@ -67,13 +66,12 @@ class Actions(ActionBase): def listseed(self, args): '''Pretty-print the selected seed file''' - handler = SeedHandler(self.logger, self.config) - kwargs = handler.build_gkeydict(args) + kwargs = self.seedhandler.build_gkeydict(args) self.logger.debug(_unicode("ACTIONS: listseed; kwargs: %s") % _unicode(kwargs)) if not self.seeds: try: - self.seeds = handler.load_seeds(args.category, args.nick) + self.seeds = self.seedhandler.load_seeds(args.category, args.nick) except ValueError: return (False, ['', "Failed to load seed file. Consider fetching seedfiles."]) if self.seeds: @@ -89,8 +87,10 @@ class Actions(ActionBase): % _unicode(args)) if not args.category: return (False, ["Please specify seeds category."]) - handler = SeedHandler(self.logger, self.config) - success, messages = handler.fetch_seeds(args.category, args, self.verify) + self._set_category(self.config.get_key('verify-keyring')) + verifyargs = Args() + verifyargs.category=args.category + success, messages = self.seedhandler.fetch_seeds(args.category, verifyargs, self.verify) messages.append("") messages.append("Fetch operation completed") return (False not in success, messages) @@ -98,7 +98,6 @@ class Actions(ActionBase): def addseed(self, args): '''Add or replace a key in the selected seed file''' - handler = SeedHandler(self.logger, self.config) success, data = self.listseed(args) gkeys = data[1] if not args.nick or not args.name or not args.keys or not args.keydir: @@ -107,7 +106,7 @@ class Actions(ActionBase): args.fingerprint = args.keys if args.uid is None: args.uid = [] - gkey = handler.new(args, checkgkey=True) + gkey = self.seedhandler.new(args, checkgkey=True) if not gkey: return (False, ["Failed to create a valid GKEY instance.", "Check for invalid data entries"]) @@ -150,13 +149,12 @@ class Actions(ActionBase): def moveseed(self, args): '''Move keys between seed files''' - handler = SeedHandler(self.logger, self.config) - searchkey = handler.new(args, checkgkey=False) + searchkey = self.seedhandler.new(args, checkgkey=False) self.logger.debug(_unicode("ACTIONS: moveseed; gkey: %s") % _unicode(searchkey)) if not self.seeds: self.seeds = self.load_seeds(args.category) - kwargs = handler.build_gkeydict(args) + kwargs = self.seedhandler.build_gkeydict(args) sourcekeys = self.seeds.list(**kwargs) dest = self.load_seeds(args.destination) destkeys = dest.list(**kwargs) @@ -199,21 +197,17 @@ class Actions(ActionBase): # fill in code here if not args.category: args.category = 'gentoo' - keyring = self.config.get_key('keyring') - catdir = os.path.join(keyring, args.category) - self.logger.debug(_unicode("ACTIONS: listkey; catdir = %s") % catdir) - self.gpg = GkeysGPG(self.config, catdir, self.logger) - handler = SeedHandler(self.logger, self.config) + self.category = args.category if args.keydir: self.gpg.set_keydir(args.keydir, "list-keys") self.gpg.set_keyseedfile() seeds = self.gpg.seedfile else: - seeds = handler.load_category(args.category) + seeds = self.seedhandler.load_category(args.category) results = {} success = [] messages = [] - kwargs = handler.build_gkeydict(args) + kwargs = self.seedhandler.build_gkeydict(args) keyresults = seeds.list(**kwargs) for key in sorted(keyresults): if args.fingerprint: @@ -272,11 +266,7 @@ class Actions(ActionBase): return (False, ["Search failed for search term"]) # get confirmation # fill in code here - keyring = self.config.get_key('keyring') - catdir = os.path.join(keyring, args.category) - self.logger.debug(_unicode("ACTIONS: installkey; catdir = %s") - % catdir) - self.gpg = GkeysGPG(self.config, catdir, self.logger) + self.category = args.category for gkey in gkeys: self.gpg.set_keydir(gkey.keydir, "recv-keys") self.gpg.set_keyseedfile() @@ -333,15 +323,11 @@ class Actions(ActionBase): if not args.category: return (False, [_unicode("Please specify seeds category.")]) self.logger.debug(_unicode("ACTIONS: checkkey; args: %s") % _unicode(args)) - handler = SeedHandler(self.logger, self.config) - seeds = handler.load_category(args.category) - keyring = self.config.get_key('keyring') - catdir = os.path.join(keyring, args.category) - self.logger.debug(_unicode("ACTIONS: checkkey; catdir = %s") % catdir) - self.gpg = GkeysGPG(self.config, catdir, self.logger) + seeds = self.seedhandler.load_category(args.category) + self.category = args.category results = {} failed = defaultdict(list) - kwargs = handler.build_gkeydict(args) + kwargs = self.seedhandler.build_gkeydict(args) keyresults = seeds.list(**kwargs) self.output('', '\n Checking keys...') for gkey in sorted(keyresults): @@ -388,16 +374,11 @@ class Actions(ActionBase): return (False, ["Please specify seeds category."]) self.logger.debug(_unicode("ACTIONS: speccheck; args: %s") % _unicode(args)) - handler = SeedHandler(self.logger, self.config) - seeds = handler.load_category(args.category) - keyring = self.config.get_key('keyring') - catdir = os.path.join(keyring, args.category) + self.category = args.category + catdir, keyresults = self.keyhandler.determine_keys(args) self.logger.debug(_unicode("ACTIONS: speccheck; catdir = %s") % catdir) - self.gpg = GkeysGPG(self.config, catdir, self.logger) results = {} failed = defaultdict(list) - kwargs = handler.build_gkeydict(args) - keyresults = seeds.list(**kwargs) self.output('', '\n Checking keys...') for gkey in sorted(keyresults): self.logger.info(_unicode("Checking key %s, %s") @@ -527,11 +508,10 @@ class Actions(ActionBase): '''Remove an installed key''' if not args.nick: return (False, ["Please provide a nickname or -n *"]) - handler = SeedHandler(self.logger, self.config) - kwargs = handler.build_gkeydict(args) + kwargs = self.seedhandler.build_gkeydict(args) self.logger.debug(_unicode("ACTIONS: removekey; kwargs: %s") % _unicode(kwargs)) - seeds = handler.load_category(args.category) + seeds = self.seedhandler.load_category(args.category) messages = [] if args.nick == '*': self.output([''],_unicode('Remove All keys in category: %s') @@ -565,11 +545,6 @@ class Actions(ActionBase): if ans in ["no", "n"]: messages.append("Key removal aborted... Nothing to be done.") else: - keyring = self.config.get_key('keyring') - catdir = os.path.join(keyring, args.category) - self.logger.debug(_unicode("ACTIONS: removekey; catdir = %s") - % catdir) - self.gpg = GkeysGPG(self.config, catdir, self.logger) if len(gkey.keys) == 1 or args.keys == gkey.keys: success, msgs = self.gpg.del_keydir(gkey) messages.extend(msgs) @@ -592,11 +567,7 @@ class Actions(ActionBase): def importkey(self, args): '''Add a specified key to a specified keyring''' if args.category: - keyring = self.config.get_key('keyring') - catdir = os.path.join(keyring, args.category) - self.logger.debug(_unicode("ACTIONS: importkey; catdir = %s") - % catdir) - self.gpg = GkeysGPG(self.config, catdir, self.logger) + catdir = self._set_category(args.category) success, data = self.listseed(args) gkeys = data[1] results = {} @@ -693,11 +664,10 @@ class Actions(ActionBase): self.logger.debug(_unicode( "ACTIONS: verify; keyring category not specified, using default: %s") % args.category) - handler = SeedHandler(self.logger, self.config) - keys = handler.load_category(args.category) + keys = self.seedhandler.load_category(args.category) if not keys: return (False, ['No installed keys found, try installkey action.']) - key = handler.seeds.nick_search(args.nick) + key = self.seedhandler.seeds.nick_search(args.nick) if not key: if args.nick: messages.append(_unicode( @@ -709,14 +679,10 @@ class Actions(ActionBase): % (args.category, args.nick)) return self.verify(args, messages) - keyrings = self.config.get_key('keyring') - catdir = os.path.join(keyrings, args.category) - self.logger.debug(_unicode("ACTIONS: verify; catdir = %s") % catdir) - self.gpg = GkeysGPG(self.config, catdir, self.logger) - return self._verify(args, handler, key, messages) + return self._verify(args, key, messages) - def _verify(self, args, handler, key, messages=None): + def _verify(self, args, key, messages=None): if messages == None: messages = [] filepath, signature = args.filename, args.signature @@ -807,25 +773,10 @@ class Actions(ActionBase): [_unicode("Verification failed....: %s") % (filepath), _unicode("Key info...............: %s <%s>, %s") % ( key.name, key.nick, keyid)]) - has_no_pubkey, s_keyid = results.no_pubkey - if has_no_pubkey: - messages.append( - _unicode("Auto-searching for key.: 0x%s") % s_keyid) - # reset all but keyid and pass thru data - args.keyid = s_keyid - args.keydir = None - args.fingerprint = None - args.exact = False - args.category = None - args.nick = None - args.name = None - args.all = False - keys = self.key_search(args, data_only=True) - if keys: - args.category = list(keys)[0] - args.nick = keys[args.category][0].nick - return self.verify(args, messages) - messages.append(_unicode("Failed to find gpg key.: 0x%s") % s_keyid) + found, args, new_msgs = self.keyhandler.autosearch_key(args, results) + messages.extend(new_msgs) + if found: + return self.verify(args, messages) return (verified, messages) @@ -856,11 +807,9 @@ class Actions(ActionBase): else: nicks = args.nick # load our installed signing keys db - handler = SeedHandler(self.logger, self.config) - self.seeds = handler.load_category('sign', nicks) + self.seeds = self.seedhandler.load_category('sign', nicks) if not self.seeds.seeds: return (False, ['No installed keys, try installkey action.', '']) - basedir = self.config.get_key("sign-keydir") keydir = self.config.get_key("sign", "keydir") task = self.config.get_key("sign", "type") keyring = self.config.get_key("sign", "keyring") @@ -869,7 +818,6 @@ class Actions(ActionBase): self.logger.debug(_unicode("ACTIONS: sign; keydir = %s") % keydir) - self.gpg = GkeysGPG(self.config, basedir, self.logger) self.gpg.set_keydir(keydir, task) if keyring not in ['', None]: self.gpg.set_keyring(keyring, task) @@ -902,14 +850,10 @@ class Actions(ActionBase): return (False, ["Please specify seeds type."]) self.logger.debug(_unicode("ACTIONS: refreshkey; args: %s") % _unicode(args)) - handler = SeedHandler(self.logger, self.config) - seeds = handler.load_category(args.category, refresh=True) - keyring = self.config.get_key('keyring') - catdir = os.path.join(keyring, args.category) - self.logger.debug(_unicode("ACTIONS: refreshkey; catdir = %s") % catdir) - self.gpg = GkeysGPG(self.config, catdir, self.logger) + seeds = self.seedhandler.load_category(args.category, refresh=True) + self.category = args.category results = {} - kwargs = handler.build_gkeydict(args) + kwargs = self.seedhandler.build_gkeydict(args) keyresults = seeds.list(**kwargs) self.output('', '\n Refreshig keys...') for gkey in sorted(keyresults): @@ -926,37 +870,13 @@ class Actions(ActionBase): def key_search(self, args, data_only=False): '''Search for a key's seed in the installed keys db''' - handler = SeedHandler(self.logger, self.config) - results = {} - search_args = [x for x in - ['nick', 'name', 'keydir', 'fingerprint', 'keyid', 'uid'] - if getattr(args, x)] - if args.category: - handler.load_category(args.category) - results[args.category] = handler.key_search(args, search_args) - else: - for cat in list(self.config.get_key('seeds')): - handler.load_category(cat) - found = handler.key_search(args, search_args) - if found: - if cat in results: - results[cat].extend(found) - else: - results[cat] = found - keys = {} - for cat in results: - keys[cat] = [] - for result in results[cat]: - if result and result.nick not in keys[cat]: - if isinstance(result, GKEY): - keys[cat].append(result) + keys = self.keyhandler.key_search(args) if data_only: - del found, cat, handler return keys msgs = [] for cat in list(keys): msgs.append(_unicode("Category.....: %s") % cat) msgs.append(keys[cat]) - del keys, cat + del keys return (True, msgs) diff --git a/gkeys/gkeys/keyhandler.py b/gkeys/gkeys/keyhandler.py new file mode 100644 index 0000000..9043fcd --- /dev/null +++ b/gkeys/gkeys/keyhandler.py @@ -0,0 +1,110 @@ +# +#-*- coding:utf-8 -*- + +""" + Gentoo-keys - keyhandler.py + + GKEY handling interface module + + @copyright: 2015 by Brian Dolbec + @license: GNU GPL2, see COPYING for details. +""" +import os +import sys + +from snakeoil.demandload import demandload + +if sys.version_info[0] >= 3: + _unicode = str +else: + _unicode = unicode + +from gkeys.gkey import GKEY + + +demandload( + "gkeys.seedhandler:SeedHandler", +) + +KEY_OPTIONS = ['nick', 'name', 'keydir', 'fingerprint', 'keyid', 'uid'] + + +class KeyHandler(object): + '''Class to hold various key operations''' + + + def __init__(self, config, logger): + self.config = config + self.logger = logger + self._seedhandler = None + + + @property + def seedhandler(self): + if not self._seedhandler: + self._seedhandler = SeedHandler(self.logger, self.config) + return self._seedhandler + + + def autosearch_key(self, args, results): + '''Search for the correct keyid from the GPGResult''' + messages = [] + has_no_pubkey, s_keyid = results.no_pubkey + if has_no_pubkey: + messages.append( + _unicode("Auto-searching for key.: 0x%s") % s_keyid) + # reset all but keyid and pass thru data + args.keyid = s_keyid + args.keydir = None + args.fingerprint = None + args.exact = False + args.category = None + args.nick = None + args.name = None + args.all = False + keys = self.key_search(args) + if keys: + args.category = list(keys)[0] + args.nick = keys[args.category][0].nick + return (True, args, messages) + messages.append(_unicode("Failed to find gpg key.: 0x%s") % s_keyid) + return (False, args, messages) + + + def determine_keys(self, args, default_cat=None): + seeds = self.seedhandler.load_category(args.category or default_cat) + keyring = self.config.get_key('keyring') + catdir = os.path.join(keyring, args.category) + self.logger.debug(_unicode("KeyHandler: determine_keys; catdir = %s") % catdir) + kwargs = self.seedhandler.build_gkeydict(args) + return (catdir, seeds.list(**kwargs)) + + + def key_search(self, args, first_match=False): + '''Search for a key's seed in the installed keys db''' + results = {} + search_args = [x for x in KEY_OPTIONS if getattr(args, x)] + if args.category: + self.seedhandler.load_category(args.category) + results[args.category] = self.seedhandler.key_search(args, search_args) + else: + for cat in sorted(self.config.get_key('seeds')): + self.seedhandler.load_category(cat) + found = self.seedhandler.key_search(args, search_args) + if found: + if cat in results: + results[cat].extend(found) + else: + results[cat] = found + if first_match: + break + keys = {} + for cat in results: + keys[cat] = [] + for result in results[cat]: + if result and result.nick not in keys[cat]: + if isinstance(result, GKEY): + keys[cat].append(result) + + self.logger.debug(_unicode("KeyHandler: key_search; keys = %s") % str(keys)) + return keys