aboutsummaryrefslogtreecommitdiff
path: root/secrets/vault_client.go
blob: 9d4b7728667c0bd995a3731f36070fb8cd87ce63 (plain)
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package secrets

import (
	"container/heap"
	"context"
	"encoding/base64"
	"encoding/json"
	"errors"
	"fmt"
	"net/http"
	"path"
	"sync"
	"time"

	glos "code.crute.us/mcrute/golib/os"

	"github.com/hashicorp/vault/api"
	"github.com/hashicorp/vault/api/auth/approle"
	"github.com/mitchellh/mapstructure"
)

const (
	renewalStartPercent = 0.8
	notificationChanLen = 100
	defaultIncrement    = 30 * 60 // 30 minutes (as seconds)
	defaultTimeout      = 10 * time.Second
	renewalWindow       = 30 * time.Second
)

type vaultRenewalMinHeap []*VaultHandle

func (h vaultRenewalMinHeap) Len() int           { return len(h) }
func (h vaultRenewalMinHeap) Less(i, j int) bool { return h[i].renewAfter() < h[j].renewAfter() }
func (h vaultRenewalMinHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }
func (h *vaultRenewalMinHeap) Push(x any)        { *h = append(*h, x.(*VaultHandle)) }
func (h vaultRenewalMinHeap) Root() *VaultHandle { return h[0] }

// Convenience methods to hide the collections/heap stuff from users
func (h *vaultRenewalMinHeap) PopHeap() *VaultHandle   { return heap.Pop(h).(*VaultHandle) }
func (h *vaultRenewalMinHeap) PushHeap(i *VaultHandle) { heap.Push(h, i) }
func (h *vaultRenewalMinHeap) Init()                   { heap.Init(h) }

func (h *vaultRenewalMinHeap) Pop() any {
	old := *h
	n := len(old)
	item := old[n-1]
	old[n-1] = nil // avoid memory leak
	*h = old[0 : n-1]
	return item
}

func (h *vaultRenewalMinHeap) FindRemove(handle *VaultHandle) bool {
	for i, hnd := range *h {
		if hnd.name == handle.name {
			heap.Remove(h, i)
			return true
		}
	}
	return false
}

type VaultHandle struct {
	name     string
	critical bool
	acquired time.Time
	secret   *api.Secret
}

var _ Handle = (*VaultHandle)(nil)

func (h *VaultHandle) isAuthToken() bool {
	return h.secret.Auth != nil
}

func (h *VaultHandle) renewAfter() time.Duration {
	after := float64(h.leaseDuration().Nanoseconds()) * renewalStartPercent
	afterTime := h.acquired.Add(time.Duration(after))
	return afterTime.Sub(time.Now()).Round(time.Second)
}

func (h *VaultHandle) leaseDuration() time.Duration {
	duration := time.Duration(h.secret.LeaseDuration) * time.Second
	if h.isAuthToken() {
		return time.Duration(h.secret.Auth.LeaseDuration) * time.Second
	}
	return duration
}

func (h *VaultHandle) renew(ctx context.Context, c VaultServiceClient, inc int) (err error) {
	var s *api.Secret

	vctx, cancel := context.WithTimeout(ctx, defaultTimeout)
	defer cancel()

	if h.isAuthToken() {
		s, err = c.Auth().Token().RenewTokenAsSelfWithContext(vctx, h.secret.Auth.ClientToken, inc)
		if err != nil {
			return err
		}
	} else {
		s, err = c.Sys().RenewWithContext(vctx, h.secret.LeaseID, inc)
		if err != nil {
			return err
		}
	}

	h.secret = s
	h.acquired = time.Now()

	return nil
}

func (h *VaultHandle) Reference() string {
	return h.name
}

type VaultServiceClient interface {
	Auth() *api.Auth
	Sys() *api.Sys
	Token() string
}

type VaultClient struct {
	sync.Mutex

	client         VaultServiceClient
	logical        *api.Logical
	auth           *approle.AppRoleAuth
	secrets        vaultRenewalMinHeap
	renewIncrement int
	notifications  chan Renewal
}

