/
secrets.go
304 lines (246 loc) · 7.46 KB
/
secrets.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package hal
/*
* Copyright 2016-2017 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"io"
"sync"
)
// secrets stores a plaintext key/value store for
// sensitive data that the bot and plugins need to operate
// along with methods for persisting encrypted copies to the database
type SecretStore struct {
key []byte // encryption key for persistence
keyed bool // track whether the key has been set
kv map[string]string // the in-memory k/v store
mut sync.Mutex // protect concurrent access
init sync.Once // singleton initialization
itbl sync.Once // table initialization
}
var secrets SecretStore
// SECRETS_TABLE holds encrypted key/value pairs along with their nonces.
// uses VARBINARY instead of BINARY to avoid null termination issues.
const SECRETS_TABLE = `
CREATE TABLE IF NOT EXISTS secrets (
pkey VARCHAR(191) NOT NULL, -- plaintext key
sval VARBINARY(16384) NOT NULL, -- AES/GCM sealed value
nonce VARBINARY(12) NOT NULL, -- GCM nonce for the value
ts TIMESTAMP, -- timestamp, for debugging/cleanup
PRIMARY KEY(pkey)
)`
// for temporarily holding encrypted data
type ssRec struct {
pkey []byte
sval []byte
nonce []byte
}
// 256-bit AES key and 96-bit nonce size in bytes
const KEY_SIZE = 32
const NONCE_SIZE = 12
// Secrets returns a handle for accessing secrets managed by hal.
func Secrets() *SecretStore {
secrets.init.Do(func() {
secrets.kv = make(map[string]string)
secrets.key = make([]byte, KEY_SIZE)
secrets.keyed = false
})
return &secrets
}
// SetEncryptionKey sets the key used to encrypt/decrypt credentials
// stored in the database. This needs to be called before anything
// will work.
func (ss *SecretStore) SetEncryptionKey(in []byte) {
ss.mut.Lock()
defer ss.mut.Unlock()
// do not rely on the caller's memory: make a copy
done := copy(ss.key, in)
// catch unlikely errors and anyone trying to use a smaller key
if done != KEY_SIZE {
log.Fatalf("BUG: SetEncryptionKey failed to store the key. Only %d bytes copied.", done)
}
ss.keyed = true
}
// Get returns the value of a key from the secret store.
// If the key doesn't exist, empty string is returned.
// To check existence, use Exists(string).
func (ss *SecretStore) Get(key string) string {
ss.mut.Lock()
defer ss.mut.Unlock()
if _, exists := ss.kv[key]; exists {
return ss.kv[key]
} else {
return ""
}
}
// Exists checks to see if the provided key exists
// in the secret store.
func (ss *SecretStore) Exists(key string) bool {
ss.mut.Lock()
defer ss.mut.Unlock()
_, exists := ss.kv[key]
return exists
}
// Put adds a key/value to the in-memory secret store.
// Put'ing a key that already exists overwrites the previous
// value. The secret store is not persisted at this point,
// an additional call to Save() is required.
func (ss *SecretStore) Set(key, value string) {
ss.mut.Lock()
defer ss.mut.Unlock()
ss.kv[key] = value
}
// Put is an alias for Set
func (ss *SecretStore) Put(key, value string) {
ss.Set(key, value)
}
// Delete removes the key from the in-memory secret store.
// This is not persisted.
func (ss *SecretStore) Delete(key string) {
ss.mut.Lock()
defer ss.mut.Unlock()
delete(ss.kv, key)
}
// Dump returns a copy of the kv store. DO NOT USE IN PLUGINS.
// This returns an UNENCRYPTED copy of the kv store for CLI
// tools and debugging. This might go away.
func (ss *SecretStore) Dump() map[string]string {
out := make(map[string]string)
ss.mut.Lock()
defer ss.mut.Unlock()
for k, v := range ss.kv {
out[k] = v
}
return out
}
// Load secrets from the database and decrypt them into hal's in-memory secret
// store. The database-side secrets will be added to the existing store,
// overwriting on conflict (e.g. the database secrets).
// Any errors during this process are fatal.
func (ss *SecretStore) LoadFromDB() {
if !ss.keyed {
panic("The secret store key has not been set!")
}
ss.initTable()
db := SqlDB()
rows, err := db.Query("SELECT pkey, sval, nonce FROM secrets")
if err != nil {
log.Fatalf("Could not read secrets from the database: %s", err)
}
defer rows.Close()
// encrypted key/value and key/nonce
encrypted := make([]ssRec, 0)
// pull the encrypted data into memory
for rows.Next() {
ssr := ssRec{}
err := rows.Scan(&ssr.pkey, &ssr.sval, &ssr.nonce)
if err != nil {
log.Fatalf("Could not rows.Scan: %s", err)
}
encrypted = append(encrypted, ssr)
}
gcm := ss.getGCM()
// decrypt the keys/values into ss.kv
for _, ssr := range encrypted {
value, err := gcm.Open(nil, ssr.nonce, ssr.sval, nil)
if err != nil {
log.Fatalf("value decryption failed: %s\n", err)
}
ss.kv[string(ssr.pkey)] = string(value)
}
}
// Serialize the secret store, encrypt it, and store it in the database.
// Any errors during this process are fatal.
func (ss *SecretStore) SaveToDB() {
gcm := ss.getGCM()
ss.initTable()
ss.mut.Lock()
defer ss.mut.Unlock()
db := SqlDB()
tx, err := db.Begin()
if err != nil {
log.Fatalf("Failed to create transaction for saving secrets: %s", err)
}
insert, err := tx.Prepare(`INSERT INTO secrets (pkey,sval,nonce) VALUES (?,?,?)`)
if err != nil {
log.Fatalf("Failed to prepare insert query: %s", err)
}
defer insert.Close()
_, err = tx.Exec(`TRUNCATE TABLE secrets`)
if err != nil {
log.Fatalf("Failed to truncate secrets table: %s", err)
}
// use a unique nonce for each key/value pair
// TODO: ask infosec if using the nonce for both is OK
for key, val := range ss.kv {
nonce, err := ss.mkNonce()
if err != nil {
log.Fatalf("Could not create a new nonce: %s", err)
}
sealed := gcm.Seal(nil, nonce, []byte(val), nil)
_, err = insert.Exec(key, sealed, nonce)
if err != nil {
log.Fatalf("Could not write encrypted key/value/nonce to DB: %s", err)
}
}
err = tx.Commit()
if err != nil {
log.Fatalf("secrets.SaveToDB transaction failed: %s", err)
}
}
// initTable runs the table initialization statement once
func (ss *SecretStore) initTable() {
ss.itbl.Do(func() {
err := SqlInit(SECRETS_TABLE)
if err != nil {
log.Printf("Failed to initialize the secrets table: %s", err)
}
})
}
func (ss *SecretStore) WipeDB() {
SqlDB().Exec(`TRUNCATE TABLE secrets`)
}
func (ss *SecretStore) InitDB() {
ss.initTable()
ss.SaveToDB()
}
func (ss *SecretStore) mkNonce() ([]byte, error) {
nonce := make([]byte, NONCE_SIZE)
_, err := io.ReadFull(rand.Reader, nonce)
if err != nil {
log.Printf("Could not acquire nonce: %s", err)
return nil, err
}
return nonce, nil
}
// getGCM returns an AES/GCM cipher configured with the default nonce size.
func (ss *SecretStore) getGCM() cipher.AEAD {
if !ss.keyed {
panic("The secret store key has not been set!")
}
ss.mut.Lock()
defer ss.mut.Unlock()
block, err := aes.NewCipher(ss.key)
if err != nil {
log.Fatalf("aes.NewCipher failed: %s", err)
}
gcm, err := cipher.NewGCM(block)
if err != nil {
log.Fatalf("cipher.NewGCM(aes block) failed: %s", err)
}
return gcm
}