Skip to content

Commit 906d5d1

Browse files
Merge pull request #8276 from jcpowermac/capv-no-auth
SPLAT-1585: capv - allow no auth to vcenter
2 parents 9938156 + f76a5f0 commit 906d5d1

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

pkg/asset/installconfig/vsphere/metadata.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package vsphere
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"path"
8+
"strings"
79
"sync"
810

911
"github.com/vmware/govmomi/object"
12+
"github.com/vmware/govmomi/vim25/soap"
1013
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/session"
1114

1215
"github.com/openshift/installer/pkg/types/vsphere"
@@ -122,10 +125,33 @@ func (m *Metadata) unlockedSession(ctx context.Context, server string) (*session
122125
return m.sessions[server], err
123126
}
124127

128+
// unwrapToSoapFault is required because soapErrorFaul is not exported
129+
// and are unable to use errors.As()
130+
// https://github.com/vmware/govmomi/blob/main/vim25/soap/error.go#L38
131+
func unwrapToSoapFault(err error) error {
132+
if err != nil {
133+
if soapFault := soap.IsSoapFault(err); !soapFault {
134+
return unwrapToSoapFault(errors.Unwrap(err))
135+
}
136+
return err
137+
}
138+
return err
139+
}
140+
125141
// Networks populates VCenterContext and the ClusterNetworkMap based on the vCenter server url and the FailureDomains.
126142
func (m *Metadata) Networks(ctx context.Context, vcenter vsphere.VCenter, failureDomains []vsphere.FailureDomain) error {
127143
_, err := m.Session(ctx, vcenter.Server)
128144
if err != nil {
145+
// Defense against potential issues with assisted installer
146+
if soapErr := unwrapToSoapFault(err); soapErr != nil {
147+
soapFault := soap.ToSoapFault(soapErr)
148+
// The assisted installer provides bogus username and password
149+
// values. Only return the soap error (fault) if it matches incorrect username or password.
150+
if strings.Contains(soapFault.String, "Cannot complete login due to an incorrect user name or password") {
151+
return soapErr
152+
}
153+
}
154+
// if soapErr is nil then this is not a SOAP fault, return err
129155
return err
130156
}
131157

pkg/asset/machines/clusterapi.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package machines
33
import (
44
"context"
55
"fmt"
6+
"net"
67
"path/filepath"
78
"strings"
9+
"time"
810

911
"github.com/pkg/errors"
1012
"github.com/sirupsen/logrus"
13+
"github.com/vmware/govmomi/vim25/soap"
1114
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1215
"k8s.io/utils/ptr"
1316
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -303,10 +306,41 @@ func (c *ClusterAPI) Generate(dependencies asset.Parents) error {
303306
mpool.Set(pool.Platform.VSphere)
304307

305308
platform := ic.VSphere
309+
resolver := &net.Resolver{
310+
PreferGo: true,
311+
}
306312

307313
for _, v := range platform.VCenters {
308-
err := installConfig.VSphere.Networks(ctx, v, platform.FailureDomains)
314+
// Defense against potential issues with assisted installer
315+
// If the installer is unable to resolve vCenter there is a good possibility
316+
// that the installer's install-config has been provided with bogus values.
317+
318+
// Timeout context for Lookup
319+
ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
320+
defer cancel()
321+
322+
_, err := resolver.LookupHost(ctx, v.Server)
323+
if err != nil {
324+
logrus.Warnf("unable to resolve vSphere server %s", v.Server)
325+
return nil
326+
}
327+
328+
// Timeout context for Networks
329+
// vCenter APIs can be unreliable in performance, extended this context
330+
// timeout to 60 seconds.
331+
ctx, cancel = context.WithTimeout(context.TODO(), 60*time.Second)
332+
defer cancel()
333+
334+
err = installConfig.VSphere.Networks(ctx, v, platform.FailureDomains)
309335
if err != nil {
336+
// If we are receiving an error as a Soap Fault this is caused by
337+
// incorrect credentials and in the scenario of assisted installer
338+
// the credentials are never valid. Since vCenter hostname is
339+
// incorrect as well we shouldn't get this far.
340+
if soap.IsSoapFault(err) {
341+
logrus.Warn("authentication failure to vCenter, Cluster API machine manifests not created, cluster may not install")
342+
return nil
343+
}
310344
return err
311345
}
312346
}

0 commit comments

Comments
 (0)