Code

Fix PGP implementation -- the pyme API has changed significantly since
authorschlatterbeck <schlatterbeck@57a73879-2fb5-44c3-a270-3262357dd7e2>
Thu, 6 Oct 2011 21:02:09 +0000 (21:02 +0000)
committerschlatterbeck <schlatterbeck@57a73879-2fb5-44c3-a270-3262357dd7e2>
Thu, 6 Oct 2011 21:02:09 +0000 (21:02 +0000)
this worked (API is now better). Add regression-test for PGP support
(this isn't run if pyme isn't installed). We're testing only reception
of a signed message for now.

git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/roundup/trunk@4653 57a73879-2fb5-44c3-a270-3262357dd7e2

roundup/mailgw.py
test/test_mailgw.py

index d2890e0c196bbb4ac24051becc0cc0b311d7bcda..d269554f8783216a05f51de73ed062c15fa28ca3 100644 (file)
@@ -92,7 +92,7 @@ from roundup.i18n import _
 from roundup.hyperdb import iter_roles
 
 try:
-    import pyme, pyme.core, pyme.gpgme
+    import pyme, pyme.core, pyme.constants, pyme.constants.sigsum
 except ImportError:
     pyme = None
 
@@ -156,39 +156,31 @@ def gpgh_key_getall(key, attr):
     ''' return list of given attribute for all uids in
         a key
     '''
-    u = key.uids
-    while u:
+    for u in key.uids:
         yield getattr(u, attr)
-        u = u.next
 
-def gpgh_sigs(sig):
-    ''' more pythonic iteration over GPG signatures '''
-    while sig:
-        yield sig
-        sig = sig.next
-
-def check_pgp_sigs(sig, gpgctx, author):
+def check_pgp_sigs(sigs, gpgctx, author):
     ''' Theoretically a PGP message can have several signatures. GPGME
-        returns status on all signatures in a linked list. Walk that
-        linked list looking for the author's signature
+        returns status on all signatures in a list. Walk that list
+        looking for the author's signature
     '''
-    for sig in gpgh_sigs(sig):
+    for sig in sigs:
         key = gpgctx.get_key(sig.fpr, False)
         # we really only care about the signature of the user who
         # submitted the email
         if key and (author in gpgh_key_getall(key, 'email')):
-            if sig.summary & pyme.gpgme.GPGME_SIGSUM_VALID:
+            if sig.summary & pyme.constants.sigsum.VALID:
                 return True
             else:
                 # try to narrow down the actual problem to give a more useful
                 # message in our bounce
-                if sig.summary & pyme.gpgme.GPGME_SIGSUM_KEY_MISSING:
+                if sig.summary & pyme.constants.sigsum.KEY_MISSING:
                     raise MailUsageError, \
                         _("Message signed with unknown key: %s") % sig.fpr
-                elif sig.summary & pyme.gpgme.GPGME_SIGSUM_KEY_EXPIRED:
+                elif sig.summary & pyme.constants.sigsum.KEY_EXPIRED:
                     raise MailUsageError, \
                         _("Message signed with an expired key: %s") % sig.fpr
-                elif sig.summary & pyme.gpgme.GPGME_SIGSUM_KEY_REVOKED:
+                elif sig.summary & pyme.constants.sigsum.KEY_REVOKED:
                     raise MailUsageError, \
                         _("Message signed with a revoked key: %s") % sig.fpr
                 else:
@@ -511,7 +503,6 @@ class Message(mimetools.Message):
             raise MailUsageError, \
                 _("No PGP signature found in message.")
 
-        context = pyme.core.Context()
         # msg.getbody() is skipping over some headers that are
         # required to be present for verification to succeed so
         # we'll do this by hand
@@ -526,6 +517,7 @@ class Message(mimetools.Message):
         msg_data = pyme.core.Data(canonical_msg)
         sig_data = pyme.core.Data(sig.getbody())
 
+        context = pyme.core.Context()
         context.op_verify(sig_data, msg_data, None)
 
         # check all signatures for validity
