Merge lp:~michael.nelson/launchpad/ppa-generate-key-failure into lp:launchpad

Proposed by Michael Nelson
Status: Merged
Approved by: Michael Nelson
Approved revision: no longer in the source branch.
Merged at revision: 10834
Proposed branch: lp:~michael.nelson/launchpad/ppa-generate-key-failure
Merge into: lp:launchpad
Diff against target: 60 lines (+31/-2)
2 files modified
lib/canonical/launchpad/utilities/ftests/test_gpghandler.py (+24/-2)
lib/canonical/launchpad/utilities/gpghandler.py (+7/-0)
To merge this branch: bzr merge lp:~michael.nelson/launchpad/ppa-generate-key-failure
Reviewer Review Type Date Requested Status
Abel Deuring (community) code Approve
Review via email: mp+24871@code.launchpad.net

Commit message

Ensure only byte-strings are passed to gpgme.Context().keylist()

Description of the change

This branch fixes bug 576405 by ensuring that IGPGHandler.localKeys() passes only byte-strings to gpgme.Context().keylist().

To test:
bin/test -vv -m test_gpghandler

Initially I refactored IGPGHandler.generateKey() to make it more testable (ie. to be able to test everything outside of the gpgme.Context().genkey() call), but deleted this in the end in favour of a direct test of the issue.

This will need to be CP'd, so if you review this and I'm not around, please feel free to send it off to land on devel.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) wrote :

Isn't this going to explode if the first PPA's display name contains non-ASCII characters?

Revision history for this message
Michael Nelson (michael.nelson) wrote :

Right - I had understood that we were only using localKeys() with fingerprints and email addresses, but better to be safe.

I couldn't find much documentation for gpgme, and so have just encoded the string in utf-8.

Thanks William.

Revision history for this message
Abel Deuring (adeuring) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/canonical/launchpad/utilities/ftests/test_gpghandler.py'
--- lib/canonical/launchpad/utilities/ftests/test_gpghandler.py 2009-06-25 05:30:52 +0000
+++ lib/canonical/launchpad/utilities/ftests/test_gpghandler.py 2010-05-07 12:26:30 +0000
@@ -6,8 +6,8 @@
66
7from zope.component import getUtility7from zope.component import getUtility
88
9from canonical.launchpad.ftests import login, ANONYMOUS, logout9from canonical.launchpad.ftests import (
10from canonical.launchpad.ftests import keys_for_tests10 ANONYMOUS, keys_for_tests, login, logout)
11from canonical.launchpad.interfaces.gpghandler import IGPGHandler11from canonical.launchpad.interfaces.gpghandler import IGPGHandler
12from canonical.testing import LaunchpadFunctionalLayer12from canonical.testing import LaunchpadFunctionalLayer
1313
@@ -97,6 +97,28 @@
97 [key] = filtered_keys97 [key] = filtered_keys
98 self.assertEqual(key.fingerprint, secret_target_fpr)98 self.assertEqual(key.fingerprint, secret_target_fpr)
9999
100 def test_unicode_filter(self):
101 """Using a unicode filter works also.
102
103 XXX michaeln 2010-05-07 bug=576405
104 Recent versions of gpgme return unicode fingerprints, but
105 at the same time, gpgme.Context().keylist falls over if
106 it receives a unicode string.
107 """
108 self.populateKeyring()
109
110 target_fpr = u'340CA3BB270E2716C9EE0B768E7EB7086C64A8C5'
111
112 # Finding a key by its unicode fingerprint.
113 filtered_keys = self.gpg_handler.localKeys(target_fpr)
114 [key] = filtered_keys
115 self.assertEqual(key.fingerprint, target_fpr)
116
117 def test_non_ascii_filter(self):
118 """localKeys should not error if passed non-ascii unicode strings."""
119 filtered_keys = self.gpg_handler.localKeys(u'non-ascii \u8463')
120 self.failUnlessRaises(StopIteration, filtered_keys.next)
121
100 def testTestkeyrings(self):122 def testTestkeyrings(self):
101 """Do we have the expected test keyring files"""123 """Do we have the expected test keyring files"""
102 self.assertEqual(len(list(keys_for_tests.test_keyrings())), 1)124 self.assertEqual(len(list(keys_for_tests.test_keyrings())), 1)
103125
=== modified file 'lib/canonical/launchpad/utilities/gpghandler.py'
--- lib/canonical/launchpad/utilities/gpghandler.py 2010-04-26 16:00:31 +0000
+++ lib/canonical/launchpad/utilities/gpghandler.py 2010-05-07 12:26:30 +0000
@@ -399,6 +399,13 @@
399 already knows about.399 already knows about.
400 """400 """
401 ctx = gpgme.Context()401 ctx = gpgme.Context()
402
403 # XXX michaeln 2010-05-07 bug=576405
404 # Currently gpgme.Context().keylist fails if passed a unicode
405 # string even though that's what is returned for fingerprints.
406 if type(filter) == unicode:
407 filter = filter.encode('utf-8')
408
402 for key in ctx.keylist(filter, secret):409 for key in ctx.keylist(filter, secret):
403 yield PymeKey.newFromGpgmeKey(key)410 yield PymeKey.newFromGpgmeKey(key)
404411