~cmars/hockeypuck/master

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
/*
   Hockeypuck - OpenPGP key server
   Copyright (C) 2012-2014  Casey Marshall

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, version 3.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package openpgp

import (
	"fmt"

	"github.com/jmoiron/sqlx"

	"launchpad.net/hockeypuck/util"
)

type Loader struct {
	db   *DB
	tx   *sqlx.Tx
	bulk bool
}

func NewLoader(db *DB, bulk bool) *Loader {
	return &Loader{db: db, bulk: bulk}
}

func (l *Loader) Begin() (_ *sqlx.Tx, err error) {
	l.tx, err = l.db.Beginx()
	return l.tx, err
}

func (l *Loader) Commit() (err error) {
	if err = l.tx.Commit(); err != nil {
		return
	}
	return
}

func (l *Loader) Rollback() (err error) {
	err = l.tx.Rollback()
	return
}

func (l *Loader) InsertKey(pubkey *Pubkey) (err error) {
	var signable PacketRecord
	err = pubkey.Visit(func(rec PacketRecord) error {
		switch r := rec.(type) {
		case *Pubkey:
			if err := l.insertPubkey(r); err != nil {
				return err
			}
			signable = r
		case *Subkey:
			if err := l.insertSubkey(pubkey, r); err != nil {
				return err
			}
			signable = r
		case *UserId:
			if err := l.insertUid(pubkey, r); err != nil {
				return err
			}
			signable = r
		case *UserAttribute:
			if err := l.insertUat(pubkey, r); err != nil {
				return err
			}
			signable = r
		case *Signature:
			if err := l.insertSig(pubkey, r); err != nil {
				return err
			}
			if err := l.insertSigRelations(pubkey, signable, r); err != nil {
				return err
			}
		}
		return nil
	})
	return err
}

// insertSelectFrom completes an INSERT INTO .. SELECT FROM
// SQL statement based on the loader's bulk loading mode.
func (l *Loader) insertSelectFrom(sql, table, where string) string {
	if !l.bulk {
		sql = fmt.Sprintf("%s WHERE NOT EXISTS (SELECT 1 FROM %s WHERE %s)",
			sql, table, where)
	}
	return sql
}

func (l *Loader) insertPubkey(r *Pubkey) error {
	_, err := l.tx.Execv(l.insertSelectFrom(`
INSERT INTO openpgp_pubkey (
	uuid, creation, expiration, state, packet,
	ctime, mtime,
    md5, sha256, revsig_uuid, primary_uid, primary_uat,
	algorithm, bit_len, unsupp)
SELECT $1, $2, $3, $4, $5,
	now(), now(),
    $6, $7, $8, $9, $10,
	$11, $12, $13`,
		"openpgp_pubkey", "uuid = $1"),
		r.RFingerprint, r.Creation, r.Expiration, r.State, r.Packet,
		// TODO: use mtime and ctime from record, or use RETURNING to set it
		r.Md5, r.Sha256, r.RevSigDigest, r.PrimaryUid, r.PrimaryUat,
		r.Algorithm, r.BitLen, r.Unsupported)
	return err
}

func (l *Loader) insertSubkey(pubkey *Pubkey, r *Subkey) error {
	_, err := l.tx.Execv(l.insertSelectFrom(`
INSERT INTO openpgp_subkey (
	uuid, creation, expiration, state, packet,
	pubkey_uuid, revsig_uuid, algorithm, bit_len)
SELECT $1, $2, $3, $4, $5,
	$6, $7, $8, $9`,
		"openpgp_subkey", "uuid = $1"),
		r.RFingerprint, r.Creation, r.Expiration, r.State, r.Packet,
		pubkey.RFingerprint, r.RevSigDigest, r.Algorithm, r.BitLen)
	return err
}

func (l *Loader) insertUid(pubkey *Pubkey, r *UserId) error {
	_, err := l.tx.Execv(l.insertSelectFrom(`
INSERT INTO openpgp_uid (
	uuid, creation, expiration, state, packet,
	pubkey_uuid, revsig_uuid, keywords, keywords_fulltext)
SELECT $1, $2, $3, $4, $5,
	$6, $7, $8, to_tsvector($8)`,
		"openpgp_uid", "uuid = $1"),
		r.ScopedDigest, r.Creation, r.Expiration, r.State, r.Packet,
		pubkey.RFingerprint, r.RevSigDigest, util.CleanUtf8(r.Keywords))
	return err
}

func (l *Loader) insertUat(pubkey *Pubkey, r *UserAttribute) error {
	_, err := l.tx.Execv(l.insertSelectFrom(`
INSERT INTO openpgp_uat (
	uuid, creation, expiration, state, packet,
	pubkey_uuid, revsig_uuid)
SELECT $1, $2, $3, $4, $5,
	$6, $7`,
		"openpgp_uat", "uuid = $1"),
		r.ScopedDigest, r.Creation, r.Expiration, r.State, r.Packet,
		pubkey.RFingerprint, r.RevSigDigest)
	return err
}

func (l *Loader) insertSig(pubkey *Pubkey, r *Signature) error {
	_, err := l.tx.Execv(l.insertSelectFrom(`
INSERT INTO openpgp_sig (
	uuid, creation, expiration, state, packet,
	sig_type, signer, signer_uuid)
SELECT $1, $2, $3, $4, $5, $6, $7, $8`,
		"openpgp_sig", "uuid = $1"),
		r.ScopedDigest, r.Creation, r.Expiration, r.State, r.Packet,
		r.SigType, r.RIssuerKeyId, r.RIssuerFingerprint)
	// TODO: use RETURNING to update matched issuer fingerprint
	return err
}

func (l *Loader) insertSigRelations(pubkey *Pubkey, signable PacketRecord, r *Signature) error {
	sigRelationUuid, err := NewUuid()
	if err != nil {
		return err
	}
	// Add signature relation to other packets
	switch signed := signable.(type) {
	case *Pubkey:
		_, err = l.tx.Execv(l.insertSelectFrom(`
INSERT INTO openpgp_pubkey_sig (uuid, pubkey_uuid, sig_uuid)
SELECT $1, $2, $3`,
			"openpgp_pubkey_sig", "pubkey_uuid = $2 AND sig_uuid = $3"),
			sigRelationUuid, signed.RFingerprint, r.ScopedDigest)
	case *Subkey:
		_, err = l.tx.Execv(l.insertSelectFrom(`
INSERT INTO openpgp_subkey_sig (uuid, pubkey_uuid, subkey_uuid, sig_uuid)
SELECT $1, $2, $3, $4`,
			"openpgp_subkey_sig", "pubkey_uuid = $2 AND subkey_uuid = $3 AND sig_uuid = $4"),
			sigRelationUuid, pubkey.RFingerprint,
			signed.RFingerprint, r.ScopedDigest)
	case *UserId:
		_, err = l.tx.Execv(l.insertSelectFrom(`
INSERT INTO openpgp_uid_sig (uuid, pubkey_uuid, uid_uuid, sig_uuid)
SELECT $1, $2, $3, $4`,
			"openpgp_uid_sig", "pubkey_uuid = $2 AND uid_uuid = $3 AND sig_uuid = $4"),
			sigRelationUuid, pubkey.RFingerprint,
			signed.ScopedDigest, r.ScopedDigest)
	case *UserAttribute:
		_, err = l.tx.Execv(l.insertSelectFrom(`
INSERT INTO openpgp_uat_sig (uuid, pubkey_uuid, uat_uuid, sig_uuid)
SELECT $1, $2, $3, $4`,
			"openpgp_uat_sig", "pubkey_uuid = $2 AND uat_uuid = $3 AND sig_uuid = $4"),
			sigRelationUuid, pubkey.RFingerprint,
			signed.ScopedDigest, r.ScopedDigest)
	}
	return err
}