var _ Client = (*VaultClient)(nil)
var _ ClientManager = (*VaultClient)(nil)

type VaultClientConfig struct {
	Host        string `env:"VAULT_ADDR"`
	Token       string `env:"VAULT_TOKEN"`
	RoleId      string `env:"VAULT_ROLE_ID"`
	RoleSecret  string `env:"VAULT_SECRET_ID"`
	Increment   int    `env:"VAULT_INCREMENT"`
	AppRoleAuth *approle.AppRoleAuth
}

func (c *VaultClientConfig) Validate() error {
	if c.Host == "" {
		return fmt.Errorf("VaultClientConfig: Vault host is not specified")
	}

	// The presence of a token is always assumed to be valid, client errors
	// will occur otherwise.
	if c.Token != "" {
		return nil
	}

	// This constructor does a bunch of validation internally so just let
	// it do its thing and return any errors from that directly to the
	// user.
	ar, err := approle.NewAppRoleAuth(c.RoleId, &approle.SecretID{FromString: c.RoleSecret})
	if err != nil {
		return fmt.Errorf("VaultClientConfig: AppRole credentials invalid: %w", err)
	}
	c.AppRoleAuth = ar

	return nil
}

// NewVaultClient will attempt to create a secrets.Client from the
// passed config. Config can be nil, in which case an attempt will
// be made to load the configuration from environment variables. See
// VaultClientConfig for the expected names of those variables.
func NewVaultClient(cfg *VaultClientConfig) (ClientManager, error) {
	if cfg == nil {
		cfg = &VaultClientConfig{}
	}

	if err := glos.UnmarshalEnvironment(cfg); err != nil {
		return nil, err
	}

	if err := cfg.Validate(); err != nil {
		return nil, err
	}

	vc, err := api.NewClient(api.DefaultConfig())
	if err != nil {
		return nil, fmt.Errorf("NewVaultClient: error building client config: %w", err)
	}
	vc.SetAddress(cfg.Host)

	if cfg.Token != "" {
		vc.SetToken(cfg.Token)
	}

	c := &VaultClient{
		client:         vc,
		logical:        vc.Logical(),
		secrets:        vaultRenewalMinHeap{},
		notifications:  make(chan Renewal, notificationChanLen),
		auth:           cfg.AppRoleAuth,
		renewIncrement: cfg.Increment,
	}

	c.secrets.Init()

	if c.renewIncrement == 0 {
		c.renewIncrement = defaultIncrement
	}

	return c, nil
}

func (c *VaultClient) Notifications() <-chan Renewal {
	return c.notifications
}

func (c *VaultClient) Authenticate(ctx context.Context) error {
	if c.auth == nil {
		return c.authToken(ctx)
	} else {
		return c.authAppRole(ctx)
	}
}

// VaultToken is not part of the official API but is exposed for
// clients that need to gain access to this for some reason. There are
// no compatibility guarantees with this method and it's use limits
// portability.
func (c *VaultClient) VaultToken() string {
	return c.client.Token()
}

func (c *VaultClient) authToken(ctx context.Context) error {
	if c.client.Token() == "" {
		return fmt.Errorf("Authenticate: unable to authenticate, neither token nor approle provided")
	}

	vctx, cancel := context.WithTimeout(ctx, defaultTimeout)
	defer cancel()

	secret, err := c.client.Auth().Token().LookupSelfWithContext(vctx)
	if err != nil {
		return err
	}

	// Looking up self does not return an auth token just a map of data
	// about the current token. Convert this into a SecretAuth so that
	// downstream renewal code does the right thing.
	secret.Auth = &api.SecretAuth{}
	if err := mapstructure.Decode(secret.Data, secret.Auth); err != nil {
		return err
	}
	secret.Auth.ClientToken = c.client.Token()

	c.makeHandle("login", secret)

	return nil
}

func (c *VaultClient) authAppRole(ctx context.Context) error {
	vctx, cancel := context.WithTimeout(ctx, defaultTimeout)
	defer cancel()

	s, err := c.client.Auth().Login(vctx, c.auth)
	if err != nil {
		return fmt.Errorf("Authenticate: error logging in to vault: %w", err)
	}
	c.makeHandle("login", s)
	return err
}

