summaryrefslogtreecommitdiff
path: root/devel/management/commands/pgp_import.py
blob: 3f557fe92a5e9178861a53e53d04e1d5e4f84c45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# -*- coding: utf-8 -*-
"""
pgp_import command

Import keys and signatures from a given GPG keyring.

Usage: ./manage.py pgp_import <keyring_path>
"""

from collections import namedtuple, OrderedDict
from datetime import datetime
import logging
from pytz import utc
import subprocess
import sys

from django.core.management.base import BaseCommand, CommandError
from django.db import transaction

from devel.models import DeveloperKey, PGPSignature
from devel.utils import UserFinder


logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s -> %(levelname)s: %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S',
    stream=sys.stderr)
logger = logging.getLogger()

class Command(BaseCommand):
    args = "<keyring_path>"
    help = "Import keys and signatures from a given GPG keyring."

    def handle(self, *args, **options):
        v = int(options.get('verbosity', None))
        if v == 0:
            logger.level = logging.ERROR
        elif v == 1:
            logger.level = logging.INFO
        elif v == 2:
            logger.level = logging.DEBUG

        if len(args) < 1:
            raise CommandError("keyring_path must be provided")

        import_keys(args[0])
        import_signatures(args[0])


def get_date(epoch_string):
    '''Convert a epoch string into a python 'date' object (not datetime).'''
    if not epoch_string:
        return None
    return datetime.utcfromtimestamp(int(epoch_string)).date()


def get_datetime(epoch_string):
    '''Convert a epoch string into a python 'datetime' object.'''
    if not epoch_string:
        return None
    return datetime.utcfromtimestamp(int(epoch_string)).replace(tzinfo=utc)


def call_gpg(keyring, *args):
    # GPG is stupid and interprets any filename without path portion as being
    # in ~/.gnupg/. Fake it out if we just get a bare filename.
    if '/' not in keyring:
        keyring = './%s' % keyring
    gpg_cmd = ["gpg2", "--no-default-keyring", "--keyring", keyring,
            "--with-colons", "--fixed-list-mode"]
    gpg_cmd.extend(args)
    logger.info("running command: %s", ' '.join(gpg_cmd))
    proc = subprocess.Popen(gpg_cmd, stdout=subprocess.PIPE)
    outdata, errdata = proc.communicate()
    if proc.returncode != 0:
        logger.error(errdata)
        raise subprocess.CalledProcessError(proc.returncode, gpg_cmd)
    return outdata


class KeyData(object):
    def __init__(self, key, created, expires):
        self.key = key
        self.created = get_datetime(created)
        self.expires = get_datetime(expires)
        self.parent = None
        self.revoked = None
        self.db_id = None


def parse_keydata(data):
    keys = OrderedDict()
    current_pubkey = None

    # parse all of the output from our successful GPG command
    logger.info("parsing command output")
    node = None
    for line in data.split('\n'):
        parts = line.split(':')
        if parts[0] == 'pub':
            key = parts[4]
            current_pubkey = key
            keys[key] = KeyData(key, parts[5], parts[6])
            node = parts[0]
        elif parts[0] == 'sub':
            key = parts[4]
            keys[key] = KeyData(key, parts[5], parts[6])
            keys[key].parent = current_pubkey
            node = parts[0]
        elif parts[0] == 'uid':
            node = parts[0]
        elif parts[0] == 'rev' and node in ('pub', 'sub'):
            keys[current_pubkey].revoked = get_datetime(parts[5])

    return keys


def find_key_owner(key, keys, finder):
    '''Recurse up the chain, looking for an owner.'''
    if key is None:
        return None
    owner = finder.find_by_pgp_key(key.key)
    if owner:
        return owner
    if key.parent:
        return find_key_owner(keys[key.parent], keys, finder)
    return None


def import_keys(keyring):
    outdata = call_gpg(keyring, "--list-sigs")
    keydata = parse_keydata(outdata)

    logger.info("creating or finding %d keys", len(keydata))
    created_ct = updated_ct = 0
    with transaction.atomic():
        finder = UserFinder()
        # we are dependent on parents coming before children; parse_keydata
        # uses an OrderedDict to ensure this is the case.
        for data in keydata.values():
            parent_id = None
            if data.parent:
                parent_data = keydata.get(data.parent, None)
                if parent_data:
                    parent_id = parent_data.db_id
            other = {
                'expires': data.expires,
                'revoked': data.revoked,
                'parent_id': parent_id,
            }
            dkey, created = DeveloperKey.objects.get_or_create(
                    key=data.key, created=data.created, defaults=other)
            data.db_id = dkey.id

            # set or update any additional data we might need to
            needs_save = False
            if created:
                created_ct += 1
            else:
                for k, v in other.items():
                    if getattr(dkey, k) != v:
                        setattr(dkey, k, v)
                        needs_save = True
            if dkey.owner_id is None:
                owner = find_key_owner(data, keydata, finder)
                if owner is not None:
                    dkey.owner = owner
                    needs_save = True
            if needs_save:
                dkey.save()
                updated_ct += 1

    key_ct = DeveloperKey.objects.all().count()
    logger.info("%d total keys in database", key_ct)
    logger.info("created %d, updated %d keys", created_ct, updated_ct)


class SignatureData(object):
    def __init__(self, signer, signee, created):
        self.signer = signer
        self.signee = signee
        self.created = created
        self.expires = None
        self.revoked = None


def parse_sigdata(data):
    nodes = {}
    edges = []
    current_pubkey = None

    # parse all of the output from our successful GPG command
    logger.info("parsing command output")
    for line in data.split('\n'):
        parts = line.split(':')
        if parts[0] == 'pub':
            current_pubkey = parts[4]
            nodes[current_pubkey] = None
        elif parts[0] == 'uid':
            uid = parts[9]
            # only set uid if this is the first one encountered
            if nodes[current_pubkey] is None:
                nodes[current_pubkey] = uid
        elif parts[0] == 'sig':
            signer = parts[4]
            created = get_date(parts[5])
            edge = SignatureData(signer, current_pubkey, created)
            if parts[6]:
                edge.expires = get_date(parts[6])
            edges.append(edge)
        elif parts[0] == 'rev':
            signer = parts[4]
            revoked = get_date(parts[5])
            # revoke any prior edges that match
            matches = [e for e in edges if e.signer == signer
                    and e.signee == current_pubkey]
            for edge in matches:
                edge.revoked = revoked

    return nodes, edges


def import_signatures(keyring):
    outdata = call_gpg(keyring, "--list-sigs")
    nodes, edges = parse_sigdata(outdata)

    # now prune the data down to what we actually want.
    # prune edges not in nodes, remove duplicates, and self-sigs
    pruned_edges = {edge for edge in edges
            if edge.signer in nodes and edge.signer != edge.signee}

    logger.info("creating or finding up to %d signatures", len(pruned_edges))
    created_ct = updated_ct = 0
    with transaction.atomic():
        for edge in pruned_edges:
            sig, created = PGPSignature.objects.get_or_create(
                    signer=edge.signer, signee=edge.signee,
                    created=edge.created, expires=edge.expires,
                    defaults={ 'revoked': edge.revoked })
            if sig.revoked != edge.revoked:
                sig.revoked = edge.revoked
                sig.save()
                updated_ct += 1
            if created:
                created_ct += 1

    sig_ct = PGPSignature.objects.all().count()
    logger.info("%d total signatures in database", sig_ct)
    logger.info("created %d, updated %d signatures", created_ct, updated_ct)

# vim: set ts=4 sw=4 et: