~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/gpg.py

  • Committer: Jelmer Vernooij
  • Date: 2011-10-04 22:20:49 UTC
  • mto: This revision was merged to the branch mainline in revision 6190.
  • Revision ID: jelmer@samba.org-20111004222049-d9glniyleu0pppzd
Add a load_plugin_translations method.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
    trace,
32
32
    ui,
33
33
    )
 
34
from bzrlib.i18n import (
 
35
    gettext, 
 
36
    ngettext,
 
37
    )
34
38
""")
35
39
 
36
 
class i18n:
37
 
    """this class is ready to use bzrlib.i18n but bzrlib.i18n is not ready to
38
 
    use so here is a stub until it is"""
39
 
    @staticmethod
40
 
    def gettext(string):
41
 
        return string
42
 
        
43
 
    @staticmethod
44
 
    def ngettext(single, plural, number):
45
 
        if number == 1:
46
 
            return single
47
 
        else:
48
 
            return plural
49
 
 
50
40
#verification results
51
41
SIGNATURE_VALID = 0
52
42
SIGNATURE_KEY_MISSING = 1
53
43
SIGNATURE_NOT_VALID = 2
54
44
SIGNATURE_NOT_SIGNED = 3
 
45
SIGNATURE_EXPIRED = 4
55
46
 
56
47
 
57
48
class DisabledGPGStrategy(object):
76
67
 
77
68
 
78
69
class LoopbackGPGStrategy(object):
79
 
    """A GPG Strategy that acts like 'cat' - data is just passed through."""
 
70
    """A GPG Strategy that acts like 'cat' - data is just passed through.
 
71
    Used in tests.
 
72
    """
80
73
 
81
74
    @staticmethod
82
75
    def verify_signatures_available():
106
99
        count = {SIGNATURE_VALID: 0,
107
100
                 SIGNATURE_KEY_MISSING: 0,
108
101
                 SIGNATURE_NOT_VALID: 0,
109
 
                 SIGNATURE_NOT_SIGNED: 0}
 
102
                 SIGNATURE_NOT_SIGNED: 0,
 
103
                 SIGNATURE_EXPIRED: 0}
110
104
        result = []
111
105
        all_verifiable = True
112
106
        for rev_id in revisions:
119
113
        return (count, result, all_verifiable)
120
114
 
121
115
    def valid_commits_message(self, count):
122
 
        return i18n.gettext("{0} commits with valid signatures").format(
 
116
        return gettext(u"{0} commits with valid signatures").format(
123
117
                                        count[SIGNATURE_VALID])            
124
118
 
125
119
    def unknown_key_message(self, count):
126
 
        return i18n.ngettext("{0} commit with unknown key",
127
 
                             "{0} commits with unknown keys",
 
120
        return ngettext(u"{0} commit with unknown key",
 
121
                             u"{0} commits with unknown keys",
128
122
                             count[SIGNATURE_KEY_MISSING]).format(
129
123
                                        count[SIGNATURE_KEY_MISSING])
130
124
 
131
125
    def commit_not_valid_message(self, count):
132
 
        return i18n.ngettext("{0} commit not valid",
133
 
                             "{0} commits not valid",
 
126
        return ngettext(u"{0} commit not valid",
 
127
                             u"{0} commits not valid",
134
128
                             count[SIGNATURE_NOT_VALID]).format(
135
129
                                            count[SIGNATURE_NOT_VALID])
136
130
 
137
131
    def commit_not_signed_message(self, count):
138
 
        return i18n.ngettext("{0} commit not signed",
139
 
                             "{0} commits not signed",
 
132
        return ngettext(u"{0} commit not signed",
 
133
                             u"{0} commits not signed",
140
134
                             count[SIGNATURE_NOT_SIGNED]).format(
141
135
                                        count[SIGNATURE_NOT_SIGNED])
142
136
 
 
137
    def expired_commit_message(self, count):
 
138
        return ngettext(u"{0} commit with key now expired",
 
139
                        u"{0} commits with key now expired",
 
140
                        count[SIGNATURE_EXPIRED]).format(
 
141
                                        count[SIGNATURE_EXPIRED])
 
142
 
143
143
 
144
144
def _set_gpg_tty():
145
145
    tty = os.environ.get('TTY')
161
161
 
162
162
    @staticmethod
163
163
    def verify_signatures_available():
 
164
        """
 
