-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathvsphere_test.go
591 lines (546 loc) · 17.1 KB
/
vsphere_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
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vsphere
import (
"context"
"crypto/tls"
"log"
"os"
"testing"
lookup "github.com/vmware/govmomi/lookup/simulator"
"github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/simulator/vpx"
sts "github.com/vmware/govmomi/sts/simulator"
vapi "github.com/vmware/govmomi/vapi/simulator"
ccfg "k8s.io/cloud-provider-vsphere/pkg/cloudprovider/vsphere/config"
vcfg "k8s.io/cloud-provider-vsphere/pkg/common/config"
cm "k8s.io/cloud-provider-vsphere/pkg/common/connectionmanager"
"k8s.io/cloud-provider-vsphere/pkg/common/vclib"
)
// localhostCert was generated from crypto/tls/generate_cert.go with the following command:
// go run generate_cert.go --rsa-bits 512 --host 127.0.0.1,::1,example.com --ca --start-date "Jan 1 00:00:00 1970" --duration=1000000h
var localhostCert = `-----BEGIN CERTIFICATE-----
MIIBjzCCATmgAwIBAgIRAKpi2WmTcFrVjxrl5n5YDUEwDQYJKoZIhvcNAQELBQAw
EjEQMA4GA1UEChMHQWNtZSBDbzAgFw03MDAxMDEwMDAwMDBaGA8yMDg0MDEyOTE2
MDAwMFowEjEQMA4GA1UEChMHQWNtZSBDbzBcMA0GCSqGSIb3DQEBAQUAA0sAMEgC
QQC9fEbRszP3t14Gr4oahV7zFObBI4TfA5i7YnlMXeLinb7MnvT4bkfOJzE6zktn
59zP7UiHs3l4YOuqrjiwM413AgMBAAGjaDBmMA4GA1UdDwEB/wQEAwICpDATBgNV
HSUEDDAKBggrBgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MC4GA1UdEQQnMCWCC2V4
YW1wbGUuY29thwR/AAABhxAAAAAAAAAAAAAAAAAAAAABMA0GCSqGSIb3DQEBCwUA
A0EAUsVE6KMnza/ZbodLlyeMzdo7EM/5nb5ywyOxgIOCf0OOLHsPS9ueGLQX9HEG
//yjTXuhNcUugExIjM/AIwAZPQ==
-----END CERTIFICATE-----`
// localhostKey is the private key for localhostCert.
var localhostKey = `-----BEGIN RSA PRIVATE KEY-----
MIIBOwIBAAJBAL18RtGzM/e3XgavihqFXvMU5sEjhN8DmLtieUxd4uKdvsye9Phu
R84nMTrOS2fn3M/tSIezeXhg66quOLAzjXcCAwEAAQJBAKcRxH9wuglYLBdI/0OT
BLzfWPZCEw1vZmMR2FF1Fm8nkNOVDPleeVGTWoOEcYYlQbpTmkGSxJ6ya+hqRi6x
goECIQDx3+X49fwpL6B5qpJIJMyZBSCuMhH4B7JevhGGFENi3wIhAMiNJN5Q3UkL
IuSvv03kaPR5XVQ99/UeEetUgGvBcABpAiBJSBzVITIVCGkGc7d+RCf49KTCIklv
bGWObufAR8Ni4QIgWpILjW8dkGg8GOUZ0zaNA6Nvt6TIv2UWGJ4v5PoV98kCIQDx
rIiZs5QbKdycsv9gQJzwQAogC8o04X3Zz3dsoX+h4A==
-----END RSA PRIVATE KEY-----`
// configFromSim starts a vcsim instance and returns config for use against the vcsim instance.
// The vcsim instance is configured with an empty tls.Config.
func configFromSim(multiDc bool) (*vcfg.Config, func()) {
return configFromSimWithTLS(new(tls.Config), true, multiDc)
}
// configFromSimWithTLS starts a vcsim instance and returns config for use against the vcsim instance.
// The vcsim instance is configured with a tls.Config. The returned client
// config can be configured to allow/decline insecure connections.
func configFromSimWithTLS(tlsConfig *tls.Config, insecureAllowed bool, multiDc bool) (*vcfg.Config, func()) {
cfg := &vcfg.Config{}
model := simulator.VPX()
if multiDc {
model.Datacenter = 2
model.Datastore = 1
model.Cluster = 1
model.Host = 0
}
err := model.Create()
if err != nil {
log.Fatal(err)
}
model.Service.TLS = tlsConfig
s := model.Service.NewServer()
// STS simulator
path, handler := sts.New(s.URL, vpx.Setting)
model.Service.ServeMux.Handle(path, handler)
// vAPI simulator
path, handler = vapi.New(s.URL, nil)
model.Service.ServeMux.Handle(path, handler)
// Lookup Service simulator
model.Service.RegisterSDK(lookup.New())
cfg.Global.InsecureFlag = insecureAllowed
cfg.Global.VCenterIP = s.URL.Hostname()
cfg.Global.VCenterPort = s.URL.Port()
cfg.Global.User = s.URL.User.Username()
cfg.Global.Password, _ = s.URL.User.Password()
if multiDc {
cfg.Global.Datacenters = "DC0,DC1"
} else {
cfg.Global.Datacenters = vclib.TestDefaultDatacenter
}
cfg.VirtualCenter = make(map[string]*vcfg.VirtualCenterConfig)
cfg.VirtualCenter[s.URL.Hostname()] = &vcfg.VirtualCenterConfig{
User: cfg.Global.User,
Password: cfg.Global.Password,
TenantRef: cfg.Global.VCenterIP,
VCenterIP: cfg.Global.VCenterIP,
VCenterPort: cfg.Global.VCenterPort,
InsecureFlag: cfg.Global.InsecureFlag,
Datacenters: cfg.Global.Datacenters,
IPFamilyPriority: []string{"ipv4"},
}
// Configure region and zone categories
cfg.Labels.Region = "k8s-region"
cfg.Labels.Zone = "k8s-zone"
return cfg, func() {
s.Close()
model.Remove()
}
}
// configFromEnvOrSim builds a config from configFromSim and overrides using configFromEnv
func configFromEnvOrSim(multiDc bool) (*vcfg.Config, func()) {
cfg, fin := configFromSim(multiDc)
if err := cfg.FromEnv(); err != nil {
return nil, nil
}
return cfg, fin
}
func TestNewVSphere(t *testing.T) {
cfg := &ccfg.CPIConfig{}
if err := cfg.FromCPIEnv(); err != nil {
t.Skipf("No config found in environment")
}
_, err := newVSphere(cfg, nil)
if err != nil {
t.Fatalf("Failed to construct/authenticate vSphere: %s", err)
}
}
func TestVSphereLogin(t *testing.T) {
initCfg, cleanup := configFromEnvOrSim(false)
defer cleanup()
cfg := &ccfg.CPIConfig{}
cfg.Config = *initCfg
// Create vSphere configuration object
vs, err := newVSphere(cfg, nil)
if err != nil {
t.Fatalf("Failed to construct/authenticate vSphere: %s", err)
}
vs.connectionManager = cm.NewConnectionManager(&cfg.Config, nil, nil)
defer vs.connectionManager.Logout()
// Create context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Create vSphere client
vcInstance, ok := vs.connectionManager.VsphereInstanceMap[cfg.Global.VCenterIP]
if !ok {
t.Fatalf("Couldn't get vSphere instance: %s", cfg.Global.VCenterIP)
}
err = vcInstance.Conn.Connect(ctx)
if err != nil {
t.Errorf("Failed to connect to vSphere: %s", err)
}
vcInstance.Conn.Logout(ctx)
}
func TestVSphereLoginByToken(t *testing.T) {
initCfg, cleanup := configFromEnvOrSim(false)
defer cleanup()
cfg := &ccfg.CPIConfig{}
cfg.Config = *initCfg
// Configure for SAML token auth
cfg.Global.User = localhostCert
cfg.Global.Password = localhostKey
// Create vSphere configuration object
vs, err := newVSphere(cfg, nil)
if err != nil {
t.Fatalf("Failed to construct/authenticate vSphere: %s", err)
}
vs.connectionManager = cm.NewConnectionManager(&cfg.Config, nil, nil)
ctx := context.Background()
// Create vSphere client
vcInstance, ok := vs.connectionManager.VsphereInstanceMap[cfg.Global.VCenterIP]
if !ok {
t.Fatalf("Couldn't get vSphere instance: %s", cfg.Global.VCenterIP)
}
err = vcInstance.Conn.Connect(ctx)
if err != nil {
t.Errorf("Failed to connect to vSphere: %s", err)
}
vcInstance.Conn.Logout(ctx)
}
func TestAlphaDualStackConfig(t *testing.T) {
var testCases = []struct {
testName string
conf string
enableDualStackFeature bool
expectedError string
}{
{
testName: "Verifying dual stack env var required when providing two ip families",
conf: `[Global]
user = user
password = password
datacenters = us-west
[VirtualCenter "0.0.0.0"]
user = user
password = password
ip-family = ipv6,ipv4
`,
enableDualStackFeature: false,
expectedError: "two IP families provided, but dual stack feature is not enabled",
},
{
testName: "Verifying dual stack env var existing when providing two ip families",
conf: `[Global]
user = user
password = password
datacenters = us-west
[VirtualCenter "0.0.0.0"]
user = user
password = password
ip-family = ipv6,ipv4
`,
enableDualStackFeature: true,
expectedError: "",
},
{
testName: "Dual stack env var not required when providing single ip family",
conf: `[Global]
user = user
password = password
datacenters = us-west
[VirtualCenter "0.0.0.0"]
user = user
password = password
ip-family = ipv6
`,
enableDualStackFeature: false,
expectedError: "",
},
}
for _, testcase := range testCases {
t.Run(testcase.testName, func(t *testing.T) {
cfg, err := ccfg.ReadCPIConfig([]byte(testcase.conf))
if err != nil {
if testcase.expectedError != "" {
if err.Error() != testcase.expectedError {
t.Fatalf("readConfig: expected err: %s, received err: %s", testcase.expectedError, err)
}
} else {
t.Fatalf("readConfig: unexpected error returned: %v", err)
}
}
if testcase.enableDualStackFeature {
err := os.Setenv("ENABLE_ALPHA_DUAL_STACK", "1")
if err != nil {
t.Fatalf("Received error %s when setting env var ENABLE_ALPHA_DUAL_STACK", err)
}
defer func() {
err := os.Unsetenv("ENABLE_ALPHA_DUAL_STACK")
if err != nil {
t.Fatalf("Received error %s when unsetting env var", err)
}
}()
}
_, err = buildVSphereFromConfig(cfg, nil)
if err != nil {
if testcase.expectedError != "" {
if err.Error() != testcase.expectedError {
t.Fatalf("buildVSphereFromConfig: expected err: %s, receiver err: %s", testcase.expectedError, err)
}
} else {
t.Fatalf("buildVSphereFromConfig: Should succeed when a valid config is provided: %v", err)
}
}
})
}
}
func TestSecretVSphereConfig(t *testing.T) {
var vs *VSphere
var (
username = "user"
password = "password"
)
var testcases = []struct {
testName string
conf string
expectedIsSecretProvided bool
expectedUsername string
expectedPassword string
expectedError error
expectedThumbprints map[string]string
}{
{
testName: "Username and password with old configuration",
conf: `[Global]
server = 0.0.0.0
user = user
password = password
datacenters = us-west
`,
expectedUsername: username,
expectedPassword: password,
expectedError: nil,
},
{
testName: "SecretName and SecretNamespace in old configuration",
conf: `[Global]
server = 0.0.0.0
datacenters = us-west
secret-name = "vccreds"
secret-namespace = "kube-system"
`,
expectedIsSecretProvided: true,
expectedError: nil,
},
{
testName: "SecretName and SecretNamespace with Username and Password in old configuration",
conf: `[Global]
server = 0.0.0.0
user = user
password = password
datacenters = us-west
secret-name = "vccreds"
secret-namespace = "kube-system"
`,
expectedIsSecretProvided: true,
expectedError: nil,
},
{
testName: "SecretName and SecretNamespace with Username missing in old configuration",
conf: `[Global]
server = 0.0.0.0
password = password
datacenters = us-west
secret-name = "vccreds"
secret-namespace = "kube-system"
`,
expectedIsSecretProvided: true,
expectedError: nil,
},
{
testName: "SecretNamespace missing with Username and Password in old configuration",
conf: `[Global]
server = 0.0.0.0
user = user
password = password
datacenters = us-west
secret-name = "vccreds"
`,
expectedUsername: username,
expectedPassword: password,
expectedError: nil,
},
{
testName: "SecretNamespace and Username missing in old configuration",
conf: `[Global]
server = 0.0.0.0
password = password
datacenters = us-west
secret-name = "vccreds"
`,
expectedPassword: password,
expectedError: vcfg.ErrUsernameMissing,
},
{
testName: "SecretNamespace and Password missing in old configuration",
conf: `[Global]
server = 0.0.0.0
user = user
datacenters = us-west
secret-name = "vccreds"
`,
expectedUsername: username,
expectedError: vcfg.ErrPasswordMissing,
},
{
testName: "SecretNamespace, Username and Password missing in old configuration",
conf: `[Global]
server = 0.0.0.0
datacenters = us-west
secret-name = "vccreds"
`,
expectedError: vcfg.ErrUsernameMissing,
},
{
testName: "Username and password with new configuration but username and password in global section",
conf: `[Global]
user = user
password = password
datacenters = us-west
[VirtualCenter "0.0.0.0"]
`,
expectedUsername: username,
expectedPassword: password,
expectedError: nil,
},
{
testName: "Username and password with new configuration, username and password in virtualcenter section",
conf: `[Global]
server = 0.0.0.0
port = 443
insecure-flag = true
datacenters = us-west
[VirtualCenter "0.0.0.0"]
user = user
password = password
`,
expectedUsername: username,
expectedPassword: password,
expectedError: nil,
},
{
testName: "SecretName and SecretNamespace with new configuration",
conf: `[Global]
server = 0.0.0.0
secret-name = "vccreds"
secret-namespace = "kube-system"
datacenters = us-west
[VirtualCenter "0.0.0.0"]
`,
expectedIsSecretProvided: true,
expectedError: nil,
},
{
testName: "SecretName and SecretNamespace with Username missing in new configuration",
conf: `[Global]
server = 0.0.0.0
port = 443
insecure-flag = true
datacenters = us-west
secret-name = "vccreds"
secret-namespace = "kube-system"
[VirtualCenter "0.0.0.0"]
password = password
`,
expectedIsSecretProvided: true,
expectedError: nil,
},
{
testName: "virtual centers with a thumbprint",
conf: `[Global]
server = global
user = user
password = password
datacenters = us-west
thumbprint = "thumbprint:global"
`,
expectedUsername: username,
expectedPassword: password,
expectedError: nil,
expectedThumbprints: map[string]string{
"global": "thumbprint:global",
},
},
{
testName: "Multiple virtual centers with different thumbprints",
conf: `[Global]
user = user
password = password
datacenters = us-west
[VirtualCenter "0.0.0.0"]
thumbprint = thumbprint:0
[VirtualCenter "no_thumbprint"]
[VirtualCenter "1.1.1.1"]
thumbprint = thumbprint:1
`,
expectedUsername: username,
expectedPassword: password,
expectedError: nil,
expectedThumbprints: map[string]string{
"0.0.0.0": "thumbprint:0",
"1.1.1.1": "thumbprint:1",
},
},
{
testName: "Multiple virtual centers use the global CA cert",
conf: `[Global]
user = user
password = password
datacenters = us-west
ca-file = /some/path/to/my/trusted/ca.pem
[VirtualCenter "0.0.0.0"]
user = user
password = password
[VirtualCenter "1.1.1.1"]
user = user
password = password
`,
expectedUsername: username,
expectedPassword: password,
expectedError: nil,
},
}
for _, testcase := range testcases {
t.Logf("Executing Testcase: %s", testcase.testName)
cfg, err := ccfg.ReadCPIConfig([]byte(testcase.conf))
if err != nil {
if testcase.expectedError != nil {
if err != testcase.expectedError {
t.Fatalf("readConfig: expected err: %s, received err: %s", testcase.expectedError, err)
} else {
continue
}
} else {
t.Fatalf("readConfig: unexpected error returned: %v", err)
}
}
vs, err = buildVSphereFromConfig(cfg, nil)
if err != nil { // testcase.expectedError {
t.Fatalf("buildVSphereFromConfig: Should succeed when a valid config is provided: %v", err)
}
vs.connectionManager = cm.NewConnectionManager(&cfg.Config, nil, nil)
if testcase.expectedIsSecretProvided && (vs.cfg.Global.SecretNamespace == "" || vs.cfg.Global.SecretName == "") {
t.Fatalf("SecretName and SecretNamespace was expected in config %s. error: %s",
testcase.conf, err)
}
if !testcase.expectedIsSecretProvided {
for _, vsInstance := range vs.connectionManager.VsphereInstanceMap {
if vsInstance.Conn.Username != testcase.expectedUsername {
t.Fatalf("Expected username %s doesn't match actual username %s in config %s. error: %s",
testcase.expectedUsername, vsInstance.Conn.Username, testcase.conf, err)
}
if vsInstance.Conn.Password != testcase.expectedPassword {
t.Fatalf("Expected password %s doesn't match actual password %s in config %s. error: %s",
testcase.expectedPassword, vsInstance.Conn.Password, testcase.conf, err)
}
}
}
// Check, if all the expected thumbprints are configured
for instanceName, expectedThumbprint := range testcase.expectedThumbprints {
instanceConfig, ok := vs.connectionManager.VsphereInstanceMap[instanceName]
if !ok {
t.Fatalf("Could not find configuration for instance %s", instanceName)
}
if actualThumbprint := instanceConfig.Conn.Thumbprint; actualThumbprint != expectedThumbprint {
t.Fatalf(
"Expected thumbprint for instance '%s' to be '%s', got '%s'",
instanceName, expectedThumbprint, actualThumbprint,
)
}
}
// Check, if all connections are configured with the global CA certificate
if expectedCaPath := cfg.Global.CAFile; expectedCaPath != "" {
for name, instance := range vs.connectionManager.VsphereInstanceMap {
if actualCaPath := instance.Conn.CACert; actualCaPath != expectedCaPath {
t.Fatalf(
"Expected CA certificate path for instance '%s' to be the globally configured one ('%s'), got '%s'",
name, expectedCaPath, actualCaPath,
)
}
}
}
}
}