@@ -995,7 +987,7 @@ Subject was: "%(subject)s"
             """
             if self.config.PGP_ROLES:
                 return self.db.user.has_role(self.author,
-                    iter_roles(self.config.PGP_ROLES))
+                    *iter_roles(self.config.PGP_ROLES))
             else:
                 return True
 
index 6b64a30caae047aa1bfa85d54617ea632f614726..ea1a53a67c9906abaafba1c2bbf40dcfc4b4fafe 100644 (file)
 
 import unittest, tempfile, os, shutil, errno, imp, sys, difflib, rfc822, time
 
+
+try:
+    import pyme, pyme.core
+except ImportError:
+    pyme = None
+
+
 from cStringIO import StringIO
 
 if not os.environ.has_key('SENDMAILDEBUG'):
@@ -116,13 +123,13 @@ class DiffHelper:
 
         return res
 
-class MailgwTestCase(unittest.TestCase, DiffHelper):
+class MailgwTestAbstractBase(unittest.TestCase, DiffHelper):
     count = 0
     schema = 'classic'
     def setUp(self):
         self.old_translate_ = mailgw._
         roundupdb._ = mailgw._ = i18n.get_translation(language='C').gettext
-        MailgwTestCase.count = MailgwTestCase.count + 1
+        self.__class__.count = self.__class__.count + 1
 
         # and open the database / "instance"
         self.db = memorydb.create('admin')
@@ -169,6 +176,8 @@ class MailgwTestCase(unittest.TestCase, DiffHelper):
         finally:
             f.close()
 
+    # Normal test-case used for both non-pgp test and a test while pgp
+    # is enabled, so this test is run in both test suites.
     def testEmptyMessage(self):
         nodeid = self._handle_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
@@ -183,6 +192,9 @@ Subject: [issue] Testing...
         assert not os.path.exists(SENDMAILDEBUG)
         self.assertEqual(self.db.issue.get(nodeid, 'title'), 'Testing...')
 
+
+class MailgwTestCase(MailgwTestAbstractBase):
+
     def testMessageWithFromInIt(self):
         nodeid = self._handle_mail('''Content-Type: text/plain;
   charset="iso-8859-1"
@@ -1780,7 +1792,7 @@ Unknown address: fubar@bork.bork.bork
 """)
             assert not body_diff, body_diff
         else:
-            raise AssertionError, "Unathorized not raised when handling mail"
+            raise AssertionError, "Unauthorized not raised when handling mail"
 
         # Make sure list of users is the same as before.
         m = self.db.user.list()
@@ -2950,9 +2962,189 @@ Stack trace:
         fileid = self.db.msg.get(msgid, 'files')[0]
         self.assertEqual(self.db.file.get(fileid, 'type'), 'message/rfc822')
 
