-
Notifications
You must be signed in to change notification settings - Fork 8
/
GPGKey.go
159 lines (132 loc) · 4.53 KB
/
GPGKey.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package pg
import (
"github.com/jmoiron/sqlx"
"github.com/quan-to/chevron/pkg/models"
)
// database calls
// UpdateGPGKey updates the specified GPG key by using it's ID
func (h *PostgreSQLDBDriver) UpdateGPGKey(key models.GPGKey) (err error) {
h.log.Debug("UpdateGPGKey(%s)", key.FullFingerprint)
tx, err := h.conn.Beginx()
if err != nil {
return err
}
defer func() { h.rollbackIfErrorCommitIfNot(err, tx) }()
return h.updateGPGKey(tx, key)
}
// DeleteGPGKey deletes the specified GPG key by using it's ID
func (h *PostgreSQLDBDriver) DeleteGPGKey(key models.GPGKey) error {
h.log.Debug("DeleteGPGKey(%s)", key.FullFingerprint)
tx, err := h.conn.Beginx()
if err != nil {
return err
}
defer func() { h.rollbackIfErrorCommitIfNot(err, tx) }()
return h.deleteGPGKey(tx, key)
}
// AddGPGKey adds a GPG Key to the database or update an existing one by fingerprint
// Returns generated id / hasBeenAdded / error
func (h *PostgreSQLDBDriver) AddGPGKey(key models.GPGKey) (string, bool, error) {
h.log.Debug("AddGPGKey(%s)", key.FullFingerprint)
tx, err := h.conn.Beginx()
if err != nil {
return "", false, err
}
defer func() { h.rollbackIfErrorCommitIfNot(err, tx) }()
return h.addGPGKey(tx, key)
}
// FetchGPGKeysWithoutSubKeys fetch all keys that does not have a subkey
// This query is not implemented on PostgreSQL
func (h *PostgreSQLDBDriver) FetchGPGKeysWithoutSubKeys() (res []models.GPGKey, err error) {
h.log.Debug("FetchGPGKeysWithoutSubKeys()")
tx, err := h.conn.Beginx()
if err != nil {
return nil, err
}
defer func() { h.rollbackIfErrorCommitIfNot(err, tx) }()
keys, err := h.fetchGPGKeysWithoutSubKeys(tx)
if err != nil {
return nil, err
}
return convertArray(keys, tx)
}
// FetchGPGKeyByFingerprint fetch a GPG Key by its fingerprint
func (h *PostgreSQLDBDriver) FetchGPGKeyByFingerprint(fingerprint string) (*models.GPGKey, error) {
h.log.Debug("FetchGPGKeyByFingerprint(%s)", fingerprint)
tx, err := h.conn.Beginx()
if err != nil {
return nil, err
}
defer func() { h.rollbackIfErrorCommitIfNot(err, tx) }()
k, err := h.fetchGPGKeyByFingerprint(tx, fingerprint)
if err != nil {
return nil, err
}
return k.toGPGKey(tx)
}
// FindGPGKeyByEmail find all keys that has a underlying UID that contains that email
func (h *PostgreSQLDBDriver) FindGPGKeyByEmail(email string, pageStart, pageEnd int) ([]models.GPGKey, error) {
h.log.Debug("FindGPGKeyByEmail(%s, %d, %d)", email, pageStart, pageEnd)
tx, err := h.conn.Beginx()
if err != nil {
return nil, err
}
defer func() { h.rollbackIfErrorCommitIfNot(err, tx) }()
keys, err := h.findGPGKeyByEmail(tx, email, pageStart, pageEnd)
if err != nil {
return nil, err
}
return convertArray(keys, tx)
}
// FindGPGKeyByFingerPrint find all keys that has a fingerprint that matches the specified fingerprint
func (h *PostgreSQLDBDriver) FindGPGKeyByFingerPrint(fingerPrint string, pageStart, pageEnd int) ([]models.GPGKey, error) {
h.log.Debug("FindGPGKeyByFingerPrint(%s, %d, %d)", fingerPrint, pageStart, pageEnd)
tx, err := h.conn.Beginx()
if err != nil {
return nil, err
}
defer func() { h.rollbackIfErrorCommitIfNot(err, tx) }()
keys, err := h.findGPGKeyByFingerPrint(tx, fingerPrint, pageStart, pageEnd)
if err != nil {
return nil, err
}
return convertArray(keys, tx)
}
// FindGPGKeyByValue find all keys that has a underlying UID that contains that email, name or fingerprint specified by value
func (h *PostgreSQLDBDriver) FindGPGKeyByValue(value string, pageStart, pageEnd int) ([]models.GPGKey, error) {
h.log.Debug("FindGPGKeyByValue(%s, %d, %d)", value, pageStart, pageEnd)
tx, err := h.conn.Beginx()
if err != nil {
return nil, err
}
defer func() { h.rollbackIfErrorCommitIfNot(err, tx) }()
keys, err := h.findGPGKeyByValue(tx, value, pageStart, pageEnd)
if err != nil {
return nil, err
}
return convertArray(keys, tx)
}
// FindGPGKeyByName find all keys that has a underlying UID that contains that name
func (h *PostgreSQLDBDriver) FindGPGKeyByName(name string, pageStart, pageEnd int) ([]models.GPGKey, error) {
h.log.Debug("FindGPGKeyByName(%s, %d, %d)", name, pageStart, pageEnd)
tx, err := h.conn.Beginx()
if err != nil {
return nil, err
}
defer func() { h.rollbackIfErrorCommitIfNot(err, tx) }()
keys, err := h.findGPGKeyByName(tx, name, pageStart, pageEnd)
if err != nil {
return nil, err
}
return convertArray(keys, tx)
}
func convertArray(keys []pgGPGKey, tx *sqlx.Tx) (res []models.GPGKey, err error) {
for _, v := range keys {
k, err := v.toGPGKey(tx)
if err != nil {
return nil, err
}
res = append(res, *k)
}
return res, nil
}