-
Notifications
You must be signed in to change notification settings - Fork 8
/
GPGKeyTransactional.go
140 lines (110 loc) · 3.57 KB
/
GPGKeyTransactional.go
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
package pg
import (
"fmt"
"strings"
"github.com/jmoiron/sqlx"
"github.com/quan-to/chevron/pkg/models"
)
func (h *PostgreSQLDBDriver) updateGPGKey(tx *sqlx.Tx, key models.GPGKey) error {
gpgKey, err := h.fetchGPGKeyByFingerprint(tx, key.FullFingerprint)
if errorIsNotNilAndNotNotFound(err) {
return err
}
if gpgKey != nil {
// Preload UIDs
_, err = gpgKey.getKeyUids(tx)
if err != nil {
return err
}
// Update UIDs
err = gpgKey.updateUIDs(tx, key.KeyUids)
if err != nil {
return err
}
// Update Fields if needed
if gpgKey.fieldsChanged(key) {
gpgKey.KeyBits = key.KeyBits
gpgKey.ASCIIArmoredPublicKey = key.AsciiArmoredPublicKey
gpgKey.ASCIIArmoredPrivateKey = key.AsciiArmoredPrivateKey
err := gpgKey.save(tx)
if err != nil {
return err
}
}
return nil
}
gpgKey = pgGPGKeyFromGPGKey(key)
return gpgKey.save(tx)
}
func (h *PostgreSQLDBDriver) deleteGPGKey(tx *sqlx.Tx, key models.GPGKey) error {
pgKey := pgGPGKeyFromGPGKey(key)
_, err := tx.NamedExec(`DELETE FROM chevron_gpg_key WHERE gpg_key_id = :gpg_key_id`, &pgKey)
return err
}
// Transactional Methods
func (h *PostgreSQLDBDriver) addGPGKey(tx *sqlx.Tx, key models.GPGKey) (string, bool, error) {
existingKey, err := h.fetchGPGKeyByFingerprint(tx, key.FullFingerprint)
if errorIsNotNilAndNotNotFound(err) {
return "", false, err
}
if existingKey != nil {
key.ID = existingKey.ID
err := h.updateGPGKey(tx, key)
return existingKey.ID, false, err
}
key.ID = "" // Avoid bugs on inserting
pgKey := pgGPGKeyFromGPGKey(key)
err = pgKey.save(tx)
if err != nil {
return "", false, err
}
return pgKey.ID, true, nil
}
func (h *PostgreSQLDBDriver) fetchGPGKeysWithoutSubKeys(tx *sqlx.Tx) ([]pgGPGKey, error) {
// Not supported. This would be a heavy query and it isn't used as much
return nil, fmt.Errorf("not supported")
}
func (h *PostgreSQLDBDriver) fetchGPGKeyByFingerprint(tx *sqlx.Tx, fingerprint string) (*pgGPGKey, error) {
if len(fingerprint) < 16 {
return nil, fmt.Errorf("expected fingerprint length >= 16")
}
if len(fingerprint) > 16 {
fingerprint = fingerprint[len(fingerprint)-16:]
}
key := &pgGPGKey{}
err := tx.Get(key, "SELECT * FROM chevron_gpg_key WHERE gpg_key_fingerprint16 = $1 LIMIT 1", fingerprint)
if err != nil && !strings.EqualFold("sql: no rows in result set", err.Error()) {
return nil, err
}
if err != nil {
return nil, fmt.Errorf("not found")
}
return key, nil
}
func (h *PostgreSQLDBDriver) findGPGKeyByEmail(tx *sqlx.Tx, email string, pageStart, pageEnd int) (res []pgGPGKey, err error) {
return nil, fmt.Errorf("not supported") // Slow query
}
func (h *PostgreSQLDBDriver) findGPGKeyByFingerPrint(tx *sqlx.Tx, fingerPrint string, pageStart, pageEnd int) (res []pgGPGKey, err error) {
if pageStart < 0 {
pageStart = models.DefaultPageStart
}
if pageEnd < 0 {
pageEnd = models.DefaultPageEnd
}
numItems := pageEnd - pageStart
if len(fingerPrint) > 16 {
fingerPrint = fingerPrint[len(fingerPrint)-16:]
}
fingerPrint = strings.ToUpper("%" + fingerPrint)
err = tx.Select(&res, "SELECT * FROM chevron_gpg_key WHERE gpg_key_fingerprint16 LIKE $1 LIMIT $2 OFFSET $3", fingerPrint, numItems, pageStart)
if err != nil {
return nil, err
}
return res, nil
}
func (h *PostgreSQLDBDriver) findGPGKeyByValue(tx *sqlx.Tx, value string, pageStart, pageEnd int) (res []pgGPGKey, err error) {
return nil, fmt.Errorf("not supported") // Slow query
}
func (h *PostgreSQLDBDriver) findGPGKeyByName(tx *sqlx.Tx, name string, pageStart, pageEnd int) (res []pgGPGKey, err error) {
return nil, fmt.Errorf("not supported") // Slow query
}