+pgp_test_key = """
+-----BEGIN PGP PRIVATE KEY BLOCK-----
+Version: GnuPG v1.4.10 (GNU/Linux)
+
+lQOYBE6NqtsBCADG3UUMYxjwUOpDDVvr0Y8qkvKsgdF79en1zfHtRYlmZc+EJxg8
+53CCFGReQWJwOjyP3/SLJwJqfiPR7MAYAqJsm/4U2lxF7sIlEnlrRpFuvB625KOQ
+oedCkI4nLa+4QAXHxVX2qLx7es3r2JAoitZLX7ZtUB7qGSRh98DmdAgCY3CFN7iZ
+w6xpvIU+LNbsHSo1sf8VP6z7NHQFacgrVvLyRJ4C5lTPU42iM5E6HKxYFExNV3Rn
++2G0bsuiifH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+XTnjElhLsmrgjl7aAFveb30R6
+ImmcpKMN31vAp4RZlnyYbYUCY4IXFuz3n1CaUL+mRx5yNJykrZNfpWNf2pwozkZq
+lcDI69ymIW5acXUAEQEAAQAD/R7Jdf98l1scngMYo228ikYUxBqm2eX/fiQNXDWM
+ZR2u+TJ9O53MvFejfXX7Pd6lTDQUBwDFncjgXO0YYSrMzabhqpqoKLqOIpZmBuWC
+Hh1lvcFoIYoDR2LkiJ9EPBUEVUBDsUO8ajkILEE3G+DDpCaf9Vo82lCVyhDESqyt
+v4lxAgDOLpoq1Whv5Ejr6FifTWytCiQjH2P1SmePlQmy6oEJRUYA1t4zYrzCJUX8
+VAvPjh9JXilP6mhDbyQArWllewV9AgDPbVOf75ktRwfhje26tZsukqWYJCc1XvoH
+3PTzA7vH1HZZq7dvxa87PiSnkOLEsIAsI+4jpeMxpPlQRxUvHf1ZAf9rK3v3HMJ/
+2xVzwK24Oaj+g2O7D/fdqtLFGe5S5JobnTyp9xArDAhaZ/AKfDMYjUIKMP+bdNAf
+y8fQUtuawFltm1GInwQYAQIACQUCTo3C8AIbDAAKCRC/z7qg+FujnDzYA/9EU6Pv
+Ci1+DCtxjnq7IOvOjqExhFNGvN9Dw17Tl8HcyW3if9v5RxeSWYKl0DhzVdzMQgH/
+78q4F4W1q2IkB7SCpXizHLIc3eh8iZkbWZE+CGPvTpqyF03Yi16qhxpAbkGs2Yhq
+jTx5oJ4CL5fybBOZLg+BTlK4HIee6xEcbNoq+A==
+=ZKBW
+-----END PGP PRIVATE KEY BLOCK-----
+"""
+
+ownertrust = """
+723762CD5A5FECB76DC72DF85ADCFD5631C38741:6:
+2940C247A1FBAD508A1AF24BBFCFBAA0F85BA39C:6:
+"""
+
+class MailgwPGPTestCase(MailgwTestAbstractBase):
+    pgphome = 'pgp-test-home'
+    def setUp(self):
+        MailgwTestAbstractBase.setUp(self)
+        self.db.security.addRole (name = 'pgp', description = 'PGP Role')
+        self.instance.config['PGP_HOMEDIR'] = self.pgphome
+        self.instance.config['PGP_ROLES'] = 'pgp'
+        self.instance.config['PGP_ENABLE'] = True
+        self.db.user.set(self.john_id, roles='User,pgp')
+        os.mkdir(self.pgphome)
+        os.environ['GNUPGHOME'] = self.pgphome
+        ctx = pyme.core.Context()
+        key = pyme.core.Data(pgp_test_key)
+        ctx.op_import(key)
+        key = pyme.core.Data(john_doe_key)
+        ctx.op_import(key)
+        # trust-modelling with pyme isn't working in 0.8.1
+        # based on libgpgme11 1.2.0, also tried in C -- same thing.
+        otrust = os.popen ('gpg --import-ownertrust 2> /dev/null', 'w')
+        otrust.write(ownertrust)
+        otrust.close()
+
+    def tearDown(self):
+        MailgwTestAbstractBase.tearDown(self)
+        if os.path.exists(self.pgphome):
+            shutil.rmtree(self.pgphome)
+
+    def testUnsignedMessage(self):
+        self.assertRaises(MailUsageError, self._handle_mail,
+            '''Content-Type: text/plain;
+  charset="iso-8859-1"
+From: John Doe <john@test.test>
+To: issue_tracker@your.tracker.email.domain.example
+Message-Id: <dummy_test_message_id>
+Subject: [issue] Testing non-signed message...
+
+This is no pgp signed message.
+''')
+
+    def testSignedMessage(self):
+        nodeid = self._handle_mail('''Content-Disposition: inline
+From: John Doe <john@test.test>
+To: issue_tracker@your.tracker.email.domain.example
+Subject: [issue] Testing signed message...
+Content-Type: multipart/signed; micalg=pgp-sha1;
+        protocol="application/pgp-signature"; boundary="cWoXeonUoKmBZSoM"
+
+
+--cWoXeonUoKmBZSoM
+Content-Type: text/plain; charset=us-ascii
+Content-Disposition: inline
+
+This is a pgp signed message.
+
+--cWoXeonUoKmBZSoM
+Content-Type: application/pgp-signature; name="signature.asc"
+Content-Description: Digital signature
+Content-Disposition: inline
+
+-----BEGIN PGP SIGNATURE-----
+Version: GnuPG v1.4.10 (GNU/Linux)
+
+iJwEAQECAAYFAk6N4A4ACgkQv8+6oPhbo5x5nAP/d7R7SxTvLoVESI+1r7eDXp1J
+LvBVU2EF3YFYKBHMLcWmjG92fNjnHX6NENTEhTeBynba5IPEwUfITC+7PmgPmQkA
+VXnFZnwraHxsYgyFsVFN1kkTSbwRUlWl9+nTEsr0yBLTpZN0QSIDcwu+i/xVcg+t
+ZQ4K6R3m3AOw7BLdvZs=
+=wpYk
+-----END PGP SIGNATURE-----
+
+--cWoXeonUoKmBZSoM--
+''')
+        m = self.db.issue.get (nodeid, 'messages') [0]
+        self.assertEqual(self.db.msg.get(m, 'content'), 
+            'This is a pgp signed message.')
+
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTest(unittest.makeSuite(MailgwTestCase))
+    if pyme is not None:
+        suite.addTest(unittest.makeSuite(MailgwPGPTestCase))
+    else:
+        print "Skipping PGP tests"
     return suite
 
 if __name__ == '__main__':
@@ -2960,7 +3152,3 @@ if __name__ == '__main__':
     unittest.main(testRunner=runner)
 
 # vim: set filetype=python sts=4 sw=4 et si :
-
-
-
-