forked from hashicorp/vault-plugin-auth-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_config_test.go
335 lines (284 loc) · 9.49 KB
/
path_config_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
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
package kubeauth
import (
"context"
"reflect"
"testing"
"github.com/hashicorp/vault/sdk/logical"
)
func TestConfig_Read(t *testing.T) {
b, storage := getBackend(t)
data := map[string]interface{}{
"pem_keys": []string{testRSACert, testECCert},
"kubernetes_host": "host",
"kubernetes_ca_cert": testCACert,
}
req := &logical.Request{
Operation: logical.CreateOperation,
Path: configPath,
Storage: storage,
Data: data,
}
resp, err := b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}
req = &logical.Request{
Operation: logical.ReadOperation,
Path: configPath,
Storage: storage,
Data: nil,
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}
if !reflect.DeepEqual(resp.Data, data) {
t.Fatalf("Expected did not equal actual: expected %#v\n got %#v\n", data, resp.Data)
}
}
func TestConfig(t *testing.T) {
b, storage := getBackend(t)
// test no certificate
data := map[string]interface{}{
"kubernetes_host": "host",
}
req := &logical.Request{
Operation: logical.CreateOperation,
Path: configPath,
Storage: storage,
Data: data,
}
resp, err := b.HandleRequest(context.Background(), req)
if resp == nil || !resp.IsError() {
t.Fatal("expected error")
}
if resp.Error().Error() != "one of pem_keys or kubernetes_ca_cert must be set" {
t.Fatalf("got unexpected error: %v", resp.Error())
}
// test no host
data = map[string]interface{}{
"pem_keys": testRSACert,
}
req = &logical.Request{
Operation: logical.CreateOperation,
Path: configPath,
Storage: storage,
Data: data,
}
resp, err = b.HandleRequest(context.Background(), req)
if resp == nil || !resp.IsError() {
t.Fatal("expected error")
}
if resp.Error().Error() != "no host provided" {
t.Fatalf("got unexpected error: %v", resp.Error())
}
// test invalid cert
data = map[string]interface{}{
"pem_keys": "bad",
"kubernetes_host": "host",
}
req = &logical.Request{
Operation: logical.CreateOperation,
Path: configPath,
Storage: storage,
Data: data,
}
resp, err = b.HandleRequest(context.Background(), req)
if resp == nil || !resp.IsError() {
t.Fatal("expected error")
}
if resp.Error().Error() != "data does not contain any valid RSA or ECDSA public keys" {
t.Fatalf("got unexpected error: %v", resp.Error())
}
// Test success with no certs
data = map[string]interface{}{
"kubernetes_host": "host",
"kubernetes_ca_cert": testCACert,
}
req = &logical.Request{
Operation: logical.CreateOperation,
Path: configPath,
Storage: storage,
Data: data,
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}
cert, err := parsePublicKeyPEM([]byte(testRSACert))
if err != nil {
t.Fatal(err)
}
expected := &kubeConfig{
PublicKeys: []interface{}{},
PEMKeys: []string{},
Host: "host",
CACert: testCACert,
}
conf, err := b.(*kubeAuthBackend).config(context.Background(), storage)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, conf) {
t.Fatalf("expected did not match actual: expected %#v\n got %#v\n", expected, conf)
}
// Test success TokenReviewer
data = map[string]interface{}{
"kubernetes_host": "host",
"kubernetes_ca_cert": testCACert,
"token_reviewer_jwt": jwtData,
}
req = &logical.Request{
Operation: logical.CreateOperation,
Path: configPath,
Storage: storage,
Data: data,
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}
cert, err = parsePublicKeyPEM([]byte(testRSACert))
if err != nil {
t.Fatal(err)
}
expected = &kubeConfig{
PublicKeys: []interface{}{},
PEMKeys: []string{},
Host: "host",
CACert: testCACert,
TokenReviewerJWT: jwtData,
}
conf, err = b.(*kubeAuthBackend).config(context.Background(), storage)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, conf) {
t.Fatalf("expected did not match actual: expected %#v\n got %#v\n", expected, conf)
}
// Test success with one cert
data = map[string]interface{}{
"pem_keys": testRSACert,
"kubernetes_host": "host",
"kubernetes_ca_cert": testCACert,
}
req = &logical.Request{
Operation: logical.CreateOperation,
Path: configPath,
Storage: storage,
Data: data,
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}
cert, err = parsePublicKeyPEM([]byte(testRSACert))
if err != nil {
t.Fatal(err)
}
expected = &kubeConfig{
PublicKeys: []interface{}{cert},
PEMKeys: []string{testRSACert},
Host: "host",
CACert: testCACert,
}
conf, err = b.(*kubeAuthBackend).config(context.Background(), storage)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, conf) {
t.Fatalf("expected did not match actual: expected %#v\n got %#v\n", expected, conf)
}
// Test success with two certs
data = map[string]interface{}{
"pem_keys": []string{testRSACert, testECCert},
"kubernetes_host": "host",
"kubernetes_ca_cert": testCACert,
}
req = &logical.Request{
Operation: logical.CreateOperation,
Path: configPath,
Storage: storage,
Data: data,
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}
cert, err = parsePublicKeyPEM([]byte(testRSACert))
if err != nil {
t.Fatal(err)
}
cert2, err := parsePublicKeyPEM([]byte(testECCert))
if err != nil {
t.Fatal(err)
}
expected = &kubeConfig{
PublicKeys: []interface{}{cert, cert2},
PEMKeys: []string{testRSACert, testECCert},
Host: "host",
CACert: testCACert,
}
conf, err = b.(*kubeAuthBackend).config(context.Background(), storage)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, conf) {
t.Fatalf("expected did not match actual: expected %#v\n got %#v\n", expected, conf)
}
}
var testRSACert string = `-----BEGIN CERTIFICATE-----
MIIDcjCCAlqgAwIBAgIBAjANBgkqhkiG9w0BAQsFADAVMRMwEQYDVQQDEwptaW5p
a3ViZUNBMB4XDTE3MDgzMDE5MDgzNloXDTE4MDgzMDE5MDgzNlowLDEXMBUGA1UE
ChMOc3lzdGVtOm1hc3RlcnMxETAPBgNVBAMTCG1pbmlrdWJlMIIBIjANBgkqhkiG
9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxD3eM3+WNc4phxAeQxNOmcybKlNJWowuC12u
v+cGJWxxpDx/OoEIxKI5wmgHxEwFCZL545sjfLqyBcgxQR2xSCib+bYzjBtfA6uV
6d/35nurzz21okcMffc5xKMyZhEwt98WAvYWD71Bihz7iGBq5Sw9md6pqnkNoScR
Hhi3Vl94a6D6shwb6nXA2hlwYLcnoKtpe3Ptq6MW6CpfBA8C11q5eeW4xdvrwKt3
Vd1TgFeEnnqwzUWGapU2uwwUfbRkLTDvrp6791uq0Vo7mzz00xYhV1PLCeA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XcCAwEAAaNCMEAwDgYDVR0PAQH/BAQDAgKkMB0GA1UdJQQW
MBQGCCsGAQUFBwMCBggrBgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3
DQEBCwUAA4IBAQCSkrhE1PczqeqXfRaWayJUbXWPwKFbszO0MhGB1zwnPZq39qjY
ySQiGvnjV3fP+N5CTQAwMNe79Xiw31fSoexgceCPJpraWrTOLdCv04SbGDBapMFM
aezBu9jzZm0CNt60jHXWXuHHVPFX6u7ZR8W+RiBvsT8GZ5U6sNs3aN3M9Vym06BL
aSphIw1v+hRlPfnrlJwUnQp158DRgkt/9ncTa/k88KoIoZAbulaiGB4zHxxkbura
GSlgpZzhHSrBDLuXf65GHwwGxSExhgY5AA/n8rumGVvE8IYohS9yg/jOG0xP2WQH
u/ABoYtOyseO+lgElA8R4PB9MtwgN6c/b0xH
-----END CERTIFICATE-----`