165
        check if this strategy can verify signatures
 
166
 
 
167
        :return: boolean if this strategy can verify signatures
 
168
        """
164
169
        try:
165
170
            import gpgme
166
171
            return True
168
173
            return False
169
174
 
170
175
    def _command_line(self):
171
 
        return [self._config.gpg_signing_command(), '--clearsign']
 
176
        
 
177
        return [self._config.gpg_signing_command(), '--clearsign', '-u',
 
178
                                                self._config.gpg_signing_key()]
172
179
 
173
180
    def __init__(self, config):
174
181
        self._config = config
235
242
        except gpgme.GpgmeError,error:
236
243
            raise errors.SignatureVerificationFailed(error[2])
237
244
 
 
245
        # No result if input is invalid.
 
246
        # test_verify_invalid()
238
247
        if len(result) == 0:
239
248
            return SIGNATURE_NOT_VALID, None
 
249
        # User has specified a list of acceptable keys, check our result is in it.
 
250
        # test_verify_unacceptable_key()
240
251
        fingerprint = result[0].fpr
241
252
        if self.acceptable_keys is not None:
242
 
            if not fingerprint in self.acceptable_keys:
 
253
            if not fingerprint in self.acceptable_keys:                
243
254
                return SIGNATURE_KEY_MISSING, fingerprint[-8:]
 
255
        # Check the signature actually matches the testament.
 
256
        # test_verify_bad_testament()
244
257
        if testament != plain_output.getvalue():
245
 
            return SIGNATURE_NOT_VALID, None
 
258
            return SIGNATURE_NOT_VALID, None 
 
259
        # Yay gpgme set the valid bit.
 
260
        # Can't write a test for this one as you can't set a key to be
 
261
        # trusted using gpgme.
246
262
        if result[0].summary & gpgme.SIGSUM_VALID:
247
263
            key = self.context.get_key(fingerprint)
248
264
            name = key.uids[0].name
249
265
            email = key.uids[0].email
250
266
            return SIGNATURE_VALID, name + " <" + email + ">"
 
267
        # Sigsum_red indicates a problem, unfortunatly I have not been able
 
268
        # to write any tests which actually set this.
251
269
        if result[0].summary & gpgme.SIGSUM_RED:
252
270
            return SIGNATURE_NOT_VALID, None
 
271
        # GPG does not know this key.
 
272
        # test_verify_unknown_key()
253
273
        if result[0].summary & gpgme.SIGSUM_KEY_MISSING:
254
274
            return SIGNATURE_KEY_MISSING, fingerprint[-8:]
255
 
        #summary isn't set if sig is valid but key is untrusted
 
275
        # Summary isn't set if sig is valid but key is untrusted
 
276
        # but if user has explicity set the key as acceptable we can validate it.
256
277
        if result[0].summary == 0 and self.acceptable_keys is not None:
257
278
            if fingerprint in self.acceptable_keys:
258
 
                return SIGNATURE_VALID, None
259
 
        else:
260
 
            return SIGNATURE_KEY_MISSING, None
 
279
                # test_verify_untrusted_but_accepted()
 
280
                return SIGNATURE_VALID, None 
 
281
        # test_verify_valid_but_untrusted()
 
282
        if result[0].summary == 0 and self.acceptable_keys is None:
 
283
            return SIGNATURE_NOT_VALID, None
 
284
        if result[0].summary & gpgme.SIGSUM_KEY_EXPIRED:
 
285
            expires = self.context.get_key(result[0].fpr).subkeys[0].expires
 
286
            if expires > result[0].timestamp:
 
287
                # The expired key was not expired at time of signing.
 
288
                # test_verify_expired_but_valid()
 
289
                return SIGNATURE_EXPIRED, fingerprint[-8:]
 
290
            else:
 
291
                # I can't work out how to create a test where the signature
 
292
                # was expired at the time of signing.
 
293
                return SIGNATURE_NOT_VALID, None
 
294
        # A signature from a revoked key gets this.
 
295
        # test_verify_revoked_signature()
 
296
        if result[0].summary & gpgme.SIGSUM_SYS_ERROR:
 
297
            return SIGNATURE_NOT_VALID, None
 
298
        # Other error types such as revoked keys should (I think) be caught by
 
299
        # SIGSUM_RED so anything else means something is buggy.
261
300
        raise errors.SignatureVerificationFailed("Unknown GnuPG key "\
262
301
                                                 "verification result")
263
302
 
274
313
            if isinstance(acceptable_keys_config, unicode):
275
314
                acceptable_keys_config = str(acceptable_keys_config)
276
315
        except UnicodeEncodeError:
277
 
            raise errors.BzrCommandError('Only ASCII permitted in option names')
 
316
            #gpg Context.keylist(pattern) does not like unicode
 
317
            raise errors.BzrCommandError(gettext('Only ASCII permitted in option names'))
278
318
 
279
319
        if acceptable_keys_config is not None:
280
320
            key_patterns = acceptable_keys_config
292
332
                    self.acceptable_keys.append(key.subkeys[0].fpr)
293
333
                    trace.mutter("Added acceptable key: " + key.subkeys[0].fpr)
294
334
                if not found_key:
295
 
                    trace.note(i18n.gettext(
296
 
                            "No GnuPG key results for pattern: {}"
 
335
                    trace.note(gettext(
 
336
                            "No GnuPG key results for pattern: {0}"
297
337
                                ).format(pattern))
298
338
 
299
 
    def do_verifications(self, revisions, repository):
 
339
    def do_verifications(self, revisions, repository,
 
340
                            process_events_callback=None):
300
341
        """do verifications on a set of revisions
301
342
        
302
343
        :param revisions: list of revision ids to verify
303
344
        :param repository: repository object
 
345
        :param process_events_callback: method to call for GUI frontends that
 
346
                                                want to keep their UI refreshed
304
347
        
305
348
        :return: count dictionary of results of each type,
306
349
                 result list for each revision,
309
352
        count = {SIGNATURE_VALID: 0,
310
353
                 SIGNATURE_KEY_MISSING: 0,
311
354
                 SIGNATURE_NOT_VALID: 0,
312
 
                 SIGNATURE_NOT_SIGNED: 0}
 
355
                 SIGNATURE_NOT_SIGNED: 0,
 
356
                 SIGNATURE_EXPIRED: 0}
313
357
        result = []
314
358
        all_verifiable = True
315
359
        for rev_id in revisions:
319
363
            count[verification_result] += 1
320
364
            if verification_result != SIGNATURE_VALID:
321
365
                all_verifiable = False
 
366
            if process_events_callback is not None:
 
367
                process_events_callback()
322
368
        return (count, result, all_verifiable)
323
369
 
324
370
    def verbose_valid_message(self, result):
330
376
                signers[uid] += 1
331
377
        result = []
332
378
        for uid, number in signers.items():
333
 
             result.append( i18n.ngettext("{0} signed {1} commit", 
334
 
                             "{0} signed {1} commits",
 
379
             result.append( ngettext(u"{0} signed {1} commit", 
 
380
                             u"{0} signed {1} commits",
335
381
                             number).format(uid, number) )
336
382
        return result
337
383
 
347
393
                signers[authors] += 1
348
394
        result = []
349
395
        for authors, number in signers.items():
350
 
            result.append( i18n.ngettext("{0} commit by author {1}", 
351
 
                                 "{0} commits by author {1}",
 
396
            result.append( ngettext(u"{0} commit by author {1}", 
 
397
                                 u"{0} commits by author {1}",
352
398
                                 number).format(number, authors) )
353
399
        return result
354
400
 
363
409
                signers[authors] += 1
364
410
        result = []
365
411
        for authors, number in signers.items():
366
 
            result.append( i18n.ngettext("{0} commit by author {1}", 
367
 
                                 "{0} commits by author {1}",
 
412
            result.append( ngettext(u"{0} commit by author {1}", 
 
413
                                 u"{0} commits by author {1}",
368
414
                                 number).format(number, authors) )
369
415
        return result
370
416
 
377
423
                signers[fingerprint] += 1
378
424
        result = []
379
425
        for fingerprint, number in signers.items():
380
 
            result.append( i18n.ngettext("Unknown key {0} signed {1} commit", 
381
 
                                 "Unknown key {0} signed {1} commits",
 
426
            result.append( ngettext(u"Unknown key {0} signed {1} commit", 
 
427
                                 u"Unknown key {0} signed {1} commits",
382
428
                                 number).format(fingerprint, number) )
383
429
        return result
384
430
 
 
431
    def verbose_expired_key_message(self, result, repo):
 
432
        """takes a verify result and returns list of expired key info"""
 
433
        signers = {}
 
434
        fingerprint_to_authors = {}
 
435
        for rev_id, validity, fingerprint in result:
 
436
            if validity == SIGNATURE_EXPIRED:
 
437
                revision = repo.get_revision(rev_id)
 
438
                authors = ', '.join(revision.get_apparent_authors())
 
439
                signers.setdefault(fingerprint, 0)
 
440
                signers[fingerprint] += 1
 
441
                fingerprint_to_authors[fingerprint] = authors
 
442
        result = []
 
443
        for fingerprint, number in signers.items():
 
444
            result.append(ngettext(u"{0} commit by author {1} with "\
 
445
                                    "key {2} now expired", 
 
446
                                   u"{0} commits by author {1} with key {2} now "\
 
447
                                    "expired",
 
448
                                    number).format(number,
 
449
                            fingerprint_to_authors[fingerprint], fingerprint) )
 
450
        return result
 
451
 
385
452
    def valid_commits_message(self, count):
386
453
        """returns message for number of commits"""
387
 
        return i18n.gettext("{0} commits with valid signatures").format(
 
454
        return gettext(u"{0} commits with valid signatures").format(
388
455
                                        count[SIGNATURE_VALID])
389
456
 
390
457
    def unknown_key_message(self, count):
391
458
        """returns message for number of commits"""
392
 
        return i18n.ngettext("{0} commit with unknown key",
393
 
                             "{0} commits with unknown keys",
394
 
                             count[SIGNATURE_KEY_MISSING]).format(
 
459
        return ngettext(u"{0} commit with unknown key",
 
460
                        u"{0} commits with unknown keys",
 
461
                        count[SIGNATURE_KEY_MISSING]).format(
395
462
                                        count[SIGNATURE_KEY_MISSING])
396
463
 
397
464
    def commit_not_valid_message(self, count):
398
465
        """returns message for number of commits"""
399
 
        return i18n.ngettext("{0} commit not valid",
400
 
                             "{0} commits not valid",
401
 
                             count[SIGNATURE_NOT_VALID]).format(
 
466
        return ngettext(u"{0} commit not valid",
 
467
                        u"{0} commits not valid",
 
468
                        count[SIGNATURE_NOT_VALID]).format(
402
469
                                            count[SIGNATURE_NOT_VALID])
403
470
 
404
471
    def commit_not_signed_message(self, count):
405
472
        """returns message for number of commits"""
406
 
        return i18n.ngettext("{0} commit not signed",
407
 
                             "{0} commits not signed",
408
 
                             count[SIGNATURE_NOT_SIGNED]).format(
 
473
        return ngettext(u"{0} commit not signed",
 
474
                        u"{0} commits not signed",
 
475
                        count[SIGNATURE_NOT_SIGNED]).format(
409
476
                                        count[SIGNATURE_NOT_SIGNED])
 
477
 
 
478
    def expired_commit_message(self, count):
 
479
        """returns message for number of commits"""
 
480
        return ngettext(u"{0} commit with key now expired",
 
481
                        u"{0} commits with key now expired",
 
482
                        count[SIGNATURE_EXPIRED]).format(
 
483
                                    count[SIGNATURE_EXPIRED])