-
Notifications
You must be signed in to change notification settings - Fork 2
/
badstate_test.go
165 lines (156 loc) · 8.6 KB
/
badstate_test.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
package vaultkv_test
import (
"fmt"
"github.com/cloudfoundry-community/vaultkv"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = When("the vault is uninitialized", func() {
type spec struct {
Name string
Setup func()
MinVersion *semver
}
Specify("Most commands should return ErrUninitialized", func() {
for _, s := range []spec{
{"Health", func() { err = vault.Health(true) }, nil},
{"EnableSecretsMount", func() { err = vault.EnableSecretsMount("beep", vaultkv.Mount{}) }, nil},
{"Unseal", func() { _, err = vault.Unseal("pLacEhoLdeR=") }, nil},
{"Get", func() { err = vault.Get("secret/sure/whatever", nil) }, nil},
{"Set", func() { err = vault.Set("secret/sure/whatever", map[string]string{"foo": "bar"}) }, nil},
{"Delete", func() { err = vault.Delete("secret/sure/whatever") }, nil},
{"List", func() { _, err = vault.List("secret/sure/whatever") }, nil},
{"V2Get", func() { _, err = vault.V2Get("secret", "foo", nil, nil) }, &semver{0, 10, 0}},
{"V2Set", func() { _, err = vault.V2Set("secret", "foo", map[string]string{"beep": "boop"}, nil) }, &semver{0, 10, 0}},
{"V2Delete", func() { err = vault.V2Delete("secret", "foo", nil) }, &semver{0, 10, 0}},
{"V2Undelete", func() { err = vault.V2Undelete("secret", "foo", []uint{1}) }, &semver{0, 10, 0}},
{"V2Destroy", func() { err = vault.V2Destroy("secret", "foo", []uint{1}) }, &semver{0, 10, 0}},
{"V2DestroyMetadata", func() { err = vault.V2DestroyMetadata("secret", "foo") }, &semver{0, 10, 0}},
{"V2GetMetadata", func() { _, err = vault.V2GetMetadata("secret", "foo") }, &semver{0, 10, 0}},
{"KVGet", func() { _, err = vault.NewKV().Get("secret/foo", nil, nil) }, &semver{0, 10, 0}},
{"KVSet", func() { _, err = vault.NewKV().Set("secret/foo", map[string]string{"beep": "boop"}, nil) }, &semver{0, 10, 10}},
{"KVDelete", func() { err = vault.NewKV().Delete("secret/foo", nil) }, &semver{0, 10, 0}},
{"KVUndelete", func() { err = vault.NewKV().Undelete("secret/foo", []uint{1}) }, &semver{0, 10, 0}},
{"KVDestroy", func() { err = vault.NewKV().Destroy("secret/foo", []uint{1}) }, &semver{0, 10, 0}},
{"KVDestroyAll", func() { err = vault.NewKV().DestroyAll("secret/foo") }, &semver{0, 10, 0}},
} {
if s.MinVersion != nil && parseSemver(currentVaultVersion).LessThan(*s.MinVersion) {
continue
}
(s.Setup)()
Expect(err).To(HaveOccurred(),
fmt.Sprintf("`%s' did not produce an error", s.Name))
Expect(vaultkv.IsUninitialized(err)).To(BeTrue())
}
})
})
var _ = When("the vault is initialized", func() {
type spec struct {
Name string
Setup func()
MinVersion *semver
}
var initOut *vaultkv.InitVaultOutput
BeforeEach(func() {
initOut, err = vault.InitVault(vaultkv.InitConfig{
Shares: 1,
Threshold: 1,
})
Expect(err).NotTo(HaveOccurred())
})
When("the vault is sealed", func() {
Specify("Most commands should return ErrSealed", func() {
for _, s := range []spec{
{"Health", func() { err = vault.Health(true) }, nil},
{"EnableSecretsMount", func() { err = vault.EnableSecretsMount("beep", vaultkv.Mount{}) }, nil},
{"DisableSecretsMount", func() { err = vault.DisableSecretsMount("beep") }, nil},
{"Get", func() { err = vault.Get("secret/sure/whatever", nil) }, nil},
{"Set", func() { err = vault.Set("secret/sure/whatever", map[string]string{"foo": "bar"}) }, nil},
{"Delete", func() { err = vault.Delete("secret/sure/whatever") }, nil},
{"List", func() { _, err = vault.List("secret/sure/whatever") }, nil},
{"V2Get", func() { _, err = vault.V2Get("secret", "foo", nil, nil) }, &semver{0, 10, 0}},
{"V2Set", func() { _, err = vault.V2Set("secret", "foo", map[string]string{"beep": "boop"}, nil) }, &semver{0, 10, 0}},
{"V2Delete", func() { err = vault.V2Delete("secret", "foo", nil) }, &semver{0, 10, 0}},
{"V2Undelete", func() { err = vault.V2Undelete("secret", "foo", []uint{1}) }, &semver{0, 10, 0}},
{"V2Destroy", func() { err = vault.V2Destroy("secret", "foo", []uint{1}) }, &semver{0, 10, 0}},
{"V2DestroyMetadata", func() { err = vault.V2DestroyMetadata("secret", "foo") }, &semver{0, 10, 0}},
{"V2GetMetadata", func() { _, err = vault.V2GetMetadata("secret", "foo") }, &semver{0, 10, 0}},
{"KVGet", func() { _, err = vault.NewKV().Get("secret/foo", nil, nil) }, &semver{0, 10, 0}},
{"KVSet", func() { _, err = vault.NewKV().Set("secret/foo", map[string]string{"beep": "boop"}, nil) }, &semver{0, 10, 10}},
{"KVDelete", func() { err = vault.NewKV().Delete("secret/foo", nil) }, &semver{0, 10, 0}},
{"KVUndelete", func() { err = vault.NewKV().Undelete("secret/foo", []uint{1}) }, &semver{0, 10, 0}},
{"KVDestroy", func() { err = vault.NewKV().Destroy("secret/foo", []uint{1}) }, &semver{0, 10, 0}},
{"KVDestroyAll", func() { err = vault.NewKV().DestroyAll("secret/foo") }, &semver{0, 10, 0}},
} {
if s.MinVersion != nil && parseSemver(currentVaultVersion).LessThan(*s.MinVersion) {
continue
}
(s.Setup)()
Expect(err).To(HaveOccurred(),
fmt.Sprintf("`%s' did not produce an error", s.Name))
Expect(vaultkv.IsSealed(err)).To(BeTrue(),
fmt.Sprintf("`%s' did not make error of type *ErrSealed", s.Name))
}
})
When("the vault is unsealed", func() {
BeforeEach(func() {
sealState, err := vault.Unseal(initOut.Keys[0])
Expect(err).NotTo(HaveOccurred())
Expect(sealState).NotTo(BeNil())
Expect(sealState.Sealed).To(BeFalse())
})
Specify("KV commands targeted at non-existent things should 404", func() {
for _, s := range []spec{
{"Get", func() { err = vault.Get("secret/sure/whatever", nil) }, nil},
{"List", func() { _, err = vault.List("secret/sure/whatever") }, nil},
{"V2Get", func() { _, err = vault.V2Get("secret", "foo", nil, nil) }, &semver{0, 10, 0}},
{"V2GetMetadata", func() { _, err = vault.V2GetMetadata("secret", "foo") }, &semver{0, 10, 0}},
} {
if s.MinVersion != nil && parseSemver(currentVaultVersion).LessThan(*s.MinVersion) {
continue
}
(s.Setup)()
Expect(err).To(HaveOccurred(),
fmt.Sprintf("`%s' did not produce an error", s.Name))
Expect(vaultkv.IsNotFound(err)).To(BeTrue(),
fmt.Sprintf("`%s' did not make error of type *ErrNotFound", s.Name))
}
})
When("the auth token is wrong", func() {
BeforeEach(func() {
//If this is your token, I'm sorry
vault.AuthToken = "01234567-89ab-cdef-0123-456789abcdef"
})
Specify("Most commands should give a 403", func() {
for _, s := range []spec{
{"EnableSecretsMount", func() { err = vault.EnableSecretsMount("beep", vaultkv.Mount{}) }, nil},
{"DisableSecretsMount", func() { err = vault.DisableSecretsMount("beep") }, nil},
{"Get", func() { err = vault.Get("secret/sure/whatever", nil) }, nil},
{"Set", func() { err = vault.Set("secret/sure/whatever", map[string]string{"foo": "bar"}) }, nil},
{"Delete", func() { err = vault.Delete("secret/sure/whatever") }, nil},
{"List", func() { _, err = vault.List("secret/sure/whatever") }, nil},
{"V2Get", func() { _, err = vault.V2Get("secret", "foo", nil, nil) }, &semver{0, 10, 0}},
{"V2Set", func() { _, err = vault.V2Set("secret", "foo", map[string]string{"beep": "boop"}, nil) }, &semver{0, 10, 0}},
{"V2Delete", func() { err = vault.V2Delete("secret", "foo", nil) }, &semver{0, 10, 0}},
{"V2Undelete", func() { err = vault.V2Undelete("secret", "foo", []uint{1}) }, &semver{0, 10, 0}},
{"V2Destroy", func() { err = vault.V2Destroy("secret", "foo", []uint{1}) }, &semver{0, 10, 0}},
{"V2DestroyMetadata", func() { err = vault.V2DestroyMetadata("secret", "foo") }, &semver{0, 10, 0}},
{"V2GetMetadata", func() { _, err = vault.V2GetMetadata("secret", "foo") }, &semver{0, 10, 0}},
{"KVGet", func() { _, err = vault.NewKV().Get("secret/foo", nil, nil) }, &semver{0, 10, 0}},
{"KVSet", func() { _, err = vault.NewKV().Set("secret/foo", map[string]string{"beep": "boop"}, nil) }, &semver{0, 10, 10}},
{"KVDelete", func() { err = vault.NewKV().Delete("secret/foo", &vaultkv.KVDeleteOpts{V1Destroy: true}) }, &semver{0, 10, 0}},
{"KVUndelete", func() { err = vault.NewKV().Undelete("secret/foo", []uint{1}) }, &semver{0, 10, 0}},
{"KVDestroy", func() { err = vault.NewKV().Destroy("secret/foo", []uint{1}) }, &semver{0, 10, 0}},
{"KVDestroyAll", func() { err = vault.NewKV().DestroyAll("secret/foo") }, &semver{0, 10, 0}},
} {
(s.Setup)()
Expect(err).To(HaveOccurred(),
fmt.Sprintf("`%s' did not produce an error", s.Name))
Expect(vaultkv.IsForbidden(err)).To(BeTrue(),
fmt.Sprintf("`%s' did not give a 403 - gave %s", s.Name, err))
}
})
})
})
})
})