-
Notifications
You must be signed in to change notification settings - Fork 39
/
vault.go
343 lines (310 loc) · 8.85 KB
/
vault.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Copyright (c) 2013-2016, Jeremy Bingham (<jeremy@goiardi.gl>)
*
* 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.
*/
package secret
// Functions for using hashicorp vault (https://www.vaultproject.io/) to store
// secrets in goiardi.
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/ctdk/goiardi/config"
vault "github.com/hashicorp/vault/api"
"github.com/tideland/golib/logger"
"sync"
"time"
)
// make this a pool later?
type vaultSecretStore struct {
m sync.RWMutex
secrets map[string]*secretVal
*vault.Client
}
const MaxStaleAgeSeconds = 3600 // configurable later, but make it an hour for
// now
const StaleTryAgainSeconds = 60 // try stale values again in a minute
type secretVal struct {
path string
secretType string
created time.Time
renewable bool
ttl time.Duration
expires time.Time
stale bool
staleTryAgain time.Time
staleTime time.Time
value interface{}
}
type secretConvert func(interface{}) (interface{}, error)
func configureVault() (*vaultSecretStore, error) {
conf := vault.DefaultConfig()
if err := conf.ReadEnvironment(); err != nil {
return nil, err
}
if config.Config.VaultAddr != "" {
conf.Address = config.Config.VaultAddr
}
c, err := vault.NewClient(conf)
if err != nil {
return nil, err
}
var m sync.RWMutex
secrets := make(map[string]*secretVal)
v := &vaultSecretStore{m, secrets, c}
return v, nil
}
func (v *vaultSecretStore) getSecret(path string, secretType string) (interface{}, error) {
if v.secrets[path] == nil {
logger.Debugf("secret (%s) for %s is nil, fetching from vault", secretType, path)
s, err := v.getSecretPath(path, secretType)
if err != nil {
return "", err
}
v.secrets[path] = s
} else {
logger.Debugf("using cached secret for %s", path)
}
return v.secretValue(v.secrets[path])
}
func (v *vaultSecretStore) getSecretPath(path string, secretType string) (*secretVal, error) {
t := time.Now()
s, err := v.Logical().Read(path)
if err != nil {
err := fmt.Errorf("Failed to read %s (%s) from vault: %s", path, secretType, err.Error())
return nil, err
}
if s == nil {
err := fmt.Errorf("No secret returned from vault for %s (%s)", path, secretType)
return nil, err
}
p := s.Data[secretType]
if p == nil {
err := fmt.Errorf("no data for %s (%s) from vault", path, secretType)
return nil, err
}
p, err = convertors(secretType)(p)
if err != nil {
return nil, err
}
sVal := newSecretVal(path, secretType, p, t, s)
return sVal, nil
}
func (v *vaultSecretStore) setSecret(path string, secretType string, value interface{}) error {
logger.Debugf("setting public key for %s (%s)", path, secretType)
t := time.Now()
_, err := v.Logical().Write(path, map[string]interface{}{
secretType: value,
})
if err != nil {
return err
}
s, err := v.Logical().Read(path)
if err != nil {
return fmt.Errorf("Error re-reading secret from vault after setting: %s", err.Error())
}
sVal := newSecretVal(path, secretType, value, t, s)
v.secrets[path] = sVal
return nil
}
func (v *vaultSecretStore) deleteSecret(path string) error {
delete(v.secrets, path)
_, err := v.Logical().Delete(path)
if err != nil {
return err
}
return nil
}
func (v *vaultSecretStore) getPublicKey(c ActorKeyer) (string, error) {
v.m.RLock()
defer v.m.RUnlock()
path := makePubKeyPath(c)
s, err := v.getSecret(path, "pubKey")
switch s := s.(type) {
case string:
return s, err
case []byte:
return string(s), err
case nil:
return "", err
default:
var errStr string
if err != nil {
errStr = err.Error()
}
err := fmt.Errorf("The type was wrong fetching the public key from vault: %T -- error, if any: %s", s, errStr)
return "", err
}
}
func (v *vaultSecretStore) setPublicKey(c ActorKeyer, pubKey string) error {
v.m.Lock()
defer v.m.Unlock()
path := makePubKeyPath(c)
return v.setSecret(path, "pubKey", pubKey)
}
func (v *vaultSecretStore) deletePublicKey(c ActorKeyer) error {
v.m.Lock()
defer v.m.Unlock()
path := makePubKeyPath(c)
return v.deleteSecret(path)
}
func makePubKeyPath(c ActorKeyer) string {
return fmt.Sprintf("keys/%s/%s", c.URLType(), c.GetName())
}
func makeHashPath(c ActorKeyer) string {
// strictly speaking only users actually have passwords, but in case
// something else ever comes up, make the path a little longer.
return fmt.Sprintf("keys/passwd/%s/%s", c.URLType(), c.GetName())
}
func newSecretVal(path string, secretType string, value interface{}, t time.Time, s *vault.Secret) *secretVal {
sVal := new(secretVal)
sVal.path = path
sVal.secretType = secretType
sVal.created = t
sVal.renewable = s.Renewable
sVal.ttl = time.Duration(s.LeaseDuration) * time.Second
sVal.expires = t.Add(sVal.ttl)
sVal.value = value
return sVal
}
func (s *secretVal) isExpired() bool {
if s.ttl == 0 {
return false
}
return time.Now().After(s.expires)
}
func (v *vaultSecretStore) secretValue(s *secretVal) (interface{}, error) {
if s.isExpired() {
logger.Debugf("trying to renew secret for %s", s.path)
s2, err := v.getSecretPath(s.path, s.secretType)
if !s.stale {
if err != nil {
logger.Debugf("error trying to renew the secret for %s: %s -- marking as stale", s.path, err.Error())
s.stale = true
s.staleTime = time.Now().Add(MaxStaleAgeSeconds * time.Second)
s.staleTryAgain = time.Now().Add(StaleTryAgainSeconds * time.Second)
} else {
logger.Debugf("successfully renewed secret for %s", s.path)
s = s2
}
} else if time.Now().After(s.staleTime) {
if err != nil {
err := fmt.Errorf("Couldn't renew the secret for %s before %d seconds ran out, giving up", s.path, MaxStaleAgeSeconds)
return nil, err
}
logger.Debugf("successfully renewed secret for %s beforegiving up due to staleness", s.path)
s = s2
} else if time.Now().After(s.staleTryAgain) {
if err != nil {
logger.Debugf("error trying to renew the secret for %s: %s -- will renew again in %d seconds", s.path, err.Error(), StaleTryAgainSeconds)
s.staleTryAgain = time.Now().Add(StaleTryAgainSeconds)
} else {
logger.Debugf("successfully renewed secret after being stale")
s = s2
}
}
}
return s.value, nil
}
func (v *vaultSecretStore) valueStr(s *secretVal) (string, error) {
val, err := v.secretValue(s)
if err != nil {
return "", err
}
valStr, ok := val.(string)
if !ok {
err := fmt.Errorf("value for %s was not a string, but a %T!", s.path, val)
return "", err
}
return valStr, nil
}
// shovey signing key
func (v *vaultSecretStore) getSigningKey(path string) (*rsa.PrivateKey, error) {
v.m.RLock()
defer v.m.RUnlock()
s, err := v.getSecret(path, "RSAKey")
switch s := s.(type) {
case *rsa.PrivateKey:
return s, err
default:
var errStr string
if err != nil {
errStr = err.Error()
}
kerr := fmt.Errorf("RSA private key for shovey was not returned. An object of type %T was. Error, if any: %s", s, errStr)
return nil, kerr
}
}
// user passwd hash methods
func (v *vaultSecretStore) setPasswdHash(c ActorKeyer, pwhash string) error {
v.m.Lock()
defer v.m.Unlock()
path := makeHashPath(c)
return v.setSecret(path, "passwd", pwhash)
}
func (v *vaultSecretStore) getPasswdHash(c ActorKeyer) (string, error) {
v.m.RLock()
defer v.m.RUnlock()
path := makeHashPath(c)
s, err := v.getSecret(path, "passwd")
switch s := s.(type) {
case string:
return s, err
case []byte:
return string(s), err
case nil:
return "", err
default:
var errStr string
if err != nil {
errStr = err.Error()
}
err := fmt.Errorf("The type was wrong fetching the passwd hash from vault: %T -- error, if any: %s", s, errStr)
return "", err
}
}
func (v *vaultSecretStore) deletePasswdHash(c ActorKeyer) error {
v.m.Lock()
defer v.m.Unlock()
path := makeHashPath(c)
return v.deleteSecret(path)
}
// funcs to process secrets after fetching them from vault
func secretPassThrough(i interface{}) (interface{}, error) {
return i, nil
}
func secretRSAKey(i interface{}) (interface{}, error) {
p, ok := i.(string)
if !ok {
return nil, fmt.Errorf("not an RSA private key in string form")
}
pBlock, _ := pem.Decode([]byte(p))
if pBlock == nil {
return nil, fmt.Errorf("invalid block size for private key for shovey from vault")
}
pk, err := x509.ParsePKCS1PrivateKey(pBlock.Bytes)
if err != nil {
return nil, err
}
return pk, nil
}
func convertors(secretType string) secretConvert {
switch secretType {
case "RSAKey":
return secretRSAKey
default:
return secretPassThrough
}
}