1 |
commit: 8e56c1e4fb4d7d2e6bae2f12f64aed86617aed68 |
2 |
Author: Pavlos Ratis <dastergon <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sat May 24 17:56:39 2014 +0000 |
4 |
Commit: Brian Dolbec <brian.dolbec <AT> gmail <DOT> com> |
5 |
CommitDate: Sat May 24 17:56:39 2014 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/gentoo-keys.git;a=commit;h=8e56c1e4 |
7 |
|
8 |
refine seed handling |
9 |
|
10 |
--- |
11 |
gkeys/actions.py | 12 ++-- |
12 |
gkeys/seedhandler.py | 153 ++++++++++++++------------------------------------- |
13 |
2 files changed, 48 insertions(+), 117 deletions(-) |
14 |
|
15 |
diff --git a/gkeys/actions.py b/gkeys/actions.py |
16 |
index 79690fc..79c42ee 100644 |
17 |
--- a/gkeys/actions.py |
18 |
+++ b/gkeys/actions.py |
19 |
@@ -69,11 +69,11 @@ class Actions(object): |
20 |
def addseed(self, args): |
21 |
'''Action addseed method''' |
22 |
handler = SeedHandler(self.logger) |
23 |
- gkey = handler.new(args) |
24 |
+ gkey = handler.new(args, checkgkey=True) |
25 |
gkeys = self.listseed(args) |
26 |
if len(gkeys) == 0: |
27 |
self.logger.debug("ACTIONS: addkey; now adding gkey: %s" % str(gkey)) |
28 |
- success = self.seeds.add(gkey) |
29 |
+ success = self.seeds.add(getattr(gkey, 'nick')[0], gkey) |
30 |
if success: |
31 |
success = self.seeds.save() |
32 |
return ["Successfully added new seed: %s" % str(success), gkey] |
33 |
@@ -87,19 +87,19 @@ class Actions(object): |
34 |
def removeseed(self, args): |
35 |
'''Action removeseed method''' |
36 |
handler = SeedHandler(self.logger) |
37 |
- searchkey = handler.new(args, needkeyid=False, checkintegrity=False) |
38 |
+ searchkey = handler.new(args) |
39 |
self.logger.debug("ACTIONS: removeseed; gkey: %s" % str(searchkey)) |
40 |
gkeys = self.listseed(args) |
41 |
if not gkeys: |
42 |
return ["Failed to remove seed: No gkeys returned from listseed()", |
43 |
None] |
44 |
if len(gkeys) == 1: |
45 |
- self.logger.debug("ACTIONS: removeseed; now deleting gkey: %s" % str(gkeys[0])) |
46 |
- success = self.seeds.delete(gkeys[0]) |
47 |
+ self.logger.debug("ACTIONS: removeseed; now deleting gkey: %s" % str(gkeys)) |
48 |
+ success = self.seeds.delete(gkeys) |
49 |
if success: |
50 |
success = self.seeds.save() |
51 |
return ["Successfully removed seed: %s" % str(success), |
52 |
- gkeys[0]] |
53 |
+ gkeys] |
54 |
elif len(gkeys): |
55 |
messages = ["Too many seeds found to remove"] |
56 |
messages.extend(gkeys) |
57 |
|
58 |
diff --git a/gkeys/seedhandler.py b/gkeys/seedhandler.py |
59 |
index be68990..8aac6a8 100644 |
60 |
--- a/gkeys/seedhandler.py |
61 |
+++ b/gkeys/seedhandler.py |
62 |
@@ -12,29 +12,29 @@ |
63 |
|
64 |
import re |
65 |
|
66 |
-from gkeys.config import (GKEY, NICK, NAME, KEYID, LONGKEYID, FINGERPRINT, |
67 |
- KEYLEN_MAP) |
68 |
+from gkeys.config import GKEY |
69 |
|
70 |
|
71 |
class SeedHandler(object): |
72 |
|
73 |
- |
74 |
def __init__(self,logger): |
75 |
self.logger = logger |
76 |
self.fingerprint_re = re.compile('[0-9A-Fa-f]{40}') |
77 |
self.finerprint_re2 = re.compile('[0-9A-Fa-f]{4}( [0-9A-Fa-f]{4}){9}') |
78 |
|
79 |
|
80 |
- def new(self, args, needkeyid=True, checkintegrity=True): |
81 |
- parts = self.build_gkeylist(args, needkeyid, checkintegrity) |
82 |
- if parts: |
83 |
- gkey = GKEY._make(parts) |
84 |
- self.logger.debug("SeedHandler: new() new gkey: %s" % str(gkey)) |
85 |
+ def new(self, args, checkgkey=False): |
86 |
+ newgkey = self.build_gkeydict(args) |
87 |
+ if checkgkey: |
88 |
+ newgkey = self.check_gkey(newgkey) |
89 |
+ if newgkey: |
90 |
+ newgkey = GKEY(**newgkey) |
91 |
+ self.logger.debug("SeedHandler: new() new gkey: %s" % str(newgkey)) |
92 |
else: |
93 |
self.logger.debug("SeedHandler: new() FAILED to et parts from: %s" |
94 |
% str(args)) |
95 |
return None |
96 |
- return gkey |
97 |
+ return newgkey |
98 |
|
99 |
|
100 |
@staticmethod |
101 |
@@ -49,109 +49,40 @@ class SeedHandler(object): |
102 |
pass |
103 |
return keyinfo |
104 |
|
105 |
- |
106 |
- def build_gkeylist(self, args, needkeyid=True, checkintegrity=True): |
107 |
- keyinfo = [] |
108 |
- keyid_found = False |
109 |
- # assume it's good until an error is found |
110 |
- is_good = True |
111 |
- #self.logger.debug("SeedHandler: build_gkeylist; args = %s" % str(args)) |
112 |
- for attr in GKEY._fields: |
113 |
- if GKEY.field_types[attr] is str: |
114 |
- try: |
115 |
- value = getattr(args, attr) |
116 |
- except AttributeError: |
117 |
- value = None |
118 |
- elif GKEY.field_types[attr] is list: |
119 |
- try: |
120 |
- values = [y for y in getattr(args, attr).split(':')] |
121 |
- value = [v.replace(' ', '') for v in values] |
122 |
- except AttributeError: |
123 |
- value = None |
124 |
- keyinfo.append(value) |
125 |
- if attr in ["keyid", "longkeyid"] and value: |
126 |
- keyid_found = True |
127 |
- if not keyid_found and needkeyid: |
128 |
- fingerprint = keyinfo[FINGERPRINT] |
129 |
- if fingerprint: |
130 |
- self.logger.debug(' Generate gpgkey longkeyid, Found ' |
131 |
- 'fingerprint in args') |
132 |
- # assign it to gpgkey to prevent a possible |
133 |
- # "gpgkey" undefined error |
134 |
- gpgkey = ['0x' + x[-KEYLEN_MAP['longkeyid']:] for x in fingerprint] |
135 |
- keyinfo[LONGKEYID] = gpgkey |
136 |
- self.logger.debug(' Generate gpgkey longkeyid, NEW ' |
137 |
- 'keyinfo[LONGKEYID] = %s' % str(keyinfo[LONGKEYID])) |
138 |
- else: |
139 |
- gpgkey = 'Missing or Bad fingerprint from command line args' |
140 |
+ def check_gkey(self, args): |
141 |
+ # assume it's good until an error is found |
142 |
+ is_good = True |
143 |
+ try: |
144 |
+ if args['fingerprint']: |
145 |
+ # create a longkeyid based on fingerprint |
146 |
+ is_ok = self._check_fingerprint_integrity(args) |
147 |
+ args['keydir'] = args.get('keydir', args['nick']) |
148 |
+ if not is_ok: |
149 |
is_good = False |
150 |
- if not keyinfo[LONGKEYID]: |
151 |
- self.logger.error('ERROR in seed creation info for: %s, %s' |
152 |
- %(keyinfo[NICK], keyinfo[NAME])) |
153 |
- self.logger.error(' A valid keyid, longkeyid or fingerprint ' |
154 |
- 'was not found for %s : gpgkey = %s' |
155 |
- %(keyinfo[NAME], gpgkey)) |
156 |
- is_good = False |
157 |
- if is_good: |
158 |
- if keyinfo[FINGERPRINT]: # fingerprints exist check |
159 |
- is_ok = self._check_fingerprint_integrity(keyinfo) |
160 |
- is_match = self._check_id_fingerprint_match(keyinfo) |
161 |
- if not is_ok or not is_match: |
162 |
- is_good = False |
163 |
- if is_good: |
164 |
- return keyinfo |
165 |
- return None |
166 |
- |
167 |
- |
168 |
- def _check_id_fingerprint_match(self, keyinfo): |
169 |
- # assume it's good until found an error is found |
170 |
+ self.logger.error('Bad fingerprint from command line args.') |
171 |
+ except KeyError: |
172 |
+ self.logger.error('GPG fingerprint not found.') |
173 |
+ is_good = False |
174 |
+ # need to add values to a list |
175 |
+ for key,value in args.items(): |
176 |
+ args[key] = value.split() |
177 |
+ if is_good: |
178 |
+ return args |
179 |
+ else: |
180 |
+ self.logger.error('A valid fingerprint ' |
181 |
+ 'was not found for %s' % args['name']) |
182 |
+ return args |
183 |
+ |
184 |
+ def _check_fingerprint_integrity(self, gkey): |
185 |
+ # assume it's good unti an error is found |
186 |
is_good = True |
187 |
- for x in [KEYID, LONGKEYID]: |
188 |
- # skip blank id field |
189 |
- if not keyinfo[x]: |
190 |
- continue |
191 |
- for y in keyinfo[x]: |
192 |
- index = len(y.lstrip('0x')) |
193 |
- if y.lstrip('0x').upper() not in \ |
194 |
- [x[-index:].upper() for x in keyinfo[FINGERPRINT]]: |
195 |
- self.logger.error('ERROR in keyinfo for: %s, %s' |
196 |
- %(keyinfo[NICK], keyinfo[NAME])) |
197 |
- self.logger.error(' ' + str(keyinfo)) |
198 |
- self.logger.error(' GPGKey id %s not found in the ' |
199 |
- % y.lstrip('0x') + 'listed fingerprint(s)') |
200 |
- is_good = False |
201 |
- ids = 0 |
202 |
- for x in [KEYID, LONGKEYID]: |
203 |
- if keyinfo[x]: |
204 |
- ids = ids + len(keyinfo[x]) |
205 |
- if ids != len(keyinfo[FINGERPRINT]): |
206 |
- self.logger.error('ERROR in keyinfo for: %s, %s' |
207 |
- %(keyinfo[NICK], keyinfo[NAME])) |
208 |
- self.logger.error(' ' + str(keyinfo)) |
209 |
- self.logger.error(' GPGKey the number of ids %d DO NOT match ' |
210 |
- 'the number of listed fingerprint(s), {%s,%s}, %s' |
211 |
- % (ids, keyinfo[KEYID], keyinfo[LONGKEYID], keyinfo[FINGERPRINT])) |
212 |
- is_good = False |
213 |
- |
214 |
- return is_good |
215 |
- |
216 |
- |
217 |
- def _check_fingerprint_integrity(self, keyinfo): |
218 |
- # assume it's good until an error is found |
219 |
- is_good = True |
220 |
- for fingerprint in keyinfo[FINGERPRINT]: |
221 |
- # check fingerprint integrity |
222 |
- if len(fingerprint) != 40: |
223 |
- self.logger.error('ERROR in keyinfo for: %s, %s' |
224 |
- %(keyinfo[NICK], keyinfo[NAME])) |
225 |
- self.logger.error(' GPGKey incorrect fingerprint ' + |
226 |
+ fingerprint = gkey['fingerprint'] |
227 |
+ # check fingerprint integrity |
228 |
+ if len(fingerprint) != 40: |
229 |
+ self.logger.error(' GPGKey incorrect fingerprint ' + |
230 |
'length (%s) for fingerprint: %s' %(len(fingerprint), fingerprint)) |
231 |
- is_good = False |
232 |
- continue |
233 |
- if not self.fingerprint_re.match(fingerprint): |
234 |
- self.logger.error('ERROR in keyinfo info for: %s, %s' |
235 |
- %(keyinfo[NICK], keyinfo[NAME])) |
236 |
- self.logger.error(' GPGKey: Non hexadecimal digits in ' + |
237 |
- 'fingerprint for fingerprint: ' + fingerprint) |
238 |
- is_good = False |
239 |
+ is_good = False |
240 |
+ if not self.fingerprint_re.match(fingerprint): |
241 |
+ self.logger.error(' GPGKey: Non hexadecimal digits in ' + 'fingerprint for fingerprint: ' + fingerprint) |
242 |
+ is_good = False |
243 |
return is_good |