forked from canonical/lxd
/
db_certificates.go
137 lines (117 loc) · 2.68 KB
/
db_certificates.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
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
// dbCertInfo is here to pass the certificates content
// from the database around
type dbCertInfo struct {
ID int
Fingerprint string
Type int
Name string
Certificate string
}
// dbCertsGet returns all certificates from the DB as CertBaseInfo objects.
func dbCertsGet(db *sql.DB) (certs []*dbCertInfo, err error) {
rows, err := dbQuery(
db,
"SELECT id, fingerprint, type, name, certificate FROM certificates",
)
if err != nil {
return certs, err
}
defer rows.Close()
for rows.Next() {
cert := new(dbCertInfo)
rows.Scan(
&cert.ID,
&cert.Fingerprint,
&cert.Type,
&cert.Name,
&cert.Certificate,
)
certs = append(certs, cert)
}
return certs, nil
}
// dbCertGet gets an CertBaseInfo object from the database.
// The argument fingerprint will be queried with a LIKE query, means you can
// pass a shortform and will get the full fingerprint.
// There can never be more than one image with a given fingerprint, as it is
// enforced by a UNIQUE constraint in the schema.
func dbCertGet(db *sql.DB, fingerprint string) (cert *dbCertInfo, err error) {
cert = new(dbCertInfo)
inargs := []interface{}{fingerprint + "%"}
outfmt := []interface{}{
&cert.ID,
&cert.Fingerprint,
&cert.Type,
&cert.Name,
&cert.Certificate,
}
query := `
SELECT
id, fingerprint, type, name, certificate
FROM
certificates
WHERE fingerprint LIKE ?`
if err = dbQueryRowScan(db, query, inargs, outfmt); err != nil {
return nil, err
}
return cert, err
}
// dbCertSave stores a CertBaseInfo object in the db,
// it will ignore the ID field from the dbCertInfo.
func dbCertSave(db *sql.DB, cert *dbCertInfo) error {
tx, err := dbBegin(db)
if err != nil {
return err
}
stmt, err := tx.Prepare(`
INSERT INTO certificates (
fingerprint,
type,
name,
certificate
) VALUES (?, ?, ?, ?)`,
)
if err != nil {
tx.Rollback()
return err
}
defer stmt.Close()
_, err = stmt.Exec(
cert.Fingerprint,
cert.Type,
cert.Name,
cert.Certificate,
)
if err != nil {
tx.Rollback()
return err
}
return txCommit(tx)
}
// dbCertDelete deletes a certificate from the db.
func dbCertDelete(db *sql.DB, fingerprint string) error {
_, err := dbExec(
db,
"DELETE FROM certificates WHERE fingerprint=?",
fingerprint,
)
return err
}
func dbCertUpdate(db *sql.DB, fingerprint string, certName string, certType int) error {
tx, err := dbBegin(db)
if err != nil {
return err
}
_, err = tx.Exec("UPDATE certificates SET name=?, type=? WHERE fingerprint=?", certName, certType, fingerprint)
if err != nil {
tx.Rollback()
return err
}
err = txCommit(tx)
return err
}