This repository was archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathmanifest.go
110 lines (101 loc) · 3.03 KB
/
manifest.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: Aaron Meihm [email protected] [:alm]
package database /* import "github.com/mozilla/mig/database" */
import (
"fmt"
_ "github.com/lib/pq"
"github.com/mozilla/mig"
)
// Add a new manifest record to the database
func (db *DB) ManifestAdd(mr mig.ManifestRecord) (err error) {
_, err = db.c.Exec(`INSERT INTO manifests VALUES
(DEFAULT, $1, $2, now(), 'staged', $3)`, mr.Name,
mr.Content, mr.Target)
return
}
// Add a signature to the database for an existing manifest
func (db *DB) ManifestAddSignature(mid float64, sig string, invid float64, reqsig int) (err error) {
res, err := db.c.Exec(`INSERT INTO manifestsig
(manifestid, pgpsignature, investigatorid)
SELECT $1, $2, $3
WHERE EXISTS (SELECT id FROM manifests
WHERE id=$4 AND status!='disabled')`, mid, sig, invid, mid)
if err != nil {
return
}
ra, err := res.RowsAffected()
if err != nil {
return
}
if ra != 1 {
return fmt.Errorf("Manifest signing operation failed")
}
err = db.ManifestUpdateStatus(mid, reqsig)
return
}
// Disable a manifest record
func (db *DB) ManifestDisable(mid float64) (err error) {
_, err = db.c.Exec(`UPDATE manifests SET status='disabled' WHERE
id=$1`, mid)
if err != nil {
return
}
return
}
// Update the status of a manifest based on the number of signatures it has,
// reqsig is passed as an argument that indicates the number of signatures
// a manifest must have to be considered active
func (db *DB) ManifestUpdateStatus(mid float64, reqsig int) (err error) {
var cnt int
err = db.c.QueryRow(`SELECT COUNT(*) FROM manifestsig
WHERE manifestid=$1`, mid).Scan(&cnt)
if err != nil {
return err
}
status := "staged"
if cnt >= reqsig {
status = "active"
}
_, err = db.c.Exec(`UPDATE manifests SET status=$1 WHERE
id=$2 AND status!='disabled'`, status, mid)
if err != nil {
return err
}
return
}
// Clear existing signatures for a manifest record
func (db *DB) ManifestClearSignatures(mid float64) (err error) {
_, err = db.c.Exec(`DELETE FROM manifestsig WHERE manifestid=$1`, mid)
return err
}
// Return the entire contents of manifest ID mid from the database
func (db *DB) GetManifestFromID(mid float64) (ret mig.ManifestRecord, err error) {
row := db.c.QueryRow(`SELECT id, name, content, timestamp, status, target
FROM manifests WHERE id=$1`, mid)
err = row.Scan(&ret.ID, &ret.Name, &ret.Content, &ret.Timestamp, &ret.Status, &ret.Target)
if err != nil {
err = fmt.Errorf("Error while retrieving manifest: '%v'", err)
return
}
// Also add any signatures that exist for this manifest record
rows, err := db.c.Query(`SELECT pgpsignature FROM manifestsig
WHERE manifestid=$1`, mid)
if err != nil {
return
}
if rows != nil {
defer rows.Close()
}
for rows.Next() {
var s string
err = rows.Scan(&s)
if err != nil {
return ret, err
}
ret.Signatures = append(ret.Signatures, s)
}
return
}