-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathreflector_test.go
300 lines (254 loc) · 7.15 KB
/
reflector_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
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
package pentagon
import (
"context"
"testing"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sfake "k8s.io/client-go/kubernetes/fake"
"github.com/vimeo/pentagon/vault"
)
func allEngineTest(t *testing.T, subTest func(testing.TB, vault.EngineType)) {
types := vault.AllEngineTypes
for _, engineType := range types {
et := engineType
t.Run(string(engineType), func(innerT *testing.T) {
innerT.Parallel()
subTest(innerT, et)
})
}
}
func TestRefactorSimple(t *testing.T) {
allEngineTest(t, func(t testing.TB, engineType vault.EngineType) {
ctx := context.Background()
k8sClient := k8sfake.NewSimpleClientset()
vaultClient := vault.NewMock(map[string]vault.EngineType{
"secrets": engineType,
})
data := map[string]interface{}{
"foo": "bar",
"bar": "baz",
}
vaultClient.Write("secrets/data/foo", data)
r := NewReflector(
vaultClient,
k8sClient, DefaultNamespace,
DefaultLabelValue,
)
err := r.Reflect(ctx, []Mapping{
{
VaultPath: "secrets/data/foo",
SecretName: "foo",
VaultEngineType: engineType,
},
})
if err != nil {
t.Fatalf("reflect didn't work: %s", err)
}
// now get the secret out of k8s
secrets := k8sClient.CoreV1().Secrets(DefaultNamespace)
secret, err := secrets.Get(ctx, "foo", metav1.GetOptions{})
if err != nil {
t.Fatalf("secret should be there: %s", err)
}
if secret.Labels[LabelKey] != DefaultLabelValue {
t.Fatalf(
"secret pentagon label should be %s is %s",
DefaultLabelValue,
secret.Labels[LabelKey],
)
}
if string(secret.Data["foo"]) != "bar" {
t.Fatalf("foo does not equal bar: %s", string(secret.Data["foo"]))
}
if string(secret.Data["bar"]) != "baz" {
t.Fatalf("bar does not equal baz: %s", string(secret.Data["bar"]))
}
})
}
func TestReflectorNoReconcile(t *testing.T) {
allEngineTest(t, func(t testing.TB, engineType vault.EngineType) {
ctx := context.Background()
k8sClient := k8sfake.NewSimpleClientset()
vaultClient := vault.NewMock(map[string]vault.EngineType{
"secrets": engineType,
})
data := map[string]interface{}{
"foo": "bar",
"bar": "baz",
}
// write two secrets
vaultClient.Write("secrets/data/foo1", data)
vaultClient.Write("secrets/data/foo2", data)
r := NewReflector(
vaultClient,
k8sClient,
DefaultNamespace,
DefaultLabelValue,
)
// reflect both secrets
err := r.Reflect(ctx, []Mapping{
{
VaultPath: "secrets/data/foo1",
SecretName: "foo1",
VaultEngineType: engineType,
},
{
VaultPath: "secrets/data/foo2",
SecretName: "foo2",
VaultEngineType: engineType,
},
})
if err != nil {
t.Fatalf("reflect didn't work: %s", err)
}
// now get the secrets out of k8s
secrets := k8sClient.CoreV1().Secrets(DefaultNamespace)
_, err = secrets.Get(ctx, "foo1", metav1.GetOptions{})
if err != nil {
t.Fatalf("foo1 should be there: %s", err)
}
_, err = secrets.Get(ctx, "foo2", metav1.GetOptions{})
if err != nil {
t.Fatalf("foo2 should be there: %s", err)
}
// reflect again, this time without foo2 -- it should still be there
// and not get reconciled because we're using the default label value.
err = r.Reflect(ctx, []Mapping{
{
VaultPath: "secrets/data/foo1",
SecretName: "foo1",
VaultEngineType: engineType,
},
})
if err != nil {
t.Fatalf("reflect didn't work the second time: %s", err)
}
_, err = secrets.Get(ctx, "foo1", metav1.GetOptions{})
if err != nil {
t.Fatalf("foo1 should still be there: %s", err)
}
_, err = secrets.Get(ctx, "foo2", metav1.GetOptions{})
if err != nil {
t.Fatalf("foo2 should still be there: %s", err)
}
})
}
func TestReflectorWithReconcile(t *testing.T) {
allEngineTest(t, func(t testing.TB, engineType vault.EngineType) {
ctx := context.Background()
k8sClient := k8sfake.NewSimpleClientset()
vaultClient := vault.NewMock(map[string]vault.EngineType{
"secrets": engineType,
})
data := map[string]interface{}{
"foo": "bar",
"bar": "baz",
}
// write two secrets
vaultClient.Write("secrets/data/foo1", data)
vaultClient.Write("secrets/data/foo2", data)
secrets := k8sClient.CoreV1().Secrets(DefaultNamespace)
// make another secret with a different label value so we confirm that
// it's still there after reconciliation
otherLabel := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "other-reflect",
Labels: map[string]string{
LabelKey: "other",
},
},
Data: map[string][]byte{
"something": []byte("else"),
},
}
_, err := secrets.Create(ctx, otherLabel, metav1.CreateOptions{})
if err != nil {
t.Fatalf("unable to create other-reflect secret: %s", err)
}
r := NewReflector(vaultClient, k8sClient, DefaultNamespace, "test")
err = r.Reflect(ctx, []Mapping{
{
VaultPath: "secrets/data/foo1",
SecretName: "foo1",
VaultEngineType: engineType,
},
{
VaultPath: "secrets/data/foo2",
SecretName: "foo2",
VaultEngineType: engineType,
},
})
if err != nil {
t.Fatalf("reflect didn't work: %s", err)
}
s, err := secrets.Get(ctx, "foo1", metav1.GetOptions{})
if err != nil {
t.Fatalf("foo1 should be there: %s", err)
}
if s.Labels[LabelKey] != "test" {
t.Fatalf(
"foo1 pentagon label should have been 'test': %s",
s.Labels[LabelKey],
)
}
_, err = secrets.Get(ctx, "foo2", metav1.GetOptions{})
if err != nil {
t.Fatalf("foo2 should be there: %s", err)
}
// reflect again, this time without foo2 -- it should get reconciled
// because we're using a non-default label value.
err = r.Reflect(ctx, []Mapping{
{
VaultPath: "secrets/data/foo1",
SecretName: "foo1",
VaultEngineType: engineType,
},
})
if err != nil {
t.Fatalf("reflect didn't work the second time: %s", err)
}
// foo1 should still be there because it's still in the mapping
_, err = secrets.Get(ctx, "foo1", metav1.GetOptions{})
if err != nil {
t.Fatalf("foo1 should still be there: %s", err)
}
// foo2 should have been deleted because it wasn't in the mapping
// and we're using a non-default label
_, err = secrets.Get(ctx, "foo2", metav1.GetOptions{})
if !errors.IsNotFound(err) {
t.Fatalf("foo2 should NOT still be there: %s", err)
}
// the one with the different label value should still be there
_, err = secrets.Get(ctx, "other-reflect", metav1.GetOptions{})
if err != nil {
t.Fatalf("other-reflect should still be there: %s", err)
}
})
}
func TestUnsupportedEngineType(t *testing.T) {
ctx := context.Background()
k8sClient := k8sfake.NewSimpleClientset()
vaultClient := vault.NewMock(map[string]vault.EngineType{
"secrets": vault.EngineTypeKeyValueV2,
})
data := map[string]interface{}{
"foo": "bar",
}
vaultClient.Write("secrets/data/foo", data)
r := NewReflector(
vaultClient,
k8sClient, DefaultNamespace,
DefaultLabelValue,
)
err := r.Reflect(ctx, []Mapping{
{
VaultPath: "secrets/data/foo",
SecretName: "foo",
VaultEngineType: vault.EngineType("unsupported"),
},
})
if err == nil {
t.Fatal("expected error from unsupported engine type")
}
}