forked from mutecomm/mute
/
dbstorage.go
188 lines (171 loc) · 4.33 KB
/
dbstorage.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
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
// Copyright (c) 2015 Mute Communications Ltd.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package keydb implements a DB-based keypool storage backend.
package keydb
import (
"database/sql"
"encoding/base64"
"encoding/hex"
"errors"
_ "github.com/go-sql-driver/mysql" //
"github.com/mutecomm/mute/serviceguard/common/keypool"
"github.com/mutecomm/mute/serviceguard/common/signkeys"
)
var (
// ErrClosed is returned if trying to work with a closed DB connection
ErrClosed = errors.New("spendbook: DB is closed")
)
const (
createQuery = `
CREATE TABLE IF NOT EXISTS keypool (
KeyID VARCHAR(128),
KeyUsage VARCHAR(255),
KeyData TEXT,
CONSTRAINT KeyID UNIQUE (KeyID)
);`
selectQuery = `SELECT KeyData FROM keypool WHERE KeyID=?;`
insertQuery = `INSERT INTO keypool (KeyID, KeyUsage, KeyData) VALUES (?, ?, ?);`
loadQuery = `SELECT KeyData FROM keypool;`
)
// KeyDB contains a key storage in sql database
type KeyDB struct {
DB *sql.DB
selectQuery *sql.Stmt
insertQuery *sql.Stmt
loadQuery *sql.Stmt
mayClose bool
}
// New returns a new spendbook. Takes an existing database handle or URL
func New(db interface{}) (*KeyDB, error) {
if dbConn, ok := db.(*sql.DB); ok {
return NewFromDB(dbConn)
}
return NewFromURL(db.(string))
}
// NewFromDB returns a KeyDB from an existing database handler
func NewFromDB(db *sql.DB) (*KeyDB, error) {
if db == nil {
return nil, ErrClosed
}
kd := new(KeyDB)
kd.DB = db
kd.mayClose = false
err := kd.initDB()
if err != nil {
return nil, err
}
return kd, nil
}
// NewFromURL returns a KeyDB from a URL
func NewFromURL(dburl string) (*KeyDB, error) {
db, err := sql.Open("mysql", dburl)
if err != nil {
return nil, err
}
kd := new(KeyDB)
kd.DB = db
kd.mayClose = true
err = kd.initDB()
if err != nil {
return nil, err
}
return kd, nil
}
// Close the database
func (kd *KeyDB) Close() {
if kd.mayClose {
kd.DB.Close()
}
kd.DB = nil
}
func (kd *KeyDB) initDB() error {
var err error
kd.DB.Exec(createQuery)
kd.insertQuery, err = kd.DB.Prepare(insertQuery)
if err != nil {
return err
}
kd.selectQuery, err = kd.DB.Prepare(selectQuery)
if err != nil {
return err
}
kd.loadQuery, err = kd.DB.Prepare(loadQuery)
if err != nil {
return err
}
return nil
}
// Add keyDB to keypool storage handlers
func Add(kp *keypool.KeyPool, db interface{}) error {
keydb, err := New(db)
if err != nil {
return err
}
kp.RegisterStorage(keydb.fetchFunc(), keydb.writeFunc(), keydb.loadFunc())
return nil
}
// Add keyDB to keypool storage handlers
func (kd *KeyDB) Add(kp *keypool.KeyPool) error {
kp.RegisterStorage(kd.fetchFunc(), kd.writeFunc(), kd.loadFunc())
return nil
}
// writeFunc returns a function ready to write keys to storage
func (kd *KeyDB) writeFunc() keypool.WriteKeyCallbackFunc {
return func(keyid []byte, usage string, marshalledKey []byte) error {
id := hex.EncodeToString(keyid)
marshalled := base64.StdEncoding.EncodeToString(marshalledKey)
_, err := kd.insertQuery.Exec(id, usage, marshalled)
if err == nil {
return err
}
return nil
}
}
// Fetch fetches keyid from the database and returns the marshalled key
func (kd *KeyDB) Fetch(keyid []byte) (marshalledKey []byte, err error) {
var encodedKey string
id := hex.EncodeToString(keyid)
err = kd.selectQuery.QueryRow(id).Scan(&encodedKey)
if err != nil {
if err == sql.ErrNoRows {
return nil, keypool.ErrNotFound
}
return nil, err
}
marshalledKey, err = base64.StdEncoding.DecodeString(encodedKey)
if err != nil {
return nil, err
}
return marshalledKey, nil
}
// fetchFunc returns a function ready to fech a key from storage
func (kd *KeyDB) fetchFunc() keypool.FetchKeyCallBackFunc {
return kd.Fetch
}
// loadFunc returns a callback that loads keys from storage
func (kd *KeyDB) loadFunc() keypool.LoadKeysCallbackFunc {
return func(keypool *keypool.KeyPool) error {
rows, err := kd.loadQuery.Query()
if err != nil {
return err
}
for rows.Next() {
var KeyData string
err := rows.Scan(&KeyData)
if err != nil {
return err
}
marshalledKey, err := base64.StdEncoding.DecodeString(KeyData)
if err != nil {
return err
}
loadKey, err := new(signkeys.PublicKey).Unmarshal(marshalledKey)
if err != nil {
return err
}
keypool.LoadKey(loadKey) // ignore errors
}
return nil
}
}