/
vault.go
89 lines (69 loc) · 2.49 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
package mock
import (
"context"
"github.com/evergreen-ci/cocoa"
"github.com/evergreen-ci/utility"
)
// Vault provides a mock implementation of a cocoa.Vault backed by any vault by
// default. This makes it possible to introspect on inputs to the vault and
// control the vault's output. It provides some default implementations where
// possible.
type Vault struct {
cocoa.Vault
CreateSecretInput *cocoa.NamedSecret
CreateSecretOutput *string
CreateSecretError error
GetValueInput *string
GetValueOutput *string
GetValueError error
UpdateValueInput *cocoa.NamedSecret
UpdateValueError error
DeleteSecretInput *string
DeleteSecretError error
}
// NewVault creates a mock Vault backed by the given Vault.
func NewVault(v cocoa.Vault) *Vault {
return &Vault{
Vault: v,
}
}
// CreateSecret saves the input options and returns a mock secret ID. The mock
// output can be customized. By default, it will call the backing Vault
// implementation's CreateSecret.
func (m *Vault) CreateSecret(ctx context.Context, s cocoa.NamedSecret) (id string, err error) {
m.CreateSecretInput = &s
if m.CreateSecretOutput != nil || m.CreateSecretError != nil {
return utility.FromStringPtr(m.CreateSecretOutput), m.CreateSecretError
}
return m.Vault.CreateSecret(ctx, s)
}
// GetValue saves the input options and returns an existing mock secret's value.
// The mock output can be customized. By default, it will call the backing Vault
// implementation's GetValue.
func (m *Vault) GetValue(ctx context.Context, id string) (val string, err error) {
m.GetValueInput = &id
if m.GetValueOutput != nil || m.GetValueError != nil {
return utility.FromStringPtr(m.GetValueOutput), m.GetValueError
}
return m.Vault.GetValue(ctx, id)
}
// UpdateValue saves the input options and updates an existing mock secret. The
// mock output can be customized. By default, it will call the backing Vault
// implementation's UpdateValue.
func (m *Vault) UpdateValue(ctx context.Context, s cocoa.NamedSecret) error {
m.UpdateValueInput = &s
if m.UpdateValueError != nil {
return m.UpdateValueError
}
return m.Vault.UpdateValue(ctx, s)
}
// DeleteSecret saves the input options and deletes an existing mock secret. The
// mock output can be customized. By default, it will call the backing Vault
// implementation's DeleteSecret.
func (m *Vault) DeleteSecret(ctx context.Context, id string) error {
m.DeleteSecretInput = &id
if m.DeleteSecretError != nil {
return m.DeleteSecretError
}
return m.Vault.DeleteSecret(ctx, id)
}