// makeHandle creates a secret handle and schedules it for renewal if it
// is renewable.
func (c *VaultClient) makeHandle(name string, s *api.Secret) Handle {
	h := &VaultHandle{
		name:     name,
		critical: true, // Everything is critical unless marked otherwise
		acquired: time.Now(),
		secret:   s,
	}

	// If this is renewable then schedule it for renewal
	if (s.Auth != nil && s.Auth.Renewable) || s.Renewable {
		c.Lock()
		c.secrets.PushHeap(h)
		c.Unlock()
	}

	return h
}

func (c *VaultClient) writeHandle(ctx context.Context, prefix, suffix string, data map[string]any) (Handle, error) {
	key := suffix
	if prefix != "" {
		key = path.Join(prefix, suffix)
	}

	s, err := c.logical.WriteWithContext(ctx, key, data)
	if err != nil {
		return nil, fmt.Errorf("writeHandle: error writing to Vault: %w", err)
	}

	return c.makeHandle(key, s), nil
}

func (c *VaultClient) read(ctx context.Context, prefix, suffix string) (Handle, error) {
	key := suffix
	if prefix != "" {
		key = path.Join(prefix, suffix)
	}

	s, err := c.logical.ReadWithContext(ctx, key)
	if err != nil {
		return nil, fmt.Errorf("read: error reading from Vault: %w", err)
	}

	return c.makeHandle(key, s), nil
}

func (c *VaultClient) isRecoverableDbError(err error) bool {
	var apiErr *api.ResponseError
	return errors.Is(api.ErrSecretNotFound, err) ||
		(errors.As(err, &apiErr) && apiErr.StatusCode == http.StatusForbidden)
}

func (c *VaultClient) DatabaseCredential(ctx context.Context, suffix string) (*Credential, Handle, error) {
	cred, hnd, err := c.databaseCredentialDynamic(ctx, suffix)
	if err != nil {
		if c.isRecoverableDbError(err) {
			cred, hnd, err = c.databaseCredentialStatic(ctx, suffix)
		}
	}
	return cred, hnd, err
}

func (c *VaultClient) databaseCredentialDynamic(ctx context.Context, suffix string) (*Credential, Handle, error) {
	h, err := c.read(ctx, "database/creds", suffix)
	if err != nil {
		return nil, nil, err
	}
	vh := h.(*VaultHandle)

	var d Credential
	if err = mapstructure.Decode(vh.secret.Data, &d); err != nil {
		return nil, nil, fmt.Errorf("databaseCredentialStatic: error decoding secret: %w", err)
	}

	return &d, h, nil
}

func (c *VaultClient) databaseCredentialStatic(ctx context.Context, suffix string) (*Credential, Handle, error) {
	h, err := c.read(ctx, "database/static-creds", suffix)
	if err != nil {
		return nil, nil, err
	}

	var d Credential
	if err = mapstructure.Decode(h.(*VaultHandle).secret.Data, &d); err != nil {
		return nil, nil, fmt.Errorf("databaseCredentialStatic: error decoding secret: %w", err)
	}

	return &d, h, nil
}

func (c *VaultClient) Secret(ctx context.Context, suffix string, out any) (Handle, error) {
	h, err := c.read(ctx, "kv/data", suffix)
	if err != nil {
		return nil, err
	}

	if err = mapstructure.Decode(h.(*VaultHandle).secret.Data["data"], out); err != nil {
		return nil, err
	}

	return h, nil
}

func (c *VaultClient) RawSecret(ctx context.Context, path string, out any) (Handle, error) {
	h, err := c.read(ctx, "", path)
	if err != nil {
		return nil, err
	}

	if err = mapstructure.Decode(h.(*VaultHandle).secret.Data["data"], out); err != nil {
		return nil, err
	}

	return h, nil
}

func (c *VaultClient) AWSAssumeRoleSimple(ctx context.Context, name string) (*AWSCredential, Handle, error) {
	return c.AWSAssumeRole(ctx, name, fmt.Sprintf("%s-%d", name, time.Now().UnixNano()), time.Hour)
}

func (c *VaultClient) AWSAssumeRole(ctx context.Context, name string, sessionName string, ttl time.Duration) (*AWSCredential, Handle, error) {
	h, err := c.writeHandle(ctx, "aws/sts", name, map[string]any{
		"role_session_name": sessionName,
		"ttl":               ttl.String(),
	})
	if err != nil {
		return nil, nil, err
	}

	var d AWSCredential
	if err = mapstructure.Decode(h.(*VaultHandle).secret.Data, &d); err != nil {
		return nil, nil, fmt.Errorf("AWSIAMUser: error decoding secret: %w", err)
	}

	return nil, nil, nil
}

func (c *VaultClient) AWSIAMUser(ctx context.Context, name string) (*AWSCredential, Handle, error) {
	h, err := c.read(ctx, "aws/creds", name)
	if err != nil {
		return nil, nil, err
	}

	var d AWSCredential
	if err = mapstructure.Decode(h.(*VaultHandle).secret.Data, &d); err != nil {
		return nil, nil, fmt.Errorf("AWSIAMUser: error decoding secret: %w", err)
	}

	return &d, h, nil
}

func (c *VaultClient) WriteSecret(ctx context.Context, suffix string, in any) error {
	inb, err := json.Marshal(in)
	if err != nil {
		return fmt.Errorf("WriteSecret: error encoding json: %w", err)
	}

	if _, err = c.logical.WriteBytesWithContext(ctx, path.Join("kv/data", suffix), inb); err != nil {
		return fmt.Errorf("WriteSecret: error writing to vault: %w", err)
	}

	return nil
}

func (c *VaultClient) Encrypt(ctx context.Context, suffix string, data []byte) (string, error) {
	s, err := c.logical.WriteWithContext(
		ctx,
		path.Join("transit/encrypt", suffix),
		map[string]any{"plaintext": base64.StdEncoding.EncodeToString(data)},
	)
	if err != nil {
		return "", fmt.Errorf("Encrypt: unable to write to vault: %w", err)
	}

	return s.Data["ciphertext"].(string), nil
}

func (c *VaultClient) Decrypt(ctx context.Context, suffix, data string) ([]byte, error) {
	s, err := c.logical.WriteWithContext(
		ctx,
		path.Join("transit/decrypt", suffix),
		map[string]any{"ciphertext": data},
	)
	if err != nil {
		return nil, fmt.Errorf("Decrypt: unable to write to vault: %w", err)
	}

	d, err := base64.StdEncoding.DecodeString(s.Data["plaintext"].(string))
	if err != nil {
		return nil, fmt.Errorf("Decrypt: unable to base64 decode plaintext: %w", err)
	}

	return d, nil
}

func (c *VaultClient) Destroy(h Handle) error {
	c.Lock()
	defer c.Unlock()

	c.secrets.FindRemove(h.(*VaultHandle))

	return nil
}

func (c *VaultClient) MakeNonCritical(h Handle) error {
	h.(*VaultHandle).critical = false
	return nil
}

func (c *VaultClient) renewAttempt(ctx context.Context) (next time.Duration) {
	c.Lock()
	defer c.Unlock()

	// In the absence of any other time, run once a second
	next = 5 * time.Second

	if c.secrets.Len() < 1 {
		return
	}

	for {
		s := c.secrets.PopHeap()
		if s.renewAfter() < renewalWindow {
			// Underlying client does backoff and retry
			c.notifications <- Renewal{
				Name:     s.name,
				Critical: s.critical,
				Time:     time.Now(),
				Error:    s.renew(ctx, c.client, c.renewIncrement),
			}
			c.secrets.PushHeap(s)
		} else {
			c.secrets.PushHeap(s)
			next = c.secrets.Root().renewAfter()
			break
		}
	}

	return next
}

func (c *VaultClient) Run(ctx context.Context, wg *sync.WaitGroup) error {
	wg.Add(1)
	defer wg.Done()

	for {
		sleepTime := c.renewAttempt(ctx)

		select {
		case <-time.After(sleepTime):
			continue
		case <-ctx.Done():
			return nil
		}
	}
}